Skip to content

Commit d6ad4f0

Browse files
committed
[MemProf] Context disambiguation cloning pass [patch 1a/3]
Support for building, printing, and displaying CallsiteContextGraph which represents the MemProf metadata contexts. Uses CRTP to enable support for both IR (regular LTO) and summary (ThinLTO). This patch includes the support for building it in regular LTO mode (from memprof and callsite metadata), and the next patch will add the handling for building it from ThinLTO summaries. Also includes support for dumping the graph to text and to dot files. Follow-on patches will contain the support for cloning on the graph and in the IR. The graph represents the call contexts in all memprof metadata on allocation calls, with nodes for the allocations themselves, as well as for the calls in each context. The graph is initially built from the allocation memprof metadata (or summary) MIBs. It is then updated to match calls with callsite metadata onto the nodes, updating it to reflect any inlining performed on those calls. Each MIB (representing an allocation's call context with allocation behavior) is assigned a unique context id during the graph build. The edges and nodes in the graph are decorated with the context ids they carry. This is used to correctly update the graph when cloning is performed so that we can uniquify the context for a single (possibly cloned) allocation. Depends on D140786. Differential Revision: https://reviews.llvm.org/D140908
1 parent ee5617d commit d6ad4f0

14 files changed

+3037
-184
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//==- MemProfContextDisambiguation.h - Context Disambiguation ----*- C++ -*-==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implements support for context disambiguation of allocation calls for profile
10+
// guided heap optimization using memprof metadata. See implementation file for
11+
// details.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
16+
#define LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H
17+
18+
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/ADT/StringSet.h"
20+
#include "llvm/IR/GlobalValue.h"
21+
#include "llvm/IR/PassManager.h"
22+
23+
namespace llvm {
24+
class Module;
25+
26+
class MemProfContextDisambiguation
27+
: public PassInfoMixin<MemProfContextDisambiguation> {
28+
/// Run the context disambiguator on \p M, returns true if any changes made.
29+
bool processModule(Module &M);
30+
31+
public:
32+
MemProfContextDisambiguation() {}
33+
34+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
35+
};
36+
} // end namespace llvm
37+
38+
#endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
#include "llvm/Transforms/IPO/Internalize.h"
118118
#include "llvm/Transforms/IPO/LoopExtractor.h"
119119
#include "llvm/Transforms/IPO/LowerTypeTests.h"
120+
#include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
120121
#include "llvm/Transforms/IPO/MergeFunctions.h"
121122
#include "llvm/Transforms/IPO/ModuleInliner.h"
122123
#include "llvm/Transforms/IPO/OpenMPOpt.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
5858
#include "llvm/Transforms/IPO/Inliner.h"
5959
#include "llvm/Transforms/IPO/LowerTypeTests.h"
60+
#include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
6061
#include "llvm/Transforms/IPO/MergeFunctions.h"
6162
#include "llvm/Transforms/IPO/ModuleInliner.h"
6263
#include "llvm/Transforms/IPO/OpenMPOpt.h"
@@ -271,6 +272,10 @@ static cl::opt<AttributorRunOption> AttributorRun(
271272
clEnumValN(AttributorRunOption::NONE, "none",
272273
"disable attributor runs")));
273274

275+
cl::opt<bool> EnableMemProfContextDisambiguation(
276+
"enable-memprof-context-disambiguation", cl::init(false), cl::Hidden,
277+
cl::ZeroOrMore, cl::desc("Enable MemProf context disambiguation"));
278+
274279
PipelineTuningOptions::PipelineTuningOptions() {
275280
LoopInterleaving = true;
276281
LoopVectorization = true;
@@ -1709,6 +1714,12 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
17091714
InlineContext{ThinOrFullLTOPhase::FullLTOPostLink,
17101715
InlinePass::CGSCCInliner}));
17111716

1717+
// Perform context disambiguation after inlining, since that would reduce the
1718+
// amount of additional cloning required to distinguish the allocation
1719+
// contexts.
1720+
if (EnableMemProfContextDisambiguation)
1721+
MPM.addPass(MemProfContextDisambiguation());
1722+
17121723
// Optimize globals again after we ran the inliner.
17131724
MPM.addPass(GlobalOptPass());
17141725

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
8787
MODULE_PASS("no-op-module", NoOpModulePass())
8888
MODULE_PASS("objc-arc-apelim", ObjCARCAPElimPass())
8989
MODULE_PASS("partial-inliner", PartialInlinerPass())
90+
MODULE_PASS("memprof-context-disambiguation", MemProfContextDisambiguation())
9091
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
9192
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
9293
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())

llvm/lib/Transforms/IPO/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_llvm_component_library(LLVMipo
2727
Internalize.cpp
2828
LoopExtractor.cpp
2929
LowerTypeTests.cpp
30+
MemProfContextDisambiguation.cpp
3031
MergeFunctions.cpp
3132
ModuleInliner.cpp
3233
OpenMPOpt.cpp

0 commit comments

Comments
 (0)