Skip to content

Commit b836f96

Browse files
authored
[Coverage] Support -fprofile-list for cold function coverage (#136333)
Add a new instrumentation section type `[sample-coldcov]` to support`-fprofile-list` for sample pgo based cold function coverage. Note that the current cold function coverage is based on sampling PGO pipeline, which is incompatible with the existing [llvm] option(see [PGOOptions](https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/PGOOptions.h#L27-L43)), so we can't reuse the IR-PGO(-fprofile-instrument=llvm) flag.
1 parent a7b5c30 commit b836f96

File tree

8 files changed

+44
-18
lines changed

8 files changed

+44
-18
lines changed

clang/docs/UsersManual.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,9 +3394,9 @@ This can be done using the ``-fprofile-list`` option.
33943394
33953395
$ clang++ -O2 -fprofile-instr-generate -fcoverage-mapping -fprofile-list=fun.list -fprofile-list=code.list code.cc -o code
33963396
3397-
Supported sections are ``[clang]``, ``[llvm]``, and ``[csllvm]`` representing
3398-
clang PGO, IRPGO, and CSIRPGO, respectively. Supported prefixes are ``function``
3399-
and ``source``. Supported categories are ``allow``, ``skip``, and ``forbid``.
3397+
Supported sections are ``[clang]``, ``[llvm]``, ``[csllvm]``, and ``[sample-coldcov]`` representing
3398+
clang PGO, IRPGO, CSIRPGO and sample PGO based cold function coverage, respectively. Supported prefixes
3399+
are ``function`` and ``source``. Supported categories are ``allow``, ``skip``, and ``forbid``.
34003400
``skip`` adds the ``skipprofile`` attribute while ``forbid`` adds the
34013401
``noprofile`` attribute to the appropriate function. Use
34023402
``default:<allow|skip|forbid>`` to specify the default category.

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is
223223
CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic
224224
CODEGENOPT(ContinuousProfileSync, 1, 0) ///< Enable continuous instrumentation profiling
225225
/// Choose profile instrumenation kind or no instrumentation.
226-
ENUM_CODEGENOPT(ProfileInstr, ProfileInstrKind, 2, ProfileNone)
226+
ENUM_CODEGENOPT(ProfileInstr, ProfileInstrKind, 4, ProfileNone)
227227
/// Choose profile kind for PGO use compilation.
228228
ENUM_CODEGENOPT(ProfileUse, ProfileInstrKind, 2, ProfileNone)
229229
/// Partition functions into N groups and select only functions in group i to be

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class CodeGenOptions : public CodeGenOptionsBase {
8686
// to use with PGO.
8787
ProfileIRInstr, // IR level PGO instrumentation in LLVM.
8888
ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
89+
ProfileIRSampleColdCov, // IR level sample pgo based cold function coverage
90+
// instrumentation in LLVM.
8991
};
9092

9193
enum EmbedBitcodeKind {

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7733,9 +7733,9 @@ def fpatchable_function_entry_section_EQ
77337733
HelpText<"Use Section instead of __patchable_function_entries">,
77347734
MarshallingInfoString<CodeGenOpts<"PatchableFunctionEntrySection">>;
77357735
def fprofile_instrument_EQ : Joined<["-"], "fprofile-instrument=">,
7736-
HelpText<"Enable PGO instrumentation">, Values<"none,clang,llvm,csllvm">,
7736+
HelpText<"Enable PGO instrumentation">, Values<"none,clang,llvm,csllvm,sample-coldcov">,
77377737
NormalizedValuesScope<"CodeGenOptions">,
7738-
NormalizedValues<["ProfileNone", "ProfileClangInstr", "ProfileIRInstr", "ProfileCSIRInstr"]>,
7738+
NormalizedValues<["ProfileNone", "ProfileClangInstr", "ProfileIRInstr", "ProfileCSIRInstr", "ProfileIRSampleColdCov"]>,
77397739
MarshallingInfoEnum<CodeGenOpts<"ProfileInstr">, "ProfileNone">;
77407740
def fprofile_instrument_path_EQ : Joined<["-"], "fprofile-instrument-path=">,
77417741
HelpText<"Generate instrumented code to collect execution counts into "

clang/lib/Basic/ProfileList.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static StringRef getSectionName(CodeGenOptions::ProfileInstrKind Kind) {
8181
return "llvm";
8282
case CodeGenOptions::ProfileCSIRInstr:
8383
return "csllvm";
84+
case CodeGenOptions::ProfileIRSampleColdCov:
85+
return "sample-coldcov";
8486
}
8587
llvm_unreachable("Unhandled CodeGenOptions::ProfileInstrKind enum");
8688
}

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
628628
CmdArgs.push_back("--pgo-instrument-cold-function-only");
629629
CmdArgs.push_back("-mllvm");
630630
CmdArgs.push_back("--pgo-function-entry-coverage");
631+
CmdArgs.push_back("-fprofile-instrument=sample-coldcov");
631632
}
632633

633634
if (auto *A = Args.getLastArg(options::OPT_ftemporal_profile)) {

clang/test/CodeGen/profile-filter.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm %s -o - | FileCheck %s
1+
// RUN: rm -rf %t && split-file %s %t
22

3-
// RUN: echo "fun:test1" > %t-func.list
4-
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t-func.list -emit-llvm %s -o - | FileCheck %s --check-prefix=FUNC
3+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm %t/main.c -o - | FileCheck %s
4+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t/func.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=FUNC
55

6-
// RUN: echo "src:%s" | sed -e 's/\\/\\\\/g' > %t-file.list
7-
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t-file.list -emit-llvm %s -o - | FileCheck %s --check-prefix=FILE
6+
// RUN: echo "src:%t/main.c" | sed -e 's/\\/\\\\/g' > %t-file.list
7+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t-file.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=FILE
8+
// RUN: %clang_cc1 -fprofile-instrument=llvm -fprofile-list=%t/section.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=SECTION
9+
// RUN: %clang_cc1 -fprofile-instrument=sample-coldcov -fprofile-list=%t/cold-func.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=COLDCOV
10+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t/exclude.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=EXCLUDE
11+
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t/exclude-only.list -emit-llvm %t/main.c -o - | FileCheck %s --check-prefix=EXCLUDE
812

9-
// RUN: echo -e "[clang]\nfun:test1\n[llvm]\nfun:test2" > %t-section.list
10-
// RUN: %clang_cc1 -fprofile-instrument=llvm -fprofile-list=%t-section.list -emit-llvm %s -o - | FileCheck %s --check-prefix=SECTION
13+
//--- func.list
14+
fun:test1
1115

12-
// RUN: echo -e "fun:test*\n!fun:test1" > %t-exclude.list
13-
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t-exclude.list -emit-llvm %s -o - | FileCheck %s --check-prefix=EXCLUDE
16+
//--- section.list
17+
[clang]
18+
fun:test1
19+
[llvm]
20+
fun:test2
1421

15-
// RUN: echo "!fun:test1" > %t-exclude-only.list
16-
// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -fprofile-list=%t-exclude-only.list -emit-llvm %s -o - | FileCheck %s --check-prefix=EXCLUDE
22+
//--- cold-func.list
23+
[sample-coldcov]
24+
fun:test*
25+
!fun:test2
1726

27+
//--- exclude.list
28+
fun:test*
29+
!fun:test1
30+
31+
//--- exclude-only.list
32+
!fun:test1
33+
34+
//--- main.c
1835
unsigned i;
1936

2037
// CHECK: test1
@@ -36,6 +53,8 @@ unsigned i;
3653
// SECTION: @test1
3754
// EXCLUDE: noprofile
3855
// EXCLUDE: @test1
56+
// COLDCOV-NOT: noprofile
57+
// COLDCOV: @test1
3958
unsigned test1(void) {
4059
// CHECK: %pgocount = load i64, ptr @__profc_{{_?}}test1
4160
// FUNC: %pgocount = load i64, ptr @__profc_{{_?}}test1
@@ -55,6 +74,8 @@ unsigned test1(void) {
5574
// SECTION: @test2
5675
// EXCLUDE-NOT: noprofile
5776
// EXCLUDE: @test2
77+
// COLDCOV: noprofile
78+
// COLDCOV: @test2
5879
unsigned test2(void) {
5980
// CHECK: %pgocount = load i64, ptr @__profc_{{_?}}test2
6081
// FUNC-NOT: %pgocount = load i64, ptr @__profc_{{_?}}test2

clang/test/Driver/fprofile-generate-cold-function-coverage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// CHECK: "--instrument-cold-function-only-path=default_%m.profraw"
33
// CHECK: "--pgo-instrument-cold-function-only"
44
// CHECK: "--pgo-function-entry-coverage"
5-
// CHECK-NOT: "-fprofile-instrument"
5+
// CHECK: "-fprofile-instrument=sample-coldcov"
66
// CHECK-NOT: "-fprofile-instrument-path=
77

88
// RUN: %clang -### -c -fprofile-generate-cold-function-coverage=dir %s 2>&1 | FileCheck %s --check-prefix=CHECK-EQ

0 commit comments

Comments
 (0)