Skip to content

Commit f30416f

Browse files
LemonBoyefriedma-quic
authored andcommitted
[AsmPrinter] Fix emission of non-standard integer constants for BE targets
The code assumed that zero-extending the integer constant to the designated alloc size would be fine even for BE targets, but that's not the case as that pulls in zeros from the MSB side while we actually expect the padding zeros to go after the LSB. I've changed the codepath handling the constant integers to use the store size for both small(er than u64) and big constants and then add zero padding right after that. Differential Revision: https://reviews.llvm.org/D78011
1 parent 7a8c226 commit f30416f

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,9 +2617,10 @@ static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
26172617
// [chunk1][chunk2] ... [chunkN].
26182618
// The most significant chunk is chunkN and it should be emitted first.
26192619
// However, due to the alignment issue chunkN contains useless bits.
2620-
// Realign the chunks so that they contain only useless information:
2620+
// Realign the chunks so that they contain only useful information:
26212621
// ExtraBits 0 1 (BitWidth / 64) - 1
26222622
// chu[nk1 chu][nk2 chu] ... [nkN-1 chunkN]
2623+
ExtraBitsSize = alignTo(ExtraBitsSize, 8);
26232624
ExtraBits = Realigned.getRawData()[0] &
26242625
(((uint64_t)-1) >> (64 - ExtraBitsSize));
26252626
Realigned.lshrInPlace(ExtraBitsSize);
@@ -2640,7 +2641,7 @@ static void emitGlobalConstantLargeInt(const ConstantInt *CI, AsmPrinter &AP) {
26402641
// Emit the extra bits after the 64-bits chunks.
26412642

26422643
// Emit a directive that fills the expected size.
2643-
uint64_t Size = AP.getDataLayout().getTypeAllocSize(CI->getType());
2644+
uint64_t Size = AP.getDataLayout().getTypeStoreSize(CI->getType());
26442645
Size -= (BitWidth / 64) * 8;
26452646
assert(Size && Size * 8 >= ExtraBitsSize &&
26462647
(ExtraBits & (((uint64_t)-1) >> (64 - ExtraBitsSize)))
@@ -2755,20 +2756,22 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
27552756
return AP.OutStreamer->emitZeros(Size);
27562757

27572758
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
2758-
switch (Size) {
2759-
case 1:
2760-
case 2:
2761-
case 4:
2762-
case 8:
2759+
const uint64_t StoreSize = DL.getTypeStoreSize(CV->getType());
2760+
2761+
if (StoreSize < 8) {
27632762
if (AP.isVerbose())
27642763
AP.OutStreamer->GetCommentOS() << format("0x%" PRIx64 "\n",
27652764
CI->getZExtValue());
2766-
AP.OutStreamer->emitIntValue(CI->getZExtValue(), Size);
2767-
return;
2768-
default:
2765+
AP.OutStreamer->emitIntValue(CI->getZExtValue(), StoreSize);
2766+
} else {
27692767
emitGlobalConstantLargeInt(CI, AP);
2770-
return;
27712768
}
2769+
2770+
// Emit tail padding if needed
2771+
if (Size != StoreSize)
2772+
AP.OutStreamer->emitZeros(Size - StoreSize);
2773+
2774+
return;
27722775
}
27732776

27742777
if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))

llvm/test/CodeGen/ARM/emit-big-cst.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
; CHECK: bigCst:
55
; CHECK-NEXT: .long 1694510592
66
; CHECK-NEXT: .long 2960197
7-
; CHECK-NEXT: .long 26220
7+
; CHECK-NEXT: .short 26220
8+
; CHECK-NEXT: .byte 0
9+
; CHECK-NEXT: .zero 1
810
; CHECK-NEXT: .size bigCst, 12
911

1012
@bigCst = internal constant i82 483673642326615442599424
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1-
; RUN: llc -march=mips < %s | FileCheck %s
1+
; RUN: llc -march=mips < %s | FileCheck %s --check-prefix=BE
2+
; RUN: llc -march=mipsel < %s | FileCheck %s --check-prefix=LE
23
; Check assembly printing of odd constants.
34

