CompileUtils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 MCContext;
  25. class MemoryBuffer;
  26. class Module;
  27. class ObjectCache;
  28. class TargetMachine;
  29. namespace orc {
  30. IRSymbolMapper::ManglingOptions
  31. irManglingOptionsFromTargetOptions(const TargetOptions &Opts);
  32. /// Simple compile functor: Takes a single IR module and returns an ObjectFile.
  33. /// This compiler supports a single compilation thread and LLVMContext only.
  34. /// For multithreaded compilation, use ConcurrentIRCompiler below.
  35. class SimpleCompiler : public IRCompileLayer::IRCompiler {
  36. public:
  37. using CompileResult = std::unique_ptr<MemoryBuffer>;
  38. /// Construct a simple compile functor with the given target.
  39. SimpleCompiler(TargetMachine &TM, ObjectCache *ObjCache = nullptr)
  40. : IRCompiler(irManglingOptionsFromTargetOptions(TM.Options)), TM(TM),
  41. ObjCache(ObjCache) {}
  42. /// Set an ObjectCache to query before compiling.
  43. void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
  44. /// Compile a Module to an ObjectFile.
  45. Expected<CompileResult> operator()(Module &M) override;
  46. private:
  47. IRSymbolMapper::ManglingOptions
  48. manglingOptionsForTargetMachine(const TargetMachine &TM);
  49. CompileResult tryToLoadFromObjectCache(const Module &M);
  50. void notifyObjectCompiled(const Module &M, const MemoryBuffer &ObjBuffer);
  51. TargetMachine &TM;
  52. ObjectCache *ObjCache = nullptr;
  53. };
  54. /// A SimpleCompiler that owns its TargetMachine.
  55. ///
  56. /// This convenient for clients who don't want to own their TargetMachines,
  57. /// e.g. LLJIT.
  58. class TMOwningSimpleCompiler : public SimpleCompiler {
  59. public:
  60. TMOwningSimpleCompiler(std::unique_ptr<TargetMachine> TM,
  61. ObjectCache *ObjCache = nullptr)
  62. : SimpleCompiler(*TM, ObjCache), TM(std::move(TM)) {}
  63. private:
  64. // FIXME: shared because std::functions (and consequently
  65. // IRCompileLayer::CompileFunction) are not moveable.
  66. std::shared_ptr<llvm::TargetMachine> TM;
  67. };
  68. /// A thread-safe version of SimpleCompiler.
  69. ///
  70. /// This class creates a new TargetMachine and SimpleCompiler instance for each
  71. /// compile.
  72. class ConcurrentIRCompiler : public IRCompileLayer::IRCompiler {
  73. public:
  74. ConcurrentIRCompiler(JITTargetMachineBuilder JTMB,
  75. ObjectCache *ObjCache = nullptr);
  76. void setObjectCache(ObjectCache *ObjCache) { this->ObjCache = ObjCache; }
  77. Expected<std::unique_ptr<MemoryBuffer>> operator()(Module &M) override;
  78. private:
  79. JITTargetMachineBuilder JTMB;
  80. ObjectCache *ObjCache = nullptr;
  81. };
  82. } // end namespace orc
  83. } // end namespace llvm
  84. #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEUTILS_H
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif