Skip to content

Commit d27de9f

Browse files
committed
[ctx_prof] Profile flatterner
1 parent cbd3024 commit d27de9f

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

llvm/include/llvm/Analysis/CtxProfAnalysis.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_ANALYSIS_CTXPROFANALYSIS_H
1010
#define LLVM_ANALYSIS_CTXPROFANALYSIS_H
1111

12+
#include "llvm/ADT/DenseMap.h"
13+
#include "llvm/IR/GlobalValue.h"
1214
#include "llvm/IR/InstrTypes.h"
1315
#include "llvm/IR/IntrinsicInst.h"
1416
#include "llvm/IR/PassManager.h"
@@ -18,6 +20,12 @@ namespace llvm {
1820

1921
class CtxProfAnalysis;
2022

23+
// Setting initial capacity to 1 because all contexts must have at least 1
24+
// counter, and then, because all contexts belonging to a function have the same
25+
// size, there'll be at most one other heap allocation.
26+
using CtxProfFlatProfile =
27+
DenseMap<GlobalValue::GUID, SmallVector<uint64_t, 1>>;
28+
2129
/// The instrumented contextual profile, produced by the CtxProfAnalysis.
2230
class PGOContextualProfile {
2331
friend class CtxProfAnalysis;
@@ -65,6 +73,8 @@ class PGOContextualProfile {
6573
return FuncInfo.find(getDefinedFunctionGUID(F))->second.NextCallsiteIndex++;
6674
}
6775

76+
const CtxProfFlatProfile flatten() const;
77+
6878
bool invalidate(Module &, const PreservedAnalyses &PA,
6979
ModuleAnalysisManager::Invalidator &) {
7080
// Check whether the analysis has been explicitly invalidated. Otherwise,

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ PreservedAnalyses CtxProfAnalysisPrinterPass::run(Module &M,
184184
OS << "\nCurrent Profile:\n";
185185
OS << formatv("{0:2}", JSONed);
186186
OS << "\n";
187+
OS << "\nFlat Profile:\n";
188+
auto Flat = C.flatten();
189+
for (const auto &[Guid, Counters] : Flat) {
190+
OS << Guid << " : ";
191+
for (auto V : Counters)
192+
OS << V << " ";
193+
OS << "\n";
194+
}
187195
return PreservedAnalyses::all();
188196
}
189197

@@ -193,3 +201,35 @@ InstrProfCallsite *CtxProfAnalysis::getCallsiteInstrumentation(CallBase &CB) {
193201
return IPC;
194202
return nullptr;
195203
}
204+
205+
static void
206+
preorderVisit(const PGOCtxProfContext::CallTargetMapTy &Profiles,
207+
function_ref<void(const PGOCtxProfContext &)> Visitor) {
208+
std::function<void(const PGOCtxProfContext &)> Traverser =
209+
[&](const auto &Ctx) {
210+
Visitor(Ctx);
211+
for (const auto &[_, SubCtxSet] : Ctx.callsites())
212+
for (const auto &[__, Subctx] : SubCtxSet)
213+
Traverser(Subctx);
214+
};
215+
for (const auto &[_, P] : Profiles)
216+
Traverser(P);
217+
}
218+
219+
const CtxProfFlatProfile PGOContextualProfile::flatten() const {
220+
assert(Profiles.has_value());
221+
CtxProfFlatProfile Flat;
222+
preorderVisit(*Profiles, [&](const PGOCtxProfContext &Ctx) {
223+
auto [It, Ins] = Flat.insert({Ctx.guid(), {}});
224+
if (Ins) {
225+
llvm::append_range(It->second, Ctx.counters());
226+
return;
227+
}
228+
assert(It->second.size() == Ctx.counters().size() &&
229+
"All contexts corresponding to a function should have the exact "
230+
"same number of counters.");
231+
for (size_t I = 0, E = It->second.size(); I < E; ++I)
232+
It->second[I] += Ctx.counters()[I];
233+
});
234+
return Flat;
235+
}

llvm/test/Analysis/CtxProfAnalysis/full-cycle.ll

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
; RUN: split-file %s %t
55
;
66
; Test that the GUID metadata survives through thinlink.
7+
; Also test that the flattener works correctly. f2 is called in 2 places, with
8+
; different counter values, and we expect resulting flat profile to be the sum
9+
; (of values at the same index).
710
;
811
; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata
912
;
@@ -17,7 +20,9 @@
1720
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc -o %t/ -thinlto-distributed-indexes \
1821
; RUN: -use-ctx-profile=%t/profile.ctxprofdata \
1922
; RUN: -r %t/m1.bc,f1,plx \
23+
; RUN: -r %t/m1.bc,f3,plx \
2024
; RUN: -r %t/m2.bc,f1 \
25+
; RUN: -r %t/m2.bc,f3 \
2126
; RUN: -r %t/m2.bc,entrypoint,plx
2227
; RUN: opt --passes='function-import,require<ctx-prof-analysis>,print<ctx-prof-analysis>' \
2328
; RUN: -summary-file=%t/m2.bc.thinlto.bc -use-ctx-profile=%t/profile.ctxprofdata %t/m2.bc \
@@ -38,6 +43,11 @@ define void @f1() #0 {
3843
ret void
3944
}
4045

46+
define void @f3() #0 {
47+
call void @f2()
48+
ret void
49+
}
50+
4151
attributes #0 = { noinline }
4252
!0 = !{ i64 3087265239403591524 }
4353

@@ -48,9 +58,11 @@ target triple = "x86_64-pc-linux-gnu"
4858
source_filename = "random_path/m2.cc"
4959

5060
declare void @f1()
61+
declare void @f3()
5162

5263
define void @entrypoint() {
5364
call void @f1()
65+
call void @f3()
5466
ret void
5567
}
5668
;--- profile.json
@@ -63,7 +75,8 @@ define void @entrypoint() {
6375
[
6476
{
6577
"Counters": [
66-
10
78+
10,
79+
7
6780
],
6881
"Guid": 3087265239403591524
6982
}
@@ -74,6 +87,25 @@ define void @entrypoint() {
7487
],
7588
"Guid": 2072045998141807037
7689
}
90+
],
91+
[
92+
{
93+
"Callsites": [
94+
[
95+
{
96+
"Counters": [
97+
1,
98+
2
99+
],
100+
"Guid": 3087265239403591524
101+
}
102+
]
103+
],
104+
"Counters": [
105+
2
106+
],
107+
"Guid": 4197650231481825559
108+
}
77109
]
78110
],
79111
"Counters": [
@@ -84,8 +116,9 @@ define void @entrypoint() {
84116
]
85117
;--- expected.txt
86118
Function Info:
87-
10507721908651011566 : entrypoint. MaxCounterID: 1. MaxCallsiteID: 1
119+
10507721908651011566 : entrypoint. MaxCounterID: 1. MaxCallsiteID: 2
88120
3087265239403591524 : f2.llvm.0. MaxCounterID: 1. MaxCallsiteID: 0
121+
4197650231481825559 : f3. MaxCounterID: 1. MaxCallsiteID: 1
89122
2072045998141807037 : f1. MaxCounterID: 1. MaxCallsiteID: 1
90123

91124
Current Profile:
@@ -98,7 +131,8 @@ Current Profile:
98131
[
99132
{
100133
"Counters": [
101-
10
134+
10,
135+
7
102136
],
103137
"Guid": 3087265239403591524
104138
}
@@ -109,6 +143,25 @@ Current Profile:
109143
],
110144
"Guid": 2072045998141807037
111145
}
146+
],
147+
[
148+
{
149+
"Callsites": [
150+
[
151+
{
152+
"Counters": [
153+
1,
154+
2
155+
],
156+
"Guid": 3087265239403591524
157+
}
158+
]
159+
],
160+
"Counters": [
161+
2
162+
],
163+
"Guid": 4197650231481825559
164+
}
112165
]
113166
],
114167
"Counters": [
@@ -117,3 +170,9 @@ Current Profile:
117170
"Guid": 10507721908651011566
118171
}
119172
]
173+
174+
Flat Profile:
175+
10507721908651011566 : 1
176+
3087265239403591524 : 11 9
177+
4197650231481825559 : 2
178+
2072045998141807037 : 7

llvm/test/Analysis/CtxProfAnalysis/load.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ Current Profile:
8686
"Guid": 12074870348631550642
8787
}
8888
]
89+
90+
Flat Profile:
91+
728453322856651412 : 6 7
92+
12074870348631550642 : 5
93+
11872291593386833696 : 1
8994
;--- example.ll
9095
declare void @bar()
9196

0 commit comments

Comments
 (0)