Skip to content

Commit a98546e

Browse files
committed
[Orc] Add a method for ObjectLinkingLayer to return ownership of object buffers.
RTDyldObjectLinkingLayer allowed clients to register a NotifyEmitted function to reclaim ownership of object buffers once they had been linked. This patch adds similar functionality to ObjectLinkingLayer: Clients can now optionally call the ObjectLinkingLayer::setReturnObjectBuffer method to register a function that will be called when discarding object buffers. If set, this function will be called to return ownership of the object regardless of whether the link succeeded or failed. Use cases for this function include debug dumping (it provides a way to dump all objects linked into JIT'd code) and object re-use (e.g. storing an object in a cache). llvm-svn: 374951
1 parent 75b991e commit a98546e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

llvm/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class ObjectLinkingLayer : public ObjectLayer {
7373
virtual Error notifyRemovingAllModules() { return Error::success(); }
7474
};
7575

76+
using ReturnObjectBufferFunction =
77+
std::function<void(std::unique_ptr<MemoryBuffer>)>;
78+
7679
/// Construct an ObjectLinkingLayer with the given NotifyLoaded,
7780
/// and NotifyEmitted functors.
7881
ObjectLinkingLayer(ExecutionSession &ES,
@@ -81,6 +84,13 @@ class ObjectLinkingLayer : public ObjectLayer {
8184
/// Destruct an ObjectLinkingLayer.
8285
~ObjectLinkingLayer();
8386

87+
/// Set an object buffer return function. By default object buffers are
88+
/// deleted once the JIT has linked them. If a return function is set then
89+
/// it will be called to transfer ownership of the buffer instead.
90+
void setReturnObjectBuffer(ReturnObjectBufferFunction ReturnObjectBuffer) {
91+
this->ReturnObjectBuffer = std::move(ReturnObjectBuffer);
92+
}
93+
8494
/// Add a pass-config modifier.
8595
ObjectLinkingLayer &addPlugin(std::unique_ptr<Plugin> P) {
8696
std::lock_guard<std::mutex> Lock(LayerMutex);
@@ -138,6 +148,7 @@ class ObjectLinkingLayer : public ObjectLayer {
138148
jitlink::JITLinkMemoryManager &MemMgr;
139149
bool OverrideObjectFlags = false;
140150
bool AutoClaimObjectSymbols = false;
151+
ReturnObjectBufferFunction ReturnObjectBuffer;
141152
DenseMap<VModuleKey, AllocPtr> TrackedAllocs;
142153
std::vector<AllocPtr> UntrackedAllocs;
143154
std::vector<std::unique_ptr<Plugin>> Plugins;

llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class ObjectLinkingLayerJITLinkContext final : public JITLinkContext {
2929
std::unique_ptr<MemoryBuffer> ObjBuffer)
3030
: Layer(Layer), MR(std::move(MR)), ObjBuffer(std::move(ObjBuffer)) {}
3131

32+
~ObjectLinkingLayerJITLinkContext() {
33+
// If there is an object buffer return function then use it to
34+
// return ownership of the buffer.
35+
if (Layer.ReturnObjectBuffer)
36+
Layer.ReturnObjectBuffer(std::move(ObjBuffer));
37+
}
38+
3239
JITLinkMemoryManager &getMemoryManager() override { return Layer.MemMgr; }
3340

3441
MemoryBufferRef getObjectBuffer() const override {

0 commit comments

Comments
 (0)