Skip to content

Commit c908bba

Browse files
committed
Add a new fixed metadata node to carry the xcoff pgo association.
Instead of decomposing the initializer for a __profd profiling data symbol to get its associated __profc symbol attach a metadata node tying the 2 respective globals together.
1 parent e77a670 commit c908bba

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

llvm/include/llvm/IR/FixedMetadataKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ LLVM_FIXED_MD_KIND(MD_coro_outside_frame, "coro.outside.frame", 39)
5454
LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40)
5555
LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41)
5656
LLVM_FIXED_MD_KIND(MD_callee_type, "callee_type", 42)
57+
LLVM_FIXED_MD_KIND(MD_pgo_associated, "pgo.associated", 43)

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,46 +2813,17 @@ void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
28132813
// associated profiling counters.
28142814
if (TM.getFunctionSections() &&
28152815
Csect->getName().starts_with("__llvm_prf_data.")) {
2816-
// Unraveling the initializer to find the related counters variable. The
2817-
// initializer is a structure whose third member is a subtract expression
2818-
// between the counters label and the label for the start of this structure.
2819-
// Use the subtract expression to get the GlobalValue for the counters
2820-
// global.
2821-
assert(GV->hasInitializer() &&
2822-
"profiling data symbol must have an initializer");
2823-
assert(isa<ConstantStruct>(GV->getInitializer()) &&
2824-
"expect the initializer for a profiling data symbol to be a struct");
2825-
const ConstantStruct *Initializer =
2826-
cast<ConstantStruct>(GV->getInitializer());
2827-
2828-
// The initializer structure is: { i64, i64, i32, ptr, ptr, i32, [4 x i16] }
2829-
// and the reference to the global variable for the counters is in the
2830-
// first i32 member.
2831-
const Constant *Member = Initializer->getAggregateElement(2);
2832-
assert(Member && "profiling data symbol has more then 3 elements");
2833-
2834-
// Want to decompose a constant expression of the form:
2835-
// sub (ptrtoint (ptr @__profc_sym), ptrtoint (ptr @__profd_sym))
2836-
// to get the GlobalVariable for the '@__profc_sym` symbol.
2837-
assert(isa<ConstantExpr>(Member) &&
2838-
"expected member initializer is a constant expression.");
2839-
const ConstantExpr *CExpr = cast<ConstantExpr>(Member);
2840-
assert(CExpr->getOpcode() == Instruction::Sub &&
2841-
"expected member intializer is a sub expression.");
2842-
2843-
Value *V1 = CExpr->getOperand(0);
2844-
assert(V1 && isa<ConstantExpr>(V1) &&
2845-
"expected sub expression operand to be constant expr.");
2846-
ConstantExpr *PointerToIntExpr = cast<ConstantExpr>(V1);
2847-
assert(PointerToIntExpr->isCast() && "unexpected operand type.");
2848-
2849-
Value *PointerToIntOperand = PointerToIntExpr->getOperand(0);
2850-
assert(isa<GlobalVariable>(PointerToIntOperand) &&
2851-
"expected global variable of profc symbol");
2852-
2853-
const GlobalVariable *ProfCGV = cast<GlobalVariable>(PointerToIntOperand);
2816+
MDNode *MD = GV->getMetadata(LLVMContext::MD_pgo_associated);
2817+
2818+
assert(MD &&
2819+
"profiling data symbol must have an associated counter symbol");
2820+
2821+
const ValueAsMetadata *VAM = cast<ValueAsMetadata>(MD->getOperand(0).get());
2822+
const GlobalVariable *ProfCGV = cast<GlobalVariable>(VAM->getValue());
2823+
28542824
// Map the global variable to its CSECT.
28552825
SectionKind ProfCKind = getObjFileLowering().getKindForGlobal(GV, TM);
2826+
28562827
MCSectionXCOFF *ProfCCsect = cast<MCSectionXCOFF>(
28572828
getObjFileLowering().SectionForGlobal(ProfCGV, ProfCKind, TM));
28582829

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,12 @@ void InstrLowerer::createDataVariable(InstrProfCntrInstBase *Inc) {
18751875
Data->setAlignment(Align(INSTR_PROF_DATA_ALIGNMENT));
18761876
maybeSetComdat(Data, Fn, CntsVarName);
18771877

1878+
if (TT.isOSBinFormatXCOFF()) {
1879+
Data->setMetadata(
1880+
LLVMContext::MD_pgo_associated,
1881+
MDNode::get(M.getContext(), ValueAsMetadata::get(PD.RegionCounters)));
1882+
}
1883+
18781884
PD.DataVar = Data;
18791885

18801886
// Mark the data variable as used so that it isn't stripped out.

llvm/test/CodeGen/PowerPC/aix-pgo-function-sections.ll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
@i = external local_unnamed_addr global i32, align 4
88
@__llvm_profile_raw_version = weak hidden local_unnamed_addr constant i64 72057594037927944
99
@__profc_func1 = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
10-
@__profd_func1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -2545542355363006406, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_func1 to i32), i32 ptrtoint (ptr @__profd_func1 to i32)), ptr @func1.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
10+
@__profd_func1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -2545542355363006406, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_func1 to i32), i32 ptrtoint (ptr @__profd_func1 to i32)), ptr @func1.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !0
1111
@__profc_func2 = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
12-
@__profd_func2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -4377547752858689819, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_func2 to i32), i32 ptrtoint (ptr @__profd_func2 to i32)), ptr @func2.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
12+
@__profd_func2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -4377547752858689819, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_func2 to i32), i32 ptrtoint (ptr @__profd_func2 to i32)), ptr @func2.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !1
1313
@__llvm_prf_nm = private constant [13 x i8] c"\0B\00func1\01func2", section "__llvm_prf_names", align 1
1414
@__llvm_profile_filename = weak hidden local_unnamed_addr constant [19 x i8] c"default_%m.profraw\00"
1515
@llvm.used = appending global [3 x ptr] [ptr @__llvm_prf_nm, ptr @__profd_func1, ptr @__profd_func2], section "llvm.metadata"
@@ -38,6 +38,9 @@ entry:
3838

