Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 1 | //===---------------- Layer.h -- Layer interfaces --------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Layer interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H |
| 15 | #define LLVM_EXECUTIONENGINE_ORC_LAYER_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/Orc/Core.h" |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 18 | #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | namespace orc { |
| 24 | |
| 25 | /// Interface for layers that accept LLVM IR. |
| 26 | class IRLayer { |
| 27 | public: |
| 28 | IRLayer(ExecutionSession &ES); |
| 29 | virtual ~IRLayer(); |
| 30 | |
| 31 | /// Returns the ExecutionSession for this layer. |
| 32 | ExecutionSession &getExecutionSession() { return ES; } |
| 33 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 34 | /// Sets the CloneToNewContextOnEmit flag (false by default). |
| 35 | /// |
| 36 | /// When set, IR modules added to this layer will be cloned on to a new |
| 37 | /// context before emit is called. This can be used by clients who want |
| 38 | /// to load all IR using one LLVMContext (to save memory via type and |
| 39 | /// constant uniquing), but want to move Modules to fresh contexts before |
| 40 | /// compiling them to enable concurrent compilation. |
| 41 | /// Single threaded clients, or clients who load every module on a new |
| 42 | /// context, need not set this. |
| 43 | void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) { |
| 44 | this->CloneToNewContextOnEmit = CloneToNewContextOnEmit; |
| 45 | } |
| 46 | |
| 47 | /// Returns the current value of the CloneToNewContextOnEmit flag. |
| 48 | bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; } |
| 49 | |
| 50 | /// Adds a MaterializationUnit representing the given IR to the given |
| 51 | /// JITDylib. |
| 52 | virtual Error add(JITDylib &JD, VModuleKey K, ThreadSafeModule TSM); |
| 53 | |
| 54 | /// Adds a MaterializationUnit representing the given IR to the main |
| 55 | /// JITDylib. |
| 56 | Error add(VModuleKey K, ThreadSafeModule TSM) { |
| 57 | return add(ES.getMainJITDylib(), K, std::move(TSM)); |
| 58 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 59 | |
| 60 | /// Emit should materialize the given IR. |
| 61 | virtual void emit(MaterializationResponsibility R, VModuleKey K, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 62 | ThreadSafeModule TSM) = 0; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 63 | |
| 64 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 65 | bool CloneToNewContextOnEmit = false; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 66 | ExecutionSession &ES; |
| 67 | }; |
| 68 | |
| 69 | /// IRMaterializationUnit is a convenient base class for MaterializationUnits |
| 70 | /// wrapping LLVM IR. Represents materialization responsibility for all symbols |
| 71 | /// in the given module. If symbols are overridden by other definitions, then |
| 72 | /// their linkage is changed to available-externally. |
| 73 | class IRMaterializationUnit : public MaterializationUnit { |
| 74 | public: |
| 75 | using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>; |
| 76 | |
| 77 | /// Create an IRMaterializationLayer. Scans the module to build the |
| 78 | /// SymbolFlags and SymbolToDefinition maps. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 79 | IRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | |
| 81 | /// Create an IRMaterializationLayer from a module, and pre-existing |
| 82 | /// SymbolFlags and SymbolToDefinition maps. The maps must provide |
| 83 | /// entries for each definition in M. |
| 84 | /// This constructor is useful for delegating work from one |
| 85 | /// IRMaterializationUnit to another. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 86 | IRMaterializationUnit(ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 87 | SymbolNameToDefinitionMap SymbolToDefinition); |
| 88 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 89 | /// Return the ModuleIdentifier as the name for this MaterializationUnit. |
| 90 | StringRef getName() const override; |
| 91 | |
| 92 | const ThreadSafeModule &getModule() const { return TSM; } |
| 93 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 94 | protected: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 95 | ThreadSafeModule TSM; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 96 | SymbolNameToDefinitionMap SymbolToDefinition; |
| 97 | |
| 98 | private: |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 99 | void discard(const JITDylib &JD, const SymbolStringPtr &Name) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /// MaterializationUnit that materializes modules by calling the 'emit' method |
| 103 | /// on the given IRLayer. |
| 104 | class BasicIRLayerMaterializationUnit : public IRMaterializationUnit { |
| 105 | public: |
| 106 | BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 107 | ThreadSafeModule TSM); |
| 108 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 109 | private: |
| 110 | |
| 111 | void materialize(MaterializationResponsibility R) override; |
| 112 | |
| 113 | IRLayer &L; |
| 114 | VModuleKey K; |
| 115 | }; |
| 116 | |
| 117 | /// Interface for Layers that accept object files. |
| 118 | class ObjectLayer { |
| 119 | public: |
| 120 | ObjectLayer(ExecutionSession &ES); |
| 121 | virtual ~ObjectLayer(); |
| 122 | |
| 123 | /// Returns the execution session for this layer. |
| 124 | ExecutionSession &getExecutionSession() { return ES; } |
| 125 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 126 | /// Adds a MaterializationUnit representing the given IR to the given |
| 127 | /// JITDylib. |
| 128 | virtual Error add(JITDylib &JD, VModuleKey K, std::unique_ptr<MemoryBuffer> O); |
| 129 | |
| 130 | /// Adds a MaterializationUnit representing the given object to the main |
| 131 | /// JITDylib. |
| 132 | Error add(VModuleKey K, std::unique_ptr<MemoryBuffer> O) { |
| 133 | return add(ES.getMainJITDylib(), K, std::move(O)); |
| 134 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 135 | |
| 136 | /// Emit should materialize the given IR. |
| 137 | virtual void emit(MaterializationResponsibility R, VModuleKey K, |
| 138 | std::unique_ptr<MemoryBuffer> O) = 0; |
| 139 | |
| 140 | private: |
| 141 | ExecutionSession &ES; |
| 142 | }; |
| 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 | static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>> |
| 149 | Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O); |
| 150 | |
| 151 | BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K, |
| 152 | std::unique_ptr<MemoryBuffer> O, |
| 153 | SymbolFlagsMap SymbolFlags); |
| 154 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 155 | /// Return the buffer's identifier as the name for this MaterializationUnit. |
| 156 | StringRef getName() const override; |
| 157 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 158 | private: |
| 159 | |
| 160 | void materialize(MaterializationResponsibility R) override; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 161 | void discard(const JITDylib &JD, const SymbolStringPtr &Name) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 162 | |
| 163 | ObjectLayer &L; |
| 164 | VModuleKey K; |
| 165 | std::unique_ptr<MemoryBuffer> O; |
| 166 | }; |
| 167 | |
| 168 | /// Returns a SymbolFlagsMap for the object file represented by the given |
| 169 | /// buffer, or an error if the buffer does not contain a valid object file. |
| 170 | // FIXME: Maybe move to Core.h? |
| 171 | Expected<SymbolFlagsMap> getObjectSymbolFlags(ExecutionSession &ES, |
| 172 | MemoryBufferRef ObjBuffer); |
| 173 | |
| 174 | } // End namespace orc |
| 175 | } // End namespace llvm |
| 176 | |
| 177 | #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H |