Skip to content

Commit 19c7613

Browse files
teresajohnsongithub-actions[bot]
authored andcommitted
Automerge: [MemProf] Convert removal of memprof attrs and metadata to a pass (#163841)
In preparation for a follow on fix that removes these attributes and metadata in non-LTO pipelines, convert updateMemProfAttributes to a new MemProfRemoveInfo pass that executes at the start of the LTO backend pass pipelines when we don't have an index indicating that we linked with a library support hot cold operator new. This is largely NFC from an end user perspective but changes where the removal can be observed, hence the test updates. A follow on change will use the new pass for non-LTO pipelines (for cases when the bitcode is initially matched with memprof data but we decide to complete the compile without LTO).
2 parents dfa2f32 + 2a7e7e2 commit 19c7613

File tree

10 files changed

+81
-62
lines changed

10 files changed

+81
-62
lines changed

clang/test/CodeGen/distributed-thin-lto/supports-hot-cold-new.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t1.o -x ir %t.o -c -fthinlto-index=%t.o.thinlto.bc -save-temps=obj
2424

25-
; RUN: llvm-dis %t.s.3.import.bc -o - | FileCheck %s --check-prefix=CHECK-IR
25+
; RUN: llvm-dis %t.s.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
2626
; CHECK-IR: !memprof {{.*}} !callsite
2727
; CHECK-IR: "memprof"="cold"
2828

@@ -42,7 +42,7 @@
4242

4343
; RUN: %clang -target x86_64-unknown-linux-gnu -O2 -o %t1.o -x ir %t.o -c -fthinlto-index=%t.o.thinlto.bc -save-temps=obj
4444

45-
; RUN: llvm-dis %t.s.3.import.bc -o - | FileCheck %s \
45+
; RUN: llvm-dis %t.s.4.opt.bc -o - | FileCheck %s \
4646
; RUN: --implicit-check-not "!memprof" --implicit-check-not "!callsite" \
4747
; RUN: --implicit-check-not "memprof"="cold"
4848

llvm/include/llvm/LTO/LTO.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ setupStatsFile(StringRef StatsFilename);
105105
/// ordered indices to elements in the input array.
106106
LLVM_ABI std::vector<int> generateModulesOrdering(ArrayRef<BitcodeModule *> R);
107107

108-
/// Updates MemProf attributes (and metadata) based on whether the index
109-
/// has recorded that we are linking with allocation libraries containing
110-
/// the necessary APIs for downstream transformations.
111-
LLVM_ABI void updateMemProfAttributes(Module &Mod,
112-
const ModuleSummaryIndex &Index);
113-
114108
class LTO;
115109
struct SymbolResolution;
116110

llvm/include/llvm/Transforms/IPO/MemProfContextDisambiguation.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ class MemProfContextDisambiguation
9595
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
9696
isPrevailing);
9797
};
98+
99+
/// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
100+
/// when we don't have an index that has recorded that we are linking with
101+
/// allocation libraries containing the necessary APIs for downstream
102+
/// transformations.
103+
class MemProfRemoveInfo : public PassInfoMixin<MemProfRemoveInfo> {
104+
public:
105+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
106+
};
107+
98108
} // end namespace llvm
99109

100110
#endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H

llvm/lib/LTO/LTO.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,38 +1257,6 @@ Error LTO::run(AddStreamFn AddStream, FileCache Cache) {
12571257
return Result;
12581258
}
12591259

1260-
void lto::updateMemProfAttributes(Module &Mod,
1261-
const ModuleSummaryIndex &Index) {
1262-
llvm::TimeTraceScope timeScope("LTO update memprof attributes");
1263-
if (Index.withSupportsHotColdNew())
1264-
return;
1265-
1266-
// The profile matcher applies hotness attributes directly for allocations,
1267-
// and those will cause us to generate calls to the hot/cold interfaces
1268-
// unconditionally. If supports-hot-cold-new was not enabled in the LTO
1269-
// link then assume we don't want these calls (e.g. not linking with
1270-
// the appropriate library, or otherwise trying to disable this behavior).
1271-
for (auto &F : Mod) {
1272-
for (auto &BB : F) {
1273-
for (auto &I : BB) {
1274-
auto *CI = dyn_cast<CallBase>(&I);
1275-
if (!CI)
1276-
continue;
1277-
if (CI->hasFnAttr("memprof"))
1278-
CI->removeFnAttr("memprof");
1279-
// Strip off all memprof metadata as it is no longer needed.
1280-
// Importantly, this avoids the addition of new memprof attributes
1281-
// after inlining propagation.
1282-
// TODO: If we support additional types of MemProf metadata beyond hot
1283-
// and cold, we will need to update the metadata based on the allocator
1284-
// APIs supported instead of completely stripping all.
1285-
CI->setMetadata(LLVMContext::MD_memprof, nullptr);
1286-
CI->setMetadata(LLVMContext::MD_callsite, nullptr);
1287-
}
1288-
}
1289-
}
1290-
}
1291-
12921260
Error LTO::runRegularLTO(AddStreamFn AddStream) {
12931261
llvm::TimeTraceScope timeScope("Run regular LTO");
12941262
LLVMContext &CombinedCtx = RegularLTO.CombinedModule->getContext();
@@ -1346,8 +1314,6 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
13461314
}
13471315
}
13481316

