Skip to content

Commit 174cdec

Browse files
authored
[nfc] Clarify when the various PGO instrumentation passes run (#92330)
The code seems easier to read if it's centered on what the user wants rather than combinations of whatever internal variables.
1 parent 476f7f6 commit 174cdec

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

llvm/include/llvm/Passes/PassBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ class PassBuilder {
723723
bool AtomicCounterUpdate, std::string ProfileFile,
724724
std::string ProfileRemappingFile,
725725
IntrusiveRefCntPtr<vfs::FileSystem> FS);
726+
void addPostPGOLoopRotation(ModulePassManager &MPM, OptimizationLevel Level);
726727

727728
// Extension Point callbacks
728729
SmallVector<std::function<void(FunctionPassManager &, OptimizationLevel)>, 2>

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,20 @@ void PassBuilder::addPreInlinerPasses(ModulePassManager &MPM,
803803
MPM.addPass(GlobalDCEPass());
804804
}
805805

806+
void PassBuilder::addPostPGOLoopRotation(ModulePassManager &MPM,
807+
OptimizationLevel Level) {
808+
if (EnablePostPGOLoopRotation) {
809+
// Disable header duplication in loop rotation at -Oz.
810+
MPM.addPass(createModuleToFunctionPassAdaptor(
811+
createFunctionToLoopPassAdaptor(
812+
LoopRotatePass(EnableLoopHeaderDuplication ||
813+
Level != OptimizationLevel::Oz),
814+
/*UseMemorySSA=*/false,
815+
/*UseBlockFrequencyInfo=*/false),
816+
PTO.EagerlyInvalidateAnalyses));
817+
}
818+
}
819+
806820
void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
807821
OptimizationLevel Level, bool RunProfileGen,
808822
bool IsCS, bool AtomicCounterUpdate,
@@ -824,17 +838,7 @@ void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM,
824838
// Perform PGO instrumentation.
825839
MPM.addPass(PGOInstrumentationGen(IsCS));
826840

827-
if (EnablePostPGOLoopRotation) {
828-
// Disable header duplication in loop rotation at -Oz.
829-
MPM.addPass(createModuleToFunctionPassAdaptor(
830-
createFunctionToLoopPassAdaptor(
831-
LoopRotatePass(EnableLoopHeaderDuplication ||
832-
Level != OptimizationLevel::Oz),
833-
/*UseMemorySSA=*/false,
834-
/*UseBlockFrequencyInfo=*/false),
835-
PTO.EagerlyInvalidateAnalyses));
836-
}
837-
841+
addPostPGOLoopRotation(MPM, Level);
838842
if (PGOCtxProfLoweringPass::isContextualIRPGOEnabled()) {
839843
MPM.addPass(PGOCtxProfLoweringPass());
840844
return;
@@ -1145,29 +1149,34 @@ PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
11451149
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM),
11461150
PTO.EagerlyInvalidateAnalyses));
11471151

1148-
// Invoke the pre-inliner passes for instrumentation PGO or MemProf.
1149-
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
1150-
(PGOOpt->Action == PGOOptions::IRInstr ||
1151-
PGOOpt->Action == PGOOptions::IRUse || !PGOOpt->MemoryProfile.empty()))
1152+
// We already asserted this happens in non-FullLTOPostLink earlier.
1153+
const bool IsPreLink = Phase != ThinOrFullLTOPhase::ThinLTOPostLink;
1154+
const bool IsPGOPreLink = PGOOpt && IsPreLink;
1155+
const bool IsPGOInstrGen =
1156+
IsPGOPreLink && PGOOpt->Action == PGOOptions::IRInstr;
1157+
const bool IsPGOInstrUse =
1158+
IsPGOPreLink && PGOOpt->Action == PGOOptions::IRUse;
1159+
const bool IsMemprofUse = IsPGOPreLink && !PGOOpt->MemoryProfile.empty();
1160+
1161+
if (IsPGOInstrGen || IsPGOInstrUse || IsMemprofUse)
11521162
addPreInlinerPasses(MPM, Level, Phase);
11531163

11541164
// Add all the requested passes for instrumentation PGO, if requested.
1155-
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
1156-
(PGOOpt->Action == PGOOptions::IRInstr ||
1157-
PGOOpt->Action == PGOOptions::IRUse)) {
1165+
if (IsPGOInstrGen || IsPGOInstrUse) {
11581166
addPGOInstrPasses(MPM, Level,
1159-
/*RunProfileGen=*/PGOOpt->Action == PGOOptions::IRInstr,
1167+
/*RunProfileGen=*/IsPGOInstrGen,
11601168
/*IsCS=*/false, PGOOpt->AtomicCounterUpdate,
11611169
PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile,
11621170
PGOOpt->FS);
1163-
MPM.addPass(PGOIndirectCallPromotion(false, false));
11641171
}
1165-
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
1166-
PGOOpt->CSAction == PGOOptions::CSIRInstr)
1172+
1173+
if (IsPGOInstrGen || IsPGOInstrUse)
1174+
MPM.addPass(PGOIndirectCallPromotion(false, false));
1175+
1176+
if (IsPGOPreLink && PGOOpt->CSAction == PGOOptions::CSIRInstr)
11671177
MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile));
11681178

1169-
if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink &&
1170-
!PGOOpt->MemoryProfile.empty())
1179+
if (IsMemprofUse)
11711180
MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS));
11721181

11731182
// Synthesize function entry counts for non-PGO compilation.

0 commit comments

Comments
 (0)