Skip to content

Commit 71bc606

Browse files
authored
[LowerAllowCheckPass] allow to specify runtime.check hotness (#145998)
1 parent 6c2e912 commit 71bc606

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

llvm/include/llvm/Transforms/Instrumentation/LowerAllowCheckPass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class LowerAllowCheckPass : public PassInfoMixin<LowerAllowCheckPass> {
2727
public:
2828
struct Options {
2929
std::vector<unsigned int> cutoffs;
30+
unsigned int runtime_check = 0;
3031
};
3132

3233
explicit LowerAllowCheckPass(LowerAllowCheckPass::Options Opts)

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,19 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
938938

939939
Result.cutoffs[index] = cutoff;
940940
}
941+
} else if (ParamName.starts_with("runtime_check")) {
942+
StringRef ValueString;
943+
std::tie(std::ignore, ValueString) = ParamName.split("=");
944+
int runtime_check;
945+
if (ValueString.getAsInteger(0, runtime_check)) {
946+
return make_error<StringError>(
947+
formatv("invalid LowerAllowCheck pass runtime_check parameter '{}' "
948+
"({})",
949+
ValueString, Params)
950+
.str(),
951+
inconvertibleErrorCode());
952+
}
953+
Result.runtime_check = runtime_check;
941954
} else {
942955
return make_error<StringError>(
943956
formatv("invalid LowerAllowCheck pass parameter '{}'", ParamName)

llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE,
7474
static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
7575
const ProfileSummaryInfo *PSI,
7676
OptimizationRemarkEmitter &ORE,
77-
const std::vector<unsigned int> &cutoffs) {
77+
const LowerAllowCheckPass::Options &Opts) {
7878
SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;
7979
std::unique_ptr<RandomNumberGenerator> Rng;
8080

@@ -89,8 +89,10 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
8989
return HotPercentileCutoff;
9090
else if (II->getIntrinsicID() == Intrinsic::allow_ubsan_check) {
9191
auto *Kind = cast<ConstantInt>(II->getArgOperand(0));
92-
if (Kind->getZExtValue() < cutoffs.size())
93-
return cutoffs[Kind->getZExtValue()];
92+
if (Kind->getZExtValue() < Opts.cutoffs.size())
93+
return Opts.cutoffs[Kind->getZExtValue()];
94+
} else if (II->getIntrinsicID() == Intrinsic::allow_runtime_check) {
95+
return Opts.runtime_check;
9496
}
9597

9698
return 0;
@@ -157,7 +159,7 @@ PreservedAnalyses LowerAllowCheckPass::run(Function &F,
157159
OptimizationRemarkEmitter &ORE =
158160
AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
159161

160-
return removeUbsanTraps(F, BFI, PSI, ORE, Opts.cutoffs)
162+
return removeUbsanTraps(F, BFI, PSI, ORE, Opts)
161163
// We do not change the CFG, we only replace the intrinsics with
162164
// true or false.
163165
? PreservedAnalyses::none().preserveSet<CFGAnalyses>()
@@ -193,5 +195,11 @@ void LowerAllowCheckPass::printPipeline(
193195

194196
i++;
195197
}
198+
if (Opts.runtime_check) {
199+
if (printed)
200+
OS << ";";
201+
OS << "runtime_check=" << Opts.runtime_check;
202+
}
203+
196204
OS << '>';
197205
}

llvm/test/Transforms/lower-builtin-allow-check-remarks.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=0 -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s
55
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check)' -lower-allow-check-percentile-cutoff-hot=1000000 -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s --check-prefixes=REMOVE
66

7+
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check<cutoffs[7]=0;runtime_check=1000000>)' -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s --check-prefixes=MIXED
8+
; RUN: opt < %s -passes='require<profile-summary>,function(lower-allow-check<cutoffs[7]=1000000;runtime_check=0>)' -pass-remarks=lower-allow-check -pass-remarks-missed=lower-allow-check -S 2>&1 | FileCheck %s --check-prefixes=MIXED2
9+
710
; CHECK: remark: <unknown>:0:0: Allowed check: Kind=test_check F=test_runtime BB=entry1
811
; CHECK: remark: <unknown>:0:0: Allowed check: Kind=7 F=test_ubsan BB=entry2
912

1013
; REMOVE: remark: <unknown>:0:0: Removed check: Kind=test_check F=test_runtime BB=entry1
1114
; REMOVE: remark: <unknown>:0:0: Removed check: Kind=7 F=test_ubsan BB=entry2
1215

16+
; MIXED: remark: <unknown>:0:0: Removed check: Kind=test_check F=test_runtime BB=entry1
17+
; MIXED: remark: <unknown>:0:0: Allowed check: Kind=7 F=test_ubsan BB=entry2
18+
19+
; MIXED2: remark: <unknown>:0:0: Allowed check: Kind=test_check F=test_runtime BB=entry1
20+
; MIXED2: remark: <unknown>:0:0: Removed check: Kind=7 F=test_ubsan BB=entry2
21+
1322
target triple = "x86_64-pc-linux-gnu"
1423

1524
define i1 @test_runtime() local_unnamed_addr {

0 commit comments

Comments
 (0)