From dd51ad933e8ffb1dbd7aa165ad304db069b59038 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames@gmail.com>
Date: Wed, 25 Feb 2015 20:58:28 +0000
Subject: [PATCH] [Orc][Kaleidoscope] Clean up the Orc/Kaleidoscope tutorials
 to minimize the diffs between them.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230542 91177308-0d34-0410-b5e6-96231b3b80d8
---
 examples/Kaleidoscope/Orc/fully_lazy/toy.cpp  |  87 ++++++-----
 examples/Kaleidoscope/Orc/initial/toy.cpp     | 130 +++++++++--------
 .../Kaleidoscope/Orc/lazy_codegen/toy.cpp     | 137 ++++++++++--------
 examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp  |  71 ++++-----
 4 files changed, 222 insertions(+), 203 deletions(-)

diff --git a/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp b/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
index 840bf6c7b9a..333cb94e365 100644
--- a/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
+++ b/examples/Kaleidoscope/Orc/fully_lazy/toy.cpp
@@ -682,13 +682,18 @@ std::string MakeLegalFunctionName(std::string Name)
 
 class SessionContext {
 public:
-  SessionContext(LLVMContext &C) : Context(C) {}
+  SessionContext(LLVMContext &C)
+    : Context(C), TM(EngineBuilder().selectTarget()) {}
   LLVMContext& getLLVMContext() const { return Context; }
+  TargetMachine& getTarget() { return *TM; }
   void addPrototypeAST(std::unique_ptr<PrototypeAST> P);
   PrototypeAST* getPrototypeAST(const std::string &Name);
 private:
   typedef std::map<std::string, std::unique_ptr<PrototypeAST>> PrototypeMap;
+  
   LLVMContext &Context;
+  std::unique_ptr<TargetMachine> TM;
+      
   PrototypeMap Prototypes;
 };
 
@@ -710,7 +715,9 @@ public:
     : Session(S),
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
-      Builder(Session.getLLVMContext()) {}
+      Builder(Session.getLLVMContext()) {
+    M->setDataLayout(Session.getTarget().getDataLayout());
+  }
 
   SessionContext& getSession() { return Session; }
   Module& getM() const { return *M; }
@@ -1139,6 +1146,12 @@ static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
   return C.takeM();
 }
 
