Skip to content

Commit 3635cb0

Browse files
authored
Merge pull request #2017 from lhames/swift-stable-jit-updates
Swift stable jit updates
2 parents cd56dfb + 68400f3 commit 3635cb0

File tree

226 files changed

+6132
-13338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+6132
-13338
lines changed

llvm/docs/tutorial/BuildingAJIT4.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
===========================================================================
2-
Building a JIT: Extreme Laziness - Using Compile Callbacks to JIT from ASTs
3-
===========================================================================
1+
=======================================================================
2+
Building a JIT: Extreme Laziness - Using LazyReexports to JIT from ASTs
3+
=======================================================================
44

55
.. contents::
66
:local:
@@ -13,9 +13,9 @@ Chapter 4 Introduction
1313
======================
1414

1515
Welcome to Chapter 4 of the "Building an ORC-based JIT in LLVM" tutorial. This
16-
chapter introduces the Compile Callbacks and Indirect Stubs APIs and shows how
17-
they can be used to replace the CompileOnDemand layer from
18-
`Chapter 3 <BuildingAJIT3.html>`_ with a custom lazy-JITing scheme that JITs
16+
chapter introduces custom MaterializationUnits and Layers, and the lazy
17+
reexports API. Together these will be used to replace the CompileOnDemandLayer
18+
from `Chapter 3 <BuildingAJIT3.html>`_ with a custom lazy-JITing scheme that JITs
1919
directly from Kaleidoscope ASTs.
2020

2121
**To be done:**

llvm/docs/tutorial/BuildingAJIT5.rst

Lines changed: 0 additions & 57 deletions
This file was deleted.

llvm/examples/Kaleidoscope/BuildingAJIT/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ add_subdirectory(Chapter2)
33
add_subdirectory(Chapter3)
44
add_subdirectory(Chapter4)
55

