summaryrefslogtreecommitdiffstats
path: root/llvm/examples
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/examples')
-rw-r--r--llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h48
-rw-r--r--llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp26
2 files changed, 38 insertions, 36 deletions
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
index d9e320f5478..1df5aff0869 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
@@ -32,37 +32,45 @@ namespace orc {
class KaleidoscopeJIT {
private:
-
ExecutionSession ES;
- RTDyldObjectLinkingLayer ObjectLayer{ES, getMemoryMgr};
- IRCompileLayer CompileLayer{ES, ObjectLayer,
- ConcurrentIRCompiler(getJTMB())};
- DataLayout DL{cantFail(getJTMB().getDefaultDataLayoutForTarget())};
- MangleAndInterner Mangle{ES, DL};
- ThreadSafeContext Ctx{llvm::make_unique<LLVMContext>()};
-
- static JITTargetMachineBuilder getJTMB() {
- return cantFail(JITTargetMachineBuilder::detectHost());
- }
+ RTDyldObjectLinkingLayer ObjectLayer;
+ IRCompileLayer CompileLayer;
- static std::unique_ptr<SectionMemoryManager> getMemoryMgr() {
- return llvm::make_unique<SectionMemoryManager>();
- }
+ DataLayout DL;
+ MangleAndInterner Mangle;
+ ThreadSafeContext Ctx;
public:
-
- KaleidoscopeJIT() {
+ KaleidoscopeJIT(JITTargetMachineBuilder JTMB, DataLayout DL)
+ : ObjectLayer(ES,
+ []() { return llvm::make_unique<SectionMemoryManager>(); }),
+ CompileLayer(ES, ObjectLayer, ConcurrentIRCompiler(std::move(JTMB))),
+ DL(std::move(DL)), Mangle(ES, this->DL),
+ Ctx(llvm::make_unique<LLVMContext>()) {
ES.getMainJITDylib().setGenerator(
- cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
+ cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
+ }
+
+ static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
+ auto JTMB = JITTargetMachineBuilder::detectHost();
+
+ if (!JTMB)
+ return JTMB.takeError();
+
+ auto DL = JTMB->getDefaultDataLayoutForTarget();
+ if (!DL)
+ return DL.takeError();
+
+ return llvm::make_unique<KaleidoscopeJIT>(std::move(*JTMB), std::move(*DL));
}
const DataLayout &getDataLayout() const { return DL; }
LLVMContext &getContext() { return *Ctx.getContext(); }
- void addModule(std::unique_ptr<Module> M) {
- cantFail(CompileLayer.add(ES.getMainJITDylib(),
- ThreadSafeModule(std::move(M), Ctx)));
+ Error addModule(std::unique_ptr<Module> M) {
+ return CompileLayer.add(ES.getMainJITDylib(),
+ ThreadSafeModule(std::move(M), Ctx));
}
Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
index 1d0730f99ef..5a66b367c27 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
@@ -703,6 +703,7 @@ static std::unique_ptr<IRBuilder<>> Builder;
static std::unique_ptr<Module> TheModule;
static std::map<std::string, AllocaInst *> NamedValues;
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
+static ExitOnError ExitOnErr;
Value *LogErrorV(const char *Str) {
LogError(Str);
@@ -1116,7 +1117,7 @@ static void HandleDefinition() {
fprintf(stderr, "Read function definition:");
FnIR->print(errs());
fprintf(stderr, "\n");
- TheJIT->addModule(std::move(TheModule));
+ ExitOnErr(TheJIT->addModule(std::move(TheModule)));
InitializeModule();
}
} else {
@@ -1151,23 +1152,16 @@ static void HandleTopLevelExpression() {
if (FnAST->codegen()) {
// JIT the module containing the anonymous expression, keeping a handle so
// we can free it later.
- TheJIT->addModule(std::move(TheModule));
+ ExitOnErr(TheJIT->addModule(std::move(TheModule)));
InitializeModule();
// Get the anonymous expression's JITSymbol.
- auto Sym = TheJIT->lookup(("__anon_expr" + Twine(ExprCount)).str());
-
- if (Sym) {
- // If the lookup succeeded, cast the symbol's address to a function
- // pointer then call it.
- auto *FP = (double (*)())(intptr_t)Sym->getAddress();
- assert(FP && "Failed to codegen function");
- fprintf(stderr, "Evaluated to %f\n", FP());
- } else {
- // Otherwise log the reason the symbol lookup failed.
- logAllUnhandledErrors(Sym.takeError(), errs(),
- "Could not evaluate: ");
- }
+ auto Sym =
+ ExitOnErr(TheJIT->lookup(("__anon_expr" + Twine(ExprCount)).str()));
+
+ auto *FP = (double (*)())(intptr_t)Sym.getAddress();
+ assert(FP && "Failed to codegen function");
+ fprintf(stderr, "Evaluated to %f\n", FP());
}
} else {
// Skip token for error recovery.
@@ -1235,7 +1229,7 @@ int main() {
fprintf(stderr, "ready> ");
getNextToken();
- TheJIT = llvm::make_unique<KaleidoscopeJIT>();
+ TheJIT = ExitOnErr(KaleidoscopeJIT::Create());
TheContext = &TheJIT->getContext();
InitializeModule();
OpenPOWER on IntegriCloud