Skip to content

Commit

Permalink
[NFCI][sanitizers][metadata] Exctract create{Unlikely,Likely}BranchWe…
Browse files Browse the repository at this point in the history
…ights (#89464)

We have a lot of repeated code with random constants.
Particular values are not important, the one just needs to be
bigger then another.

UR_NONTAKEN_WEIGHT is selected as it's the most common one.
  • Loading branch information
vitalybuka authored Apr 20, 2024
1 parent f3587d4 commit c60aa43
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 20 deletions.
8 changes: 8 additions & 0 deletions llvm/include/llvm/IR/MDBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class MDBuilder {
/// Return metadata containing two branch weights.
MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);

/// Return metadata containing two branch weights, with significant bias
/// towards `true` destination.
MDNode *createLikelyBranchWeights();

/// Return metadata containing two branch weights, with significant bias
/// towards `false` destination.
MDNode *createUnlikelyBranchWeights();

/// Return metadata containing a number of branch weights.
MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);

Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/IR/MDBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
return createBranchWeights({TrueWeight, FalseWeight});
}

MDNode *MDBuilder::createLikelyBranchWeights() {
// Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
return createBranchWeights((1U << 20) - 1, 1);
}

MDNode *MDBuilder::createUnlikelyBranchWeights() {
// Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
return createBranchWeights(1, (1U << 20) - 1);
}

MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
assert(Weights.size() >= 1 && "Need at least one branch weights!");

Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/IPO/CrossDSOCFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
}

bool CrossDSOCFI::runOnModule(Module &M) {
VeryLikelyWeights =
MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
VeryLikelyWeights = MDBuilder(M.getContext()).createLikelyBranchWeights();
if (M.getModuleFlag("Cross-DSO CFI") == nullptr)
return false;
buildCFICheck(M);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,8 +1196,7 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
// function pointer to the devirtualized target. In case of a mismatch,
// fall back to indirect call.
if (DevirtCheckMode == WPDCheckMode::Fallback) {
MDNode *Weights =
MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
MDNode *Weights = MDBuilder(M.getContext()).createLikelyBranchWeights();
// Version the indirect call site. If the called value is equal to the
// given callee, 'NewInst' will be executed, otherwise the original call
// site will be executed.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ Instruction *AddressSanitizer::genAMDGPUReportBlock(IRBuilder<> &IRB,

auto *Trm =
SplitBlockAndInsertIfThen(ReportCond, &*IRB.GetInsertPoint(), false,
MDBuilder(*C).createBranchWeights(1, 100000));
MDBuilder(*C).createUnlikelyBranchWeights());
Trm->getParent()->setName("asan.report");

if (Recover)
Expand Down Expand Up @@ -1894,7 +1894,7 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
// We use branch weights for the slow path check, to indicate that the slow
// path is rarely taken. This seems to be the case for SPEC benchmarks.
Instruction *CheckTerm = SplitBlockAndInsertIfThen(
Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000));
Cmp, InsertBefore, false, MDBuilder(*C).createUnlikelyBranchWeights());
assert(cast<BranchInst>(CheckTerm)->isUnconditional());
BasicBlock *NextBB = CheckTerm->getSuccessor(0);
IRB.SetInsertPoint(CheckTerm);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,8 @@ bool DataFlowSanitizer::initializeModule(Module &M) {
FunctionType::get(Type::getVoidTy(*Ctx), DFSanMemTransferCallbackArgs,
/*isVarArg=*/false);

ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
OriginStoreWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
ColdCallWeights = MDBuilder(*Ctx).createUnlikelyBranchWeights();
OriginStoreWeights = MDBuilder(*Ctx).createUnlikelyBranchWeights();
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ HWAddressSanitizer::insertShadowTagCheck(Value *Ptr, Instruction *InsertBefore,

R.TagMismatchTerm = SplitBlockAndInsertIfThen(
TagMismatch, InsertBefore, false,
MDBuilder(*C).createBranchWeights(1, 100000), &DTU, LI);
MDBuilder(*C).createUnlikelyBranchWeights(), &DTU, LI);

return R;
}
Expand Down Expand Up @@ -952,15 +952,15 @@ void HWAddressSanitizer::instrumentMemAccessInline(Value *Ptr, bool IsWrite,
IRB.CreateICmpUGT(TCI.MemTag, ConstantInt::get(Int8Ty, 15));
Instruction *CheckFailTerm = SplitBlockAndInsertIfThen(
OutOfShortGranuleTagRange, TCI.TagMismatchTerm, !Recover,
MDBuilder(*C).createBranchWeights(1, 100000), &DTU, LI);
MDBuilder(*C).createUnlikelyBranchWeights(), &DTU, LI);

