diff --git a/llvm/include/llvm/Analysis/InlineAdvisor.h b/llvm/include/llvm/Analysis/InlineAdvisor.h --- a/llvm/include/llvm/Analysis/InlineAdvisor.h +++ b/llvm/include/llvm/Analysis/InlineAdvisor.h @@ -194,7 +194,9 @@ } /// NOTE pass name is annotated only when inline advisor constructor provides InlineContext. - const char *getAnnotatedInlinePassName(); + const char *getAnnotatedInlinePassName() const { + return AnnotatedInlinePassName.c_str(); + } protected: InlineAdvisor(Module &M, FunctionAnalysisManager &FAM, @@ -206,6 +208,7 @@ Module &M; FunctionAnalysisManager &FAM; const Optional IC; + const std::string AnnotatedInlinePassName; std::unique_ptr ImportedFunctionsStats; enum class MandatoryInliningKind { NotMandatory, Always, Never }; diff --git a/llvm/include/llvm/Transforms/IPO/SampleProfile.h b/llvm/include/llvm/Transforms/IPO/SampleProfile.h --- a/llvm/include/llvm/Transforms/IPO/SampleProfile.h +++ b/llvm/include/llvm/Transforms/IPO/SampleProfile.h @@ -36,7 +36,7 @@ private: std::string ProfileFileName; std::string ProfileRemappingFileName; - ThinOrFullLTOPhase LTOPhase; + const ThinOrFullLTOPhase LTOPhase; }; } // end namespace llvm diff --git a/llvm/lib/Analysis/InlineAdvisor.cpp b/llvm/lib/Analysis/InlineAdvisor.cpp --- a/llvm/lib/Analysis/InlineAdvisor.cpp +++ b/llvm/lib/Analysis/InlineAdvisor.cpp @@ -508,7 +508,9 @@ InlineAdvisor::InlineAdvisor(Module &M, FunctionAnalysisManager &FAM, Optional IC) - : M(M), FAM(FAM), IC(IC) { + : M(M), FAM(FAM), IC(IC), + AnnotatedInlinePassName(IC ? llvm::AnnotateInlinePassName(*IC) + : DEBUG_TYPE) { if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) { ImportedFunctionsStats = std::make_unique(); @@ -572,17 +574,6 @@ std::string(getInlineAdvisorContext(IC.Pass)); } -const char *InlineAdvisor::getAnnotatedInlinePassName() { - if (!IC) - return DEBUG_TYPE; - - // IC is constant and initialized in constructor, so compute the annotated - // name only once. - static const std::string PassName = llvm::AnnotateInlinePassName(*IC); - - return PassName.c_str(); -} - InlineAdvisor::MandatoryInliningKind InlineAdvisor::getMandatoryKind(CallBase &CB, FunctionAnalysisManager &FAM, OptimizationRemarkEmitter &ORE) { diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -291,6 +291,11 @@ "overwrite-existing-weights", cl::Hidden, cl::init(false), cl::desc("Ignore existing branch weights on IR and always overwrite.")); +static cl::opt AnnotateSampleProfileInlinePhase( + "annotate-sample-profile-inline-phase", cl::Hidden, cl::init(false), + cl::desc("Annotate LTO phase (prelink / postlink), or main (no LTO) for " + "sample-profile inline pass name.")); + extern cl::opt EnableExtTspBlockPlacement; namespace { @@ -423,7 +428,11 @@ : SampleProfileLoaderBaseImpl(std::string(Name), std::string(RemapName)), GetAC(std::move(GetAssumptionCache)), GetTTI(std::move(GetTargetTransformInfo)), GetTLI(std::move(GetTLI)), - LTOPhase(LTOPhase) {} + LTOPhase(LTOPhase), + AnnotatedPassName(AnnotateSampleProfileInlinePhase + ? llvm::AnnotateInlinePassName(InlineContext{ + LTOPhase, InlinePass::SampleProfileInliner}) + : CSINLINE_DEBUG) {} bool doInitialization(Module &M, FunctionAnalysisManager *FAM = nullptr); bool runOnModule(Module &M, ModuleAnalysisManager *AM, @@ -490,7 +499,8 @@ /// We need to know the LTO phase because for example in ThinLTOPrelink /// phase, in annotation, we should not promote indirect calls. Instead, /// we will mark GUIDs that needs to be annotated to the function. - ThinOrFullLTOPhase LTOPhase; + const ThinOrFullLTOPhase LTOPhase; + const std::string AnnotatedPassName; /// Profle Symbol list tells whether a function name appears in the binary /// used to generate the current profile. @@ -530,6 +540,11 @@ // A pseudo probe helper to correlate the imported sample counts. std::unique_ptr ProbeManager; + +private: + const char *getAnnotatedRemarkPassName() const { + return AnnotatedPassName.c_str(); + } }; class SampleProfileLoaderLegacyPass : public ModulePass { @@ -1019,8 +1034,9 @@ for (auto I : Candidates) { Function *CalledFunction = I->getCalledFunction(); if (CalledFunction) { - ORE->emit(OptimizationRemarkAnalysis(CSINLINE_DEBUG, "InlineAttempt", - I->getDebugLoc(), I->getParent()) + ORE->emit(OptimizationRemarkAnalysis(getAnnotatedRemarkPassName(), + "InlineAttempt", I->getDebugLoc(), + I->getParent()) << "previous inlining reattempted for " << (Hot ? "hotness: '" : "size: '") << ore::NV("Callee", CalledFunction) << "' into '" @@ -1239,7 +1255,8 @@ InlineCost Cost = shouldInlineCandidate(Candidate); if (Cost.isNever()) { - ORE->emit(OptimizationRemarkAnalysis(CSINLINE_DEBUG, "InlineFail", DLoc, BB) + ORE->emit(OptimizationRemarkAnalysis(getAnnotatedRemarkPassName(), + "InlineFail", DLoc, BB) << "incompatible inlining"); return false; } @@ -1257,8 +1274,8 @@ *CalledFunction); // The call to InlineFunction erases I, so we can't pass it here. - emitInlinedIntoBasedOnCost(*ORE, DLoc, BB, *CalledFunction, - *BB->getParent(), Cost, true, CSINLINE_DEBUG); + emitInlinedIntoBasedOnCost(*ORE, DLoc, BB, *CalledFunction, *BB->getParent(), + Cost, true, getAnnotatedRemarkPassName()); // Now populate the list of newly exposed call sites. if (InlinedCallSites) { @@ -1544,11 +1561,11 @@ if (!Callee || Callee->isDeclaration()) continue; - ORE->emit(OptimizationRemarkAnalysis(CSINLINE_DEBUG, "NotInline", - I->getDebugLoc(), I->getParent()) - << "previous inlining not repeated: '" - << ore::NV("Callee", Callee) << "' into '" - << ore::NV("Caller", &F) << "'"); + ORE->emit( + OptimizationRemarkAnalysis(getAnnotatedRemarkPassName(), "NotInline", + I->getDebugLoc(), I->getParent()) + << "previous inlining not repeated: '" << ore::NV("Callee", Callee) + << "' into '" << ore::NV("Caller", &F) << "'"); ++NumCSNotInlined; const FunctionSamples *FS = Pair.getSecond(); diff --git a/llvm/test/Transforms/SampleProfile/pseudo-probe-inline.ll b/llvm/test/Transforms/SampleProfile/pseudo-probe-inline.ll --- a/llvm/test/Transforms/SampleProfile/pseudo-probe-inline.ll +++ b/llvm/test/Transforms/SampleProfile/pseudo-probe-inline.ll @@ -1,9 +1,15 @@ -; RUN: opt < %s -passes=pseudo-probe,sample-profile -sample-profile-file=%S/Inputs/pseudo-probe-inline.prof -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s -; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml +; RUN: opt < %s -passes=pseudo-probe,sample-profile -sample-profile-file=%S/Inputs/pseudo-probe-inline.prof -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-NO-ANNOTATE < %t.opt.yaml ; RUN: llvm-profdata merge --sample --extbinary %S/Inputs/pseudo-probe-inline.prof -o %t2 -; RUN: opt < %s -passes=pseudo-probe,sample-profile -sample-profile-file=%t2 -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t2.opt.yaml 2>&1 | FileCheck %s -; RUN: FileCheck %s -check-prefix=YAML < %t2.opt.yaml +; RUN: opt < %s -passes=pseudo-probe,sample-profile -sample-profile-file=%t2 -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t2.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-NO-ANNOTATE < %t2.opt.yaml + +; RUN: opt < %s -passes=pseudo-probe,sample-profile -annotate-sample-profile-inline-phase=true -sample-profile-file=%S/Inputs/pseudo-probe-inline.prof -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t3.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-ANNOTATE < %t3.opt.yaml + +; RUN: opt < %s -passes=pseudo-probe,sample-profile -annotate-sample-profile-inline-phase=true -sample-profile-file=%t2 -S -pass-remarks=sample-profile -sample-profile-prioritized-inline=0 -pass-remarks-output=%t4.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-ANNOTATE < %t4.opt.yaml target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -74,7 +80,8 @@ ; Checking to see if YAML file is generated and contains remarks ;YAML: --- !Passed -;YAML-NEXT: Pass: sample-profile-inline +;YAML-NO-ANNOTATE-NEXT: Pass: sample-profile-inline +;YAML-ANNOTATE-NEXT: Pass: main-sample-profile-inline ;YAML-NEXT: Name: Inlined ;YAML-NEXT: DebugLoc: { File: test.cpp, Line: 10, Column: 11 } ;YAML-NEXT: Function: foo diff --git a/llvm/test/Transforms/SampleProfile/remarks.ll b/llvm/test/Transforms/SampleProfile/remarks.ll --- a/llvm/test/Transforms/SampleProfile/remarks.ll +++ b/llvm/test/Transforms/SampleProfile/remarks.ll @@ -1,7 +1,12 @@ ; RUN: opt < %s -sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s -; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml +; RUN: FileCheck %s -check-prefixes=YAML,YAML-NO-ANNOTATE < %t.opt.yaml ; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s -; RUN: FileCheck %s -check-prefix=YAML < %t.opt.yaml +; RUN: FileCheck %s -check-prefixes=YAML,YAML-NO-ANNOTATE < %t.opt.yaml + +; RUN: opt < %s -sample-profile -annotate-sample-profile-inline-phase -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-ANNOTATE < %t.opt.yaml +; RUN: opt < %s -passes=sample-profile -annotate-sample-profile-inline-phase -sample-profile-file=%S/Inputs/remarks.prof -S -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml 2>&1 | FileCheck %s +; RUN: FileCheck %s -check-prefixes=YAML,YAML-ANNOTATE < %t.opt.yaml ; Original test case. ; @@ -32,7 +37,8 @@ ; Checking to see if YAML file is generated and contains remarks ;YAML: --- !Passed -;YAML-NEXT: Pass: sample-profile-inline +;YAML-NO-ANNOTATE-NEXT: Pass: sample-profile-inline +;YAML-ANNOTATE-NEXT: Pass: main-sample-profile-inline ;YAML-NEXT: Name: Inlined ;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 13, Column: 21 } ;YAML-NEXT: Function: main @@ -60,7 +66,8 @@ ;YAML-NEXT: - String: ';' ;YAML-NEXT: ... ;YAML: --- !Passed -;YAML-NEXT: Pass: sample-profile-inline +;YAML-NO-ANNOTATE-NEXT: Pass: sample-profile-inline +;YAML-ANNOTATE-NEXT: Pass: main-sample-profile-inline ;YAML-NEXT: Name: AlwaysInline ;YAML-NEXT: DebugLoc: { File: remarks.cc, Line: 9, Column: 19 } ;YAML-NEXT: Function: main