Skip to content

Commit 66f837a

Browse files
committed
add a flag to control unprofiled function
1 parent f07457f commit 66f837a

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
660660
llvm::sys::path::append(Path, "default_%m.profraw");
661661
CmdArgs.push_back("-mllvm");
662662
CmdArgs.push_back(Args.MakeArgString(
663-
Twine("--instrument-sample-cold-function-path=") + Path));
663+
Twine("--instrument-cold-function-coverage-path=") + Path));
664664
CmdArgs.push_back("-mllvm");
665665
CmdArgs.push_back("--instrument-cold-function-coverage");
666666
CmdArgs.push_back("-mllvm");
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %clang -### -c -fprofile-generate-cold-function-coverage %s 2>&1 | FileCheck %s
2-
// CHECK: "--instrument-sample-cold-function-path=default_%m.profraw"
2+
// CHECK: "--instrument-cold-function-coverage-path=default_%m.profraw"
33
// CHECK: "--instrument-cold-function-coverage"
44
// CHECK: "--pgo-function-entry-coverage"
55
// CHECK-NOT: "-fprofile-instrument"
66
// CHECK-NOT: "-fprofile-instrument-path=
77

8-
// RUN: %clang -### -c -fprofile-generate-cold-function-coverage=dir %s 2>&1 | FileCheck %s --check-prefix=CHECK-PATH
9-
// CHECK-PATH: "--instrument-sample-cold-function-path=dir{{/|\\\\}}default_%m.profraw"
8+
// RUN: %clang -### -c -fprofile-generate-cold-function-coverage=dir %s 2>&1 | FileCheck %s --check-prefix=CHECK-EQ
9+
// CHECK-EQ: "--instrument-cold-function-coverage-path=dir{{/|\\\\}}default_%m.profraw"

llvm/include/llvm/Transforms/Instrumentation/PGOInstrumentation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class PGOInstrumentationGenCreateVar
5353
bool ProfileSampling;
5454
};
5555

56+
enum class InstrColdFuncCovMode { Conservative = 0, Optimistic };
57+
5658
enum class PGOInstrumentationType { Invalid = 0, FDO, CSFDO, CTXPROF };
5759
/// The instrumentation (profile-instr-gen) pass for IR based PGO.
5860
class PGOInstrumentationGen : public PassInfoMixin<PGOInstrumentationGen> {

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static cl::opt<bool> UseLoopVersioningLICM(
298298

299299
static cl::opt<std::string> InstrumentColdFuncCoveragePath(
300300
"instrument-cold-function-coverage-path", cl::init(""),
301-
cl::desc("File path for cold function coverage instrumentation"));,
301+
cl::desc("File path for cold function coverage instrumentation"),
302302
cl::Hidden);
303303

304304
extern cl::opt<std::string> UseCtxProfile;

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ static cl::opt<uint64_t> ColdFuncCoverageMaxEntryCount(
325325
"instrumenting the function whose entry count is above the given "
326326
"value"));
327327

328+
static cl::opt<InstrColdFuncCovMode> InstrumentColdFunctionCoverageMode(
329+
"instrument-cold-function-coverage-mode",
330+
cl::init(InstrColdFuncCovMode::Conservative), cl::Hidden,
331+
cl::desc("Control whether instrumenting unprofiled functions for cold "
332+
"function coverage."),
333+
cl::values(
334+
clEnumValN(InstrColdFuncCovMode::Conservative, "conservative",
335+
"Assume unprofiled functions are not cold, skip "
336+
"instrumenting them."),
337+
clEnumValN(InstrColdFuncCovMode::Optimistic, "optimistic",
338+
"Treat unprofiled functions as cold and instrument them.")));
339+
328340
cl::opt<bool> InstrumentColdFunctionCoverage(
329341
"instrument-cold-function-coverage", cl::init(false), cl::Hidden,
330342
cl::desc("Enable cold function coverage instrumentation (currently only "
@@ -1902,10 +1914,12 @@ static bool skipPGOGen(const Function &F) {
19021914
return true;
19031915
if (F.getInstructionCount() < PGOFunctionSizeThreshold)
19041916
return true;
1905-
if (InstrumentColdFunctionCoverage &&
1906-
(!F.getEntryCount() ||
1907-
F.getEntryCount()->getCount() > ColdFuncCoverageMaxEntryCount))
1908-
return true;
1917+
if (InstrumentColdFunctionCoverage) {
1918+
if (!F.getEntryCount())
1919+
return InstrumentColdFunctionCoverageMode ==
1920+
InstrColdFuncCovMode::Conservative;
1921+
return F.getEntryCount()->getCount() > ColdFuncCoverageMaxEntryCount;
1922+
}
19091923
return false;
19101924
}
19111925

llvm/test/Transforms/PGOProfile/instr-gen-cold-function.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
; RUN: opt < %s --passes=pgo-instr-gen -instrument-cold-function-coverage -pgo-function-entry-coverage -S | FileCheck --check-prefixes=COLD %s
22
; RUN: opt < %s --passes=pgo-instr-gen -instrument-cold-function-coverage -pgo-function-entry-coverage -cold-function-coverage-max-entry-count=1 -S | FileCheck --check-prefixes=ENTRY-COUNT %s
3+
; RUN: opt < %s --passes=pgo-instr-gen -instrument-cold-function-coverage -pgo-function-entry-coverage -instrument-cold-function-coverage-mode=optimistic -S | FileCheck --check-prefixes=UNPROFILED-FUNC %s
34

45
; COLD: call void @llvm.instrprof.cover(ptr @__profn_foo, i64 [[#]], i32 1, i32 0)
56
; COLD-NOT: __profn_main
7+
; COLD-NOT: __profn_bar
68

79
; ENTRY-COUNT: call void @llvm.instrprof.cover(ptr @__profn_foo, i64 [[#]], i32 1, i32 0)
810
; ENTRY-COUNT: call void @llvm.instrprof.cover(ptr @__profn_main, i64 [[#]], i32 1, i32 0)
911

12+
; UNPROFILED-FUNC: call void @llvm.instrprof.cover(ptr @__profn_bar, i64 [[#]], i32 1, i32 0)
13+
; UNPROFILED-FUNC: call void @llvm.instrprof.cover(ptr @__profn_foo, i64 [[#]], i32 1, i32 0)
14+
15+
1016
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
1117
target triple = "x86_64-unknown-linux-gnu"
1218

19+
define void @bar() {
20+
entry:
21+
ret void
22+
}
23+
1324
define void @foo() !prof !0 {
1425
entry:
1526
ret void

0 commit comments

Comments
 (0)