Skip to content

Commit 9bc4200

Browse files
authored
[llvm-profgen] Add an option to mark all the profile context as preinlined (#156501)
Add a new option (under `--mark-all-context-preinlined`) that marks all function samples with the `ContextShouldBeInlined ` attribute during post-processing to make the profile as preinlined. This can be useful for experiments outside of the CS preinliner, e.g. to fully replay the inlining for a given profile.
1 parent b6be44a commit 9bc4200

File tree

6 files changed

+112
-15
lines changed

6 files changed

+112
-15
lines changed

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,17 @@ class FunctionSamples {
868868
}
869869
}
870870

871+
// Propagate the given attribute to this profile context and all callee
872+
// contexts.
873+
void setContextAttribute(ContextAttributeMask Attr) {
874+
Context.setAttribute(Attr);
875+
for (auto &I : CallsiteSamples) {
876+
for (auto &CS : I.second) {
877+
CS.second.setContextAttribute(Attr);
878+
}
879+
}
880+
}
881+
871882
// Query the stale profile matching results and remap the location.
872883
const LineLocation &mapIRLocToProfileLoc(const LineLocation &IRLoc) const {
873884
// There is no remapping if the profile is not stale or the matching gives

llvm/test/tools/llvm-profgen/inline-cs-pseudoprobe.test

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-UNWINDER-OFFSET2
77
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/inline-cs-pseudoprobe.perfscript --binary=%S/Inputs/inline-cs-pseudoprobe.perfbin --output=%t --profile-summary-cold-count=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0
88
; RUN: FileCheck %s --input-file %t
9+
; RUN: llvm-profgen --format=text --perfscript=%S/Inputs/inline-cs-pseudoprobe.perfscript --binary=%S/Inputs/inline-cs-pseudoprobe.perfbin --output=%t --profile-summary-cold-count=0 --csspgo-preinliner=0 --gen-cs-nested-profile=0 --mark-all-context-preinlined
10+
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-MARK-PREINLINED
11+
912

1013
; CHECK: [main:2 @ foo]:74:0
1114
; CHECK-NEXT: 1: 0
@@ -18,10 +21,30 @@
1821
; CHECK-NEXT: 8: 14 bar:14
1922
; CHECK-NEXT: 9: 0
2023
; CHECK-NEXT: !CFGChecksum: 563088904013236
21-
; CHECK:[main:2 @ foo:8 @ bar]:28:14
24+
; CHECK-NEXT: !Attributes: 1
25+
; CHECK-NEXT:[main:2 @ foo:8 @ bar]:28:14
2226
; CHECK-NEXT: 1: 14
2327
; CHECK-NEXT: 4: 14
2428
; CHECK-NEXT: !CFGChecksum: 72617220756
29+
; CHECK-NEXT: !Attributes: 1
30+
31+
; CHECK-MARK-PREINLINED: [main:2 @ foo]:74:0
32+
; CHECK-MARK-PREINLINED-NEXT: 1: 0
33+
; CHECK-MARK-PREINLINED-NEXT: 2: 15
34+
; CHECK-MARK-PREINLINED-NEXT: 3: 15
35+
; CHECK-MARK-PREINLINED-NEXT: 4: 14
36+
; CHECK-MARK-PREINLINED-NEXT: 5: 1
37+
; CHECK-MARK-PREINLINED-NEXT: 6: 15
38+
; CHECK-MARK-PREINLINED-NEXT: 7: 0
39+
; CHECK-MARK-PREINLINED-NEXT: 8: 14 bar:14
40+
; CHECK-MARK-PREINLINED-NEXT: 9: 0
41+
; CHECK-MARK-PREINLINED-NEXT: !CFGChecksum: 563088904013236
42+
; CHECK-MARK-PREINLINED-NEXT: !Attributes: 3
43+
; CHECK-MARK-PREINLINED-NEXT:[main:2 @ foo:8 @ bar]:28:14
44+
; CHECK-MARK-PREINLINED-NEXT: 1: 14
45+
; CHECK-MARK-PREINLINED-NEXT: 4: 14
46+
; CHECK-MARK-PREINLINED-NEXT: !CFGChecksum: 72617220756
47+
; CHECK-MARK-PREINLINED-NEXT: !Attributes: 3
2548

2649
; CHECK-UNWINDER: 3
2750
; CHECK-UNWINDER-NEXT: 201800-201858:1

llvm/test/tools/llvm-profgen/inline-noprobe.test

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,48 @@
88
; RUN: FileCheck %s --input-file %t1 --check-prefix=CHECK-UPDATE-TOTAL-SAMPLE
99
; RUN: llvm-profgen --format=text --use-dwarf-correlation --perfscript=%S/Inputs/inline-noprobe.perfscript --binary=%S/Inputs/inline-noprobe.perfbin --output=%t
1010
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK
11+
; RUN: llvm-profgen --format=text --use-dwarf-correlation --perfscript=%S/Inputs/inline-noprobe.perfscript --binary=%S/Inputs/inline-noprobe.perfbin --output=%t --mark-all-context-preinlined
12+
; RUN: FileCheck %s --input-file %t --check-prefix=CHECK-MARK-PREINLINED
13+
1114
; RUN: echo -e "0\n0" > %t
1215
; RUN: llvm-profgen --format=text --unsymbolized-profile=%t --binary=%S/Inputs/inline-noprobe.perfbin --output=%t1 --fill-zero-for-all-funcs
1316
; RUN: FileCheck %s --input-file %t1 --check-prefix=CHECK-ALL-ZERO
1417
; RUN: llvm-profgen --format=text --unsymbolized-profile=%S/Inputs/out-of-bounds.raw.prof --binary=%S/Inputs/inline-noprobe.perfbin --output=%t1
1518
; RUN: FileCheck %s --input-file %t1 --check-prefix=CHECK-OB
1619

17-
CHECK: main:2609:0
18-
CHECK: 0: 0
19-
CHECK: 2: 0
20-
CHECK: 1: foo:2609
21-
CHECK: 2.1: 42
22-
CHECK: 3: 62
23-
CHECK: 3.2: 21
24-
CHECK: 4: 0
25-
CHECK: 65526: 62
26-
CHECK: 3.1: bar:546
27-
CHECK: 1: 42
28-
CHECK: 65533: 42
29-
CHECK: 3.2: bar:189
30-
CHECK: 1: 21
20+
CHECK: main:2609:0
21+
CHECK-NEXT: 0: 0
22+
CHECK-NEXT: 2: 0
23+
CHECK-NEXT: 1: foo:2609
24+
CHECK-NEXT: 2.1: 42
25+
CHECK-NEXT: 3: 62
26+
CHECK-NEXT: 3.2: 21
27+
CHECK-NEXT: 4: 0
28+
CHECK-NEXT: 65526: 62
29+
CHECK-NEXT: 3.1: bar:546
30+
CHECK-NEXT: 1: 42
31+
CHECK-NEXT: 65533: 42
32+
CHECK-NEXT: 3.2: bar:189
33+
CHECK-NEXT: 1: 21
34+
35+
CHECK-MARK-PREINLINED: main:2609:0
36+
CHECK-MARK-PREINLINED-NEXT: 0: 0
37+
CHECK-MARK-PREINLINED-NEXT: 2: 0
38+
CHECK-MARK-PREINLINED-NEXT: 1: foo:2609
39+
CHECK-MARK-PREINLINED-NEXT: 2.1: 42
40+
CHECK-MARK-PREINLINED-NEXT: 3: 62
41+
CHECK-MARK-PREINLINED-NEXT: 3.2: 21
42+
CHECK-MARK-PREINLINED-NEXT: 4: 0
43+
CHECK-MARK-PREINLINED-NEXT: 65526: 62
44+
CHECK-MARK-PREINLINED-NEXT: 3.1: bar:546
45+
CHECK-MARK-PREINLINED-NEXT: 1: 42
46+
CHECK-MARK-PREINLINED-NEXT: 65533: 42
47+
CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
48+
CHECK-MARK-PREINLINED-NEXT: 3.2: bar:189
49+
CHECK-MARK-PREINLINED-NEXT: 1: 21
50+
CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
51+
CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
52+
CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
3153

3254
CHECK-UPDATE-TOTAL-SAMPLE: main:292:0
3355
CHECK-UPDATE-TOTAL-SAMPLE: 0: 0

llvm/test/tools/llvm-profgen/inline-pseudoprobe.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; RUN: llvm-profgen --format=text --ignore-stack-samples --perfscript=%S/Inputs/inline-cs-pseudoprobe.perfscript --binary=%S/Inputs/inline-cs-pseudoprobe.perfbin --output=%t --profile-summary-cold-count=0
22
; RUN: FileCheck %s --input-file %t
3+
; RUN: llvm-profgen --format=text --ignore-stack-samples --perfscript=%S/Inputs/inline-cs-pseudoprobe.perfscript --binary=%S/Inputs/inline-cs-pseudoprobe.perfbin --output=%t1 --profile-summary-cold-count=0 --mark-all-context-preinlined
4+
; RUN: FileCheck %s --input-file %t1 --check-prefix=CHECK-MARK-PREINLINED
35

46
; CHECK: main:88:0
57
; CHECK-NEXT: 1: 0
@@ -19,6 +21,26 @@
1921
; CHECK-NEXT: !CFGChecksum: 563088904013236
2022
; CHECK-NEXT: !CFGChecksum: 281479271677951
2123

24+
; CHECK-MARK-PREINLINED: main:88:0
25+
; CHECK-MARK-PREINLINED-NEXT: 1: 0
26+
; CHECK-MARK-PREINLINED-NEXT: 2: foo:88
27+
; CHECK-MARK-PREINLINED-NEXT: 1: 0
28+
; CHECK-MARK-PREINLINED-NEXT: 2: 15
29+
; CHECK-MARK-PREINLINED-NEXT: 3: 15
30+
; CHECK-MARK-PREINLINED-NEXT: 4: 14
31+
; CHECK-MARK-PREINLINED-NEXT: 5: 1
32+
; CHECK-MARK-PREINLINED-NEXT: 6: 15
33+
; CHECK-MARK-PREINLINED-NEXT: 7: 0
34+
; CHECK-MARK-PREINLINED-NEXT: 9: 0
35+
; CHECK-MARK-PREINLINED-NEXT: 8: bar:28
36+
; CHECK-MARK-PREINLINED-NEXT: 1: 14
37+
; CHECK-MARK-PREINLINED-NEXT: 4: 14
38+
; CHECK-MARK-PREINLINED-NEXT: !CFGChecksum: 72617220756
39+
; CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
40+
; CHECK-MARK-PREINLINED-NEXT: !CFGChecksum: 563088904013236
41+
; CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
42+
; CHECK-MARK-PREINLINED-NEXT: !CFGChecksum: 281479271677951
43+
; CHECK-MARK-PREINLINED-NEXT: !Attributes: 2
2244

2345
; clang -O3 -fuse-ld=lld -fpseudo-probe-for-profiling
2446
; -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -Xclang -mdisable-tail-calls

llvm/tools/llvm-profgen/ProfileGenerator.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ static cl::opt<bool>
6464
"than threshold, it will be trimmed."),
6565
cl::cat(ProfGenCategory));
6666

