[SampleProfile][Inline] Annotate sample profile inline remarks with link phase (prelink/postlink) information.

Differential Revision: https://reviews.llvm.org/D126833
This commit is contained in:
Mingming Liu
2022-06-01 15:21:59 -07:00
parent e8716179eb
commit bc856eb3fc
6 changed files with 60 additions and 35 deletions

View File

@@ -194,7 +194,9 @@ public:
}
/// 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 @@ protected:
Module &M;
FunctionAnalysisManager &FAM;
const Optional<InlineContext> IC;
const std::string AnnotatedInlinePassName;
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
enum class MandatoryInliningKind { NotMandatory, Always, Never };

View File

@@ -36,7 +36,7 @@ public:
private:
std::string ProfileFileName;
std::string ProfileRemappingFileName;
ThinOrFullLTOPhase LTOPhase;
const ThinOrFullLTOPhase LTOPhase;
};
} // end namespace llvm

View File

@@ -508,7 +508,9 @@ void llvm::emitInlinedIntoBasedOnCost(
InlineAdvisor::InlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
Optional<InlineContext> 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<ImportedFunctionsInliningStatistics>();
@@ -572,17 +574,6 @@ std::string llvm::AnnotateInlinePassName(InlineContext IC) {
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) {

View File

@@ -291,6 +291,11 @@ static cl::opt<bool> OverwriteExistingWeights(
"overwrite-existing-weights", cl::Hidden, cl::init(false),
cl::desc("Ignore existing branch weights on IR and always overwrite."));
static cl::opt<bool> 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<bool> EnableExtTspBlockPlacement;
namespace {
@@ -423,7 +428,11 @@ public:
: 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 @@ protected:
/// 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 @@ protected:
// A pseudo probe helper to correlate the imported sample counts.
std::unique_ptr<PseudoProbeManager> ProbeManager;
private:
const char *getAnnotatedRemarkPassName() const {
return AnnotatedPassName.c_str();
}
};
class SampleProfileLoaderLegacyPass : public ModulePass {
@@ -1019,8 +1034,9 @@ void SampleProfileLoader::emitOptimizationRemarksForInlineCandidates(
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 @@ bool SampleProfileLoader::tryInlineCandidate(
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 @@ bool SampleProfileLoader::tryInlineCandidate(
*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 @@ void SampleProfileLoader::promoteMergeNotInlinedContextSamples(
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();

View File

@@ -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 @@ if.end:
; 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

View File

@@ -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