5
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
- //
9
- //
10
- // ===----------------------------------------------------------------------===//
11
8
12
9
#include " llvm/Transforms/Instrumentation/RemoveTrapsPass.h"
13
10
17
14
#include " llvm/IR/Instructions.h"
18
15
#include " llvm/IR/IntrinsicInst.h"
19
16
#include " llvm/IR/Intrinsics.h"
20
- #include < cstdint>
17
+ #include " llvm/Support/RandomNumberGenerator.h"
18
+ #include < memory>
19
+ #include < random>
21
20
22
21
using namespace llvm ;
23
22
24
23
#define DEBUG_TYPE " remove-traps"
25
24
26
- static constexpr unsigned MaxRandomRate = 1000 ;
27
-
28
25
static cl::opt<int > HotPercentileCutoff (
29
26
" remove-traps-percentile-cutoff-hot" , cl::init(0 ),
30
27
cl::desc(" Alternative hot percentile cuttoff. By default "
31
28
" `-profile-summary-cutoff-hot` is used." ));
29
+
32
30
static cl::opt<float > RandomRate (
33
31
" remove-traps-random-rate" , cl::init(0.0 ),
34
- cl::desc(
35
- " Probability to use for pseudorandom unconditional checks removal." ));
32
+ cl::desc(" Probability of unconditional pseudorandom checks removal." ));
36
33
37
34
STATISTIC (NumChecksTotal, " Number of checks" );
38
35
STATISTIC (NumChecksRemoved, " Number of removed checks" );
39
36
40
- static SmallVector<IntrinsicInst *, 16 >
41
- removeUbsanTraps (Function &F, FunctionAnalysisManager &FAM,
42
- ProfileSummaryInfo *PSI) {
37
+ static bool removeUbsanTraps (Function &F, const BlockFrequencyInfo &BFI,
38
+ const ProfileSummaryInfo *PSI) {
43
39
SmallVector<IntrinsicInst *, 16 > Remove;
40
+ std::unique_ptr<RandomNumberGenerator> Rng;
44
41
45
- if (F.isDeclaration ())
46
- return {};
47
-
48
- auto &BFI = FAM.getResult <BlockFrequencyAnalysis>(F);
49
-
50
- int BBCounter = 0 ;
51
42
for (BasicBlock &BB : F) {
52
43
for (Instruction &I : BB) {
53
44
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
@@ -65,18 +56,34 @@ removeUbsanTraps(Function &F, FunctionAnalysisManager &FAM,
65
56
Count += BFI.getBlockProfileCount (PR).value_or (0 );
66
57
67
58
IsHot = HotPercentileCutoff.getNumOccurrences ()
68
- ? PSI->isHotCountNthPercentile (HotPercentileCutoff, Count)
59
+ ? (HotPercentileCutoff > 0 &&
60
+ PSI->isHotCountNthPercentile (HotPercentileCutoff, Count))
69
61
: PSI->isHotCount (Count);
70
62
}
71
63
72
- if ((IsHot) || ((F.getGUID () + BBCounter++) % MaxRandomRate) <
73
- RandomRate * RandomRate) {
64
+ auto ShouldRemove = [&]() {
65
+ if (IsHot)
66
+ return true ;
67
+ if (!Rng) {
68
+ if (!RandomRate.getNumOccurrences ())
69
+ return false ;
70
+ Rng = F.getParent ()->createRNG (F.getName ());
71
+ }
72
+ std::bernoulli_distribution D (RandomRate);
73
+ return D (*Rng);
74
+ };
75
+
76
+ if (ShouldRemove ()) {
74
77
Remove.push_back (II);
75
78
++NumChecksRemoved;
76
79
}
77
80
}
78
81
}
79
- return Remove;
82
+
83
+ for (auto *I : Remove)
84
+ I->eraseFromParent ();
85
+
86
+ return !Remove.empty ();
80
87
}
81
88
82
89
PreservedAnalyses RemoveTrapsPass::run (Function &F,
@@ -86,10 +93,8 @@ PreservedAnalyses RemoveTrapsPass::run(Function &F,
86
93
auto &MAMProxy = AM.getResult <ModuleAnalysisManagerFunctionProxy>(F);
87
94
ProfileSummaryInfo *PSI =
88
95
MAMProxy.getCachedResult <ProfileSummaryAnalysis>(*F.getParent ());
96
+ BlockFrequencyInfo &BFI = AM.getResult <BlockFrequencyAnalysis>(F);
89
97
90
- auto Remove = removeUbsanTraps (F, AM, PSI);
91
- for (auto *I : Remove)
92
- I->eraseFromParent ();
93
-
94
- return Remove.empty () ? PreservedAnalyses::all () : PreservedAnalyses::none ();
98
+ return removeUbsanTraps (F, BFI, PSI) ? PreservedAnalyses::none ()
99
+ : PreservedAnalyses::all ();
95
100
}
0 commit comments