+template <typename T>
+static std::vector<T> singletonSet(T t) {
+  std::vector<T> Vec;
+  Vec.push_back(std::move(t));
+  return Vec;
+}
 
 static void EarthShatteringKaboom() {
   fprintf(stderr, "Earth shattering kaboom.");
@@ -1150,10 +1163,20 @@ public:
   typedef ObjectLinkingLayer<> ObjLayerT;
   typedef IRCompileLayer<ObjLayerT> CompileLayerT;
   typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
-
   typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
 
-  std::string Mangle(const std::string &Name) {
+  KaleidoscopeJIT(SessionContext &Session)
+    : Session(Session),
+      Mang(Session.getTarget().getDataLayout()),
+      ObjectLayer(
+        [](){ return llvm::make_unique<SectionMemoryManager>(); }),
+      CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
+      LazyEmitLayer(CompileLayer),
+      CompileCallbacks(LazyEmitLayer, Session.getLLVMContext(),
+                       reinterpret_cast<uintptr_t>(EarthShatteringKaboom),
+                       64) {}
+
+  std::string mangle(const std::string &Name) {
     std::string MangledName;
     {
       raw_string_ostream MangledNameStream(MangledName);
@@ -1162,32 +1185,18 @@ public:
     return MangledName;
   }
 
-  KaleidoscopeJIT(SessionContext &Session)
-    : TM(EngineBuilder().selectTarget()),
-      Mang(TM->getDataLayout()), Session(Session),
-      ObjectLayer(
-        [](){ return llvm::make_unique<SectionMemoryManager>(); }),
-      CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
-      LazyEmitLayer(CompileLayer),
-      CompileCallbacks(LazyEmitLayer, Session.getLLVMContext(),
-                       reinterpret_cast<uintptr_t>(EarthShatteringKaboom),
-                       64) {}
+  void addFunctionDefinition(std::unique_ptr<FunctionAST> FnAST) {
+    FunctionDefs[mangle(FnAST->Proto->Name)] = std::move(FnAST);
+  }
 
   ModuleHandleT addModule(std::unique_ptr<Module> M) {
-    if (!M->getDataLayout())
-      M->setDataLayout(TM->getDataLayout());
-
-    // The LazyEmitLayer takes lists of modules, rather than single modules, so
-    // we'll just build a single-element list.
-    std::vector<std::unique_ptr<Module>> S;
-    S.push_back(std::move(M));
-
     // 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.
+    // new module. Create one that resolves symbols by looking back into the
+    // JIT.
     auto MM = createLookasideRTDyldMM<SectionMemoryManager>(
                 [&](const std::string &Name) {
                   // First try to find 'Name' within the JIT.
-                  if (auto Symbol = findMangledSymbol(Name))
+                  if (auto Symbol = findSymbol(Name))
                     return Symbol.getAddress();
 
                   // If we don't already have a definition of 'Name' then search
@@ -1196,29 +1205,22 @@ public:
                 },
                 [](const std::string &S) { return 0; } );
 
-    return LazyEmitLayer.addModuleSet(std::move(S), std::move(MM));
+    return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
+                                      std::move(MM));
   }
 
   void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
 
-  JITSymbol findMangledSymbol(const std::string &Name) {
+  JITSymbol findSymbol(const std::string &Name) {
     return LazyEmitLayer.findSymbol(Name, true);
   }
 
-  JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) {
+  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
     return LazyEmitLayer.findSymbolIn(H, Name, true);
   }
 
-  JITSymbol findSymbol(const std::string &Name) {
-    return findMangledSymbol(Mangle(Name));
-  }
-
-  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
-    return findMangledSymbolIn(H, Mangle(Name));
-  }
-
-  void addFunctionDefinition(std::unique_ptr<FunctionAST> FnAST) {
-    FunctionDefs[Mangle(FnAST->Proto->Name)] = std::move(FnAST);
+  JITSymbol findUnmangledSymbol(const std::string &Name) {
+    return findSymbol(mangle(Name));
   }
 
 private:
@@ -1239,7 +1241,7 @@ private:
     FunctionDefs.erase(DefI);
 
     // Return the address of the stub.
-    return findMangledSymbolIn(H, Name).getAddress();
+    return findSymbolIn(H, Name).getAddress();
   }
 
   // This method will take the AST for a function definition and IR-gen a stub
@@ -1250,7 +1252,6 @@ private:
     //         the function.
     IRGenContext C(Session);
     Function *F = FnAST->Proto->IRGen(C);
-    C.getM().setDataLayout(TM->getDataLayout());
 
     // Step 2) Get a compile callback that can be used to compile the body of
     //         the function. The resulting CallbackInfo type will let us set the
@@ -1286,15 +1287,13 @@ private:
       return findSymbolIn(H, Fn->Proto->Name).getAddress();
     });
     CallbackInfo.setUpdateAction(
-      CompileCallbacks.getLocalFPUpdater(H, Mangle(BodyPtrName)));
+      CompileCallbacks.getLocalFPUpdater(H, mangle(BodyPtrName)));
 
     return H;
   }
 
-  std::unique_ptr<TargetMachine> TM;
-  Mangler Mang;
   SessionContext &Session;
-
+  Mangler Mang;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
   LazyEmitLayerT LazyEmitLayer;
@@ -1337,7 +1336,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
       auto H = J.addModule(C.takeM());
 
       // Get the address of the JIT'd function in memory.
-      auto ExprSymbol = J.findSymbol("__anon_expr");
+      auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
       
       // Cast it to the right type (takes no arguments, returns a double) so we
       // can call it as a native function.
diff --git a/examples/Kaleidoscope/Orc/initial/toy.cpp b/examples/Kaleidoscope/Orc/initial/toy.cpp
index c01aff2f99b..3bf2fb6dd9c 100644
--- a/examples/Kaleidoscope/Orc/initial/toy.cpp
+++ b/examples/Kaleidoscope/Orc/initial/toy.cpp
@@ -1,4 +1,3 @@
-
 #include "llvm/Analysis/Passes.h"
 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
