[llvm] f082782 - [NFC][Fuzzer] Extract CreateGateBranch method. (#117236)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 21 21:21:31 PST 2024
Author: thetruestblue
Date: 2024-11-21T21:21:27-08:00
New Revision: f082782c1b3ec98f50237ddfc92e6776013bf62f
URL: https://github.com/llvm/llvm-project/commit/f082782c1b3ec98f50237ddfc92e6776013bf62f
DIFF: https://github.com/llvm/llvm-project/commit/f082782c1b3ec98f50237ddfc92e6776013bf62f.diff
LOG: [NFC][Fuzzer] Extract CreateGateBranch method. (#117236)
A Pre-commit for use in adding gated tracing callbacks support to
trace-cmp
[#113227](https://github.com/llvm/llvm-project/pull/113227/commits/53b316d74683064f2db88ec401f6c3018ee6896a)
rdar://135404160
Patch by: Andrea Fioraldi
Added:
Modified:
llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 34006bfda96c5f..139e75dd3ddb34 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -250,6 +250,8 @@ class ModuleSanitizerCoverage {
const char *Section);
GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
+ Instruction *CreateGateBranch(Function &F, Value *&FunctionGateCmp,
+ Instruction *I);
Value *CreateFunctionLocalGateCmp(IRBuilder<> &IRB);
void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
Value *&FunctionGateCmp, bool IsLeafFunc = true);
@@ -815,6 +817,24 @@ Value *ModuleSanitizerCoverage::CreateFunctionLocalGateCmp(IRBuilder<> &IRB) {
return Cmp;
}
+Instruction *ModuleSanitizerCoverage::CreateGateBranch(Function &F,
+ Value *&FunctionGateCmp,
+ Instruction *IP) {
+ if (!FunctionGateCmp) {
+ // Create this in the entry block
+ BasicBlock &BB = F.getEntryBlock();
+ BasicBlock::iterator IP = BB.getFirstInsertionPt();
+ IP = PrepareToSplitEntryBlock(BB, IP);
+ IRBuilder<> EntryIRB(&*IP);
+ FunctionGateCmp = CreateFunctionLocalGateCmp(EntryIRB);
+ }
+ // Set the branch weights in order to minimize the price paid when the
+ // gate is turned off, allowing the default enablement of this
+ // instrumentation with as little of a performance cost as possible
+ auto Weights = MDBuilder(*C).createBranchWeights(1, 100000);
+ return SplitBlockAndInsertIfThen(FunctionGateCmp, IP, false, Weights);
+}
+
bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
ArrayRef<BasicBlock *> AllBlocks,
bool IsLeafFunc) {
@@ -1012,19 +1032,10 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
ConstantInt::get(IntptrTy, Idx * 4)),
PtrTy);
if (Options.GatedCallbacks) {
- if (!FunctionGateCmp) {
- // Create this in the entry block
- assert(IsEntryBB);
- FunctionGateCmp = CreateFunctionLocalGateCmp(IRB);
- }
- // Set the branch weights in order to minimize the price paid when the
- // gate is turned off, allowing the default enablement of this
- // instrumentation with as little of a performance cost as possible
- auto Weights = MDBuilder(*C).createBranchWeights(1, 100000);
- auto ThenTerm =
- SplitBlockAndInsertIfThen(FunctionGateCmp, &*IP, false, Weights);
- IRBuilder<> ThenIRB(ThenTerm);
- ThenIRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
+ Instruction *I = &*IP;
+ auto GateBranch = CreateGateBranch(F, FunctionGateCmp, I);
+ IRBuilder<> GateIRB(GateBranch);
+ GateIRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
} else {
IRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
}
More information about the llvm-commits
mailing list