LLVM 20.0.0git
ObjectLinkingLayer.h
Go to the documentation of this file.
1//===-- ObjectLinkingLayer.h - JITLink-based jit linking layer --*- 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// Contains the definition for an JITLink-based, in-process object linking
10// layer.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
15#define LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
16
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/Error.h"
24#include <algorithm>
25#include <cassert>
26#include <functional>
27#include <list>
28#include <memory>
29#include <utility>
30#include <vector>
31
32namespace llvm {
33
34namespace jitlink {
35class EHFrameRegistrar;
36class LinkGraph;
37class Symbol;
38} // namespace jitlink
39
40namespace orc {
41
42class ObjectLinkingLayerJITLinkContext;
43
44/// An ObjectLayer implementation built on JITLink.
45///
46/// Clients can use this class to add relocatable object files to an
47/// ExecutionSession, and it typically serves as the base layer (underneath
48/// a compiling layer like IRCompileLayer) for the rest of the JIT.
49class ObjectLinkingLayer : public RTTIExtends<ObjectLinkingLayer, ObjectLayer>,
50 private ResourceManager {
52
53public:
54 static char ID;
55
56 /// Plugin instances can be added to the ObjectLinkingLayer to receive
57 /// callbacks when code is loaded or emitted, and when JITLink is being
58 /// configured.
59 class Plugin {
60 public:
64
65 virtual ~Plugin();
69
70 // Deprecated. Don't use this in new code. There will be a proper mechanism
71 // for capturing object buffers.
75 MemoryBufferRef InputObject) {}
76
79 return Error::success();
80 }
84 ResourceKey SrcKey) = 0;
85
86 /// Return any dependencies that synthetic symbols (e.g. init symbols)
87 /// have on symbols in the LinkGraph.
88 /// This is used by the ObjectLinkingLayer to update the dependencies for
89 /// the synthetic symbols.
93 }
94 };
95
97 std::function<void(std::unique_ptr<MemoryBuffer>)>;
98
99 /// Construct an ObjectLinkingLayer using the ExecutorProcessControl
100 /// instance's memory manager.
102
103 /// Construct an ObjectLinkingLayer using a custom memory manager.
106
107 /// Construct an ObjectLinkingLayer. Takes ownership of the given
108 /// JITLinkMemoryManager. This method is a temporary hack to simplify
109 /// co-existence with RTDyldObjectLinkingLayer (which also owns its
110 /// allocators).
112 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
113
114 /// Destruct an ObjectLinkingLayer.
116
117 /// Set an object buffer return function. By default object buffers are
118 /// deleted once the JIT has linked them. If a return function is set then
119 /// it will be called to transfer ownership of the buffer instead.
121 this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
122 }
123
124 /// Add a plugin.
125 ObjectLinkingLayer &addPlugin(std::shared_ptr<Plugin> P) {
126 std::lock_guard<std::mutex> Lock(LayerMutex);
127 Plugins.push_back(std::move(P));
128 return *this;
129 }
130
131 /// Remove a plugin. This remove applies only to subsequent links (links
132 /// already underway will continue to use the plugin), and does not of itself
133 /// destroy the plugin -- destruction will happen once all shared pointers
134 /// (including those held by in-progress links) are destroyed.
136 std::lock_guard<std::mutex> Lock(LayerMutex);
137 auto I = llvm::find_if(Plugins, [&](const std::shared_ptr<Plugin> &Elem) {
138 return Elem.get() == &P;
139 });
140 assert(I != Plugins.end() && "Plugin not present");
141 Plugins.erase(I);
142 }
143
144 /// Add a LinkGraph to the JITDylib targeted by the given tracker.
145 Error add(ResourceTrackerSP, std::unique_ptr<jitlink::LinkGraph> G);
146
147 /// Add a LinkGraph to the given JITDylib.
148 Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G) {
149 return add(JD.getDefaultResourceTracker(), std::move(G));
150 }
151
152 // Un-hide ObjectLayer add methods.
153 using ObjectLayer::add;
154
155 /// Emit an object file.
156 void emit(std::unique_ptr<MaterializationResponsibility> R,
157 std::unique_ptr<MemoryBuffer> O) override;
158
159 /// Emit a LinkGraph.
160 void emit(std::unique_ptr<MaterializationResponsibility> R,
161 std::unique_ptr<jitlink::LinkGraph> G);
162
163 /// Instructs this ObjectLinkingLayer instance to override the symbol flags
164 /// found in the AtomGraph with the flags supplied by the
165 /// MaterializationResponsibility instance. This is a workaround to support
166 /// symbol visibility in COFF, which does not use the libObject's
167 /// SF_Exported flag. Use only when generating / adding COFF object files.
168 ///
169 /// FIXME: We should be able to remove this if/when COFF properly tracks
170 /// exported symbols.
173 this->OverrideObjectFlags = OverrideObjectFlags;
174 return *this;
175 }
176
177 /// If set, this ObjectLinkingLayer instance will claim responsibility
178 /// for any symbols provided by a given object file that were not already in
179 /// the MaterializationResponsibility instance. Setting this flag allows
180 /// higher-level program representations (e.g. LLVM IR) to be added based on
181 /// only a subset of the symbols they provide, without having to write
182 /// intervening layers to scan and add the additional symbols. This trades
183 /// diagnostic quality for convenience however: If all symbols are enumerated
184 /// up-front then clashes can be detected and reported early (and usually
185 /// deterministically). If this option is set, clashes for the additional
186 /// symbols may not be detected until late, and detection may depend on
187 /// the flow of control through JIT'd code. Use with care.
189 setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
190 this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
191 return *this;
192 }
193
194private:
196
197 Error recordFinalizedAlloc(MaterializationResponsibility &MR,
198 FinalizedAlloc FA);
199
200 Error handleRemoveResources(JITDylib &JD, ResourceKey K) override;
201 void handleTransferResources(JITDylib &JD, ResourceKey DstKey,
202 ResourceKey SrcKey) override;
203
204 mutable std::mutex LayerMutex;
206 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgrOwnership;
207 bool OverrideObjectFlags = false;
208 bool AutoClaimObjectSymbols = false;
209 ReturnObjectBufferFunction ReturnObjectBuffer;
211 std::vector<std::shared_ptr<Plugin>> Plugins;
212};
213
215public:
218 std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
221 jitlink::PassConfiguration &PassConfig) override;
226 ResourceKey SrcKey) override;
227
228private:
229 std::mutex EHFramePluginMutex;
231 std::unique_ptr<jitlink::EHFrameRegistrar> Registrar;
234};
235
236} // end namespace orc
237} // end namespace llvm
238
239#endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
RelaxConfig Config
Definition: ELF_riscv.cpp:506
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Inheritance utility for extensible RTTI.
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey) override
Error notifyEmitted(MaterializationResponsibility &MR) override
Error notifyFailed(MaterializationResponsibility &MR) override
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &PassConfig) override
An ExecutionSession represents a running JIT program.
Definition: Core.h:1431
Represents a JIT'd dynamic library.
Definition: Core.h:989
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:691
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:555
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
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
virtual Error notifyRemovingResources(JITDylib &JD, ResourceKey K)=0
virtual void notifyMaterializing(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::JITLinkContext &Ctx, MemoryBufferRef InputObject)
virtual Error notifyFailed(MaterializationResponsibility &MR)=0
DenseMap< SymbolStringPtr, JITLinkSymbolSet > SyntheticSymbolDependenciesMap
virtual void notifyLoaded(MaterializationResponsibility &MR)
virtual Error notifyEmitted(MaterializationResponsibility &MR)
virtual void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &Config)
virtual SyntheticSymbolDependenciesMap getSyntheticSymbolDependencies(MaterializationResponsibility &MR)
Return any dependencies that synthetic symbols (e.g.
virtual void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey)=0
An ObjectLayer implementation built on JITLink.
void removePlugin(Plugin &P)
Remove a plugin.
ObjectLinkingLayer & addPlugin(std::shared_ptr< Plugin > P)
Add a plugin.
ObjectLinkingLayer & setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols)
If set, this ObjectLinkingLayer instance will claim responsibility for any symbols provided by a give...
ObjectLinkingLayer & setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags)
Instructs this ObjectLinkingLayer instance to override the symbol flags found in the AtomGraph with t...
void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer)
Set an object buffer return function.
std::function< void(std::unique_ptr< MemoryBuffer >)> ReturnObjectBufferFunction
Error add(ResourceTrackerSP, std::unique_ptr< jitlink::LinkGraph > G)
Add a LinkGraph to the JITDylib targeted by the given tracker.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< jitlink::LinkGraph > G)
Emit a LinkGraph.
~ObjectLinkingLayer()
Destruct an ObjectLinkingLayer.
ObjectLinkingLayer(ExecutionSession &ES, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr)
Construct an ObjectLinkingLayer.
Error add(JITDylib &JD, std::unique_ptr< jitlink::LinkGraph > G)
Add a LinkGraph to the given JITDylib.
Listens for ResourceTracker operations.
Definition: Core.h:104
uintptr_t ResourceKey
Definition: Core.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749