Skip to content

Commit 0146448

Browse files
committed
[clang][ubsan] Switch UBSAN optimization to llvm.allow.{runtime,ubsan}.check()
Intrinsic introduced with #84850. Intrinsics improves performance by 3% comparing to removing traps (on "test-suite/MultiSource/Benchmarks" with PGO+ThinLTO). The pass will be renamed with #84853. RFC: https://discourse.llvm.org/t/rfc-add-llvm-experimental-hot-intrinsic-or-llvm-hot/77641 Reviewers: aeubanks, nikic, kstoimenov, dvyukov Reviewed By: kstoimenov Pull Request: #84858
1 parent 852eb20 commit 0146448

File tree

3 files changed

+107
-72
lines changed

3 files changed

+107
-72
lines changed

clang/test/CodeGen/remote-traps.c

Lines changed: 0 additions & 37 deletions
This file was deleted.

llvm/lib/Transforms/Instrumentation/RemoveTrapsPass.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/SmallVector.h"
1212
#include "llvm/ADT/Statistic.h"
1313
#include "llvm/Analysis/ProfileSummaryInfo.h"
14+
#include "llvm/IR/Constant.h"
1415
#include "llvm/IR/Instructions.h"
1516
#include "llvm/IR/IntrinsicInst.h"
1617
#include "llvm/IR/Intrinsics.h"
@@ -35,9 +36,11 @@ STATISTIC(NumChecksRemoved, "Number of removed checks");
3536

3637
static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
3738
const ProfileSummaryInfo *PSI) {
38-
SmallVector<IntrinsicInst *, 16> Remove;
39+
SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;
3940
std::unique_ptr<RandomNumberGenerator> Rng;
4041

42+
// TODO:
43+
// https://github.com/llvm/llvm-project/pull/84858#discussion_r1520603139
4144
auto ShouldRemove = [&](bool IsHot) {
4245
if (!RandomRate.getNumOccurrences())
4346
return IsHot;
@@ -54,21 +57,23 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
5457
continue;
5558
auto ID = II->getIntrinsicID();
5659
switch (ID) {
57-
case Intrinsic::ubsantrap: {
60+
case Intrinsic::allow_ubsan_check:
61+
case Intrinsic::allow_runtime_check: {
5862
++NumChecksTotal;
5963

6064
bool IsHot = false;
6165
if (PSI) {
62-
uint64_t Count = 0;
63-
for (const auto *PR : predecessors(&BB))
64-
Count += BFI.getBlockProfileCount(PR).value_or(0);
66+
uint64_t Count = BFI.getBlockProfileCount(&BB).value_or(0);
6567
IsHot = PSI->isHotCountNthPercentile(HotPercentileCutoff, Count);
6668
}
6769

68-
if (ShouldRemove(IsHot)) {
69-
Remove.push_back(II);
70+
bool ToRemove = ShouldRemove(IsHot);
71+
ReplaceWithValue.push_back({
72+
II,
73+
ToRemove,
74+
});
75+
if (ToRemove)
7076
++NumChecksRemoved;
71-
}
7277
break;
7378
}
7479
default:
@@ -77,10 +82,12 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
7782
}
7883
}
7984

80-
for (IntrinsicInst *I : Remove)
85+
for (auto [I, V] : ReplaceWithValue) {
86+
I->replaceAllUsesWith(ConstantInt::getBool(I->getType(), !V));
8187
I->eraseFromParent();
88+
}
8289

83-
return !Remove.empty();
90+
return !ReplaceWithValue.empty();
8491
}
8592

8693
PreservedAnalyses RemoveTrapsPass::run(Function &F,

0 commit comments

Comments
 (0)