Skip to content

Commit cdd6c12

Browse files
committed
[ctx_prof] Handle case when no root is in this Module.
1 parent 787cd8f commit cdd6c12

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
; REQUIRES: x86_64-linux
2+
;
3+
; Check that, if none of the roots in the profile are defined in the module, the
4+
; profile is treated as empty (i.e. "none provided")
5+
;
6+
; RUN: rm -rf %t
7+
; RUN: split-file %s %t
8+
; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata
9+
; RUN: opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' -ctx-profile-printer-level=everything \
10+
; RUN: %t/example.ll -S 2>&1 | FileCheck %s
11+
12+
; CHECK: No contextual profile was provided
13+
;
14+
; This is the reference profile, laid out in the format the json formatter will
15+
; output it from opt.
16+
;--- profile.json
17+
[
18+
{
19+
"Counters": [
20+
9
21+
],
22+
"Guid": 12341
23+
},
24+
{
25+
"Counters": [
26+
5
27+
],
28+
"Guid": 12074870348631550642
29+
},
30+
{
31+
"Callsites": [
32+
[
33+
{
34+
"Counters": [
35+
6,
36+
7
37+
],
38+
"Guid": 728453322856651412
39+
}
40+
]
41+
],
42+
"Counters": [
43+
1
44+
],
45+
"Guid": 11872291593386833696
46+
}
47+
]
48+
;--- example.ll
49+
declare void @bar()
50+
51+
define void @an_entrypoint(i32 %a) !guid !0 {
52+
%t = icmp eq i32 %a, 0
53+
br i1 %t, label %yes, label %no
54+
55+
yes:
56+
call void @bar()
57+
ret void
58+
no:
59+
ret void
60+
}
61+
62+
attributes #0 = { noinline }
63+
!0 = !{ i64 1000 }

0 commit comments

Comments
 (0)