Layer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---------------- Layer.h -- Layer interfaces --------------*- 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. // Layer interfaces.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
  18. #define LLVM_EXECUTIONENGINE_ORC_LAYER_H
  19. #include "llvm/ExecutionEngine/Orc/Core.h"
  20. #include "llvm/ExecutionEngine/Orc/Mangling.h"
  21. #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
  22. #include "llvm/IR/Module.h"
  23. #include "llvm/Support/Casting.h"
  24. #include "llvm/Support/ExtensibleRTTI.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. namespace llvm {
  27. namespace orc {
  28. /// IRMaterializationUnit is a convenient base class for MaterializationUnits
  29. /// wrapping LLVM IR. Represents materialization responsibility for all symbols
  30. /// in the given module. If symbols are overridden by other definitions, then
  31. /// their linkage is changed to available-externally.
  32. class IRMaterializationUnit : public MaterializationUnit {
  33. public:
  34. using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
  35. /// Create an IRMaterializationLayer. Scans the module to build the
  36. /// SymbolFlags and SymbolToDefinition maps.
  37. IRMaterializationUnit(ExecutionSession &ES,
  38. const IRSymbolMapper::ManglingOptions &MO,
  39. ThreadSafeModule TSM);
  40. /// Create an IRMaterializationLayer from a module, and pre-existing
  41. /// SymbolFlags and SymbolToDefinition maps. The maps must provide
  42. /// entries for each definition in M.
  43. /// This constructor is useful for delegating work from one
  44. /// IRMaterializationUnit to another.
  45. IRMaterializationUnit(ThreadSafeModule TSM, Interface I,
  46. SymbolNameToDefinitionMap SymbolToDefinition);
  47. /// Return the ModuleIdentifier as the name for this MaterializationUnit.
  48. StringRef getName() const override;
  49. /// Return a reference to the contained ThreadSafeModule.
  50. const ThreadSafeModule &getModule() const { return TSM; }
  51. protected:
  52. ThreadSafeModule TSM;
  53. SymbolNameToDefinitionMap SymbolToDefinition;
  54. private:
  55. static SymbolStringPtr getInitSymbol(ExecutionSession &ES,
  56. const ThreadSafeModule &TSM);
  57. void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
  58. };
  59. /// Interface for layers that accept LLVM IR.
  60. class IRLayer {
  61. public:
  62. IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
  63. : ES(ES), MO(MO) {}
  64. virtual ~IRLayer();
  65. /// Returns the ExecutionSession for this layer.
  66. ExecutionSession &getExecutionSession() { return ES; }
  67. /// Get the mangling options for this layer.
  68. const IRSymbolMapper::ManglingOptions *&getManglingOptions() const {
  69. return MO;
  70. }
  71. /// Sets the CloneToNewContextOnEmit flag (false by default).
  72. ///
  73. /// When set, IR modules added to this layer will be cloned on to a new
  74. /// context before emit is called. This can be used by clients who want
  75. /// to load all IR using one LLVMContext (to save memory via type and
  76. /// constant uniquing), but want to move Modules to fresh contexts before
  77. /// compiling them to enable concurrent compilation.
  78. /// Single threaded clients, or clients who load every module on a new
  79. /// context, need not set this.
  80. void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
  81. this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
  82. }
  83. /// Returns the current value of the CloneToNewContextOnEmit flag.
  84. bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
  85. /// Add a MaterializatinoUnit representing the given IR to the JITDylib
  86. /// targeted by the given tracker.
  87. virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM);
  88. /// Adds a MaterializationUnit representing the given IR to the given
  89. /// JITDylib. If RT is not specif
  90. Error add(JITDylib &JD, ThreadSafeModule TSM) {
  91. return add(JD.getDefaultResourceTracker(), std::move(TSM));
  92. }
  93. /// Emit should materialize the given IR.
  94. virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
  95. ThreadSafeModule TSM) = 0;
  96. private:
  97. bool CloneToNewContextOnEmit = false;
  98. ExecutionSession &ES;
  99. const IRSymbolMapper::ManglingOptions *&MO;
  100. };
  101. /// MaterializationUnit that materializes modules by calling the 'emit' method
  102. /// on the given IRLayer.
  103. class BasicIRLayerMaterializationUnit : public IRMaterializationUnit {
  104. public:
  105. BasicIRLayerMaterializationUnit(IRLayer &L,
  106. const IRSymbolMapper::ManglingOptions &MO,
  107. ThreadSafeModule TSM);
  108. private:
  109. void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
  110. IRLayer &L;
  111. };
  112. /// Interface for Layers that accept object files.
  113. class ObjectLayer : public RTTIExtends<ObjectLayer, RTTIRoot> {
  114. public:
  115. static char ID;
  116. ObjectLayer(ExecutionSession &ES);
  117. virtual ~ObjectLayer();
  118. /// Returns the execution session for this layer.
  119. ExecutionSession &getExecutionSession() { return ES; }
  120. /// Adds a MaterializationUnit for the object file in the given memory buffer
  121. /// to the JITDylib for the given ResourceTracker.
  122. virtual Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
  123. MaterializationUnit::Interface I);
  124. /// Adds a MaterializationUnit for the object file in the given memory buffer
  125. /// to the JITDylib for the given ResourceTracker. The interface for the
  126. /// object will be built using the default object interface builder.
  127. Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O);
  128. /// Adds a MaterializationUnit for the object file in the given memory buffer
  129. /// to the given JITDylib.
  130. Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
  131. MaterializationUnit::Interface I) {
  132. return add(JD.getDefaultResourceTracker(), std::move(O), std::move(I));
  133. }
  134. /// Adds a MaterializationUnit for the object file in the given memory buffer
  135. /// to the given JITDylib. The interface for the object will be built using
  136. /// the default object interface builder.
  137. Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O);
  138. /// Emit should materialize the given IR.
  139. virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
  140. std::unique_ptr<MemoryBuffer> O) = 0;
  141. private:
  142. ExecutionSession &ES;
  143. };
  144. /// Materializes the given object file (represented by a MemoryBuffer
  145. /// instance) by calling 'emit' on the given ObjectLayer.
  146. class BasicObjectLayerMaterializationUnit : public MaterializationUnit {
  147. public:
  148. /// Create using the default object interface builder function.
  149. static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
  150. Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> O);
  151. BasicObjectLayerMaterializationUnit(ObjectLayer &L,
  152. std::unique_ptr<MemoryBuffer> O,
  153. Interface I);
  154. /// Return the buffer's identifier as the name for this MaterializationUnit.
  155. StringRef getName() const override;
  156. private:
  157. void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
  158. void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
  159. ObjectLayer &L;
  160. std::unique_ptr<MemoryBuffer> O;
  161. };
  162. } // End namespace orc
  163. } // End namespace llvm
  164. #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
  165. #ifdef __GNUC__
  166. #pragma GCC diagnostic pop
  167. #endif