[PATCH] D45897: Convert clang-interpreter to ORC JIT API
This revision was automatically updated to reflect the committed changes. Closed by commit rL02: Convert clang-interpreter to ORC JIT API (authored by sas, committed by ). Herald added a subscriber: llvm-commits. Changed prior to commit: https://reviews.llvm.org/D45897?vs=143376=148656#toc Repository: rL LLVM https://reviews.llvm.org/D45897 Files: cfe/trunk/examples/clang-interpreter/CMakeLists.txt cfe/trunk/examples/clang-interpreter/Invoke.cpp cfe/trunk/examples/clang-interpreter/Invoke.h cfe/trunk/examples/clang-interpreter/Manager.cpp cfe/trunk/examples/clang-interpreter/Manager.h cfe/trunk/examples/clang-interpreter/main.cpp Index: cfe/trunk/examples/clang-interpreter/CMakeLists.txt === --- cfe/trunk/examples/clang-interpreter/CMakeLists.txt +++ cfe/trunk/examples/clang-interpreter/CMakeLists.txt @@ -12,8 +12,6 @@ add_clang_executable(clang-interpreter main.cpp - Invoke.cpp - Manager.cpp ) add_dependencies(clang-interpreter Index: cfe/trunk/examples/clang-interpreter/main.cpp === --- cfe/trunk/examples/clang-interpreter/main.cpp +++ cfe/trunk/examples/clang-interpreter/main.cpp @@ -7,11 +7,8 @@ // //===--===// -#include "Invoke.h" -#include "Manager.h" - -#include "clang/CodeGen/CodeGenAction.h" #include "clang/Basic/DiagnosticOptions.h" +#include "clang/CodeGen/CodeGenAction.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" @@ -21,37 +18,24 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/ADT/SmallString.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/ExecutionEngine/Orc/CompileUtils.h" +#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" using namespace clang; using namespace clang::driver; -namespace interpreter { - -static llvm::ExecutionEngine * -createExecutionEngine(std::unique_ptr M, std::string *ErrorStr) { - llvm::EngineBuilder EB(std::move(M)); - EB.setErrorStr(ErrorStr); - EB.setMemoryManager(llvm::make_unique()); - llvm::ExecutionEngine *EE = EB.create(); - EE->finalizeObject(); - return EE; -} - -// Invoked from a try/catch block in invoke.cpp. -// -static int Invoke(llvm::ExecutionEngine *EE, llvm::Function *EntryFn, - const std::vector , char *const *EnvP) { - return EE->runFunctionAsMain(EntryFn, Args, EnvP); -} - // This function isn't referenced outside its translation unit, but it // can't use the "static" keyword because its address is used for // GetMainExecutable (since some platforms don't support taking the @@ -61,13 +45,76 @@ return llvm::sys::fs::getMainExecutable(Argv0, MainAddr); } -} // namespace interpreter +namespace llvm { +namespace orc { + +class SimpleJIT { +private: + ExecutionSession ES; + std::shared_ptr Resolver; + std::unique_ptr TM; + const DataLayout DL; + RTDyldObjectLinkingLayer ObjectLayer; + IRCompileLayerCompileLayer; + +public: + SimpleJIT() + : Resolver(createLegacyLookupResolver( +ES, +[this](const std::string ) -> JITSymbol { + if (auto Sym = CompileLayer.findSymbol(Name, false)) +return Sym; + else if (auto Err = Sym.takeError()) +return std::move(Err); + if (auto SymAddr = + RTDyldMemoryManager::getSymbolAddressInProcess(Name)) +return JITSymbol(SymAddr, JITSymbolFlags::Exported); + return nullptr; +}, +[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })), +TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), +ObjectLayer(ES, +[this](VModuleKey) { + return RTDyldObjectLinkingLayer::Resources{ + std::make_shared(), Resolver}; +}), +CompileLayer(ObjectLayer, SimpleCompiler(*TM)) { +llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); + } + + const TargetMachine () const { return *TM; } -int main(int argc, const char **argv, char * const *envp) { + VModuleKey addModule(std::unique_ptr M) { +// Add the module to the JIT with a new VModuleKey. +auto K = ES.allocateVModule(); +
[PATCH] D45897: Convert clang-interpreter to ORC JIT API
lhames accepted this revision. lhames added a comment. This revision is now accepted and ready to land. Herald added a subscriber: mgrang. Looks good to me. :) Repository: rC Clang https://reviews.llvm.org/D45897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D45897: Convert clang-interpreter to ORC JIT API
sas updated this revision to Diff 143376. sas added a comment. Review comments from @alexshap. Repository: rC Clang https://reviews.llvm.org/D45897 Files: examples/clang-interpreter/CMakeLists.txt examples/clang-interpreter/Invoke.cpp examples/clang-interpreter/Invoke.h examples/clang-interpreter/Manager.cpp examples/clang-interpreter/Manager.h examples/clang-interpreter/main.cpp Index: examples/clang-interpreter/main.cpp === --- examples/clang-interpreter/main.cpp +++ examples/clang-interpreter/main.cpp @@ -7,11 +7,8 @@ // //===--===// -#include "Invoke.h" -#include "Manager.h" - -#include "clang/CodeGen/CodeGenAction.h" #include "clang/Basic/DiagnosticOptions.h" +#include "clang/CodeGen/CodeGenAction.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" @@ -21,37 +18,24 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/ADT/SmallString.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/ExecutionEngine/Orc/CompileUtils.h" +#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" using namespace clang; using namespace clang::driver; -namespace interpreter { - -static llvm::ExecutionEngine * -createExecutionEngine(std::unique_ptr M, std::string *ErrorStr) { - llvm::EngineBuilder EB(std::move(M)); - EB.setErrorStr(ErrorStr); - EB.setMemoryManager(llvm::make_unique()); - llvm::ExecutionEngine *EE = EB.create(); - EE->finalizeObject(); - return EE; -} - -// Invoked from a try/catch block in invoke.cpp. -// -static int Invoke(llvm::ExecutionEngine *EE, llvm::Function *EntryFn, - const std::vector , char *const *EnvP) { - return EE->runFunctionAsMain(EntryFn, Args, EnvP); -} - // This function isn't referenced outside its translation unit, but it // can't use the "static" keyword because its address is used for // GetMainExecutable (since some platforms don't support taking the @@ -61,13 +45,75 @@ return llvm::sys::fs::getMainExecutable(Argv0, MainAddr); } -} // namespace interpreter +namespace llvm { +namespace orc { + +class SimpleJIT { +private: + ExecutionSession ES; + std::shared_ptr Resolver; + std::unique_ptr TM; + const DataLayout DL; + RTDyldObjectLinkingLayer ObjectLayer; + IRCompileLayerCompileLayer; + +public: + SimpleJIT() + : Resolver(createLegacyLookupResolver( +[this](const std::string ) -> JITSymbol { + if (auto Sym = CompileLayer.findSymbol(Name, false)) +return Sym; + else if (auto Err = Sym.takeError()) +return std::move(Err); + if (auto SymAddr = + RTDyldMemoryManager::getSymbolAddressInProcess(Name)) +return JITSymbol(SymAddr, JITSymbolFlags::Exported); + return nullptr; +}, +[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })), +TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), +ObjectLayer(ES, +[this](VModuleKey) { + return RTDyldObjectLinkingLayer::Resources{ + std::make_shared(), Resolver}; +}), +CompileLayer(ObjectLayer, SimpleCompiler(*TM)) { +llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); + } + + const TargetMachine () const { return *TM; } -int main(int argc, const char **argv, char * const *envp) { + VModuleKey addModule(std::unique_ptr M) { +// Add the module to the JIT with a new VModuleKey. +auto K = ES.allocateVModule(); +cantFail(CompileLayer.addModule(K, std::move(M))); +return K; + } + + JITSymbol findSymbol(const StringRef ) { +std::string MangledName; +raw_string_ostream MangledNameStream(MangledName); +Mangler::getNameWithPrefix(MangledNameStream, Name, DL); +return CompileLayer.findSymbol(MangledNameStream.str(), true); + } + + JITTargetAddress getSymbolAddress(const StringRef ) { +return cantFail(findSymbol(Name).getAddress()); + } + + void removeModule(VModuleKey K) { +cantFail(CompileLayer.removeModule(K)); + } +}; + +} // end namespace orc +} // end namespace llvm + +int main(int argc, const char **argv) { // This just needs to be some symbol in the binary; C++ doesn't //
[PATCH] D45897: Convert clang-interpreter to ORC JIT API
sas added inline comments. Comment at: examples/clang-interpreter/main.cpp:52 +class SimpleJIT { +private: + ExecutionSession ES; alexshap wrote: > not needed Seems cleaner to me to have these explicit, and avoids potentiel code churn if there are changes above. Is there something in the llvm coding style that recommends skipping these? Repository: rC Clang https://reviews.llvm.org/D45897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D45897: Convert clang-interpreter to ORC JIT API
alexshap added inline comments. Comment at: examples/clang-interpreter/main.cpp:52 +class SimpleJIT { +private: + ExecutionSession ES; not needed Comment at: examples/clang-interpreter/main.cpp:84 + + TargetMachine () { return *TM; } const TargetMachine & ? Comment at: examples/clang-interpreter/main.cpp:93 + + JITSymbol findSymbol(const std::string Name) { +std::string MangledName; const string & or maybe even StringRef and the same below Repository: rC Clang https://reviews.llvm.org/D45897 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D45897: Convert clang-interpreter to ORC JIT API
sas created this revision. sas added reviewers: ddunbar, lhames. Herald added a subscriber: mgorny. This mostly re-uses code from the KaleidoscopeJIT example. Repository: rC Clang https://reviews.llvm.org/D45897 Files: examples/clang-interpreter/CMakeLists.txt examples/clang-interpreter/Invoke.cpp examples/clang-interpreter/Invoke.h examples/clang-interpreter/Manager.cpp examples/clang-interpreter/Manager.h examples/clang-interpreter/main.cpp Index: examples/clang-interpreter/main.cpp === --- examples/clang-interpreter/main.cpp +++ examples/clang-interpreter/main.cpp @@ -7,11 +7,8 @@ // //===--===// -#include "Invoke.h" -#include "Manager.h" - -#include "clang/CodeGen/CodeGenAction.h" #include "clang/Basic/DiagnosticOptions.h" +#include "clang/CodeGen/CodeGenAction.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" @@ -21,37 +18,24 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "llvm/ADT/SmallString.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/ExecutionEngine/MCJIT.h" +#include "llvm/ExecutionEngine/Orc/CompileUtils.h" +#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" +#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/Mangler.h" #include "llvm/IR/Module.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" using namespace clang; using namespace clang::driver; -namespace interpreter { - -static llvm::ExecutionEngine * -createExecutionEngine(std::unique_ptr M, std::string *ErrorStr) { - llvm::EngineBuilder EB(std::move(M)); - EB.setErrorStr(ErrorStr); - EB.setMemoryManager(llvm::make_unique()); - llvm::ExecutionEngine *EE = EB.create(); - EE->finalizeObject(); - return EE; -} - -// Invoked from a try/catch block in invoke.cpp. -// -static int Invoke(llvm::ExecutionEngine *EE, llvm::Function *EntryFn, - const std::vector , char *const *EnvP) { - return EE->runFunctionAsMain(EntryFn, Args, EnvP); -} - // This function isn't referenced outside its translation unit, but it // can't use the "static" keyword because its address is used for // GetMainExecutable (since some platforms don't support taking the @@ -61,13 +45,75 @@ return llvm::sys::fs::getMainExecutable(Argv0, MainAddr); } -} // namespace interpreter +namespace llvm { +namespace orc { + +class SimpleJIT { +private: + ExecutionSession ES; + std::shared_ptr Resolver; + std::unique_ptr TM; + const DataLayout DL; + RTDyldObjectLinkingLayer ObjectLayer; + IRCompileLayerCompileLayer; + +public: + SimpleJIT() + : Resolver(createLegacyLookupResolver( +[this](const std::string ) -> JITSymbol { + if (auto Sym = CompileLayer.findSymbol(Name, false)) +return Sym; + else if (auto Err = Sym.takeError()) +return std::move(Err); + if (auto SymAddr = + RTDyldMemoryManager::getSymbolAddressInProcess(Name)) +return JITSymbol(SymAddr, JITSymbolFlags::Exported); + return nullptr; +}, +[](Error Err) { cantFail(std::move(Err), "lookupFlags failed"); })), +TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), +ObjectLayer(ES, +[this](VModuleKey) { + return RTDyldObjectLinkingLayer::Resources{ + std::make_shared(), Resolver}; +}), +CompileLayer(ObjectLayer, SimpleCompiler(*TM)) { +llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); + } + + TargetMachine () { return *TM; } -int main(int argc, const char **argv, char * const *envp) { + VModuleKey addModule(std::unique_ptr M) { +// Add the module to the JIT with a new VModuleKey. +auto K = ES.allocateVModule(); +cantFail(CompileLayer.addModule(K, std::move(M))); +return K; + } + + JITSymbol findSymbol(const std::string Name) { +std::string MangledName; +raw_string_ostream MangledNameStream(MangledName); +Mangler::getNameWithPrefix(MangledNameStream, Name, DL); +return CompileLayer.findSymbol(MangledNameStream.str(), true); + } + + JITTargetAddress getSymbolAddress(const std::string Name) { +return cantFail(findSymbol(Name).getAddress()); + } + + void removeModule(VModuleKey K) { +cantFail(CompileLayer.removeModule(K)); + } +}; + +} // end namespace orc +} // end namespace llvm + +int main(int argc, const char **argv) { // This