| 1 |
//===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 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/Dominators.h" |
| 16 |
#include "llvm/IR/Function.h" |
| 17 |
#include "llvm/IR/LLVMContext.h" |
| 18 |
#include "llvm/IR/MDBuilder.h" |
| 19 |
#include "llvm/IR/Module.h" |
| 20 |
#include "llvm/Support/CommandLine.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" |
| 26 |
|
| 27 |
extern llvm::cl::opt<bool> ScalePartialSampleProfileWorkingSetSize; |
| 28 |
|
| 29 |
namespace llvm { |
| 30 |
namespace { |
| 31 |
|
| 32 |
class ProfileSummaryInfoTest : public testing::Test { |
| 33 |
protected: |
| 34 |
LLVMContext C; |
| 35 |
std::unique_ptr<BranchProbabilityInfo> BPI; |
| 36 |
std::unique_ptr<DominatorTree> DT; |
| 37 |
std::unique_ptr<LoopInfo> LI; |
| 38 |
|
| 39 |
ProfileSummaryInfo buildPSI(Module *M) { |
| 40 |
return ProfileSummaryInfo(*M); |
| 41 |
} |
| 42 |
BlockFrequencyInfo buildBFI(Function &F) { |
| 43 |
DT.reset(new DominatorTree(F)); |
| 44 |
LI.reset(new LoopInfo(*DT)); |
| 45 |
BPI.reset(new BranchProbabilityInfo(F, *LI)); |
| 46 |
return BlockFrequencyInfo(F, *BPI, *LI); |
| 47 |
} |
| 48 |
std::unique_ptr<Module> makeLLVMModule(const char *ProfKind = nullptr, |
| 49 |
uint64_t NumCounts = 3, |
| 50 |
uint64_t IsPartialProfile = 0, |
| 51 |
double PartialProfileRatio = 0.0, |
| 52 |
uint64_t HotNumCounts = 3, |
| 53 |
uint64_t ColdNumCounts = 10) { |
| 54 |
const char *ModuleString = |
| 55 |
"define i32 @g(i32 %x) !prof !21 {{\n" |
| 56 |
" ret i32 0\n" |
| 57 |
"}\n" |
| 58 |
"define i32 @h(i32 %x) !prof !22 {{\n" |
| 59 |
" ret i32 0\n" |
| 60 |
"}\n" |
| 61 |
"define i32 @f(i32 %x) !prof !20 {{\n" |
| 62 |
"bb0:\n" |
| 63 |
" %y1 = icmp eq i32 %x, 0 \n" |
| 64 |
" br i1 %y1, label %bb1, label %bb2, !prof !23 \n" |
| 65 |
"bb1:\n" |
| 66 |
" %z1 = call i32 @g(i32 %x)\n" |
| 67 |
" br label %bb3\n" |
| 68 |
"bb2:\n" |
| 69 |
" %z2 = call i32 @h(i32 %x)\n" |
| 70 |
" br label %bb3\n" |
| 71 |
"bb3:\n" |
| 72 |
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n" |
| 73 |
" ret i32 %y2\n" |
| 74 |
"}\n" |
| 75 |
"define i32 @l(i32 %x) {{\n" |
| 76 |
"bb0:\n" |
| 77 |
" %y1 = icmp eq i32 %x, 0 \n" |
| 78 |
" br i1 %y1, label %bb1, label %bb2, !prof !23 \n" |
| 79 |
"bb1:\n" |
| 80 |
" %z1 = call i32 @g(i32 %x)\n" |
| 81 |
" br label %bb3\n" |
| 82 |
"bb2:\n" |
| 83 |
" %z2 = call i32 @h(i32 %x)\n" |
| 84 |
" br label %bb3\n" |
| 85 |
"bb3:\n" |
| 86 |
" %y2 = phi i32 [0, %bb1], [1, %bb2] \n" |
| 87 |
" ret i32 %y2\n" |
| 88 |
"}\n" |
| 89 |
"!20 = !{{!\"function_entry_count\", i64 400}\n" |
| 90 |
"!21 = !{{!\"function_entry_count\", i64 1}\n" |
| 91 |
"!22 = !{{!\"function_entry_count\", i64 100}\n" |
| 92 |
"!23 = !{{!\"branch_weights\", i32 64, i32 4}\n" |
| 93 |
"{0}"; |
| 94 |
const char *SummaryString = |
| 95 |
"!llvm.module.flags = !{{!1}\n" |
| 96 |
"!1 = !{{i32 1, !\"ProfileSummary\", !2}\n" |
| 97 |
"!2 = !{{!3, !4, !5, !6, !7, !8, !9, !10, !11, !12}\n" |
| 98 |
"!3 = !{{!\"ProfileFormat\", !\"{0}\"}\n" |
| 99 |
"!4 = !{{!\"TotalCount\", i64 10000}\n" |
| 100 |
"!5 = !{{!\"MaxCount\", i64 10}\n" |
| 101 |
"!6 = !{{!\"MaxInternalCount\", i64 1}\n" |
| 102 |
"!7 = !{{!\"MaxFunctionCount\", i64 1000}\n" |
| 103 |
"!8 = !{{!\"NumCounts\", i64 {1}}\n" |
| 104 |
"!9 = !{{!\"NumFunctions\", i64 3}\n" |
| 105 |
"!10 = !{{!\"IsPartialProfile\", i64 {2}}\n" |
| 106 |
"!11 = !{{!\"PartialProfileRatio\", double {3}}\n" |
| 107 |
"!12 = !{{!\"DetailedSummary\", !13}\n" |
| 108 |
"!13 = !{{!14, !15, !16}\n" |
| 109 |
"!14 = !{{i32 10000, i64 1000, i32 1}\n" |
| 110 |
"!15 = !{{i32 990000, i64 300, i32 {4}}\n" |
| 111 |
"!16 = !{{i32 999999, i64 5, i32 {5}}\n"; |
| 112 |
SMDiagnostic Err; |
| 113 |
if (ProfKind) { |
| 114 |
auto Summary = |
| 115 |
formatv(SummaryString, ProfKind, NumCounts, IsPartialProfile, |
| 116 |
PartialProfileRatio, HotNumCounts, ColdNumCounts) |
| 117 |
.str(); |
| 118 |
return parseAssemblyString(formatv(ModuleString, Summary).str(), Err, C); |
| 119 |
} else |
| 120 |
return parseAssemblyString(formatv(ModuleString, "").str(), Err, C); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
TEST_F(ProfileSummaryInfoTest, TestNoProfile) { |
| 125 |
auto M = makeLLVMModule(/*ProfKind=*/nullptr); |
| 126 |
Function *F = M->getFunction("f"); |
| 127 |
|
| 128 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 129 |
EXPECT_FALSE(PSI.hasProfileSummary()); |
| 130 |
EXPECT_FALSE(PSI.hasSampleProfile()); |
| 131 |
EXPECT_FALSE(PSI.hasInstrumentationProfile()); |
| 132 |
// In the absence of profiles, is{Hot|Cold}X methods should always return |
| 133 |
// false. |
| 134 |
EXPECT_FALSE(PSI.isHotCount(1000)); |
| 135 |
EXPECT_FALSE(PSI.isHotCount(0)); |
| 136 |
EXPECT_FALSE(PSI.isColdCount(1000)); |
| 137 |
EXPECT_FALSE(PSI.isColdCount(0)); |
| 138 |
|
| 139 |
EXPECT_FALSE(PSI.isFunctionEntryHot(F)); |
| 140 |
EXPECT_FALSE(PSI.isFunctionEntryCold(F)); |
| 141 |
|
| 142 |
BasicBlock &BB0 = F->getEntryBlock(); |
| 143 |
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 144 |
|
| 145 |
BlockFrequencyInfo BFI = buildBFI(*F); |
| 146 |
EXPECT_FALSE(PSI.isHotBlock(&BB0, &BFI)); |
| 147 |
EXPECT_FALSE(PSI.isColdBlock(&BB0, &BFI)); |
| 148 |
|
| 149 |
CallBase &CS1 = cast<CallBase>(*BB1->getFirstNonPHI()); |
| 150 |
EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI)); |
| 151 |
EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI)); |
| 152 |
} |
| 153 |
TEST_F(ProfileSummaryInfoTest, TestCommon) { |
| 154 |
auto M = makeLLVMModule("InstrProf"); |
| 155 |
Function *F = M->getFunction("f"); |
| 156 |
Function *G = M->getFunction("g"); |
| 157 |
Function *H = M->getFunction("h"); |
| 158 |
|
| 159 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 160 |
EXPECT_TRUE(PSI.hasProfileSummary()); |
| 161 |
EXPECT_TRUE(PSI.isHotCount(400)); |
| 162 |
EXPECT_TRUE(PSI.isColdCount(2)); |
| 163 |
EXPECT_FALSE(PSI.isColdCount(100)); |
| 164 |
EXPECT_FALSE(PSI.isHotCount(100)); |
| 165 |
|
| 166 |
EXPECT_TRUE(PSI.isHotCountNthPercentile(990000, 400)); |
| 167 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(990000, 100)); |
| 168 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(990000, 2)); |
| 169 |
|
| 170 |
EXPECT_FALSE(PSI.isColdCountNthPercentile(990000, 400)); |
| 171 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(990000, 100)); |
| 172 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(990000, 2)); |
| 173 |
|
| 174 |
EXPECT_TRUE(PSI.isHotCountNthPercentile(999999, 400)); |
| 175 |
EXPECT_TRUE(PSI.isHotCountNthPercentile(999999, 100)); |
| 176 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(999999, 2)); |
| 177 |
|
| 178 |
EXPECT_FALSE(PSI.isColdCountNthPercentile(999999, 400)); |
| 179 |
EXPECT_FALSE(PSI.isColdCountNthPercentile(999999, 100)); |
| 180 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(999999, 2)); |
| 181 |
|
| 182 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(10000, 400)); |
| 183 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(10000, 100)); |
| 184 |
EXPECT_FALSE(PSI.isHotCountNthPercentile(10000, 2)); |
| 185 |
|
| 186 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(10000, 400)); |
| 187 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(10000, 100)); |
| 188 |
EXPECT_TRUE(PSI.isColdCountNthPercentile(10000, 2)); |
| 189 |
|
| 190 |
EXPECT_TRUE(PSI.isFunctionEntryHot(F)); |
| 191 |
EXPECT_FALSE(PSI.isFunctionEntryHot(G)); |
| 192 |
EXPECT_FALSE(PSI.isFunctionEntryHot(H)); |
| 193 |
} |
| 194 |
|
| 195 |
TEST_F(ProfileSummaryInfoTest, InstrProf) { |
| 196 |
auto M = makeLLVMModule("InstrProf"); |
| 197 |
Function *F = M->getFunction("f"); |
| 198 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 199 |
EXPECT_TRUE(PSI.hasProfileSummary()); |
| 200 |
EXPECT_TRUE(PSI.hasInstrumentationProfile()); |
| 201 |
|
| 202 |
BasicBlock &BB0 = F->getEntryBlock(); |
| 203 |
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 204 |
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 205 |
BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 206 |
|
| 207 |
BlockFrequencyInfo BFI = buildBFI(*F); |
| 208 |
EXPECT_TRUE(PSI.isHotBlock(&BB0, &BFI)); |
| 209 |
EXPECT_TRUE(PSI.isHotBlock(BB1, &BFI)); |
| 210 |
EXPECT_FALSE(PSI.isHotBlock(BB2, &BFI)); |
| 211 |
EXPECT_TRUE(PSI.isHotBlock(BB3, &BFI)); |
| 212 |
|
| 213 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, &BB0, &BFI)); |
| 214 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, BB1, &BFI)); |
| 215 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB2, &BFI)); |
| 216 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, BB3, &BFI)); |
| 217 |
|
| 218 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, &BB0, &BFI)); |
| 219 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB1, &BFI)); |
| 220 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(990000, BB2, &BFI)); |
| 221 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB3, &BFI)); |
| 222 |
|
| 223 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, &BB0, &BFI)); |
| 224 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB1, &BFI)); |
| 225 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB2, &BFI)); |
| 226 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB3, &BFI)); |
| 227 |
|
| 228 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, &BB0, &BFI)); |
| 229 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB1, &BFI)); |
| 230 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB2, &BFI)); |
| 231 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB3, &BFI)); |
| 232 |
|
| 233 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, &BB0, &BFI)); |
| 234 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB1, &BFI)); |
| 235 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB2, &BFI)); |
| 236 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB3, &BFI)); |
| 237 |
|
| 238 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, &BB0, &BFI)); |
| 239 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB1, &BFI)); |
| 240 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB2, &BFI)); |
| 241 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB3, &BFI)); |
| 242 |
|
| 243 |
CallBase &CS1 = cast<CallBase>(*BB1->getFirstNonPHI()); |
| 244 |
auto *CI2 = BB2->getFirstNonPHI(); |
| 245 |
CallBase &CS2 = cast<CallBase>(*CI2); |
| 246 |
|
| 247 |
EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI)); |
| 248 |
EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI)); |
| 249 |
|
| 250 |
// Test that adding an MD_prof metadata with a hot count on CS2 does not |
| 251 |
// change its hotness as it has no effect in instrumented profiling. |
| 252 |
MDBuilder MDB(M->getContext()); |
| 253 |
CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400})); |
| 254 |
EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI)); |
| 255 |
|
| 256 |
EXPECT_TRUE(PSI.isFunctionHotInCallGraphNthPercentile(990000, F, BFI)); |
| 257 |
EXPECT_FALSE(PSI.isFunctionColdInCallGraphNthPercentile(990000, F, BFI)); |
| 258 |
EXPECT_FALSE(PSI.isFunctionHotInCallGraphNthPercentile(10000, F, BFI)); |
| 259 |
EXPECT_TRUE(PSI.isFunctionColdInCallGraphNthPercentile(10000, F, BFI)); |
| 260 |
} |
| 261 |
|
| 262 |
TEST_F(ProfileSummaryInfoTest, InstrProfNoFuncEntryCount) { |
| 263 |
auto M = makeLLVMModule("InstrProf"); |
| 264 |
Function *F = M->getFunction("l"); |
| 265 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 266 |
EXPECT_TRUE(PSI.hasProfileSummary()); |
| 267 |
EXPECT_TRUE(PSI.hasInstrumentationProfile()); |
| 268 |
|
| 269 |
BasicBlock &BB0 = F->getEntryBlock(); |
| 270 |
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 271 |
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 272 |
BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 273 |
|
| 274 |
BlockFrequencyInfo BFI = buildBFI(*F); |
| 275 |
|
| 276 |
// Without the entry count, all should return false. |
| 277 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, &BB0, &BFI)); |
| 278 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB1, &BFI)); |
| 279 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB2, &BFI)); |
| 280 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB3, &BFI)); |
| 281 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, &BB0, &BFI)); |
| 282 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB1, &BFI)); |
| 283 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB2, &BFI)); |
| 284 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB3, &BFI)); |
| 285 |
|
| 286 |
EXPECT_FALSE(PSI.isFunctionHotInCallGraphNthPercentile(990000, F, BFI)); |
| 287 |
EXPECT_FALSE(PSI.isFunctionColdInCallGraphNthPercentile(990000, F, BFI)); |
| 288 |
} |
| 289 |
|
| 290 |
TEST_F(ProfileSummaryInfoTest, SampleProf) { |
| 291 |
auto M = makeLLVMModule("SampleProfile"); |
| 292 |
Function *F = M->getFunction("f"); |
| 293 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 294 |
EXPECT_TRUE(PSI.hasProfileSummary()); |
| 295 |
EXPECT_TRUE(PSI.hasSampleProfile()); |
| 296 |
EXPECT_FALSE(PSI.hasPartialSampleProfile()); |
| 297 |
|
| 298 |
BasicBlock &BB0 = F->getEntryBlock(); |
| 299 |
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 300 |
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 301 |
BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 302 |
|
| 303 |
BlockFrequencyInfo BFI = buildBFI(*F); |
| 304 |
EXPECT_TRUE(PSI.isHotBlock(&BB0, &BFI)); |
| 305 |
EXPECT_TRUE(PSI.isHotBlock(BB1, &BFI)); |
| 306 |
EXPECT_FALSE(PSI.isHotBlock(BB2, &BFI)); |
| 307 |
EXPECT_TRUE(PSI.isHotBlock(BB3, &BFI)); |
| 308 |
|
| 309 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, &BB0, &BFI)); |
| 310 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, BB1, &BFI)); |
| 311 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB2, &BFI)); |
| 312 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(990000, BB3, &BFI)); |
| 313 |
|
| 314 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, &BB0, &BFI)); |
| 315 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB1, &BFI)); |
| 316 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(990000, BB2, &BFI)); |
| 317 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB3, &BFI)); |
| 318 |
|
| 319 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, &BB0, &BFI)); |
| 320 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB1, &BFI)); |
| 321 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB2, &BFI)); |
| 322 |
EXPECT_TRUE(PSI.isHotBlockNthPercentile(999900, BB3, &BFI)); |
| 323 |
|
| 324 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, &BB0, &BFI)); |
| 325 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB1, &BFI)); |
| 326 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB2, &BFI)); |
| 327 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(999900, BB3, &BFI)); |
| 328 |
|
| 329 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, &BB0, &BFI)); |
| 330 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB1, &BFI)); |
| 331 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB2, &BFI)); |
| 332 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(10000, BB3, &BFI)); |
| 333 |
|
| 334 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, &BB0, &BFI)); |
| 335 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB1, &BFI)); |
| 336 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB2, &BFI)); |
| 337 |
EXPECT_TRUE(PSI.isColdBlockNthPercentile(10000, BB3, &BFI)); |
| 338 |
|
| 339 |
CallBase &CS1 = cast<CallBase>(*BB1->getFirstNonPHI()); |
| 340 |
auto *CI2 = BB2->getFirstNonPHI(); |
| 341 |
// Manually attach branch weights metadata to the call instruction. |
| 342 |
SmallVector<uint32_t, 1> Weights; |
| 343 |
Weights.push_back(1000); |
| 344 |
MDBuilder MDB(M->getContext()); |
| 345 |
CI2->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); |
| 346 |
CallBase &CS2 = cast<CallBase>(*CI2); |
| 347 |
|
| 348 |
EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI)); |
| 349 |
EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI)); |
| 350 |
|
| 351 |
// Test that CS2 is considered hot when it gets an MD_prof metadata with |
| 352 |
// weights that exceed the hot count threshold. |
| 353 |
CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400})); |
| 354 |
EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI)); |
| 355 |
|
| 356 |
EXPECT_TRUE(PSI.isFunctionHotInCallGraphNthPercentile(990000, F, BFI)); |
| 357 |
EXPECT_FALSE(PSI.isFunctionColdInCallGraphNthPercentile(990000, F, BFI)); |
| 358 |
EXPECT_FALSE(PSI.isFunctionHotInCallGraphNthPercentile(10000, F, BFI)); |
| 359 |
EXPECT_TRUE(PSI.isFunctionColdInCallGraphNthPercentile(10000, F, BFI)); |
| 360 |
} |
| 361 |
|
| 362 |
TEST_F(ProfileSummaryInfoTest, SampleProfNoFuncEntryCount) { |
| 363 |
auto M = makeLLVMModule("SampleProfile"); |
| 364 |
Function *F = M->getFunction("l"); |
| 365 |
ProfileSummaryInfo PSI = buildPSI(M.get()); |
| 366 |
EXPECT_TRUE(PSI.hasProfileSummary()); |
| 367 |
EXPECT_TRUE(PSI.hasSampleProfile()); |
| 368 |
|
| 369 |
BasicBlock &BB0 = F->getEntryBlock(); |
| 370 |
BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 371 |
BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 372 |
BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 373 |
|
| 374 |
BlockFrequencyInfo BFI = buildBFI(*F); |
| 375 |
|
| 376 |
// Without the entry count, all should return false. |
| 377 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, &BB0, &BFI)); |
| 378 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB1, &BFI)); |
| 379 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB2, &BFI)); |
| 380 |
EXPECT_FALSE(PSI.isHotBlockNthPercentile(990000, BB3, &BFI)); |
| 381 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, &BB0, &BFI)); |
| 382 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB1, &BFI)); |
| 383 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB2, &BFI)); |
| 384 |
EXPECT_FALSE(PSI.isColdBlockNthPercentile(990000, BB3, &BFI)); |
| 385 |
|
| 386 |
EXPECT_FALSE(PSI.isFunctionHotInCallGraphNthPercentile(990000, F, BFI)); |
| 387 |
EXPECT_FALSE(PSI.isFunctionColdInCallGraphNthPercentile(990000, F, BFI)); |
| 388 |
} |
| 389 |
|
| 390 |
TEST_F(ProfileSummaryInfoTest, PartialSampleProfWorkingSetSize) { |
| 391 |
ScalePartialSampleProfileWorkingSetSize.setValue(true); |
| 392 |
|
| 393 |
// With PartialProfileRatio unset (zero.) |
| 394 |
auto M1 = makeLLVMModule("SampleProfile", /*NumCounts*/ 3, |
| 395 |
/*IsPartialProfile*/ 1, |
| 396 |
/*PartialProfileRatio*/ 0.0, |
| 397 |
/*HotNumCounts*/ 3, /*ColdNumCounts*/ 10); |
| 398 |
ProfileSummaryInfo PSI1 = buildPSI(M1.get()); |
| 399 |
EXPECT_TRUE(PSI1.hasProfileSummary()); |
| 400 |
EXPECT_TRUE(PSI1.hasSampleProfile()); |
| 401 |
EXPECT_TRUE(PSI1.hasPartialSampleProfile()); |
| 402 |
EXPECT_FALSE(PSI1.hasHugeWorkingSetSize()); |
| 403 |
EXPECT_FALSE(PSI1.hasLargeWorkingSetSize()); |
| 404 |
|
| 405 |
// With PartialProfileRatio set (non-zero) and a small working set size. |
| 406 |
auto M2 = makeLLVMModule("SampleProfile", /*NumCounts*/ 27493235, |
| 407 |
/*IsPartialProfile*/ 1, |
| 408 |
/*PartialProfileRatio*/ 0.00000012, |
| 409 |
/*HotNumCounts*/ 3102082, |
| 410 |
/*ColdNumCounts*/ 18306149); |
| 411 |
ProfileSummaryInfo PSI2 = buildPSI(M2.get()); |
| 412 |
EXPECT_TRUE(PSI2.hasProfileSummary()); |
| 413 |
EXPECT_TRUE(PSI2.hasSampleProfile()); |
| 414 |
EXPECT_TRUE(PSI2.hasPartialSampleProfile()); |
| 415 |
EXPECT_FALSE(PSI2.hasHugeWorkingSetSize()); |
| 416 |
EXPECT_FALSE(PSI2.hasLargeWorkingSetSize()); |
| 417 |
|
| 418 |
// With PartialProfileRatio is set (non-zero) and a large working set size. |
| 419 |
auto M3 = makeLLVMModule("SampleProfile", /*NumCounts*/ 27493235, |
| 420 |
/*IsPartialProfile*/ 1, |
| 421 |
/*PartialProfileRatio*/ 0.9, |
| 422 |
/*HotNumCounts*/ 3102082, |
| 423 |
/*ColdNumCounts*/ 18306149); |
| 424 |
ProfileSummaryInfo PSI3 = buildPSI(M3.get()); |
| 425 |
EXPECT_TRUE(PSI3.hasProfileSummary()); |
| 426 |
EXPECT_TRUE(PSI3.hasSampleProfile()); |
| 427 |
EXPECT_TRUE(PSI3.hasPartialSampleProfile()); |
| 428 |
EXPECT_TRUE(PSI3.hasHugeWorkingSetSize()); |
| 429 |
EXPECT_TRUE(PSI3.hasLargeWorkingSetSize()); |
| 430 |
} |
| 431 |
|
| 432 |
} // end anonymous namespace |
| 433 |
} // end namespace llvm |