IRB.SetInsertPoint(TCI.TagMismatchTerm);
Value *PtrLowBits = IRB.CreateTrunc(IRB.CreateAnd(TCI.PtrLong, 15), Int8Ty);
PtrLowBits = IRB.CreateAdd(
PtrLowBits, ConstantInt::get(Int8Ty, (1 << AccessSizeIndex) - 1));
Value *PtrLowBitsOOB = IRB.CreateICmpUGE(PtrLowBits, TCI.MemTag);
SplitBlockAndInsertIfThen(PtrLowBitsOOB, TCI.TagMismatchTerm, false,
MDBuilder(*C).createBranchWeights(1, 100000), &DTU,
MDBuilder(*C).createUnlikelyBranchWeights(), &DTU,
LI, CheckFailTerm->getParent());

IRB.SetInsertPoint(TCI.TagMismatchTerm);
Expand All @@ -969,7 +969,7 @@ void HWAddressSanitizer::instrumentMemAccessInline(Value *Ptr, bool IsWrite,
Value *InlineTag = IRB.CreateLoad(Int8Ty, InlineTagAddr);
Value *InlineTagMismatch = IRB.CreateICmpNE(TCI.PtrTag, InlineTag);
SplitBlockAndInsertIfThen(InlineTagMismatch, TCI.TagMismatchTerm, false,
MDBuilder(*C).createBranchWeights(1, 100000), &DTU,
MDBuilder(*C).createUnlikelyBranchWeights(), &DTU,
LI, CheckFailTerm->getParent());

IRB.SetInsertPoint(CheckFailTerm);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Transforms/Instrumentation/KCFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ PreservedAnalyses KCFIPass::run(Function &F, FunctionAnalysisManager &AM) {
"compatible with -fsanitize=kcfi on this target"));

IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
MDNode *VeryUnlikelyWeights =
MDBuilder(Ctx).createBranchWeights(1, (1U << 20) - 1);
MDNode *VeryUnlikelyWeights = MDBuilder(Ctx).createUnlikelyBranchWeights();
Triple T(M.getTargetTriple());

for (CallInst *CI : KCFICalls) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,8 @@ void MemorySanitizer::initializeModule(Module &M) {
OriginTy = IRB.getInt32Ty();
PtrTy = IRB.getPtrTy();

ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000);
OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
ColdCallWeights = MDBuilder(*C).createUnlikelyBranchWeights();
OriginStoreWeights = MDBuilder(*C).createUnlikelyBranchWeights();

if (!CompileKernel) {
if (TrackOrigins)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
auto Load = IRB.CreateLoad(Int1Ty, FlagPtr);
auto ThenTerm = SplitBlockAndInsertIfThen(
IRB.CreateIsNull(Load), &*IP, false,
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
IRBuilder<> ThenIRB(ThenTerm);
auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr);
Load->setNoSanitizeMetadata();
Expand All @@ -1001,7 +1001,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
auto ThenTerm = SplitBlockAndInsertIfThen(
IsStackLower, &*IP, false,
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
IRBuilder<> ThenIRB(ThenTerm);
auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
LowestStack->setNoSanitizeMetadata();
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll
Original file line number Diff line number Diff line change
Expand Up @@ -580,5 +580,5 @@ ehcleanup: ; preds = %entry
cleanupret from %0 unwind to caller
}
;.
; CHECK-INLINE: [[PROF0]] = !{!"branch_weights", i32 1, i32 100000}
; CHECK-INLINE: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
;.
2 changes: 1 addition & 1 deletion llvm/test/Instrumentation/AddressSanitizer/basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,4 @@ define void @test_swifterror_3() sanitize_address {
; CHECK: attributes #[[#ATTR]] = { nounwind }

; PROF
; CHECK: ![[PROF]] = !{!"branch_weights", i32 1, i32 100000}
; CHECK: ![[PROF]] = !{!"branch_weights", i32 1, i32 1048575}

0 comments on commit c60aa43

Please sign in to comment.