[llvm] [SampleFDO] Support enabling sample loader pass in O0 mode (PR #113985)

Lei Wang via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 5 21:20:00 PST 2024


https://github.com/wlei-llvm updated https://github.com/llvm/llvm-project/pull/113985

>From 9b41a3a3321c4ce0f4a7ca9f6548c601d6ecd949 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Mon, 28 Oct 2024 16:34:17 -0700
Subject: [PATCH 1/4] [SampleFDO] Support running sample loader in O0 mode

---
 llvm/include/llvm/Transforms/IPO/SampleProfile.h |  4 +++-
 llvm/lib/Passes/PassBuilderPipelines.cpp         | 11 +++++++++++
 llvm/lib/Transforms/IPO/SampleProfile.cpp        |  8 ++++++--
 llvm/test/Other/new-pm-pgo-O0.ll                 |  6 +++++-
 4 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/SampleProfile.h b/llvm/include/llvm/Transforms/IPO/SampleProfile.h
index e94f6ba55cd0dd..5adda3a0a16f3a 100644
--- a/llvm/include/llvm/Transforms/IPO/SampleProfile.h
+++ b/llvm/include/llvm/Transforms/IPO/SampleProfile.h
@@ -41,7 +41,8 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
   SampleProfileLoaderPass(
       std::string File = "", std::string RemappingFile = "",
       ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None,
-      IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr);
+      IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr,
+      bool DisableSampleProfileInlining = false);
 
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 
@@ -50,6 +51,7 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
   std::string ProfileRemappingFileName;
   const ThinOrFullLTOPhase LTOPhase;
   IntrusiveRefCntPtr<vfs::FileSystem> FS;
+  bool DisableSampleProfileInlining;
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 466fbcd7bb7703..8b8a6142d7205f 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -2153,6 +2153,17 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
   if (PGOOpt && PGOOpt->DebugInfoForProfiling)
     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
 
+  if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
+    // Explicitly disable sample loader inlining in O0 pipeline.
+    MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
+                                        PGOOpt->ProfileRemappingFile,
+                                        ThinOrFullLTOPhase::None, nullptr,
+                                        /*DisableSampleProfileInlining*/ true));
+    // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
+    // RequireAnalysisPass for PSI before subsequent non-module passes.
+    MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
+  }
+
   invokePipelineEarlySimplificationEPCallbacks(MPM, Level);
 
   // Build a minimal pipeline based on the semantics required by LLVM,
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index 5d1ec7248ae679..9cd025efb0a5d8 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -2305,9 +2305,10 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
 }
 SampleProfileLoaderPass::SampleProfileLoaderPass(
     std::string File, std::string RemappingFile, ThinOrFullLTOPhase LTOPhase,
-    IntrusiveRefCntPtr<vfs::FileSystem> FS)
+    IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining)
     : ProfileFileName(File), ProfileRemappingFileName(RemappingFile),
