From ccc8e454044477de9ce71c1b22dd048f189a9601 Mon Sep 17 00:00:00 2001 From: Haohai Wen Date: Fri, 13 Dec 2024 17:05:03 +0800 Subject: [PATCH] [PseudoProbe] Fix cleanup for pseudo probe after annotation (#119660) When using -sample-profile-remove-probe, pseudo probe desc should also be removed and dwarf discriminator for call instruction should be restored. --- llvm/lib/Transforms/IPO/SampleProfile.cpp | 24 +++++++++++++++---- .../SampleProfile/pseudo-probe-profile.ll | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index b2fa66f2a6d379..603beb3b883d7f 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -529,7 +529,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl { void generateMDProfMetadata(Function &F); bool rejectHighStalenessProfile(Module &M, ProfileSummaryInfo *PSI, const SampleProfileMap &Profiles); - void removePseudoProbeInsts(Module &M); + void removePseudoProbeInstsDiscriminator(Module &M); /// Map from function name to Function *. Used to find the function from /// the function name. If the function name contains suffix, additional @@ -2138,13 +2138,25 @@ bool SampleProfileLoader::rejectHighStalenessProfile( return false; } -void SampleProfileLoader::removePseudoProbeInsts(Module &M) { +void SampleProfileLoader::removePseudoProbeInstsDiscriminator(Module &M) { for (auto &F : M) { std::vector InstsToDel; for (auto &BB : F) { for (auto &I : BB) { if (isa(&I)) InstsToDel.push_back(&I); + else if (isa(&I)) + if (const DILocation *DIL = I.getDebugLoc().get()) { + // Restore dwarf discriminator for call. + unsigned Discriminator = DIL->getDiscriminator(); + if (DILocation::isPseudoProbeDiscriminator(Discriminator)) { + std::optional DwarfDiscriminator = + PseudoProbeDwarfDiscriminator::extractDwarfBaseDiscriminator( + Discriminator); + I.setDebugLoc(DIL->cloneWithDiscriminator( + DwarfDiscriminator ? *DwarfDiscriminator : 0)); + } + } } } for (auto *I : InstsToDel) @@ -2224,8 +2236,12 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM, notInlinedCallInfo) updateProfileCallee(pair.first, pair.second.entryCount); - if (RemoveProbeAfterProfileAnnotation && FunctionSamples::ProfileIsProbeBased) - removePseudoProbeInsts(M); + if (RemoveProbeAfterProfileAnnotation && + FunctionSamples::ProfileIsProbeBased) { + removePseudoProbeInstsDiscriminator(M); + if (auto *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName)) + M.eraseNamedMetadata(FuncInfo); + } return retval; } diff --git a/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll b/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll index b52f93763d4926..66dbc49a1d210a 100644 --- a/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll +++ b/llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll @@ -4,6 +4,8 @@ ; 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 +; REMOVE-PROBE-NOT: !llvm.pseudo_probe_desc +; REMOVE-PROBE: !DILexicalBlockFile({{.*}}, discriminator: 0) define dso_local i32 @foo(i32 %x, ptr %f) #0 !dbg !4 { entry: