Skip to content

Commit 611ce24

Browse files
author
Qiongsi Wu
committed
[PGO] Enable -fprofile-update for -fprofile-generate
Currently, the `-fprofile-udpate` is ignored when `-fprofile-generate` is in effect. This patch enables `-fprofile-update` for `-fprofile-generate`. This patch continues the work from https://reviews.llvm.org/D87737, which added `-fprofile-update` in the first place. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D157280
1 parent b7fcf51 commit 611ce24

File tree

7 files changed

+41
-30
lines changed

7 files changed

+41
-30
lines changed

clang/docs/UsersManual.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,9 +2759,6 @@ programs using the same instrumentation method as ``-fprofile-generate``.
27592759
overhead. ``prefer-atomic`` will be transformed to ``atomic`` when supported
27602760
by the target, or ``single`` otherwise.
27612761

2762-
This option currently works with ``-fprofile-arcs`` and ``-fprofile-instr-generate``,
2763-
but not with ``-fprofile-generate``.
2764-
27652762
Disabling Instrumentation
27662763
^^^^^^^^^^^^^^^^^^^^^^^^^
27672764

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
768768
CodeGenOpts.InstrProfileOutput.empty() ? getDefaultProfileGenName()
769769
: CodeGenOpts.InstrProfileOutput,
770770
"", "", CodeGenOpts.MemoryProfileUsePath, nullptr, PGOOptions::IRInstr,
771-
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
771+
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling,
772+
/*PseudoProbeForProfiling=*/false, CodeGenOpts.AtomicProfileUpdate);
772773
else if (CodeGenOpts.hasProfileIRUse()) {
773774
// -fprofile-use.
774775
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse

clang/test/CodeGen/tsan-instrprof-atomic.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %clang_cc1 %s -emit-llvm -fprofile-instrument=clang -fprofile-update=atomic -o - | FileCheck %s
2+
// RUN: %clang %s -S -emit-llvm -fprofile-generate -fprofile-update=atomic -o - | FileCheck %s
3+
// RUN: %clang -O3 %s -S -emit-llvm -fprofile-generate -fprofile-update=atomic -o - | FileCheck %s
24

35
// CHECK: define {{.*}}@foo
46
// CHECK-NOT: load {{.*}}foo

llvm/include/llvm/Passes/PassBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ class PassBuilder {
560560

561561
/// Add PGOInstrumenation passes for O0 only.
562562
void addPGOInstrPassesForO0(ModulePassManager &MPM, bool RunProfileGen,
563-
bool IsCS, std::string ProfileFile,
563+
bool IsCS, bool AtomicCounterUpdate,
564+
std::string ProfileFile,
564565
std::string ProfileRemappingFile,
565566
IntrusiveRefCntPtr<vfs::FileSystem> FS);
566567

@@ -628,7 +629,8 @@ class PassBuilder {
628629
ArrayRef<PipelineElement> Pipeline);
629630

630631
void addPGOInstrPasses(ModulePassManager &MPM, OptimizationLevel Level,
631-
bool RunProfileGen, bool IsCS, std::string ProfileFile,
632+
bool RunProfileGen, bool IsCS,
633+
bool AtomicCounterUpdate, std::string ProfileFile,
632634
std::string ProfileRemappingFile,
633635
ThinOrFullLTOPhase LTOPhase,
634636
IntrusiveRefCntPtr<vfs::FileSystem> FS);

llvm/include/llvm/Support/PGOOptions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ struct PGOOptions {
3232
IntrusiveRefCntPtr<vfs::FileSystem> FS,
3333
PGOAction Action = NoAction, CSPGOAction CSAction = NoCSAction,
3434
bool DebugInfoForProfiling = false,
35-
bool PseudoProbeForProfiling = false);
35+
bool PseudoProbeForProfiling = false,
36+
bool AtomicCounterUpdate = false);
3637
PGOOptions(const PGOOptions &);
3738
~PGOOptions();
3839
PGOOptions &operator=(const PGOOptions &);
@@ -45,6 +46,7 @@ struct PGOOptions {
4546
CSPGOAction CSAction;
4647
bool DebugInfoForProfiling;
4748
bool PseudoProbeForProfiling;
49+
bool AtomicCounterUpdate;
4850
IntrusiveRefCntPtr<vfs::FileSystem> FS;
4951
};
5052
} // namespace llvm

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,8 @@ void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) {
724724

725725
void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
726726
OptimizationLevel Level, bool RunProfileGen,
727-
bool IsCS, std::string ProfileFile,
727+
bool IsCS, bool AtomicCounterUpdate,
728+
std::string ProfileFile,
728729
std::string ProfileRemappingFile,
729730
ThinOrFullLTOPhase LTOPhase,
730731
IntrusiveRefCntPtr<vfs::FileSystem> FS) {
@@ -793,13 +794,14 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
793794
// Do counter promotion at Level greater than O0.
794795
Options.DoCounterPromotion = true;
795796
Options.UseBFIInPromotion = IsCS;
797+
Options.Atomic = AtomicCounterUpdate;
796798
MPM.addPass(InstrProfiling(Options, IsCS));
797799
}
798800

