[PATCH] D99592: [PoC][Clang] Use TypeSize instead of uint64_t for getTypeAllocSize().

Hsiangkai Wang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 30 06:57:15 PDT 2021


HsiangKai created this revision.
HsiangKai added reviewers: craig.topper, frasercrmck, rogfer01.
Herald added a subscriber: StephenFan.
HsiangKai requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The return type of getTypeAllocSize() is TypeSize. We do not rely on the implicit conversion to uint64_t. The return value may be scalable values.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99592

Files:
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CGRecordLayoutBuilder.cpp


Index: clang/lib/CodeGen/CGRecordLayoutBuilder.cpp
===================================================================
--- clang/lib/CodeGen/CGRecordLayoutBuilder.cpp
+++ clang/lib/CodeGen/CGRecordLayoutBuilder.cpp
@@ -159,7 +159,8 @@
     return Context.toCharUnitsFromBits(BitOffset);
   }
   CharUnits getSize(llvm::Type *Type) {
-    return CharUnits::fromQuantity(DataLayout.getTypeAllocSize(Type));
+    return CharUnits::fromQuantity(
+        DataLayout.getTypeAllocSize(Type).getKnownMinValue());
   }
   CharUnits getAlignment(llvm::Type *Type) {
     return CharUnits::fromQuantity(DataLayout.getABITypeAlignment(Type));
@@ -929,7 +930,8 @@
   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
 
   uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
-  assert(TypeSizeInBits == getDataLayout().getTypeAllocSizeInBits(Ty) &&
+  assert(TypeSizeInBits ==
+             getDataLayout().getTypeAllocSizeInBits(Ty).getKnownMinValue() &&
          "Type size mismatch!");
 
   if (BaseTy) {
@@ -938,9 +940,10 @@
     uint64_t AlignedNonVirtualTypeSizeInBits =
       getContext().toBits(NonVirtualSize);
 
-    assert(AlignedNonVirtualTypeSizeInBits ==
-           getDataLayout().getTypeAllocSizeInBits(BaseTy) &&
-           "Type size mismatch!");
+    assert(
+        AlignedNonVirtualTypeSizeInBits ==
+            getDataLayout().getTypeAllocSizeInBits(BaseTy).getKnownMinValue() &&
+        "Type size mismatch!");
   }
 
   // Verify that the LLVM and AST field offsets agree.
@@ -960,7 +963,8 @@
     // AST offset.
     if (!FD->isBitField()) {
       unsigned FieldNo = RL->getLLVMFieldNo(FD);
-      assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
+      assert(AST_RL.getFieldOffset(i) ==
+                 SL->getElementOffsetInBits(FieldNo).getKnownMinSize() &&
              "Invalid field offset!");
       continue;
     }
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -2848,12 +2848,12 @@
       llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
       if (ArgI.isDirect() && ArgI.getCanBeFlattened() && STy &&
           STy->getNumElements() > 1) {
-        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
+        llvm::TypeSize SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
         llvm::Type *DstTy = Ptr.getElementType();
-        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
+        llvm::TypeSize DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
 
         Address AddrToStoreInto = Address::invalid();
-        if (SrcSize <= DstSize) {
+        if (llvm::TypeSize::isKnownLE(SrcSize, DstSize)) {
           AddrToStoreInto = Builder.CreateElementBitCast(Ptr, STy);
         } else {
           AddrToStoreInto =
@@ -2878,7 +2878,7 @@
           }
         }
 
-        if (SrcSize > DstSize) {
+        if (llvm::TypeSize::isKnownGT(SrcSize, DstSize)) {
           Builder.CreateMemCpy(Ptr, AddrToStoreInto, DstSize);
         }
 
@@ -4915,18 +4915,18 @@
             dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType());
       if (STy && ArgInfo.isDirect() && ArgInfo.getCanBeFlattened()) {
         llvm::Type *SrcTy = Src.getElementType();
-        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
-        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
+        llvm::TypeSize SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
+        llvm::TypeSize DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
 
         // If the source type is smaller than the destination type of the
         // coerce-to logic, copy the source value into a temp alloca the size
         // of the destination type to allow loading all of it. The bits past
         // the source value are left undef.
-        if (SrcSize < DstSize) {
+        if (llvm::TypeSize::isKnownLT(SrcSize, DstSize)) {
           Address TempAlloca
             = CreateTempAlloca(STy, Src.getAlignment(),
                                Src.getName() + ".coerce");
-          Builder.CreateMemCpy(TempAlloca, Src, SrcSize);
+          Builder.CreateMemCpy(TempAlloca, Src, SrcSize.getKnownMinSize());
           Src = TempAlloca;
         } else {
           Src = Builder.CreateBitCast(Src,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99592.334147.patch
Type: text/x-patch
Size: 4385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210330/88136e95/attachment.bin>


More information about the cfe-commits mailing list