llvm.org GIT mirror llvm / 9d419ab
[BPI] Use a safer constructor to calculate branch probabilities BPI may trigger signed overflow UB while computing branch probabilities for cold calls or to unreachables. For example, with our current choice of weights, we'll crash if there are >= 2^12 branches to an unreachable. Use a safer BranchProbability constructor which is better at handling fractions with large denominators. rdar://problem/29368161 Differential Revision: https://reviews.llvm.org/D27862 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@290016 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 2 years ago
3 changed file(s) with 101 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
161161 return true;
162162 }
163163
164 BranchProbability UnreachableProb(UR_TAKEN_WEIGHT,
165 (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *
166 UnreachableEdges.size());
167 BranchProbability ReachableProb(UR_NONTAKEN_WEIGHT,
168 (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) *
169 ReachableEdges.size());
164 auto UnreachableProb = BranchProbability::getBranchProbability(
165 UR_TAKEN_WEIGHT,
166 (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) * UnreachableEdges.size());
167 auto ReachableProb = BranchProbability::getBranchProbability(
168 UR_NONTAKEN_WEIGHT,
169 (UR_TAKEN_WEIGHT + UR_NONTAKEN_WEIGHT) * ReachableEdges.size());
170170
171171 for (unsigned SuccIdx : UnreachableEdges)
172172 setEdgeProbability(BB, SuccIdx, UnreachableProb);
299299 return true;
300300 }
301301
302 BranchProbability ColdProb(CC_TAKEN_WEIGHT,
303 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *
304 ColdEdges.size());
305 BranchProbability NormalProb(CC_NONTAKEN_WEIGHT,
306 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *
307 NormalEdges.size());
302 auto ColdProb = BranchProbability::getBranchProbability(
303 CC_TAKEN_WEIGHT,
304 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * ColdEdges.size());
305 auto NormalProb = BranchProbability::getBranchProbability(
306 CC_NONTAKEN_WEIGHT,
307 (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) * NormalEdges.size());
308308
309309 for (unsigned SuccIdx : ColdEdges)
310310 setEdgeProbability(BB, SuccIdx, ColdProb);
0 //===- BranchProbabilityInfoTest.cpp - BranchProbabilityInfo unit tests ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/BranchProbabilityInfo.h"
10 #include "llvm/Analysis/LoopInfo.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/BasicBlock.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/Dominators.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "gtest/gtest.h"
23
24 namespace llvm {
25 namespace {
26
27 struct BranchProbabilityInfoTest : public testing::Test {
28 std::unique_ptr BPI;
29 std::unique_ptr DT;
30 std::unique_ptr LI;
31 LLVMContext C;
32
33 BranchProbabilityInfo &buildBPI(Function &F) {
34 DT.reset(new DominatorTree(F));
35 LI.reset(new LoopInfo(*DT));
36 BPI.reset(new BranchProbabilityInfo(F, *LI));
37 return *BPI;
38 }
39
40 std::unique_ptr makeLLVMModule() {
41 const char *ModuleString = "define void @f() { exit: ret void }\n";
42 SMDiagnostic Err;
43 return parseAssemblyString(ModuleString, Err, C);
44 }
45 };
46
47 TEST_F(BranchProbabilityInfoTest, StressUnreachableHeuristic) {
48 auto M = makeLLVMModule();
49 Function *F = M->getFunction("f");
50
51 // define void @f() {
52 // entry:
53 // switch i32 undef, label %exit, [
54 // i32 0, label %preexit
55 // ... ;;< Add lots of cases to stress the heuristic.
56 // ]
57 // preexit:
58 // unreachable
59 // exit:
60 // ret void
61 // }
62
63 auto *ExitBB = &F->back();
64 auto *EntryBB = BasicBlock::Create(C, "entry", F, /*insertBefore=*/ExitBB);
65
66 auto *PreExitBB =
67 BasicBlock::Create(C, "preexit", F, /*insertBefore=*/ExitBB);
68 new UnreachableInst(C, PreExitBB);
69
70 unsigned NumCases = 4096;
71 auto *I32 = IntegerType::get(C, 32);
72 auto *Undef = UndefValue::get(I32);
73 auto *Switch = SwitchInst::Create(Undef, ExitBB, NumCases, EntryBB);
74 for (unsigned I = 0; I < NumCases; ++I)
75 Switch->addCase(ConstantInt::get(I32, I), PreExitBB);
76
77 BranchProbabilityInfo &BPI = buildBPI(*F);
78
79 // FIXME: This doesn't seem optimal. Since all of the cases handled by the
80 // switch have the *same* destination block ("preexit"), shouldn't it be the
81 // hot one? I'd expect the results to be reversed here...
82 EXPECT_FALSE(BPI.isEdgeHot(EntryBB, PreExitBB));
83 EXPECT_TRUE(BPI.isEdgeHot(EntryBB, ExitBB));
84 }
85
86 } // end anonymous namespace
87 } // end namespace llvm
77 add_llvm_unittest(AnalysisTests
88 AliasAnalysisTest.cpp
99 BlockFrequencyInfoTest.cpp
10 BranchProbabilityInfoTest.cpp
1011 CallGraphTest.cpp
1112 CFGTest.cpp
1213 CGSCCPassManagerTest.cpp