3939
declare i32 @external_func(i32 noundef)
4040

41+
!0 = !{ptr @__profc_func1}
42+
!1 = !{ptr @__profc_func2}
43+
4144
; CHECK-DAG: .csect __llvm_prf_cnts.__profc_func1[RW]
4245
; CHECK-DAG: .csect __llvm_prf_data.__profd_func1[RW]
4346
; CHECK-DAG: .csect __llvm_prf_cnts.__profc_func2[RW]

llvm/test/LTO/PowerPC/pgo-function-sections-aix.ll

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ target triple = "powerpc-ibm-aix7.2.0.0"
1616

1717
@__llvm_profile_raw_version = weak hidden constant i64 72057594037927944
1818
@__profc_func1 = private global [2 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
19-
@__profd_func1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -2545542355363006406, i64 146835647075900052, i32 sub (i32 ptrtoint (ptr @__profc_func1 to i32), i32 ptrtoint (ptr @__profd_func1 to i32)), ptr @func1.local, ptr null, i32 2, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
19+
@__profd_func1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -2545542355363006406, i64 146835647075900052, i32 sub (i32 ptrtoint (ptr @__profc_func1 to i32), i32 ptrtoint (ptr @__profd_func1 to i32)), ptr @func1.local, ptr null, i32 2, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !0
2020
@__profc_file1.c_internal_func = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
21-
@__profd_file1.c_internal_func = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 2905054957054668920, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_file1.c_internal_func to i32), i32 ptrtoint(ptr @__profd_file1.c_internal_func to i32)), ptr @internal_func, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
21+
@__profd_file1.c_internal_func = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 2905054957054668920, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_file1.c_internal_func to i32), i32 ptrtoint(ptr @__profd_file1.c_internal_func to i32)), ptr @internal_func, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !1
2222
@__profc_escape1 = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
23-
@__profd_escape1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 3473293639883741762, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_escape1 to i32), i32 ptrtoint (ptr @__profd_escape1 to i32)), ptr @escape1.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
23+
@__profd_escape1 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 3473293639883741762, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_escape1 to i32), i32 ptrtoint (ptr @__profd_escape1 to i32)), ptr @escape1.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !2
2424
@__llvm_prf_nm = private constant [37 x i8] c"#\00func1\01file1.c:internal_func\01escape1", section "__llvm_prf_names", align 1
2525
@llvm.used = appending global [4 x ptr] [ptr @__profd_func1, ptr @__profd_file1.c_internal_func, ptr @__profd_escape1, ptr @__llvm_prf_nm], section "llvm.metadata"
2626
@__llvm_profile_filename = weak hidden constant [19 x i8] c"default_%m.profraw\00"
@@ -87,17 +87,21 @@ declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #1
8787
attributes #0 = { noinline nounwind optnone }
8888
attributes #1 = { nounwind }
8989

