Skip to content

[XCOFF] Do not generate the special .ref for zero-length sections #66805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class PPCAIXAsmPrinter : public PPCAsmPrinter {

void emitFunctionBodyEnd() override;

void emitPGORefs();
void emitPGORefs(Module &M);

void emitEndOfAsmFile(Module &) override;

Expand Down Expand Up @@ -2652,10 +2652,28 @@ void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
}

void PPCAIXAsmPrinter::emitPGORefs() {
if (OutContext.hasXCOFFSection(
void PPCAIXAsmPrinter::emitPGORefs(Module &M) {
if (!OutContext.hasXCOFFSection(
"__llvm_prf_cnts",
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD))) {
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)))
return;

// When inside a csect `foo`, a .ref directive referring to a csect `bar`
// translates into a relocation entry from `foo` to` bar`. The referring
// csect, `foo`, is identified by its address. If multiple csects have the
// same address (because one or more of them are zero-length), the referring
// csect cannot be determined. Hence, we don't generate the .ref directives
// if `__llvm_prf_cnts` is an empty section.
bool HasNonZeroLengthPrfCntsSection = false;
const DataLayout &DL = M.getDataLayout();
for (GlobalVariable &GV : M.globals())
if (GV.hasSection() && GV.getSection().equals("__llvm_prf_cnts") &&
DL.getTypeAllocSize(GV.getValueType()) > 0) {
HasNonZeroLengthPrfCntsSection = true;
break;
}

if (HasNonZeroLengthPrfCntsSection) {
MCSection *CntsSection = OutContext.getXCOFFSection(
"__llvm_prf_cnts", SectionKind::getData(),
XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD),
Expand Down Expand Up @@ -2689,7 +2707,7 @@ void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
if (M.empty() && TOCDataGlobalVars.empty())
return;

emitPGORefs();
emitPGORefs(M);

// Switch to section to emit TOC base.
OutStreamer->switchSection(getObjFileLowering().getTOCBaseSection());
Expand Down
75 changes: 75 additions & 0 deletions llvm/test/CodeGen/PowerPC/pgo-ref-directive.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/with-vnds.ll -o %t/with-vnds.o
; RUN: llvm-objdump %t/with-vnds.o -tr | FileCheck %s --check-prefix=WITHVNDS-OBJ

; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false < %t/zero-size-cnts-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-CNTS
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-cnts-section.ll -o %t/zero-size-cnts-section.o
; RUN: llvm-objdump %t/zero-size-cnts-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-CNTS-OBJ

; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false < %t/zero-size-other-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-OTHER
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc64-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false < %t/zero-size-other-section.ll | FileCheck %s --check-prefixes=ZERO-SIZE-OTHER
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-other-section.ll -o %t/zero-size-other-section.o
; RUN: llvm-objdump %t/zero-size-other-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-OTHER-OBJ
; RUN: llc -verify-machineinstrs -mcpu=pwr4 -mattr=-altivec -mtriple powerpc64-ibm-aix-xcoff \
; RUN: -xcoff-traceback-table=false --filetype=obj < %t/zero-size-other-section.ll -o %t/zero-size-other-section.o
; RUN: llvm-objdump %t/zero-size-other-section.o -tr | FileCheck %s --check-prefix=ZERO-SIZE-OTHER-OBJ


;--- no-ref.ll
; The absence of a __llvm_prf_cnts section should stop generating the .refs.
;
Expand Down Expand Up @@ -120,3 +138,60 @@ entry:
; WITHVNDS-OBJ-NEXT: 00000000 R_REF __llvm_prf_vnds
; WITHVNDS-OBJ-NEXT: 00000100 R_POS .main
; WITHVNDS-OBJ-NEXT: 00000104 R_POS TOC

;--- zero-size-cnts-section.ll
; If __llvm_prf_cnts is of zero size, do not generate the .ref directive.
; The size of the other sections does not matter.

@dummy_cnts = private global [0 x i32] zeroinitializer, section "__llvm_prf_cnts", align 4
@dummy_data = private global [1 x i64] zeroinitializer, section "__llvm_prf_data", align 8
@dummy_name = private constant [0 x i32] zeroinitializer, section "__llvm_prf_names", align 4

@llvm.used = appending global [3 x ptr]
[ptr @dummy_cnts,
ptr @dummy_data,
ptr @dummy_name], section "llvm.metadata"

define i32 @main() #0 {
entry:
ret i32 1
}

; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_data[RW]
; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_names[RO]
; ZERO-SIZE-CNTS-NOT: .ref __llvm_prf_vnds

; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_data
; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_names
; ZERO-SIZE-CNTS-OBJ-NOT: R_REF __llvm_prf_vnds

;--- zero-size-other-section.ll
; If __llvm_prf_cnts is of non-zero size, generate the .ref directive even if other sections
; are zero-sized;

@__profc_main = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
@__profd_main = private global [0 x i64] zeroinitializer, section "__llvm_prf_data", align 8
@__llvm_prf_nm = private constant [0 x i8] zeroinitializer, section "__llvm_prf_names", align 1
@__llvm_prf_vnodes = private global [0 x { i64, i64, ptr }] zeroinitializer, section "__llvm_prf_vnds"

@llvm.used = appending global [4 x ptr]
[ptr @__profc_main,
ptr @__profd_main,
ptr @__llvm_prf_nm,
ptr @__llvm_prf_vnodes], section "llvm.metadata"

define i32 @main() #0 {
entry:
ret i32 1
}

; ZERO-SIZE-OTHER: .csect __llvm_prf_cnts[RW],3
; ZERO-SIZE-OTHER: .csect __llvm_prf_cnts[RW],3
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_data[RW]
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_names[RO]
; ZERO-SIZE-OTHER-NEXT: .ref __llvm_prf_vnds[RW]

; ZERO-SIZE-OTHER-OBJ: R_REF __llvm_prf_data
; ZERO-SIZE-OTHER-OBJ-NEXT: R_REF __llvm_prf_names
; ZERO-SIZE-OTHER-OBJ-NEXT: R_REF __llvm_prf_vnds