Skip to content

Commit d2cac52

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 f8da9c2 commit d2cac52

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
@@ -53,3 +53,4 @@ LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 38)
5353
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)
56+
LLVM_FIXED_MD_KIND(MD_pgo_associated, "pgo.associated", 42)

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,46 +2831,17 @@ void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
28312831
// associated profiling counters.
28322832
if (TM.getFunctionSections() &&
28332833
Csect->getName().starts_with("__llvm_prf_data.")) {
2834-
// Unraveling the initializer to find the related counters variable. The
2835-
// initializer is a structure whose third member is a subtract expression
2836-
// between the counters label and the label for the start of this structure.
2837-
// Use the subtract expression to get the GlobalValue for the counters
2838-
// global.
2839-
assert(GV->hasInitializer() &&
2840-
"profiling data symbol must have an initializer");
2841-
assert(isa<ConstantStruct>(GV->getInitializer()) &&
2842-
"expect the initializer for a profiling data symbol to be a struct");
2843-
const ConstantStruct *Initializer =
2844-
cast<ConstantStruct>(GV->getInitializer());
2845-
2846-
// The initializer structure is: { i64, i64, i32, ptr, ptr, i32, [4 x i16] }
2847-
// and the reference to the global variable for the counters is in the
2848-
// first i32 member.
2849-
const Constant *Member = Initializer->getAggregateElement(2);
2850-
assert(Member && "profiling data symbol has more then 3 elements");
2851-
2852-
// Want to decompose a constant expression of the form:
2853-
// sub (ptrtoint (ptr @__profc_sym), ptrtoint (ptr @__profd_sym))
2854-
// to get the GlobalVariable for the '@__profc_sym` symbol.
2855-
assert(isa<ConstantExpr>(Member) &&
2856-
"expected member initializer is a constant expression.");
2857-
const ConstantExpr *CExpr = cast<ConstantExpr>(Member);
2858-
assert(CExpr->getOpcode() == Instruction::Sub &&
2859-
"expected member intializer is a sub expression.");
2860-
2861-
Value *V1 = CExpr->getOperand(0);
2862-
assert(V1 && isa<ConstantExpr>(V1) &&
2863-
"expected sub expression operand to be constant expr.");
2864-
ConstantExpr *PointerToIntExpr = cast<ConstantExpr>(V1);
2865-
assert(PointerToIntExpr->isCast() && "unexpected operand type.");
2866-
2867-
Value *PointerToIntOperand = PointerToIntExpr->getOperand(0);
2868-
assert(isa<GlobalVariable>(PointerToIntOperand) &&
2869-
"expected global variable of profc symbol");
2870-
2871-
const GlobalVariable *ProfCGV = cast<GlobalVariable>(PointerToIntOperand);
2834+
MDNode *MD = GV->getMetadata(LLVMContext::MD_pgo_associated);
2835+
2836+
assert(MD &&
2837+
"profiling data symbol must have an associated counter symbol");
2838+
2839+
const ValueAsMetadata *VAM = cast<ValueAsMetadata>(MD->getOperand(0).get());
2840+
const GlobalVariable *ProfCGV = cast<GlobalVariable>(VAM->getValue());
2841+
28722842
// Map the global variable to its CSECT.
28732843
SectionKind ProfCKind = getObjFileLowering().getKindForGlobal(GV, TM);
2844+
28742845
MCSectionXCOFF *ProfCCsect = cast<MCSectionXCOFF>(
28752846
getObjFileLowering().SectionForGlobal(ProfCGV, ProfCKind, TM));
28762847

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

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

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

18791885
// 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)