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