CompileUtils.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- CompileUtils.h - Utilities for compiling IR in the JIT ---*- C++ -*-===//
  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. // Contains utilities for compiling IR to object files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
  13. #define LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
  14. #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
  15. #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
  16. #include "llvm/ExecutionEngine/Orc/Layer.h"
  17. #include <memory>
  18. namespace llvm {
  19. class MCContext;
  20. class MemoryBuffer;
  21. class Module;
  22. class ObjectCache;
  23. class TargetMachine;
  24. namespace orc {
  25. IRSymbolMapper::ManglingOptions
  26. irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
  27. /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
  28. /// This compiler supports a single compilation thread and LLVMContext only.
  29. /// For multithreaded compilation, use ConcurrentIRCompiler below.
  30. class SimpleCompiler : public IRCompileLayer::IRCompiler {
  31. public:
  32. using CompileResult = std::unique_ptr<MemoryBuffer>;
  33. /// Construct a simple compile functor with the given target.
  34. SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
  35. : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
  36. ObjCache(ObjCache) {}
  37. /// Set an ObjectCache to query before compiling.
  38. void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
  39. /// Compile a Module to an ObjectFile.
  40. Expected<CompileResult> operator()(Module &M) override;
  41. private:
  42. IRSymbolMapper::ManglingOptions
  43. manglingOptionsForTargetMachine(const TargetMachine &TM);
  44. CompileResult tryToLoadFromObjectCache(const Module &M);
  45. void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
  46. TargetMachine &TM;
  47. ObjectCache *ObjCache = nullptr;
  48. };
  49. /// A SimpleCompiler that owns its TargetMachine.
  50. ///
  51. /// This convenient for clients who don't want to own their TargetMachines,
  52. /// e.g. LLJIT.
  53. class TMOwningSimpleCompiler : public SimpleCompiler {
  54. public:
  55. TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
  56. ObjectCache *ObjCache = nullptr)
  57. : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
  58. private:
  59. // FIXME: shared because std::functions (and consequently
  60. // IRCompileLayer::CompileFunction) are not moveable.
  61. std::shared_ptr<llvm::TargetMachine> TM;
  62. };
  63. /// A thread-safe version of SimpleCompiler.
  64. ///
  65. /// This class creates a new TargetMachine and SimpleCompiler instance for each
  66. /// compile.
  67. class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler {
  68. public:
  69. ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
  70. ObjectCache *ObjCache = nullptr);
  71. void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
  72. Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
  73. private:
  74. JITTargetMachineBuilder JTMB;
  75. ObjectCache *ObjCache = nullptr;
  76. };
  77. } // end namespace orc
  78. } // end namespace llvm
  79. #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H