Skip to content
Open
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
18 changes: 18 additions & 0 deletions clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,24 @@ void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
CmdArgs.push_back(
Args.MakeArgString(Twine("--codegen-data-generate-path=") +
CodeGenDataGenArg->getValue()));
} else {
if (auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args)) {
SmallString<128> Path(CSPGOGenerateArg->getNumValues() == 0
? ""
: CSPGOGenerateArg->getValue());
llvm::sys::path::append(Path, "default_%m.profraw");
CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-cs-profile-generate");
CmdArgs.push_back("-mllvm");
CmdArgs.push_back(Args.MakeArgString(Twine("-cs-profile-path=") + Path));
} else if (auto *ProfileUseArg = getLastProfileUseArg(Args)) {
SmallString<128> Path(
ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
if (Path.empty() || llvm::sys::fs::is_directory(Path))
llvm::sys::path::append(Path, "default.profdata");
CmdArgs.push_back("-mllvm");
CmdArgs.push_back(Args.MakeArgString(Twine("-cs-profile-path=") + Path));
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/cspgo-lto.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@
// DARWIN-GEN1-SAME: "--cs-profile-path=default_%m.profraw"
// DARWIN-GEN2: "--cs-profile-generate"
// DARWIN-GEN2-SAME: "--cs-profile-path=directory{{(/|\\\\)}}default_%m.profraw"

// RUN: %clang --target=arm64-apple-macos -### %t.o -flto=thin -fuse-ld=ld -fcs-profile-generate 2>&1 | FileCheck %s --check-prefix=DARWIN-LD-GEN1
// RUN: %clang --target=arm64-apple-macos -### %t.o -flto=thin -fuse-ld=ld -fcs-profile-generate=directory 2>&1 | FileCheck %s --check-prefix=DARWIN-LD-GEN2

// DARWIN-LD-GEN1: "-mllvm" "-cs-profile-generate"
// DARWIN-LD-GEN1-SAME: "-mllvm" "-cs-profile-path=default_%m.profraw"
// DARWIN-LD-GEN2: "-mllvm" "-cs-profile-generate"
// DARWIN-LD-GEN2-SAME: "-mllvm" "-cs-profile-path=directory{{(/|\\\\)}}default_%m.profraw"

// RUN: %clang --target=arm64-apple-macos -### %t.o -flto=thin -fuse-ld=ld -fprofile-use 2>&1 | FileCheck %s --check-prefix=DARWIN-LD-USE1
// RUN: %clang --target=arm64-apple-macos -### %t.o -flto=thin -fuse-ld=ld -fprofile-use=a.profdata 2>&1 | FileCheck %s --check-prefix=DARWIN-LD-USE2

// DARWIN-LD-USE1-NOT: "-cs-profile-generate"
// DARWIN-LD-USE1: "-mllvm" "-cs-profile-path=default.profdata"
// DARWIN-LD-USE2-NOT: "-cs-profile-generate"
// DARWIN-LD-USE2: "-mllvm" "-cs-profile-path=a.profdata"
7 changes: 5 additions & 2 deletions llvm/lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ static cl::opt<std::string> AIXSystemAssemblerPath(
cl::desc("Path to a system assembler, picked up on AIX only"),
cl::value_desc("path"));

static cl::opt<bool>
cl::opt<bool>
LTORunCSIRInstr("cs-profile-generate",
cl::desc("Perform context sensitive PGO instrumentation"));

static cl::opt<std::string>
cl::opt<std::string>
LTOCSIRProfile("cs-profile-path",
cl::desc("Context sensitive profile file path"));
} // namespace llvm
Expand Down Expand Up @@ -560,6 +560,9 @@ bool LTOCodeGenerator::optimize() {

// libLTO parses options late, so re-set them here.
Context.setDiscardValueNames(LTODiscardValueNames);
Config.StatsFile = LTOStatsFile;
Config.RunCSIRInstr = LTORunCSIRInstr;
Config.CSIRProfile = LTOCSIRProfile;

auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
Context, RemarksFilename, RemarksPasses, RemarksFormat,
Expand Down
12 changes: 12 additions & 0 deletions llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ extern cl::opt<bool> RemarksWithHotness;
extern cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
RemarksHotnessThreshold;
extern cl::opt<std::string> RemarksFormat;
extern cl::opt<bool> LTORunCSIRInstr;
extern cl::opt<std::string> LTOCSIRProfile;
}

// Default to using all available threads in the system, but using only one
Expand Down Expand Up @@ -274,6 +276,16 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
unsigned OptLevel, bool Freestanding,
bool DebugPassManager, ModuleSummaryIndex *Index) {
std::optional<PGOOptions> PGOOpt;
if (LTORunCSIRInstr) {
PGOOpt =
PGOOptions("", LTOCSIRProfile, "",
/*MemoryProfile=*/"", PGOOptions::IRUse,
PGOOptions::CSIRInstr, PGOOptions::ColdFuncOpt::Default);
} else if (!LTOCSIRProfile.empty()) {
PGOOpt = PGOOptions(LTOCSIRProfile, "", "",
/*MemoryProfile=*/"", PGOOptions::IRUse,
PGOOptions::CSIRUse, PGOOptions::ColdFuncOpt::Default);
}
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/tools/llvm-lto/Inputs/cspgo-cs.proftext
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:csir
main
1895182923573755903
1
100000
1 change: 1 addition & 0 deletions llvm/test/tools/llvm-lto/Inputs/cspgo-noncs.proftext
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:ir
17 changes: 17 additions & 0 deletions llvm/test/tools/llvm-lto/cspgo-generate.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; REQUIRES: aarch64-registered-target
;
; RUN: llvm-profdata merge %S/Inputs/cspgo-noncs.proftext -o %t.profdata
; RUN: llvm-as %s -o %t.o
; RUN: llvm-lto -cs-profile-generate -cs-profile-path=%t.profdata \
; RUN: -exported-symbol=_main -o %t.lto.o %t.o
; RUN: llvm-nm %t.lto.o | FileCheck %s
;
; CHECK: __profc_main

target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx14.0.0"

define i32 @main() {
entry:
ret i32 0
}
21 changes: 21 additions & 0 deletions llvm/test/tools/llvm-lto/cspgo-thinlto-generate.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; REQUIRES: aarch64-registered-target
;
; RUN: llvm-profdata merge %S/Inputs/cspgo-noncs.proftext -o %t.profdata
; RUN: opt -module-summary %s -o %t.o
; RUN: llvm-lto -thinlto-action=optimize \
; RUN: -cs-profile-generate -cs-profile-path=%t.profdata \
; RUN: -o %t.opt.bc %t.o
; RUN: llvm-dis %t.opt.bc -o - | FileCheck %s
;
; CHECK: @__profc_main = private global
; CHECK-LABEL: @main()
; CHECK: %pgocount = load i64, ptr @__profc_main
; CHECK: store i64 %{{.*}}, ptr @__profc_main

target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx14.0.0"

define i32 @main() {
entry:
ret i32 0
}
20 changes: 20 additions & 0 deletions llvm/test/tools/llvm-lto/cspgo-thinlto-use.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; REQUIRES: aarch64-registered-target
;
; RUN: llvm-profdata merge %S/Inputs/cspgo-cs.proftext -o %t.profdata
; RUN: opt -module-summary %s -o %t.o
; RUN: llvm-lto -thinlto-action=optimize \
; RUN: -cs-profile-path=%t.profdata \
; RUN: -o %t.opt.bc %t.o
; RUN: llvm-dis %t.opt.bc -o - | FileCheck %s
;
; CHECK: @main(){{.*}} !prof [[PROF:![0-9]+]]
; CHECK: CSProfileSummary
; CHECK: [[PROF]] = !{!"function_entry_count", i64 100000}

target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx14.0.0"

define i32 @main() {
entry:
ret i32 0
}
18 changes: 18 additions & 0 deletions llvm/test/tools/llvm-lto/cspgo-use.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; REQUIRES: aarch64-registered-target
;
; RUN: llvm-profdata merge %S/Inputs/cspgo-cs.proftext -o %t.profdata
; RUN: llvm-as %s -o %t.o
; RUN: llvm-lto -cs-profile-path=%t.profdata \
; RUN: -exported-symbol=_main -o %t.lto.o %t.o
; RUN: llvm-nm %t.lto.o | FileCheck %s
;
; CHECK-NOT: __profc_
; CHECK: _main

target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "arm64-apple-macosx14.0.0"

define i32 @main() {
entry:
ret i32 0
}