X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=examples%2FKaleidoscope%2FOrc%2Finitial%2Ftoy.cpp;h=1b65e8c2a053093211612c5602eca6f10a0fb76c;hb=69d25395542d5ff7cfa4422dcff7fe8569475910;hp=f075af156c2bf2dc49eba03c9719b7bb85852554;hpb=861a3dd510ef903aed2210dcd5722a7f34d3a243;p=oota-llvm.git diff --git a/examples/Kaleidoscope/Orc/initial/toy.cpp b/examples/Kaleidoscope/Orc/initial/toy.cpp index f075af156c2..1b65e8c2a05 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,10 +6,10 @@ #include "llvm/IR/DataLayout.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" -#include "llvm/PassManager.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Transforms/Scalar.h" #include @@ -20,7 +19,9 @@ #include #include #include + using namespace llvm; +using namespace llvm::orc; //===----------------------------------------------------------------------===// // Lexer @@ -680,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 P); PrototypeAST* getPrototypeAST(const std::string &Name); private: typedef std::map> PrototypeMap; + LLVMContext &Context; + std::unique_ptr TM; + PrototypeMap Prototypes; }; @@ -708,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; } @@ -1124,75 +1132,75 @@ Function *FunctionAST::IRGen(IRGenContext &C) const { // Top-Level parsing and JIT Driver //===----------------------------------------------------------------------===// +static std::unique_ptr 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 +static std::vector singletonSet(T t) { + std::vector Vec; + Vec.push_back(std::move(t)); + return Vec; +} + class KaleidoscopeJIT { public: typedef ObjectLinkingLayer<> ObjLayerT; typedef IRCompileLayer CompileLayerT; - typedef CompileLayerT::ModuleSetHandleT ModuleHandleT; - KaleidoscopeJIT() - : TM(EngineBuilder().selectTarget()), - Mang(TM->getDataLayout()), - CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {} - - ModuleHandleT addModule(std::unique_ptr M) { - if (!M->getDataLayout()) - M->setDataLayout(TM->getDataLayout()); + KaleidoscopeJIT(SessionContext &Session) + : Mang(Session.getTarget().getDataLayout()), + CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())) {} - // The LazyEmitLayer takes lists of modules, rather than single modules, so - // we'll just build a single-element list. - std::vector> S; - S.push_back(std::move(M)); + 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 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( - [&](const std::string &S) { - return getMangledSymbolAddress(S); - }, + [&](const std::string &Name) { + return findSymbol(Name).getAddress(); + }, [](const std::string &S) { return 0; } ); - return CompileLayer.addModuleSet(std::move(S), std::move(MM)); + return CompileLayer.addModuleSet(singletonSet(std::move(M)), std::move(MM)); } void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); } - uint64_t getMangledSymbolAddress(const std::string &Name) { - return CompileLayer.getSymbolAddress(Name, false); + JITSymbol findSymbol(const std::string &Name) { + return CompileLayer.findSymbol(Name, true); } - uint64_t getSymbolAddress(const std::string Name) { - std::string MangledName; - { - raw_string_ostream MangledNameStream(MangledName); - Mang.getNameWithPrefix(MangledNameStream, Name); - } - return getMangledSymbolAddress(MangledName); + JITSymbol findUnmangledSymbol(const std::string Name) { + return findSymbol(mangle(Name)); } private: - std::unique_ptr TM; Mangler Mang; - ObjLayerT ObjectLayer; CompileLayerT CompileLayer; }; -static std::unique_ptr 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)) { @@ -1228,11 +1236,11 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) { auto H = J.addModule(C.takeM()); // Get the address of the JIT'd function in memory. - uint64_t ExprFuncAddr = J.getSymbolAddress("__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. - double (*FP)() = (double (*)())(intptr_t)ExprFuncAddr; + double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress(); #ifdef MINIMAL_STDERR_OUTPUT FP(); #else @@ -1250,20 +1258,20 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) { /// top ::= definition | external | expression | ';' static void MainLoop() { - KaleidoscopeJIT J; SessionContext S(getGlobalContext()); + KaleidoscopeJIT J(S); while (1) { -#ifndef MINIMAL_STDERR_OUTPUT - std::cerr << "ready> "; -#endif switch (CurTok) { case tok_eof: return; - case ';': getNextToken(); break; // ignore top-level semicolons. + case ';': getNextToken(); continue; // ignore top-level semicolons. case tok_def: HandleDefinition(S, J); break; case tok_extern: HandleExtern(S); break; default: HandleTopLevelExpression(S, J); break; } +#ifndef MINIMAL_STDERR_OUTPUT + std::cerr << "ready> "; +#endif } }