Skip to content

Commit cb23434

Browse files
w2yehiaWael Yehia
authored andcommitted
[XCOFF] Do not generate the special .ref for zero-length sections (#66805)
Co-authored-by: Wael Yehia <[email protected]> (cherry picked from commit da55b1b)
1 parent 1b55dc9 commit cb23434

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class PPCAIXAsmPrinter : public PPCAsmPrinter {
279279

280280
void emitFunctionBodyEnd() override;
281281

282-
void emitPGORefs();
282+
void emitPGORefs(Module &M);
283283

284284
void emitEndOfAsmFile(Module &) override;
285285

@@ -2636,10 +2636,28 @@ void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
26362636
getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
26372637
}
26382638

2639-
void PPCAIXAsmPrinter::emitPGORefs() {
2640-
if (OutContext.hasXCOFFSection(
2639+
void PPCAIXAsmPrinter::emitPGORefs(Module &M) {
2640+
if (!OutContext.hasXCOFFSection(
26412641
"__llvm_prf_cnts",
2642-
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD))) {
2642+
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)))
2643+
return;
2644+
2645+
// When inside a csect `foo`, a .ref directive referring to a csect `bar`
2646+
// translates into a relocation entry from `foo` to` bar`. The referring
2647+
// csect, `foo`, is identified by its address. If multiple csects have the
2648+
// same address (because one or more of them are zero-length), the referring
2649+
// csect cannot be determined. Hence, we don't generate the .ref directives
2650+
// if `__llvm_prf_cnts` is an empty section.
2651+
bool HasNonZeroLengthPrfCntsSection = false;
2652+
const DataLayout &DL = M.getDataLayout();
2653+
for (GlobalVariable &GV : M.globals())
2654+
if (GV.hasSection() && GV.getSection().equals("__llvm_prf_cnts") &&
2655+
DL.getTypeAllocSize(GV.getValueType()) > 0) {
2656+
HasNonZeroLengthPrfCntsSection = true;
2657+
break;
2658+
}
2659+
2660+
if (HasNonZeroLengthPrfCntsSection) {
26432661
MCSection *CntsSection = OutContext.getXCOFFSection(
26442662
"__llvm_prf_cnts", SectionKind::getData(),
26452663
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD),
@@ -2673,7 +2691,7 @@ void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
26732691
if (M.empty() && TOCDataGlobalVars.empty())
26742692
return;
26752693

2676-
emitPGORefs();
2694+
emitPGORefs(M);
26772695

26782696
// Switch to section to emit TOC base.
26792697
OutStreamer->switchSection(getObjFileLowering().getTOCBaseSection());

llvm/test/CodeGen/PowerPC/pgo-ref-directive.ll

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@
1818
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/with-vnds.ll -o %t/with-vnds.o
1919
; RUN: llvm-objdump %t/with-vnds.o -tr | FileCheck %s --check-prefix=WITHVNDS-OBJ
2020

21+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
22+
; RUN: -xcoff-traceback-table=false < %t/zero-size-cnts-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-CNTS
23+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
24+
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-cnts-section.ll -o %t/zero-size-cnts-section.o
25+
; RUN: llvm-objdump %t/zero-size-cnts-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-CNTS-OBJ
26+
27+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
28+
; RUN: -xcoff-traceback-table=false < %t/zero-size-other-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-OTHER
29+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc64-ibm-aix-xcoff \
30+
; RUN: -xcoff-traceback-table=false < %t/zero-size-other-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-OTHER
31+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
32+
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-other-section.ll -o %t/zero-size-other-section.o
33+
; RUN: llvm-objdump %t/zero-size-other-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-OTHER-OBJ
34+
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc64-ibm-aix-xcoff \
35+
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-other-section.ll -o %t/zero-size-other-section.o
36+
; RUN: llvm-objdump %t/zero-size-other-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-OTHER-OBJ
37+
38+
2139
;--- no-ref.ll
2240
; The absence of a __llvm_prf_cnts section should stop generating the .refs.
2341
;
@@ -120,3 +138,60 @@ entry:
120138
; WITHVNDS-OBJ-NEXT: 00000000 R_REF __llvm_prf_vnds
121139
; WITHVNDS-OBJ-NEXT: 00000100 R_POS .main
122140
; WITHVNDS-OBJ-NEXT: 00000104 R_POS TOC
141+
142+
;--- zero-size-cnts-section.ll
143+
; If __llvm_prf_cnts is of zero size, do not generate the .ref directive.
144+
; The size of the other sections does not matter.
145+
146+
@dummy_cnts = private global [0 x i32] zeroinitializer, section "__llvm_prf_cnts", align 4
147+
@dummy_data = private global [1 x i64] zeroinitializer, section "__llvm_prf_data", align 8
148+
@dummy_name = private constant [0 x i32] zeroinitializer, section "__llvm_prf_names", align 4
149+
150+
@llvm.used = appending global [3 x ptr]
151+
[ptr @dummy_cnts,
152+
ptr @dummy_data,
153+
ptr @dummy_name], section "llvm.metadata"
154+
155+
define i32 @main() #0 {
156+
entry:
157+
ret i32 1
158+
}
159+
160+
; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_data[RW]
161+
; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_names[RO]
162+
; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_vnds
163+
164+
; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_data
165+
; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_names
166+
; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_vnds
167+
168+
;--- zero-size-other-section.ll
169+
; If __llvm_prf_cnts is of non-zero size, generate the .ref directive even if other sections
170+
; are zero-sized;
171+
172+
@__profc_main = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
173+
@__profd_main = private global [0 x i64] zeroinitializer, section "__llvm_prf_data", align 8
174+
@__llvm_prf_nm = private constant [0 x i8] zeroinitializer, section "__llvm_prf_names", align 1
175+
@__llvm_prf_vnodes = private global [0 x { i64, i64, ptr }] zeroinitializer, section "__llvm_prf_vnds"
176+
177+
@llvm.used = appending global [4 x ptr]
178+
[ptr @__profc_main,
179+
ptr @__profd_main,
180+
ptr @__llvm_prf_nm,
181+
ptr @__llvm_prf_vnodes], section "llvm.metadata"
182+
183+
define i32 @main() #0 {
184+
entry:
185+
ret i32 1
186+
}
187+
188+
; ZERO-SIZE-OTHER: .csect __llvm_prf_cnts[RW],3
189+
; ZERO-SIZE-OTHER: .csect __llvm_prf_cnts[RW],3
190+
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_data[RW]
191+
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_names[RO]
192+
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_vnds[RW]
193+
194+
; ZERO-SIZE-OTHER-OBJ: R_REF __llvm_prf_data
195+
; ZERO-SIZE-OTHER-OBJ-NEXT: R_REF __llvm_prf_names
196+
; ZERO-SIZE-OTHER-OBJ-NEXT: R_REF __llvm_prf_vnds
197+

0 commit comments

Comments
 (0)