LLVM  14.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/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/Error.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <functional>
28 #include <list>
29 #include <memory>
30 #include <utility>
31 #include <vector>
32 
33 namespace llvm {
34 
35 namespace jitlink {
36 class EHFrameRegistrar;
37 class LinkGraph;
38 class Symbol;
39 } // namespace jitlink
40 
41 namespace object {
42 class ObjectFile;
43 } // namespace object
44 
45 namespace orc {
46 
47 class ObjectLinkingLayerJITLinkContext;
48 
49 /// An ObjectLayer implementation built on JITLink.
50 ///
51 /// Clients can use this class to add relocatable object files to an
52 /// ExecutionSession, and it typically serves as the base layer (underneath
53 /// a compiling layer like IRCompileLayer) for the rest of the JIT.
54 class ObjectLinkingLayer : public RTTIExtends<ObjectLinkingLayer, ObjectLayer>,
55  private ResourceManager {
57 
58 public:
59  static char ID;
60 
61  /// Plugin instances can be added to the ObjectLinkingLayer to receive
62  /// callbacks when code is loaded or emitted, and when JITLink is being
63  /// configured.
64  class Plugin {
65  public:
69 
70  virtual ~Plugin();
73  jitlink::PassConfiguration &Config) {}
74 
75  // Deprecated. Don't use this in new code. There will be a proper mechanism
76  // for capturing object buffers.
80  MemoryBufferRef InputObject) {}
81 
84  return Error::success();
85  }
88  virtual void notifyTransferringResources(ResourceKey DstKey,
89  ResourceKey SrcKey) = 0;
90 
91  /// Return any dependencies that synthetic symbols (e.g. init symbols)
92  /// have on symbols in the LinkGraph.
93  /// This is used by the ObjectLinkingLayer to update the dependencies for
94  /// the synthetic symbols.
98  }
99  };
100 
102  std::function<void(std::unique_ptr<MemoryBuffer>)>;
103 
104  /// Construct an ObjectLinkingLayer using the ExecutorProcessControl
105  /// instance's memory manager.
107 
108  /// Construct an ObjectLinkingLayer using a custom memory manager.
111 
112  /// Construct an ObjectLinkingLayer. Takes ownership of the given
113  /// JITLinkMemoryManager. This method is a temporary hack to simplify
114  /// co-existence with RTDyldObjectLinkingLayer (which also owns its
115  /// allocators).
117  std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
118 
119  /// Destruct an ObjectLinkingLayer.
121 
122  /// Set an object buffer return function. By default object buffers are
123  /// deleted once the JIT has linked them. If a return function is set then
124  /// it will be called to transfer ownership of the buffer instead.
126  this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
127  }
128 
129  /// Add a pass-config modifier.
130  ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
131  std::lock_guard<std::mutex> Lock(LayerMutex);
132  Plugins.push_back(std::move(P));
133  return *this;
134  }
135 
136  /// Add a LinkGraph to the JITDylib targeted by the given tracker.
137  Error add(ResourceTrackerSP, std::unique_ptr<jitlink::LinkGraph> G);
138 
139  /// Add a LinkGraph to the given JITDylib.
140  Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G) {
141  return add(JD.getDefaultResourceTracker(), std::move(G));
142  }
143 
144  // Un-hide ObjectLayer add methods.
145  using ObjectLayer::add;
146 
147  /// Emit an object file.
148  void emit(std::unique_ptr<MaterializationResponsibility> R,
149  std::unique_ptr<MemoryBuffer> O) override;
150 
151  /// Emit a LinkGraph.
152  void emit(std::unique_ptr<MaterializationResponsibility> R,
153  std::unique_ptr<jitlink::LinkGraph> G);
154 
155  /// Instructs this ObjectLinkingLayer instance to override the symbol flags
156  /// found in the AtomGraph with the flags supplied by the
157  /// MaterializationResponsibility instance. This is a workaround to support
158  /// symbol visibility in COFF, which does not use the libObject's
159  /// SF_Exported flag. Use only when generating / adding COFF object files.
160  ///
161  /// FIXME: We should be able to remove this if/when COFF properly tracks
162  /// exported symbols.
165  this->OverrideObjectFlags = OverrideObjectFlags;
166  return *this;
167  }
168 
169  /// If set, this ObjectLinkingLayer instance will claim responsibility
170  /// for any symbols provided by a given object file that were not already in
171  /// the MaterializationResponsibility instance. Setting this flag allows
172  /// higher-level program representations (e.g. LLVM IR) to be added based on
173  /// only a subset of the symbols they provide, without having to write
174  /// intervening layers to scan and add the additional symbols. This trades
175  /// diagnostic quality for convenience however: If all symbols are enumerated
176  /// up-front then clashes can be detected and reported early (and usually
177  /// deterministically). If this option is set, clashes for the additional
178  /// symbols may not be detected until late, and detection may depend on
179  /// the flow of control through JIT'd code. Use with care.
181  setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
182  this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
183  return *this;
184  }
185 
186 private:
187  using FinalizedAlloc = jitlink::JITLinkMemoryManager::FinalizedAlloc;
188 
189  void modifyPassConfig(MaterializationResponsibility &MR,
191  jitlink::PassConfiguration &PassConfig);
192  void notifyLoaded(MaterializationResponsibility &MR);
193  Error notifyEmitted(MaterializationResponsibility &MR, FinalizedAlloc FA);
194 
195  Error handleRemoveResources(ResourceKey K) override;
196  void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override;
197 
198  mutable std::mutex LayerMutex;
200  std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgrOwnership;
201  bool OverrideObjectFlags = false;
202  bool AutoClaimObjectSymbols = false;
203  ReturnObjectBufferFunction ReturnObjectBuffer;
205  std::vector<std::unique_ptr<Plugin>> Plugins;
206 };
207 
209 public:
211  ExecutionSession &ES,
212  std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
215  jitlink::PassConfiguration &PassConfig) override;
220  ResourceKey SrcKey) override;
221 
222 private:
223 
224  struct EHFrameRange {
226  size_t Size;
227  };
228 
229  std::mutex EHFramePluginMutex;
230  ExecutionSession &ES;
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
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::orc::ResourceKey
uintptr_t ResourceKey
Definition: Core.h:50
llvm::orc::ObjectLinkingLayer::emit
void emit(std::unique_ptr< MaterializationResponsibility > R, std::unique_ptr< MemoryBuffer > O) override
Emit an object file.
Definition: ObjectLinkingLayer.cpp:648
llvm::orc::MaterializationResponsibility
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:520
llvm::orc::ObjectLinkingLayer::add
Error add(JITDylib &JD, std::unique_ptr< jitlink::LinkGraph > G)
Add a LinkGraph to the given JITDylib.
Definition: ObjectLinkingLayer.h:140
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::orc::JITDylib
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:922
llvm::orc::ObjectLinkingLayer::Plugin::notifyFailed
virtual Error notifyFailed(MaterializationResponsibility &MR)=0
llvm::orc::EHFrameRegistrationPlugin::EHFrameRegistrationPlugin
EHFrameRegistrationPlugin(ExecutionSession &ES, std::unique_ptr< jitlink::EHFrameRegistrar > Registrar)
Definition: ObjectLinkingLayer.cpp:740
JITSymbol.h
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::orc::ObjectLinkingLayer::Plugin::notifyMaterializing
virtual void notifyMaterializing(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::JITLinkContext &Ctx, MemoryBufferRef InputObject)
Definition: ObjectLinkingLayer.h:77
llvm::orc::JITDylib::getDefaultResourceTracker
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:629
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
Error.h
llvm::orc::EHFrameRegistrationPlugin::notifyRemovingResources
Error notifyRemovingResources(ResourceKey K) override
Definition: ObjectLinkingLayer.cpp:789
llvm::orc::EHFrameRegistrationPlugin::notifyTransferringResources
void notifyTransferringResources(ResourceKey DstKey, ResourceKey SrcKey) override
Definition: ObjectLinkingLayer.cpp:813
llvm::orc::ObjectLinkingLayer::ReturnObjectBufferFunction
std::function< void(std::unique_ptr< MemoryBuffer >)> ReturnObjectBufferFunction
Definition: ObjectLinkingLayer.h:102
llvm::orc::EHFrameRegistrationPlugin::modifyPassConfig
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &PassConfig) override
Definition: ObjectLinkingLayer.cpp:744
STLExtras.h
llvm::orc::ObjectLinkingLayer::~ObjectLinkingLayer
~ObjectLinkingLayer()
Destruct an ObjectLinkingLayer.
Definition: ObjectLinkingLayer.cpp:636
Layer.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::orc::ObjectLinkingLayer::add
Error add(ResourceTrackerSP, std::unique_ptr< jitlink::LinkGraph > G)
Add a LinkGraph to the JITDylib targeted by the given tracker.
llvm::orc::ObjectLinkingLayer::Plugin::SyntheticSymbolDependenciesMap
DenseMap< SymbolStringPtr, JITLinkSymbolSet > SyntheticSymbolDependenciesMap
Definition: ObjectLinkingLayer.h:68
llvm::orc::ObjectLinkingLayer::Plugin::modifyPassConfig
virtual void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &Config)
Definition: ObjectLinkingLayer.h:71
llvm::Lock
static sys::Mutex Lock
Definition: NVPTXUtilities.cpp:39
llvm::orc::ObjectLinkingLayer::Plugin::notifyEmitted
virtual Error notifyEmitted(MaterializationResponsibility &MR)
Definition: ObjectLinkingLayer.h:83
StringMap.h
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:65
llvm::orc::EHFrameRegistrationPlugin
Definition: ObjectLinkingLayer.h:208
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:202
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:206
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
uint64_t
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
llvm::orc::ObjectLinkingLayer::Plugin::notifyTransferringResources
virtual void notifyTransferringResources(ResourceKey DstKey, ResourceKey SrcKey)=0
llvm::orc::ObjectLinkingLayer::ObjectLinkingLayer
ObjectLinkingLayer(ExecutionSession &ES)
Construct an ObjectLinkingLayer using the ExecutorProcessControl instance's memory manager.
Definition: ObjectLinkingLayer.cpp:619
llvm::orc::ObjectLinkingLayer::Plugin::~Plugin
virtual ~Plugin()
Definition: ObjectLinkingLayer.cpp:613
llvm::orc::ResourceTrackerSP
uint8_t IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition: Core.h:47
llvm::orc::ObjectLayer::add
virtual Error add(ResourceTrackerSP RT, std::unique_ptr< MemoryBuffer > O)
Adds a MaterializationUnit representing the given IR to the given JITDylib.
Definition: Layer.cpp:164
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::codeview::CodeViewContainer::ObjectFile
@ ObjectFile
llvm::orc::ObjectLinkingLayer::Plugin::notifyRemovingResources
virtual Error notifyRemovingResources(ResourceKey K)=0
llvm::orc::EHFrameRegistrationPlugin::notifyEmitted
Error notifyEmitted(MaterializationResponsibility &MR) override
Definition: ObjectLinkingLayer.cpp:759
llvm::orc::ObjectLinkingLayer::Plugin::notifyLoaded
virtual void notifyLoaded(MaterializationResponsibility &MR)
Definition: ObjectLinkingLayer.h:82
llvm::orc::ObjectLinkingLayer
An ObjectLayer implementation built on JITLink.
Definition: ObjectLinkingLayer.h:54
llvm::orc::ResourceManager
Listens for ResourceTracker operations.
Definition: Core.h:97
llvm::orc::ObjectLinkingLayer::Plugin::getSyntheticSymbolDependencies
virtual SyntheticSymbolDependenciesMap getSyntheticSymbolDependencies(MaterializationResponsibility &MR)
Return any dependencies that synthetic symbols (e.g.
Definition: ObjectLinkingLayer.h:96
llvm::orc::ObjectLinkingLayer::ID
static char ID
Definition: ObjectLinkingLayer.h:59
llvm::orc::ObjectLinkingLayer::addPlugin
ObjectLinkingLayer & addPlugin(std::unique_ptr< Plugin > P)
Add a pass-config modifier.
Definition: ObjectLinkingLayer.h:130
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1271
llvm::orc::ObjectLinkingLayer::setAutoClaimResponsibilityForObjectSymbols
ObjectLinkingLayer & setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols)
If set, this ObjectLinkingLayer instance will claim responsibility for any symbols provided by a give...
Definition: ObjectLinkingLayer.h:181
llvm::orc::EHFrameRegistrationPlugin::notifyFailed
Error notifyFailed(MaterializationResponsibility &MR) override
Definition: ObjectLinkingLayer.cpp:782
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
Core.h
llvm::orc::ObjectLinkingLayer::setReturnObjectBuffer
void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer)
Set an object buffer return function.
Definition: ObjectLinkingLayer.h:125
llvm::orc::ObjectLinkingLayer::Plugin
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
Definition: ObjectLinkingLayer.h:64
llvm::orc::ObjectLinkingLayerJITLinkContext
Definition: ObjectLinkingLayer.cpp:129
llvm::orc::ObjectLinkingLayer::setOverrideObjectFlagsWithResponsibilityFlags
ObjectLinkingLayer & setOverrideObjectFlagsWithResponsibilityFlags(bool OverrideObjectFlags)
Instructs this ObjectLinkingLayer instance to override the symbol flags found in the AtomGraph with t...
Definition: ObjectLinkingLayer.h:164