[llvm] r230202 - [Orc][Kaleidoscope] Tidy up the lazy_irgen tutorial, touch up a couple of

Lang Hames lhames at gmail.com
Sun Feb 22 20:45:05 PST 2015


Author: lhames
Date: Sun Feb 22 22:45:05 2015
New Revision: 230202

URL: http://llvm.org/viewvc/llvm-project?rev=230202&view=rev
Log:
[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.

Modified:
    llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
    llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp

Modified: llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp?rev=230202&r1=230201&r2=230202&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp Sun Feb 22 22:45:05 2015
@@ -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<LazyEmitLayerT, OrcX86_64> CompileCallbacks;
-
   std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs;
+
+  JITCompileCallbackManager<LazyEmitLayerT, OrcX86_64> CompileCallbacks;
 };
 
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {

Modified: llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp?rev=230202&r1=230201&r2=230202&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp Sun Feb 22 22:45:05 2015
@@ -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<PrototypeAST> P);
   PrototypeAST* getPrototypeAST(const std::string &Name);
-  std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs; 
 private:
   typedef std::map<std::string, std::unique_ptr<PrototypeAST>> 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<SectionMemoryManager>(
-                [&](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<FunctionAST> 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<TargetMachine> TM;
   Mangler Mang;
   SessionContext &Session;
@@ -1220,12 +1234,14 @@ private:
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
   LazyEmitLayerT LazyEmitLayer;
+
+  std::map<std::string, std::unique_ptr<FunctionAST>> FunctionDefs;
 };
 
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
     S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
-    S.FunctionDefs[J.Mangle(F->Proto->Name)] = std::move(F);
+    J.addFunctionDefinition(std::move(F));
   } else {
     // Skip token for error recovery.
     getNextToken();





More information about the llvm-commits mailing list