summaryrefslogtreecommitdiffstats
path: root/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2018-10-18 00:51:38 +0000
committerLang Hames <lhames@gmail.com>2018-10-18 00:51:38 +0000
commit33a2f5e8ddd0edc9b07c93e4f924d1ceca8badbe (patch)
treef51aef73d81c5410bb6ee603f23a9b80856d6b11 /llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1
parent60b2e054dcc730e6ab9d9e6c1a159a72fd7ab3cb (diff)
downloadbcm5719-llvm-33a2f5e8ddd0edc9b07c93e4f924d1ceca8badbe.tar.gz
bcm5719-llvm-33a2f5e8ddd0edc9b07c93e4f924d1ceca8badbe.zip
[BuildingAJIT] Update the Ch1 KaleidoscopeJIT class to expose errors to clients.
Returning the error to clients provides an opportunity to introduce readers to the Expected and Error APIs and makes the tutorial more useful as a starting point for a real JIT class, while only slightly complicating the code. llvm-svn: 344720
Diffstat (limited to 'llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1')
-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