-      LTOPhase(LTOPhase), FS(std::move(FS)) {}
+      LTOPhase(LTOPhase), FS(std::move(FS)),
+      DisableSampleProfileInlining(DisableSampleProfileInlining) {}
 
 PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
                                                ModuleAnalysisManager &AM) {
@@ -2326,6 +2327,9 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
 
   if (!FS)
     FS = vfs::getRealFileSystem();
+  if (!DisableSampleLoaderInlining.getNumOccurrences() &&
+      DisableSampleProfileInlining)
+    DisableSampleLoaderInlining = true;
   LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M);
 
   SampleProfileLoader SampleLoader(
diff --git a/llvm/test/Other/new-pm-pgo-O0.ll b/llvm/test/Other/new-pm-pgo-O0.ll
index d7a6a03b8e44e3..d4f662fb25ace7 100644
--- a/llvm/test/Other/new-pm-pgo-O0.ll
+++ b/llvm/test/Other/new-pm-pgo-O0.ll
@@ -9,8 +9,9 @@
 ; RUN:     |FileCheck %s --check-prefixes=USE_POST_LINK,USE
 ; RUN: opt -debug-pass-manager -passes='lto<O0>' -pgo-kind=pgo-instr-use-pipeline -profile-file='%t.profdata' %s 2>&1 \
 ; RUN:     |FileCheck %s --check-prefixes=USE_POST_LINK,USE
+; 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 \
+; RUN:     |FileCheck %s --check-prefixes=SAMPLE_USE
 
-;
 ; GEN: Running pass: PGOInstrumentationGen
 ; USE_DEFAULT: Running pass: PGOInstrumentationUse
 ; USE_PRE_LINK: Running pass: PGOInstrumentationUse
@@ -18,6 +19,9 @@
 ; USE-NOT: Running pass: PGOIndirectCallPromotion
 ; USE-NOT: Running pass: PGOMemOPSizeOpt
 
+; SAMPLE_USE: Running pass: AddDiscriminatorsPass
+; SAMPLE_USE: Running pass: SampleProfileLoaderPass
+
 define void @foo() {
   ret void
 }

>From 2d4a25329c9822c7e1e092ae7d34b8e1b03b1c74 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Fri, 1 Nov 2024 15:24:27 -0700
Subject: [PATCH 2/4] addressing comments, avoid seting global cmd var

---
 llvm/lib/Passes/PassBuilderPipelines.cpp  |  2 +-
 llvm/lib/Transforms/IPO/SampleProfile.cpp | 20 ++++++++++++--------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 8b8a6142d7205f..39bba82fb5c38e 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -2158,7 +2158,7 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
     MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
                                         PGOOpt->ProfileRemappingFile,
                                         ThinOrFullLTOPhase::None, nullptr,
-                                        /*DisableSampleProfileInlining*/ true));
+                                        /*DisableSampleProfileInlining=*/true));
     // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
     // RequireAnalysisPass for PSI before subsequent non-module passes.
     MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index 9cd025efb0a5d8..ad70e0f957bd8b 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -470,7 +470,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
       std::function<AssumptionCache &(Function &)> GetAssumptionCache,
       std::function<TargetTransformInfo &(Function &)> GetTargetTransformInfo,
       std::function<const TargetLibraryInfo &(Function &)> GetTLI,
-      LazyCallGraph &CG)
+      LazyCallGraph &CG, bool DisableSampleProfileInlining)
       : SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName),
                                     std::move(FS)),
         GetAC(std::move(GetAssumptionCache)),
