1 //===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/ProfileSummaryInfo.h"
10 #include "llvm/Analysis/BlockFrequencyInfo.h"
11 #include "llvm/Analysis/BranchProbabilityInfo.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/IR/BasicBlock.h"
15 #include "llvm/IR/CallSite.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/MDBuilder.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/FormatVariadic.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "gtest/gtest.h"
30 class ProfileSummaryInfoTest
: public testing::Test
{
33 std::unique_ptr
<BranchProbabilityInfo
> BPI
;
34 std::unique_ptr
<DominatorTree
> DT
;
35 std::unique_ptr
<LoopInfo
> LI
;
37 ProfileSummaryInfo
buildPSI(Module
*M
) {
38 return ProfileSummaryInfo(*M
);
40 BlockFrequencyInfo
buildBFI(Function
&F
) {
41 DT
.reset(new DominatorTree(F
));
42 LI
.reset(new LoopInfo(*DT
));
43 BPI
.reset(new BranchProbabilityInfo(F
, *LI
));
44 return BlockFrequencyInfo(F
, *BPI
, *LI
);
46 std::unique_ptr
<Module
> makeLLVMModule(const char *ProfKind
= nullptr) {
47 const char *ModuleString
=
48 "define i32 @g(i32 %x) !prof !21 {{\n"
51 "define i32 @h(i32 %x) !prof !22 {{\n"
54 "define i32 @f(i32 %x) !prof !20 {{\n"
56 " %y1 = icmp eq i32 %x, 0 \n"
57 " br i1 %y1, label %bb1, label %bb2, !prof !23 \n"
59 " %z1 = call i32 @g(i32 %x)\n"
62 " %z2 = call i32 @h(i32 %x)\n"
65 " %y2 = phi i32 [0, %bb1], [1, %bb2] \n"
68 "!20 = !{{!\"function_entry_count\", i64 400}\n"
69 "!21 = !{{!\"function_entry_count\", i64 1}\n"
70 "!22 = !{{!\"function_entry_count\", i64 100}\n"
71 "!23 = !{{!\"branch_weights\", i32 64, i32 4}\n"
73 const char *SummaryString
= "!llvm.module.flags = !{{!1}"
74 "!1 = !{{i32 1, !\"ProfileSummary\", !2}"
75 "!2 = !{{!3, !4, !5, !6, !7, !8, !9, !10}"
76 "!3 = !{{!\"ProfileFormat\", !\"{0}\"}"
77 "!4 = !{{!\"TotalCount\", i64 10000}"
78 "!5 = !{{!\"MaxCount\", i64 10}"
79 "!6 = !{{!\"MaxInternalCount\", i64 1}"
80 "!7 = !{{!\"MaxFunctionCount\", i64 1000}"
81 "!8 = !{{!\"NumCounts\", i64 3}"
82 "!9 = !{{!\"NumFunctions\", i64 3}"
83 "!10 = !{{!\"DetailedSummary\", !11}"
84 "!11 = !{{!12, !13, !14}"
85 "!12 = !{{i32 10000, i64 1000, i32 1}"
86 "!13 = !{{i32 999000, i64 300, i32 3}"
87 "!14 = !{{i32 999999, i64 5, i32 10}";
90 return parseAssemblyString(
91 formatv(ModuleString
, formatv(SummaryString
, ProfKind
).str()).str(),
94 return parseAssemblyString(formatv(ModuleString
, "").str(), Err
, C
);
98 TEST_F(ProfileSummaryInfoTest
, TestNoProfile
) {
99 auto M
= makeLLVMModule(/*ProfKind=*/nullptr);
100 Function
*F
= M
->getFunction("f");
102 ProfileSummaryInfo PSI
= buildPSI(M
.get());
103 EXPECT_FALSE(PSI
.hasProfileSummary());
104 EXPECT_FALSE(PSI
.hasSampleProfile());
105 EXPECT_FALSE(PSI
.hasInstrumentationProfile());
106 // In the absence of profiles, is{Hot|Cold}X methods should always return
108 EXPECT_FALSE(PSI
.isHotCount(1000));
109 EXPECT_FALSE(PSI
.isHotCount(0));
110 EXPECT_FALSE(PSI
.isColdCount(1000));
111 EXPECT_FALSE(PSI
.isColdCount(0));
113 EXPECT_FALSE(PSI
.isFunctionEntryHot(F
));
114 EXPECT_FALSE(PSI
.isFunctionEntryCold(F
));
116 BasicBlock
&BB0
= F
->getEntryBlock();
117 BasicBlock
*BB1
= BB0
.getTerminator()->getSuccessor(0);
119 BlockFrequencyInfo BFI
= buildBFI(*F
);
120 EXPECT_FALSE(PSI
.isHotBlock(&BB0
, &BFI
));
121 EXPECT_FALSE(PSI
.isColdBlock(&BB0
, &BFI
));
123 CallSite
CS1(BB1
->getFirstNonPHI());
124 EXPECT_FALSE(PSI
.isHotCallSite(CS1
, &BFI
));
125 EXPECT_FALSE(PSI
.isColdCallSite(CS1
, &BFI
));
127 TEST_F(ProfileSummaryInfoTest
, TestCommon
) {
128 auto M
= makeLLVMModule("InstrProf");
129 Function
*F
= M
->getFunction("f");
130 Function
*G
= M
->getFunction("g");
131 Function
*H
= M
->getFunction("h");
133 ProfileSummaryInfo PSI
= buildPSI(M
.get());
134 EXPECT_TRUE(PSI
.hasProfileSummary());
135 EXPECT_TRUE(PSI
.isHotCount(400));
136 EXPECT_TRUE(PSI
.isColdCount(2));
137 EXPECT_FALSE(PSI
.isColdCount(100));
138 EXPECT_FALSE(PSI
.isHotCount(100));
140 EXPECT_TRUE(PSI
.isHotCountNthPercentile(990000, 400));
141 EXPECT_FALSE(PSI
.isHotCountNthPercentile(990000, 100));
142 EXPECT_FALSE(PSI
.isHotCountNthPercentile(990000, 2));
144 EXPECT_TRUE(PSI
.isHotCountNthPercentile(999999, 400));
145 EXPECT_TRUE(PSI
.isHotCountNthPercentile(999999, 100));
146 EXPECT_FALSE(PSI
.isHotCountNthPercentile(999999, 2));
148 EXPECT_FALSE(PSI
.isHotCountNthPercentile(10000, 400));
149 EXPECT_FALSE(PSI
.isHotCountNthPercentile(10000, 100));
150 EXPECT_FALSE(PSI
.isHotCountNthPercentile(10000, 2));
152 EXPECT_TRUE(PSI
.isFunctionEntryHot(F
));
153 EXPECT_FALSE(PSI
.isFunctionEntryHot(G
));
154 EXPECT_FALSE(PSI
.isFunctionEntryHot(H
));
157 TEST_F(ProfileSummaryInfoTest
, InstrProf
) {
158 auto M
= makeLLVMModule("InstrProf");
159 Function
*F
= M
->getFunction("f");
160 ProfileSummaryInfo PSI
= buildPSI(M
.get());
161 EXPECT_TRUE(PSI
.hasProfileSummary());
162 EXPECT_TRUE(PSI
.hasInstrumentationProfile());
164 BasicBlock
&BB0
= F
->getEntryBlock();
165 BasicBlock
*BB1
= BB0
.getTerminator()->getSuccessor(0);
166 BasicBlock
*BB2
= BB0
.getTerminator()->getSuccessor(1);
167 BasicBlock
*BB3
= BB1
->getSingleSuccessor();
169 BlockFrequencyInfo BFI
= buildBFI(*F
);
170 EXPECT_TRUE(PSI
.isHotBlock(&BB0
, &BFI
));
171 EXPECT_TRUE(PSI
.isHotBlock(BB1
, &BFI
));
172 EXPECT_FALSE(PSI
.isHotBlock(BB2
, &BFI
));
173 EXPECT_TRUE(PSI
.isHotBlock(BB3
, &BFI
));
175 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, &BB0
, &BFI
));
176 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, BB1
, &BFI
));
177 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(990000, BB2
, &BFI
));
178 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, BB3
, &BFI
));
180 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, &BB0
, &BFI
));
181 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB1
, &BFI
));
182 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB2
, &BFI
));
183 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB3
, &BFI
));
185 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, &BB0
, &BFI
));
186 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB1
, &BFI
));
187 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB2
, &BFI
));
188 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB3
, &BFI
));
190 CallSite
CS1(BB1
->getFirstNonPHI());
191 auto *CI2
= BB2
->getFirstNonPHI();
194 EXPECT_TRUE(PSI
.isHotCallSite(CS1
, &BFI
));
195 EXPECT_FALSE(PSI
.isHotCallSite(CS2
, &BFI
));
197 // Test that adding an MD_prof metadata with a hot count on CS2 does not
198 // change its hotness as it has no effect in instrumented profiling.
199 MDBuilder
MDB(M
->getContext());
200 CI2
->setMetadata(llvm::LLVMContext::MD_prof
, MDB
.createBranchWeights({400}));
201 EXPECT_FALSE(PSI
.isHotCallSite(CS2
, &BFI
));
204 TEST_F(ProfileSummaryInfoTest
, SampleProf
) {
205 auto M
= makeLLVMModule("SampleProfile");
206 Function
*F
= M
->getFunction("f");
207 ProfileSummaryInfo PSI
= buildPSI(M
.get());
208 EXPECT_TRUE(PSI
.hasProfileSummary());
209 EXPECT_TRUE(PSI
.hasSampleProfile());
211 BasicBlock
&BB0
= F
->getEntryBlock();
212 BasicBlock
*BB1
= BB0
.getTerminator()->getSuccessor(0);
213 BasicBlock
*BB2
= BB0
.getTerminator()->getSuccessor(1);
214 BasicBlock
*BB3
= BB1
->getSingleSuccessor();
216 BlockFrequencyInfo BFI
= buildBFI(*F
);
217 EXPECT_TRUE(PSI
.isHotBlock(&BB0
, &BFI
));
218 EXPECT_TRUE(PSI
.isHotBlock(BB1
, &BFI
));
219 EXPECT_FALSE(PSI
.isHotBlock(BB2
, &BFI
));
220 EXPECT_TRUE(PSI
.isHotBlock(BB3
, &BFI
));
222 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, &BB0
, &BFI
));
223 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, BB1
, &BFI
));
224 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(990000, BB2
, &BFI
));
225 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(990000, BB3
, &BFI
));
227 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, &BB0
, &BFI
));
228 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB1
, &BFI
));
229 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB2
, &BFI
));
230 EXPECT_TRUE(PSI
.isHotBlockNthPercentile(999900, BB3
, &BFI
));
232 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, &BB0
, &BFI
));
233 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB1
, &BFI
));
234 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB2
, &BFI
));
235 EXPECT_FALSE(PSI
.isHotBlockNthPercentile(10000, BB3
, &BFI
));
237 CallSite
CS1(BB1
->getFirstNonPHI());
238 auto *CI2
= BB2
->getFirstNonPHI();
239 // Manually attach branch weights metadata to the call instruction.
240 SmallVector
<uint32_t, 1> Weights
;
241 Weights
.push_back(1000);
242 MDBuilder
MDB(M
->getContext());
243 CI2
->setMetadata(LLVMContext::MD_prof
, MDB
.createBranchWeights(Weights
));
246 EXPECT_FALSE(PSI
.isHotCallSite(CS1
, &BFI
));
247 EXPECT_TRUE(PSI
.isHotCallSite(CS2
, &BFI
));
249 // Test that CS2 is considered hot when it gets an MD_prof metadata with
250 // weights that exceed the hot count threshold.
251 CI2
->setMetadata(llvm::LLVMContext::MD_prof
, MDB
.createBranchWeights({400}));
252 EXPECT_TRUE(PSI
.isHotCallSite(CS2
, &BFI
));
255 } // end anonymous namespace
256 } // end namespace llvm