Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -fprofile-sample-use #4531

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Big news
- Frontend, druntime and Phobos are at version [2.106.0](https://dlang.org/changelog/2.106.0.html). (#4522)
- New command-line options `-fno-{exceptions,moduleinfo,rtti}` to selectively enable some `-betterC` effects. (#4522)
- New command-line option `-fprofile-sample-use` for using sample-based profile data for optimization. Functionality and usage is identical to Clang's option with same name. (#4531).

#### Platform support

Expand Down
15 changes: 13 additions & 2 deletions driver/cl_options_instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ cl::opt<std::string> IRPGOInstrGenFile(
"'=<filename>' or LLVM_PROFILE_FILE env var)"),
cl::ZeroOrMore, cl::ValueOptional);

/// Option for generating IR-based PGO instrumentation (LLVM pass)
/// Option for using IR-based PGO instrumentation (LLVM pass)
cl::opt<std::string> IRPGOInstrUseFile(
"fprofile-use", cl::ZeroOrMore, cl::value_desc("filename"),
cl::desc("Use instrumentation data for profile-guided optimization"),
Expand All @@ -45,12 +45,20 @@ cl::opt<std::string> ASTPGOInstrGenFile(
"'=<filename>' or LLVM_PROFILE_FILE env var)"),
cl::ZeroOrMore, cl::ValueOptional);

/// Option for generating frontend-based PGO instrumentation
/// Option for using frontend-based PGO instrumentation
cl::opt<std::string> ASTPGOInstrUseFile(
"fprofile-instr-use", cl::ZeroOrMore, cl::value_desc("filename"),
cl::desc("Use instrumentation data for profile-guided optimization"),
cl::ValueRequired);

/// Option for using sample-based PGO instrumentation
cl::opt<std::string>
SamplePGOInstrUseFile("fprofile-sample-use", cl::Optional,
cl::value_desc("filename"),
cl::desc("Use instrumentation data for sample-based "
"profile guided optimizations"),
cl::ValueRequired);

cl::opt<int> fXRayInstructionThreshold(
"fxray-instruction-threshold", cl::value_desc("value"),
cl::desc("Sets the minimum function size to instrument with XRay"),
Expand Down Expand Up @@ -119,6 +127,9 @@ void initializeInstrumentationOptionsFromCmdline(const llvm::Triple &triple) {
} else if (!IRPGOInstrUseFile.empty()) {
pgoMode = PGO_IRBasedUse;
global.params.datafileInstrProf = fromPathString(IRPGOInstrUseFile).ptr;
} else if (!SamplePGOInstrUseFile.empty()) {
pgoMode = PGO_SampleBasedUse;
global.params.datafileInstrProf = fromPathString(SamplePGOInstrUseFile).ptr;
}

if (dmdFunctionTrace)
Expand Down
4 changes: 4 additions & 0 deletions driver/cl_options_instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum PGOKind {
PGO_ASTBasedUse,
PGO_IRBasedInstr,
PGO_IRBasedUse,
PGO_SampleBasedUse,
};
extern PGOKind pgoMode;
inline bool isInstrumentingForPGO() {
Expand All @@ -59,5 +60,8 @@ inline bool isInstrumentingForIRBasedPGO() {
return pgoMode == PGO_IRBasedInstr;
}
inline bool isUsingIRBasedPGOProfile() { return pgoMode == PGO_IRBasedUse; }
inline bool isUsingSampleBasedPGOProfile() {
return pgoMode == PGO_SampleBasedUse;
}

} // namespace opts
3 changes: 3 additions & 0 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,9 @@ void DtoDefineFunction(FuncDeclaration *fd, bool linkageAvailableExternally) {
if (opts::fSplitStack && !hasNoSplitStackUDA(fd)) {
func->addFnAttr("split-stack");
}
if (opts::isUsingSampleBasedPGOProfile()) {
func->addFnAttr("use-sample-profile");
}

llvm::BasicBlock *beginbb =
llvm::BasicBlock::Create(gIR->context(), "", func);
Expand Down
5 changes: 5 additions & 0 deletions gen/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,11 @@ static llvm::Optional<PGOOptions> getPGOOptions() {
PGOOptions::PGOAction::IRUse,
PGOOptions::CSPGOAction::NoCSAction,
debugInfoForProfiling, pseudoProbeForProfiling);
} else if (opts::isUsingSampleBasedPGOProfile()) {
return PGOOptions(global.params.datafileInstrProf, "", "",
PGOOptions::PGOAction::SampleUse,
PGOOptions::CSPGOAction::NoCSAction,
debugInfoForProfiling, pseudoProbeForProfiling);
}
#if LDC_LLVM_VER < 1600
return None;
Expand Down
21 changes: 21 additions & 0 deletions tests/PGO/sample_based.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Test basic use of sample-based PGO profile

// REQUIRES: atleast_llvm1500

// RUN: split-file %s %t
// RUN: %ldc -O2 -c -gline-tables-only -output-ll -of=%t.ll -fprofile-sample-use=%t/pgo-sample.prof %t/testcase.d && FileCheck %s < %t.ll

//--- pgo-sample.prof
foo:100:100
1: 100

//--- testcase.d
// CHECK: define{{.*}} @foo{{.*}} #[[ATTRID:[0-9]+]]{{.*}} !prof ![[PROFID:[0-9]+]]
// CHECK: attributes #[[ATTRID]] = {{.*}} "use-sample-profile"
// CHECK-DAG: "ProfileFormat", !"SampleProfile"
// CHECK-DAG: "TotalCount", i64 100
// CHECK-DAG: ![[PROFID]] = !{!"function_entry_count", i64 101}

extern (C) int foo () {
return 1;
}