Skip to content

Commit 53b11dd

Browse files
committed
[ctx_prof] Handle case when no root is in this Module.
1 parent f32e5bd commit 53b11dd

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

llvm/lib/Analysis/CtxProfAnalysis.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M,
132132
return {};
133133
}
134134

135+
DenseSet<GlobalValue::GUID> ProfileRootsInModule;
136+
for (const auto &F : M)
137+
if (!F.isDeclaration())
138+
if (auto GUID = AssignGUIDPass::getGUID(F);
139+
MaybeCtx->find(GUID) != MaybeCtx->end())
140+
ProfileRootsInModule.insert(GUID);
141+
142+
// Trim first the roots that aren't in this module.
143+
for (auto &[RootGuid, _] : llvm::make_early_inc_range(*MaybeCtx))
144+
if (!ProfileRootsInModule.contains(RootGuid))
145+
MaybeCtx->erase(RootGuid);
146+
// If none of the roots are in the module, we have no profile (for this
147+
// module)
148+
if (MaybeCtx->empty())
149+
return {};
150+
151+
// OK, so we have a valid profile and it's applicable to roots in this module.
135152
PGOContextualProfile Result;
136153

137154
for (const auto &F : M) {
@@ -166,10 +183,6 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M,
166183
}
167184
// If we made it this far, the Result is valid - which we mark by setting
168185
// .Profiles.
169-
// Trim first the roots that aren't in this module.
170-
for (auto &[RootGuid, _] : llvm::make_early_inc_range(*MaybeCtx))
171-
if (!Result.FuncInfo.contains(RootGuid))
172-
MaybeCtx->erase(RootGuid);
173186
Result.Profiles = std::move(*MaybeCtx);
174187
return Result;
175188
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; REQUIRES: x86_64-linux
2+
;
3+
; RUN: rm -rf %t
4+
; RUN: split-file %s %t
5+
; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata
6+
; RUN: opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' -ctx-profile-printer-level=everything \
7+
; RUN: %t/example.ll -S 2>&1 | FileCheck %s
8+
9+
; CHECK: No contextual profile was provided
10+
;
11+
; This is the reference profile, laid out in the format the json formatter will
12+
; output it from opt.
13+
;--- profile.json
14+
[
15+
{
16+
"Counters": [
17+
9
18+
],
19+
"Guid": 12341
20+
},
21+
{
22+
"Counters": [
23+
5
24+
],
25+
"Guid": 12074870348631550642
26+
},
27+
{
28+
"Callsites": [
29+
[
30+
{
31+
"Counters": [
32+
6,
33+
7
34+
],
35+
"Guid": 728453322856651412
36+
}
37+
]
38+
],
39+
"Counters": [
40+
1
41+
],
42+
"Guid": 11872291593386833696
43+
}
44+
]
45+
;--- example.ll
46+
declare void @bar()
47+
48+
define void @an_entrypoint(i32 %a) {
49+
%t = icmp eq i32 %a, 0
50+
br i1 %t, label %yes, label %no
51+
52+
yes:
53+
call void @bar()
54+
ret void
55+
no:
56+
ret void
57+
}
58+
59+
attributes #0 = { noinline }
60+
!0 = !{ i64 1000 }

0 commit comments

Comments
 (0)