LLJITWithObjectCache.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===--- LLJITWithObjectCache.cpp - An LLJIT example with an ObjectCache --===//
  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. #include "llvm/ADT/StringMap.h"
  9. #include "llvm/ExecutionEngine/ObjectCache.h"
  10. #include "llvm/ExecutionEngine/Orc/LLJIT.h"
  11. #include "llvm/IR/Function.h"
  12. #include "llvm/IR/IRBuilder.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/Support/InitLLVM.h"
  15. #include "llvm/Support/TargetSelect.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include "../ExampleModules.h"
  18. using namespace llvm;
  19. using namespace llvm::orc;
  20. ExitOnError ExitOnErr;
  21. class MyObjectCache : public ObjectCache {
  22. public:
  23. void notifyObjectCompiled(const Module *M,
  24. MemoryBufferRef ObjBuffer) override {
  25. CachedObjects[M->getModuleIdentifier()] = MemoryBuffer::getMemBufferCopy(
  26. ObjBuffer.getBuffer(), ObjBuffer.getBufferIdentifier());
  27. }
  28. std::unique_ptr<MemoryBuffer> getObject(const Module *M) override {
  29. auto I = CachedObjects.find(M->getModuleIdentifier());
  30. if (I == CachedObjects.end()) {
  31. dbgs() << "No object for " << M->getModuleIdentifier()
  32. << " in cache. Compiling.\n";
  33. return nullptr;
  34. }
  35. dbgs() << "Object for " << M->getModuleIdentifier()
  36. << " loaded from cache.\n";
  37. return MemoryBuffer::getMemBuffer(I->second->getMemBufferRef());
  38. }
  39. private:
  40. StringMap<std::unique_ptr<MemoryBuffer>> CachedObjects;
  41. };
  42. void runJITWithCache(ObjectCache &ObjCache) {
  43. // Create an LLJIT instance with a custom CompileFunction.
  44. auto J = ExitOnErr(
  45. LLJITBuilder()
  46. .setCompileFunctionCreator(
  47. [&](JITTargetMachineBuilder JTMB)
  48. -> Expected<IRCompileLayer::CompileFunction> {
  49. auto TM = JTMB.createTargetMachine();
  50. if (!TM)
  51. return TM.takeError();
  52. return IRCompileLayer::CompileFunction(
  53. TMOwningSimpleCompiler(std::move(*TM), &ObjCache));
  54. })
  55. .create());
  56. auto M = ExitOnErr(parseExampleModule(Add1Example, "add1"));
  57. ExitOnErr(J->addIRModule(std::move(M)));
  58. // Look up the JIT'd function, cast it to a function pointer, then call it.
  59. auto Add1Sym = ExitOnErr(J->lookup("add1"));
  60. int (*Add1)(int) = (int (*)(int))Add1Sym.getAddress();
  61. int Result = Add1(42);
  62. outs() << "add1(42) = " << Result << "\n";
  63. }
  64. int main(int argc, char *argv[]) {
  65. // Initialize LLVM.
  66. InitLLVM X(argc, argv);
  67. InitializeNativeTarget();
  68. InitializeNativeTargetAsmPrinter();
  69. cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT");
  70. ExitOnErr.setBanner(std::string(argv[0]) + ": ");
  71. MyObjectCache MyCache;
  72. runJITWithCache(MyCache);
  73. runJITWithCache(MyCache);
  74. return 0;
  75. }