blob: 002901d943b37db88556d9e8cd42a8724396b1ad [file] [log] [blame]
Easwaran Ramanb035f912017-01-13 01:34:00 +00001//===- ProfileSummaryInfoTest.cpp - ProfileSummaryInfo unit tests ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Ramanb035f912017-01-13 01:34:00 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm/Analysis/ProfileSummaryInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000010#include "llvm/Analysis/BlockFrequencyInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000011#include "llvm/Analysis/BranchProbabilityInfo.h"
12#include "llvm/Analysis/LoopInfo.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000013#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
27namespace llvm {
28namespace {
29
30class ProfileSummaryInfoTest : public testing::Test {
31protected:
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 Ramana7bdb8a2017-01-14 00:32:37 +000046 std::unique_ptr<Module> makeLLVMModule(const char *ProfKind = nullptr) {
47 const char *ModuleString =
Easwaran Ramanb035f912017-01-13 01:34:00 +000048 "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 Ramana7bdb8a2017-01-14 00:32:37 +000068 "!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 Ramanb035f912017-01-13 01:34:00 +000088 SMDiagnostic Err;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000089 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 Ramanb035f912017-01-13 01:34:00 +000095 }
96};
97
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000098TEST_F(ProfileSummaryInfoTest, TestNoProfile) {
99 auto M = makeLLVMModule(/*ProfKind=*/nullptr);
100 Function *F = M->getFunction("f");
101
102 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000103 EXPECT_FALSE(PSI.hasProfileSummary());
104 EXPECT_FALSE(PSI.hasSampleProfile());
105 EXPECT_FALSE(PSI.hasInstrumentationProfile());
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000106 // 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 Kumare7b789b2018-11-19 05:23:16 +0000120 EXPECT_FALSE(PSI.isHotBlock(&BB0, &BFI));
121 EXPECT_FALSE(PSI.isColdBlock(&BB0, &BFI));
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000122
123 CallSite CS1(BB1->getFirstNonPHI());
124 EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
125 EXPECT_FALSE(PSI.isColdCallSite(CS1, &BFI));
126}
Easwaran Ramanb035f912017-01-13 01:34:00 +0000127TEST_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 Ramandadc0f12017-05-16 20:14:39 +0000134 EXPECT_TRUE(PSI.hasProfileSummary());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000135 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
145TEST_F(ProfileSummaryInfoTest, InstrProf) {
146 auto M = makeLLVMModule("InstrProf");
147 Function *F = M->getFunction("f");
148 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000149 EXPECT_TRUE(PSI.hasProfileSummary());
150 EXPECT_TRUE(PSI.hasInstrumentationProfile());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000151
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 Kumare7b789b2018-11-19 05:23:16 +0000158 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 Ramanb035f912017-01-13 01:34:00 +0000162
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 Johnson2a6b7992017-05-11 23:18:05 +0000169
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 Ramanb035f912017-01-13 01:34:00 +0000175}
176
177TEST_F(ProfileSummaryInfoTest, SampleProf) {
178 auto M = makeLLVMModule("SampleProfile");
179 Function *F = M->getFunction("f");
180 ProfileSummaryInfo PSI = buildPSI(M.get());
Easwaran Ramandadc0f12017-05-16 20:14:39 +0000181 EXPECT_TRUE(PSI.hasProfileSummary());
182 EXPECT_TRUE(PSI.hasSampleProfile());
Easwaran Ramanb035f912017-01-13 01:34:00 +0000183
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 Kumare7b789b2018-11-19 05:23:16 +0000190 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 Ramanb035f912017-01-13 01:34:00 +0000194
195 CallSite CS1(BB1->getFirstNonPHI());
196 auto *CI2 = BB2->getFirstNonPHI();
Dehao Chenf58df392017-08-03 17:11:41 +0000197 // 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 Ramanb035f912017-01-13 01:34:00 +0000202 CallSite CS2(CI2);
203
Dehao Chenf58df392017-08-03 17:11:41 +0000204 EXPECT_FALSE(PSI.isHotCallSite(CS1, &BFI));
205 EXPECT_TRUE(PSI.isHotCallSite(CS2, &BFI));
Easwaran Ramanb035f912017-01-13 01:34:00 +0000206
207 // Test that CS2 is considered hot when it gets an MD_prof metadata with
208 // weights that exceed the hot count threshold.
Easwaran Ramanb035f912017-01-13 01:34:00 +0000209 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