Revert "Remove access to the DataLayout in the TargetMachine"
[oota-llvm.git] / examples / Kaleidoscope / Orc / lazy_codegen / toy.cpp
index ee42d79e58544110dd08d33bf8a8314d6f9b15ce..4b4c191171b48a7549cff880ccce15ee8ab925b0 100644 (file)
@@ -1,13 +1,14 @@
 #include "llvm/Analysis/Passes.h"
 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
+#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
 #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 +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;
 };
 
@@ -709,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; }
@@ -1125,6 +1133,26 @@ Function *FunctionAST::IRGen(IRGenContext &C) const {
 // 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();
+}
+
+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;
@@ -1133,70 +1161,55 @@ public:
 
   typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
 
-  KaleidoscopeJIT()
-    : TM(EngineBuilder().selectTarget()),
-      Mang(TM->getDataLayout()),
-      CompileLayer(ObjectLayer, SimpleCompiler(*TM)),
-      LazyEmitLayer(CompileLayer) {}
+  KaleidoscopeJIT(SessionContext &Session)
+      : DL(*Session.getTarget().getDataLayout()),
+        CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
+        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));
+  std::string mangle(const std::string &Name) {
+    std::string MangledName;
+    {
+      raw_string_ostream MangledNameStream(MangledName);
+      Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
+    }
+    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 findMangledSymbol(S).getAddress();
-                },
-                [](const std::string &S) { return 0; } );
-
-    return LazyEmitLayer.addModuleSet(std::move(S), std::move(MM));
+    // new module. Create one that resolves symbols by looking back into the
+    // JIT.
+    auto Resolver = createLambdaResolver(
+                      [&](const std::string &Name) {
+                        if (auto Sym = findSymbol(Name))
+                          return RuntimeDyld::SymbolInfo(Sym.getAddress(),
+                                                         Sym.getFlags());
+                        return RuntimeDyld::SymbolInfo(nullptr);
+                      },
+                      [](const std::string &S) { return nullptr; } );
+
+    return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
+                                      make_unique<SectionMemoryManager>(),
+                                      std::move(Resolver));
   }
 
   void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
 
-  JITSymbol findMangledSymbol(const std::string &Name) {
-    return LazyEmitLayer.findSymbol(Name, false);
+  JITSymbol findSymbol(const std::string &Name) {
+    return LazyEmitLayer.findSymbol(Name, true);
   }
 
-  JITSymbol findSymbol(const std::string Name) {
-    std::string MangledName;
-    {
-      raw_string_ostream MangledNameStream(MangledName);
-      Mang.getNameWithPrefix(MangledNameStream, Name);
-    }
-    return findMangledSymbol(MangledName);
+  JITSymbol findUnmangledSymbol(const std::string Name) {
+    return findSymbol(mangle(Name));
   }
 
 private:
-
-  std::unique_ptr<TargetMachine> TM;
-  Mangler Mang;
-
+  const DataLayout &DL;
   ObjLayerT ObjectLayer;
   CompileLayerT CompileLayer;
   LazyEmitLayerT LazyEmitLayer;
 };
 
-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();
-}
-
 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
   if (auto F = ParseDefinition()) {
     if (auto M = IRGen(S, *F)) {
@@ -1232,7 +1245,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 +1267,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) {