LLVM 20.0.0git
LinkGraphLinkingLayer.h
Go to the documentation of this file.
1//===-- LinkGraphLinkingLayer.h - Link LinkGraphs with JITLink --*- 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// LinkGraphLinkingLayer and associated utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLINKINGLAYER_H
14#define LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLINKINGLAYER_H
15
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include <algorithm>
24#include <cassert>
25#include <functional>
26#include <memory>
27#include <mutex>
28#include <utility>
29#include <vector>
30
31namespace llvm {
32
33namespace jitlink {
34class EHFrameRegistrar;
35} // namespace jitlink
36
37namespace orc {
38
39/// LinkGraphLinkingLayer links LinkGraphs into the Executor using JITLink.
40///
41/// Clients can use this class to add LinkGraphs to an ExecutionSession, and it
42/// serves as a base for the ObjectLinkingLayer that can link object files.
44 class JITLinkCtx;
45
46public:
47 /// Plugin instances can be added to the ObjectLinkingLayer to receive
48 /// callbacks when code is loaded or emitted, and when JITLink is being
49 /// configured.
50 class Plugin {
51 public:
52 virtual ~Plugin();
56
57 // Deprecated. Don't use this in new code. There will be a proper mechanism
58 // for capturing object buffers.
62 MemoryBufferRef InputObject) {}
63
66 return Error::success();
67 }
71 ResourceKey SrcKey) = 0;
72 };
73
74 /// Construct a LinkGraphLinkingLayer using the ExecutorProcessControl
75 /// instance's memory manager.
77
78 /// Construct a LinkGraphLinkingLayer using a custom memory manager.
81
82 /// Construct an LinkGraphLinkingLayer. Takes ownership of the given
83 /// JITLinkMemoryManager. This method is a temporary hack to simplify
84 /// co-existence with RTDyldObjectLinkingLayer (which also owns its
85 /// allocators).
87 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
88
89 /// Destroy the LinkGraphLinkingLayer.
91
92 /// Add a plugin.
93 LinkGraphLinkingLayer &addPlugin(std::shared_ptr<Plugin> P) {
94 std::lock_guard<std::mutex> Lock(LayerMutex);
95 Plugins.push_back(std::move(P));
96 return *this;
97 }
98
99 /// Remove a plugin. This remove applies only to subsequent links (links
100 /// already underway will continue to use the plugin), and does not of itself
101 /// destroy the plugin -- destruction will happen once all shared pointers
102 /// (including those held by in-progress links) are destroyed.
104 std::lock_guard<std::mutex> Lock(LayerMutex);
105 auto I = llvm::find_if(Plugins, [&](const std::shared_ptr<Plugin> &Elem) {
106 return Elem.get() == &P;
107 });
108 assert(I != Plugins.end() && "Plugin not present");
109 Plugins.erase(I);
110 }
111
112 /// Emit a LinkGraph.
113 void emit(std::unique_ptr<MaterializationResponsibility> R,
114 std::unique_ptr<jitlink::LinkGraph> G) override;
115
116 /// Instructs this LinkgraphLinkingLayer instance to override the symbol flags
117 /// found in the LinkGraph with the flags supplied by the
118 /// MaterializationResponsibility instance. This is a workaround to support
119 /// symbol visibility in COFF, which does not use the libObject's
120 /// SF_Exported flag. Use only when generating / adding COFF object files.
121 ///
122 /// FIXME: We should be able to remove this if/when COFF properly tracks
123 /// exported symbols.
126 this->OverrideObjectFlags = OverrideObjectFlags;
127 return *this;
128 }
129
130 /// If set, this LinkGraphLinkingLayer instance will claim responsibility
131 /// for any symbols provided by a given object file that were not already in
132 /// the MaterializationResponsibility instance. Setting this flag allows
133 /// higher-level program representations (e.g. LLVM IR) to be added based on
134 /// only a subset of the symbols they provide, without having to write
135 /// intervening layers to scan and add the additional symbols. This trades
136 /// diagnostic quality for convenience however: If all symbols are enumerated
137 /// up-front then clashes can be detected and reported early (and usually
138 /// deterministically). If this option is set, clashes for the additional
139 /// symbols may not be detected until late, and detection may depend on
140 /// the flow of control through JIT'd code. Use with care.
142 setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
143 this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
144 return *this;
145 }
146
147protected:
148 /// Emit a LinkGraph with the given backing buffer.
149 ///
150 /// This overload is intended for use by ObjectLinkingLayer.
151 void emit(std::unique_ptr<MaterializationResponsibility> R,
152 std::unique_ptr<jitlink::LinkGraph> G,
153 std::unique_ptr<MemoryBuffer> ObjBuf);
154
155 std::function<void(std::unique_ptr<MemoryBuffer>)> ReturnObjectBuffer;
156
157private:
159
160 Error recordFinalizedAlloc(MaterializationResponsibility &MR,
161 FinalizedAlloc FA);
162
163 Error handleRemoveResources(JITDylib &JD, ResourceKey K) override;
164 void handleTransferResources(JITDylib &JD, ResourceKey DstKey,
165 ResourceKey SrcKey) override;
166
167 mutable std::mutex LayerMutex;
169 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgrOwnership;
170 bool OverrideObjectFlags = false;
171 bool AutoClaimObjectSymbols = false;
173 std::vector<std::shared_ptr<Plugin>> Plugins;
174};
175
177public:
180 std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
183 jitlink::PassConfiguration &PassConfig) override;
188 ResourceKey SrcKey) override;
189
190private:
191 std::mutex EHFramePluginMutex;
193 std::unique_ptr<jitlink::EHFrameRegistrar> Registrar;
196};
197
198} // end namespace orc
199} // end namespace llvm
200
201#endif // LLVM_EXECUTIONENGINE_ORC_LINKGRAPHLINKINGLAYER_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.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
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:1339
Represents a JIT'd dynamic library.
Definition: Core.h:897
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
virtual Error notifyEmitted(MaterializationResponsibility &MR)
virtual Error notifyRemovingResources(JITDylib &JD, ResourceKey K)=0
virtual Error notifyFailed(MaterializationResponsibility &MR)=0
virtual void notifyLoaded(MaterializationResponsibility &MR)
virtual void notifyMaterializing(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::JITLinkContext &Ctx, MemoryBufferRef InputObject)
virtual void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &Config)
virtual void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, ResourceKey SrcKey)=0
LinkGraphLinkingLayer links LinkGraphs into the Executor using JITLink.
LinkGraphLinkingLayer(ExecutionSession &ES, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr)
Construct an LinkGraphLinkingLayer.
LinkGraphLinkingLayer & setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols)
If set, this LinkGraphLinkingLayer instance will claim responsibility for any symbols provided by a g...
void removePlugin(Plugin &P)
Remove a plugin.
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< jitlink::LinkGraph > G) override
Emit a LinkGraph.
LinkGraphLinkingLayer & setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags)
Instructs this LinkgraphLinkingLayer instance to override the symbol flags found in the LinkGraph wit...
std::function< void(std::unique_ptr< MemoryBuffer >)> ReturnObjectBuffer
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< jitlink::LinkGraph > G, std::unique_ptr< MemoryBuffer > ObjBuf)
Emit a LinkGraph with the given backing buffer.
~LinkGraphLinkingLayer()
Destroy the LinkGraphLinkingLayer.
LinkGraphLinkingLayer & addPlugin(std::shared_ptr< Plugin > P)
Add a plugin.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:571
Listens for ResourceTracker operations.
Definition: Core.h:125
uintptr_t ResourceKey
Definition: Core.h:74
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:1766