[llvm] [NFC][Fuzzer] Extract CreateGateBranch method. (PR #117236)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 21 14:01:05 PST 2024
https://github.com/thetruestblue updated https://github.com/llvm/llvm-project/pull/117236
>From 6b63903d3325b95fa4cbbc9361fc7879ea95a896 Mon Sep 17 00:00:00 2001
From: Blue Gaston <bgaston2 at apple.com>
Date: Thu, 21 Nov 2024 12:28:43 -0800
Subject: [PATCH 1/2] [NFC][Fuzzer] Extract CreateGateBranch method.
---
.../Instrumentation/SanitizerCoverage.cpp | 27 ++++++++++++++++---
1 file changed, 24 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 34006bfda96c5f..4689cb29236f06 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -244,12 +244,14 @@ class ModuleSanitizerCoverage {
void InjectTraceForSwitch(Function &F,
ArrayRef<Instruction *> SwitchTraceTargets);
bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
- bool IsLeafFunc = true);
+ Value *&FunctionGateCmp, bool IsLeafFunc = true);
GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
Function &F, Type *Ty,
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);
@@ -723,7 +725,8 @@ void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
if (Options.CollectControlFlow)
createFunctionControlFlow(F);
- InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
+ Value *FunctionGateCmp = nullptr;
+ InjectCoverage(F, BlocksToInstrument, FunctionGateCmp, IsLeafFunc);
InjectCoverageForIndirectCalls(F, IndirCalls);
InjectTraceForCmp(F, CmpTraceTargets);
InjectTraceForSwitch(F, SwitchTraceTargets);
@@ -815,12 +818,30 @@ 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,
+ Value *&FunctionGateCmp,
bool IsLeafFunc) {
if (AllBlocks.empty()) return false;
CreateFunctionLocalArrays(F, AllBlocks);
- Value *FunctionGateCmp = nullptr;
for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
InjectCoverageAtBlock(F, *AllBlocks[i], i, FunctionGateCmp, IsLeafFunc);
return true;
>From 2840c8ba6140d8a346d5b056db87891973be34ea Mon Sep 17 00:00:00 2001
From: thetruestblue <bblueconway at gmail.com>
Date: Thu, 21 Nov 2024 12:58:10 -0800
Subject: [PATCH 2/2] Update SanitizerCoverage.cpp
fixup....
---
.../Instrumentation/SanitizerCoverage.cpp | 28 ++++++-------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index 4689cb29236f06..139e75dd3ddb34 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -244,7 +244,7 @@ class ModuleSanitizerCoverage {
void InjectTraceForSwitch(Function &F,
ArrayRef<Instruction *> SwitchTraceTargets);
bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
- Value *&FunctionGateCmp, bool IsLeafFunc = true);
+ bool IsLeafFunc = true);
GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
Function &F, Type *Ty,
const char *Section);
@@ -725,8 +725,7 @@ void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
if (Options.CollectControlFlow)
createFunctionControlFlow(F);
- Value *FunctionGateCmp = nullptr;
- InjectCoverage(F, BlocksToInstrument, FunctionGateCmp, IsLeafFunc);
+ InjectCoverage(F, BlocksToInstrument, IsLeafFunc);
InjectCoverageForIndirectCalls(F, IndirCalls);
InjectTraceForCmp(F, CmpTraceTargets);
InjectTraceForSwitch(F, SwitchTraceTargets);
@@ -820,8 +819,8 @@ Value *ModuleSanitizerCoverage::CreateFunctionLocalGateCmp(IRBuilder<> &IRB) {
Instruction *ModuleSanitizerCoverage::CreateGateBranch(Function &F,
Value *&FunctionGateCmp,
- Instruction *IP) {
- if (!FunctionGateCmp) {
+ Instruction *IP) {
+ if (!FunctionGateCmp) {
// Create this in the entry block
BasicBlock &BB = F.getEntryBlock();
BasicBlock::iterator IP = BB.getFirstInsertionPt();
@@ -838,10 +837,10 @@ Instruction *ModuleSanitizerCoverage::CreateGateBranch(Function &F,
bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
ArrayRef<BasicBlock *> AllBlocks,
- Value *&FunctionGateCmp,
bool IsLeafFunc) {
if (AllBlocks.empty()) return false;
CreateFunctionLocalArrays(F, AllBlocks);
+ Value *FunctionGateCmp = nullptr;
for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
InjectCoverageAtBlock(F, *AllBlocks[i], i, FunctionGateCmp, IsLeafFunc);
return true;
@@ -1033,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