Kaleidoscope-Orc: Reuse the IRGen utility function in later chapters, and remove...
authorDavid Blaikie <dblaikie@gmail.com>
Sun, 8 Feb 2015 21:03:30 +0000 (21:03 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Sun, 8 Feb 2015 21:03:30 +0000 (21:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@228543 91177308-0d34-0410-b5e6-96231b3b80d8

examples/Kaleidoscope/Orc/initial/toy.cpp
examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp

index 1b772259a19ef8f384e644e0fa81f6d761b68d4c..f075af156c2bf2dc49eba03c9719b7bb85852554 100644 (file)
@@ -1180,8 +1180,8 @@ private:
   CompileLayerT CompileLayer;
 };
 
   CompileLayerT CompileLayer;
 };
 
-static std::unique_ptr<llvm::Module>
-IRGen(KaleidoscopeJIT &J, SessionContext &S, const FunctionAST &F) {
+static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
+                                           const FunctionAST &F) {
   IRGenContext C(S);
   auto LF = F.IRGen(C);
   if (!LF)
   IRGenContext C(S);
   auto LF = F.IRGen(C);
   if (!LF)
@@ -1195,7 +1195,7 @@ IRGen(KaleidoscopeJIT &J, SessionContext &S, const FunctionAST &F) {
 
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
 
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
-    if (auto M = IRGen(J, S, *F)) {
+    if (auto M = IRGen(S, *F)) {
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
       J.addModule(std::move(M));
     }
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
       J.addModule(std::move(M));
     }
index 41abd27d349fa9d7982318bc156be547ed82beda..1a2043f5933cd57e86263da22d76dac1388aa09f 100644 (file)
@@ -1182,16 +1182,24 @@ private:
   LazyEmitLayerT LazyEmitLayer;
 };
 
   LazyEmitLayerT LazyEmitLayer;
 };
 
-static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
-  if (auto F = ParseDefinition()) {
-    IRGenContext C(S);
-    if (auto LF = F->IRGen(C)) {
+static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
+                                           const FunctionAST &F) {
+  IRGenContext C(S);
+  auto LF = F.IRGen(C);
+  if (!LF)
+    return nullptr;
 #ifndef MINIMAL_STDERR_OUTPUT
 #ifndef MINIMAL_STDERR_OUTPUT
-      std::cerr << "Read function definition:\n";
-      LF->dump();
+  fprintf(stderr, "Read function definition:");
+  LF->dump();
 #endif
 #endif
-      J.addModule(C.takeM());
+  return C.takeM();
+}
+
+static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
+  if (auto F = ParseDefinition()) {
+    if (auto M = IRGen(S, *F)) {
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
       S.addPrototypeAST(llvm::make_unique<PrototypeAST>(*F->Proto));
+      J.addModule(std::move(M));
     }
   } else {
     // Skip token for error recovery.
     }
   } else {
     // Skip token for error recovery.
index e1e259da76ce2386e4e6e5fe4fcf5feb25074f2b..2963f30e2edbea490c103977345468edf94d2e40 100644 (file)
@@ -1124,6 +1124,19 @@ Function *FunctionAST::IRGen(IRGenContext &C) const {
 // Top-Level parsing and JIT Driver
 //===----------------------------------------------------------------------===//
 
 // Top-Level parsing and JIT Driver
 //===----------------------------------------------------------------------===//
 
+static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
+                                           const FunctionAST &F) {
+  IRGenContext C(S);
+  auto LF = F.IRGen(C);
+  if (!LF)
+    return nullptr;
+#ifndef MINIMAL_STDERR_OUTPUT
+  fprintf(stderr, "Read function definition:");
+  LF->dump();
+#endif
+  return C.takeM();
+}
+
 class KaleidoscopeJIT {
 public:
   typedef ObjectLinkingLayer<> ObjLayerT;
 class KaleidoscopeJIT {
 public:
   typedef ObjectLinkingLayer<> ObjLayerT;
@@ -1166,20 +1179,19 @@ public:
 
                   // If we don't find 'Name' in the JIT, see if we have some AST
                   // for it.
 
                   // If we don't find 'Name' in the JIT, see if we have some AST
                   // for it.
-                  if (!Session.FunctionDefs.count(Name))
+                  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.
                     return 0;
 
                   // We have AST for 'Name'. IRGen it, add it to the JIT, and
                   // return the address for it.
-                  IRGenContext C(Session);
-                  {
-                    // Take ownership of the AST: We can release the memory as
-                    // soon as we've IRGen'd it.
-                    auto FuncAST = std::move(Session.FunctionDefs[Name]);
-                    FuncAST->IRGen(C);
-                  }
-
-                  addModule(C.takeM());
+                  // 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 getMangledSymbolAddress(Name);
                 }, 
                 [](const std::string &S) { return 0; } );
                   return getMangledSymbolAddress(Name);
                 }, 
                 [](const std::string &S) { return 0; } );