LLVM 22.0.0git
Layer.h
Go to the documentation of this file.
1//===---------------- Layer.h -- Layer interfaces --------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Layer interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_LAYER_H
14#define LLVM_EXECUTIONENGINE_ORC_LAYER_H
15
19#include "llvm/IR/Module.h"
24
25namespace llvm {
26namespace orc {
27
28/// IRMaterializationUnit is a convenient base class for MaterializationUnits
29/// wrapping LLVM IR. Represents materialization responsibility for all symbols
30/// in the given module. If symbols are overridden by other definitions, then
31/// their linkage is changed to available-externally.
33public:
34 using SymbolNameToDefinitionMap = std::map<SymbolStringPtr, GlobalValue *>;
35
36 /// Create an IRMaterializationLayer. Scans the module to build the
37 /// SymbolFlags and SymbolToDefinition maps.
41
42 /// Create an IRMaterializationLayer from a module, and pre-existing
43 /// SymbolFlags and SymbolToDefinition maps. The maps must provide
44 /// entries for each definition in M.
45 /// This constructor is useful for delegating work from one
46 /// IRMaterializationUnit to another.
48 SymbolNameToDefinitionMap SymbolToDefinition);
49
50 /// Return the ModuleIdentifier as the name for this MaterializationUnit.
51 StringRef getName() const override;
52
53 /// Return a reference to the contained ThreadSafeModule.
54 const ThreadSafeModule &getModule() const { return TSM; }
55
56protected:
59
60private:
61 static SymbolStringPtr getInitSymbol(ExecutionSession &ES,
62 const ThreadSafeModule &TSM);
63
64 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
65};
66
67/// Interface for layers that accept LLVM IR.
69public:
71 : ES(ES), MO(MO) {}
72
73 virtual ~IRLayer();
74
75 /// Returns the ExecutionSession for this layer.
77
78 /// Get the mangling options for this layer.
80 return MO;
81 }
82
83 /// Sets the CloneToNewContextOnEmit flag (false by default).
84 ///
85 /// When set, IR modules added to this layer will be cloned on to a new
86 /// context before emit is called. This can be used by clients who want
87 /// to load all IR using one LLVMContext (to save memory via type and
88 /// constant uniquing), but want to move Modules to fresh contexts before
89 /// compiling them to enable concurrent compilation.
90 /// Single threaded clients, or clients who load every module on a new
91 /// context, need not set this.
92 void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit) {
93 this->CloneToNewContextOnEmit = CloneToNewContextOnEmit;
94 }
95
96 /// Returns the current value of the CloneToNewContextOnEmit flag.
97 bool getCloneToNewContextOnEmit() const { return CloneToNewContextOnEmit; }
98
99 /// Add a MaterializatinoUnit representing the given IR to the JITDylib
100 /// targeted by the given tracker.
101 virtual Error add(ResourceTrackerSP RT, ThreadSafeModule TSM);
102
103 /// Adds a MaterializationUnit representing the given IR to the given
104 /// JITDylib. If RT is not specif
106 return add(JD.getDefaultResourceTracker(), std::move(TSM));
107 }
108
109 /// Emit should materialize the given IR.
110 virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
111 ThreadSafeModule TSM) = 0;
112
113private:
114 bool CloneToNewContextOnEmit = false;
117};
118
119/// MaterializationUnit that materializes modules by calling the 'emit' method
120/// on the given IRLayer.
122public:
125 ThreadSafeModule TSM);
126
127private:
128 void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
129
130 IRLayer &L;
131};
132
133/// Interface for Layers that accept object files.
134class LLVM_ABI ObjectLayer : public RTTIExtends<ObjectLayer, RTTIRoot> {
135public:
136 static char ID;
137
139 virtual ~ObjectLayer();
140
141 /// Returns the execution session for this layer.
143
144 /// Adds a MaterializationUnit for the object file in the given memory buffer
145 /// to the JITDylib for the given ResourceTracker.
146 virtual Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
148
149 /// Adds a MaterializationUnit for the object file in the given memory buffer
150 /// to the JITDylib for the given ResourceTracker. The interface for the
151 /// object will be built using the default object interface builder.
152 Error add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O);
153
154 /// Adds a MaterializationUnit for the object file in the given memory buffer
155 /// to the given JITDylib.
156 Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O,
158 return add(JD.getDefaultResourceTracker(), std::move(O), std::move(I));
159 }
160
161 /// Adds a MaterializationUnit for the object file in the given memory buffer
162 /// to the given JITDylib. The interface for the object will be built using
163 /// the default object interface builder.
164 Error add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O);
165
166 /// Emit should materialize the given IR.
167 virtual void emit(std::unique_ptr<MaterializationResponsibility> R,
168 std::unique_ptr<MemoryBuffer> O) = 0;
169
170private:
172};
173
174/// Materializes the given object file (represented by a MemoryBuffer
175/// instance) by calling 'emit' on the given ObjectLayer.
177 : public MaterializationUnit {
178public:
179 /// Create using the default object interface builder function.
181 Create(ObjectLayer &L, std::unique_ptr<MemoryBuffer> O);
182
184 std::unique_ptr<MemoryBuffer> O,
185 Interface I);
186
187 /// Return the buffer's identifier as the name for this MaterializationUnit.
188 StringRef getName() const override;
189
190private:
191 void materialize(std::unique_ptr<MaterializationResponsibility> R) override;
192 void discard(const JITDylib &JD, const SymbolStringPtr &Name) override;
193
194 ObjectLayer &L;
195 std::unique_ptr<MemoryBuffer> O;
196};
197
198} // End namespace orc
199} // End namespace llvm
200
201#endif // LLVM_EXECUTIONENGINE_ORC_LAYER_H
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
static StringRef getName(Value *V)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
Inheritance utility for extensible RTTI.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
MaterializationUnit that materializes modules by calling the 'emit' method on the given IRLayer.
Definition: Layer.h:121
Materializes the given object file (represented by a MemoryBuffer instance) by calling 'emit' on the ...
Definition: Layer.h:177
An ExecutionSession represents a running JIT program.
Definition: Core.h:1355
Interface for layers that accept LLVM IR.
Definition: Layer.h:68
IRLayer(ExecutionSession &ES, const IRSymbolMapper::ManglingOptions *&MO)
Definition: Layer.h:70
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, ThreadSafeModule TSM)=0
Emit should materialize the given IR.
ExecutionSession & getExecutionSession()
Returns the ExecutionSession for this layer.
Definition: Layer.h:76
void setCloneToNewContextOnEmit(bool CloneToNewContextOnEmit)
Sets the CloneToNewContextOnEmit flag (false by default).
Definition: Layer.h:92
Error add(JITDylib &JD, ThreadSafeModule TSM)
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.h:105
const IRSymbolMapper::ManglingOptions *& getManglingOptions() const
Get the mangling options for this layer.
Definition: Layer.h:79
bool getCloneToNewContextOnEmit() const
Returns the current value of the CloneToNewContextOnEmit flag.
Definition: Layer.h:97
IRMaterializationUnit is a convenient base class for MaterializationUnits wrapping LLVM IR.
Definition: Layer.h:32
SymbolNameToDefinitionMap SymbolToDefinition
Definition: Layer.h:58
const ThreadSafeModule & getModule() const
Return a reference to the contained ThreadSafeModule.
Definition: Layer.h:54
ThreadSafeModule TSM
Definition: Layer.h:57
std::map< SymbolStringPtr, GlobalValue * > SymbolNameToDefinitionMap
Definition: Layer.h:34
Represents a JIT'd dynamic library.
Definition: Core.h:902
LLVM_ABI ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:671
A MaterializationUnit represents a set of symbol definitions that can be materialized as a group,...
Interface for Layers that accept object files.
Definition: Layer.h:134
Error add(JITDylib &JD, std::unique_ptr< MemoryBuffer > O, MaterializationUnit::Interface I)
Adds a MaterializationUnit for the object file in the given memory buffer to the given JITDylib.
Definition: Layer.h:156
virtual void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O)=0
Emit should materialize the given IR.
static char ID
Definition: Layer.h:136
ExecutionSession & getExecutionSession()
Returns the execution session for this layer.
Definition: Layer.h:142
Pointer to a pooled string representing a symbol name.
An LLVM Module together with a shared ThreadSafeContext.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18