Skip to content

Commit 8d2034c

Browse files
[clang] Add flag fallow-runtime-check-skip-hot-cutoff (#145999)
Co-authored-by: Kazu Hirata <[email protected]>
1 parent 23be14b commit 8d2034c

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ class CodeGenOptions : public CodeGenOptionsBase {
399399
/// with extra debug info.
400400
SanitizerSet SanitizeAnnotateDebugInfo;
401401

402+
std::optional<double> AllowRuntimeCheckSkipHotCutoff;
403+
402404
/// List of backend command-line options for -fembed-bitcode.
403405
std::vector<uint8_t> CmdArgs;
404406

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,15 @@ def fsanitize_skip_hot_cutoff_EQ
26682668

26692669
} // end -f[no-]sanitize* flags
26702670

2671+
def fallow_runtime_check_skip_hot_cutoff_EQ
2672+
: Joined<["-"], "fallow-runtime-check-skip-hot-cutoff=">,
2673+
Group<f_clang_Group>,
2674+
Visibility<[ClangOption, CC1Option]>,
2675+
HelpText<"Exclude __builtin_allow_runtime_check for the top hottest "
2676+
"code responsible for the given fraction of PGO counters "
2677+
"(0.0 [default] = skip none; 1.0 = skip all). "
2678+
"Argument format: <value>">;
2679+
26712680
def funsafe_math_optimizations : Flag<["-"], "funsafe-math-optimizations">,
26722681
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
26732682
HelpText<"Allow unsafe floating-point math optimizations which may decrease precision">,

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,17 +805,21 @@ static void addSanitizers(const Triple &TargetTriple,
805805
// SanitizeSkipHotCutoffs: doubles with range [0, 1]
806806
// Opts.cutoffs: unsigned ints with range [0, 1000000]
807807
auto ScaledCutoffs = CodeGenOpts.SanitizeSkipHotCutoffs.getAllScaled(1000000);
808-
808+
uint64_t AllowRuntimeCheckSkipHotCutoff =
809+
CodeGenOpts.AllowRuntimeCheckSkipHotCutoff.value_or(0.0) * 1000000;
809810
// TODO: remove IsRequested()
810-
if (LowerAllowCheckPass::IsRequested() || ScaledCutoffs.has_value()) {
811+
if (LowerAllowCheckPass::IsRequested() || ScaledCutoffs.has_value() ||
812+
CodeGenOpts.AllowRuntimeCheckSkipHotCutoff.has_value()) {
811813
// We want to call it after inline, which is about OptimizerEarlyEPCallback.
812814
PB.registerOptimizerEarlyEPCallback(
813-
[ScaledCutoffs](ModulePassManager &MPM, OptimizationLevel Level,
814-
ThinOrFullLTOPhase Phase) {
815+
[ScaledCutoffs, AllowRuntimeCheckSkipHotCutoff](
816+
ModulePassManager &MPM, OptimizationLevel Level,
817+
ThinOrFullLTOPhase Phase) {
815818
LowerAllowCheckPass::Options Opts;
816819
// TODO: after removing IsRequested(), make this unconditional
817820
if (ScaledCutoffs.has_value())
818821
Opts.cutoffs = ScaledCutoffs.value();
822+
Opts.runtime_check = AllowRuntimeCheckSkipHotCutoff;
819823
MPM.addPass(
820824
createModuleToFunctionPassAdaptor(LowerAllowCheckPass(Opts)));
821825
});

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6028,7 +6028,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
60286028
// -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
60296029
// complicated ways.
60306030
auto SanitizeArgs = TC.getSanitizerArgs(Args);
6031-
6031+
Args.AddLastArg(CmdArgs,
6032+
options::OPT_fallow_runtime_check_skip_hot_cutoff_EQ);
60326033
bool IsAsyncUnwindTablesDefault =
60336034
TC.getDefaultUnwindTableLevel(Args) == ToolChain::UnwindTableLevel::Asynchronous;
60346035
bool IsSyncUnwindTablesDefault =

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,11 @@ void CompilerInvocationBase::GenerateCodeGenArgs(const CodeGenOptions &Opts,
18201820
for (std::string Sanitizer : Values)
18211821
GenerateArg(Consumer, OPT_fsanitize_skip_hot_cutoff_EQ, Sanitizer);
18221822

1823+
if (Opts.AllowRuntimeCheckSkipHotCutoff) {
1824+
GenerateArg(Consumer, OPT_fallow_runtime_check_skip_hot_cutoff_EQ,
1825+
std::to_string(*Opts.AllowRuntimeCheckSkipHotCutoff));
1826+
}
1827+
18231828
for (StringRef Sanitizer :
18241829
serializeSanitizerKinds(Opts.SanitizeAnnotateDebugInfo))
18251830
GenerateArg(Consumer, OPT_fsanitize_annotate_debug_info_EQ, Sanitizer);
@@ -2322,6 +2327,18 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
23222327
Args.getAllArgValues(OPT_fsanitize_annotate_debug_info_EQ), Diags,
23232328
Opts.SanitizeAnnotateDebugInfo);
23242329

2330+
if (StringRef V =
2331+
Args.getLastArgValue(OPT_fallow_runtime_check_skip_hot_cutoff_EQ);
2332+
!V.empty()) {
2333+
double A;
2334+
if (V.getAsDouble(A) || A < 0.0 || A > 1.0) {
2335+
Diags.Report(diag::err_drv_invalid_value)
2336+
<< "-fallow-runtime-check-skip-hot-cutoff=" << V;
2337+
} else {
2338+
Opts.AllowRuntimeCheckSkipHotCutoff = A;
2339+
}
2340+
}
2341+
23252342
Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true);
23262343

23272344
if (!LangOpts->CUDAIsDevice)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang -fallow-runtime-check-skip-hot-cutoff=1.0 -S -emit-llvm %s -o - -O2 | FileCheck --check-prefix=ONE %s
2+
// RUN: %clang -fallow-runtime-check-skip-hot-cutoff=0.0 -S -emit-llvm %s -o - -O2 | FileCheck --check-prefix=ZERO %s
3+
// RUN: not %clang -fallow-runtime-check-skip-hot-cutoff=6.0 -S -emit-llvm %s -o - -O2 2>&1 | FileCheck --check-prefix=SIX %s
4+
// RUN: not %clang -fallow-runtime-check-skip-hot-cutoff=-1.0 -S -emit-llvm %s -o - -O2 2>&1 | FileCheck --check-prefix=MINUSONE %s
5+
// RUN: not %clang -fallow-runtime-check-skip-hot-cutoff=string -S -emit-llvm %s -o - -O2 2>&1 | FileCheck --check-prefix=STRING %s
6+
7+
// ONE: ret i32 0
8+
// ZERO: ret i32 1
9+
// SIX: invalid value '6.0' in '-fallow-runtime-check-skip-hot-cutoff='
10+
// MINUSONE: invalid value '-1.0' in '-fallow-runtime-check-skip-hot-cutoff='
11+
// STRING: invalid value 'string' in '-fallow-runtime-check-skip-hot-cutoff='
12+
13+
int main(int argc, char** argv) {
14+
return __builtin_allow_runtime_check("foo");
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %clang -### -fallow-runtime-check-skip-hot-cutoff=1.0 %s 2>&1 | FileCheck %s
2+
// CHECK: -fallow-runtime-check-skip-hot-cutoff=1.0
3+
4+
int main(int argc, char** argv) {
5+
return __builtin_allow_runtime_check("foo");
6+
}

0 commit comments

Comments
 (0)