@@ -7,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"
@@ -682,13 +681,18 @@ std::string MakeLegalFunctionName(std::string Name)
 
 class SessionContext {
 public:
-  SessionContext(LLVMContext &C) : Context(C) {}
+  SessionContext(LLVMContext &C)
+    : Context(C), TM(EngineBuilder().selectTarget()) {}
   LLVMContext& getLLVMContext() const { return Context; }
+  TargetMachine& getTarget() { return *TM; }
   void addPrototypeAST(std::unique_ptr<PrototypeAST> P);
   PrototypeAST* getPrototypeAST(const std::string &Name);
 private:
   typedef std::map<std::string, std::unique_ptr<PrototypeAST>> PrototypeMap;
+  
   LLVMContext &Context;
+  std::unique_ptr<TargetMachine> TM;
+      
   PrototypeMap Prototypes;
 };
 
@@ -710,7 +714,9 @@ public:
     : Session(S),
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
-      Builder(Session.getLLVMContext()) {}
+      Builder(Session.getLLVMContext()) {
+    M->setDataLayout(Session.getTarget().getDataLayout());
+  }
 
   SessionContext& getSession() { return Session; }
   Module& getM() const { return *M; }
@@ -1126,62 +1132,6 @@ Function *FunctionAST::IRGen(IRGenContext &C) const {
 // Top-Level parsing and JIT Driver
 //===----------------------------------------------------------------------===//
 
-class KaleidoscopeJIT {
-public:
-  typedef ObjectLinkingLayer<> ObjLayerT;
-  typedef IRCompileLayer<ObjLayerT> CompileLayerT;
-
-  typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;
-
-  KaleidoscopeJIT()
-    : TM(EngineBuilder().selectTarget()),
-      Mang(TM->getDataLayout()),
-      CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {}
-
-  ModuleHandleT addModule(std::unique_ptr<Module> M) {
-    if (!M->getDataLayout())
-      M->setDataLayout(TM->getDataLayout());
-
-    // The LazyEmitLayer takes lists of modules, rather than single modules, so
-    // we'll just build a single-element list.
-    std::vector<std::unique_ptr<Module>> S;
-    S.push_back(std::move(M));
-
-    // 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 &S) {
-                  return findMangledSymbol(S).getAddress();
-                },
-                [](const std::string &S) { return 0; } );
-
-    return CompileLayer.addModuleSet(std::move(S), std::move(MM));
-  }
-
-  void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); }
-
-  JITSymbol findMangledSymbol(const std::string &Name) {
-    return CompileLayer.findSymbol(Name, false);
-  }
-
-  JITSymbol findSymbol(const std::string Name) {
-    std::string MangledName;
-    {
-      raw_string_ostream MangledNameStream(MangledName);
-      Mang.getNameWithPrefix(MangledNameStream, Name);
-    }
-    return findMangledSymbol(MangledName);
-  }
-
-private:
-
-  std::unique_ptr<TargetMachine> TM;
-  Mangler Mang;
-
-  ObjLayerT ObjectLayer;
-  CompileLayerT CompileLayer;
-};
-
 static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
                                            const FunctionAST &F) {
   IRGenContext C(S);
@@ -1195,6 +1145,62 @@ static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
   return C.takeM();
 }
 
