1 //===- LowerAllowCheckPass.cpp ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
15 #include "llvm/Analysis/ProfileSummaryInfo.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/RandomNumberGenerator.h"
30 #define DEBUG_TYPE "lower-allow-check"
33 HotPercentileCutoff("lower-allow-check-percentile-cutoff-hot",
34 cl::desc("Hot percentile cutoff."));
37 RandomRate("lower-allow-check-random-rate",
38 cl::desc("Probability value in the range [0.0, 1.0] of "
39 "unconditional pseudo-random checks."));
41 STATISTIC(NumChecksTotal
, "Number of checks");
42 STATISTIC(NumChecksRemoved
, "Number of removed checks");
48 explicit RemarkInfo(IntrinsicInst
*II
)
49 : Kind("Kind", II
->getArgOperand(0)),
50 F("Function", II
->getParent()->getParent()),
51 BB("Block", II
->getParent()->getName()) {}
54 static void emitRemark(IntrinsicInst
*II
, OptimizationRemarkEmitter
&ORE
,
59 return OptimizationRemark(DEBUG_TYPE
, "Removed", II
)
60 << "Removed check: Kind=" << Info
.Kind
<< " F=" << Info
.F
66 return OptimizationRemarkMissed(DEBUG_TYPE
, "Allowed", II
)
67 << "Allowed check: Kind=" << Info
.Kind
<< " F=" << Info
.F
73 static bool removeUbsanTraps(Function
&F
, const BlockFrequencyInfo
&BFI
,
74 const ProfileSummaryInfo
*PSI
,
75 OptimizationRemarkEmitter
&ORE
,
76 const std::vector
<unsigned int> &cutoffs
) {
77 SmallVector
<std::pair
<IntrinsicInst
*, bool>, 16> ReplaceWithValue
;
78 std::unique_ptr
<RandomNumberGenerator
> Rng
;
80 auto GetRng
= [&]() -> RandomNumberGenerator
& {
82 Rng
= F
.getParent()->createRNG(F
.getName());
86 auto GetCutoff
= [&](const IntrinsicInst
*II
) -> unsigned {
87 if (HotPercentileCutoff
.getNumOccurrences())
88 return HotPercentileCutoff
;
89 else if (II
->getIntrinsicID() == Intrinsic::allow_ubsan_check
) {
90 auto *Kind
= cast
<ConstantInt
>(II
->getArgOperand(0));
91 if (Kind
->getZExtValue() < cutoffs
.size())
92 return cutoffs
[Kind
->getZExtValue()];
98 auto ShouldRemoveHot
= [&](const BasicBlock
&BB
, unsigned int cutoff
) {
99 return (cutoff
== 1000000) ||
100 (PSI
&& PSI
->isHotCountNthPercentile(
101 cutoff
, BFI
.getBlockProfileCount(&BB
).value_or(0)));
104 auto ShouldRemoveRandom
= [&]() {
105 return RandomRate
.getNumOccurrences() &&
106 !std::bernoulli_distribution(RandomRate
)(GetRng());
109 auto ShouldRemove
= [&](const IntrinsicInst
*II
) {
110 unsigned int cutoff
= GetCutoff(II
);
111 return ShouldRemoveRandom() || ShouldRemoveHot(*(II
->getParent()), cutoff
);
114 for (BasicBlock
&BB
: F
) {
115 for (Instruction
&I
: BB
) {
116 IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(&I
);
119 auto ID
= II
->getIntrinsicID();
121 case Intrinsic::allow_ubsan_check
:
122 case Intrinsic::allow_runtime_check
: {
125 bool ToRemove
= ShouldRemove(II
);
127 ReplaceWithValue
.push_back({
133 emitRemark(II
, ORE
, ToRemove
);
142 for (auto [I
, V
] : ReplaceWithValue
) {
143 I
->replaceAllUsesWith(ConstantInt::getBool(I
->getType(), !V
));
144 I
->eraseFromParent();
147 return !ReplaceWithValue
.empty();
150 PreservedAnalyses
LowerAllowCheckPass::run(Function
&F
,
151 FunctionAnalysisManager
&AM
) {
152 if (F
.isDeclaration())
153 return PreservedAnalyses::all();
154 auto &MAMProxy
= AM
.getResult
<ModuleAnalysisManagerFunctionProxy
>(F
);
155 ProfileSummaryInfo
*PSI
=
156 MAMProxy
.getCachedResult
<ProfileSummaryAnalysis
>(*F
.getParent());
157 BlockFrequencyInfo
&BFI
= AM
.getResult
<BlockFrequencyAnalysis
>(F
);
158 OptimizationRemarkEmitter
&ORE
=
159 AM
.getResult
<OptimizationRemarkEmitterAnalysis
>(F
);
161 return removeUbsanTraps(F
, BFI
, PSI
, ORE
, Opts
.cutoffs
)
162 ? PreservedAnalyses::none()
163 : PreservedAnalyses::all();
166 bool LowerAllowCheckPass::IsRequested() {
167 return RandomRate
.getNumOccurrences() ||
168 HotPercentileCutoff
.getNumOccurrences();
171 void LowerAllowCheckPass::printPipeline(
172 raw_ostream
&OS
, function_ref
<StringRef(StringRef
)> MapClassName2PassName
) {
173 static_cast<PassInfoMixin
<LowerAllowCheckPass
> *>(this)->printPipeline(
174 OS
, MapClassName2PassName
);
177 // Format is <cutoffs[0,1,2]=70000;cutoffs[5,6,8]=90000>
178 // but it's equally valid to specify
179 // cutoffs[0]=70000;cutoffs[1]=70000;cutoffs[2]=70000;cutoffs[5]=90000;...
180 // and that's what we do here. It is verbose but valid and easy to verify
182 // TODO: print shorter output by combining adjacent runs, etc.
184 for (unsigned int cutoff
: Opts
.cutoffs
) {
188 OS
<< "cutoffs[" << i
<< "]=" << cutoff
;