Skip to content

Commit bc1aa28

Browse files
authored
[SampleFDO] Support enabling sample loader pass in O0 mode (#113985)
Add support for enabling sample loader pass in O0 mode(under `-fsample-profile-use`). This can help verify PGO raw profile count quality or provide a more accurate performance proxy(predictor), as O0 mode has minimal or no compiler optimizations that might otherwise impact profile count accuracy. - Explicitly disable the sample loader inlining to ensure it only emits sampling annotation. - Use flattened profile for O0 mode. - Add the pass after `AddDiscriminatorsPass` pass to work with `-fdebug-info-for-profiling`.
1 parent 62a7bb0 commit bc1aa28

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ 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,
46+
bool UseFlattenedProfile = false);
4547

4648
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
4749

@@ -50,6 +52,8 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
5052
std::string ProfileRemappingFileName;
5153
const ThinOrFullLTOPhase LTOPhase;
5254
IntrusiveRefCntPtr<vfs::FileSystem> FS;
55+
bool DisableSampleProfileInlining;
56+
bool UseFlattenedProfile;
5357
};
5458

5559
} // end namespace llvm

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,19 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
21622162
if (PGOOpt && PGOOpt->DebugInfoForProfiling)
21632163
MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
21642164

2165+
if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
2166+
// Explicitly disable sample loader inlining and use flattened profile in O0
2167+
// pipeline.
2168+
MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
2169+
PGOOpt->ProfileRemappingFile,
2170+
ThinOrFullLTOPhase::None, nullptr,
2171+
/*DisableSampleProfileInlining=*/true,
2172+
/*UseFlattenedProfile=*/true));
2173+
// Cache ProfileSummaryAnalysis once to avoid the potential need to insert
2174+
// RequireAnalysisPass for PSI before subsequent non-module passes.
2175+
MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
2176+
}
2177+
21652178
invokePipelineEarlySimplificationEPCallbacks(MPM, Level, Phase);
21662179

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

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
469469
std::function<AssumptionCache &(Function &)> GetAssumptionCache,
470470
std::function<TargetTransformInfo &(Function &)> GetTargetTransformInfo,
471471
std::function<const TargetLibraryInfo &(Function &)> GetTLI,
472-
LazyCallGraph &CG)
472+
LazyCallGraph &CG, bool DisableSampleProfileInlining,
473+
bool UseFlattenedProfile)
473474
: SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName),
474475
std::move(FS)),
475476
GetAC(std::move(GetAssumptionCache)),
@@ -478,7 +479,9 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
478479
AnnotatedPassName(AnnotateSampleProfileInlinePhase
479480
? llvm::AnnotateInlinePassName(InlineContext{
480481
LTOPhase, InlinePass::SampleProfileInliner})
481-
: CSINLINE_DEBUG) {}
482+
: CSINLINE_DEBUG),
483+
DisableSampleProfileInlining(DisableSampleProfileInlining),
484+
UseFlattenedProfile(UseFlattenedProfile) {}
482485

483486
bool doInitialization(Module &M, FunctionAnalysisManager *FAM = nullptr);
484487
bool runOnModule(Module &M, ModuleAnalysisManager *AM,
@@ -592,6 +595,10 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
592595
// attribute.
593596
bool ProfAccForSymsInList;
594597

598+
bool DisableSampleProfileInlining;
599+
600+
bool UseFlattenedProfile;
601+
595602
// External inline advisor used to replay inline decision from remarks.
596603
std::unique_ptr<InlineAdvisor> ExternalInlineAdvisor;
597604

@@ -919,7 +926,7 @@ bool SampleProfileLoader::tryPromoteAndInlineCandidate(
919926
Function &F, InlineCandidate &Candidate, uint64_t SumOrigin, uint64_t &Sum,
920927
SmallVector<CallBase *, 8> *InlinedCallSite) {
921928
// Bail out early if sample-loader inliner is disabled.
922-
if (DisableSampleLoaderInlining)
929+
if (DisableSampleProfileInlining)
923930
return false;
924931

925932
// Bail out early if MaxNumPromotions is zero.
@@ -1230,7 +1237,7 @@ bool SampleProfileLoader::tryInlineCandidate(
12301237
InlineCandidate &Candidate, SmallVector<CallBase *, 8> *InlinedCallSites) {
12311238
// Do not attempt to inline a candidate if
12321239
// --disable-sample-loader-inlining is true.
1233-
if (DisableSampleLoaderInlining)
1240+
if (DisableSampleProfileInlining)
12341241
return false;
12351242

12361243
CallBase &CB = *Candidate.CallInstr;
@@ -1974,6 +1981,13 @@ bool SampleProfileLoader::doInitialization(Module &M,
19741981

19751982
PSL = Reader->getProfileSymbolList();
19761983

1984+
if (DisableSampleLoaderInlining.getNumOccurrences())
1985+
DisableSampleProfileInlining = DisableSampleLoaderInlining;
1986+
1987+
if (UseFlattenedProfile)
1988+
ProfileConverter::flattenProfile(Reader->getProfiles(),
1989+
Reader->profileIsCS());
1990+
19771991
// While profile-sample-accurate is on, ignore symbol list.
19781992
ProfAccForSymsInList =
19791993
ProfileAccurateForSymsInList && PSL && !ProfileSampleAccurate;
@@ -2304,9 +2318,12 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
23042318
}
23052319
SampleProfileLoaderPass::SampleProfileLoaderPass(
23062320
std::string File, std::string RemappingFile, ThinOrFullLTOPhase LTOPhase,
2307-
IntrusiveRefCntPtr<vfs::FileSystem> FS)
2321+
IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining,
2322+
bool UseFlattenedProfile)
23082323
: ProfileFileName(File), ProfileRemappingFileName(RemappingFile),
2309-
LTOPhase(LTOPhase), FS(std::move(FS)) {}
2324+
LTOPhase(LTOPhase), FS(std::move(FS)),
2325+
DisableSampleProfileInlining(DisableSampleProfileInlining),
2326+
UseFlattenedProfile(UseFlattenedProfile) {}
23102327

23112328
PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23122329
ModuleAnalysisManager &AM) {
@@ -2331,7 +2348,8 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23312348
ProfileFileName.empty() ? SampleProfileFile : ProfileFileName,
23322349
ProfileRemappingFileName.empty() ? SampleProfileRemappingFile
23332350
: ProfileRemappingFileName,
2334-
LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG);
2351+
LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG,
2352+
DisableSampleProfileInlining, UseFlattenedProfile);
23352353
if (!SampleLoader.doInitialization(M, &FAM))
23362354
return PreservedAnalyses::all();
23372355

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)