Skip to content

Commit 93dc1b5

Browse files
committed
[Remarks][2/2] Expand remarks hotness threshold option support in more tools
This is the #2 of 2 changes that make remarks hotness threshold option available in more tools. The changes also allow the threshold to sync with hotness threshold from profile summary with special value 'auto'. This change expands remarks hotness threshold option -fdiagnostics-hotness-threshold in clang and *-remarks-hotness-threshold in other tools to utilize hotness threshold from profile summary. Remarks hotness filtering relies on several driver options. Table below lists how different options are correlated and affect final remarks outputs: | profile | hotness | threshold | remarks printed | |---------|---------|-----------|-----------------| | No | No | No | All | | No | No | Yes | None | | No | Yes | No | All | | No | Yes | Yes | None | | Yes | No | No | All | | Yes | No | Yes | None | | Yes | Yes | No | All | | Yes | Yes | Yes | >=threshold | In the presence of profile summary, it is often more desirable to directly use the hotness threshold from profile summary. The new argument value 'auto' indicates threshold will be synced with hotness threshold from profile summary during compilation. The "auto" threshold relies on the availability of profile summary. In case of missing such information, no remarks will be generated. Differential Revision: https://reviews.llvm.org/D85808
1 parent 3acda91 commit 93dc1b5

File tree

18 files changed

+313
-23
lines changed

18 files changed

+313
-23
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,6 @@ VALUE_CODEGENOPT(EmitCheckPathComponentsToStrip, 32, 0)
366366
/// Whether to report the hotness of the code region for optimization remarks.
367367
CODEGENOPT(DiagnosticsWithHotness, 1, 0)
368368

