1 //===---------------- Layer.h -- Layer interfaces --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_LAYER_H
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"
25 /// Interface for layers that accept LLVM IR.
28 IRLayer(ExecutionSession &ES);
31 /// Returns the ExecutionSession for this layer.
32 ExecutionSession &getExecutionSession() { return ES; }
34 /// Sets the CloneToNewContextOnEmit flag (false by default).
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;
47 /// Returns the current value of the CloneToNewContextOnEmit flag.
48 bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
50 /// Adds a MaterializationUnit representing the given IR to the given
52 virtual Error add(JITDylib &JD, ThreadSafeModule TSM,
53 VModuleKey K = VModuleKey());
55 /// Emit should materialize the given IR.
56 virtual void emit(MaterializationResponsibility R, ThreadSafeModule TSM) = 0;
59 bool CloneToNewContextOnEmit = false;
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 {
69 using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
71 /// Create an IRMaterializationLayer. Scans the module to build the
72 /// SymbolFlags and SymbolToDefinition maps.
73 IRMaterializationUnit(ExecutionSession &ES, ThreadSafeModule TSM,
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);
85 /// Return the ModuleIdentifier as the name for this MaterializationUnit.
86 StringRef getName() const override;
88 const ThreadSafeModule &getModule() const { return TSM; }
92 SymbolNameToDefinitionMap SymbolToDefinition;
95 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
98 /// MaterializationUnit that materializes modules by calling the 'emit' method
99 /// on the given IRLayer.
100 class BasicIRLayerMaterializationUnit : public IRMaterializationUnit {
102 BasicIRLayerMaterializationUnit(IRLayer &L, VModuleKey K,
103 ThreadSafeModule TSM);
107 void materialize(MaterializationResponsibility R) override;
113 /// Interface for Layers that accept object files.
116 ObjectLayer(ExecutionSession &ES);
117 virtual ~ObjectLayer();
119 /// Returns the execution session for this layer.
120 ExecutionSession &getExecutionSession() { return ES; }
122 /// Adds a MaterializationUnit representing the given IR to the given
124 virtual Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
125 VModuleKey K = VModuleKey());
127 /// Emit should materialize the given IR.
128 virtual void emit(MaterializationResponsibility R,
129 std::unique_ptr<MemoryBuffer> O) = 0;
132 ExecutionSession &ES;
135 /// Materializes the given object file (represented by a MemoryBuffer
136 /// instance) by calling 'emit' on the given ObjectLayer.
137 class BasicObjectLayerMaterializationUnit : public MaterializationUnit {
139 static Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
140 Create(ObjectLayer &L, VModuleKey K, std::unique_ptr<MemoryBuffer> O);
142 BasicObjectLayerMaterializationUnit(ObjectLayer &L, VModuleKey K,
143 std::unique_ptr<MemoryBuffer> O,
144 SymbolFlagsMap SymbolFlags);
146 /// Return the buffer's identifier as the name for this MaterializationUnit.
147 StringRef getName() const override;
151 void materialize(MaterializationResponsibility R) override;
152 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
155 std::unique_ptr<MemoryBuffer> O;
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);
164 } // End namespace orc
165 } // End namespace llvm
167 #endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H