r366511 - Update the SimpleJIT class in the clang-interpreter example to use ORCv2.
Hans Wennborg via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 19 02:55:02 PDT 2019
Merged to 9.0 in r366556.
On Fri, Jul 19, 2019 at 12:46 AM Lang Hames via cfe-commits
<cfe-commits at lists.llvm.org> wrote:
>
> Author: lhames
> Date: Thu Jul 18 15:47:18 2019
> New Revision: 366511
>
> URL: http://llvm.org/viewvc/llvm-project?rev=366511&view=rev
> Log:
> Update the SimpleJIT class in the clang-interpreter example to use ORCv2.
>
> This will remove the ORCv1 deprecation warnings.
>
> Modified:
> cfe/trunk/examples/clang-interpreter/main.cpp
>
> Modified: cfe/trunk/examples/clang-interpreter/main.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/main.cpp?rev=366511&r1=366510&r2=366511&view=diff
> ==============================================================================
> --- cfe/trunk/examples/clang-interpreter/main.cpp (original)
> +++ cfe/trunk/examples/clang-interpreter/main.cpp Thu Jul 18 15:47:18 2019
> @@ -18,6 +18,7 @@
> #include "llvm/ADT/SmallString.h"
> #include "llvm/ExecutionEngine/ExecutionEngine.h"
> #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
> +#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
> #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
> #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
> #include "llvm/ExecutionEngine/SectionMemoryManager.h"
> @@ -50,65 +51,69 @@ namespace orc {
> class SimpleJIT {
> private:
> ExecutionSession ES;
> - std::shared_ptr<SymbolResolver> Resolver;
> std::unique_ptr<TargetMachine> TM;
> const DataLayout DL;
> - LegacyRTDyldObjectLinkingLayer ObjectLayer;
> - LegacyIRCompileLayer<decltype(ObjectLayer), SimpleCompiler> CompileLayer;
> + MangleAndInterner Mangle{ES, DL};
> + RTDyldObjectLinkingLayer ObjectLayer{ES, createMemMgr};
> + IRCompileLayer CompileLayer{ES, ObjectLayer, SimpleCompiler(*TM)};
>
> -public:
> - SimpleJIT()
> - : Resolver(createLegacyLookupResolver(
> - ES,
> - [this](const std::string &Name) -> 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 LegacyRTDyldObjectLinkingLayer::Resources{
> - std::make_shared<SectionMemoryManager>(), Resolver};
> - }),
> - CompileLayer(ObjectLayer, SimpleCompiler(*TM)) {
> + static std::unique_ptr<SectionMemoryManager> createMemMgr() {
> + return llvm::make_unique<SectionMemoryManager>();
> + }
> +
> + SimpleJIT(std::unique_ptr<TargetMachine> TM, DataLayout DL,
> + DynamicLibrarySearchGenerator ProcessSymbolsGenerator)
> + : TM(std::move(TM)), DL(std::move(DL)) {
> llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
> + ES.getMainJITDylib().setGenerator(std::move(ProcessSymbolsGenerator));
> }
>
> - const TargetMachine &getTargetMachine() const { return *TM; }
> +public:
> + static Expected<std::unique_ptr<SimpleJIT>> Create() {
> + auto JTMB = JITTargetMachineBuilder::detectHost();
> + if (!JTMB)
> + return JTMB.takeError();
> +
> + auto TM = JTMB->createTargetMachine();
> + if (!TM)
> + return TM.takeError();
> +
> + auto DL = (*TM)->createDataLayout();
> +
> + auto ProcessSymbolsGenerator =
> + DynamicLibrarySearchGenerator::GetForCurrentProcess(
> + DL.getGlobalPrefix());
> +
> + if (!ProcessSymbolsGenerator)
> + return ProcessSymbolsGenerator.takeError();
>
> - VModuleKey addModule(std::unique_ptr<Module> M) {
> - // Add the module to the JIT with a new VModuleKey.
> - auto K = ES.allocateVModule();
> - cantFail(CompileLayer.addModule(K, std::move(M)));
> - return K;
> + return std::unique_ptr<SimpleJIT>(new SimpleJIT(
> + std::move(*TM), std::move(DL), std::move(*ProcessSymbolsGenerator)));
> }
>
> - JITSymbol findSymbol(const StringRef &Name) {
> - std::string MangledName;
> - raw_string_ostream MangledNameStream(MangledName);
> - Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
> - return CompileLayer.findSymbol(MangledNameStream.str(), true);
> + const TargetMachine &getTargetMachine() const { return *TM; }
> +
> + Error addModule(ThreadSafeModule M) {
> + return CompileLayer.add(ES.getMainJITDylib(), std::move(M));
> }
>
> - JITTargetAddress getSymbolAddress(const StringRef &Name) {
> - return cantFail(findSymbol(Name).getAddress());
> + Expected<JITEvaluatedSymbol> findSymbol(const StringRef &Name) {
> + return ES.lookup({&ES.getMainJITDylib()}, Mangle(Name));
> }
>
> - void removeModule(VModuleKey K) {
> - cantFail(CompileLayer.removeModule(K));
> + Expected<JITTargetAddress> getSymbolAddress(const StringRef &Name) {
> + auto Sym = findSymbol(Name);
> + if (!Sym)
> + return Sym.takeError();
> + return Sym->getAddress();
> }
> };
>
> } // end namespace orc
> } // end namespace llvm
>
> +llvm::ExitOnError ExitOnErr;
> +
> int main(int argc, const char **argv) {
> // This just needs to be some symbol in the binary; C++ doesn't
> // allow taking the address of ::main however.
> @@ -130,6 +135,8 @@ int main(int argc, const char **argv) {
> T.setObjectFormat(llvm::Triple::ELF);
> #endif
>
> + ExitOnErr.setBanner("clang interpreter");
> +
> Driver TheDriver(Path, T.str(), Diags);
> TheDriver.setTitle("clang interpreter");
> TheDriver.setCheckInputsExist(false);
> @@ -204,14 +211,16 @@ int main(int argc, const char **argv) {
> llvm::InitializeNativeTargetAsmPrinter();
>
> int Res = 255;
> + std::unique_ptr<llvm::LLVMContext> Ctx(Act->takeLLVMContext());
> std::unique_ptr<llvm::Module> Module = Act->takeModule();
>
> if (Module) {
> - llvm::orc::SimpleJIT J;
> - auto H = J.addModule(std::move(Module));
> - auto Main = (int(*)(...))J.getSymbolAddress("main");
> + auto J = ExitOnErr(llvm::orc::SimpleJIT::Create());
> +
> + ExitOnErr(J->addModule(
> + llvm::orc::ThreadSafeModule(std::move(Module), std::move(Ctx))));
> + auto Main = (int (*)(...))ExitOnErr(J->getSymbolAddress("main"));
> Res = Main();
> - J.removeModule(H);
> }
>
> // Shutdown.
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list