Skip to content

Commit 9b41a3a

Browse files
committed
[SampleFDO] Support running sample loader in O0 mode
1 parent 308c007 commit 9b41a3a

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

llvm/include/llvm/Transforms/IPO/SampleProfile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
4141
SampleProfileLoaderPass(
4242
std::string File = "", std::string RemappingFile = "",
4343
ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None,
44-
IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
44+
IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr,
45+
bool DisableSampleProfileInlining = false);
4546

4647
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
4748

@@ -50,6 +51,7 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
5051
std::string ProfileRemappingFileName;
5152
const ThinOrFullLTOPhase LTOPhase;
5253
IntrusiveRefCntPtr<vfs::FileSystem> FS;
54+
bool DisableSampleProfileInlining;
5355
};
5456

5557
} // end namespace llvm

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,17 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
21532153
if (PGOOpt && PGOOpt->DebugInfoForProfiling)
21542154
MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
21552155

2156+
if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
2157+
// Explicitly disable sample loader inlining in O0 pipeline.
2158+
MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
2159+
PGOOpt->ProfileRemappingFile,
2160+
ThinOrFullLTOPhase::None, nullptr,
2161+
/*DisableSampleProfileInlining*/ true));
2162+
// Cache ProfileSummaryAnalysis once to avoid the potential need to insert
2163+
// RequireAnalysisPass for PSI before subsequent non-module passes.
2164+
MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
2165+
}
2166+
21562167
invokePipelineEarlySimplificationEPCallbacks(MPM, Level);
21572168

21582169
// Build a minimal pipeline based on the semantics required by LLVM,

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,9 +2305,10 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
23052305
}
23062306
SampleProfileLoaderPass::SampleProfileLoaderPass(
23072307
std::string File, std::string RemappingFile, ThinOrFullLTOPhase LTOPhase,
2308-
IntrusiveRefCntPtr<vfs::FileSystem> FS)
2308+
IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining)
23092309
: ProfileFileName(File), ProfileRemappingFileName(RemappingFile),
2310-
LTOPhase(LTOPhase), FS(std::move(FS)) {}
2310+
LTOPhase(LTOPhase), FS(std::move(FS)),
2311+
DisableSampleProfileInlining(DisableSampleProfileInlining) {}
23112312

23122313
PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23132314
ModuleAnalysisManager &AM) {
@@ -2326,6 +2327,9 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23262327

23272328
if (!FS)
23282329
FS = vfs::getRealFileSystem();
2330+
if (!DisableSampleLoaderInlining.getNumOccurrences() &&
2331+
DisableSampleProfileInlining)
2332+
DisableSampleLoaderInlining = true;
23292333
LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M);
23302334

23312335
SampleProfileLoader SampleLoader(

llvm/test/Other/new-pm-pgo-O0.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
; RUN: |FileCheck %s --check-prefixes=USE_POST_LINK,USE
1010
; RUN: opt -debug-pass-manager -passes='lto<O0>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' %s 2>&1 \
1111
; RUN: |FileCheck %s --check-prefixes=USE_POST_LINK,USE
12+
; RUN: opt -debug-pass-manager -passes='default<O0>' -pgo-kind=pgo-sample-use-pipeline -profile-file='%S/Inputs/new-pm-pgo.prof' %s 2>&1 \
13+
; RUN: |FileCheck %s --check-prefixes=SAMPLE_USE
1214

13-
;
1415
; GEN: Running pass: PGOInstrumentationGen
1516
; USE_DEFAULT: Running pass: PGOInstrumentationUse
1617
; USE_PRE_LINK: Running pass: PGOInstrumentationUse
1718
; USE_POST_LINK-NOT: Running pass: PGOInstrumentationUse
1819
; USE-NOT: Running pass: PGOIndirectCallPromotion
1920
; USE-NOT: Running pass: PGOMemOPSizeOpt
2021

22+
; SAMPLE_USE: Running pass: AddDiscriminatorsPass
23+
; SAMPLE_USE: Running pass: SampleProfileLoaderPass
24+
2125
define void @foo() {
2226
ret void
2327
}

0 commit comments

Comments
 (0)