90+
!0 = !{ptr @__profc_func1}
91+
!1 = !{ptr @__profc_file1.c_internal_func}
92+
!2 = !{ptr @__profc_escape1}
93+
9094
;--- f2.ll
9195
target datalayout = "E-m:a-p:32:32-i64:64-n32"
9296
target triple = "powerpc-ibm-aix7.2.0.0"
9397

9498
@__llvm_profile_raw_version = weak hidden constant i64 72057594037927944
9599
@__profc_func2 = private global [2 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
96-
@__profd_func2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -4377547752858689819, i64 146835647075900052, i32 sub (i32 ptrtoint (ptr @__profc_func2 to i32), i32 ptrtoint (ptr @__profd_func2 to i32)), ptr @func2.local, ptr null, i32 2, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
100+
@__profd_func2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 -4377547752858689819, i64 146835647075900052, i32 sub (i32 ptrtoint (ptr @__profc_func2 to i32), i32 ptrtoint (ptr @__profd_func2 to i32)), ptr @func2.local, ptr null, i32 2, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !0
97101
@__profc_file2.c_internal_func = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
98-
@__profd_file2.c_internal_func = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 4899437111831460578, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_file2.c_internal_func to i32), i32 ptrtoint (ptr @__profd_file2.c_internal_func to i32)), ptr @internal_func, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
102+
@__profd_file2.c_internal_func = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 4899437111831460578, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_file2.c_internal_func to i32), i32 ptrtoint (ptr @__profd_file2.c_internal_func to i32)), ptr @internal_func, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !1
99103
@__profc_escape2 = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", align 8
100-
@__profd_escape2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 3333263850724280696, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_escape2 to i32), i32 ptrtoint (ptr @__profd_escape2 to i32)), ptr @escape2.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8
104+
@__profd_escape2 = private global { i64, i64, i32, ptr, ptr, i32, [4 x i16] } { i64 3333263850724280696, i64 742261418966908927, i32 sub (i32 ptrtoint (ptr @__profc_escape2 to i32), i32 ptrtoint (ptr @__profd_escape2 to i32)), ptr @escape2.local, ptr null, i32 1, [4 x i16] zeroinitializer }, section "__llvm_prf_data", align 8, !pgo.associated !2
101105
@__llvm_prf_nm = private constant [37 x i8] c"#\00func2\01file2.c:internal_func\01escape2", section "__llvm_prf_names", align 1
102106
@llvm.used = appending global [4 x ptr] [ptr @__profd_func2, ptr @__profd_file2.c_internal_func, ptr @__profd_escape2, ptr @__llvm_prf_nm], section "llvm.metadata"
103107
@__llvm_profile_filename = weak hidden constant [19 x i8] c"default_%m.profraw\00"
@@ -166,6 +170,10 @@ declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #1
166170
attributes #0 = { noinline nounwind optnone }
167171
attributes #1 = { nounwind }
168172

173+
!0 = !{ptr @__profc_func2}
174+
!1 = !{ptr @__profc_file2.c_internal_func}
175+
!2 = !{ptr @__profc_escape2}
176+
169177
; CHECK-DAG: .csect __llvm_prf_cnts.__profc_func1[RW]
170178
; CHECK-DAG: .csect __llvm_prf_data.__profd_func1[RW]
171179
; CHECK-DAG: .csect __llvm_prf_cnts.__profc_file1.c_internal_func[RW]

0 commit comments

Comments
 (0)