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/Dialect/LLVMIR/LLVMDialect.h"
15 #include "mlir/IR/BuiltinOps.h"
16 #include "mlir/Support/FileUtilities.h"
17 #include "mlir/Target/LLVMIR/Export.h"
18 
19 #include "llvm/ExecutionEngine/JITEventListener.h"
20 #include "llvm/ExecutionEngine/ObjectCache.h"
21 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
22 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
23 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
24 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
25 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
26 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/MC/SubtargetFeature.h"
29 #include "llvm/MC/TargetRegistry.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/Host.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 
35 #define DEBUG_TYPE "execution-engine"
36 
37 using namespace mlir;
38 using llvm::dbgs;
39 using llvm::Error;
40 using llvm::errs;
41 using llvm::Expected;
42 using llvm::LLVMContext;
43 using llvm::MemoryBuffer;
44 using llvm::MemoryBufferRef;
45 using llvm::Module;
46 using llvm::SectionMemoryManager;
47 using llvm::StringError;
48 using llvm::Triple;
49 using llvm::orc::DynamicLibrarySearchGenerator;
50 using llvm::orc::ExecutionSession;
51 using llvm::orc::IRCompileLayer;
52 using llvm::orc::JITTargetMachineBuilder;
53 using llvm::orc::MangleAndInterner;
54 using llvm::orc::RTDyldObjectLinkingLayer;
55 using llvm::orc::SymbolMap;
56 using llvm::orc::ThreadSafeModule;
57 using llvm::orc::TMOwningSimpleCompiler;
58 
59 /// Wrap a string into an llvm::StringError.
60 static Error makeStringError(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 void ExecutionEngine::registerSymbols(
104     llvm::function_ref<SymbolMap(MangleAndInterner)> symbolMap) {
105   auto &mainJitDylib = jit->getMainJITDylib();
106   cantFail(mainJitDylib.define(
107       absoluteSymbols(symbolMap(llvm::orc::MangleAndInterner(
108           mainJitDylib.getExecutionSession(), jit->getDataLayout())))));
109 }
110 
111 // Setup LLVM target triple from the current machine.
112 bool ExecutionEngine::setupTargetTriple(Module *llvmModule) {
113   // Setup the machine properties from the current architecture.
114   auto targetTriple = llvm::sys::getDefaultTargetTriple();
115   std::string errorMessage;
116   const auto *target =
117       llvm::TargetRegistry::lookupTarget(targetTriple, errorMessage);
118   if (!target) {
119     errs() << "NO target: " << errorMessage << "\n";
120     return true;
121   }
122 
123   std::string cpu(llvm::sys::getHostCPUName());
124   llvm::SubtargetFeatures features;
125   llvm::StringMap<bool> hostFeatures;
126 
127   if (llvm::sys::getHostCPUFeatures(hostFeatures))
128     for (auto &f : hostFeatures)
129       features.AddFeature(f.first(), f.second);
130 
131   std::unique_ptr<llvm::TargetMachine> machine(target->createTargetMachine(
132       targetTriple, cpu, features.getString(), {}, {}));
133   if (!machine) {
134     errs() << "Unable to create target machine\n";
135     return true;
136   }
137   llvmModule->setDataLayout(machine->createDataLayout());
138   llvmModule->setTargetTriple(targetTriple);
139   return false;
140 }
141 
142 static std::string makePackedFunctionName(StringRef name) {
143   return "_mlir_" + name.str();
144 }
145 
146 // For each function in the LLVM module, define an interface function that wraps
147 // all the arguments of the original function and all its results into an i8**
148 // pointer to provide a unified invocation interface.
149 static void packFunctionArguments(Module *module) {
150   auto &ctx = module->getContext();
151   llvm::IRBuilder<> builder(ctx);
152   DenseSet<llvm::Function *> interfaceFunctions;
153   for (auto &func : module->getFunctionList()) {
154     if (func.isDeclaration()) {
155       continue;
156     }
157     if (interfaceFunctions.count(&func)) {
158       continue;
159     }
160 
161     // Given a function `foo(<...>)`, define the interface function
162     // `mlir_foo(i8**)`.
163     auto *newType = llvm::FunctionType::get(
164         builder.getVoidTy(), builder.getInt8PtrTy()->getPointerTo(),
165         /*isVarArg=*/false);
166     auto newName = makePackedFunctionName(func.getName());
167     auto funcCst = module->getOrInsertFunction(newName, newType);
168     llvm::Function *interfaceFunc = cast<llvm::Function>(funcCst.getCallee());
169     interfaceFunctions.insert(interfaceFunc);
170 
171     // Extract the arguments from the type-erased argument list and cast them to
172     // the proper types.
173     auto *bb = llvm::BasicBlock::Create(ctx);
174     bb->insertInto(interfaceFunc);
175     builder.SetInsertPoint(bb);
176     llvm::Value *argList = interfaceFunc->arg_begin();
177     SmallVector<llvm::Value *, 8> args;
178     args.reserve(llvm::size(func.args()));
179     for (auto &indexedArg : llvm::enumerate(func.args())) {
180       llvm::Value *argIndex = llvm::Constant::getIntegerValue(
181           builder.getInt64Ty(), APInt(64, indexedArg.index()));
182       llvm::Value *argPtrPtr =
183           builder.CreateGEP(builder.getInt8PtrTy(), argList, argIndex);
184       llvm::Value *argPtr =
185           builder.CreateLoad(builder.getInt8PtrTy(), argPtrPtr);
186       llvm::Type *argTy = indexedArg.value().getType();
187       argPtr = builder.CreateBitCast(argPtr, argTy->getPointerTo());
188       llvm::Value *arg = builder.CreateLoad(argTy, argPtr);
189       args.push_back(arg);
190     }
191 
192     // Call the implementation function with the extracted arguments.
193     llvm::Value *result = builder.CreateCall(&func, args);
194 
195     // Assuming the result is one value, potentially of type `void`.
196     if (!result->getType()->isVoidTy()) {
197       llvm::Value *retIndex = llvm::Constant::getIntegerValue(
198           builder.getInt64Ty(), APInt(64, llvm::size(func.args())));
199       llvm::Value *retPtrPtr =
200           builder.CreateGEP(builder.getInt8PtrTy(), argList, retIndex);
201       llvm::Value *retPtr =
202           builder.CreateLoad(builder.getInt8PtrTy(), retPtrPtr);
203       retPtr = builder.CreateBitCast(retPtr, result->getType()->getPointerTo());
204       builder.CreateStore(result, retPtr);
205     }
206 
207     // The interface function returns void.
208     builder.CreateRetVoid();
209   }
210 }
211 
212 ExecutionEngine::ExecutionEngine(bool enableObjectCache,
213                                  bool enableGDBNotificationListener,
214                                  bool enablePerfNotificationListener)
215     : cache(enableObjectCache ? new SimpleObjectCache() : nullptr),
216       gdbListener(enableGDBNotificationListener
217                       ? llvm::JITEventListener::createGDBRegistrationListener()
218                       : nullptr),
219       perfListener(nullptr) {
220   if (enablePerfNotificationListener) {
221     if (auto *listener = llvm::JITEventListener::createPerfJITEventListener())
222       perfListener = listener;
223     else if (auto *listener =
224                  llvm::JITEventListener::createIntelJITEventListener())
225       perfListener = listener;
226   }
227 }
228 
229 Expected<std::unique_ptr<ExecutionEngine>>
230 ExecutionEngine::create(ModuleOp m, const ExecutionEngineOptions &options) {
231   auto engine = std::make_unique<ExecutionEngine>(
232       options.enableObjectCache, options.enableGDBNotificationListener,
233       options.enablePerfNotificationListener);
234 
235   std::unique_ptr<llvm::LLVMContext> ctx(new llvm::LLVMContext);
236   auto llvmModule = options.llvmModuleBuilder
237                         ? options.llvmModuleBuilder(m, *ctx)
238                         : translateModuleToLLVMIR(m, *ctx);
239   if (!llvmModule)
240     return makeStringError("could not convert to LLVM IR");
241   // FIXME: the triple should be passed to the translation or dialect conversion
242   // instead of this.  Currently, the LLVM module created above has no triple
243   // associated with it.
244   setupTargetTriple(llvmModule.get());
245   packFunctionArguments(llvmModule.get());
246 
247   auto dataLayout = llvmModule->getDataLayout();
248 
249   // Callback to create the object layer with symbol resolution to current
250   // process and dynamically linked libraries.
251   auto objectLinkingLayerCreator = [&](ExecutionSession &session,
252                                        const Triple &tt) {
253     auto objectLayer = std::make_unique<RTDyldObjectLinkingLayer>(
254         session, [sectionMemoryMapper = options.sectionMemoryMapper]() {
255           return std::make_unique<SectionMemoryManager>(sectionMemoryMapper);
256         });
257 
258     // Register JIT event listeners if they are enabled.
259     if (engine->gdbListener)
260       objectLayer->registerJITEventListener(*engine->gdbListener);
261     if (engine->perfListener)
262       objectLayer->registerJITEventListener(*engine->perfListener);
263 
264     // COFF format binaries (Windows) need special handling to deal with
265     // exported symbol visibility.
266     // cf llvm/lib/ExecutionEngine/Orc/LLJIT.cpp LLJIT::createObjectLinkingLayer
267     llvm::Triple targetTriple(llvm::Twine(llvmModule->getTargetTriple()));
268     if (targetTriple.isOSBinFormatCOFF()) {
269       objectLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);
270       objectLayer->setAutoClaimResponsibilityForObjectSymbols(true);
271     }
272 
273     // Resolve symbols from shared libraries.
274     for (auto libPath : options.sharedLibPaths) {
275       auto mb = llvm::MemoryBuffer::getFile(libPath);
276       if (!mb) {
277         errs() << "Failed to create MemoryBuffer for: " << libPath
278                << "\nError: " << mb.getError().message() << "\n";
279         continue;
280       }
281       auto &jd = session.createBareJITDylib(std::string(libPath));
282       auto loaded = DynamicLibrarySearchGenerator::Load(
283           libPath.data(), dataLayout.getGlobalPrefix());
284       if (!loaded) {
285         errs() << "Could not load " << libPath << ":\n  " << loaded.takeError()
286                << "\n";
287         continue;
288       }
289       jd.addGenerator(std::move(*loaded));
290       cantFail(objectLayer->add(jd, std::move(mb.get())));
291     }
292 
293     return objectLayer;
294   };
295 
296   // Callback to inspect the cache and recompile on demand. This follows Lang's
297   // LLJITWithObjectCache example.
298   auto compileFunctionCreator = [&](JITTargetMachineBuilder jtmb)
299       -> Expected<std::unique_ptr<IRCompileLayer::IRCompiler>> {
300     if (options.jitCodeGenOptLevel)
301       jtmb.setCodeGenOptLevel(options.jitCodeGenOptLevel.getValue());
302     auto tm = jtmb.createTargetMachine();
303     if (!tm)
304       return tm.takeError();
305     return std::make_unique<TMOwningSimpleCompiler>(std::move(*tm),
306                                                     engine->cache.get());
307   };
308 
309   // Create the LLJIT by calling the LLJITBuilder with 2 callbacks.
310   auto jit =
311       cantFail(llvm::orc::LLJITBuilder()
312                    .setCompileFunctionCreator(compileFunctionCreator)
313                    .setObjectLinkingLayerCreator(objectLinkingLayerCreator)
314                    .create());
315 
316   // Add a ThreadSafemodule to the engine and return.
317   ThreadSafeModule tsm(std::move(llvmModule), std::move(ctx));
318   if (options.transformer)
319     cantFail(tsm.withModuleDo(
320         [&](llvm::Module &module) { return options.transformer(&module); }));
321   cantFail(jit->addIRModule(std::move(tsm)));
322   engine->jit = std::move(jit);
323 
324   // Resolve symbols that are statically linked in the current process.
325   llvm::orc::JITDylib &mainJD = engine->jit->getMainJITDylib();
326   mainJD.addGenerator(
327       cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
328           dataLayout.getGlobalPrefix())));
329 
330   return std::move(engine);
331 }
332 
333 Expected<void (*)(void **)>
334 ExecutionEngine::lookupPacked(StringRef name) const {
335   auto result = lookup(makePackedFunctionName(name));
336   if (!result)
337     return result.takeError();
338   return reinterpret_cast<void (*)(void **)>(result.get());
339 }
340 
341 Expected<void *> ExecutionEngine::lookup(StringRef name) const {
342   auto expectedSymbol = jit->lookup(name);
343 
344   // JIT lookup may return an Error referring to strings stored internally by
345   // the JIT. If the Error outlives the ExecutionEngine, it would want have a
346   // dangling reference, which is currently caught by an assertion inside JIT
347   // thanks to hand-rolled reference counting. Rewrap the error message into a
348   // string before returning. Alternatively, ORC JIT should consider copying
349   // the string into the error message.
350   if (!expectedSymbol) {
351     std::string errorMessage;
352     llvm::raw_string_ostream os(errorMessage);
353     llvm::handleAllErrors(expectedSymbol.takeError(),
354                           [&os](llvm::ErrorInfoBase &ei) { ei.log(os); });
355     return makeStringError(os.str());
356   }
357 
358   auto rawFPtr = expectedSymbol->getAddress();
359   auto *fptr = reinterpret_cast<void *>(rawFPtr);
360   if (!fptr)
361     return makeStringError("looked up function is null");
362   return fptr;
363 }
364 
365 Error ExecutionEngine::invokePacked(StringRef name,
366                                     MutableArrayRef<void *> args) {
367   auto expectedFPtr = lookupPacked(name);
368   if (!expectedFPtr)
369     return expectedFPtr.takeError();
370   auto fptr = *expectedFPtr;
371 
372   (*fptr)(args.data());
373 
374   return Error::success();
375 }
376