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