Skip to content

Commit c60aa43

Browse files
authored
[NFCI][sanitizers][metadata] Exctract create{Unlikely,Likely}BranchWeights (#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.
1 parent f3587d4 commit c60aa43

File tree

12 files changed

+35
-20
lines changed

12 files changed

+35
-20
lines changed

llvm/include/llvm/IR/MDBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class MDBuilder {
6161
/// Return metadata containing two branch weights.
6262
MDNode *createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight);
6363

64+
/// Return metadata containing two branch weights, with significant bias
65+
/// towards `true` destination.
66+
MDNode *createLikelyBranchWeights();
67+
68+
/// Return metadata containing two branch weights, with significant bias
69+
/// towards `false` destination.
70+
MDNode *createUnlikelyBranchWeights();
71+
6472
/// Return metadata containing a number of branch weights.
6573
MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);
6674

llvm/lib/IR/MDBuilder.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
3939
return createBranchWeights({TrueWeight, FalseWeight});
4040
}
4141

42+
MDNode *MDBuilder::createLikelyBranchWeights() {
43+
// Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
44+
return createBranchWeights((1U << 20) - 1, 1);
45+
}
46+
47+
MDNode *MDBuilder::createUnlikelyBranchWeights() {
48+
// Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
49+
return createBranchWeights(1, (1U << 20) - 1);
50+
}
51+
4252
MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
4353
assert(Weights.size() >= 1 && "Need at least one branch weights!");
4454

llvm/lib/Transforms/IPO/CrossDSOCFI.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ void CrossDSOCFI::buildCFICheck(Module &M) {
139139
}
140140

141141
bool CrossDSOCFI::runOnModule(Module &M) {
142-
VeryLikelyWeights =
143-
MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
142+
VeryLikelyWeights = MDBuilder(M.getContext()).createLikelyBranchWeights();
144143
if (M.getModuleFlag("Cross-DSO CFI") == nullptr)
145144
return false;
146145
buildCFICheck(M);

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,7 @@ void DevirtModule::applySingleImplDevirt(VTableSlotInfo &SlotInfo,
11961196
// function pointer to the devirtualized target. In case of a mismatch,
11971197
// fall back to indirect call.
11981198
if (DevirtCheckMode == WPDCheckMode::Fallback) {
1199-
MDNode *Weights =
1200-
MDBuilder(M.getContext()).createBranchWeights((1U << 20) - 1, 1);
1199+
MDNode *Weights = MDBuilder(M.getContext()).createLikelyBranchWeights();
12011200
// Version the indirect call site. If the called value is equal to the
12021201
// given callee, 'NewInst' will be executed, otherwise the original call
12031202
// site will be executed.

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ Instruction *AddressSanitizer::genAMDGPUReportBlock(IRBuilder<> &IRB,
18171817

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

18231823
if (Recover)
@@ -1894,7 +1894,7 @@ void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
18941894
// We use branch weights for the slow path check, to indicate that the slow
18951895
// path is rarely taken. This seems to be the case for SPEC benchmarks.
18961896
Instruction *CheckTerm = SplitBlockAndInsertIfThen(
1897-
Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000));
1897+
Cmp, InsertBefore, false, MDBuilder(*C).createUnlikelyBranchWeights());
18981898
assert(cast<BranchInst>(CheckTerm)->isUnconditional());
18991899
BasicBlock *NextBB = CheckTerm->getSuccessor(0);
19001900
IRB.SetInsertPoint(CheckTerm);

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,8 @@ bool DataFlowSanitizer::initializeModule(Module &M) {
12291229
FunctionType::get(Type::getVoidTy(*Ctx), DFSanMemTransferCallbackArgs,
12301230
/*isVarArg=*/false);
12311231

1232-
ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
1233-
OriginStoreWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
1232+
ColdCallWeights = MDBuilder(*Ctx).createUnlikelyBranchWeights();
1233+
OriginStoreWeights = MDBuilder(*Ctx).createUnlikelyBranchWeights();
12341234
return true;
12351235
}
12361236

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ HWAddressSanitizer::insertShadowTagCheck(Value *Ptr, Instruction *InsertBefore,
911911

912912
R.TagMismatchTerm = SplitBlockAndInsertIfThen(
913913
TagMismatch, InsertBefore, false,
914-
MDBuilder(*C).createBranchWeights(1, 100000), &DTU, LI);
914+
MDBuilder(*C).createUnlikelyBranchWeights(), &DTU, LI);
915915

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

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

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

975975
IRB.SetInsertPoint(CheckFailTerm);

llvm/lib/Transforms/Instrumentation/KCFI.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ PreservedAnalyses KCFIPass::run(Function &F, FunctionAnalysisManager &AM) {
7171
"compatible with -fsanitize=kcfi on this target"));
7272

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

7877
for (CallInst *CI : KCFICalls) {

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,8 @@ void MemorySanitizer::initializeModule(Module &M) {
10401040
OriginTy = IRB.getInt32Ty();
10411041
PtrTy = IRB.getPtrTy();
10421042

1043-
ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000);
1044-
OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
1043+
ColdCallWeights = MDBuilder(*C).createUnlikelyBranchWeights();
1044+
OriginStoreWeights = MDBuilder(*C).createUnlikelyBranchWeights();
10451045

10461046
if (!CompileKernel) {
10471047
if (TrackOrigins)

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
982982
auto Load = IRB.CreateLoad(Int1Ty, FlagPtr);
983983
auto ThenTerm = SplitBlockAndInsertIfThen(
984984
IRB.CreateIsNull(Load), &*IP, false,
985-
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
985+
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
986986
IRBuilder<> ThenIRB(ThenTerm);
987987
auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr);
988988
Load->setNoSanitizeMetadata();
@@ -1001,7 +1001,7 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
10011001
auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
10021002
auto ThenTerm = SplitBlockAndInsertIfThen(
10031003
IsStackLower, &*IP, false,
1004-
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
1004+
MDBuilder(IRB.getContext()).createUnlikelyBranchWeights());
10051005
IRBuilder<> ThenIRB(ThenTerm);
10061006
auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
10071007
LowestStack->setNoSanitizeMetadata();

llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,5 +580,5 @@ ehcleanup: ; preds = %entry
580580
cleanupret from %0 unwind to caller
581581
}
582582
;.
583-
; CHECK-INLINE: [[PROF0]] = !{!"branch_weights", i32 1, i32 100000}
583+
; CHECK-INLINE: [[PROF0]] = !{!"branch_weights", i32 1, i32 1048575}
584584
;.

llvm/test/Instrumentation/AddressSanitizer/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,4 @@ define void @test_swifterror_3() sanitize_address {
218218
; CHECK: attributes #[[#ATTR]] = { nounwind }
219219

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

0 commit comments

Comments
 (0)