[PATCH] D129164: [AsmPrinter] Fix bit pattern for i1 vectors.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 5 15:30:14 PDT 2022


efriedma created this revision.
efriedma added reviewers: craig.topper, dmgreen, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
efriedma requested review of this revision.
Herald added a project: LLVM.

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

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129164

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/AArch64/vector-global-i1.ll


Index: llvm/test/CodeGen/AArch64/vector-global-i1.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/vector-global-i1.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s -mtriple aarch64 | FileCheck --check-prefixes=CHECK %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>
+
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2973,14 +2973,38 @@
   }
 }
 
+static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
+
 static void emitGlobalConstantVector(const DataLayout &DL,
                                      const ConstantVector *CV, AsmPrinter &AP) {
-  for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
-    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 different 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");
+    }
+    emitGlobalConstantLargeInt(CI, AP);
+    EmittedSize = DL.getTypeStoreSize(CV->getType());
+  } else {
+    for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
+      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);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129164.442404.patch
Type: text/x-patch
Size: 3035 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220705/c5e1a503/attachment.bin>


More information about the llvm-commits mailing list