67+
static cl::opt<bool> MarkAllContextPreinlined(
68+
"mark-all-context-preinlined",
69+
cl::desc("Mark all function samples as preinlined(set "
70+
"ContextShouldBeInlined attribute)."),
71+
cl::init(false));
72+
6773
static cl::opt<bool> CSProfMergeColdContext(
6874
"csprof-merge-cold-context", cl::init(true),
6975
cl::desc("If the total count of context profile is smaller than "
@@ -511,10 +517,19 @@ void ProfileGenerator::generateProfile() {
511517
postProcessProfiles();
512518
}
513519

520+
void ProfileGeneratorBase::markAllContextPreinlined(
521+
SampleProfileMap &ProfileMap) {
522+
for (auto &I : ProfileMap)
523+
I.second.setContextAttribute(ContextShouldBeInlined);
524+
FunctionSamples::ProfileIsPreInlined = true;
525+
}
526+
514527
void ProfileGenerator::postProcessProfiles() {
515528
computeSummaryAndThreshold(ProfileMap);
516529
trimColdProfiles(ProfileMap, ColdCountThreshold);
517530
filterAmbiguousProfile(ProfileMap);
531+
if (MarkAllContextPreinlined)
532+
markAllContextPreinlined(ProfileMap);
518533
calculateAndShowDensity(ProfileMap);
519534
}
520535

@@ -1130,6 +1145,8 @@ void CSProfileGenerator::postProcessProfiles() {
11301145
FunctionSamples::ProfileIsCS = false;
11311146
}
11321147
filterAmbiguousProfile(ProfileMap);
1148+
if (MarkAllContextPreinlined)
1149+
markAllContextPreinlined(ProfileMap);
11331150
ProfileGeneratorBase::calculateAndShowDensity(ProfileMap);
11341151
}
11351152

llvm/tools/llvm-profgen/ProfileGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class ProfileGeneratorBase {
123123

124124
void showDensitySuggestion(double Density);
125125

126+
void markAllContextPreinlined(SampleProfileMap &ProfileMap);
127+
126128
void collectProfiledFunctions();
127129

128130
bool collectFunctionsFromRawProfile(

0 commit comments

Comments
 (0)