LLVM: lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
10
27#include
28#include
29
30using namespace llvm;
31
32#define DEBUG_TYPE "lower-allow-check"
33
36 cl::desc("Hot percentile cutoff."));
37
40 cl::desc("Probability value in the range [0.0, 1.0] of "
41 "unconditional pseudo-random checks."));
42
43STATISTIC(NumChecksTotal, "Number of checks");
44STATISTIC(NumChecksRemoved, "Number of removed checks");
45
51 : Kind("Kind", II->getArgOperand(0)),
54};
55
57 bool Removed) {
58 if (Removed) {
59 ORE.emit([&]() {
62 << "Removed check: Kind=" << Info.Kind << " F=" << Info.F
63 << " BB=" << Info.BB;
64 });
65 } else {
66 ORE.emit([&]() {
69 << "Allowed check: Kind=" << Info.Kind << " F=" << Info.F
70 << " BB=" << Info.BB;
71 });
72 }
73}
74
80 std::unique_ptr Rng;
81
83 if (!Rng)
84 Rng = F.getParent()->createRNG(F.getName());
85 return *Rng;
86 };
87
88 auto GetCutoff = [&](const IntrinsicInst *II) -> unsigned {
91 else if (II->getIntrinsicID() == Intrinsic::allow_ubsan_check) {
93 if (Kind->getZExtValue() < Opts.cutoffs.size())
94 return Opts.cutoffs[Kind->getZExtValue()];
95 } else if (II->getIntrinsicID() == Intrinsic::allow_runtime_check) {
97 }
98
99 return 0;
100 };
101
102 auto ShouldRemoveHot = [&](const BasicBlock &BB, unsigned int cutoff) {
103 return (cutoff == 1000000) ||
106 };
107
108 auto ShouldRemoveRandom = [&]() {
109 return RandomRate.getNumOccurrences() &&
110 !std::bernoulli_distribution(RandomRate)(GetRng());
111 };
112
114 unsigned int cutoff = GetCutoff(II);
115 return ShouldRemoveRandom() || ShouldRemoveHot(*(II->getParent()), cutoff);
116 };
117
120 if ()
121 continue;
122 auto ID = II->getIntrinsicID();
123 switch (ID) {
124 case Intrinsic::allow_ubsan_check:
125 case Intrinsic::allow_runtime_check: {
126 ++NumChecksTotal;
127
129
133 });
135 ++NumChecksRemoved;
137 break;
138 }
139 default:
140 break;
141 }
142 }
143
144 for (auto [I, V] : ReplaceWithValue) {
146 I->eraseFromParent();
147 }
148
149 return !ReplaceWithValue.empty();
150}
151
154 if (F.isDeclaration())
162
164
165
168}
169
174
178 OS, MapClassName2PassName);
179 OS << "<";
180
181
182
183
184
185
186
187 int i = 0;
189 for (unsigned int cutoff : Opts.cutoffs) {
190 if (cutoff > 0)
191 OS << LS << "cutoffs[" << i << "]=" << cutoff;
192 i++;
193 }
194 if (Opts.runtime_check)
195 OS << LS << "runtime_check=" << Opts.runtime_check;
196
197 OS << '>';
198}
ReachingDefInfo InstSet & ToRemove
Expand Atomic instructions
static const Function * getParent(const Value *V)
Analysis containing CSE Info
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
static cl::opt< float > RandomRate("lower-allow-check-random-rate", cl::desc("Probability value in the range [0.0, 1.0] of " "unconditional pseudo-random checks."))
static cl::opt< int > HotPercentileCutoff("lower-allow-check-percentile-cutoff-hot", cl::desc("Hot percentile cutoff."))
static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE, bool Removed)
Definition LowerAllowCheckPass.cpp:56
static bool lowerAllowChecks(Function &F, const BlockFrequencyInfo &BFI, const ProfileSummaryInfo *PSI, OptimizationRemarkEmitter &ORE, const LowerAllowCheckPass::Options &Opts)
Definition LowerAllowCheckPass.cpp:75
This file provides the interface for the pass responsible for removing expensive ubsan checks.
uint64_t IntrinsicInst * II
static StringRef getName(Value *V)
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM_ABI std::optional< uint64_t > getBlockProfileCount(const BasicBlock *BB, bool AllowSynthetic=false) const
Returns the estimated profile count of BB.
Represents analyses that only rely on functions' control flow.
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
A wrapper class for inspecting calls to intrinsic functions.
A helper class to return the specified delimiter string after the first invocation of operator String...
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition LowerAllowCheckPass.cpp:152
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition LowerAllowCheckPass.cpp:175
static LLVM_ABI bool IsRequested()
Definition LowerAllowCheckPass.cpp:170
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis providing profile information.
LLVM_ABI bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered hot with regard to a given hot percentile cutoff value.
A random number generator.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
unsigned int runtime_check
std::vector< unsigned int > cutoffs
A CRTP mix-in to automatically provide informational APIs needed for passes.