Skip to content

Commit f3587d4

Browse files
authored
[sancov] Apply branch weights when checking counters (#89458)
It reduces instrumentation overhead by ~50%.
1 parent 777d2e5 commit f3587d4

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/IntrinsicInst.h"
2626
#include "llvm/IR/Intrinsics.h"
2727
#include "llvm/IR/LLVMContext.h"
28+
#include "llvm/IR/MDBuilder.h"
2829
#include "llvm/IR/Module.h"
2930
#include "llvm/IR/Type.h"
3031
#include "llvm/Support/CommandLine.h"
@@ -979,8 +980,9 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
979980
FunctionBoolArray->getValueType(), FunctionBoolArray,
980981
{ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
981982
auto Load = IRB.CreateLoad(Int1Ty, FlagPtr);
982-
auto ThenTerm =
983-
SplitBlockAndInsertIfThen(IRB.CreateIsNull(Load), &*IP, false);
983+
auto ThenTerm = SplitBlockAndInsertIfThen(
984+
IRB.CreateIsNull(Load), &*IP, false,
985+
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
984986
IRBuilder<> ThenIRB(ThenTerm);
985987
auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr);
986988
Load->setNoSanitizeMetadata();
@@ -997,7 +999,9 @@ void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
997999
auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
9981000
auto LowestStack = IRB.CreateLoad(IntptrTy, SanCovLowestStack);
9991001
auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
1000-
auto ThenTerm = SplitBlockAndInsertIfThen(IsStackLower, &*IP, false);
1002+
auto ThenTerm = SplitBlockAndInsertIfThen(
1003+
IsStackLower, &*IP, false,
1004+
MDBuilder(IRB.getContext()).createBranchWeights(1, (1 << 20) - 1));
10011005
IRBuilder<> ThenIRB(ThenTerm);
10021006
auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
10031007
LowestStack->setNoSanitizeMetadata();

llvm/test/Instrumentation/SanitizerCoverage/inline-bool-flag.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define void @foo() {
1919
; CHECK-NEXT: entry:
2020
; CHECK-NEXT: [[TMP0:%.*]] = load i1, ptr @__sancov_gen_, align 1, !nosanitize [[META0:![0-9]+]]
2121
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i1 [[TMP0]], false
22-
; CHECK-NEXT: br i1 [[TMP1]], label [[TMP2:%.*]], label [[TMP3:%.*]]
22+
; CHECK-NEXT: br i1 [[TMP1]], label [[TMP2:%.*]], label [[TMP3:%.*]], !prof [[PROF1:![0-9]+]]
2323
; CHECK: 2:
2424
; CHECK-NEXT: store i1 true, ptr @__sancov_gen_, align 1, !nosanitize [[META0]]
2525
; CHECK-NEXT: br label [[TMP3]]
@@ -34,4 +34,5 @@ entry:
3434
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind }
3535
;.
3636
; CHECK: [[META0]] = !{}
37+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 1, i32 1048575}
3738
;.

llvm/test/Instrumentation/SanitizerCoverage/stack-depth.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ define i32 @bar() {
4343
; L1-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[TMP0]] to i64
4444
; L1-NEXT: [[TMP2:%.*]] = load i64, ptr @__sancov_lowest_stack, align 8, !nosanitize [[META0:![0-9]+]]
4545
; L1-NEXT: [[TMP3:%.*]] = icmp ult i64 [[TMP1]], [[TMP2]]
46-
; L1-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]]
46+
; L1-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]], !prof [[PROF1:![0-9]+]]
4747
; L1: 4:
4848
; L1-NEXT: store i64 [[TMP1]], ptr @__sancov_lowest_stack, align 8, !nosanitize [[META0]]
4949
; L1-NEXT: br label [[TMP5]]
@@ -58,7 +58,7 @@ define i32 @bar() {
5858
; L3-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[TMP0]] to i64
5959
; L3-NEXT: [[TMP2:%.*]] = load i64, ptr @__sancov_lowest_stack, align 8, !nosanitize [[META0:![0-9]+]]
6060
; L3-NEXT: [[TMP3:%.*]] = icmp ult i64 [[TMP1]], [[TMP2]]
61-
; L3-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]]
61+
; L3-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]], !prof [[PROF1:![0-9]+]]
6262
; L3: 4:
6363
; L3-NEXT: store i64 [[TMP1]], ptr @__sancov_lowest_stack, align 8, !nosanitize [[META0]]
6464
; L3-NEXT: br label [[TMP5]]
@@ -90,6 +90,8 @@ define weak_odr hidden ptr @_ZTW21__sancov_lowest_stack() {
9090
; L3: attributes #[[ATTR2]] = { nomerge }
9191
;.
9292
; L1: [[META0]] = !{}
93+
; L1: [[PROF1]] = !{!"branch_weights", i32 1, i32 1048575}
9394
;.
9495
; L3: [[META0]] = !{}
96+
; L3: [[PROF1]] = !{!"branch_weights", i32 1, i32 1048575}
9597
;.

0 commit comments

Comments
 (0)