+template <typename T>
+static std::vector<T> singletonSet(T t) {
+  std::vector<T> Vec;
+  Vec.push_back(std::move(t));
+  return Vec;
+}
+
+class KaleidoscopeJIT {
+public:
+  typedef ObjectLinkingLayer<> ObjLayerT;
+  typedef IRCompileLayer<ObjLayerT> CompileLayerT;
+  typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;
+
+  KaleidoscopeJIT(SessionContext &Session)
+    : Mang(Session.getTarget().getDataLayout()),
+      CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())) {}
+
+  std::string mangle(const std::string &Name) {
+    std::string MangledName;
+    {
+      raw_string_ostream MangledNameStream(MangledName);
+      Mang.getNameWithPrefix(MangledNameStream, Name);
+    }
+    return MangledName;
+  }
+
+  ModuleHandleT addModule(std::unique_ptr<Module> M) {
+    // 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 &S) {
+                  return findSymbol(S).getAddress();
+                },
+                [](const std::string &S) { return 0; } );
+
+    return CompileLayer.addModuleSet(singletonSet(std::move(M)), std::move(MM));
+  }
+
+  void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); }
+
+  JITSymbol findSymbol(const std::string &Name) {
+    return CompileLayer.findSymbol(Name, true);
+  }
+
+  JITSymbol findUnmangledSymbol(const std::string Name) {
+    return findSymbol(mangle(Name));
+  }
+
+private:
+
+  Mangler Mang;
+  ObjLayerT ObjectLayer;
+  CompileLayerT CompileLayer;
+};
+
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
     if (auto M = IRGen(S, *F)) {
@@ -1230,7 +1236,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
       auto H = J.addModule(C.takeM());
 
       // Get the address of the JIT'd function in memory.
-      auto ExprSymbol = J.findSymbol("__anon_expr");
+      auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
       
       // Cast it to the right type (takes no arguments, returns a double) so we
       // can call it as a native function.
@@ -1252,8 +1258,8 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
 
 /// top ::= definition | external | expression | ';'
 static void MainLoop() {
-  KaleidoscopeJIT J;
   SessionContext S(getGlobalContext());
+  KaleidoscopeJIT J(S);
 
   while (1) {
     switch (CurTok) {
diff --git a/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp b/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
index ee42d79e585..1ed267d5d68 100644
--- a/examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp
+++ b/examples/Kaleidoscope/Orc/lazy_codegen/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"
@@ -681,13 +681,18 @@ std::string MakeLegalFunctionName(std::string Name)
 
 class SessionContext {
 public:
-  SessionContext(LLVMContext &C) : Context(C) {}
+  SessionContext(LLVMContext &C)
+    : Context(C), TM(EngineBuilder().selectTarget()) {}
   LLVMContext& getLLVMContext() const { return Context; }
+  TargetMachine& getTarget() { return *TM; }
   void addPrototypeAST(std::unique_ptr<PrototypeAST> P);
   PrototypeAST* getPrototypeAST(const std::string &Name);
 private:
   typedef std::map<std::string, std::unique_ptr<PrototypeAST>> PrototypeMap;
+  
   LLVMContext &Context;
+  std::unique_ptr<TargetMachine> TM;
+      
   PrototypeMap Prototypes;
 };
 
@@ -709,7 +714,9 @@ public:
     : Session(S),
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
-      Builder(Session.getLLVMContext()) {}
+      Builder(Session.getLLVMContext()) {
+    M->setDataLayout(Session.getTarget().getDataLayout());
+  }
 
   SessionContext& getSession() { return Session; }
   Module& getM() const { return *M; }
@@ -1125,65 +1132,6 @@ Function *FunctionAST::IRGen(IRGenContext &C) const {
 // Top-Level parsing and JIT Driver
 //===----------------------------------------------------------------------===//
 
-class KaleidoscopeJIT {
-public:
-  typedef ObjectLinkingLayer<> ObjLayerT;
-  typedef IRCompileLayer<ObjLayerT> CompileLayerT;
-  typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
-
-  typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
-
-  KaleidoscopeJIT()
-    : TM(EngineBuilder().selectTarget()),
-      Mang(TM->getDataLayout()),
-      CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
-      LazyEmitLayer(CompileLayer) {}
-
-  ModuleHandleT addModule(std::unique_ptr<Module> M) {
-    if (!M->getDataLayout())
-      M->setDataLayout(TM->getDataLayout());
-
-    // The LazyEmitLayer takes lists of modules, rather than single modules, so
-    // we'll just build a single-element list.
-    std::vector<std::unique_ptr<Module>> S;
-    S.push_back(std::move(M));
-
-    // 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 &S) {
-                  return findMangledSymbol(S).getAddress();
-                },
-                [](const std::string &S) { return 0; } );
-
-    return LazyEmitLayer.addModuleSet(std::move(S), std::move(MM));
-  }
-
-  void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
-
-  JITSymbol findMangledSymbol(const std::string &Name) {
-    return LazyEmitLayer.findSymbol(Name, false);
-  }
-
-  JITSymbol findSymbol(const std::string Name) {
-    std::string MangledName;
-    {
-      raw_string_ostream MangledNameStream(MangledName);
-      Mang.getNameWithPrefix(MangledNameStream, Name);
-    }
-    return findMangledSymbol(MangledName);
-  }
-
-private:
-
-  std::unique_ptr<TargetMachine> TM;
-  Mangler Mang;
-
-  ObjLayerT ObjectLayer;
-  CompileLayerT CompileLayer;
-  LazyEmitLayerT LazyEmitLayer;
-};
-
 static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
                                            const FunctionAST &F) {
   IRGenContext C(S);
@@ -1197,6 +1145,67 @@ static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
   return C.takeM();
 }
 
+template <typename T>
+static std::vector<T> singletonSet(T t) {
+  std::vector<T> Vec;
+  Vec.push_back(std::move(t));
+  return Vec;
+}
+
+class KaleidoscopeJIT {
+public:
+  typedef ObjectLinkingLayer<> ObjLayerT;
+  typedef IRCompileLayer<ObjLayerT> CompileLayerT;
+  typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
+
+  typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
+
+  KaleidoscopeJIT(SessionContext &Session)
+    : Mang(Session.getTarget().getDataLayout()),
+      CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
+      LazyEmitLayer(CompileLayer) {}
+
+  std::string mangle(const std::string &Name) {
+    std::string MangledName;
+    {
+      raw_string_ostream MangledNameStream(MangledName);
+      Mang.getNameWithPrefix(MangledNameStream, Name);
+    }
+    return MangledName;
+  }
+
+  ModuleHandleT addModule(std::unique_ptr<Module> M) {
+    // 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) {
+                  return findSymbol(Name).getAddress();
+                },
+                [](const std::string &S) { return 0; } );
+
+    return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
+                                      std::move(MM));
+  }
+
+  void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
+
+  JITSymbol findSymbol(const std::string &Name) {
+    return LazyEmitLayer.findSymbol(Name, true);
+  }
+
+  JITSymbol findUnmangledSymbol(const std::string Name) {
+    return findSymbol(mangle(Name));
+  }
+
+private:
+
+  Mangler Mang;
+  ObjLayerT ObjectLayer;
+  CompileLayerT CompileLayer;
+  LazyEmitLayerT LazyEmitLayer;
+};
+
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
     if (auto M = IRGen(S, *F)) {
@@ -1232,7 +1241,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
       auto H = J.addModule(C.takeM());
 
       // Get the address of the JIT'd function in memory.
-      auto ExprSymbol = J.findSymbol("__anon_expr");
+      auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
       
       // Cast it to the right type (takes no arguments, returns a double) so we
       // can call it as a native function.
@@ -1254,8 +1263,8 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
 
 /// top ::= definition | external | expression | ';'
 static void MainLoop() {
-  KaleidoscopeJIT J;
   SessionContext S(getGlobalContext());
+  KaleidoscopeJIT J(S);
 
   while (1) {
     switch (CurTok) {
diff --git a/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp b/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
index 3225a0d93b8..d7744ece655 100644
--- a/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
+++ b/examples/Kaleidoscope/Orc/lazy_irgen/toy.cpp
@@ -681,13 +681,18 @@ std::string MakeLegalFunctionName(std::string Name)
 
 class SessionContext {
 public:
-  SessionContext(LLVMContext &C) : Context(C) {}
+  SessionContext(LLVMContext &C)
+    : Context(C), TM(EngineBuilder().selectTarget()) {}
   LLVMContext& getLLVMContext() const { return Context; }
+  TargetMachine& getTarget() { return *TM; }
   void addPrototypeAST(std::unique_ptr<PrototypeAST> P);
   PrototypeAST* getPrototypeAST(const std::string &Name);
 private:
   typedef std::map<std::string, std::unique_ptr<PrototypeAST>> PrototypeMap;
+  
   LLVMContext &Context;
+  std::unique_ptr<TargetMachine> TM;
+      
   PrototypeMap Prototypes;
 };
 
@@ -709,7 +714,9 @@ public:
     : Session(S),
       M(new Module(GenerateUniqueName("jit_module_"),
                    Session.getLLVMContext())),
-      Builder(Session.getLLVMContext()) {}
+      Builder(Session.getLLVMContext()) {
+    M->setDataLayout(Session.getTarget().getDataLayout());
+  }
 
   SessionContext& getSession() { return Session; }
   Module& getM() const { return *M; }
@@ -1138,15 +1145,27 @@ static std::unique_ptr<llvm::Module> IRGen(SessionContext &S,
   return C.takeM();
 }
 
+template <typename T>
+static std::vector<T> singletonSet(T t) {
+  std::vector<T> Vec;
+  Vec.push_back(std::move(t));
+  return Vec;
+}
+
 class KaleidoscopeJIT {
 public:
   typedef ObjectLinkingLayer<> ObjLayerT;
   typedef IRCompileLayer<ObjLayerT> CompileLayerT;
   typedef LazyEmittingLayer<CompileLayerT> LazyEmitLayerT;
-
   typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
 
-  std::string Mangle(const std::string &Name) {
+  KaleidoscopeJIT(SessionContext &Session)
+    : Session(Session),
+      Mang(Session.getTarget().getDataLayout()),
+      CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
+      LazyEmitLayer(CompileLayer) {}
+
+  std::string mangle(const std::string &Name) {
     std::string MangledName;
     {
       raw_string_ostream MangledNameStream(MangledName);
@@ -1155,27 +1174,18 @@ public:
     return MangledName;
   }
 
-  KaleidoscopeJIT(SessionContext &Session)
-    : TM(EngineBuilder().selectTarget()),
-      Mang(TM->getDataLayout()), Session(Session),
-      CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
-      LazyEmitLayer(CompileLayer) {}
+  void addFunctionDefinition(std::unique_ptr<FunctionAST> FnAST) {
+    FunctionDefs[mangle(FnAST->Proto->Name)] = std::move(FnAST);
+  }
 
   ModuleHandleT addModule(std::unique_ptr<Module> M) {
-    if (!M->getDataLayout())
-      M->setDataLayout(TM->getDataLayout());
-
-    // The LazyEmitLayer takes lists of modules, rather than single modules, so
-    // we'll just build a single-element list.
-    std::vector<std::unique_ptr<Module>> S;
-    S.push_back(std::move(M));
-
     // 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.
+    // new module. Create one that resolves symbols by looking back into the
+    // JIT.
     auto MM = createLookasideRTDyldMM<SectionMemoryManager>(
                 [&](const std::string &Name) {
                   // First try to find 'Name' within the JIT.
-                  if (auto Symbol = findMangledSymbol(Name))
+                  if (auto Symbol = findSymbol(Name))
                     return Symbol.getAddress();
 
                   // If we don't already have a definition of 'Name' then search
@@ -1184,25 +1194,22 @@ public:
                 },
                 [](const std::string &S) { return 0; } );
 
-    return LazyEmitLayer.addModuleSet(std::move(S), std::move(MM));
+    return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
+                                      std::move(MM));
   }
 
   void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
 
-  JITSymbol findMangledSymbol(const std::string &Name) {
+  JITSymbol findSymbol(const std::string &Name) {
     return LazyEmitLayer.findSymbol(Name, true);
   }
 
-  JITSymbol findMangledSymbolIn(ModuleHandleT H, const std::string &Name) {
+  JITSymbol findSymbolIn(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);
+  JITSymbol findUnmangledSymbol(const std::string &Name) {
+    return findSymbol(mangle(Name));
   }
 
 private:
@@ -1224,13 +1231,11 @@ private:
     FunctionDefs.erase(DefI);
 
     // Return the address of the function.
-    return findMangledSymbolIn(H, Name).getAddress();
+    return findSymbolIn(H, Name).getAddress();
   }
 
-  std::unique_ptr<TargetMachine> TM;
-  Mangler Mang;
   SessionContext &Session;
-
+  Mangler Mang;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
   LazyEmitLayerT LazyEmitLayer;
@@ -1271,7 +1276,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
       auto H = J.addModule(C.takeM());
 
       // Get the address of the JIT'd function in memory.
-      auto ExprSymbol = J.findSymbol("__anon_expr");
+      auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
       
       // Cast it to the right type (takes no arguments, returns a double) so we
       // can call it as a native function.