[llvm] r228543 - Kaleidoscope-Orc: Reuse the IRGen utility function in later chapters, and remove an unused parameter.

David Blaikie dblaikie at gmail.com
Sun Feb 8 13:03:30 PST 2015


Author: dblaikie
Date: Sun Feb  8 15:03:30 2015
New Revision: 228543

URL: http://llvm.org/viewvc/llvm-project?rev=228543&view=rev
Log:
Kaleidoscope-Orc: Reuse the IRGen utility function in later chapters, and remove an unused parameter.

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

Modified: llvm/trunk/examples/Kaleidoscope/Orc/initial/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/initial/toy.cpp?rev=228543&r1=228542&r2=228543&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Orc/initial/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Orc/initial/toy.cpp Sun Feb  8 15:03:30 2015
@@ -1180,8 +1180,8 @@ private:
   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)
@@ -1195,7 +1195,7 @@ IRGen(KaleidoscopeJIT &J, SessionContext
 
 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));
     }

Modified: llvm/trunk/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp?rev=228543&r1=228542&r2=228543&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp Sun Feb  8 15:03:30 2015
@@ -1182,16 +1182,24 @@ private:
   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
-      std::cerr << "Read function definition:\n";
-      LF->dump();
+  fprintf(stderr, "Read function definition:");
+  LF->dump();
 #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));
+      J.addModule(std::move(M));
     }
   } else {
     // Skip token for error recovery.

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=228543&r1=228542&r2=228543&view=diff
==============================================================================
--- llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp (original)
+++ llvm/trunk/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp Sun Feb  8 15:03:30 2015
@@ -1124,6 +1124,19 @@ Function *FunctionAST::IRGen(IRGenContex
 // 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;
@@ -1166,20 +1179,19 @@ public:
 
                   // 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.
-                  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);
-                  }
+                  // 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);
 
-                  addModule(C.takeM());
                   return getMangledSymbolAddress(Name);
                 }, 
                 [](const std::string &S) { return 0; } );





More information about the llvm-commits mailing list