Line data Source code
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/ExecutionEngine/Orc/ThreadSafeModule.h"
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 0 : ExecutionSession &getExecutionSession() { return ES; }
33 :
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 1 : this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
45 : }
46 :
47 : /// Returns the current value of the CloneToNewContextOnEmit flag.
48 0 : 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, ThreadSafeModule TSM,
53 : VModuleKey K = VModuleKey());
54 :
55 : /// Emit should materialize the given IR.
56 : virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM) = 0;
57 :
58 : private:
59 : bool CloneToNewContextOnEmit = false;
60 : ExecutionSession &ES;
61 : };
62 :
63 : /// IRMaterializationUnit is a convenient base class for MaterializationUnits
64 : /// wrapping LLVM IR. Represents materialization responsibility for all symbols
65 : /// in the given module. If symbols are overridden by other definitions, then
66 : /// their linkage is changed to available-externally.
67 : class IRMaterializationUnit : public MaterializationUnit {
68 : public:
69 : using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
70 :
71 : /// Create an IRMaterializationLayer. Scans the module to build the
72 : /// SymbolFlags and SymbolToDefinition maps.
73 : IRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM,
74 : VModuleKey K);
75 :
76 : /// Create an IRMaterializationLayer from a module, and pre-existing
77 : /// SymbolFlags and SymbolToDefinition maps. The maps must provide
78 : /// entries for each definition in M.
79 : /// This constructor is useful for delegating work from one
80 : /// IRMaterializationUnit to another.
81 : IRMaterializationUnit(ThreadSafeModule TSM, VModuleKey K,
82 : SymbolFlagsMap SymbolFlags,
83 : SymbolNameToDefinitionMap SymbolToDefinition);
84 :
85 : /// Return the ModuleIdentifier as the name for this MaterializationUnit.
86 : StringRef getName() const override;
87 :
88 : const ThreadSafeModule &getModule() const { return TSM; }
89 :
90 : protected:
91 : ThreadSafeModule TSM;
92 : SymbolNameToDefinitionMap SymbolToDefinition;
93 :
94 : private:
95 : void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
96 : };
97 :
98 : /// MaterializationUnit that materializes modules by calling the 'emit' method
99 : /// on the given IRLayer.
100 : class BasicIRLayerMaterializationUnit : public IRMaterializationUnit {
101 : public:
102 : BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K,
103 : ThreadSafeModule TSM);
104 :
105 : private:
106 :
107 : void materialize(MaterializationResponsibility R) override;
108 :
109 : IRLayer &L;
110 : VModuleKey K;
111 : };
112 :
113 : /// Interface for Layers that accept object files.
114 : class ObjectLayer {
115 : public:
116 : ObjectLayer(ExecutionSession &ES);
117 : virtual ~ObjectLayer();
118 :
119 : /// Returns the execution session for this layer.
120 0 : ExecutionSession &getExecutionSession() { return ES; }
121 :
122 : /// Adds a MaterializationUnit representing the given IR to the given
123 : /// JITDylib.
124 : virtual Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
125 : VModuleKey K = VModuleKey());
126 :
127 : /// Emit should materialize the given IR.
128 : virtual void emit(MaterializationResponsibility R,
129 : std::unique_ptr<MemoryBuffer> O) = 0;
130 :
131 : private:
132 : ExecutionSession &ES;
133 : };
134 :
135 : /// Materializes the given object file (represented by a MemoryBuffer
136 : /// instance) by calling 'emit' on the given ObjectLayer.
137 : class BasicObjectLayerMaterializationUnit : public MaterializationUnit {
138 : public:
139 : static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
140 : Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O);
141 :
142 : BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K,
143 : std::unique_ptr<MemoryBuffer> O,
144 : SymbolFlagsMap SymbolFlags);
145 :
146 : /// Return the buffer's identifier as the name for this MaterializationUnit.
147 : StringRef getName() const override;
148 :
149 : private:
150 :
151 : void materialize(MaterializationResponsibility R) override;
152 : void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
153 :
154 : ObjectLayer &L;
155 : std::unique_ptr<MemoryBuffer> O;
156 : };
157 :
158 : /// Returns a SymbolFlagsMap for the object file represented by the given
159 : /// buffer, or an error if the buffer does not contain a valid object file.
160 : // FIXME: Maybe move to Core.h?
161 : Expected<SymbolFlagsMap> getObjectSymbolFlags(ExecutionSession &ES,
162 : MemoryBufferRef ObjBuffer);
163 :
164 : } // End namespace orc
165 : } // End namespace llvm
166 :
167 : #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
|