@@ -479,7 +479,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
         AnnotatedPassName(AnnotateSampleProfileInlinePhase
                               ? llvm::AnnotateInlinePassName(InlineContext{
                                     LTOPhase, InlinePass::SampleProfileInliner})
-                              : CSINLINE_DEBUG) {}
+                              : CSINLINE_DEBUG),
+        DisableSampleProfileInlining(DisableSampleProfileInlining) {}
 
   bool doInitialization(Module &M, FunctionAnalysisManager *FAM = nullptr);
   bool runOnModule(Module &M, ModuleAnalysisManager *AM,
@@ -593,6 +594,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
   // attribute.
   bool ProfAccForSymsInList;
 
+  bool DisableSampleProfileInlining;
+
   // External inline advisor used to replay inline decision from remarks.
   std::unique_ptr<InlineAdvisor> ExternalInlineAdvisor;
 
@@ -920,7 +923,7 @@ bool SampleProfileLoader::tryPromoteAndInlineCandidate(
     Function &F, InlineCandidate &Candidate, uint64_t SumOrigin, uint64_t &Sum,
     SmallVector<CallBase *, 8> *InlinedCallSite) {
   // Bail out early if sample-loader inliner is disabled.
-  if (DisableSampleLoaderInlining)
+  if (DisableSampleProfileInlining)
     return false;
 
   // Bail out early if MaxNumPromotions is zero.
@@ -1231,7 +1234,7 @@ bool SampleProfileLoader::tryInlineCandidate(
     InlineCandidate &Candidate, SmallVector<CallBase *, 8> *InlinedCallSites) {
   // Do not attempt to inline a candidate if
   // --disable-sample-loader-inlining is true.
-  if (DisableSampleLoaderInlining)
+  if (DisableSampleProfileInlining)
     return false;
 
   CallBase &CB = *Candidate.CallInstr;
@@ -2003,6 +2006,9 @@ bool SampleProfileLoader::doInitialization(Module &M,
         /*EmitRemarks=*/false, InlineContext{LTOPhase, InlinePass::ReplaySampleProfileInliner});
   }
 
+  if (DisableSampleLoaderInlining.getNumOccurrences())
+    DisableSampleProfileInlining = DisableSampleLoaderInlining;
+
   // Apply tweaks if context-sensitive or probe-based profile is available.
   if (Reader->profileIsCS() || Reader->profileIsPreInlined() ||
       Reader->profileIsProbeBased()) {
@@ -2327,16 +2333,14 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
 
   if (!FS)
     FS = vfs::getRealFileSystem();
-  if (!DisableSampleLoaderInlining.getNumOccurrences() &&
-      DisableSampleProfileInlining)
-    DisableSampleLoaderInlining = true;
   LazyCallGraph &CG = AM.getResult<LazyCallGraphAnalysis>(M);
 
   SampleProfileLoader SampleLoader(
       ProfileFileName.empty() ? SampleProfileFile : ProfileFileName,
       ProfileRemappingFileName.empty() ? SampleProfileRemappingFile
                                        : ProfileRemappingFileName,
-      LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG);
+      LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG,
+      DisableSampleProfileInlining);
   if (!SampleLoader.doInitialization(M, &FAM))
     return PreservedAnalyses::all();
 

>From cde256b0a4ce2685332835b150fae483f872ef13 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Fri, 1 Nov 2024 16:45:58 -0700
Subject: [PATCH 3/4] Use flattened profile when sample loading inling is
 disablede

---
 llvm/lib/Transforms/IPO/SampleProfile.cpp           | 13 ++++++++-----
 .../Transforms/SampleProfile/inline-mergeprof.ll    | 12 ++++++++++--
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index ad70e0f957bd8b..96065d40970501 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -195,8 +195,7 @@ static cl::opt<bool> ProfileSizeInline(
 static cl::opt<bool> DisableSampleLoaderInlining(
     "disable-sample-loader-inlining", cl::Hidden, cl::init(false),
     cl::desc("If true, artifically skip inline transformation in sample-loader "
-             "pass, and merge (or scale) profiles (as configured by "
-             "--sample-profile-merge-inlinee)."));
+             "pass, and use flattened profiles to emit annotation."));
 
 namespace llvm {
 cl::opt<bool>
@@ -1978,6 +1977,13 @@ bool SampleProfileLoader::doInitialization(Module &M,
 
   PSL = Reader->getProfileSymbolList();
 
+  if (DisableSampleLoaderInlining.getNumOccurrences())
+    DisableSampleProfileInlining = DisableSampleLoaderInlining;
+
+  // Use flattened profile if inlining is disabled.
+  if (DisableSampleProfileInlining && !Reader->profileIsCS())
+    ProfileConverter::flattenProfile(Reader->getProfiles());
+
   // While profile-sample-accurate is on, ignore symbol list.
   ProfAccForSymsInList =
       ProfileAccurateForSymsInList && PSL && !ProfileSampleAccurate;
@@ -2006,9 +2012,6 @@ bool SampleProfileLoader::doInitialization(Module &M,
         /*EmitRemarks=*/false, InlineContext{LTOPhase, InlinePass::ReplaySampleProfileInliner});
   }
 
-  if (DisableSampleLoaderInlining.getNumOccurrences())
-    DisableSampleProfileInlining = DisableSampleLoaderInlining;
-
   // Apply tweaks if context-sensitive or probe-based profile is available.
   if (Reader->profileIsCS() || Reader->profileIsPreInlined() ||
       Reader->profileIsProbeBased()) {
diff --git a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
index 3094b25236020b..d3aebe0bb97f0d 100644
--- a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
+++ b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
@@ -9,8 +9,9 @@
 ; when the profile uses md5.
 ; 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
 
-; Test we properly merge not inlined profile with '--sample-profile-merge-inlinee' even if '--disable-sample-loader-inlining' is true
-; 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
+; Test we properly use flattened profile when using '--disable-sample-loader-inlining'.
+
+; 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
 
 @.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
 
@@ -103,3 +104,10 @@ declare i32 @printf(ptr, ...)
 ; MERGE: !{!"branch_weights", i32 10}
 ; MERGE: name: "sub"
 ; MERGE-NEXT: {!"function_entry_count", i64 3}
+
+; FLATTEN: name: "sum"
+; FLATTEN-NEXT: {!"function_entry_count", i64 35}
+; FLATTEN: !{!"branch_weights", i32 13, i32 23}
+; FLATTEN: !{!"branch_weights", i32 12}
+; FLATTEN: name: "sub"
+; FLATTEN-NEXT: {!"function_entry_count", i64 3}

>From efb68d240e6f048402387f8b7f29db0a671b33e6 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Tue, 5 Nov 2024 20:57:45 -0800
Subject: [PATCH 4/4] only use flattened profile for O0 mode

---
 .../llvm/Transforms/IPO/SampleProfile.h       |  4 ++-
 llvm/lib/Passes/PassBuilderPipelines.cpp      |  6 +++--
 llvm/lib/Transforms/IPO/SampleProfile.cpp     | 25 ++++++++++++-------
 .../SampleProfile/inline-mergeprof.ll         | 12 ++-------
 4 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/SampleProfile.h b/llvm/include/llvm/Transforms/IPO/SampleProfile.h
index 5adda3a0a16f3a..6f4f9701bae930 100644
--- a/llvm/include/llvm/Transforms/IPO/SampleProfile.h
+++ b/llvm/include/llvm/Transforms/IPO/SampleProfile.h
@@ -42,7 +42,8 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
       std::string File = "", std::string RemappingFile = "",
       ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None,
       IntrusiveRefCntPtr<vfs::FileSystem> FS = nullptr,
-      bool DisableSampleProfileInlining = false);
+      bool DisableSampleProfileInlining = false,
+      bool UseFlattenedProfile = false);
 
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 
@@ -52,6 +53,7 @@ class SampleProfileLoaderPass : public PassInfoMixin<SampleProfileLoaderPass> {
   const ThinOrFullLTOPhase LTOPhase;
   IntrusiveRefCntPtr<vfs::FileSystem> FS;
   bool DisableSampleProfileInlining;
+  bool UseFlattenedProfile;
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 39bba82fb5c38e..c278ca20d646c9 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -2154,11 +2154,13 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
 
   if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
-    // Explicitly disable sample loader inlining in O0 pipeline.
+    // Explicitly disable sample loader inlining and use flattened profile in O0
+    // pipeline.
     MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
                                         PGOOpt->ProfileRemappingFile,
                                         ThinOrFullLTOPhase::None, nullptr,
-                                        /*DisableSampleProfileInlining=*/true));
+                                        /*DisableSampleProfileInlining=*/true,
+                                        /*UseFlattenedProfile=*/true));
     // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
     // RequireAnalysisPass for PSI before subsequent non-module passes.
     MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index 96065d40970501..771d80a23574df 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -195,7 +195,8 @@ static cl::opt<bool> ProfileSizeInline(
 static cl::opt<bool> DisableSampleLoaderInlining(
     "disable-sample-loader-inlining", cl::Hidden, cl::init(false),
     cl::desc("If true, artifically skip inline transformation in sample-loader "
-             "pass, and use flattened profiles to emit annotation."));
+             "pass, and merge (or scale) profiles (as configured by "
+             "--sample-profile-merge-inlinee)."));
 
 namespace llvm {
 cl::opt<bool>
@@ -469,7 +470,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
       std::function<AssumptionCache &(Function &)> GetAssumptionCache,
       std::function<TargetTransformInfo &(Function &)> GetTargetTransformInfo,
       std::function<const TargetLibraryInfo &(Function &)> GetTLI,
-      LazyCallGraph &CG, bool DisableSampleProfileInlining)
+      LazyCallGraph &CG, bool DisableSampleProfileInlining,
+      bool UseFlattenedProfile)
       : SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName),
                                     std::move(FS)),
         GetAC(std::move(GetAssumptionCache)),
