CompileUtils.h 3.4 KB

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