1 //===- ExecutionEngine.cpp - MLIR Execution engine and utils --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the execution engine for MLIR modules based on LLVM Orc 10 // JIT engine. 11 // 12 //===----------------------------------------------------------------------===// 13 #include "mlir/ExecutionEngine/ExecutionEngine.h" 14 #include "mlir/IR/Function.h" 15 #include "mlir/IR/Module.h" 16 #include "mlir/Support/FileUtilities.h" 17 #include "mlir/Target/LLVMIR.h" 18 19 #include "llvm/Bitcode/BitcodeReader.h" 20 #include "llvm/Bitcode/BitcodeWriter.h" 21 #include "llvm/ExecutionEngine/JITEventListener.h" 22 #include "llvm/ExecutionEngine/ObjectCache.h" 23 #include "llvm/ExecutionEngine/Orc/CompileUtils.h" 24 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h" 25 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" 26 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" 27 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h" 28 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" 29 #include "llvm/ExecutionEngine/SectionMemoryManager.h" 30 #include "llvm/IR/IRBuilder.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/Error.h" 33 #include "llvm/Support/Host.h" 34 #include "llvm/Support/TargetRegistry.h" 35 #include "llvm/Support/ToolOutputFile.h" 36 37 #define DEBUG_TYPE "execution-engine" 38 39 using namespace mlir; 40 using llvm::dbgs; 41 using llvm::Error; 42 using llvm::errs; 43 using llvm::Expected; 44 using llvm::LLVMContext; 45 using llvm::MemoryBuffer; 46 using llvm::MemoryBufferRef; 47 using llvm::Module; 48 using llvm::SectionMemoryManager; 49 using llvm::StringError; 50 using llvm::Triple; 51 using llvm::orc::DynamicLibrarySearchGenerator; 52 using llvm::orc::ExecutionSession; 53 using llvm::orc::IRCompileLayer; 54 using llvm::orc::JITTargetMachineBuilder; 55 using llvm::orc::RTDyldObjectLinkingLayer; 56 using llvm::orc::ThreadSafeModule; 57 using llvm::orc::TMOwningSimpleCompiler; 58 59 /// Wrap a string into an llvm::StringError. 60 static Error make_string_error(const Twine &message) { 61 return llvm::make_error<StringError>(message.str(), 62 llvm::inconvertibleErrorCode()); 63 } 64 65 void SimpleObjectCache::notifyObjectCompiled(const Module *M, 66 MemoryBufferRef ObjBuffer) { 67 cachedObjects[M->getModuleIdentifier()] = MemoryBuffer::getMemBufferCopy( 68 ObjBuffer.getBuffer(), ObjBuffer.getBufferIdentifier()); 69 } 70 71 std::unique_ptr<MemoryBuffer> SimpleObjectCache::getObject(const Module *M) { 72 auto I = cachedObjects.find(M->getModuleIdentifier()); 73 if (I == cachedObjects.end()) { 74 LLVM_DEBUG(dbgs() << "No object for " << M->getModuleIdentifier() 75 << " in cache. Compiling.\n"); 76 return nullptr; 77 } 78 LLVM_DEBUG(dbgs() << "Object for " << M->getModuleIdentifier() 79 << " loaded from cache.\n"); 80 return MemoryBuffer::getMemBuffer(I->second->getMemBufferRef()); 81 } 82 83 void SimpleObjectCache::dumpToObjectFile(StringRef outputFilename) { 84 // Set up the output file. 85 std::string errorMessage; 86 auto file = openOutputFile(outputFilename, &errorMessage); 87 if (!file) { 88 llvm::errs() << errorMessage << "\n"; 89 return; 90 } 91 92 // Dump the object generated for a single module to the output file. 93 assert(cachedObjects.size() == 1 && "Expected only one object entry."); 94 auto &cachedObject = cachedObjects.begin()->second; 95 file->os() << cachedObject->getBuffer(); 96 file->keep(); 97 } 98 99 void ExecutionEngine::dumpToObjectFile(StringRef filename) { 100 cache->dumpToObjectFile(filename); 101 } 102 103 // Setup LLVM target triple from the current machine. 104 bool ExecutionEngine::setupTargetTriple(Module *llvmModule) { 105 // Setup the machine properties from the current architecture. 106 auto targetTriple = llvm::sys::getDefaultTargetTriple(); 107 std::string errorMessage; 108 auto target = llvm::TargetRegistry::lookupTarget(targetTriple, errorMessage); 109 if (!target) { 110 errs() << "NO target: " << errorMessage << "\n"; 111 return true; 112 } 113 std::unique_ptr<llvm::TargetMachine> machine( 114 target->createTargetMachine(targetTriple, "generic", "", {}, {})); 115 llvmModule->setDataLayout(machine->createDataLayout()); 116 llvmModule->setTargetTriple(targetTriple); 117 return false; 118 } 119 120 static std::string makePackedFunctionName(StringRef name) { 121 return "_mlir_" + name.str(); 122 } 123 124 // For each function in the LLVM module, define an interface function that wraps 125 // all the arguments of the original function and all its results into an i8** 126 // pointer to provide a unified invocation interface. 127 static void packFunctionArguments(Module *module) { 128 auto &ctx = module->getContext(); 129 llvm::IRBuilder<> builder(ctx); 130 DenseSet<llvm::Function *> interfaceFunctions; 131 for (auto &func : module->getFunctionList()) { 132 if (func.isDeclaration()) { 133 continue; 134 } 135 if (interfaceFunctions.count(&func)) { 136 continue; 137 } 138 139 // Given a function `foo(<...>)`, define the interface function 140 // `mlir_foo(i8**)`. 141 auto newType = llvm::FunctionType::get( 142 builder.getVoidTy(), builder.getInt8PtrTy()->getPointerTo(), 143 /*isVarArg=*/false); 144 auto newName = makePackedFunctionName(func.getName()); 145 auto funcCst = module->getOrInsertFunction(newName, newType); 146 llvm::Function *interfaceFunc = cast<llvm::Function>(funcCst.getCallee()); 147 interfaceFunctions.insert(interfaceFunc); 148 149 // Extract the arguments from the type-erased argument list and cast them to 150 // the proper types. 151 auto bb = llvm::BasicBlock::Create(ctx); 152 bb->insertInto(interfaceFunc); 153 builder.SetInsertPoint(bb); 154 llvm::Value *argList = interfaceFunc->arg_begin(); 155 SmallVector<llvm::Value *, 8> args; 156 args.reserve(llvm::size(func.args())); 157 for (auto &indexedArg : llvm::enumerate(func.args())) { 158 llvm::Value *argIndex = llvm::Constant::getIntegerValue( 159 builder.getInt64Ty(), APInt(64, indexedArg.index())); 160 llvm::Value *argPtrPtr = builder.CreateGEP(argList, argIndex); 161 llvm::Value *argPtr = builder.CreateLoad(argPtrPtr); 162 argPtr = builder.CreateBitCast( 163 argPtr, indexedArg.value().getType()->getPointerTo()); 164 llvm::Value *arg = builder.CreateLoad(argPtr); 165 args.push_back(arg); 166 } 167 168 // Call the implementation function with the extracted arguments. 169 llvm::Value *result = builder.CreateCall(&func, args); 170 171 // Assuming the result is one value, potentially of type `void`. 172 if (!result->getType()->isVoidTy()) { 173 llvm::Value *retIndex = llvm::Constant::getIntegerValue( 174 builder.getInt64Ty(), APInt(64, llvm::size(func.args()))); 175 llvm::Value *retPtrPtr = builder.CreateGEP(argList, retIndex); 176 llvm::Value *retPtr = builder.CreateLoad(retPtrPtr); 177 retPtr = builder.CreateBitCast(retPtr, result->getType()->getPointerTo()); 178 builder.CreateStore(result, retPtr); 179 } 180 181 // The interface function returns void. 182 builder.CreateRetVoid(); 183 } 184 } 185 186 ExecutionEngine::ExecutionEngine(bool enableObjectCache, 187 bool enableGDBNotificationListener) 188 : cache(enableObjectCache ? new SimpleObjectCache() : nullptr), 189 gdbListener(enableGDBNotificationListener 190 ? llvm::JITEventListener::createGDBRegistrationListener() 191 : nullptr) {} 192 193 Expected<std::unique_ptr<ExecutionEngine>> ExecutionEngine::create( 194 ModuleOp m, std::function<Error(llvm::Module *)> transformer, 195 Optional<llvm::CodeGenOpt::Level> jitCodeGenOptLevel, 196 ArrayRef<StringRef> sharedLibPaths, bool enableObjectCache, 197 bool enableGDBNotificationListener) { 198 auto engine = std::make_unique<ExecutionEngine>( 199 enableObjectCache, enableGDBNotificationListener); 200 201 std::unique_ptr<llvm::LLVMContext> ctx(new llvm::LLVMContext); 202 auto llvmModule = translateModuleToLLVMIR(m); 203 if (!llvmModule) 204 return make_string_error("could not convert to LLVM IR"); 205 // FIXME: the triple should be passed to the translation or dialect conversion 206 // instead of this. Currently, the LLVM module created above has no triple 207 // associated with it. 208 setupTargetTriple(llvmModule.get()); 209 packFunctionArguments(llvmModule.get()); 210 211 // Clone module in a new LLVMContext since translateModuleToLLVMIR buries 212 // ownership too deeply. 213 // TODO(zinenko): Reevaluate model of ownership of LLVMContext in LLVMDialect. 214 SmallVector<char, 1> buffer; 215 { 216 llvm::raw_svector_ostream os(buffer); 217 WriteBitcodeToFile(*llvmModule, os); 218 } 219 llvm::MemoryBufferRef bufferRef(StringRef(buffer.data(), buffer.size()), 220 "cloned module buffer"); 221 auto expectedModule = parseBitcodeFile(bufferRef, *ctx); 222 if (!expectedModule) 223 return expectedModule.takeError(); 224 std::unique_ptr<Module> deserModule = std::move(*expectedModule); 225 auto dataLayout = deserModule->getDataLayout(); 226 227 // Callback to create the object layer with symbol resolution to current 228 // process and dynamically linked libraries. 229 auto objectLinkingLayerCreator = [&](ExecutionSession &session, 230 const Triple &TT) { 231 auto objectLayer = std::make_unique<RTDyldObjectLinkingLayer>( 232 session, []() { return std::make_unique<SectionMemoryManager>(); }); 233 objectLayer->setNotifyLoaded( 234 [engine = engine.get()]( 235 llvm::orc::VModuleKey, const llvm::object::ObjectFile &object, 236 const llvm::RuntimeDyld::LoadedObjectInfo &objectInfo) { 237 if (engine->gdbListener) { 238 uint64_t key = static_cast<uint64_t>( 239 reinterpret_cast<uintptr_t>(object.getData().data())); 240 engine->gdbListener->notifyObjectLoaded(key, object, objectInfo); 241 } 242 }); 243 244 // Resolve symbols from shared libraries. 245 for (auto libPath : sharedLibPaths) { 246 auto mb = llvm::MemoryBuffer::getFile(libPath); 247 if (!mb) { 248 errs() << "Fail to create MemoryBuffer for: " << libPath << "\n"; 249 continue; 250 } 251 auto &JD = session.createBareJITDylib(std::string(libPath)); 252 auto loaded = DynamicLibrarySearchGenerator::Load( 253 libPath.data(), dataLayout.getGlobalPrefix()); 254 if (!loaded) { 255 errs() << "Could not load " << libPath << ":\n " << loaded.takeError() 256 << "\n"; 257 continue; 258 } 259 JD.addGenerator(std::move(*loaded)); 260 cantFail(objectLayer->add(JD, std::move(mb.get()))); 261 } 262 263 return objectLayer; 264 }; 265 266 // Callback to inspect the cache and recompile on demand. This follows Lang's 267 // LLJITWithObjectCache example. 268 auto compileFunctionCreator = [&](JITTargetMachineBuilder JTMB) 269 -> Expected<std::unique_ptr<IRCompileLayer::IRCompiler>> { 270 if (jitCodeGenOptLevel) 271 JTMB.setCodeGenOptLevel(jitCodeGenOptLevel.getValue()); 272 auto TM = JTMB.createTargetMachine(); 273 if (!TM) 274 return TM.takeError(); 275 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM), 276 engine->cache.get()); 277 }; 278 279 // Create the LLJIT by calling the LLJITBuilder with 2 callbacks. 280 auto jit = 281 cantFail(llvm::orc::LLJITBuilder() 282 .setCompileFunctionCreator(compileFunctionCreator) 283 .setObjectLinkingLayerCreator(objectLinkingLayerCreator) 284 .create()); 285 286 // Add a ThreadSafemodule to the engine and return. 287 ThreadSafeModule tsm(std::move(deserModule), std::move(ctx)); 288 if (transformer) 289 cantFail(tsm.withModuleDo( 290 [&](llvm::Module &module) { return transformer(&module); })); 291 cantFail(jit->addIRModule(std::move(tsm))); 292 engine->jit = std::move(jit); 293 294 // Resolve symbols that are statically linked in the current process. 295 llvm::orc::JITDylib &mainJD = engine->jit->getMainJITDylib(); 296 mainJD.addGenerator( 297 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess( 298 dataLayout.getGlobalPrefix()))); 299 300 return std::move(engine); 301 } 302 303 Expected<void (*)(void **)> ExecutionEngine::lookup(StringRef name) const { 304 auto expectedSymbol = jit->lookup(makePackedFunctionName(name)); 305 306 // JIT lookup may return an Error referring to strings stored internally by 307 // the JIT. If the Error outlives the ExecutionEngine, it would want have a 308 // dangling reference, which is currently caught by an assertion inside JIT 309 // thanks to hand-rolled reference counting. Rewrap the error message into a 310 // string before returning. Alternatively, ORC JIT should consider copying 311 // the string into the error message. 312 if (!expectedSymbol) { 313 std::string errorMessage; 314 llvm::raw_string_ostream os(errorMessage); 315 llvm::handleAllErrors(expectedSymbol.takeError(), 316 [&os](llvm::ErrorInfoBase &ei) { ei.log(os); }); 317 return make_string_error(os.str()); 318 } 319 320 auto rawFPtr = expectedSymbol->getAddress(); 321 auto fptr = reinterpret_cast<void (*)(void **)>(rawFPtr); 322 if (!fptr) 323 return make_string_error("looked up function is null"); 324 return fptr; 325 } 326 327 Error ExecutionEngine::invoke(StringRef name, MutableArrayRef<void *> args) { 328 auto expectedFPtr = lookup(name); 329 if (!expectedFPtr) 330 return expectedFPtr.takeError(); 331 auto fptr = *expectedFPtr; 332 333 (*fptr)(args.data()); 334 335 return Error::success(); 336 } 337