@@ -479,7 +481,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
                               ? llvm::AnnotateInlinePassName(InlineContext{
                                     LTOPhase, InlinePass::SampleProfileInliner})
                               : CSINLINE_DEBUG),
-        DisableSampleProfileInlining(DisableSampleProfileInlining) {}
+        DisableSampleProfileInlining(DisableSampleProfileInlining),
+        UseFlattenedProfile(UseFlattenedProfile) {}
 
   bool doInitialization(Module &M, FunctionAnalysisManager *FAM = nullptr);
   bool runOnModule(Module &M, ModuleAnalysisManager *AM,
@@ -595,6 +598,8 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
 
   bool DisableSampleProfileInlining;
 
+  bool UseFlattenedProfile;
+
   // External inline advisor used to replay inline decision from remarks.
   std::unique_ptr<InlineAdvisor> ExternalInlineAdvisor;
 
@@ -1980,9 +1985,9 @@ bool SampleProfileLoader::doInitialization(Module &M,
   if (DisableSampleLoaderInlining.getNumOccurrences())
     DisableSampleProfileInlining = DisableSampleLoaderInlining;
 
-  // Use flattened profile if inlining is disabled.
-  if (DisableSampleProfileInlining && !Reader->profileIsCS())
-    ProfileConverter::flattenProfile(Reader->getProfiles());
+  if (UseFlattenedProfile)
+    ProfileConverter::flattenProfile(Reader->getProfiles(),
+                                     Reader->profileIsCS());
 
   // While profile-sample-accurate is on, ignore symbol list.
   ProfAccForSymsInList =
@@ -2314,10 +2319,12 @@ bool SampleProfileLoader::runOnFunction(Function &F, ModuleAnalysisManager *AM)
 }
 SampleProfileLoaderPass::SampleProfileLoaderPass(
     std::string File, std::string RemappingFile, ThinOrFullLTOPhase LTOPhase,
-    IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining)
+    IntrusiveRefCntPtr<vfs::FileSystem> FS, bool DisableSampleProfileInlining,
+    bool UseFlattenedProfile)
     : ProfileFileName(File), ProfileRemappingFileName(RemappingFile),
       LTOPhase(LTOPhase), FS(std::move(FS)),
