Skip to content

Commit 6059562

Browse files
committed
[ORC] Introduce IRPartitionLayer for common partition functionality.
1 parent 7f4db32 commit 6059562

File tree

10 files changed

+417
-322
lines changed

10 files changed

+417
-322
lines changed

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
2222
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
2323
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
24+
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
2425
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2526
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
2627
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
@@ -48,6 +49,7 @@ class KaleidoscopeJIT {
4849
RTDyldObjectLinkingLayer ObjectLayer;
4950
IRCompileLayer CompileLayer;
5051
IRTransformLayer OptimizeLayer;
52+
IRPartitionLayer IPLayer;
5153
CompileOnDemandLayer CODLayer;
5254

5355
JITDylib &MainJD;
@@ -68,8 +70,8 @@ class KaleidoscopeJIT {
6870
CompileLayer(*this->ES, ObjectLayer,
6971
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
7072
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
71-
CODLayer(*this->ES, OptimizeLayer,
72-
this->EPCIU->getLazyCallThroughManager(),
73+
IPLayer(*this->ES, OptimizeLayer),
74+
CODLayer(*this->ES, IPLayer, this->EPCIU->getLazyCallThroughManager(),
7375
[this] { return this->EPCIU->createIndirectStubsManager(); }),
7476
MainJD(this->ES->createBareJITDylib("<main>")) {
7577
MainJD.addGenerator(

llvm/examples/SpeculativeJIT/SpeculativeJIT.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "llvm/ExecutionEngine/Orc/Core.h"
44
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
55
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
6+
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
67
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
78
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
89
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
@@ -107,13 +108,14 @@ class SpeculativeJIT {
107108
IndirectStubsManagerBuilderFunction ISMBuilder,
108109
std::unique_ptr<DynamicLibrarySearchGenerator> ProcessSymbolsGenerator)
109110
: ES(std::move(ES)), DL(std::move(DL)),
110-
MainJD(this->ES->createBareJITDylib("<main>")), LCTMgr(std::move(LCTMgr)),
111+
MainJD(this->ES->createBareJITDylib("<main>")),
112+
LCTMgr(std::move(LCTMgr)),
111113
CompileLayer(*this->ES, ObjLayer,
112114
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
113115
S(Imps, *this->ES),
114116
SpeculateLayer(*this->ES, CompileLayer, S, Mangle, BlockFreqQuery()),
115-
CODLayer(*this->ES, SpeculateLayer, *this->LCTMgr,
116-
std::move(ISMBuilder)) {
117+
IPLayer(*this->ES, SpeculateLayer),
118+
CODLayer(*this->ES, IPLayer, *this->LCTMgr, std::move(ISMBuilder)) {
117119
MainJD.addGenerator(std::move(ProcessSymbolsGenerator));
118120
this->CODLayer.setImplMap(&Imps);
119121
this->ES->setDispatchTask(
@@ -147,6 +149,7 @@ class SpeculativeJIT {
147149
Speculator S;
148150
RTDyldObjectLinkingLayer ObjLayer{*ES, createMemMgr};
149151
IRSpeculationLayer SpeculateLayer;
152+
IRPartitionLayer IPLayer;
150153
CompileOnDemandLayer CODLayer;
151154
};
152155

llvm/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,15 @@ namespace llvm {
5454
namespace orc {
5555

5656
class CompileOnDemandLayer : public IRLayer {
57-
friend class PartitioningIRMaterializationUnit;
58-
5957
public:
6058
/// Builder for IndirectStubsManagers.
6159
using IndirectStubsManagerBuilder =
6260
std::function<std::unique_ptr<IndirectStubsManager>()>;
6361

64-
using GlobalValueSet = std::set<const GlobalValue *>;
65-
66-
/// Partitioning function.
67-
using PartitionFunction =
68-
std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>;
69-
70-
/// Off-the-shelf partitioning which compiles all requested symbols (usually
71-
/// a single function at a time).
72-
static std::optional<GlobalValueSet>
73-
compileRequested(GlobalValueSet Requested);
74-
75-
/// Off-the-shelf partitioning which compiles whole modules whenever any
76-
/// symbol in them is requested.
77-
static std::optional<GlobalValueSet>
78-
compileWholeModule(GlobalValueSet Requested);
79-
8062
/// Construct a CompileOnDemandLayer.
8163
CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer,
82-
LazyCallThroughManager &LCTMgr,
83-
IndirectStubsManagerBuilder BuildIndirectStubsManager);
84-
85-
/// Sets the partition function.
86-
void setPartitionFunction(PartitionFunction Partition);
87-
64+
LazyCallThroughManager &LCTMgr,
65+
IndirectStubsManagerBuilder BuildIndirectStubsManager);
8866
/// Sets the ImplSymbolMap
8967
void setImplMap(ImplSymbolMap *Imp);
9068

@@ -111,22 +89,12 @@ class CompileOnDemandLayer : public IRLayer {
11189

11290
PerDylibResources &getPerDylibResources(JITDylib &TargetD);
11391

114-
void cleanUpModule(Module &M);
115-
116-
void expandPartition(GlobalValueSet &Partition);
117-
118-
void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
119-
ThreadSafeModule TSM,
120-
IRMaterializationUnit::SymbolNameToDefinitionMap Defs);
121-
12292
mutable std::mutex CODLayerMutex;
12393

12494
IRLayer &BaseLayer;
12595
LazyCallThroughManager &LCTMgr;
12696
IndirectStubsManagerBuilder BuildIndirectStubsManager;
12797
PerDylibResourcesMap DylibResources;
128-
PartitionFunction Partition = compileRequested;
129-
SymbolLinkagePromoter PromoteSymbols;
13098
ImplSymbolMap *AliaseeImpls = nullptr;
13199
};
132100

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===- IRPartitionLayer.h - Partition IR module on lookup -------*- 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+
// JIT layer for breaking up modules into smaller submodules that only contains
10+
// looked up symbols.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H
15+
#define LLVM_EXECUTIONENGINE_ORC_IRPARTITIONLAYER_H
16+
17+
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
18+
#include "llvm/ExecutionEngine/Orc/Layer.h"
19+
#include "llvm/IR/Attributes.h"
20+
#include "llvm/IR/Constant.h"
21+
#include "llvm/IR/Constants.h"
22+
#include "llvm/IR/DataLayout.h"
23+
#include "llvm/IR/Function.h"
24+
#include "llvm/IR/GlobalAlias.h"
25+
#include "llvm/IR/GlobalValue.h"
26+
#include "llvm/IR/GlobalVariable.h"
27+
#include "llvm/IR/Instruction.h"
28+
#include "llvm/IR/Mangler.h"
29+
#include "llvm/IR/Module.h"
30+
#include "llvm/IR/Type.h"
31+
32+
namespace llvm {
33+
namespace orc {
34+
35+
/// A layer that breaks up IR modules into smaller submodules that only contains
36+
/// looked up symbols.
37+
class IRPartitionLayer : public IRLayer {
38+
friend class PartitioningIRMaterializationUnit;
39+
40+
public:
41+
using GlobalValueSet = std::set<const GlobalValue *>;
42+
43+
/// Partitioning function.
44+
using PartitionFunction =
45+
std::function<std::optional<GlobalValueSet>(GlobalValueSet Requested)>;
46+
47+
/// Construct a IRPartitionLayer.
48+
IRPartitionLayer(ExecutionSession &ES, IRLayer &BaseLayer);
49+
50+
/// Off-the-shelf partitioning which compiles all requested symbols (usually
51+
/// a single function at a time).
52+
static std::optional<GlobalValueSet>
53+
compileRequested(GlobalValueSet Requested);
54+
55+
/// Off-the-shelf partitioning which compiles whole modules whenever any
56+
/// symbol in them is requested.
57+
static std::optional<GlobalValueSet>
58+
compileWholeModule(GlobalValueSet Requested);
59+
60+
/// Sets the partition function.
61+
void setPartitionFunction(PartitionFunction Partition);
62+
63+
/// Emits the given module. This should not be called by clients: it will be
64+
/// called by the JIT when a definition added via the add method is requested.
65+
void emit(std::unique_ptr<MaterializationResponsibility> R,
66+
ThreadSafeModule TSM) override;
67+
68+
private:
69+
void cleanUpModule(Module &M);
70+
71+
void expandPartition(GlobalValueSet &Partition);
72+
73+
void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
74+
ThreadSafeModule TSM,
75+
IRMaterializationUnit::SymbolNameToDefinitionMap Defs);
76+
77+
IRLayer &BaseLayer;
78+
PartitionFunction Partition = compileRequested;
79+
SymbolLinkagePromoter PromoteSymbols;
80+
};
81+
82+
} // namespace orc
83+
} // namespace llvm
84+
85+
#endif

llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
1818
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
1919
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
20+
#include "llvm/ExecutionEngine/Orc/IRPartitionLayer.h"
2021
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2122
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
2223
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
@@ -271,9 +272,8 @@ class LLLazyJIT : public LLJIT {
271272
public:
272273

273274
/// Sets the partition function.
274-
void
275-
setPartitionFunction(CompileOnDemandLayer::PartitionFunction Partition) {
276-
CODLayer->setPartitionFunction(std::move(Partition));
275+
void setPartitionFunction(IRPartitionLayer::PartitionFunction Partition) {
276+
IPLayer->setPartitionFunction(std::move(Partition));
277277
}
278278

279279
/// Returns a reference to the on-demand layer.
@@ -293,6 +293,7 @@ class LLLazyJIT : public LLJIT {
293293
LLLazyJIT(LLLazyJITBuilderState &S, Error &Err);
294294

295295
std::unique_ptr<LazyCallThroughManager> LCTMgr;
296+
std::unique_ptr<IRPartitionLayer> IPLayer;
296297
std::unique_ptr<CompileOnDemandLayer> CODLayer;
297298
};
298299

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_llvm_component_library(LLVMOrcJIT
2727
IndirectionUtils.cpp
2828
IRCompileLayer.cpp
2929
IRTransformLayer.cpp
30+
IRPartitionLayer.cpp
3031
JITTargetMachineBuilder.cpp
3132
LazyReexports.cpp
3233
Layer.cpp

0 commit comments

Comments
 (0)