4-
; CHECK: bigCst:
5-
; CHECK-NEXT: .8byte 1845068520838224192
6-
; CHECK-NEXT: .8byte 11776
7-
; CHECK-NEXT: .size bigCst, 16
5+
; BE-LABEL: bigCst:
6+
; BE-NEXT: .8byte 28829195638097253
7+
; BE-NEXT: .2byte 46
8+
; BE-NEXT: .byte 0
9+
; BE-NEXT: .space 5
10+
; BE-NEXT: .size bigCst, 16
11+
12+
; LE-LABEL: bigCst:
13+
; LE-NEXT: .8byte 12713950999227904
14+
; LE-NEXT: .2byte 26220
15+
; LE-NEXT: .byte 0
16+
; LE-NEXT: .space 5
17+
; LE-NEXT: .size bigCst, 16
18+
19+
; BE-LABEL: smallCst:
20+
; BE-NEXT: .2byte 4386
21+
; BE-NEXT: .byte 51
22+
; BE-NEXT: .space 1
23+
; BE-NEXT: .size smallCst, 4
24+
25+
; LE-LABEL: smallCst:
26+
; LE-NEXT: .2byte 8755
27+
; LE-NEXT: .byte 17
28+
; LE-NEXT: .space 1
29+
; LE-NEXT: .size smallCst, 4
830

931
@bigCst = internal constant i82 483673642326615442599424
1032

@@ -15,3 +37,5 @@ define void @accessBig(i64* %storage) {
1537
store i82 %tmp, i82* %addr
1638
ret void
1739
}
40+
41+
@smallCst = internal constant i24 1122867

llvm/test/CodeGen/PowerPC/aix-xcoff-mergeable-const.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ entry:
2525
;CHECK: .csect .rodata[RO]
2626
;CHECK-NEXT: .align 4
2727
;CHECK-NEXT: .L__const.main.cnst32:
28-
;CHECK-NEXT: .llong 4611686018427387954 # 0x4000000000000032
28+
;CHECK-NEXT: .llong 4611686018427387954
2929
;CHECK-NEXT: .long 0 # 0x0
3030
;CHECK-NEXT: .space 4
31-
;CHECK-NEXT: .llong 0 # 0x0
31+
;CHECK-NEXT: .llong 0
3232
;CHECK-NEXT: .long 0 # 0x0
3333
;CHECK-NEXT: .space 4
3434
;CHECK-NEXT: .align 3
3535
;CHECK-NEXT: .L__const.main.cnst16:
36-
;CHECK-NEXT: .llong 4611686018427387926 # 0x4000000000000016
36+
;CHECK-NEXT: .llong 4611686018427387926
3737
;CHECK-NEXT: .long 0 # 0x0
3838
;CHECK-NEXT: .space 4
3939
;CHECK-NEXT: .align 3

llvm/test/CodeGen/X86/emit-big-cst.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
; CHECK: bigCst:
55
; CHECK-NEXT: .quad 12713950999227904
6-
; CHECK-NEXT: .quad 26220
6+
; CHECK-NEXT: .short 26220
7+
; CHECK-NEXT: .byte 0
8+
; CHECK-NEXT: .zero 5
79
; CHECK-NEXT: .size bigCst, 16
810

911
@bigCst = internal constant i82 483673642326615442599424

llvm/test/CodeGen/X86/global-fill.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
@test1 = global [2 x i24] [i24 -1, i24 -1]
44
; CHECK-LABEL: test1:
5-
; CHECK-NEXT: .long 16777215
6-
; CHECK-NEXT: .long 16777215
5+
; CHECK-NEXT: .short 65535
6+
; CHECK-NEXT: .byte 255
7+
; CHECK-NEXT: .space 1
8+
; CHECK-NEXT: .short 65535
9+
; CHECK-NEXT: .byte 255
10+
; CHECK-NEXT: .space 1
711

812
@test2 = global [2 x i7] [i7 1, i7 1]
913
; CHECK-LABEL: test2:

0 commit comments

Comments
 (0)