Skip to content

Commit 8e4423e

Browse files
[AsmPrinter] Fix handling in emitGlobalConstantImpl for AIX (#116255)
When GlobalMerge creates a MergedGlobal of statics all initialized to zero, emitGlobalConstantImpl sees a ConstantAggregateZero. This results in just emitting zeros followed by labels for the aliases. We need to handle it more like how emitGlobalConstantStruct does by emitting each global inside the aggregate. --------- Co-authored-by: Hubert Tong <[email protected]>
1 parent aff98e4 commit 8e4423e

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3849,6 +3849,8 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
38493849
AsmPrinter &AP, const Constant *BaseCV,
38503850
uint64_t Offset,
38513851
AsmPrinter::AliasMapTy *AliasList) {
3852+
assert(!AliasList || AP.TM.getTargetTriple().isOSBinFormatXCOFF() &&
3853+
"AliasList only expected for XCOFF");
38523854
emitGlobalAliasInline(AP, Offset, AliasList);
38533855
uint64_t Size = DL.getTypeAllocSize(CV->getType());
38543856

@@ -3858,7 +3860,27 @@ static void emitGlobalConstantImpl(const DataLayout &DL, const Constant *CV,
38583860
if (!BaseCV && CV->hasOneUse())
38593861
BaseCV = dyn_cast<Constant>(CV->user_back());
38603862

3861-
if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
3863+
if (isa<ConstantAggregateZero>(CV)) {
3864+
StructType *structType;
3865+
if (AliasList && (structType = llvm::dyn_cast<StructType>(CV->getType()))) {
3866+
// Handle cases of aliases to direct struct elements
3867+
const StructLayout *Layout = DL.getStructLayout(structType);
3868+
uint64_t SizeSoFar = 0;
3869+
for (unsigned int i = 0, n = structType->getNumElements(); i < n - 1;
3870+
++i) {
3871+
uint64_t GapToNext = Layout->getElementOffset(i + 1) - SizeSoFar;
3872+
AP.OutStreamer->emitZeros(GapToNext);
3873+
SizeSoFar += GapToNext;
3874+
emitGlobalAliasInline(AP, Offset + SizeSoFar, AliasList);
3875+
}
3876+
AP.OutStreamer->emitZeros(Size - SizeSoFar);
3877+
return;
3878+
} else {
3879+
return AP.OutStreamer->emitZeros(Size);
3880+
}
3881+
}
3882+
3883+
if (isa<UndefValue>(CV))
38623884
return AP.OutStreamer->emitZeros(Size);
38633885

38643886
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; RUN: rm -rf %t
2+
; RUN: mkdir -p %t
3+
4+
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr8 < %s | FileCheck %s
5+
6+
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr8 --filetype=obj -o %t/global-merge-aix-sections.o < %s
7+
; RUN; llvm-objdump --syms %t/global-merge-aix-sections.o | FileCheck %s --check-prefix=DATA
8+
9+
%struct.Example = type { i32, i8 }
10+
11+
@y = internal global i32 0, section "mycsect", align 4
12+
@z = internal global i32 0, section "mycsect", align 4
13+
@l = internal global i32 0, align 4
14+
@u = internal global i16 0, section "mycsect", align 2
15+
@myStruct1 = internal global %struct.Example zeroinitializer, section "mycsect", align 4
16+
17+
; Function Attrs: nounwind
18+
define void @g() {
19+
entry:
20+
tail call void @f(ptr noundef nonnull @y, ptr noundef nonnull @z)
21+
tail call void @f(ptr noundef nonnull @l, ptr noundef nonnull @z)
22+
tail call void @h(ptr noundef nonnull @u)
23+
tail call void @s(ptr noundef nonnull @myStruct1)
24+
ret void
25+
}
26+
27+
declare void @f(ptr noundef, ptr noundef)
28+
declare void @h(ptr noundef)
29+
declare void @s(ptr noundef)
30+
31+
; CHECK: .csect mycsect[RW],2
32+
; CHECK-NEXT: .lglobl u # @_MergedGlobals
33+
; CHECK-NEXT: .lglobl y
34+
; CHECK-NEXT: .lglobl z
35+
; CHECK-NEXT: .lglobl myStruct1
36+
; CHECK-NEXT: .align 2
37+
; CHECK-NEXT: L.._MergedGlobals:
38+
; CHECK-NEXT: u:
39+
; CHECK-NEXT: .space 2
40+
; CHECK-NEXT: .space 2
41+
; CHECK-NEXT: y:
42+
; CHECK-NEXT: .space 4
43+
; CHECK-NEXT: z:
44+
; CHECK-NEXT: .space 4
45+
; CHECK-NEXT: myStruct1:
46+
; CHECK-NEXT: .space 8
47+
48+
; DATA: 00000078 l O .data 00000014 mycsect
49+
; DATA-NEXT: 00000078 l O .data (csect: mycsect) 00000000 u
50+
; DATA-NEXT: 0000007c l O .data (csect: mycsect) 00000000 y
51+
; DATA-NEXT: 00000080 l O .data (csect: mycsect) 00000000 z
52+
; DATA-NEXT: 00000084 l O .data (csect: mycsect) 00000000 myStruct1

0 commit comments

Comments
 (0)