799801
void PassBuilder::addPGOInstrPassesForO0(
800802
ModulePassManager &MPM, bool RunProfileGen, bool IsCS,
801-
std::string ProfileFile, std::string ProfileRemappingFile,
802-
IntrusiveRefCntPtr<vfs::FileSystem> FS) {
803+
bool AtomicCounterUpdate, std::string ProfileFile,
804+
std::string ProfileRemappingFile, IntrusiveRefCntPtr<vfs::FileSystem> FS) {
803805
if (!RunProfileGen) {
804806
assert(!ProfileFile.empty() && "Profile use expecting a profile file!");
805807
MPM.addPass(
@@ -819,6 +821,7 @@ void PassBuilder::addPGOInstrPassesForO0(
819821
// Do not do counter promotion at O0.
820822
Options.DoCounterPromotion = false;
821823
Options.UseBFIInPromotion = IsCS;
824+
Options.Atomic = AtomicCounterUpdate;
822825
MPM.addPass(InstrProfiling(Options, IsCS));
823826
}
824827

@@ -1093,9 +1096,10 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
10931096
(PGOOpt->Action == PGOOptions::IRInstr ||
10941097
PGOOpt->Action == PGOOptions::IRUse)) {
10951098
addPGOInstrPasses(MPM, Level,
1096-
/* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr,
1097-
/* IsCS */ false, PGOOpt->ProfileFile,
1098-
PGOOpt->ProfileRemappingFile, Phase, PGOOpt->FS);
1099+
/*RunProfileGen=*/PGOOpt->Action == PGOOptions::IRInstr,
1100+
/*IsCS=*/false, PGOOpt->AtomicCounterUpdate,
1101+
PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile, Phase,
1102+
PGOOpt->FS);
10991103
MPM.addPass(PGOIndirectCallPromotion(false, false));
11001104
}
11011105
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
@@ -1306,13 +1310,15 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
13061310
// instrumentation is after all the inlines are done.
13071311
if (!LTOPreLink && PGOOpt) {
13081312
if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
1309-
addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true,
1310-
/* IsCS */ true, PGOOpt->CSProfileGenFile,
1311-
PGOOpt->ProfileRemappingFile, LTOPhase, PGOOpt->FS);
1313+
addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/true,
1314+
/*IsCS=*/true, PGOOpt->AtomicCounterUpdate,
1315+
PGOOpt->CSProfileGenFile, PGOOpt->ProfileRemappingFile,
1316+
LTOPhase, PGOOpt->FS);
13121317
else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
1313-
addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false,
1314-
/* IsCS */ true, PGOOpt->ProfileFile,
1315-
PGOOpt->ProfileRemappingFile, LTOPhase, PGOOpt->FS);
1318+
addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/false,
1319+
/*IsCS=*/true, PGOOpt->AtomicCounterUpdate,
1320+
PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile,
1321+
LTOPhase, PGOOpt->FS);
13161322
}
13171323

13181324
// Re-compute GlobalsAA here prior to function passes. This is particularly
@@ -1787,14 +1793,14 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
17871793
// sensitive PGO pass.
17881794
if (PGOOpt) {
17891795
if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
1790-
addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true,
1791-
/* IsCS */ true, PGOOpt->CSProfileGenFile,
1792-
PGOOpt->ProfileRemappingFile,
1796+
addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/true,
1797+
/*IsCS=*/true, PGOOpt->AtomicCounterUpdate,
1798+
PGOOpt->CSProfileGenFile, PGOOpt->ProfileRemappingFile,
17931799
ThinOrFullLTOPhase::FullLTOPostLink, PGOOpt->FS);
17941800
else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
1795-
addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false,
1796-
/* IsCS */ true, PGOOpt->ProfileFile,
1797-
PGOOpt->ProfileRemappingFile,
1801+
addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/false,
1802+
/*IsCS=*/true, PGOOpt->AtomicCounterUpdate,
1803+
PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile,
17981804
ThinOrFullLTOPhase::FullLTOPostLink, PGOOpt->FS);
17991805
}
18001806

@@ -1939,9 +1945,9 @@ ModulePassManager PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
19391945
PGOOpt->Action == PGOOptions::IRUse))
19401946
addPGOInstrPassesForO0(
19411947
MPM,
1942-
/* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
1943-
/* IsCS */ false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile,
1944-
PGOOpt->FS);
1948+
/*RunProfileGen=*/(PGOOpt->Action == PGOOptions::IRInstr),
1949+
/*IsCS=*/false, PGOOpt->AtomicCounterUpdate, PGOOpt->ProfileFile,
1950+
PGOOpt->ProfileRemappingFile, PGOOpt->FS);
19451951

19461952
invokePipelineStartEPCallbacks(MPM, Level);
19471953

llvm/lib/Support/PGOOptions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ PGOOptions::PGOOptions(std::string ProfileFile, std::string CSProfileGenFile,
1616
std::string MemoryProfile,
1717
IntrusiveRefCntPtr<vfs::FileSystem> FS, PGOAction Action,
1818
CSPGOAction CSAction, bool DebugInfoForProfiling,
19-
bool PseudoProbeForProfiling)
19+
bool PseudoProbeForProfiling, bool AtomicCounterUpdate)
2020
: ProfileFile(ProfileFile), CSProfileGenFile(CSProfileGenFile),
2121
ProfileRemappingFile(ProfileRemappingFile), MemoryProfile(MemoryProfile),
2222
Action(Action), CSAction(CSAction),
2323
DebugInfoForProfiling(DebugInfoForProfiling ||
2424
(Action == SampleUse && !PseudoProbeForProfiling)),
25-
PseudoProbeForProfiling(PseudoProbeForProfiling), FS(std::move(FS)) {
25+
PseudoProbeForProfiling(PseudoProbeForProfiling),
26+
AtomicCounterUpdate(AtomicCounterUpdate), FS(std::move(FS)) {
2627
// Note, we do allow ProfileFile.empty() for Action=IRUse LTO can
2728
// callback with IRUse action without ProfileFile.
2829

0 commit comments

Comments
 (0)