[PATCH] D94979: [CGExpr] Honor getCharWidth() in ConstantAggregateBuilderUtils

Bjorn Pettersson via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 19 09:20:01 PST 2021


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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94979.317603.patch
Type: text/x-patch
Size: 2314 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210119/9d357e31/attachment.bin>


More information about the cfe-commits mailing list