Layer.h 6.7 KB

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