Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 1 | //===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 12 | #include "llvm/Analysis/LoopInfo.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 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" |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace { |
| 29 | |
| 30 | class ProfileSummaryInfoTest : public testing::Test { |
| 31 | protected: |
| 32 | LLVMContext C; |
| 33 | std::unique_ptr<BranchProbabilityInfo> BPI; |
| 34 | std::unique_ptr<DominatorTree> DT; |
| 35 | std::unique_ptr<LoopInfo> LI; |
| 36 | |
| 37 | ProfileSummaryInfo buildPSI(Module *M) { |
| 38 | return ProfileSummaryInfo(*M); |
| 39 | } |
| 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); |
| 45 | } |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 46 | std::unique_ptr<Module> makeLLVMModule(const char *ProfKind = nullptr) { |
| 47 | const char *ModuleString = |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 48 | "define i32 @g(i32 %x) !prof !21 {{\n" |
| 49 | " ret i32 0\n" |
| 50 | "}\n" |
| 51 | "define i32 @h(i32 %x) !prof !22 {{\n" |
| 52 | " ret i32 0\n" |
| 53 | "}\n" |
| 54 | "define i32 @f(i32 %x) !prof !20 {{\n" |
| 55 | "bb0:\n" |
| 56 | " %y1 = icmp eq i32 %x, 0 \n" |
| 57 | " br i1 %y1, label %bb1, label %bb2, !prof !23 \n" |
| 58 | "bb1:\n" |
| 59 | " %z1 = call i32 @g(i32 %x)\n" |
| 60 | " br label %bb3\n" |
| 61 | "bb2:\n" |
| 62 | " %z2 = call i32 @h(i32 %x)\n" |
| 63 | " br label %bb3\n" |
| 64 | "bb3:\n" |
| 65 | " %y2 = phi i32 [0, %bb1], [1, %bb2] \n" |
| 66 | " ret i32 %y2\n" |
| 67 | "}\n" |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 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" |
| 72 | "{0}"; |
| 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}"; |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 88 | SMDiagnostic Err; |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 89 | if (ProfKind) |
| 90 | return parseAssemblyString( |
| 91 | formatv(ModuleString, formatv(SummaryString, ProfKind).str()).str(), |
| 92 | Err, C); |
| 93 | else |
| 94 | return parseAssemblyString(formatv(ModuleString, "").str(), Err, C); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 95 | } |
| 96 | }; |
| 97 | |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 98 | TEST_F(ProfileSummaryInfoTest, TestNoProfile) { |
| 99 | auto M = makeLLVMModule(/*ProfKind=*/nullptr); |
| 100 | Function *F = M->getFunction("f"); |
| 101 | |
| 102 | ProfileSummaryInfo PSI = buildPSI(M.get()); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 103 | EXPECT_FALSE(PSI.hasProfileSummary()); |
| 104 | EXPECT_FALSE(PSI.hasSampleProfile()); |
| 105 | EXPECT_FALSE(PSI.hasInstrumentationProfile()); |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 106 | // In the absence of profiles, is{Hot|Cold}X methods should always return |
| 107 | // false. |
| 108 | EXPECT_FALSE(PSI.isHotCount(1000)); |
| 109 | EXPECT_FALSE(PSI.isHotCount(0)); |
| 110 | EXPECT_FALSE(PSI.isColdCount(1000)); |
| 111 | EXPECT_FALSE(PSI.isColdCount(0)); |
| 112 | |
| 113 | EXPECT_FALSE(PSI.isFunctionEntryHot(F)); |
| 114 | EXPECT_FALSE(PSI.isFunctionEntryCold(F)); |
| 115 | |
| 116 | BasicBlock &BB0 = F->getEntryBlock(); |
| 117 | BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 118 | |
| 119 | BlockFrequencyInfo BFI = buildBFI(*F); |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 120 | EXPECT_FALSE(PSI.isHotBlock(&BB0, &BFI)); |
| 121 | EXPECT_FALSE(PSI.isColdBlock(&BB0, &BFI)); |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 122 | |
| 123 | CallSite CS1(BB1->getFirstNonPHI()); |
| 124 | EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI)); |
| 125 | EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI)); |
| 126 | } |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 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"); |
| 132 | |
| 133 | ProfileSummaryInfo PSI = buildPSI(M.get()); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 134 | EXPECT_TRUE(PSI.hasProfileSummary()); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 135 | EXPECT_TRUE(PSI.isHotCount(400)); |
| 136 | EXPECT_TRUE(PSI.isColdCount(2)); |
| 137 | EXPECT_FALSE(PSI.isColdCount(100)); |
| 138 | EXPECT_FALSE(PSI.isHotCount(100)); |
| 139 | |
| 140 | EXPECT_TRUE(PSI.isFunctionEntryHot(F)); |
| 141 | EXPECT_FALSE(PSI.isFunctionEntryHot(G)); |
| 142 | EXPECT_FALSE(PSI.isFunctionEntryHot(H)); |
| 143 | } |
| 144 | |
| 145 | TEST_F(ProfileSummaryInfoTest, InstrProf) { |
| 146 | auto M = makeLLVMModule("InstrProf"); |
| 147 | Function *F = M->getFunction("f"); |
| 148 | ProfileSummaryInfo PSI = buildPSI(M.get()); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 149 | EXPECT_TRUE(PSI.hasProfileSummary()); |
| 150 | EXPECT_TRUE(PSI.hasInstrumentationProfile()); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 151 | |
| 152 | BasicBlock &BB0 = F->getEntryBlock(); |
| 153 | BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 154 | BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 155 | BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 156 | |
| 157 | BlockFrequencyInfo BFI = buildBFI(*F); |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 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)); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 162 | |
| 163 | CallSite CS1(BB1->getFirstNonPHI()); |
| 164 | auto *CI2 = BB2->getFirstNonPHI(); |
| 165 | CallSite CS2(CI2); |
| 166 | |
| 167 | EXPECT_TRUE(PSI.isHotCallSite(CS1, &BFI)); |
| 168 | EXPECT_FALSE(PSI.isHotCallSite(CS2, &BFI)); |
Teresa Johnson | 2a6b799 | 2017-05-11 23:18:05 +0000 | [diff] [blame] | 169 | |
| 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)); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST_F(ProfileSummaryInfoTest, SampleProf) { |
| 178 | auto M = makeLLVMModule("SampleProfile"); |
| 179 | Function *F = M->getFunction("f"); |
| 180 | ProfileSummaryInfo PSI = buildPSI(M.get()); |
Easwaran Raman | dadc0f1 | 2017-05-16 20:14:39 +0000 | [diff] [blame] | 181 | EXPECT_TRUE(PSI.hasProfileSummary()); |
| 182 | EXPECT_TRUE(PSI.hasSampleProfile()); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 183 | |
| 184 | BasicBlock &BB0 = F->getEntryBlock(); |
| 185 | BasicBlock *BB1 = BB0.getTerminator()->getSuccessor(0); |
| 186 | BasicBlock *BB2 = BB0.getTerminator()->getSuccessor(1); |
| 187 | BasicBlock *BB3 = BB1->getSingleSuccessor(); |
| 188 | |
| 189 | BlockFrequencyInfo BFI = buildBFI(*F); |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 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)); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 194 | |
| 195 | CallSite CS1(BB1->getFirstNonPHI()); |
| 196 | auto *CI2 = BB2->getFirstNonPHI(); |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 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)); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 202 | CallSite CS2(CI2); |
| 203 | |
Dehao Chen | f58df39 | 2017-08-03 17:11:41 +0000 | [diff] [blame] | 204 | EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI)); |
| 205 | EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI)); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 206 | |
| 207 | // Test that CS2 is considered hot when it gets an MD_prof metadata with |
| 208 | // weights that exceed the hot count threshold. |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 209 | CI2->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights({400})); |
| 210 | EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI)); |
| 211 | } |
| 212 | |
| 213 | } // end anonymous namespace |
| 214 | } // end namespace llvm |