15a440378SAlex Zinenko //===- ExecutionEngine.cpp - MLIR Execution engine and utils --------------===// 25a440378SAlex Zinenko // 35a440378SAlex Zinenko // Copyright 2019 The MLIR Authors. 45a440378SAlex Zinenko // 55a440378SAlex Zinenko // Licensed under the Apache License, Version 2.0 (the "License"); 65a440378SAlex Zinenko // you may not use this file except in compliance with the License. 75a440378SAlex Zinenko // You may obtain a copy of the License at 85a440378SAlex Zinenko // 95a440378SAlex Zinenko // http://www.apache.org/licenses/LICENSE-2.0 105a440378SAlex Zinenko // 115a440378SAlex Zinenko // Unless required by applicable law or agreed to in writing, software 125a440378SAlex Zinenko // distributed under the License is distributed on an "AS IS" BASIS, 135a440378SAlex Zinenko // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 145a440378SAlex Zinenko // See the License for the specific language governing permissions and 155a440378SAlex Zinenko // limitations under the License. 165a440378SAlex Zinenko // ============================================================================= 175a440378SAlex Zinenko // 185a440378SAlex Zinenko // This file implements the execution engine for MLIR modules based on LLVM Orc 195a440378SAlex Zinenko // JIT engine. 205a440378SAlex Zinenko // 215a440378SAlex Zinenko //===----------------------------------------------------------------------===// 225a440378SAlex Zinenko #include "mlir/ExecutionEngine/ExecutionEngine.h" 235a440378SAlex Zinenko #include "mlir/IR/Function.h" 245a440378SAlex Zinenko #include "mlir/IR/Module.h" 255a440378SAlex Zinenko #include "mlir/Target/LLVMIR.h" 265a440378SAlex Zinenko 275a440378SAlex Zinenko #include "llvm/ExecutionEngine/Orc/CompileUtils.h" 285a440378SAlex Zinenko #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 295a440378SAlex Zinenko #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 308093f17aSAlex Zinenko #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" 315a440378SAlex Zinenko #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 325a440378SAlex Zinenko #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 335a440378SAlex Zinenko #include "llvm/ExecutionEngine/SectionMemoryManager.h" 345a440378SAlex Zinenko #include "llvm/IR/IRBuilder.h" 355a440378SAlex Zinenko #include "llvm/Support/Error.h" 365a440378SAlex Zinenko #include "llvm/Support/TargetRegistry.h" 375a440378SAlex Zinenko 385a440378SAlex Zinenko using namespace mlir; 395a440378SAlex Zinenko using llvm::Error; 405a440378SAlex Zinenko using llvm::Expected; 415a440378SAlex Zinenko 425a440378SAlex Zinenko namespace { 435a440378SAlex Zinenko // Memory manager for the JIT's objectLayer. Its main goal is to fallback to 445a440378SAlex Zinenko // resolving functions in the current process if they cannot be resolved in the 455a440378SAlex Zinenko // JIT-compiled modules. 465a440378SAlex Zinenko class MemoryManager : public llvm::SectionMemoryManager { 475a440378SAlex Zinenko public: 485a440378SAlex Zinenko MemoryManager(llvm::orc::ExecutionSession &execSession) 495a440378SAlex Zinenko : session(execSession) {} 505a440378SAlex Zinenko 515a440378SAlex Zinenko // Resolve the named symbol. First, try looking it up in the main library of 525a440378SAlex Zinenko // the execution session. If there is no such symbol, try looking it up in 535a440378SAlex Zinenko // the current process (for example, if it is a standard library function). 545a440378SAlex Zinenko // Return `nullptr` if lookup fails. 555a440378SAlex Zinenko llvm::JITSymbol findSymbol(const std::string &name) override { 565a440378SAlex Zinenko auto mainLibSymbol = session.lookup({&session.getMainJITDylib()}, name); 575a440378SAlex Zinenko if (mainLibSymbol) 585a440378SAlex Zinenko return mainLibSymbol.get(); 595a440378SAlex Zinenko auto address = llvm::RTDyldMemoryManager::getSymbolAddressInProcess(name); 605a440378SAlex Zinenko if (!address) { 615a440378SAlex Zinenko llvm::errs() << "Could not look up: " << name << '\n'; 625a440378SAlex Zinenko return nullptr; 635a440378SAlex Zinenko } 645a440378SAlex Zinenko return llvm::JITSymbol(address, llvm::JITSymbolFlags::Exported); 655a440378SAlex Zinenko } 665a440378SAlex Zinenko 675a440378SAlex Zinenko private: 685a440378SAlex Zinenko llvm::orc::ExecutionSession &session; 695a440378SAlex Zinenko }; 705a440378SAlex Zinenko } // end anonymous namespace 715a440378SAlex Zinenko 725a440378SAlex Zinenko namespace mlir { 735a440378SAlex Zinenko namespace impl { 745c64d2a6SNicolas Vasilache 755c64d2a6SNicolas Vasilache /// Wrapper class around DynamicLibrarySearchGenerator to allow searching 765c64d2a6SNicolas Vasilache /// in-process symbols that have not been explicitly exported. 775c64d2a6SNicolas Vasilache /// This first tries to resolve a symbol by using DynamicLibrarySearchGenerator. 785c64d2a6SNicolas Vasilache /// For symbols that are not found this way, it then uses 795c64d2a6SNicolas Vasilache /// `llvm::sys::DynamicLibrary::SearchForAddressOfSymbol` to extract symbols 805c64d2a6SNicolas Vasilache /// that have been explicitly added with `llvm::sys::DynamicLibrary::AddSymbol`, 815c64d2a6SNicolas Vasilache /// previously. 825c64d2a6SNicolas Vasilache class SearchGenerator { 835c64d2a6SNicolas Vasilache public: 845c64d2a6SNicolas Vasilache SearchGenerator(char GlobalPrefix) 855c64d2a6SNicolas Vasilache : defaultGenerator(cantFail( 865c64d2a6SNicolas Vasilache llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess( 875c64d2a6SNicolas Vasilache GlobalPrefix))) {} 885c64d2a6SNicolas Vasilache 895c64d2a6SNicolas Vasilache // This function forwards to DynamicLibrarySearchGenerator::operator() and 905c64d2a6SNicolas Vasilache // adds an extra resolution for names explicitly registered via 915c64d2a6SNicolas Vasilache // `llvm::sys::DynamicLibrary::AddSymbol`. 925c64d2a6SNicolas Vasilache Expected<llvm::orc::SymbolNameSet> 935c64d2a6SNicolas Vasilache operator()(llvm::orc::JITDylib &JD, const llvm::orc::SymbolNameSet &Names) { 945c64d2a6SNicolas Vasilache auto res = defaultGenerator(JD, Names); 955c64d2a6SNicolas Vasilache if (!res) 965c64d2a6SNicolas Vasilache return res; 975c64d2a6SNicolas Vasilache llvm::orc::SymbolMap newSymbols; 985c64d2a6SNicolas Vasilache for (auto &Name : Names) { 995c64d2a6SNicolas Vasilache if (res.get().count(Name) > 0) 1005c64d2a6SNicolas Vasilache continue; 1015c64d2a6SNicolas Vasilache res.get().insert(Name); 1025c64d2a6SNicolas Vasilache auto addedSymbolAddress = 1035c64d2a6SNicolas Vasilache llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(*Name); 1045c64d2a6SNicolas Vasilache if (!addedSymbolAddress) 1055c64d2a6SNicolas Vasilache continue; 1065c64d2a6SNicolas Vasilache llvm::JITEvaluatedSymbol Sym( 1075c64d2a6SNicolas Vasilache reinterpret_cast<uintptr_t>(addedSymbolAddress), 1085c64d2a6SNicolas Vasilache llvm::JITSymbolFlags::Exported); 1095c64d2a6SNicolas Vasilache newSymbols[Name] = Sym; 1105c64d2a6SNicolas Vasilache } 1115c64d2a6SNicolas Vasilache if (!newSymbols.empty()) 1125c64d2a6SNicolas Vasilache cantFail(JD.define(absoluteSymbols(std::move(newSymbols)))); 1135c64d2a6SNicolas Vasilache return res; 1145c64d2a6SNicolas Vasilache } 1155c64d2a6SNicolas Vasilache 1165c64d2a6SNicolas Vasilache private: 1175c64d2a6SNicolas Vasilache llvm::orc::DynamicLibrarySearchGenerator defaultGenerator; 1185c64d2a6SNicolas Vasilache }; 1195c64d2a6SNicolas Vasilache 1205a440378SAlex Zinenko // Simple layered Orc JIT compilation engine. 1215a440378SAlex Zinenko class OrcJIT { 1225a440378SAlex Zinenko public: 1238093f17aSAlex Zinenko using IRTransformer = std::function<Error(llvm::Module *)>; 1248093f17aSAlex Zinenko 1255a440378SAlex Zinenko // Construct a JIT engine for the target host defined by `machineBuilder`, 1265a440378SAlex Zinenko // using the data layout provided as `dataLayout`. 1275c64d2a6SNicolas Vasilache // Setup the object layer to use our custom memory manager in order to 1285c64d2a6SNicolas Vasilache // resolve calls to library functions present in the process. 1295a440378SAlex Zinenko OrcJIT(llvm::orc::JITTargetMachineBuilder machineBuilder, 1306aa5cc8bSNicolas Vasilache llvm::DataLayout layout, IRTransformer transform, 1316aa5cc8bSNicolas Vasilache ArrayRef<StringRef> sharedLibPaths) 1328093f17aSAlex Zinenko : irTransformer(transform), 1338093f17aSAlex Zinenko objectLayer( 1345a440378SAlex Zinenko session, 1355a440378SAlex Zinenko [this]() { return llvm::make_unique<MemoryManager>(session); }), 1365a440378SAlex Zinenko compileLayer( 1375a440378SAlex Zinenko session, objectLayer, 1385a440378SAlex Zinenko llvm::orc::ConcurrentIRCompiler(std::move(machineBuilder))), 1398093f17aSAlex Zinenko transformLayer(session, compileLayer, makeIRTransformFunction()), 1405a440378SAlex Zinenko dataLayout(layout), mangler(session, this->dataLayout), 1415a440378SAlex Zinenko threadSafeCtx(llvm::make_unique<llvm::LLVMContext>()) { 1425a440378SAlex Zinenko session.getMainJITDylib().setGenerator( 1435c64d2a6SNicolas Vasilache SearchGenerator(layout.getGlobalPrefix())); 1446aa5cc8bSNicolas Vasilache loadLibraries(sharedLibPaths); 1455a440378SAlex Zinenko } 1465a440378SAlex Zinenko 1475a440378SAlex Zinenko // Create a JIT engine for the current host. 1488093f17aSAlex Zinenko static Expected<std::unique_ptr<OrcJIT>> 1496aa5cc8bSNicolas Vasilache createDefault(IRTransformer transformer, ArrayRef<StringRef> sharedLibPaths) { 1505a440378SAlex Zinenko auto machineBuilder = llvm::orc::JITTargetMachineBuilder::detectHost(); 1515a440378SAlex Zinenko if (!machineBuilder) 1525a440378SAlex Zinenko return machineBuilder.takeError(); 1535a440378SAlex Zinenko 1545a440378SAlex Zinenko auto dataLayout = machineBuilder->getDefaultDataLayoutForTarget(); 1555a440378SAlex Zinenko if (!dataLayout) 1565a440378SAlex Zinenko return dataLayout.takeError(); 1575a440378SAlex Zinenko 1585a440378SAlex Zinenko return llvm::make_unique<OrcJIT>(std::move(*machineBuilder), 1596aa5cc8bSNicolas Vasilache std::move(*dataLayout), transformer, 1606aa5cc8bSNicolas Vasilache sharedLibPaths); 1615a440378SAlex Zinenko } 1625a440378SAlex Zinenko 1635a440378SAlex Zinenko // Add an LLVM module to the main library managed by the JIT engine. 1645a440378SAlex Zinenko Error addModule(std::unique_ptr<llvm::Module> M) { 165f5b99275SAlex Zinenko return transformLayer.add( 1665a440378SAlex Zinenko session.getMainJITDylib(), 1675a440378SAlex Zinenko llvm::orc::ThreadSafeModule(std::move(M), threadSafeCtx)); 1685a440378SAlex Zinenko } 1695a440378SAlex Zinenko 1705a440378SAlex Zinenko // Lookup a symbol in the main library managed by the JIT engine. 1715a440378SAlex Zinenko Expected<llvm::JITEvaluatedSymbol> lookup(StringRef Name) { 1725a440378SAlex Zinenko return session.lookup({&session.getMainJITDylib()}, mangler(Name.str())); 1735a440378SAlex Zinenko } 1745a440378SAlex Zinenko 1755a440378SAlex Zinenko private: 1768093f17aSAlex Zinenko // Wrap the `irTransformer` into a function that can be called by the 1775c64d2a6SNicolas Vasilache // IRTranformLayer. If `irTransformer` is not set up, return the module as 1785c64d2a6SNicolas Vasilache // is without errors. 1798093f17aSAlex Zinenko llvm::orc::IRTransformLayer::TransformFunction makeIRTransformFunction() { 1808093f17aSAlex Zinenko return [this](llvm::orc::ThreadSafeModule module, 1818093f17aSAlex Zinenko const llvm::orc::MaterializationResponsibility &resp) 1828093f17aSAlex Zinenko -> Expected<llvm::orc::ThreadSafeModule> { 1838093f17aSAlex Zinenko (void)resp; 1848093f17aSAlex Zinenko if (!irTransformer) 185e7111fd6SJacques Pienaar return std::move(module); 1868093f17aSAlex Zinenko if (Error err = irTransformer(module.getModule())) 1878093f17aSAlex Zinenko return std::move(err); 188e7111fd6SJacques Pienaar return std::move(module); 1898093f17aSAlex Zinenko }; 1908093f17aSAlex Zinenko } 1918093f17aSAlex Zinenko 1926aa5cc8bSNicolas Vasilache // Iterate over shareLibPaths and load the corresponding libraries for symbol 1936aa5cc8bSNicolas Vasilache // resolution. 1946aa5cc8bSNicolas Vasilache void loadLibraries(ArrayRef<StringRef> sharedLibPaths); 1956aa5cc8bSNicolas Vasilache 1968093f17aSAlex Zinenko IRTransformer irTransformer; 1975a440378SAlex Zinenko llvm::orc::ExecutionSession session; 1985a440378SAlex Zinenko llvm::orc::RTDyldObjectLinkingLayer objectLayer; 1995a440378SAlex Zinenko llvm::orc::IRCompileLayer compileLayer; 2008093f17aSAlex Zinenko llvm::orc::IRTransformLayer transformLayer; 2015a440378SAlex Zinenko llvm::DataLayout dataLayout; 2025a440378SAlex Zinenko llvm::orc::MangleAndInterner mangler; 2035a440378SAlex Zinenko llvm::orc::ThreadSafeContext threadSafeCtx; 2045a440378SAlex Zinenko }; 2055a440378SAlex Zinenko } // end namespace impl 2065a440378SAlex Zinenko } // namespace mlir 2075a440378SAlex Zinenko 2086aa5cc8bSNicolas Vasilache void mlir::impl::OrcJIT::loadLibraries(ArrayRef<StringRef> sharedLibPaths) { 2096aa5cc8bSNicolas Vasilache for (auto libPath : sharedLibPaths) { 2106aa5cc8bSNicolas Vasilache auto mb = llvm::MemoryBuffer::getFile(libPath); 2116aa5cc8bSNicolas Vasilache if (!mb) { 2126aa5cc8bSNicolas Vasilache llvm::errs() << "Could not create MemoryBuffer for: " << libPath << " " 2136aa5cc8bSNicolas Vasilache << mb.getError().message() << "\n"; 2146aa5cc8bSNicolas Vasilache continue; 2156aa5cc8bSNicolas Vasilache } 2166aa5cc8bSNicolas Vasilache auto &JD = session.createJITDylib(libPath); 2176aa5cc8bSNicolas Vasilache auto loaded = llvm::orc::DynamicLibrarySearchGenerator::Load( 2186aa5cc8bSNicolas Vasilache libPath.data(), dataLayout.getGlobalPrefix()); 2196aa5cc8bSNicolas Vasilache if (!loaded) { 2206aa5cc8bSNicolas Vasilache llvm::errs() << "Could not load: " << libPath << " " << loaded.takeError() 2216aa5cc8bSNicolas Vasilache << "\n"; 2226aa5cc8bSNicolas Vasilache continue; 2236aa5cc8bSNicolas Vasilache } 2246aa5cc8bSNicolas Vasilache JD.setGenerator(loaded.get()); 2256aa5cc8bSNicolas Vasilache auto res = objectLayer.add(JD, std::move(mb.get())); 2266aa5cc8bSNicolas Vasilache if (res) 2276aa5cc8bSNicolas Vasilache llvm::errs() << "Could not add: " << libPath << " " << res << "\n"; 2286aa5cc8bSNicolas Vasilache } 2296aa5cc8bSNicolas Vasilache } 2306aa5cc8bSNicolas Vasilache 2315a440378SAlex Zinenko // Wrap a string into an llvm::StringError. 2325a440378SAlex Zinenko static inline Error make_string_error(const llvm::Twine &message) { 2335a440378SAlex Zinenko return llvm::make_error<llvm::StringError>(message.str(), 2345a440378SAlex Zinenko llvm::inconvertibleErrorCode()); 2355a440378SAlex Zinenko } 2365a440378SAlex Zinenko 2375a440378SAlex Zinenko // Setup LLVM target triple from the current machine. 2386c6ed466SMehdi Amini bool ExecutionEngine::setupTargetTriple(llvm::Module *llvmModule) { 2395a440378SAlex Zinenko // Setup the machine properties from the current architecture. 2405a440378SAlex Zinenko auto targetTriple = llvm::sys::getDefaultTargetTriple(); 2415a440378SAlex Zinenko std::string errorMessage; 2425a440378SAlex Zinenko auto target = llvm::TargetRegistry::lookupTarget(targetTriple, errorMessage); 2435a440378SAlex Zinenko if (!target) { 2445a440378SAlex Zinenko llvm::errs() << "NO target: " << errorMessage << "\n"; 2455a440378SAlex Zinenko return true; 2465a440378SAlex Zinenko } 2475a440378SAlex Zinenko auto machine = 2485a440378SAlex Zinenko target->createTargetMachine(targetTriple, "generic", "", {}, {}); 2495a440378SAlex Zinenko llvmModule->setDataLayout(machine->createDataLayout()); 2505a440378SAlex Zinenko llvmModule->setTargetTriple(targetTriple); 2515a440378SAlex Zinenko return false; 2525a440378SAlex Zinenko } 2535a440378SAlex Zinenko 2545a440378SAlex Zinenko static std::string makePackedFunctionName(StringRef name) { 2555a440378SAlex Zinenko return "_mlir_" + name.str(); 2565a440378SAlex Zinenko } 2575a440378SAlex Zinenko 2585a440378SAlex Zinenko // For each function in the LLVM module, define an interface function that wraps 2595a440378SAlex Zinenko // all the arguments of the original function and all its results into an i8** 2605a440378SAlex Zinenko // pointer to provide a unified invocation interface. 2615a440378SAlex Zinenko void packFunctionArguments(llvm::Module *module) { 2625a440378SAlex Zinenko auto &ctx = module->getContext(); 2635a440378SAlex Zinenko llvm::IRBuilder<> builder(ctx); 2645a440378SAlex Zinenko llvm::DenseSet<llvm::Function *> interfaceFunctions; 2655a440378SAlex Zinenko for (auto &func : module->getFunctionList()) { 2665a440378SAlex Zinenko if (func.isDeclaration()) { 2675a440378SAlex Zinenko continue; 2685a440378SAlex Zinenko } 2695a440378SAlex Zinenko if (interfaceFunctions.count(&func)) { 2705a440378SAlex Zinenko continue; 2715a440378SAlex Zinenko } 2725a440378SAlex Zinenko 2735a440378SAlex Zinenko // Given a function `foo(<...>)`, define the interface function 2745a440378SAlex Zinenko // `mlir_foo(i8**)`. 2755a440378SAlex Zinenko auto newType = llvm::FunctionType::get( 2765a440378SAlex Zinenko builder.getVoidTy(), builder.getInt8PtrTy()->getPointerTo(), 2775a440378SAlex Zinenko /*isVarArg=*/false); 2785a440378SAlex Zinenko auto newName = makePackedFunctionName(func.getName()); 279c46b0feaSRiver Riddle auto funcCst = module->getOrInsertFunction(newName, newType); 280c46b0feaSRiver Riddle llvm::Function *interfaceFunc = 281c46b0feaSRiver Riddle llvm::cast<llvm::Function>(funcCst.getCallee()); 2825a440378SAlex Zinenko interfaceFunctions.insert(interfaceFunc); 2835a440378SAlex Zinenko 2845a440378SAlex Zinenko // Extract the arguments from the type-erased argument list and cast them to 2855a440378SAlex Zinenko // the proper types. 2865a440378SAlex Zinenko auto bb = llvm::BasicBlock::Create(ctx); 2875a440378SAlex Zinenko bb->insertInto(interfaceFunc); 2885a440378SAlex Zinenko builder.SetInsertPoint(bb); 2895a440378SAlex Zinenko llvm::Value *argList = interfaceFunc->arg_begin(); 2905a440378SAlex Zinenko llvm::SmallVector<llvm::Value *, 8> args; 2915a440378SAlex Zinenko args.reserve(llvm::size(func.args())); 2925a440378SAlex Zinenko for (auto &indexedArg : llvm::enumerate(func.args())) { 2935a440378SAlex Zinenko llvm::Value *argIndex = llvm::Constant::getIntegerValue( 2945a440378SAlex Zinenko builder.getInt64Ty(), llvm::APInt(64, indexedArg.index())); 2955a440378SAlex Zinenko llvm::Value *argPtrPtr = builder.CreateGEP(argList, argIndex); 2965a440378SAlex Zinenko llvm::Value *argPtr = builder.CreateLoad(argPtrPtr); 2975a440378SAlex Zinenko argPtr = builder.CreateBitCast( 2985a440378SAlex Zinenko argPtr, indexedArg.value().getType()->getPointerTo()); 2995a440378SAlex Zinenko llvm::Value *arg = builder.CreateLoad(argPtr); 3005a440378SAlex Zinenko args.push_back(arg); 3015a440378SAlex Zinenko } 3025a440378SAlex Zinenko 3035a440378SAlex Zinenko // Call the implementation function with the extracted arguments. 3045a440378SAlex Zinenko llvm::Value *result = builder.CreateCall(&func, args); 3055a440378SAlex Zinenko 3065a440378SAlex Zinenko // Assuming the result is one value, potentially of type `void`. 3075a440378SAlex Zinenko if (!result->getType()->isVoidTy()) { 3085a440378SAlex Zinenko llvm::Value *retIndex = llvm::Constant::getIntegerValue( 3095a440378SAlex Zinenko builder.getInt64Ty(), llvm::APInt(64, llvm::size(func.args()))); 3105a440378SAlex Zinenko llvm::Value *retPtrPtr = builder.CreateGEP(argList, retIndex); 3115a440378SAlex Zinenko llvm::Value *retPtr = builder.CreateLoad(retPtrPtr); 3125a440378SAlex Zinenko retPtr = builder.CreateBitCast(retPtr, result->getType()->getPointerTo()); 3135a440378SAlex Zinenko builder.CreateStore(result, retPtr); 3145a440378SAlex Zinenko } 3155a440378SAlex Zinenko 3165a440378SAlex Zinenko // The interface function returns void. 3175a440378SAlex Zinenko builder.CreateRetVoid(); 3185a440378SAlex Zinenko } 3195a440378SAlex Zinenko } 3205a440378SAlex Zinenko 321d9ce382fSMehdi Amini // Out of line for PIMPL unique_ptr. 322d9ce382fSMehdi Amini ExecutionEngine::~ExecutionEngine() = default; 3235a440378SAlex Zinenko 3246aa5cc8bSNicolas Vasilache Expected<std::unique_ptr<ExecutionEngine>> 325*206e55ccSRiver Riddle ExecutionEngine::create(Module m, 3266aa5cc8bSNicolas Vasilache std::function<llvm::Error(llvm::Module *)> transformer, 3276aa5cc8bSNicolas Vasilache ArrayRef<StringRef> sharedLibPaths) { 3285a440378SAlex Zinenko auto engine = llvm::make_unique<ExecutionEngine>(); 3296aa5cc8bSNicolas Vasilache auto expectedJIT = impl::OrcJIT::createDefault(transformer, sharedLibPaths); 3305a440378SAlex Zinenko if (!expectedJIT) 3315a440378SAlex Zinenko return expectedJIT.takeError(); 3325a440378SAlex Zinenko 333*206e55ccSRiver Riddle auto llvmModule = translateModuleToLLVMIR(m); 3345a440378SAlex Zinenko if (!llvmModule) 3355a440378SAlex Zinenko return make_string_error("could not convert to LLVM IR"); 3365a440378SAlex Zinenko // FIXME: the triple should be passed to the translation or dialect conversion 3375a440378SAlex Zinenko // instead of this. Currently, the LLVM module created above has no triple 3385a440378SAlex Zinenko // associated with it. 3395a440378SAlex Zinenko setupTargetTriple(llvmModule.get()); 3405a440378SAlex Zinenko packFunctionArguments(llvmModule.get()); 3415a440378SAlex Zinenko 342cacf0589SNicolas Vasilache if (auto err = (*expectedJIT)->addModule(std::move(llvmModule))) 3435a440378SAlex Zinenko return std::move(err); 344cacf0589SNicolas Vasilache engine->jit = std::move(*expectedJIT); 3455a440378SAlex Zinenko 346e7111fd6SJacques Pienaar return std::move(engine); 3475a440378SAlex Zinenko } 3485a440378SAlex Zinenko 3495a440378SAlex Zinenko Expected<void (*)(void **)> ExecutionEngine::lookup(StringRef name) const { 3505a440378SAlex Zinenko auto expectedSymbol = jit->lookup(makePackedFunctionName(name)); 3515a440378SAlex Zinenko if (!expectedSymbol) 3525a440378SAlex Zinenko return expectedSymbol.takeError(); 3535a440378SAlex Zinenko auto rawFPtr = expectedSymbol->getAddress(); 3545a440378SAlex Zinenko auto fptr = reinterpret_cast<void (*)(void **)>(rawFPtr); 3555a440378SAlex Zinenko if (!fptr) 3565a440378SAlex Zinenko return make_string_error("looked up function is null"); 3575a440378SAlex Zinenko return fptr; 3585a440378SAlex Zinenko } 359629f5b7fSNicolas Vasilache 360629f5b7fSNicolas Vasilache llvm::Error ExecutionEngine::invoke(StringRef name, 361629f5b7fSNicolas Vasilache MutableArrayRef<void *> args) { 362629f5b7fSNicolas Vasilache auto expectedFPtr = lookup(name); 363629f5b7fSNicolas Vasilache if (!expectedFPtr) 364629f5b7fSNicolas Vasilache return expectedFPtr.takeError(); 365629f5b7fSNicolas Vasilache auto fptr = *expectedFPtr; 366629f5b7fSNicolas Vasilache 367629f5b7fSNicolas Vasilache (*fptr)(args.data()); 368629f5b7fSNicolas Vasilache 369629f5b7fSNicolas Vasilache return llvm::Error::success(); 370629f5b7fSNicolas Vasilache } 371