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
.isFunctionEntryHot(F
));
141 EXPECT_FALSE(PSI
.isFunctionEntryHot(G
));
142 EXPECT_FALSE(PSI
.isFunctionEntryHot(H
));
145 TEST_F(ProfileSummaryInfoTest
, InstrProf
) {
146 auto M
= makeLLVMModule("InstrProf");
147 Function
*F
= M
->getFunction("f");
148 ProfileSummaryInfo PSI
= buildPSI(M
.get());
149 EXPECT_TRUE(PSI
.hasProfileSummary());
150 EXPECT_TRUE(PSI
.hasInstrumentationProfile());
152 BasicBlock
&BB0
= F
->getEntryBlock();
153 BasicBlock
*BB1
= BB0
.getTerminator()->getSuccessor(0);
154 BasicBlock
*BB2
= BB0
.getTerminator()->getSuccessor(1);
155 BasicBlock
*BB3
= BB1
->getSingleSuccessor();
157 BlockFrequencyInfo BFI
= buildBFI(*F
);
158 EXPECT_TRUE(PSI
.isHotBlock(&BB0
, &BFI
));
159 EXPECT_TRUE(PSI
.isHotBlock(BB1
, &BFI
));
160 EXPECT_FALSE(PSI
.isHotBlock(BB2
, &BFI
));
161 EXPECT_TRUE(PSI
.isHotBlock(BB3
, &BFI
));
163 CallSite
CS1(BB1
->getFirstNonPHI());
164 auto *CI2
= BB2
->getFirstNonPHI();
167 EXPECT_TRUE(PSI
.isHotCallSite(CS1
, &BFI
));
168 EXPECT_FALSE(PSI
.isHotCallSite(CS2
, &BFI
));
170 // Test that adding an MD_prof metadata with a hot count on CS2 does not
171 // change its hotness as it has no effect in instrumented profiling.
172 MDBuilder
MDB(M
->getContext());
173 CI2
->setMetadata(llvm::LLVMContext::MD_prof
, MDB
.createBranchWeights({400}));
174 EXPECT_FALSE(PSI
.isHotCallSite(CS2
, &BFI
));
177 TEST_F(ProfileSummaryInfoTest
, SampleProf
) {
178 auto M
= makeLLVMModule("SampleProfile");
179 Function
*F
= M
->getFunction("f");
180 ProfileSummaryInfo PSI
= buildPSI(M
.get());
181 EXPECT_TRUE(PSI
.hasProfileSummary());
182 EXPECT_TRUE(PSI
.hasSampleProfile());
184 BasicBlock
&BB0
= F
->getEntryBlock();
185 BasicBlock
*BB1
= BB0
.getTerminator()->getSuccessor(0);
186 BasicBlock
*BB2
= BB0
.getTerminator()->getSuccessor(1);
187 BasicBlock
*BB3
= BB1
->getSingleSuccessor();
189 BlockFrequencyInfo BFI
= buildBFI(*F
);
190 EXPECT_TRUE(PSI
.isHotBlock(&BB0
, &BFI
));
191 EXPECT_TRUE(PSI
.isHotBlock(BB1
, &BFI
));
192 EXPECT_FALSE(PSI
.isHotBlock(BB2
, &BFI
));
193 EXPECT_TRUE(PSI
.isHotBlock(BB3
, &BFI
));
195 CallSite
CS1(BB1
->getFirstNonPHI());
196 auto *CI2
= BB2
->getFirstNonPHI();
197 // Manually attach branch weights metadata to the call instruction.
198 SmallVector
<uint32_t, 1> Weights
;
199 Weights
.push_back(1000);
200 MDBuilder
MDB(M
->getContext());
201 CI2
->setMetadata(LLVMContext::MD_prof
, MDB
.createBranchWeights(Weights
));
204 EXPECT_FALSE(PSI
.isHotCallSite(CS1
, &BFI
));
205 EXPECT_TRUE(PSI
.isHotCallSite(CS2
, &BFI
));
207 // Test that CS2 is considered hot when it gets an MD_prof metadata with
208 // weights that exceed the hot count threshold.
209 CI2
->setMetadata(llvm::LLVMContext::MD_prof
, MDB
.createBranchWeights({400}));
210 EXPECT_TRUE(PSI
.isHotCallSite(CS2
, &BFI
));
213 } // end anonymous namespace
214 } // end namespace llvm