-      DisableSampleProfileInlining(DisableSampleProfileInlining) {}
+      DisableSampleProfileInlining(DisableSampleProfileInlining),
+      UseFlattenedProfile(UseFlattenedProfile) {}
 
 PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
                                                ModuleAnalysisManager &AM) {
@@ -2343,7 +2350,7 @@ PreservedAnalyses SampleProfileLoaderPass::run(Module &M,
       ProfileRemappingFileName.empty() ? SampleProfileRemappingFile
                                        : ProfileRemappingFileName,
       LTOPhase, FS, GetAssumptionCache, GetTTI, GetTLI, CG,
-      DisableSampleProfileInlining);
+      DisableSampleProfileInlining, UseFlattenedProfile);
   if (!SampleLoader.doInitialization(M, &FAM))
     return PreservedAnalyses::all();
 
diff --git a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
index d3aebe0bb97f0d..3094b25236020b 100644
--- a/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
+++ b/llvm/test/Transforms/SampleProfile/inline-mergeprof.ll
@@ -9,9 +9,8 @@
 ; when the profile uses md5.
 ; 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
 
-; Test we properly use flattened profile when using '--disable-sample-loader-inlining'.
-
-; 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
+; Test we properly merge not inlined profile with '--sample-profile-merge-inlinee' even if '--disable-sample-loader-inlining' is true
+; 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
 
 @.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
 
@@ -104,10 +103,3 @@ declare i32 @printf(ptr, ...)
 ; MERGE: !{!"branch_weights", i32 10}
 ; MERGE: name: "sub"
 ; MERGE-NEXT: {!"function_entry_count", i64 3}
-
-; FLATTEN: name: "sum"
-; FLATTEN-NEXT: {!"function_entry_count", i64 35}
-; FLATTEN: !{!"branch_weights", i32 13, i32 23}
-; FLATTEN: !{!"branch_weights", i32 12}
-; FLATTEN: name: "sub"
-; FLATTEN-NEXT: {!"function_entry_count", i64 3}



More information about the llvm-commits mailing list