diff options
author | Lang Hames <lhames@gmail.com> | 2019-07-10 17:24:24 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2019-07-10 17:24:24 +0000 |
commit | 843f198a83d8161dc580562cbe11643ed2b0e54f (patch) | |
tree | 2b99b9f9be4b4f695930150cb6575e71c7d34402 /llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp | |
parent | 5a6d40be1f3c2bf5c232877969f0efbe49d370a6 (diff) | |
download | bcm5719-llvm-843f198a83d8161dc580562cbe11643ed2b0e54f.tar.gz bcm5719-llvm-843f198a83d8161dc580562cbe11643ed2b0e54f.zip |
[ORC] Add custom IR compiler configuration to LLJITBuilder to enable obj caches.
LLJITBuilder now has a setCompileFunctionCreator method which can be used to
construct a CompileFunction for the LLJIT instance being created. The motivating
use-case for this is supporting ObjectCaches, which can now be set up at
compile-function construction time. To demonstrate this an example project,
LLJITWithObjectCache, is included.
llvm-svn: 365671
Diffstat (limited to 'llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp')
-rw-r--r-- | llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp b/llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp new file mode 100644 index 00000000000..377babb8c3c --- /dev/null +++ b/llvm/examples/LLJITExamples/LLJITWithObjectCache/LLJITWithObjectCache.cpp @@ -0,0 +1,95 @@ +//===--- LLJITWithObjectCache.cpp - An LLJIT example with an ObjectCache --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringMap.h" +#include "llvm/ExecutionEngine/ObjectCache.h" +#include "llvm/ExecutionEngine/Orc/LLJIT.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" + +#include "../ExampleModules.h" + +using namespace llvm; +using namespace llvm::orc; + +ExitOnError ExitOnErr; + +class MyObjectCache : public ObjectCache { +public: + void notifyObjectCompiled(const Module *M, + MemoryBufferRef ObjBuffer) override { + CachedObjects[M->getModuleIdentifier()] = MemoryBuffer::getMemBufferCopy( + ObjBuffer.getBuffer(), ObjBuffer.getBufferIdentifier()); + } + + std::unique_ptr<MemoryBuffer> getObject(const Module *M) override { + auto I = CachedObjects.find(M->getModuleIdentifier()); + if (I == CachedObjects.end()) { + dbgs() << "No object for " << M->getModuleIdentifier() + << " in cache. Compiling.\n"; + return nullptr; + } + + dbgs() << "Object for " << M->getModuleIdentifier() + << " loaded from cache.\n"; + return MemoryBuffer::getMemBuffer(I->second->getMemBufferRef()); + } + +private: + StringMap<std::unique_ptr<MemoryBuffer>> CachedObjects; +}; + +void runJITWithCache(ObjectCache &ObjCache) { + + // Create an LLJIT instance with a custom CompileFunction. + auto J = ExitOnErr( + LLJITBuilder() + .setCompileFunctionCreator( + [&](JITTargetMachineBuilder JTMB) + -> Expected<IRCompileLayer::CompileFunction> { + auto TM = JTMB.createTargetMachine(); + if (!TM) + return TM.takeError(); + return IRCompileLayer::CompileFunction( + TMOwningSimpleCompiler(std::move(*TM), &ObjCache)); + }) + .create()); + + auto M = ExitOnErr(parseExampleModule(Add1Example, "add1")); + + ExitOnErr(J->addIRModule(std::move(M))); + + // Look up the JIT'd function, cast it to a function pointer, then call it. + auto Add1Sym = ExitOnErr(J->lookup("add1")); + int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress(); + + int Result = Add1(42); + outs() << "add1(42) = " << Result << "\n"; +} + +int main(int argc, char *argv[]) { + // Initialize LLVM. + InitLLVM X(argc, argv); + + InitializeNativeTarget(); + InitializeNativeTargetAsmPrinter(); + + cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT"); + ExitOnErr.setBanner(std::string(argv[0]) + ": "); + + MyObjectCache MyCache; + + runJITWithCache(MyCache); + runJITWithCache(MyCache); + + return 0; +} |