Skip to content

Commit 120e42d

Browse files
[MemProf] Improve metadata cleanup in LTO backend (#113039)
Previously we were attempting to remove the memprof-related metadata when iterating through instructions in the LTO backend. However, we missed some as there are a number of cases where we skip instructions, or even entire functions. Simplify the cleanup and ensure all is removed by doing a full sweep over all instructions after completing cloning. This is largely NFC except with -memprof-report-hinted-sizes enabled, because we were propagating and simplifying the metadata after inlining in the LTO backend, which caused some stray messages as metadata was re-converted to attributes.
1 parent 900b636 commit 120e42d

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4264,9 +4264,6 @@ bool MemProfContextDisambiguation::applyImport(Module &M) {
42644264
AllocVersionsThinBackend++;
42654265
if (!MaxAllocVersionsThinBackend)
42664266
MaxAllocVersionsThinBackend = 1;
4267-
// Remove any remaining callsite metadata and we can skip the rest of
4268-
// the handling for this instruction, since no cloning needed.
4269-
I.setMetadata(LLVMContext::MD_callsite, nullptr);
42704267
continue;
42714268
}
42724269

@@ -4419,16 +4416,30 @@ bool MemProfContextDisambiguation::applyImport(Module &M) {
44194416
CloneCallsite(Callsite->second, CB, CalledFunction);
44204417
}
44214418
}
4422-
// Memprof and callsite metadata on memory allocations no longer needed.
4423-
I.setMetadata(LLVMContext::MD_memprof, nullptr);
4424-
I.setMetadata(LLVMContext::MD_callsite, nullptr);
44254419
}
44264420
}
44274421

44284422
// Now do any promotion required for cloning.
44294423
performICP(M, FS->callsites(), VMaps, ICallAnalysisInfo, ORE);
44304424
}
44314425

4426+
// We skip some of the functions and instructions above, so remove all the
4427+
// metadata in a single sweep here.
4428+
for (auto &F : M) {
4429+
// We can skip memprof clones because createFunctionClones already strips
4430+
// the metadata from the newly created clones.
4431+
if (F.isDeclaration() || isMemProfClone(F))
4432+
continue;
4433+
for (auto &BB : F) {
4434+
for (auto &I : BB) {
4435+
if (!isa<CallBase>(I))
4436+
continue;
4437+
I.setMetadata(LLVMContext::MD_memprof, nullptr);
4438+
I.setMetadata(LLVMContext::MD_callsite, nullptr);
4439+
}
4440+
}
4441+
}
4442+
44324443
return Changed;
44334444
}
44344445

llvm/test/ThinLTO/X86/memprof-icp.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@
176176
; RUN: -pass-remarks=. -save-temps \
177177
; RUN: -o %t.noicp.out 2>&1 | FileCheck %s --implicit-check-not "created clone"
178178

179-
; RUN: llvm-dis %t.noicp.out.2.4.opt.bc -o - | FileCheck %s --implicit-check-not "_Z3fooR2B0j.memprof"
179+
;; Verify that we did not do any cloning of the function with the indirect call
180+
;; when memprof ICP is off. However, we should still have removed the callsite
181+
;; metadata.
182+
; RUN: llvm-dis %t.noicp.out.2.4.opt.bc -o - | FileCheck %s --implicit-check-not "_Z3fooR2B0j.memprof" --implicit-check-not "!callsite"
180183

181184
; REMARKS-MAIN: call in clone main assigned to call function clone _Z3fooR2B0j.memprof.1
182185
; REMARKS-MAIN: call in clone main assigned to call function clone _Z3fooR2B0j.memprof.1

0 commit comments

Comments
 (0)