Skip to content

Commit da55b1b

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

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
@@ -280,7 +280,7 @@ class PPCAIXAsmPrinter : public PPCAsmPrinter {
280280

281281
void emitFunctionBodyEnd() override;
282282

283-
void emitPGORefs();
283+
void emitPGORefs(Module &M);
284284

285285
void emitEndOfAsmFile(Module &) override;
286286

@@ -2652,10 +2652,28 @@ void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
26522652
getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
26532653
}
26542654

2655-
void PPCAIXAsmPrinter::emitPGORefs() {
2656-
if (OutContext.hasXCOFFSection(
2655+
void PPCAIXAsmPrinter::emitPGORefs(Module &M) {
2656+
if (!OutContext.hasXCOFFSection(
26572657
"__llvm_prf_cnts",
2658-
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD))) {
2658+
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)))
2659+
return;
2660+
2661+
// When inside a csect `foo`, a .ref directive referring to a csect `bar`
2662+
// translates into a relocation entry from `foo` to` bar`. The referring
2663+
// csect, `foo`, is identified by its address. If multiple csects have the
2664+
// same address (because one or more of them are zero-length), the referring
2665+
// csect cannot be determined. Hence, we don't generate the .ref directives
2666+
// if `__llvm_prf_cnts` is an empty section.
2667+
bool HasNonZeroLengthPrfCntsSection = false;
2668+
const DataLayout &DL = M.getDataLayout();
2669+
for (GlobalVariable &GV : M.globals())
2670+
if (GV.hasSection() && GV.getSection().equals("__llvm_prf_cnts") &&
2671+
DL.getTypeAllocSize(GV.getValueType()) > 0) {
2672+
HasNonZeroLengthPrfCntsSection = true;
2673+
break;
2674+
}
2675+
2676+
if (HasNonZeroLengthPrfCntsSection) {
26592677
MCSection *CntsSection = OutContext.getXCOFFSection(
26602678
"__llvm_prf_cnts", SectionKind::getData(),
26612679
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD),
@@ -2689,7 +2707,7 @@ void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
26892707
if (M.empty() && TOCDataGlobalVars.empty())
26902708
return;
26912709

2692-
emitPGORefs();
2710+
emitPGORefs(M);
26932711

26942712
// Switch to section to emit TOC base.
26952713
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)