llvm.org GIT mirror llvm / 5fb18b5
[ORC] Pass object buffer ownership back in NotifyEmitted. Clients who want to regain ownership of object buffers after they have been linked may now use the NotifyEmitted callback for this purpose. Note: Currently NotifyEmitted is only called if linking succeeds. If linking fails the buffer is always discarded. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359735 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 1 year, 5 months ago
2 changed file(s) with 37 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
4242 const RuntimeDyld::LoadedObjectInfo &)>;
4343
4444 /// Functor for receiving finalization notifications.
45 using NotifyEmittedFunction = std::function;
45 using NotifyEmittedFunction =
46 std::function)>;
4647
4748 using GetMemoryManagerFunction =
4849 std::function()>;
4950
5051 /// Construct an ObjectLinkingLayer with the given NotifyLoaded,
5152 /// and NotifyEmitted functors.
52 RTDyldObjectLinkingLayer(
53 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
54 NotifyLoadedFunction NotifyLoaded = NotifyLoadedFunction(),
55 NotifyEmittedFunction NotifyEmitted = NotifyEmittedFunction());
53 RTDyldObjectLinkingLayer(ExecutionSession &ES,
54 GetMemoryManagerFunction GetMemoryManager);
5655
5756 /// Emit the object.
5857 void emit(MaterializationResponsibility R,
5958 std::unique_ptr O) override;
59
60 /// Set the NotifyLoaded callback.
61 RTDyldObjectLinkingLayer &setNotifyLoaded(NotifyLoadedFunction NotifyLoaded) {
62 this->NotifyLoaded = std::move(NotifyLoaded);
63 return *this;
64 }
65
66 /// Set the NotifyEmitted callback.
67 RTDyldObjectLinkingLayer &
68 setNotifyEmitted(NotifyEmittedFunction NotifyEmitted) {
69 this->NotifyEmitted = std::move(NotifyEmitted);
70 return *this;
71 }
6072
6173 /// Set the 'ProcessAllSections' flag.
6274 ///
107119 std::map Resolved,
108120 std::set &InternalSymbols);
109121
110 void onObjEmit(VModuleKey K, MaterializationResponsibility &R, Error Err);
122 void onObjEmit(VModuleKey K, std::unique_ptr ObjBuffer,
123 MaterializationResponsibility &R, Error Err);
111124
112125 mutable std::mutex RTDyldLayerMutex;
113126 GetMemoryManagerFunction GetMemoryManager;
7676 namespace orc {
7777
7878 RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(
79 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager,
80 NotifyLoadedFunction NotifyLoaded, NotifyEmittedFunction NotifyEmitted)
81 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager),
82 NotifyLoaded(std::move(NotifyLoaded)),
83 NotifyEmitted(std::move(NotifyEmitted)) {}
79 ExecutionSession &ES, GetMemoryManagerFunction GetMemoryManager)
80 : ObjectLayer(ES), GetMemoryManager(GetMemoryManager) {}
8481
8582 void RTDyldObjectLinkingLayer::emit(MaterializationResponsibility R,
8683 std::unique_ptr O) {
9491
9592 auto &ES = getExecutionSession();
9693
97 auto Obj = object::ObjectFile::createObjectFile(*O);
94 // Create a MemoryBufferRef backed MemoryBuffer (i.e. shallow) copy of the
95 // the underlying buffer to pass into RuntimeDyld. This allows us to hold
96 // ownership of the real underlying buffer and return it to the user once
97 // the object has been emitted.
98 auto ObjBuffer = MemoryBuffer::getMemBuffer(O->getMemBufferRef(), false);
99
100 auto Obj = object::ObjectFile::createObjectFile(*ObjBuffer);
98101
99102 if (!Obj) {
100103 getExecutionSession().reportError(Obj.takeError());
132135
133136 JITDylibSearchOrderResolver Resolver(*SharedR);
134137
135 /* Thoughts on proper cross-dylib weak symbol handling:
136 *
137 * Change selection of canonical defs to be a manually triggered process, and
138 * add a 'canonical' bit to symbol definitions. When canonical def selection
139 * is triggered, sweep the JITDylibs to mark defs as canonical, discard
140 * duplicate defs.
141 */
138 // FIXME: Switch to move-capture for the 'O' buffer once we have c++14.
139 MemoryBuffer *UnownedObjBuffer = O.release();
142140 jitLinkForORC(
143141 **Obj, std::move(O), *MemMgr, Resolver, ProcessAllSections,
144142 [this, K, SharedR, &Obj, InternalSymbols](
147145 return onObjLoad(K, *SharedR, **Obj, std::move(LoadedObjInfo),
148146 ResolvedSymbols, *InternalSymbols);
149147 },
150 [this, K, SharedR](Error Err) {
151 onObjEmit(K, *SharedR, std::move(Err));
148 [this, K, SharedR, UnownedObjBuffer](Error Err) {
149 std::unique_ptr ObjBuffer(UnownedObjBuffer);
150 onObjEmit(K, std::move(ObjBuffer), *SharedR, std::move(Err));
152151 });
153152 }
154153
195194 return Error::success();
196195 }
197196
198 void RTDyldObjectLinkingLayer::onObjEmit(VModuleKey K,
199 MaterializationResponsibility &R,
200 Error Err) {
197 void RTDyldObjectLinkingLayer::onObjEmit(
198 VModuleKey K, std::unique_ptr ObjBuffer,
199 MaterializationResponsibility &R, Error Err) {
201200 if (Err) {
202201 getExecutionSession().reportError(std::move(Err));
203202 R.failMaterialization();
207206 R.emit();
208207
209208 if (NotifyEmitted)
210 NotifyEmitted(K);
209 NotifyEmitted(K, std::move(ObjBuffer));
211210 }
212211
213212 } // End namespace orc.