blob: 3bd23ae54165dc8ce81e7e4eae014fbde6203089 [file] [log] [blame]
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001//===---------------- 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 Scull0372a572018-11-16 15:47:06 +000018#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010019#include "llvm/IR/Module.h"
20#include "llvm/Support/MemoryBuffer.h"
21
22namespace llvm {
23namespace orc {
24
25/// Interface for layers that accept LLVM IR.
26class IRLayer {
27public:
28 IRLayer(ExecutionSession &ES);
29 virtual ~IRLayer();
30
31 /// Returns the ExecutionSession for this layer.
32 ExecutionSession &getExecutionSession() { return ES; }
33
Andrew Scull0372a572018-11-16 15:47:06 +000034 /// 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 Scullcdfcccc2018-10-05 20:58:37 +010059
60 /// Emit should materialize the given IR.
61 virtual void emit(MaterializationResponsibility R, VModuleKey K,
Andrew Scull0372a572018-11-16 15:47:06 +000062 ThreadSafeModule TSM) = 0;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063
64private:
Andrew Scull0372a572018-11-16 15:47:06 +000065 bool CloneToNewContextOnEmit = false;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010066 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.
73class IRMaterializationUnit : public MaterializationUnit {
74public:
75 using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
76
77 /// Create an IRMaterializationLayer. Scans the module to build the
78 /// SymbolFlags and SymbolToDefinition maps.
Andrew Scull0372a572018-11-16 15:47:06 +000079 IRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080
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 Scull0372a572018-11-16 15:47:06 +000086 IRMaterializationUnit(ThreadSafeModule TSM, SymbolFlagsMap SymbolFlags,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087 SymbolNameToDefinitionMap SymbolToDefinition);
88
Andrew Scull0372a572018-11-16 15:47:06 +000089 /// Return the ModuleIdentifier as the name for this MaterializationUnit.
90 StringRef getName() const override;
91
92 const ThreadSafeModule &getModule() const { return TSM; }
93
Andrew Scullcdfcccc2018-10-05 20:58:37 +010094protected:
Andrew Scull0372a572018-11-16 15:47:06 +000095 ThreadSafeModule TSM;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010096 SymbolNameToDefinitionMap SymbolToDefinition;
97
98private:
Andrew Scull0372a572018-11-16 15:47:06 +000099 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100100};
101
102/// MaterializationUnit that materializes modules by calling the 'emit' method
103/// on the given IRLayer.
104class BasicIRLayerMaterializationUnit : public IRMaterializationUnit {
105public:
106 BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K,
Andrew Scull0372a572018-11-16 15:47:06 +0000107 ThreadSafeModule TSM);
108
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100109private:
110
111 void materialize(MaterializationResponsibility R) override;
112
113 IRLayer &L;
114 VModuleKey K;
115};
116
117/// Interface for Layers that accept object files.
118class ObjectLayer {
119public:
120 ObjectLayer(ExecutionSession &ES);
121 virtual ~ObjectLayer();
122
123 /// Returns the execution session for this layer.
124 ExecutionSession &getExecutionSession() { return ES; }
125
Andrew Scull0372a572018-11-16 15:47:06 +0000126 /// 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 Scullcdfcccc2018-10-05 20:58:37 +0100135
136 /// Emit should materialize the given IR.
137 virtual void emit(MaterializationResponsibility R, VModuleKey K,
138 std::unique_ptr<MemoryBuffer> O) = 0;
139
140private:
141 ExecutionSession &ES;
142};
143
144/// Materializes the given object file (represented by a MemoryBuffer
145/// instance) by calling 'emit' on the given ObjectLayer.
146class BasicObjectLayerMaterializationUnit : public MaterializationUnit {
147public:
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 Scull0372a572018-11-16 15:47:06 +0000155 /// Return the buffer's identifier as the name for this MaterializationUnit.
156 StringRef getName() const override;
157
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100158private:
159
160 void materialize(MaterializationResponsibility R) override;
Andrew Scull0372a572018-11-16 15:47:06 +0000161 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100162
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?
171Expected<SymbolFlagsMap> getObjectSymbolFlags(ExecutionSession &ES,
172 MemoryBufferRef ObjBuffer);
173
174} // End namespace orc
175} // End namespace llvm
176
177#endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H