Skip to content

Commit 1f623c0

Browse files
efriedma-quicmemfrob
authored and
memfrob
committed
[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 llvm/llvm-project#49286 Fixes llvm/llvm-project#53246 Fixes llvm/llvm-project#55522 Differential Revision: https://reviews.llvm.org/D129164
1 parent b6834af commit 1f623c0

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2995,17 +2995,42 @@ static void emitGlobalConstantArray(const DataLayout &DL,
29952995
}
29962996
}
29972997

2998+
static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP);
2999+
29983000
static void emitGlobalConstantVector(const DataLayout &DL,
29993001
const ConstantVector *CV, AsmPrinter &AP,
30003002
AsmPrinter::AliasMapTy *AliasList) {
3001-
for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
3002-
emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
3003-
emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
3003+
Type *ElementType = CV->getType()->getElementType();
3004+
uint64_t ElementSizeInBits = DL.getTypeSizeInBits(ElementType);
3005+
uint64_t ElementAllocSizeInBits = DL.getTypeAllocSizeInBits(ElementType);
3006+
uint64_t EmittedSize;
3007+
if (ElementSizeInBits != ElementAllocSizeInBits) {
3008+
// If the allocation size of an element is different from the size in bits,
3009+
// printing each element separately will insert incorrect padding.
3010+
//
3011+
// The general algorithm here is complicated; instead of writing it out
3012+
// here, just use the existing code in ConstantFolding.
3013+
Type *IntT =
3014+
IntegerType::get(CV->getContext(), DL.getTypeSizeInBits(CV->getType()));
3015+
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(ConstantFoldConstant(
3016+
ConstantExpr::getBitCast(const_cast<ConstantVector *>(CV), IntT), DL));
3017+
if (!CI) {
3018+
report_fatal_error(
3019+
"Cannot lower vector global with unusual element type");
3020+
}
3021+
emitGlobalAliasInline(AP, 0, AliasList);
3022+
emitGlobalConstantLargeInt(CI, AP);
3023+
EmittedSize = DL.getTypeStoreSize(CV->getType());
3024+
} else {
3025+
for (unsigned I = 0, E = CV->getType()->getNumElements(); I != E; ++I) {
3026+
emitGlobalAliasInline(AP, DL.getTypeAllocSize(CV->getType()) * I, AliasList);
3027+
emitGlobalConstantImpl(DL, CV->getOperand(I), AP);
3028+
}
3029+
EmittedSize =
3030+
DL.getTypeAllocSize(ElementType) * CV->getType()->getNumElements();
30043031
}
30053032

30063033
unsigned Size = DL.getTypeAllocSize(CV->getType());
3007-
unsigned EmittedSize = DL.getTypeAllocSize(CV->getType()->getElementType()) *
3008-
CV->getType()->getNumElements();
30093034
if (unsigned Padding = Size - EmittedSize)
30103035
AP.OutStreamer->emitZeros(Padding);
30113036
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc < %s -mtriple aarch64 | FileCheck %s
2+
3+
; CHECK: a:
4+
; CHECK-NEXT: .zero 1
5+
@a = internal constant <4 x i1> <i1 false, i1 false, i1 false, i1 false>
6+
; CHECK: b:
7+
; CHECK-NEXT: .byte 5
8+
@b = internal constant <4 x i1> <i1 true, i1 false, i1 true, i1 false>
9+
; CHECK: c:
10+
; CHECK-NEXT: .hword 1
11+
; CHECK-NEXT: .byte 0
12+
; CHECK-NEXT: .zero 1
13+
@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>
14+

0 commit comments

Comments
 (0)