[llvm] 696f536 - [AsmPrinter] Fix bit pattern for i1 vectors.

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 6 12:57:34 PDT 2022


Author: Eli Friedman
Date: 2022-07-06T12:56:47-07:00
New Revision: 696f53665dc561ec56102bd11a000c7e7a6f3561

URL: https://github.com/llvm/llvm-project/commit/696f53665dc561ec56102bd11a000c7e7a6f3561
DIFF: https://github.com/llvm/llvm-project/commit/696f53665dc561ec56102bd11a000c7e7a6f3561.diff

LOG: [AsmPrinter] Fix bit pattern for i1 vectors.

Vectors are defined to be tightly packed, regardless of the element
type.  The AsmPrinter didn't realize this, and was allocating extra
padding.

Fixes https://github.com/llvm/llvm-project/issues/49286
Fixes https://github.com/llvm/llvm-project/issues/53246
Fixes https://github.com/llvm/llvm-project/issues/55522

Differential Revision: https://reviews.llvm.org/D129164

Added: 
    llvm/test/CodeGen/AArch64/vector-global-i1.ll

Modified: 
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 86293c7a4bf37..64ce270722c9e 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2995,17 +2995,42 @@ static void emitGlobalConstantArray(const DataLayout &DL,
   }
 }
 
+static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
+
 static void emitGlobalConstantVector(const DataLayout &DL,
                                      const ConstantVector *CV, AsmPrinter &AP,
                                      AsmPrinter::AliasMapTy *AliasList) {
-  for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
-    emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
-    emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
+  Type *ElementType = CV->getType()->getElementType();
+  uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
+  uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
+  uint64_t EmittedSize;
+  if (ElementSizeInBits != ElementAllocSizeInBits) {
+    // If the allocation size of an element is 
diff erent from the size in bits,
+    // printing each element separately will insert incorrect padding.
+    //
+    // The general algorithm here is complicated; instead of writing it out
+    // here, just use the existing code in ConstantFolding.
+    Type *IntT =
+        IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
+    ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
+        ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
+    if (!CI) {
+      report_fatal_error(
+          "Cannot lower vector global with unusual element type");
+    }
+    emitGlobalAliasInline(AP, 0, AliasList);
+    emitGlobalConstantLargeInt(CI, AP);
+    EmittedSize = DL.getTypeStoreSize(CV->getType());
+  } else {
+    for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
+      emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
+      emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
+    }
+    EmittedSize =
+        DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
   }
 
   unsigned Size = DL.getTypeAllocSize(CV->getType());
-  unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
-                         CV->getType()->getNumElements();
   if (unsigned Padding = Size - EmittedSize)
     AP.OutStreamer->emitZeros(Padding);
 }

diff  --git a/llvm/test/CodeGen/AArch64/vector-global-i1.ll b/llvm/test/CodeGen/AArch64/vector-global-i1.ll
new file mode 100644
index 0000000000000..c8d78dae84867
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/vector-global-i1.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple aarch64 | FileCheck %s
+
+; CHECK: a:
+; CHECK-NEXT: .zero 1
+ at a = internal constant <4 x i1> <i1 false, i1 false, i1 false, i1 false>
+; CHECK: b:
+; CHECK-NEXT: .byte 5
+ at b = internal constant <4 x i1> <i1 true, i1 false, i1 true, i1 false>
+; CHECK: c:
+; CHECK-NEXT: .hword 1
+; CHECK-NEXT: .byte 0
+; CHECK-NEXT: .zero 1
+ at c = internal constant <24 x i1> <i1 true, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false, i1 false>
+


        


More information about the llvm-commits mailing list