xref: /llvm-project/llvm/examples/OrcV2Examples/LLJITWithObjectCache/LLJITWithObjectCache.cpp (revision 16dcbb53dc7968a3752661aac731172ebe0faf64)
1633ea072SLang Hames //===--- LLJITWithObjectCache.cpp - An LLJIT example with an ObjectCache --===//
2633ea072SLang Hames //
3633ea072SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4633ea072SLang Hames // See https://llvm.org/LICENSE.txt for license information.
5633ea072SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6633ea072SLang Hames //
7633ea072SLang Hames //===----------------------------------------------------------------------===//
8633ea072SLang Hames 
9633ea072SLang Hames #include "llvm/ADT/StringMap.h"
10633ea072SLang Hames #include "llvm/ExecutionEngine/ObjectCache.h"
11633ea072SLang Hames #include "llvm/ExecutionEngine/Orc/LLJIT.h"
12633ea072SLang Hames #include "llvm/IR/Function.h"
13633ea072SLang Hames #include "llvm/IR/IRBuilder.h"
14633ea072SLang Hames #include "llvm/IR/Module.h"
15633ea072SLang Hames #include "llvm/Support/InitLLVM.h"
16633ea072SLang Hames #include "llvm/Support/TargetSelect.h"
17633ea072SLang Hames #include "llvm/Support/raw_ostream.h"
18633ea072SLang Hames 
19633ea072SLang Hames #include "../ExampleModules.h"
20633ea072SLang Hames 
21633ea072SLang Hames using namespace llvm;
22633ea072SLang Hames using namespace llvm::orc;
23633ea072SLang Hames 
24633ea072SLang Hames ExitOnError ExitOnErr;
25633ea072SLang Hames 
26633ea072SLang Hames class MyObjectCache : public ObjectCache {
27633ea072SLang Hames public:
notifyObjectCompiled(const Module * M,MemoryBufferRef ObjBuffer)28633ea072SLang Hames   void notifyObjectCompiled(const Module *M,
29633ea072SLang Hames                             MemoryBufferRef ObjBuffer) override {
30633ea072SLang Hames     CachedObjects[M->getModuleIdentifier()] = MemoryBuffer::getMemBufferCopy(
31633ea072SLang Hames         ObjBuffer.getBuffer(), ObjBuffer.getBufferIdentifier());
32633ea072SLang Hames   }
33633ea072SLang Hames 
getObject(const Module * M)34633ea072SLang Hames   std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
35633ea072SLang Hames     auto I = CachedObjects.find(M->getModuleIdentifier());
36633ea072SLang Hames     if (I == CachedObjects.end()) {
37633ea072SLang Hames       dbgs() << "No object for " << M->getModuleIdentifier()
38633ea072SLang Hames              << " in cache. Compiling.\n";
39633ea072SLang Hames       return nullptr;
40633ea072SLang Hames     }
41633ea072SLang Hames 
42633ea072SLang Hames     dbgs() << "Object for " << M->getModuleIdentifier()
43633ea072SLang Hames            << " loaded from cache.\n";
44633ea072SLang Hames     return MemoryBuffer::getMemBuffer(I->second->getMemBufferRef());
45633ea072SLang Hames   }
46633ea072SLang Hames 
47633ea072SLang Hames private:
48633ea072SLang Hames   StringMap<std::unique_ptr<MemoryBuffer>> CachedObjects;
49633ea072SLang Hames };
50633ea072SLang Hames 
runJITWithCache(ObjectCache & ObjCache)51633ea072SLang Hames void runJITWithCache(ObjectCache &ObjCache) {
52633ea072SLang Hames 
53633ea072SLang Hames   // Create an LLJIT instance with a custom IRCompiler.
54633ea072SLang Hames   auto J = ExitOnErr(
55633ea072SLang Hames       LLJITBuilder()
56633ea072SLang Hames           .setCompileFunctionCreator(
57633ea072SLang Hames               [&](JITTargetMachineBuilder JTMB)
58633ea072SLang Hames                   -> Expected<std::unique_ptr<IRCompileLayer::IRCompiler>> {
59633ea072SLang Hames                 auto TM = JTMB.createTargetMachine();
60633ea072SLang Hames                 if (!TM)
61633ea072SLang Hames                   return TM.takeError();
62633ea072SLang Hames                 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM),
63633ea072SLang Hames                                                                 &ObjCache);
64633ea072SLang Hames               })
65633ea072SLang Hames           .create());
66633ea072SLang Hames 
67633ea072SLang Hames   auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
68633ea072SLang Hames 
69633ea072SLang Hames   ExitOnErr(J->addIRModule(std::move(M)));
70633ea072SLang Hames 
71633ea072SLang Hames   // Look up the JIT'd function, cast it to a function pointer, then call it.
72*16dcbb53SLang Hames   auto Add1Addr = ExitOnErr(J->lookup("add1"));
73*16dcbb53SLang Hames   int (*Add1)(int) = Add1Addr.toPtr<int(int)>();
74633ea072SLang Hames 
75633ea072SLang Hames   int Result = Add1(42);
76633ea072SLang Hames   outs() << "add1(42) = " << Result << "\n";
77633ea072SLang Hames }
78633ea072SLang Hames 
main(int argc,char * argv[])79633ea072SLang Hames int main(int argc, char *argv[]) {
80633ea072SLang Hames   // Initialize LLVM.
81633ea072SLang Hames   InitLLVM X(argc, argv);
82633ea072SLang Hames 
83633ea072SLang Hames   InitializeNativeTarget();
84633ea072SLang Hames   InitializeNativeTargetAsmPrinter();
85633ea072SLang Hames 
86633ea072SLang Hames   cl::ParseCommandLineOptions(argc, argv, "LLJITWithObjectCache");
87633ea072SLang Hames   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
88633ea072SLang Hames 
89633ea072SLang Hames   MyObjectCache MyCache;
90633ea072SLang Hames 
91633ea072SLang Hames   runJITWithCache(MyCache);
92633ea072SLang Hames   runJITWithCache(MyCache);
93633ea072SLang Hames 
94633ea072SLang Hames   return 0;
95633ea072SLang Hames }
96