bjope created this revision. bjope requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
In CGExprConstant.cpp, when dealing with padding etc, some sizes are calculated as char units. But then the type used when creating the aggregate expression was hardcoded to Int8Ty. Since a char normally is eight bits this usually is fine, but it seems a bit inconsistent to use the size of a char unit when requesting the size of the aggregate, but then ending up not taking getCharWidth() into consideration when selecting the type for the aggregate. This patch honors the size of a char unit by using getCharWidth() together with getIntNTy instead of hardcoding the type to Int8Ty. This can be considered as NFC, as getCharWidth always return 8 and can't be configured (at least not in-tree). But it makes the code a bit more consistent, and might be helpful for out-of-tree targets that has different char widths. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D94979 Files: clang/lib/CodeGen/CGExprConstant.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/CodeGenModule.h Index: clang/lib/CodeGen/CodeGenModule.h =================================================================== --- clang/lib/CodeGen/CodeGenModule.h +++ clang/lib/CodeGen/CodeGenModule.h @@ -1225,6 +1225,9 @@ /// Return the store size, in character units, of the given LLVM type. CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; + // Return the LLVM type sized as one character unit. + llvm::Type *getCharSizedType(); + /// Returns LLVM linkage for a declarator. llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage, Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -3982,6 +3982,11 @@ getDataLayout().getTypeStoreSizeInBits(Ty)); } +llvm::Type *CodeGenModule::getCharSizedType() { + return llvm::Type::getIntNTy(getLLVMContext(), + getContext().getCharWidth()); +} + LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { LangAS AddrSpace = LangAS::Default; if (LangOpts.OpenCL) { Index: clang/lib/CodeGen/CGExprConstant.cpp =================================================================== --- clang/lib/CodeGen/CGExprConstant.cpp +++ clang/lib/CodeGen/CGExprConstant.cpp @@ -58,14 +58,15 @@ } llvm::Constant *getPadding(CharUnits PadSize) const { - llvm::Type *Ty = CGM.Int8Ty; + llvm::Type *Ty = CGM.getCharSizedType(); if (PadSize > CharUnits::One()) Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); return llvm::UndefValue::get(Ty); } llvm::Constant *getZeroes(CharUnits ZeroSize) const { - llvm::Type *Ty = llvm::ArrayType::get(CGM.Int8Ty, ZeroSize.getQuantity()); + llvm::Type *Ty = llvm::ArrayType::get(CGM.getCharSizedType(), + ZeroSize.getQuantity()); return llvm::ConstantAggregateZero::get(Ty); } }; @@ -1069,7 +1070,7 @@ assert(CurSize <= TotalSize && "Union size mismatch!"); if (unsigned NumPadBytes = TotalSize - CurSize) { - llvm::Type *Ty = CGM.Int8Ty; + llvm::Type *Ty = CGM.getCharSizedType(); if (NumPadBytes > 1) Ty = llvm::ArrayType::get(Ty, NumPadBytes);
Index: clang/lib/CodeGen/CodeGenModule.h =================================================================== --- clang/lib/CodeGen/CodeGenModule.h +++ clang/lib/CodeGen/CodeGenModule.h @@ -1225,6 +1225,9 @@ /// Return the store size, in character units, of the given LLVM type. CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const; + // Return the LLVM type sized as one character unit. + llvm::Type *getCharSizedType(); + /// Returns LLVM linkage for a declarator. llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage, Index: clang/lib/CodeGen/CodeGenModule.cpp =================================================================== --- clang/lib/CodeGen/CodeGenModule.cpp +++ clang/lib/CodeGen/CodeGenModule.cpp @@ -3982,6 +3982,11 @@ getDataLayout().getTypeStoreSizeInBits(Ty)); } +llvm::Type *CodeGenModule::getCharSizedType() { + return llvm::Type::getIntNTy(getLLVMContext(), + getContext().getCharWidth()); +} + LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { LangAS AddrSpace = LangAS::Default; if (LangOpts.OpenCL) { Index: clang/lib/CodeGen/CGExprConstant.cpp =================================================================== --- clang/lib/CodeGen/CGExprConstant.cpp +++ clang/lib/CodeGen/CGExprConstant.cpp @@ -58,14 +58,15 @@ } llvm::Constant *getPadding(CharUnits PadSize) const { - llvm::Type *Ty = CGM.Int8Ty; + llvm::Type *Ty = CGM.getCharSizedType(); if (PadSize > CharUnits::One()) Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); return llvm::UndefValue::get(Ty); } llvm::Constant *getZeroes(CharUnits ZeroSize) const { - llvm::Type *Ty = llvm::ArrayType::get(CGM.Int8Ty, ZeroSize.getQuantity()); + llvm::Type *Ty = llvm::ArrayType::get(CGM.getCharSizedType(), + ZeroSize.getQuantity()); return llvm::ConstantAggregateZero::get(Ty); } }; @@ -1069,7 +1070,7 @@ assert(CurSize <= TotalSize && "Union size mismatch!"); if (unsigned NumPadBytes = TotalSize - CurSize) { - llvm::Type *Ty = CGM.Int8Ty; + llvm::Type *Ty = CGM.getCharSizedType(); if (NumPadBytes > 1) Ty = llvm::ArrayType::get(Ty, NumPadBytes);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits