From b7248d53637fb534d35554f1ddda8e50fe85fa63 Mon Sep 17 00:00:00 2001 From: Lei Wang Date: Mon, 29 Apr 2024 09:27:33 -0700 Subject: [PseudoProbe] Add an option to remove pseudo probes after profile annotation (#90293) This can be used for testing perf overhead of pseudo-probe. --- llvm/lib/Transforms/IPO/SampleProfile.cpp | 25 +++++++++++++++++++--- .../SampleProfile/pseudo-probe-profile.ll | 8 +++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index 0b3a693..6cbd138 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -252,20 +252,21 @@ static cl::opt PrecentMismatchForStalenessError( static cl::opt CallsitePrioritizedInline( "sample-profile-prioritized-inline", cl::Hidden, - cl::desc("Use call site prioritized inlining for sample profile loader." "Currently only CSSPGO is supported.")); static cl::opt UsePreInlinerDecision( "sample-profile-use-preinliner", cl::Hidden, - cl::desc("Use the preinliner decisions stored in profile context.")); static cl::opt AllowRecursiveInline( "sample-profile-recursive-inline", cl::Hidden, - cl::desc("Allow sample loader inliner to inline recursive calls.")); +static cl::opt RemoveProbeAfterProfileAnnotation( + "sample-profile-remove-probe", cl::Hidden, cl::init(false), + cl::desc("Remove pseudo-probe after sample profile annotation.")); + static cl::opt ProfileInlineReplayFile( "sample-profile-inline-replay", cl::init(""), cl::value_desc("filename"), cl::desc( @@ -518,6 +519,7 @@ protected: void generateMDProfMetadata(Function &F); bool rejectHighStalenessProfile(Module &M, ProfileSummaryInfo *PSI, const SampleProfileMap &Profiles); + void removePseudoProbeInsts(Module &M); /// Map from function name to Function *. Used to find the function from /// the function name. If the function name contains suffix, additional @@ -2127,6 +2129,20 @@ bool SampleProfileLoader::rejectHighStalenessProfile( return false; } +void SampleProfileLoader::removePseudoProbeInsts(Module &M) { + for (auto &F : M) { + std::vector InstsToDel; + for (auto &BB : F) { + for (auto &I : BB) { + if (isa(&I)) + InstsToDel.push_back(&I); + } + } + for (auto *I : InstsToDel) + I->eraseFromParent(); + } +} + bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM, ProfileSummaryInfo *_PSI, LazyCallGraph &CG) { @@ -2196,6 +2212,9 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM, notInlinedCallInfo) updateProfileCallee(pair.first, pair.second.entryCount); + if (RemoveProbeAfterProfileAnnotation && FunctionSamples::ProfileIsProbeBased) + removePseudoProbeInsts(M); + return retval; } diff --git a/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll b/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll index 867a49d..7258ffc 100644 --- a/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll +++ b/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll @@ -1,5 +1,9 @@ -; RUN: opt < %s -passes=pseudo-probe,sample-profile -sample-profile-file=%S/Inputs/pseudo-probe-profile.prof -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml -sample-profile-use-profi=0 -S | 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-profile.prof -pass-remarks=sample-profile -pass-remarks-output=%t.opt.yaml -sample-profile-use-profi=0 -S -o %t +; RUN: FileCheck %s --input-file %t +; RUN: FileCheck %s -check-prefix=YAML --input-file %t.opt.yaml +; RUN: opt < %t -passes=sample-profile -sample-profile-file=%S/Inputs/pseudo-probe-profile.prof -sample-profile-remove-probe -S | FileCheck %s -check-prefix=REMOVE-PROBE + +; REMOVE-PROBE-NOT: call void @llvm.pseudoprobe define dso_local i32 @foo(i32 %x, ptr %f) #0 !dbg !4 { entry: -- cgit v1.1