Skip to content

Commit 18380c5

Browse files
authored
[UBSAN][HWASAN] Remove redundant flags (#87709)
Presense of `cutoff-hot` or `random-skip-rate` should be enough to trigger optimization.
1 parent e628581 commit 18380c5

File tree

7 files changed

+19
-23
lines changed

7 files changed

+19
-23
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ using namespace llvm;
100100
namespace llvm {
101101
extern cl::opt<bool> PrintPipelinePasses;
102102

103-
static cl::opt<bool> ClRemoveTraps("clang-remove-traps", cl::Optional,
104-
cl::desc("Insert remove-traps pass."));
105-
106103
// Experiment to move sanitizers earlier.
107104
static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
108105
"sanitizer-early-opt-ep", cl::Optional,
@@ -750,7 +747,7 @@ static void addSanitizers(const Triple &TargetTriple,
750747
PB.registerOptimizerLastEPCallback(SanitizersCallback);
751748
}
752749

753-
if (ClRemoveTraps) {
750+
if (RemoveTrapsPass::IsRequested()) {
754751
// We can optimize after inliner, and PGO profile matching. The hook below
755752
// is called at the end `buildFunctionSimplificationPipeline`, which called
756753
// from `buildInlinerPipeline`, which called after profile matching.

clang/test/CodeGen/remote-traps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang_cc1 -O1 -emit-llvm -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow %s -o - | FileCheck %s
2-
// RUN: %clang_cc1 -O1 -emit-llvm -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -mllvm -clang-remove-traps -mllvm -remove-traps-random-rate=1 %s -o - | FileCheck %s --implicit-check-not="call void @llvm.ubsantrap" --check-prefixes=REMOVE
2+
// RUN: %clang_cc1 -O1 -emit-llvm -fsanitize=signed-integer-overflow -fsanitize-trap=signed-integer-overflow -mllvm -remove-traps-random-rate=1 %s -o - | FileCheck %s --implicit-check-not="call void @llvm.ubsantrap" --check-prefixes=REMOVE
33

44
int test(int x) {
55
return x + 123;

llvm/include/llvm/Transforms/Instrumentation/RemoveTrapsPass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace llvm {
2525
class RemoveTrapsPass : public PassInfoMixin<RemoveTrapsPass> {
2626
public:
2727
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
28+
29+
static bool IsRequested();
2830
};
2931

3032
} // namespace llvm

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,6 @@ static cl::opt<bool> ClWithTls(
182182
"platforms that support this"),
183183
cl::Hidden, cl::init(true));
184184

185-
static cl::opt<bool>
186-
CSelectiveInstrumentation("hwasan-selective-instrumentation",
187-
cl::desc("Use selective instrumentation"),
188-
cl::Hidden, cl::init(false));
189-
190185
static cl::opt<int> ClHotPercentileCutoff("hwasan-percentile-cutoff-hot",
191186
cl::desc("Hot percentile cuttoff."));
192187

@@ -1503,6 +1498,8 @@ bool HWAddressSanitizer::selectiveInstrumentationShouldSkip(
15031498
std::bernoulli_distribution D(ClRandomSkipRate);
15041499
return (D(*Rng));
15051500
}
1501+
if (!ClHotPercentileCutoff.getNumOccurrences())
1502+
return false;
15061503
auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
15071504
ProfileSummaryInfo *PSI =
15081505
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
@@ -1527,7 +1524,7 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
15271524

15281525
NumTotalFuncs++;
15291526

1530-
if (CSelectiveInstrumentation && selectiveInstrumentationShouldSkip(F, FAM))
1527+
if (selectiveInstrumentationShouldSkip(F, FAM))
15311528
return;
15321529

15331530
NumInstrumentedFuncs++;

llvm/lib/Transforms/Instrumentation/RemoveTrapsPass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
4141
auto ShouldRemove = [&](bool IsHot) {
4242
if (!RandomRate.getNumOccurrences())
4343
return IsHot;
44+
assert(HotPercentileCutoff.getNumOccurrences());
4445
if (!Rng)
4546
Rng = F.getParent()->createRNG(F.getName());
4647
std::bernoulli_distribution D(RandomRate);
@@ -95,3 +96,8 @@ PreservedAnalyses RemoveTrapsPass::run(Function &F,
9596
return removeUbsanTraps(F, BFI, PSI) ? PreservedAnalyses::none()
9697
: PreservedAnalyses::all();
9798
}
99+
100+
bool RemoveTrapsPass::IsRequested() {
101+
return RandomRate.getNumOccurrences() ||
102+
HotPercentileCutoff.getNumOccurrences();
103+
}

llvm/test/Instrumentation/HWAddressSanitizer/pgo-opt-out-no-ps.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S \
2-
; RUN: -hwasan-selective-instrumentation=0 | FileCheck %s --check-prefix=FULL
3-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S \
4-
; RUN: -hwasan-selective-instrumentation=1 | FileCheck %s --check-prefix=SELSAN
1+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S | FileCheck %s --check-prefix=FULL
2+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=990000 | FileCheck %s --check-prefix=SELSAN
53

64
; FULL: @not_sanitized
75
; FULL-NEXT: %x = alloca i8, i64 4

llvm/test/Instrumentation/HWAddressSanitizer/pgo-opt-out.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-selective-instrumentation=1 \
2-
; RUN: -hwasan-percentile-cutoff-hot=700000 | FileCheck %s --check-prefix=HOT70
3-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-selective-instrumentation=1 \
4-
; RUN: -hwasan-percentile-cutoff-hot=990000 | FileCheck %s --check-prefix=HOT99
5-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-selective-instrumentation=1 \
6-
; RUN: -hwasan-random-skip-rate=0.0 | FileCheck %s --check-prefix=RANDOM0
7-
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-selective-instrumentation=1 \
8-
; RUN: -hwasan-random-skip-rate=1.0 | FileCheck %s --check-prefix=RANDOM1
1+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=700000 | FileCheck %s --check-prefix=HOT70
2+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-percentile-cutoff-hot=990000 | FileCheck %s --check-prefix=HOT99
3+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=0.0 | FileCheck %s --check-prefix=RANDOM0
4+
; RUN: opt < %s -passes='require<profile-summary>,hwasan' -S -hwasan-random-skip-rate=1.0 | FileCheck %s --check-prefix=RANDOM1
95

106
; HOT70: @sanitized
117
; HOT70-NEXT: @__hwasan_tls

0 commit comments

Comments
 (0)