Skip to content

Commit dfe9503

Browse files
HaohaiWenKornevNikita
authored andcommitted
[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.
1 parent 2b81c1c commit dfe9503

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
527527
void generateMDProfMetadata(Function &F);
528528
bool rejectHighStalenessProfile(Module &M, ProfileSummaryInfo *PSI,
529529
const SampleProfileMap &Profiles);
530-
void removePseudoProbeInsts(Module &M);
530+
void removePseudoProbeInstsDiscriminator(Module &M);
531531

532532
/// Map from function name to Function *. Used to find the function from
533533
/// the function name. If the function name contains suffix, additional
@@ -2125,13 +2125,25 @@ bool SampleProfileLoader::rejectHighStalenessProfile(
21252125
return false;
21262126
}
21272127

2128-
void SampleProfileLoader::removePseudoProbeInsts(Module &M) {
2128+
void SampleProfileLoader::removePseudoProbeInstsDiscriminator(Module &M) {
21292129
for (auto &F : M) {
21302130
std::vector<Instruction *> InstsToDel;
21312131
for (auto &BB : F) {
21322132
for (auto &I : BB) {
21332133
if (isa<PseudoProbeInst>(&I))
21342134
InstsToDel.push_back(&I);
2135+
else if (isa<CallBase>(&I))
2136+
if (const DILocation *DIL = I.getDebugLoc().get()) {
2137+
// Restore dwarf discriminator for call.
2138+
unsigned Discriminator = DIL->getDiscriminator();
2139+
if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
2140+
std::optional<uint32_t> DwarfDiscriminator =
2141+
PseudoProbeDwarfDiscriminator::extractDwarfBaseDiscriminator(
2142+
Discriminator);
2143+
I.setDebugLoc(DIL->cloneWithDiscriminator(
2144+
DwarfDiscriminator ? *DwarfDiscriminator : 0));
2145+
}
2146+
}
21352147
}
21362148
}
21372149
for (auto *I : InstsToDel)
@@ -2211,8 +2223,12 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM,
22112223
notInlinedCallInfo)
22122224
updateProfileCallee(pair.first, pair.second.entryCount);
22132225

2214-
if (RemoveProbeAfterProfileAnnotation && FunctionSamples::ProfileIsProbeBased)
2215-
removePseudoProbeInsts(M);
2226+
if (RemoveProbeAfterProfileAnnotation &&
2227+
FunctionSamples::ProfileIsProbeBased) {
2228+
removePseudoProbeInstsDiscriminator(M);
2229+
if (auto *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName))
2230+
M.eraseNamedMetadata(FuncInfo);
2231+
}
22162232

22172233
return retval;
22182234
}

llvm/test/Transforms/SampleProfile/pseudo-probe-profile.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
; 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
55

66
; REMOVE-PROBE-NOT: call void @llvm.pseudoprobe
7+
; REMOVE-PROBE-NOT: !llvm.pseudo_probe_desc
8+
; REMOVE-PROBE: !DILexicalBlockFile({{.*}}, discriminator: 0)
79

810
define dso_local i32 @foo(i32 %x, ptr %f) #0 !dbg !4 {
911
entry:

0 commit comments

Comments
 (0)