Skip to content

Commit efb68d2

Browse files
committed
only use flattened profile for O0 mode
1 parent cde256b commit efb68d2

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

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

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

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

@@ -52,6 +53,7 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
5253
const ThinOrFullLTOPhase LTOPhase;
5354
IntrusiveRefCntPtr<vfs::FileSystem> FS;
5455
bool DisableSampleProfileInlining;
56+
bool UseFlattenedProfile;
5557
};
5658

5759
} // end namespace llvm

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,11 +2154,13 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
21542154
MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
21552155

21562156
if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
2157-
// Explicitly disable sample loader inlining in O0 pipeline.
2157+
// Explicitly disable sample loader inlining and use flattened profile in O0
2158+
// pipeline.
21582159
MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
21592160
PGOOpt->ProfileRemappingFile,
21602161
ThinOrFullLTOPhase::None, nullptr,
2161-
/*DisableSampleProfileInlining=*/true));
2162+
/*DisableSampleProfileInlining=*/true,
2163+
/*UseFlattenedProfile=*/true));
21622164
// Cache ProfileSummaryAnalysis once to avoid the potential need to insert
21632165
// RequireAnalysisPass for PSI before subsequent non-module passes.
21642166
MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ static cl::opt<bool> ProfileSizeInline(
195195
static cl::opt<bool> DisableSampleLoaderInlining(
196196
"disable-sample-loader-inlining", cl::Hidden, cl::init(false),
197197
cl::desc("If true, artifically skip inline transformation in sample-loader "
198-
"pass, and use flattened profiles to emit annotation."));
198+
"pass, and merge (or scale) profiles (as configured by "
199+
"--sample-profile-merge-inlinee)."));
199200

200201
namespace llvm {
201202
cl::opt<bool>
@@ -469,7 +470,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
469470
std::function<AssumptionCache &(Function &)> GetAssumptionCache,
470471
std::function<TargetTransformInfo &(Function &)> GetTargetTransformInfo,
471472
std::function<const TargetLibraryInfo &(Function &)> GetTLI,
472-
LazyCallGraph &CG, bool DisableSampleProfileInlining)
473+
LazyCallGraph &CG, bool DisableSampleProfileInlining,
474+
bool UseFlattenedProfile)
473475
: SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName),
474476
std::move(FS)),
475477
GetAC(std::move(GetAssumptionCache)),
@@ -479,7 +481,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
479481
? llvm::AnnotateInlinePassName(InlineContext{
480482
LTOPhase, InlinePass::SampleProfileInliner})
481483
: CSINLINE_DEBUG),
482-
DisableSampleProfileInlining(DisableSampleProfileInlining) {}
484+
DisableSampleProfileInlining(DisableSampleProfileInlining),
485+
UseFlattenedProfile(UseFlattenedProfile) {}
483486

484487
bool doInitialization(Module &M, FunctionAnalysisManager *FAM = nullptr);
485488
bool runOnModule(Module &M, ModuleAnalysisManager *AM,
@@ -595,6 +598,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
595598

596599
bool DisableSampleProfileInlining;
597600

601+
bool UseFlattenedProfile;
602+
598603
// External inline advisor used to replay inline decision from remarks.
599604
std::unique_ptr<InlineAdvisor> ExternalInlineAdvisor;
600605

@@ -1980,9 +1985,9 @@ bool SampleProfileLoader::doInitialization(Module &M,
19801985
if (DisableSampleLoaderInlining.getNumOccurrences())
19811986
DisableSampleProfileInlining = DisableSampleLoaderInlining;
19821987

1983-
// Use flattened profile if inlining is disabled.
1984-
if (DisableSampleProfileInlining && !Reader->profileIsCS())
1985-
ProfileConverter::flattenProfile(Reader->getProfiles());
1988+
if (UseFlattenedProfile)
1989+
ProfileConverter::flattenProfile(Reader->getProfiles(),
1990+
Reader->profileIsCS());
19861991

19871992
// While profile-sample-accurate is on, ignore symbol list.
19881993
ProfAccForSymsInList =
@@ -2314,10 +2319,12 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
23142319
}
23152320
SampleProfileLoaderPass::SampleProfileLoaderPass(
23162321
std::string File, std::string RemappingFile, ThinOrFullLTOPhase LTOPhase,
2317-
IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining)
2322+
IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining,
2323+
bool UseFlattenedProfile)
23182324
: ProfileFileName(File), ProfileRemappingFileName(RemappingFile),
23192325
LTOPhase(LTOPhase), FS(std::move(FS)),
2320-
DisableSampleProfileInlining(DisableSampleProfileInlining) {}
2326+
DisableSampleProfileInlining(DisableSampleProfileInlining),
2327+
UseFlattenedProfile(UseFlattenedProfile) {}
23212328

23222329
PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23232330
ModuleAnalysisManager &AM) {
@@ -2343,7 +2350,7 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
23432350
ProfileRemappingFileName.empty() ? SampleProfileRemappingFile
23442351
: ProfileRemappingFileName,
23452352
LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG,
2346-
DisableSampleProfileInlining);
2353+
DisableSampleProfileInlining, UseFlattenedProfile);
23472354
if (!SampleLoader.doInitialization(M, &FAM))
23482355
return PreservedAnalyses::all();
23492356

llvm/test/Transforms/SampleProfile/inline-mergeprof.ll

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
; when the profile uses md5.
1010
; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline-mergeprof.md5.prof -sample-profile-merge-inlinee=true -use-profiled-call-graph=0 -S | FileCheck -check-prefix=MERGE %s
1111

12-
; Test we properly use flattened profile when using '--disable-sample-loader-inlining'.
13-
14-
; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline-mergeprof.prof -sample-profile-merge-inlinee=true --disable-sample-loader-inlining -use-profiled-call-graph=0 -S | FileCheck -check-prefix=FLATTEN %s
12+
; Test we properly merge not inlined profile with '--sample-profile-merge-inlinee' even if '--disable-sample-loader-inlining' is true
13+
; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/inline-mergeprof.md5.prof -sample-profile-merge-inlinee=true --disable-sample-loader-inlining -use-profiled-call-graph=0 -S | FileCheck -check-prefix=MERGE %s
1514

1615
@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
1716

@@ -104,10 +103,3 @@ declare i32 @printf(ptr, ...)
104103
; MERGE: !{!"branch_weights", i32 10}
105104
; MERGE: name: "sub"
106105
; MERGE-NEXT: {!"function_entry_count", i64 3}
107-
108-
; FLATTEN: name: "sum"
109-
; FLATTEN-NEXT: {!"function_entry_count", i64 35}
110-
; FLATTEN: !{!"branch_weights", i32 13, i32 23}
111-
; FLATTEN: !{!"branch_weights", i32 12}
112-
; FLATTEN: name: "sub"
113-
; FLATTEN-NEXT: {!"function_entry_count", i64 3}

0 commit comments

Comments
 (0)