369-
/// The minimum hotness value a diagnostic needs in order to be included in
370-
/// optimization diagnostics.
371-
VALUE_CODEGENOPT(DiagnosticsHotnessThreshold, 32, 0)
372-
373369
/// Whether copy relocations support is available when building as PIE.
374370
CODEGENOPT(PIECopyRelocations, 1, 0)
375371

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,21 @@ class CodeGenOptions : public CodeGenOptionsBase {
346346
const char *Argv0 = nullptr;
347347
ArrayRef<const char *> CommandLineArgs;
348348

349+
/// The minimum hotness value a diagnostic needs in order to be included in
350+
/// optimization diagnostics.
351+
///
352+
/// The threshold is an Optional value, which maps to one of the 3 states:
353+
/// 1. 0 => threshold disabled. All remarks will be printed.
354+
/// 2. positive int => manual threshold by user. Remarks with hotness exceed
355+
/// threshold will be printed.
356+
/// 3. None => 'auto' threshold by user. The actual value is not
357+
/// available at command line, but will be synced with
358+
/// hotness threshold from profile summary during
359+
/// compilation.
360+
///
361+
/// If threshold option is not specified, it is disabled by default.
362+
Optional<uint64_t> DiagnosticsHotnessThreshold = 0;
363+
349364
public:
350365
// Define accessors/mutators for code generation options of enumeration type.
351366
#define CODEGENOPT(Name, Bits, Default)

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def err_drv_command_failure : Error<
117117
"unable to execute command: %0">;
118118
def err_drv_invalid_darwin_version : Error<
119119
"invalid Darwin version number: %0">;
120+
def err_drv_invalid_diagnotics_hotness_threshold : Error<
121+
"invalid argument in '%0', only integer or 'auto' is supported">;
120122
def err_drv_missing_argument : Error<
121123
"argument to '%0' is missing (expected %1 value%s1)">;
122124
def err_drv_invalid_Xarch_argument_with_args : Error<

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,9 @@ def fdiagnostics_print_source_range_info : Flag<["-"], "fdiagnostics-print-sourc
984984
def fdiagnostics_show_hotness : Flag<["-"], "fdiagnostics-show-hotness">, Group<f_Group>,
985985
Flags<[CC1Option]>, HelpText<"Enable profile hotness information in diagnostic line">;
986986
def fdiagnostics_hotness_threshold_EQ : Joined<["-"], "fdiagnostics-hotness-threshold=">,
987-
Group<f_Group>, Flags<[CC1Option]>, MetaVarName<"<number>">,
988-
HelpText<"Prevent optimization remarks from being output if they do not have at least this profile count">;
987+
Group<f_Group>, Flags<[CC1Option]>, MetaVarName<"<value>">,
988+
HelpText<"Prevent optimization remarks from being output if they do not have at least this profile count. "
989+
"Use 'auto' to apply the threshold from profile summary">;
989990
def fdiagnostics_show_option : Flag<["-"], "fdiagnostics-show-option">, Group<f_Group>,
990991
HelpText<"Print option name with mappable diagnostics">;
991992
def fdiagnostics_show_note_include_stack : Flag<["-"], "fdiagnostics-show-note-include-stack">,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/Basic/CommentOptions.h"
1515
#include "clang/Basic/DebugInfoOptions.h"
1616
#include "clang/Basic/Diagnostic.h"
17+
#include "clang/Basic/DiagnosticDriver.h"
1718
#include "clang/Basic/DiagnosticOptions.h"
1819
#include "clang/Basic/FileSystemOptions.h"
1920
#include "clang/Basic/LLVM.h"
@@ -66,6 +67,7 @@
6667
#include "llvm/Option/OptTable.h"
6768
#include "llvm/Option/Option.h"
6869
#include "llvm/ProfileData/InstrProfReader.h"
70+
#include "llvm/Remarks/HotnessThresholdParser.h"
6971
#include "llvm/Support/CodeGen.h"
7072
#include "llvm/Support/Compiler.h"
7173
#include "llvm/Support/Error.h"
@@ -1501,11 +1503,24 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
15011503
Diags.Report(diag::warn_drv_diagnostics_hotness_requires_pgo)
15021504
<< "-fdiagnostics-show-hotness";
15031505

1504-
Opts.DiagnosticsHotnessThreshold = getLastArgUInt64Value(
1505-
Args, options::OPT_fdiagnostics_hotness_threshold_EQ, 0);
1506-
if (Opts.DiagnosticsHotnessThreshold > 0 && !UsingProfile)
1507-
Diags.Report(diag::warn_drv_diagnostics_hotness_requires_pgo)
1508-
<< "-fdiagnostics-hotness-threshold=";
1506+
// Parse remarks hotness threshold. Valid value is either integer or 'auto'.
1507+
if (auto *arg =
1508+
Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
1509+
auto ResultOrErr =
1510+
llvm::remarks::parseHotnessThresholdOption(arg->getValue());
1511+
1512+
if (!ResultOrErr) {
1513+
Diags.Report(diag::err_drv_invalid_diagnotics_hotness_threshold)
1514+
<< "-fdiagnostics-hotness-threshold=";
1515+
} else {
1516+
Opts.DiagnosticsHotnessThreshold = *ResultOrErr;
1517+
if ((!Opts.DiagnosticsHotnessThreshold.hasValue() ||
1518+
Opts.DiagnosticsHotnessThreshold.getValue() > 0) &&
1519+
!UsingProfile)
1520+
Diags.Report(diag::warn_drv_diagnostics_hotness_requires_pgo)
1521+
<< "-fdiagnostics-hotness-threshold=";
1522+
}
1523+
}
15091524

15101525
// If the user requested to use a sample profile for PGO, then the
15111526
// backend will need to track source location information so the profile

clang/test/Driver/opt-record.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
// RUN: %clang -target x86_64-linux -### -o FOO -fuse-ld=gold -flto -fdiagnostics-hotness-threshold=100 -fsave-optimization-record -foptimization-record-passes=inline %s 2>&1 | FileCheck %s -check-prefix=CHECK-PASS
5252
// RUN: %clang -target x86_64-linux -### -o FOO -fuse-ld=lld -flto=thin -fdiagnostics-hotness-threshold=100 -fsave-optimization-record=some-format -foptimization-record-file=FOO.txt %s 2>&1 | FileCheck %s -check-prefix=CHECK-PASS-CUSTOM
5353
// RUN: %clang -target x86_64-linux -### -o FOO -fuse-ld=lld -flto=thin -fdiagnostics-hotness-threshold=100 -Rpass=inline -Rpass-missed=inline -Rpass-analysis=inline %s 2>&1 | FileCheck %s -check-prefix=CHECK-PASS-RPASS
54+
// RUN: %clang -target x86_64-linux -### -o FOO -fuse-ld=lld -flto=thin -fdiagnostics-hotness-threshold=auto -Rpass=inline -Rpass-missed=inline -Rpass-analysis=inline %s 2>&1 | FileCheck %s -check-prefix=CHECK-PASS-AUTO
5455

5556
// CHECK-NOPASS-NOT: "--plugin-opt=opt-remarks-filename="
5657
// CHECK-NOPASS-NOT: "--plugin-opt=opt-remarks-passes=inline"
@@ -75,3 +76,5 @@
7576
// CHECK-PASS-RPASS-SAME: "--plugin-opt=-pass-remarks-missed=inline"
7677
// CHECK-PASS-RPASS-SAME: "--plugin-opt=-pass-remarks-analysis=inline"
7778
// CHECK-PASS-RPASS-SAME: "--plugin-opt=opt-remarks-hotness-threshold=100"
79+
80+
// CHECK-PASS-AUTO: "--plugin-opt=opt-remarks-hotness-threshold=auto"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
_Z7callee1v:600:600
2+
1: 600
3+
_Z7callee2v:1:1
4+
1: 1
5+
_Z7caller1v:400:400
6+
1: 400
7+
_Z7caller2v:1:1
8+
1: 1
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Without hotness threshold, print both hot and cold remarks.
2+
// RUN: %clang_cc1 -triple x86_64-linux %s -emit-llvm-only -O3 \
3+
// RUN: -fprofile-sample-use=%S/Inputs/remarks-hotness.prof \
4+
// RUN: -Rpass=inline -Rpass-analysis=inline -Rpass-missed=inline \
5+
// RUN: -fexperimental-new-pass-manager -fdiagnostics-show-hotness 2>&1 \
6+
// RUN: | FileCheck -check-prefix=REMARKS %s
7+
8+
// With auto hotness threshold, only print hot remarks.
9+
// RUN: %clang_cc1 -triple x86_64-linux %s -emit-llvm-only -O3 \
10+
// RUN: -fprofile-sample-use=%S/Inputs/remarks-hotness.prof \
11+
// RUN: -Rpass=inline -Rpass-analysis=inline -Rpass-missed=inline \
12+
// RUN: -fexperimental-new-pass-manager -fdiagnostics-show-hotness \
13+
// RUN: -fdiagnostics-hotness-threshold=auto 2>&1 \
14+
// RUN: | FileCheck -check-prefix=HOT_CALL %s
15+
16+
int callee1() {
17+
return 1;
18+
}
19+
20+
__attribute__((noinline)) int callee2() {
21+
return 2;
22+
}
23+
24+
// REMARKS: _Z7callee1v inlined into _Z7caller1v
25+
// HOT_CALL: _Z7callee1v inlined into _Z7caller1v
26+
int caller1() {
27+
return callee1();
28+
}
29+
30+
// REMARKS: _Z7callee2v not inlined into _Z7caller2v
31+
// HOT_CALL-NOT: _Z7callee2v not inlined into _Z7caller2v
32+
int caller2() {
33+
return callee2();
34+
}

llvm/include/llvm/Analysis/ProfileSummaryInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Function;
3838
// units. This would require making this depend on BFI.
3939
class ProfileSummaryInfo {
4040
private:
41-
Module &M;
41+
const Module &M;
4242
std::unique_ptr<ProfileSummary> Summary;
4343
void computeThresholds();
4444
// Count thresholds to answer isHotCount and isColdCount queries.
@@ -58,7 +58,8 @@ class ProfileSummaryInfo {
5858
mutable DenseMap<int, uint64_t> ThresholdCache;
5959

6060
public:
61-
ProfileSummaryInfo(Module &M) : M(M) { refresh(); }
61+
ProfileSummaryInfo(const Module &M) : M(M) { refresh(); }
62+
6263
ProfileSummaryInfo(ProfileSummaryInfo &&Arg) = default;
6364

6465
/// If no summary is present, attempt to refresh.

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ class LLVMContext {
237237
/// included in optimization diagnostics.
238238
void setDiagnosticsHotnessThreshold(Optional<uint64_t> Threshold);
239239

240+
/// Return if hotness threshold is requested from PSI.
241+
bool isDiagnosticsHotnessThresholdSetFromPSI() const;
242+
240243
/// The "main remark streamer" used by all the specialized remark streamers.
241244
/// This streamer keeps generic remark metadata in memory throughout the life
242245
/// of the LLVMContext. This metadata may be emitted in a section in object

0 commit comments

Comments
 (0)