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" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace orc { |
| 23 | |
| 24 | /// Interface for layers that accept LLVM IR. |
| 25 | class IRLayer { |
| 26 | public: |
| 27 | IRLayer(ExecutionSession &ES); |
| 28 | virtual ~IRLayer(); |
| 29 | |
| 30 | /// Returns the ExecutionSession for this layer. |
| 31 | ExecutionSession &getExecutionSession() { return ES; } |
| 32 | |
| 33 | /// Adds a MaterializationUnit representing the given IR to the given VSO. |
| 34 | virtual Error add(VSO &V, VModuleKey K, std::unique_ptr<Module> M); |
| 35 | |
| 36 | /// Emit should materialize the given IR. |
| 37 | virtual void emit(MaterializationResponsibility R, VModuleKey K, |
| 38 | std::unique_ptr<Module> M) = 0; |
| 39 | |
| 40 | private: |
| 41 | ExecutionSession &ES; |
| 42 | }; |
| 43 | |
| 44 | /// IRMaterializationUnit is a convenient base class for MaterializationUnits |
| 45 | /// wrapping LLVM IR. Represents materialization responsibility for all symbols |
| 46 | /// in the given module. If symbols are overridden by other definitions, then |
| 47 | /// their linkage is changed to available-externally. |
| 48 | class IRMaterializationUnit : public MaterializationUnit { |
| 49 | public: |
| 50 | using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>; |
| 51 | |
| 52 | /// Create an IRMaterializationLayer. Scans the module to build the |
| 53 | /// SymbolFlags and SymbolToDefinition maps. |
| 54 | IRMaterializationUnit(ExecutionSession &ES, std::unique_ptr<Module> M); |
| 55 | |
| 56 | /// Create an IRMaterializationLayer from a module, and pre-existing |
| 57 | /// SymbolFlags and SymbolToDefinition maps. The maps must provide |
| 58 | /// entries for each definition in M. |
| 59 | /// This constructor is useful for delegating work from one |
| 60 | /// IRMaterializationUnit to another. |
| 61 | IRMaterializationUnit(std::unique_ptr<Module> M, SymbolFlagsMap SymbolFlags, |
| 62 | SymbolNameToDefinitionMap SymbolToDefinition); |
| 63 | |
| 64 | protected: |
| 65 | std::unique_ptr<Module> M; |
| 66 | SymbolNameToDefinitionMap SymbolToDefinition; |
| 67 | |
| 68 | private: |
| 69 | void discard(const VSO &V, SymbolStringPtr Name) override; |
| 70 | }; |
| 71 | |
| 72 | /// MaterializationUnit that materializes modules by calling the 'emit' method |
| 73 | /// on the given IRLayer. |
| 74 | class BasicIRLayerMaterializationUnit : public IRMaterializationUnit { |
| 75 | public: |
| 76 | BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K, |
| 77 | std::unique_ptr<Module> M); |
| 78 | private: |
| 79 | |
| 80 | void materialize(MaterializationResponsibility R) override; |
| 81 | |
| 82 | IRLayer &L; |
| 83 | VModuleKey K; |
| 84 | }; |
| 85 | |
| 86 | /// Interface for Layers that accept object files. |
| 87 | class ObjectLayer { |
| 88 | public: |
| 89 | ObjectLayer(ExecutionSession &ES); |
| 90 | virtual ~ObjectLayer(); |
| 91 | |
| 92 | /// Returns the execution session for this layer. |
| 93 | ExecutionSession &getExecutionSession() { return ES; } |
| 94 | |
| 95 | /// Adds a MaterializationUnit representing the given IR to the given VSO. |
| 96 | virtual Error add(VSO &V, VModuleKey K, std::unique_ptr<MemoryBuffer> O); |
| 97 | |
| 98 | /// Emit should materialize the given IR. |
| 99 | virtual void emit(MaterializationResponsibility R, VModuleKey K, |
| 100 | std::unique_ptr<MemoryBuffer> O) = 0; |
| 101 | |
| 102 | private: |
| 103 | ExecutionSession &ES; |
| 104 | }; |
| 105 | |
| 106 | /// Materializes the given object file (represented by a MemoryBuffer |
| 107 | /// instance) by calling 'emit' on the given ObjectLayer. |
| 108 | class BasicObjectLayerMaterializationUnit : public MaterializationUnit { |
| 109 | public: |
| 110 | static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>> |
| 111 | Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O); |
| 112 | |
| 113 | BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K, |
| 114 | std::unique_ptr<MemoryBuffer> O, |
| 115 | SymbolFlagsMap SymbolFlags); |
| 116 | |
| 117 | private: |
| 118 | |
| 119 | void materialize(MaterializationResponsibility R) override; |
| 120 | void discard(const VSO &V, SymbolStringPtr Name) override; |
| 121 | |
| 122 | ObjectLayer &L; |
| 123 | VModuleKey K; |
| 124 | std::unique_ptr<MemoryBuffer> O; |
| 125 | }; |
| 126 | |
| 127 | /// Returns a SymbolFlagsMap for the object file represented by the given |
| 128 | /// buffer, or an error if the buffer does not contain a valid object file. |
| 129 | // FIXME: Maybe move to Core.h? |
| 130 | Expected<SymbolFlagsMap> getObjectSymbolFlags(ExecutionSession &ES, |
| 131 | MemoryBufferRef ObjBuffer); |
| 132 | |
| 133 | } // End namespace orc |
| 134 | } // End namespace llvm |
| 135 | |
| 136 | #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H |