Skip to content

Commit 91892e8

Browse files
authored
[InstrProf] Add frontend temporal profiling flag (#122385)
As discussed in #121514 add the frontend flag `-ftemporal-profile` to enable temporal profiling (https://discourse.llvm.org/t/rfc-temporal-profiling-extension-for-irpgo/68068) as a replacement for `-forder-file-instrumentation` (https://discourse.llvm.org/t/deprecate-forder-file-instrumentation-in-favor-of-temporal-profiling/83903)
1 parent 3fbc344 commit 91892e8

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

clang/docs/UsersManual.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,38 @@ indexed format, regardeless whether it is produced by frontend or the IR pass.
30353035
overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
30363036
by the target, or ``single`` otherwise.
30373037

3038+
.. option:: -ftemporal-profile
3039+
3040+
Enables the temporal profiling extension for IRPGO to improve startup time by
3041+
reducing ``.text`` section page faults. To do this, we instrument function
3042+
timestamps to measure when each function is called for the first time and use
3043+
this data to generate a function order to improve startup.
3044+
3045+
The profile is generated as normal.
3046+
3047+
.. code-block:: console
3048+
3049+
$ clang++ -O2 -fprofile-generate -ftemporal-profile code.cc -o code
3050+
$ ./code
3051+
$ llvm-profdata merge -o code.profdata yyy/zzz
3052+
3053+
Using the resulting profile, we can generate a function order to pass to the
3054+
linker via `--symbol-ordering-file` for ELF or `-order_file` for Mach-O.
3055+
3056+
.. code-block:: console
3057+
3058+
$ llvm-profdata order code.profdata -o code.orderfile
3059+
$ clang++ -O2 -Wl,--symbol-ordering-file=code.orderfile code.cc -o code
3060+
3061+
Or the profile can be passed to LLD directly.
3062+
3063+
.. code-block:: console
3064+
3065+
$ clang++ -O2 -fuse-ld=lld -Wl,--irpgo-profile=code.profdata,--bp-startup-sort=function code.cc -o code
3066+
3067+
For more information, please read the RFC:
3068+
https://discourse.llvm.org/t/rfc-temporal-profiling-extension-for-irpgo/68068
3069+
30383070
Fine Tuning Profile Collection
30393071
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30403072

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,9 @@ def fprofile_generate_cold_function_coverage : Flag<["-"], "fprofile-generate-co
17961796
def fprofile_generate_cold_function_coverage_EQ : Joined<["-"], "fprofile-generate-cold-function-coverage=">,
17971797
Group<f_Group>, Visibility<[ClangOption, CLOption]>, MetaVarName<"<directory>">,
17981798
HelpText<"Generate instrumented code to collect coverage info for cold functions into <directory>/default.profraw (overridden by LLVM_PROFILE_FILE env var)">;
1799+
def ftemporal_profile : Flag<["-"], "ftemporal-profile">,
1800+
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
1801+
HelpText<"Generate instrumented code to collect temporal information">;
17991802
def fprofile_instr_generate : Flag<["-"], "fprofile-instr-generate">,
18001803
Group<f_Group>, Visibility<[ClangOption, CLOption]>,
18011804
HelpText<"Generate instrumented code to collect execution counts into default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var)">;
@@ -1891,7 +1894,7 @@ defm pseudo_probe_for_profiling : BoolFOption<"pseudo-probe-for-profiling",
18911894
" pseudo probes for sample profiling">>;
18921895
def forder_file_instrumentation : Flag<["-"], "forder-file-instrumentation">,
18931896
Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>,
1894-
HelpText<"Generate instrumented code to collect order file into default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var). Deprecated, please use temporal profiling.">;
1897+
HelpText<"Generate instrumented code to collect order file into default.profraw file (overridden by '=' form of option or LLVM_PROFILE_FILE env var). Deprecated, please use -ftemporal-profile">;
18951898
def fprofile_list_EQ : Joined<["-"], "fprofile-list=">,
18961899
Group<f_Group>, Visibility<[ClangOption, CC1Option, CLOption]>,
18971900
HelpText<"Filename defining the list of functions/files to instrument. "

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,14 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
662662
CmdArgs.push_back("--pgo-function-entry-coverage");
663663
}
664664

665+
if (auto *A = Args.getLastArg(options::OPT_ftemporal_profile)) {
666+
if (!PGOGenerateArg && !CSPGOGenerateArg)
667+
D.Diag(clang::diag::err_drv_argument_only_allowed_with)
668+
<< A->getSpelling() << "-fprofile-generate or -fcs-profile-generate";
669+
CmdArgs.push_back("-mllvm");
670+
CmdArgs.push_back("--pgo-temporal-instrumentation");
671+
}
672+
665673
Arg *PGOGenArg = nullptr;
666674
if (PGOGenerateArg) {
667675
assert(!CSPGOGenerateArg);
@@ -8054,7 +8062,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
80548062
Args.getLastArg(options::OPT_forder_file_instrumentation)) {
80558063
D.Diag(diag::warn_drv_deprecated_arg)
80568064
<< A->getAsString(Args) << /*hasReplacement=*/true
8057-
<< "-mllvm -pgo-temporal-instrumentation";
8065+
<< "-ftemporal-profile";
80588066
CmdArgs.push_back("-forder-file-instrumentation");
80598067
// Enable order file instrumentation when ThinLTO is not on. When ThinLTO is
80608068
// on, we need to pass these flags as linker flags and that will be handled

clang/test/Driver/clang_f_opts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@
424424
// CHECK-WARNING-DAG: optimization flag '-fno-devirtualize-speculatively' is not supported
425425
// CHECK-WARNING-DAG: the flag '-fslp-vectorize-aggressive' has been deprecated and will be ignored
426426
// CHECK-WARNING-DAG: the flag '-fno-slp-vectorize-aggressive' has been deprecated and will be ignored
427-
// CHECK-WARNING-DAG: argument '-forder-file-instrumentation' is deprecated, use '-mllvm -pgo-temporal-instrumentation' instead
427+
// CHECK-WARNING-DAG: argument '-forder-file-instrumentation' is deprecated, use '-ftemporal-profile' instead
428428

429429
// Test that we mute the warning on these
430430
// RUN: %clang -### -finline-limit=1000 -Wno-invalid-command-line-argument \
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang -### -c -fprofile-generate -ftemporal-profile %s 2>&1 | FileCheck %s
2+
// RUN: %clang -### -c -fcs-profile-generate -ftemporal-profile %s 2>&1 | FileCheck %s
3+
// RUN: not %clang -### -c -ftemporal-profile %s 2>&1 | FileCheck %s --check-prefix=ERR
4+
5+
// CHECK: "-mllvm" "--pgo-temporal-instrumentation"
6+
7+
// ERR: error: invalid argument '-ftemporal-profile' only allowed with '-fprofile-generate or -fcs-profile-generate'

0 commit comments

Comments
 (0)