6-
if (NOT WIN32)
7-
add_subdirectory(Chapter5)
8-
endif()
6+
# if (NOT WIN32)
7+
# add_subdirectory(Chapter5)
8+
# endif()

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

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
2222
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
2323
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
24+
#include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
2425
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
2526
#include "llvm/IR/DataLayout.h"
2627
#include "llvm/IR/LLVMContext.h"
@@ -31,53 +32,67 @@ namespace orc {
3132

3233
class KaleidoscopeJIT {
3334
private:
34-
ExecutionSession ES;
35-
RTDyldObjectLinkingLayer ObjectLayer;
36-
IRCompileLayer CompileLayer;
35+
std::unique_ptr<TargetProcessControl> TPC;
36+
std::unique_ptr<ExecutionSession> ES;
3737

3838
DataLayout DL;
3939
MangleAndInterner Mangle;
40-
ThreadSafeContext Ctx;
40+
41+
RTDyldObjectLinkingLayer ObjectLayer;
42+
IRCompileLayer CompileLayer;
4143

4244
JITDylib &MainJD;
4345

4446
public:
45-
KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
46-
: ObjectLayer(ES,
47+
KaleidoscopeJIT(std::unique_ptr<TargetProcessControl> TPC,
48+
std::unique_ptr<ExecutionSession> ES,
49+
JITTargetMachineBuilder JTMB, DataLayout DL)
50+
: TPC(std::move(TPC)), ES(std::move(ES)), DL(std::move(DL)),
51+
Mangle(*this->ES, this->DL),
52+
ObjectLayer(*this->ES,
4753
[]() { return std::make_unique<SectionMemoryManager>(); }),
48-
CompileLayer(ES, ObjectLayer,
54+
CompileLayer(*this->ES, ObjectLayer,
4955
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
50-
DL(std::move(DL)), Mangle(ES, this->DL),
51-
Ctx(std::make_unique<LLVMContext>()),
52-
MainJD(ES.createBareJITDylib("<main>")) {
56+
MainJD(this->ES->createBareJITDylib("<main>")) {
5357
MainJD.addGenerator(
5458
cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
5559
DL.getGlobalPrefix())));
5660
}
5761

62+
~KaleidoscopeJIT() {
63+
if (auto Err = ES->endSession())
64+
ES->reportError(std::move(Err));
65+
}
66+
5867
static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
59-
auto JTMB = JITTargetMachineBuilder::detectHost();
68+
auto TPC = SelfTargetProcessControl::Create();
69+
if (!TPC)
70+
return TPC.takeError();
71+
72+
auto ES = std::make_unique<ExecutionSession>();
6073

61-
if (!JTMB)
62-
return JTMB.takeError();
74+
JITTargetMachineBuilder JTMB((*TPC)->getTargetTriple());
6375

64-
auto DL = JTMB->getDefaultDataLayoutForTarget();
76+
auto DL = JTMB.getDefaultDataLayoutForTarget();
6577
if (!DL)
6678
return DL.takeError();
6779

68-
return std::make_unique<KaleidoscopeJIT>(std::move(*JTMB), std::move(*DL));
80+
return std::make_unique<KaleidoscopeJIT>(std::move(*TPC), std::move(ES),
81+
std::move(JTMB), std::move(*DL));
6982
}
7083

7184
const DataLayout &getDataLayout() const { return DL; }
7285

73-
LLVMContext &getContext() { return *Ctx.getContext(); }
86+
JITDylib &getMainJITDylib() { return MainJD; }
7487

75-
Error addModule(std::unique_ptr<Module> M) {
76-
return CompileLayer.add(MainJD, ThreadSafeModule(std::move(M), Ctx));
88+
Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {
89+
if (!RT)
90+
RT = MainJD.getDefaultResourceTracker();
91+
return CompileLayer.add(RT, std::move(TSM));
7792
}
7893

7994
Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
80-
return ES.lookup({&MainJD}, Mangle(Name.str()));
95+
return ES->lookup({&MainJD}, Mangle(Name.str()));
8196
}
8297
};
8398

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,11 @@ static std::unique_ptr<FunctionAST> ParseDefinition() {
676676
}
677677

678678
/// toplevelexpr ::= expression
679-
static std::unique_ptr<FunctionAST> ParseTopLevelExpr(unsigned ExprCount) {
679+
static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
680680
if (auto E = ParseExpression()) {
681681
// Make an anonymous proto.
682-
auto Proto = std::make_unique<PrototypeAST>
683-
(("__anon_expr" + Twine(ExprCount)).str(), std::vector<std::string>());
682+
auto Proto = std::make_unique<PrototypeAST>("__anon_expr",
683+
std::vector<std::string>());
684684
return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
685685
}
686686
return nullptr;
@@ -697,7 +697,7 @@ static std::unique_ptr<PrototypeAST> ParseExtern() {
697697
//===----------------------------------------------------------------------===//
698698

699699
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
700-
static LLVMContext *TheContext;
700+
static std::unique_ptr<LLVMContext> TheContext;
701701
static std::unique_ptr<IRBuilder<>> Builder;
702702
static std::unique_ptr<Module> TheModule;
703703
static std::map<std::string, AllocaInst *> NamedValues;
@@ -1102,7 +1102,8 @@ Function *FunctionAST::codegen() {
11021102
//===----------------------------------------------------------------------===//
11031103

11041104
static void InitializeModule() {
1105-
// Open a new module.
1105+
// Open a new context and module.
1106+
TheContext = std::make_unique<LLVMContext>();
11061107
TheModule = std::make_unique<Module>("my cool jit", *TheContext);
11071108
TheModule->setDataLayout(TheJIT->getDataLayout());
11081109

@@ -1116,7 +1117,8 @@ static void HandleDefinition() {
11161117
fprintf(stderr, "Read function definition:");
11171118
FnIR->print(errs());
11181119
fprintf(stderr, "\n");
1119-
ExitOnErr(TheJIT->addModule(std::move(TheModule)));
1120+
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
1121+
ExitOnErr(TheJIT->addModule(std::move(TSM)));
11201122
InitializeModule();
11211123
}
11221124
} else {
@@ -1140,27 +1142,27 @@ static void HandleExtern() {
11401142
}
11411143

11421144
static void HandleTopLevelExpression() {
1143-
static unsigned ExprCount = 0;
1144-
1145-
// Update ExprCount. This number will be added to anonymous expressions to
1146-
// prevent them from clashing.
1147-
++ExprCount;
1148-
11491145
// Evaluate a top-level expression into an anonymous function.
1150-
if (auto FnAST = ParseTopLevelExpr(ExprCount)) {
1146+
if (auto FnAST = ParseTopLevelExpr()) {
11511147
if (FnAST->codegen()) {
1152-
// JIT the module containing the anonymous expression, keeping a handle so
1153-
// we can free it later.
1154-
ExitOnErr(TheJIT->addModule(std::move(TheModule)));
1148+
// Create a ResourceTracker to track JIT'd memory allocated to our
1149+
// anonymous expression -- that way we can free it after executing.
1150+
auto RT = TheJIT->getMainJITDylib().createResourceTracker();
1151+
1152+
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
1153+
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
11551154
InitializeModule();
11561155

11571156
// Get the anonymous expression's JITSymbol.
1158-
auto Sym =
1159-
ExitOnErr(TheJIT->lookup(("__anon_expr" + Twine(ExprCount)).str()));
1157+
auto Sym = ExitOnErr(TheJIT->lookup("__anon_expr"));
11601158

1159+
// Get the symbol's address and cast it to the right type (takes no
1160+
// arguments, returns a double) so we can call it as a native function.
11611161
auto *FP = (double (*)())(intptr_t)Sym.getAddress();
1162-
assert(FP && "Failed to codegen function");
11631162
fprintf(stderr, "Evaluated to %f\n", FP());
1163+
1164+
// Delete the anonymous expression module from the JIT.
1165+
ExitOnErr(RT->remove());
11641166
}
11651167
} else {
11661168
// Skip token for error recovery.
@@ -1229,8 +1231,6 @@ int main() {
12291231
getNextToken();
12301232

12311233
TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
1232-
TheContext = &TheJIT->getContext();
1233-
12341234
InitializeModule();
12351235

12361236
// Run the main "interpreter loop" now.

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

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
2323
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
2424
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25+
#include "llvm/ExecutionEngine/Orc/TargetProcessControl.h"
2526
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
2627
#include "llvm/IR/DataLayout.h"
2728
#include "llvm/IR/LLVMContext.h"
@@ -36,54 +37,70 @@ namespace orc {
3637

3738
class KaleidoscopeJIT {
3839
private:
39-
ExecutionSession ES;
40-
RTDyldObjectLinkingLayer ObjectLayer;
41-
IRCompileLayer CompileLayer;
42-
IRTransformLayer OptimizeLayer;
40+
std::unique_ptr<TargetProcessControl> TPC;
41+
std::unique_ptr<ExecutionSession> ES;
4342

4443
DataLayout DL;
4544
MangleAndInterner Mangle;
46-
ThreadSafeContext Ctx;
45+
46+
RTDyldObjectLinkingLayer ObjectLayer;
47+
IRCompileLayer CompileLayer;
48+
IRTransformLayer OptimizeLayer;
4749

4850
JITDylib &MainJD;
4951

5052
public:
51-
KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
52-
: ObjectLayer(ES,
53+
KaleidoscopeJIT(std::unique_ptr<TargetProcessControl> TPC,
54+
std::unique_ptr<ExecutionSession> ES,
55+
JITTargetMachineBuilder JTMB, DataLayout DL)
56+
: TPC(std::move(TPC)), ES(std::move(ES)), DL(std::move(DL)),
57+
Mangle(*this->ES, this->DL),
58+
ObjectLayer(*this->ES,
5359
[]() { return std::make_unique<SectionMemoryManager>(); }),
54-
CompileLayer(ES, ObjectLayer,
60+
CompileLayer(*this->ES, ObjectLayer,
5561
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
56-
OptimizeLayer(ES, CompileLayer, optimizeModule), DL(std::move(DL)),
57-
Mangle(ES, this->DL), Ctx(std::make_unique<LLVMContext>()),
58-
MainJD(ES.createBareJITDylib("<main>")) {
62+
OptimizeLayer(*this->ES, CompileLayer, optimizeModule),
63+
MainJD(this->ES->createBareJITDylib("<main>")) {
5964
MainJD.addGenerator(
6065
cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
6166
DL.getGlobalPrefix())));
6267
}
6368

64-
const DataLayout &getDataLayout() const { return DL; }
65-
66-
LLVMContext &getContext() { return *Ctx.getContext(); }
69+
~KaleidoscopeJIT() {
70+
if (auto Err = ES->endSession())
71+
ES->reportError(std::move(Err));
72+
}
6773

6874
static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
69-
auto JTMB = JITTargetMachineBuilder::detectHost();
75+
auto TPC = SelfTargetProcessControl::Create();
76+
if (!TPC)
77+
return TPC.takeError();
7078

71-
if (!JTMB)
72-
return JTMB.takeError();
79+
auto ES = std::make_unique<ExecutionSession>();
7380

74-
auto DL = JTMB->getDefaultDataLayoutForTarget();
81+
JITTargetMachineBuilder JTMB((*TPC)->getTargetTriple());
82+
83+
auto DL = JTMB.getDefaultDataLayoutForTarget();
7584
if (!DL)
7685
return DL.takeError();
7786

78-
return std::make_unique<KaleidoscopeJIT>(std::move(*JTMB), std::move(*DL));
87+
return std::make_unique<KaleidoscopeJIT>(std::move(*TPC), std::move(ES),
88+
std::move(JTMB), std::move(*DL));
7989
}
8090

81-
Error addModule(std::unique_ptr<Module> M) {
82-
return OptimizeLayer.add(MainJD, ThreadSafeModule(std::move(M), Ctx));
91+
const DataLayout &getDataLayout() const { return DL; }
92+
93+
JITDylib &getMainJITDylib() { return MainJD; }
94+
95+
Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {
96+
if (!RT)
97+
RT = MainJD.getDefaultResourceTracker();
98+
99+
return OptimizeLayer.add(RT, std::move(TSM));
83100
}
84101

85102
Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
86-
return ES.lookup({&MainJD}, Mangle(Name.str()));
103+
return ES->lookup({&MainJD}, Mangle(Name.str()));
87104
}
88105

89106
private:

0 commit comments

Comments
 (0)