Skip to content

Commit ccc8e45

Browse files
authored
[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 4c597d4 commit ccc8e45

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

llvm/lib/Transforms/IPO/SampleProfile.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ class SampleProfileLoader final : public SampleProfileLoaderBaseImpl<Function> {
529529
void generateMDProfMetadata(Function &F);
530530
bool rejectHighStalenessProfile(Module &M, ProfileSummaryInfo *PSI,
531531
const SampleProfileMap &Profiles);
532-
void removePseudoProbeInsts(Module &M);
532+
void removePseudoProbeInstsDiscriminator(Module &M);
533533

534534
/// Map from function name to Function *. Used to find the function from
535535
/// the function name. If the function name contains suffix, additional
@@ -2138,13 +2138,25 @@ bool SampleProfileLoader::rejectHighStalenessProfile(
21382138
return false;
21392139
}
21402140

2141-
void SampleProfileLoader::removePseudoProbeInsts(Module &M) {
2141+
void SampleProfileLoader::removePseudoProbeInstsDiscriminator(Module &M) {
21422142
for (auto &F : M) {
21432143
std::vector<Instruction *> InstsToDel;
21442144
for (auto &BB : F) {
21452145
for (auto &I : BB) {
21462146
if (isa<PseudoProbeInst>(&I))
21472147
InstsToDel.push_back(&I);
2148+
else if (isa<CallBase>(&I))
2149+
if (const DILocation *DIL = I.getDebugLoc().get()) {
2150+
// Restore dwarf discriminator for call.
2151+
unsigned Discriminator = DIL->getDiscriminator();
2152+
if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
2153+
std::optional<uint32_t> DwarfDiscriminator =
2154+
PseudoProbeDwarfDiscriminator::extractDwarfBaseDiscriminator(
2155+
Discriminator);
2156+
I.setDebugLoc(DIL->cloneWithDiscriminator(
2157+
DwarfDiscriminator ? *DwarfDiscriminator : 0));
2158+
}
2159+
}
21482160
}
21492161
}
21502162
for (auto *I : InstsToDel)
@@ -2224,8 +2236,12 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM,
22242236
notInlinedCallInfo)
22252237
updateProfileCallee(pair.first, pair.second.entryCount);
22262238

2227-
if (RemoveProbeAfterProfileAnnotation && FunctionSamples::ProfileIsProbeBased)
2228-
removePseudoProbeInsts(M);
2239+
if (RemoveProbeAfterProfileAnnotation &&
2240+
FunctionSamples::ProfileIsProbeBased) {
2241+
removePseudoProbeInstsDiscriminator(M);
2242+
if (auto *FuncInfo = M.getNamedMetadata(PseudoProbeDescMetadataName))
2243+
M.eraseNamedMetadata(FuncInfo);
2244+
}
22292245

22302246
return retval;
22312247
}

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

+2
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)