1349-
updateMemProfAttributes(*RegularLTO.CombinedModule, ThinLTO.CombinedIndex);
1350-
13511317
bool WholeProgramVisibilityEnabledInLTO =
13521318
Conf.HasWholeProgramVisibility &&
13531319
// If validation is enabled, upgrade visibility only when all vtables

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
726726
}
727727

728728
// Do this after any importing so that imported code is updated.
729-
updateMemProfAttributes(Mod, CombinedIndex);
730729
updatePublicTypeTestCalls(Mod, CombinedIndex.withWholeProgramVisibility());
731730

732731
if (Conf.PostImportModuleHook && !Conf.PostImportModuleHook(Task, Mod))

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,12 @@ ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
18081808
OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary) {
18091809
ModulePassManager MPM;
18101810

1811+
// If we are invoking this without a summary index noting that we are linking
1812+
// with a library containing the necessary APIs, remove any MemProf related
1813+
// attributes and metadata.
1814+
if (!ImportSummary || !ImportSummary->withSupportsHotColdNew())
1815+
MPM.addPass(MemProfRemoveInfo());
1816+
18111817
if (ImportSummary) {
18121818
// For ThinLTO we must apply the context disambiguation decisions early, to
18131819
// ensure we can correctly match the callsites to summary data.
@@ -1879,6 +1885,12 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
18791885

18801886
invokeFullLinkTimeOptimizationEarlyEPCallbacks(MPM, Level);
18811887

1888+
// If we are invoking this without a summary index noting that we are linking
1889+
// with a library containing the necessary APIs, remove any MemProf related
1890+
// attributes and metadata.
1891+
if (!ExportSummary || !ExportSummary->withSupportsHotColdNew())
1892+
MPM.addPass(MemProfRemoveInfo());
1893+
18821894
// Create a function that performs CFI checks for cross-DSO calls with targets
18831895
// in the current module.
18841896
MPM.addPass(CrossDSOCFIPass());

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ MODULE_PASS("pgo-force-function-attrs",
113113
? PGOOpt->ColdOptType
114114
: PGOOptions::ColdFuncOpt::Default))
115115
MODULE_PASS("memprof-context-disambiguation", MemProfContextDisambiguation())
116+
MODULE_PASS("memprof-remove-attributes", MemProfRemoveInfo())
116117
MODULE_PASS("memprof-module", ModuleMemProfilerPass())
117118
MODULE_PASS("mergefunc", MergeFunctionsPass())
118119
MODULE_PASS("metarenamer", MetaRenamerPass())

llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6150,3 +6150,42 @@ void MemProfContextDisambiguation::run(
61506150
IndexCallsiteContextGraph CCG(Index, isPrevailing);
61516151
CCG.process();
61526152
}
6153+
6154+
// Strips MemProf attributes and metadata. Can be invoked by the pass pipeline
6155+
// when we don't have an index that has recorded that we are linking with
6156+
// allocation libraries containing the necessary APIs for downstream
6157+
// transformations.
6158+
PreservedAnalyses MemProfRemoveInfo::run(Module &M, ModuleAnalysisManager &AM) {
6159+
// The profile matcher applies hotness attributes directly for allocations,
6160+
// and those will cause us to generate calls to the hot/cold interfaces
6161+
// unconditionally. If supports-hot-cold-new was not enabled in the LTO
6162+
// link then assume we don't want these calls (e.g. not linking with
6163+
// the appropriate library, or otherwise trying to disable this behavior).
6164+
bool Changed = false;
6165+
for (auto &F : M) {
6166+
for (auto &BB : F) {
6167+
for (auto &I : BB) {
6168+
auto *CI = dyn_cast<CallBase>(&I);
6169+
if (!CI)
6170+
continue;
6171+
if (CI->hasFnAttr("memprof")) {
6172+
CI->removeFnAttr("memprof");
6173+
Changed = true;
6174+
}
6175+
if (!CI->hasMetadata(LLVMContext::MD_callsite)) {
6176+
assert(!CI->hasMetadata(LLVMContext::MD_memprof));
6177+
continue;
6178+
}
6179+
// Strip off all memprof metadata as it is no longer needed.
6180+
// Importantly, this avoids the addition of new memprof attributes
6181+
// after inlining propagation.
6182+
CI->setMetadata(LLVMContext::MD_memprof, nullptr);
6183+
CI->setMetadata(LLVMContext::MD_callsite, nullptr);
6184+
Changed = true;
6185+
}
6186+
}
6187+
}
6188+
if (!Changed)
6189+
return PreservedAnalyses::all();
6190+
return PreservedAnalyses::none();
6191+
}

llvm/test/LTO/X86/memprof-supports-hot-cold-new.ll

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,26 @@
1313
; RUN: -r=%t.o,main,plx \
1414
; RUN: -r=%t.o,_Znam, \
1515
; RUN: -memprof-dump-ccg \
16-
; RUN: -save-temps \
17-
; RUN: -o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP
18-
; DUMP: Callsite Context Graph:
16+
; RUN: -print-before=memprof-context-disambiguation \
17+
; RUN: -o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP --check-prefix=IR
1918

20-
; RUN: llvm-dis %t.out.0.0.preopt.bc -o - | FileCheck %s --check-prefix=IR
2119
; IR: !memprof {{.*}} !callsite
2220
; IR: "memprof"="cold"
2321

22+
; DUMP: Callsite Context Graph:
23+
2424
;; Next check without -supports-hot-cold-new, we should not perform
2525
;; context disambiguation, and we should strip memprof metadata and
2626
;; attributes before optimization.
2727
; RUN: llvm-lto2 run %t.o -enable-memprof-context-disambiguation \
2828
; RUN: -r=%t.o,main,plx \
2929
; RUN: -r=%t.o,_Znam, \
3030
; RUN: -memprof-dump-ccg \
31-
; RUN: -save-temps \
31+
; RUN: -print-before=memprof-context-disambiguation \
3232
; RUN: -o %t.out 2>&1 | FileCheck %s --allow-empty \
33-
; RUN: --implicit-check-not "Callsite Context Graph:"
34-
35-
; RUN: llvm-dis %t.out.0.0.preopt.bc -o - | FileCheck %s \
36-
; RUN: --implicit-check-not "!memprof" --implicit-check-not "!callsite" \
37-
; RUN: --implicit-check-not "memprof"="cold"
33+
; RUN: --implicit-check-not "Callsite Context Graph:" \
34+
; RUN: --implicit-check-not "!memprof" --implicit-check-not "!callsite" \
35+
; RUN: --implicit-check-not "memprof"="cold"
3836

3937
source_filename = "memprof-supports-hot-cold-new.ll"
4038
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/ThinLTO/X86/memprof-supports-hot-cold-new.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
; RUN: -r=%t/foo.o,foo,plx \
1818
; RUN: -r=%t/foo.o,_Znam, \
1919
; RUN: -memprof-dump-ccg \
20-
; RUN: -save-temps \
21-
; RUN: -o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP
20+
; RUN: -print-before=memprof-context-disambiguation \
21+
; RUN: -thinlto-threads=1 \
22+
; RUN: -o %t.out 2>&1 | FileCheck %s --check-prefix=DUMP --check-prefix=IR
23+
2224
; DUMP: Callsite Context Graph:
2325

24-
; RUN: llvm-dis %t.out.1.3.import.bc -o - | FileCheck %s --check-prefix=IR
2526
; IR: @main()
2627
; IR: !memprof {{.*}} !callsite
2728
; IR: @_Znam(i64 0) #[[ATTR:[0-9]+]]
@@ -41,13 +42,12 @@
4142
; RUN: -r=%t/foo.o,foo,plx \
4243
; RUN: -r=%t/foo.o,_Znam, \
4344
; RUN: -memprof-dump-ccg \
44-
; RUN: -save-temps \
45+
; RUN: -print-before=memprof-context-disambiguation \
46+
; RUN: -thinlto-threads=1 \
4547
; RUN: -o %t.out 2>&1 | FileCheck %s --allow-empty \
46-
; RUN: --implicit-check-not "Callsite Context Graph:"
47-
48-
; RUN: llvm-dis %t.out.1.3.import.bc -o - | FileCheck %s \
49-
; RUN: --implicit-check-not "!memprof" --implicit-check-not "!callsite" \
50-
; RUN: --implicit-check-not "memprof"="cold"
48+
; RUN: --implicit-check-not "Callsite Context Graph:" \
49+
; RUN: --implicit-check-not "!memprof" --implicit-check-not "!callsite" \
50+
; RUN: --implicit-check-not "memprof"="cold"
5151

5252
;--- main.ll
5353
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)