From: Lang Hames Date: Mon, 23 Feb 2015 04:45:05 +0000 (+0000) Subject: [Orc][Kaleidoscope] Tidy up the lazy_irgen tutorial, touch up a couple of X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=053ae4ce17cd419a6b2df11837ffc6a34431248c [Orc][Kaleidoscope] Tidy up the lazy_irgen tutorial, touch up a couple of comments in the fully_lazy tutorial to minimize the diff between the two. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230202 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp b/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp index 56123bb41e3..840bf6c7b9a 100644 --- a/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp +++ b/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp @@ -1205,17 +1205,15 @@ public: return LazyEmitLayer.findSymbol(Name, true); } - JITSymbol findSymbol(const std::string &Name) { - return findMangledSymbol(Mangle(Name)); + JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) { + return LazyEmitLayer.findSymbolIn(H, Name, true); } - JITSymbol findMangledSymbolIn(LazyEmitLayerT::ModuleSetHandleT H, - const std::string &Name) { - return LazyEmitLayer.findSymbolIn(H, Name, true); + JITSymbol findSymbol(const std::string &Name) { + return findMangledSymbol(Mangle(Name)); } - JITSymbol findSymbolIn(LazyEmitLayerT::ModuleSetHandleT H, - const std::string &Name) { + JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) { return findMangledSymbolIn(H, Mangle(Name)); } @@ -1236,7 +1234,8 @@ private: // FIXME: What happens if IRGen fails? auto H = irGenStub(std::move(DefI->second)); - // Remove the map entry now that we're done with it. + // Remove the function definition's AST now that we're + // finished with it. FunctionDefs.erase(DefI); // Return the address of the stub. @@ -1300,9 +1299,9 @@ private: CompileLayerT CompileLayer; LazyEmitLayerT LazyEmitLayer; - JITCompileCallbackManager CompileCallbacks; - std::map> FunctionDefs; + + JITCompileCallbackManager CompileCallbacks; }; static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) { diff --git a/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp b/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp index 162b0e58999..3225a0d93b8 100644 --- a/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp +++ b/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp @@ -6,8 +6,8 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/IRBuilder.h" -#include "llvm/IR/LLVMContext.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" #include "llvm/Support/TargetSelect.h" @@ -685,7 +685,6 @@ public: LLVMContext& getLLVMContext() const { return Context; } void addPrototypeAST(std::unique_ptr P); PrototypeAST* getPrototypeAST(const std::string &Name); - std::map> FunctionDefs; private: typedef std::map> PrototypeMap; LLVMContext &Context; @@ -1174,27 +1173,14 @@ public: // 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 MM = createLookasideRTDyldMM( - [&](const std::string &Name) -> uint64_t { + [&](const std::string &Name) { // First try to find 'Name' within the JIT. if (auto Symbol = findMangledSymbol(Name)) return Symbol.getAddress(); - // If we don't find 'Name' in the JIT, see if we have some AST - // for it. - auto DefI = Session.FunctionDefs.find(Name); - if (DefI == Session.FunctionDefs.end()) - return 0; - - // We have AST for 'Name'. IRGen it, add it to the JIT, and - // return the address for it. - // FIXME: What happens if IRGen fails? - addModule(IRGen(Session, *DefI->second)); - - // Remove the function definition's AST now that we've - // finished with it. - Session.FunctionDefs.erase(DefI); - - return findMangledSymbol(Name).getAddress(); + // If we don't already have a definition of 'Name' then search + // the ASTs. + return searchUncompiledASTs(Name); }, [](const std::string &S) { return 0; } ); @@ -1204,15 +1190,43 @@ public: void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); } JITSymbol findMangledSymbol(const std::string &Name) { - return LazyEmitLayer.findSymbol(Name, false); + return LazyEmitLayer.findSymbol(Name, true); + } + + JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) { + return LazyEmitLayer.findSymbolIn(H, Name, true); } JITSymbol findSymbol(const std::string &Name) { return findMangledSymbol(Mangle(Name)); } + void addFunctionDefinition(std::unique_ptr FnAST) { + FunctionDefs[Mangle(FnAST->Proto->Name)] = std::move(FnAST); + } + private: + // This method searches the FunctionDefs map for a definition of 'Name'. If it + // finds one it generates a stub for it and returns the address of the stub. + TargetAddress searchUncompiledASTs(const std::string &Name) { + auto DefI = FunctionDefs.find(Name); + if (DefI == FunctionDefs.end()) + return 0; + + // We have AST for 'Name'. IRGen it, add it to the JIT, and + // return the address for it. + // FIXME: What happens if IRGen fails? + auto H = addModule(IRGen(Session, *DefI->second)); + + // Remove the function definition's AST now that we're + // finished with it. + FunctionDefs.erase(DefI); + + // Return the address of the function. + return findMangledSymbolIn(H, Name).getAddress(); + } + std::unique_ptr TM; Mangler Mang; SessionContext &Session; @@ -1220,12 +1234,14 @@ private: ObjLayerT ObjectLayer; CompileLayerT CompileLayer; LazyEmitLayerT LazyEmitLayer; + + std::map> FunctionDefs; }; static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) { if (auto F = ParseDefinition()) { S.addPrototypeAST(llvm::make_unique(*F->Proto)); - S.FunctionDefs[J.Mangle(F->Proto->Name)] = std::move(F); + J.addFunctionDefinition(std::move(F)); } else { // Skip token for error recovery. getNextToken();