aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/BrainF/BrainFDriver.cpp2
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter1/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h57
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h64
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h69
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h69
-rw-r--r--examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h74
-rw-r--r--examples/Kaleidoscope/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter4/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter5/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter6/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter7/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter9/CMakeLists.txt1
-rw-r--r--examples/Kaleidoscope/Chapter9/toy.cpp5
-rw-r--r--examples/Kaleidoscope/include/KaleidoscopeJIT.h56
-rw-r--r--examples/ModuleMaker/ModuleMaker.cpp2
-rw-r--r--examples/ParallelJIT/CMakeLists.txt2
18 files changed, 214 insertions, 194 deletions
diff --git a/examples/BrainF/BrainFDriver.cpp b/examples/BrainF/BrainFDriver.cpp
index 65f8033a7e2..24aac4aeb1d 100644
--- a/examples/BrainF/BrainFDriver.cpp
+++ b/examples/BrainF/BrainFDriver.cpp
@@ -171,7 +171,7 @@ int main(int argc, char **argv) {
// is unmanageable because stdout linkage name depends on stdlib implementation.
fflush(stdout);
} else {
- WriteBitcodeToFile(Mod.get(), *out);
+ WriteBitcodeToFile(*Mod, *out);
}
//Clean up
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/CMakeLists.txt b/examples/Kaleidoscope/BuildingAJIT/Chapter1/CMakeLists.txt
index 657a14be87d..72c9668f7d3 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/CMakeLists.txt
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
index ab675e3f742..a7eb1db8625 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
@@ -38,45 +38,46 @@ namespace orc {
class KaleidoscopeJIT {
private:
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
RTDyldObjectLinkingLayer ObjectLayer;
IRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
public:
- using ModuleHandle = decltype(CompileLayer)::ModuleHandleT;
-
KaleidoscopeJIT()
- : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
- ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
+ : ES(SSP),
+ Resolver(createLegacyLookupResolver(
+ [this](const std::string &Name) -> JITSymbol {
+ if (auto Sym = CompileLayer.findSymbol(Name, false))
+ return Sym;
+ else if (auto Err = Sym.takeError())
+ return std::move(Err);
+ if (auto SymAddr =
+ RTDyldMemoryManager::getSymbolAddressInProcess(Name))
+ return JITSymbol(SymAddr, JITSymbolFlags::Exported);
+ return nullptr;
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
+ TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
+ ObjectLayer(ES,
+ [this](VModuleKey) {
+ return RTDyldObjectLinkingLayer::Resources{
+ std::make_shared<SectionMemoryManager>(), Resolver};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandle addModule(std::unique_ptr<Module> M) {
- // Build our symbol resolver:
- // Lambda 1: Look back into the JIT itself to find symbols that are part of
- // the same "logical dylib".
- // Lambda 2: Search for external symbols in the host process.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = CompileLayer.findSymbol(Name, false))
- return Sym;
- return JITSymbol(nullptr);
- },
- [](const std::string &Name) {
- if (auto SymAddr =
- RTDyldMemoryManager::getSymbolAddressInProcess(Name))
- return JITSymbol(SymAddr, JITSymbolFlags::Exported);
- return JITSymbol(nullptr);
- });
-
- // Add the set to the JIT with the resolver we created above and a newly
- // created SectionMemoryManager.
- return cantFail(CompileLayer.addModule(std::move(M),
- std::move(Resolver)));
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ // Add the module to the JIT with a new VModuleKey.
+ auto K = ES.allocateVModule();
+ cantFail(CompileLayer.addModule(K, std::move(M)));
+ return K;
}
JITSymbol findSymbol(const std::string Name) {
@@ -90,8 +91,8 @@ public:
return cantFail(findSymbol(Name).getAddress());
}
- void removeModule(ModuleHandle H) {
- cantFail(CompileLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ cantFail(CompileLayer.removeModule(K));
}
};
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt b/examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt
index ea5bc05fa00..ba6abd72d42 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
index 9a295f1566c..b6323b8963b 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
@@ -42,6 +42,9 @@ namespace orc {
class KaleidoscopeJIT {
private:
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
RTDyldObjectLinkingLayer ObjectLayer;
@@ -53,43 +56,40 @@ private:
IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer;
public:
- using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
-
KaleidoscopeJIT()
- : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
- ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
+ : ES(SSP),
+ Resolver(createLegacyLookupResolver(
+ [this](const std::string &Name) -> JITSymbol {
+ if (auto Sym = OptimizeLayer.findSymbol(Name, false))
+ return Sym;
+ else if (auto Err = Sym.takeError())
+ return std::move(Err);
+ if (auto SymAddr =
+ RTDyldMemoryManager::getSymbolAddressInProcess(Name))
+ return JITSymbol(SymAddr, JITSymbolFlags::Exported);
+ return nullptr;
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
+ TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
+ ObjectLayer(ES,
+ [this](VModuleKey) {
+ return RTDyldObjectLinkingLayer::Resources{
+ std::make_shared<SectionMemoryManager>(), Resolver};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
- OptimizeLayer(CompileLayer,
- [this](std::shared_ptr<Module> M) {
- return optimizeModule(std::move(M));
- }) {
+ OptimizeLayer(CompileLayer, [this](std::shared_ptr<Module> M) {
+ return optimizeModule(std::move(M));
+ }) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandle addModule(std::unique_ptr<Module> M) {
- // Build our symbol resolver:
- // Lambda 1: Look back into the JIT itself to find symbols that are part of
- // the same "logical dylib".
- // Lambda 2: Search for external symbols in the host process.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = OptimizeLayer.findSymbol(Name, false))
- return Sym;
- return JITSymbol(nullptr);
- },
- [](const std::string &Name) {
- if (auto SymAddr =
- RTDyldMemoryManager::getSymbolAddressInProcess(Name))
- return JITSymbol(SymAddr, JITSymbolFlags::Exported);
- return JITSymbol(nullptr);
- });
-
- // Add the set to the JIT with the resolver we created above and a newly
- // created SectionMemoryManager.
- return cantFail(OptimizeLayer.addModule(std::move(M),
- std::move(Resolver)));
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ // Add the module to the JIT with a new VModuleKey.
+ auto K = ES.allocateVModule();
+ cantFail(OptimizeLayer.addModule(K, std::move(M)));
+ return K;
}
JITSymbol findSymbol(const std::string Name) {
@@ -99,8 +99,8 @@ public:
return OptimizeLayer.findSymbol(MangledNameStream.str(), true);
}
- void removeModule(ModuleHandle H) {
- cantFail(OptimizeLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ cantFail(OptimizeLayer.removeModule(K));
}
private:
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
index a03f5ce5e23..d6d31870002 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
@@ -17,15 +17,15 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
-#include "llvm/ExecutionEngine/RuntimeDyld.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/RuntimeDyld.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Mangler.h"
@@ -35,6 +35,7 @@
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include <algorithm>
+#include <map>
#include <memory>
#include <set>
#include <string>
@@ -45,6 +46,9 @@ namespace orc {
class KaleidoscopeJIT {
private:
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::map<VModuleKey, std::shared_ptr<SymbolResolver>> Resolvers;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
RTDyldObjectLinkingLayer ObjectLayer;
@@ -59,11 +63,14 @@ private:
CompileOnDemandLayer<decltype(OptimizeLayer)> CODLayer;
public:
- using ModuleHandle = decltype(CODLayer)::ModuleHandleT;
-
KaleidoscopeJIT()
- : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
- ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
+ : ES(SSP), TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
+ ObjectLayer(ES,
+ [this](VModuleKey K) {
+ return RTDyldObjectLinkingLayer::Resources{
+ std::make_shared<SectionMemoryManager>(),
+ Resolvers[K]};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {
@@ -71,37 +78,41 @@ public:
}),
CompileCallbackManager(
orc::createLocalCompileCallbackManager(TM->getTargetTriple(), 0)),
- CODLayer(OptimizeLayer,
- [](Function &F) { return std::set<Function*>({&F}); },
+ CODLayer(ES, OptimizeLayer,
+ [&](orc::VModuleKey K) { return Resolvers[K]; },
+ [&](orc::VModuleKey K, std::shared_ptr<SymbolResolver> R) {
+ Resolvers[K] = std::move(R);
+ },
+ [](Function &F) { return std::set<Function *>({&F}); },
*CompileCallbackManager,
orc::createLocalIndirectStubsManagerBuilder(
- TM->getTargetTriple())) {
+ TM->getTargetTriple())) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandle addModule(std::unique_ptr<Module> M) {
- // Build our symbol resolver:
- // Lambda 1: Look back into the JIT itself to find symbols that are part of
- // the same "logical dylib".
- // Lambda 2: Search for external symbols in the host process.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = CODLayer.findSymbol(Name, false))
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ // Create a new VModuleKey.
+ VModuleKey K = ES.allocateVModule();
+
+ // Build a resolver and associate it with the new key.
+ Resolvers[K] = createLegacyLookupResolver(
+ [this](const std::string &Name) -> JITSymbol {
+ if (auto Sym = CompileLayer.findSymbol(Name, false))
return Sym;
- return JITSymbol(nullptr);
- },
- [](const std::string &Name) {
+ else if (auto Err = Sym.takeError())
+ return std::move(Err);
if (auto SymAddr =
- RTDyldMemoryManager::getSymbolAddressInProcess(Name))
+ RTDyldMemoryManager::getSymbolAddressInProcess(Name))
return JITSymbol(SymAddr, JITSymbolFlags::Exported);
- return JITSymbol(nullptr);
- });
+ return nullptr;
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); });
- // Add the set to the JIT with the resolver we created above and a newly
- // created SectionMemoryManager.
- return cantFail(CODLayer.addModule(std::move(M), std::move(Resolver)));
+ // Add the module to the JIT with the new key.
+ cantFail(CODLayer.addModule(K, std::move(M)));
+ return K;
}
JITSymbol findSymbol(const std::string Name) {
@@ -111,8 +122,8 @@ public:
return CODLayer.findSymbol(MangledNameStream.str(), true);
}
- void removeModule(ModuleHandle H) {
- cantFail(CODLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ cantFail(CODLayer.removeModule(K));
}
private:
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
index 841ea74fb98..f3878918065 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
@@ -17,14 +17,14 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
-#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
+#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Mangler.h"
@@ -37,6 +37,7 @@
#include <algorithm>
#include <cassert>
#include <cstdlib>
+#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -71,6 +72,9 @@ namespace orc {
class KaleidoscopeJIT {
private:
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
RTDyldObjectLinkingLayer ObjectLayer;
@@ -85,12 +89,28 @@ private:
std::unique_ptr<IndirectStubsManager> IndirectStubsMgr;
public:
- using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
-
KaleidoscopeJIT()
- : TM(EngineBuilder().selectTarget()),
- DL(TM->createDataLayout()),
- ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
+ : ES(SSP),
+ Resolver(createLegacyLookupResolver(
+ [this](const std::string &Name) -> JITSymbol {
+ if (auto Sym = IndirectStubsMgr->findStub(Name, false))
+ return Sym;
+ if (auto Sym = OptimizeLayer.findSymbol(Name, false))
+ return Sym;
+ else if (auto Err = Sym.takeError())
+ return std::move(Err);
+ if (auto SymAddr =
+ RTDyldMemoryManager::getSymbolAddressInProcess(Name))
+ return JITSymbol(SymAddr, JITSymbolFlags::Exported);
+ return nullptr;
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
+ TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
+ ObjectLayer(ES,
+ [this](VModuleKey K) {
+ return RTDyldObjectLinkingLayer::Resources{
+ std::make_shared<SectionMemoryManager>(), Resolver};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {
@@ -106,30 +126,11 @@ public:
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandle addModule(std::unique_ptr<Module> M) {
- // Build our symbol resolver:
- // Lambda 1: Look back into the JIT itself to find symbols that are part of
- // the same "logical dylib".
- // Lambda 2: Search for external symbols in the host process.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = IndirectStubsMgr->findStub(Name, false))
- return Sym;
- if (auto Sym = OptimizeLayer.findSymbol(Name, false))
- return Sym;
- return JITSymbol(nullptr);
- },
- [](const std::string &Name) {
- if (auto SymAddr =
- RTDyldMemoryManager::getSymbolAddressInProcess(Name))
- return JITSymbol(SymAddr, JITSymbolFlags::Exported);
- return JITSymbol(nullptr);
- });
-
- // Add the set to the JIT with the resolver we created above and a newly
- // created SectionMemoryManager.
- return cantFail(OptimizeLayer.addModule(std::move(M),
- std::move(Resolver)));
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ // Add the module to the JIT with a new VModuleKey.
+ auto K = ES.allocateVModule();
+ cantFail(OptimizeLayer.addModule(K, std::move(M)));
+ return K;
}
Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) {
@@ -194,8 +195,8 @@ public:
return OptimizeLayer.findSymbol(mangle(Name), true);
}
- void removeModule(ModuleHandle H) {
- cantFail(OptimizeLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ cantFail(OptimizeLayer.removeModule(K));
}
private:
diff --git a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
index 8990a67feb7..46638c1f720 100644
--- a/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/BuildingAJIT/Chapter5/KaleidoscopeJIT.h
@@ -15,18 +15,18 @@
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#include "RemoteJITUtils.h"
-#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
-#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
+#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
-#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
+#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Mangler.h"
@@ -39,6 +39,7 @@
#include <algorithm>
#include <cassert>
#include <cstdlib>
+#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -76,6 +77,9 @@ using MyRemote = remote::OrcRemoteTargetClient;
class KaleidoscopeJIT {
private:
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
RTDyldObjectLinkingLayer ObjectLayer;
@@ -91,15 +95,30 @@ private:
MyRemote &Remote;
public:
- using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT;
-
KaleidoscopeJIT(MyRemote &Remote)
- : TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "",
+ : ES(SSP),
+ Resolver(createLegacyLookupResolver(
+ [this](const std::string &Name) -> JITSymbol {
+ if (auto Sym = IndirectStubsMgr->findStub(Name, false))
+ return Sym;
+ if (auto Sym = OptimizeLayer.findSymbol(Name, false))
+ return Sym;
+ else if (auto Err = Sym.takeError())
+ return std::move(Err);
+ if (auto Addr = cantFail(this->Remote.getSymbolAddress(Name)))
+ return JITSymbol(Addr, JITSymbolFlags::Exported);
+ return nullptr;
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
+ TM(EngineBuilder().selectTarget(Triple(Remote.getTargetTriple()), "",
"", SmallVector<std::string, 0>())),
DL(TM->createDataLayout()),
- ObjectLayer([&Remote]() {
- return cantFail(Remote.createRemoteMemoryManager());
- }),
+ ObjectLayer(ES,
+ [this](VModuleKey K) {
+ return RTDyldObjectLinkingLayer::Resources{
+ cantFail(this->Remote.createRemoteMemoryManager()),
+ Resolver};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
OptimizeLayer(CompileLayer,
[this](std::shared_ptr<Module> M) {
@@ -119,34 +138,11 @@ public:
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandle addModule(std::unique_ptr<Module> M) {
- // Build our symbol resolver:
- // Lambda 1: Look back into the JIT itself to find symbols that are part of
- // the same "logical dylib".
- // Lambda 2: Search for external symbols in the host process.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = IndirectStubsMgr->findStub(Name, false))
- return Sym;
- if (auto Sym = OptimizeLayer.findSymbol(Name, false))
- return Sym;
- return JITSymbol(nullptr);
- },
- [&](const std::string &Name) {
- if (auto AddrOrErr = Remote.getSymbolAddress(Name))
- return JITSymbol(*AddrOrErr, JITSymbolFlags::Exported);
- else {
- logAllUnhandledErrors(AddrOrErr.takeError(), errs(),
- "Error resolving remote symbol:");
- exit(1);
- }
- return JITSymbol(nullptr);
- });
-
- // Add the set to the JIT with the resolver we created above and a newly
- // created SectionMemoryManager.
- return cantFail(OptimizeLayer.addModule(std::move(M),
- std::move(Resolver)));
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ // Add the module with a new VModuleKey.
+ auto K = ES.allocateVModule();
+ cantFail(OptimizeLayer.addModule(K, std::move(M)));
+ return K;
}
Error addFunctionAST(std::unique_ptr<FunctionAST> FnAST) {
@@ -215,8 +211,8 @@ public:
return OptimizeLayer.findSymbol(mangle(Name), true);
}
- void removeModule(ModuleHandle H) {
- cantFail(OptimizeLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ cantFail(OptimizeLayer.removeModule(K));
}
private:
diff --git a/examples/Kaleidoscope/CMakeLists.txt b/examples/Kaleidoscope/CMakeLists.txt
index 543b9f73b4f..3822cdd9e1c 100644
--- a/examples/Kaleidoscope/CMakeLists.txt
+++ b/examples/Kaleidoscope/CMakeLists.txt
@@ -14,3 +14,4 @@ add_subdirectory(Chapter5)
add_subdirectory(Chapter6)
add_subdirectory(Chapter7)
add_subdirectory(Chapter8)
+add_subdirectory(Chapter9)
diff --git a/examples/Kaleidoscope/Chapter4/CMakeLists.txt b/examples/Kaleidoscope/Chapter4/CMakeLists.txt
index 89feed143ad..fdc083e0768 100644
--- a/examples/Kaleidoscope/Chapter4/CMakeLists.txt
+++ b/examples/Kaleidoscope/Chapter4/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/Chapter5/CMakeLists.txt b/examples/Kaleidoscope/Chapter5/CMakeLists.txt
index c0ae70654c3..757d901ef52 100644
--- a/examples/Kaleidoscope/Chapter5/CMakeLists.txt
+++ b/examples/Kaleidoscope/Chapter5/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/Chapter6/CMakeLists.txt b/examples/Kaleidoscope/Chapter6/CMakeLists.txt
index 49627f07ddf..ad50928a346 100644
--- a/examples/Kaleidoscope/Chapter6/CMakeLists.txt
+++ b/examples/Kaleidoscope/Chapter6/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/Chapter7/CMakeLists.txt b/examples/Kaleidoscope/Chapter7/CMakeLists.txt
index 69e78be6a62..03220358ab7 100644
--- a/examples/Kaleidoscope/Chapter7/CMakeLists.txt
+++ b/examples/Kaleidoscope/Chapter7/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
ExecutionEngine
InstCombine
Object
+ OrcJIT
RuntimeDyld
ScalarOpts
Support
diff --git a/examples/Kaleidoscope/Chapter9/CMakeLists.txt b/examples/Kaleidoscope/Chapter9/CMakeLists.txt
index a85b2c5e8b3..565a7ffd32e 100644
--- a/examples/Kaleidoscope/Chapter9/CMakeLists.txt
+++ b/examples/Kaleidoscope/Chapter9/CMakeLists.txt
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS
Core
ExecutionEngine
Object
+ OrcJIT
Support
native
)
diff --git a/examples/Kaleidoscope/Chapter9/toy.cpp b/examples/Kaleidoscope/Chapter9/toy.cpp
index 1b13e45ec46..821cf4d25a6 100644
--- a/examples/Kaleidoscope/Chapter9/toy.cpp
+++ b/examples/Kaleidoscope/Chapter9/toy.cpp
@@ -823,7 +823,7 @@ DIType *DebugInfo::getDoubleTy() {
if (DblTy)
return DblTy;
- DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
+ DblTy = DBuilder->createBasicType("double", 64, dwarf::DW_ATE_float);
return DblTy;
}
@@ -1436,7 +1436,8 @@ int main() {
// Currently down as "fib.ks" as a filename since we're redirecting stdin
// but we'd like actual source locations.
KSDbgInfo.TheCU = DBuilder->createCompileUnit(
- dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
+ dwarf::DW_LANG_C, DBuilder->createFile("fib.ks", "."),
+ "Kaleidoscope Compiler", 0, "", 0);
// Run the main "interpreter loop" now.
MainLoop();
diff --git a/examples/Kaleidoscope/include/KaleidoscopeJIT.h b/examples/Kaleidoscope/include/KaleidoscopeJIT.h
index 215ce03af99..3e2fe42a5d8 100644
--- a/examples/Kaleidoscope/include/KaleidoscopeJIT.h
+++ b/examples/Kaleidoscope/include/KaleidoscopeJIT.h
@@ -14,22 +14,23 @@
#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
-#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/iterator_range.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
-#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <algorithm>
+#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -41,38 +42,36 @@ class KaleidoscopeJIT {
public:
using ObjLayerT = RTDyldObjectLinkingLayer;
using CompileLayerT = IRCompileLayer<ObjLayerT, SimpleCompiler>;
- using ModuleHandleT = CompileLayerT::ModuleHandleT;
KaleidoscopeJIT()
- : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
- ObjectLayer([]() { return std::make_shared<SectionMemoryManager>(); }),
+ : ES(SSP),
+ Resolver(createLegacyLookupResolver(
+ [this](const std::string &Name) {
+ return ObjectLayer.findSymbol(Name, true);
+ },
+ [](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })),
+ TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()),
+ ObjectLayer(ES,
+ [this](VModuleKey) {
+ return ObjLayerT::Resources{
+ std::make_shared<SectionMemoryManager>(), Resolver};
+ }),
CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
}
TargetMachine &getTargetMachine() { return *TM; }
- ModuleHandleT addModule(std::unique_ptr<Module> M) {
- // We need a memory manager to allocate memory and resolve symbols for this
- // new module. Create one that resolves symbols by looking back into the
- // JIT.
- auto Resolver = createLambdaResolver(
- [&](const std::string &Name) {
- if (auto Sym = findMangledSymbol(Name))
- return Sym;
- return JITSymbol(nullptr);
- },
- [](const std::string &S) { return nullptr; });
- auto H = cantFail(CompileLayer.addModule(std::move(M),
- std::move(Resolver)));
-
- ModuleHandles.push_back(H);
- return H;
+ VModuleKey addModule(std::unique_ptr<Module> M) {
+ auto K = ES.allocateVModule();
+ cantFail(CompileLayer.addModule(K, std::move(M)));
+ ModuleKeys.push_back(K);
+ return K;
}
- void removeModule(ModuleHandleT H) {
- ModuleHandles.erase(find(ModuleHandles, H));
- cantFail(CompileLayer.removeModule(H));
+ void removeModule(VModuleKey K) {
+ ModuleKeys.erase(find(ModuleKeys, K));
+ cantFail(CompileLayer.removeModule(K));
}
JITSymbol findSymbol(const std::string Name) {
@@ -106,7 +105,7 @@ private:
// Search modules in reverse order: from last added to first added.
// This is the opposite of the usual search order for dlsym, but makes more
// sense in a REPL where we want to bind to the newest available definition.
- for (auto H : make_range(ModuleHandles.rbegin(), ModuleHandles.rend()))
+ for (auto H : make_range(ModuleKeys.rbegin(), ModuleKeys.rend()))
if (auto Sym = CompileLayer.findSymbolIn(H, Name, ExportedSymbolsOnly))
return Sym;
@@ -127,11 +126,14 @@ private:
return nullptr;
}
+ SymbolStringPool SSP;
+ ExecutionSession ES;
+ std::shared_ptr<SymbolResolver> Resolver;
std::unique_ptr<TargetMachine> TM;
const DataLayout DL;
ObjLayerT ObjectLayer;
CompileLayerT CompileLayer;
- std::vector<ModuleHandleT> ModuleHandles;
+ std::vector<VModuleKey> ModuleKeys;
};
} // end namespace orc
diff --git a/examples/ModuleMaker/ModuleMaker.cpp b/examples/ModuleMaker/ModuleMaker.cpp
index 54f014e6fc8..beff40a8d50 100644
--- a/examples/ModuleMaker/ModuleMaker.cpp
+++ b/examples/ModuleMaker/ModuleMaker.cpp
@@ -62,7 +62,7 @@ int main() {
BB->getInstList().push_back(ReturnInst::Create(Context, Add));
// Output the bitcode file to stdout
- WriteBitcodeToFile(M, outs());
+ WriteBitcodeToFile(*M, outs());
// Delete the module and all of its contents.
delete M;
diff --git a/examples/ParallelJIT/CMakeLists.txt b/examples/ParallelJIT/CMakeLists.txt
index deeee072b33..c42dfc85c14 100644
--- a/examples/ParallelJIT/CMakeLists.txt
+++ b/examples/ParallelJIT/CMakeLists.txt
@@ -11,4 +11,4 @@ add_llvm_example(ParallelJIT
ParallelJIT.cpp
)
-target_link_libraries(ParallelJIT ${LLVM_PTHREAD_LIB})
+target_link_libraries(ParallelJIT PRIVATE ${LLVM_PTHREAD_LIB})