LLVM  16.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 orc {
42 
43 class ObjectLinkingLayerJITLinkContext;
44 
45 /// An ObjectLayer implementation built on JITLink.
46 ///
47 /// Clients can use this class to add relocatable object files to an
48 /// ExecutionSession, and it typically serves as the base layer (underneath
49 /// a compiling layer like IRCompileLayer) for the rest of the JIT.
50 class ObjectLinkingLayer : public RTTIExtends<ObjectLinkingLayer, ObjectLayer>,
51  private ResourceManager {
53 
54 public:
55  static char ID;
56 
57  /// Plugin instances can be added to the ObjectLinkingLayer to receive
58  /// callbacks when code is loaded or emitted, and when JITLink is being
59  /// configured.
60  class Plugin {
61  public:
65 
66  virtual ~Plugin();
69  jitlink::PassConfiguration &Config) {}
70 
71  // Deprecated. Don't use this in new code. There will be a proper mechanism
72  // for capturing object buffers.
76  MemoryBufferRef InputObject) {}
77 
80  return Error::success();
81  }
84  virtual void notifyTransferringResources(ResourceKey DstKey,
85  ResourceKey SrcKey) = 0;
86 
87  /// Return any dependencies that synthetic symbols (e.g. init symbols)
88  /// have on symbols in the LinkGraph.
89  /// This is used by the ObjectLinkingLayer to update the dependencies for
90  /// the synthetic symbols.
94  }
95  };
96 
98  std::function<void(std::unique_ptr<MemoryBuffer>)>;
99 
100  /// Construct an ObjectLinkingLayer using the ExecutorProcessControl
101  /// instance's memory manager.
103 
104  /// Construct an ObjectLinkingLayer using a custom memory manager.
107 
108  /// Construct an ObjectLinkingLayer. Takes ownership of the given
109  /// JITLinkMemoryManager. This method is a temporary hack to simplify
110  /// co-existence with RTDyldObjectLinkingLayer (which also owns its
111  /// allocators).
113  std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
114 
115  /// Destruct an ObjectLinkingLayer.
117 
118  /// Set an object buffer return function. By default object buffers are
119  /// deleted once the JIT has linked them. If a return function is set then
120  /// it will be called to transfer ownership of the buffer instead.
122  this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
123  }
124 
125  /// Add a pass-config modifier.
126  ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
127  std::lock_guard<std::mutex> Lock(LayerMutex);
128  Plugins.push_back(std::move(P));
129  return *this;
130  }
131 
132  /// Add a LinkGraph to the JITDylib targeted by the given tracker.
133  Error add(ResourceTrackerSP, std::unique_ptr<jitlink::LinkGraph> G);
134 
135  /// Add a LinkGraph to the given JITDylib.
136  Error add(JITDylib &JD, std::unique_ptr<jitlink::LinkGraph> G) {
137  return add(JD.getDefaultResourceTracker(), std::move(G));
138  }
139 
140  // Un-hide ObjectLayer add methods.
141  using ObjectLayer::add;
142 
143  /// Emit an object file.
144  void emit(std::unique_ptr<MaterializationResponsibility> R,
145  std::unique_ptr<MemoryBuffer> O) override;
146 
147  /// Emit a LinkGraph.
148  void emit(std::unique_ptr<MaterializationResponsibility> R,
149  std::unique_ptr<jitlink::LinkGraph> G);
150 
151  /// Instructs this ObjectLinkingLayer instance to override the symbol flags
152  /// found in the AtomGraph with the flags supplied by the
153  /// MaterializationResponsibility instance. This is a workaround to support
154  /// symbol visibility in COFF, which does not use the libObject's
155  /// SF_Exported flag. Use only when generating / adding COFF object files.
156  ///
157  /// FIXME: We should be able to remove this if/when COFF properly tracks
158  /// exported symbols.
161  this->OverrideObjectFlags = OverrideObjectFlags;
162  return *this;
163  }
164 
165  /// If set, this ObjectLinkingLayer instance will claim responsibility
166  /// for any symbols provided by a given object file that were not already in
167  /// the MaterializationResponsibility instance. Setting this flag allows
168  /// higher-level program representations (e.g. LLVM IR) to be added based on
169  /// only a subset of the symbols they provide, without having to write
170  /// intervening layers to scan and add the additional symbols. This trades
171  /// diagnostic quality for convenience however: If all symbols are enumerated
172  /// up-front then clashes can be detected and reported early (and usually
173  /// deterministically). If this option is set, clashes for the additional
174  /// symbols may not be detected until late, and detection may depend on
175  /// the flow of control through JIT'd code. Use with care.
177  setAutoClaimResponsibilityForObjectSymbols(bool AutoClaimObjectSymbols) {
178  this->AutoClaimObjectSymbols = AutoClaimObjectSymbols;
179  return *this;
180  }
181 
182 private:
183  using FinalizedAlloc = jitlink::JITLinkMemoryManager::FinalizedAlloc;
184 
185  void modifyPassConfig(MaterializationResponsibility &MR,
187  jitlink::PassConfiguration &PassConfig);
188  void notifyLoaded(MaterializationResponsibility &MR);
189  Error notifyEmitted(MaterializationResponsibility &MR, FinalizedAlloc FA);
190 
191  Error handleRemoveResources(ResourceKey K) override;
192  void handleTransferResources(ResourceKey DstKey, ResourceKey SrcKey) override;
193 
194  mutable std::mutex LayerMutex;
196  std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgrOwnership;
197  bool OverrideObjectFlags = false;
198  bool AutoClaimObjectSymbols = false;
199  ReturnObjectBufferFunction ReturnObjectBuffer;
201  std::vector<std::unique_ptr<Plugin>> Plugins;
202 };
203 
205 public:
207  ExecutionSession &ES,
208  std::unique_ptr<jitlink::EHFrameRegistrar> Registrar);
211  jitlink::PassConfiguration &PassConfig) override;
216  ResourceKey SrcKey) override;
217 
218 private:
219  std::mutex EHFramePluginMutex;
220  ExecutionSession &ES;
221  std::unique_ptr<jitlink::EHFrameRegistrar> Registrar;
224 };
225 
226 } // end namespace orc
227 } // end namespace llvm
228 
229 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTLINKINGLAYER_H
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:658
llvm::orc::MaterializationResponsibility
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:519
llvm::orc::ObjectLinkingLayer::add
Error add(JITDylib &JD, std::unique_ptr< jitlink::LinkGraph > G)
Add a LinkGraph to the given JITDylib.
Definition: ObjectLinkingLayer.h:136
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::orc::JITDylib
Represents a JIT'd dynamic library.
Definition: Core.h:953
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:750
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:73
llvm::orc::JITDylib::getDefaultResourceTracker
ResourceTrackerSP getDefaultResourceTracker()
Get the default resource tracker for this JITDylib.
Definition: Core.cpp:660
llvm::orc::ObjectLayer::add
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:163
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
Error.h
llvm::orc::EHFrameRegistrationPlugin::notifyRemovingResources
Error notifyRemovingResources(ResourceKey K) override
Definition: ObjectLinkingLayer.cpp:799
llvm::orc::EHFrameRegistrationPlugin::notifyTransferringResources
void notifyTransferringResources(ResourceKey DstKey, ResourceKey SrcKey) override
Definition: ObjectLinkingLayer.cpp:822
llvm::orc::ObjectLinkingLayer::ReturnObjectBufferFunction
std::function< void(std::unique_ptr< MemoryBuffer >)> ReturnObjectBufferFunction
Definition: ObjectLinkingLayer.h:98
llvm::orc::EHFrameRegistrationPlugin::modifyPassConfig
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &PassConfig) override
Definition: ObjectLinkingLayer.cpp:754
STLExtras.h
llvm::orc::ObjectLinkingLayer::~ObjectLinkingLayer
~ObjectLinkingLayer()
Destruct an ObjectLinkingLayer.
Definition: ObjectLinkingLayer.cpp:646
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:64
llvm::orc::ObjectLinkingLayer::Plugin::modifyPassConfig
virtual void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &Config)
Definition: ObjectLinkingLayer.h:67
llvm::orc::ObjectLinkingLayer::Plugin::notifyEmitted
virtual Error notifyEmitted(MaterializationResponsibility &MR)
Definition: ObjectLinkingLayer.h:79
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:114
llvm::orc::EHFrameRegistrationPlugin
Definition: ObjectLinkingLayer.h:204
G
const DataFlowGraph & G
Definition: RDFGraph.cpp:200
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
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:629
llvm::orc::ObjectLinkingLayer::Plugin::~Plugin
virtual ~Plugin()
llvm::orc::ResourceTrackerSP
uint8_t IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition: Core.h:47
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::orc::ObjectLinkingLayer::Plugin::notifyRemovingResources
virtual Error notifyRemovingResources(ResourceKey K)=0
llvm::orc::EHFrameRegistrationPlugin::notifyEmitted
Error notifyEmitted(MaterializationResponsibility &MR) override
Definition: ObjectLinkingLayer.cpp:769
llvm::orc::ObjectLinkingLayer::Plugin::notifyLoaded
virtual void notifyLoaded(MaterializationResponsibility &MR)
Definition: ObjectLinkingLayer.h:78
llvm::orc::ObjectLinkingLayer
An ObjectLayer implementation built on JITLink.
Definition: ObjectLinkingLayer.h:50
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:92
llvm::orc::ObjectLinkingLayer::ID
static char ID
Definition: ObjectLinkingLayer.h:55
llvm::orc::ObjectLinkingLayer::addPlugin
ObjectLinkingLayer & addPlugin(std::unique_ptr< Plugin > P)
Add a pass-config modifier.
Definition: ObjectLinkingLayer.h:126
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::orc::ExecutionSession
An ExecutionSession represents a running JIT program.
Definition: Core.h:1365
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:177
llvm::orc::EHFrameRegistrationPlugin::notifyFailed
Error notifyFailed(MaterializationResponsibility &MR) override
Definition: ObjectLinkingLayer.cpp:792
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:83
Core.h
llvm::orc::ObjectLinkingLayer::setReturnObjectBuffer
void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer)
Set an object buffer return function.
Definition: ObjectLinkingLayer.h:121
llvm::orc::ObjectLinkingLayer::Plugin
Plugin instances can be added to the ObjectLinkingLayer to receive callbacks when code is loaded or e...
Definition: ObjectLinkingLayer.h:60
llvm::orc::ObjectLinkingLayerJITLinkContext
Definition: ObjectLinkingLayer.cpp:124
StringMap.h
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:160