diff --git a/llvm/include/llvm-c/Orc.h b/llvm/include/llvm-c/Orc.h index 560b4adc9d86b..11c846526d002 100644 --- a/llvm/include/llvm-c/Orc.h +++ b/llvm/include/llvm-c/Orc.h @@ -97,6 +97,11 @@ typedef struct LLVMOrcOpaqueLLJITBuilder *LLVMOrcLLJITBuilderRef; */ typedef struct LLVMOrcOpaqueLLJIT *LLVMOrcLLJITRef; +/** + * A reference to an orc::ResourceTracker instance. + */ +typedef struct LLVMOrcOpaqueResourceTracker *LLVMOrcResourceTrackerRef; + /** * Attach a custom error reporter function to the ExecutionSession. * @@ -300,6 +305,17 @@ LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J); */ LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J); +/** + * Return a reference to a newly created resource tracker associated + * with JD. Needs to be released with LLVMOrcResourceTrackerDispose. + */ +LLVMOrcResourceTrackerRef LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD); + +/* + * Dispose of a resource tracker reference. + */ +void LLVMOrcResourceTrackerDispose(LLVMOrcResourceTrackerRef RT); + /** * Return the target triple for this LLJIT instance. This string is owned by * the LLJIT instance and should not be freed by the client. @@ -325,20 +341,37 @@ LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName); * Add a buffer representing an object file to the given JITDylib in the given * LLJIT instance. This operation transfers ownership of the buffer to the * LLJIT instance. The buffer should not be disposed of or referenced once this - * function returns. + * function returns. RT may be null, in which case the dylib's main resource + * tracker will be used. */ LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, - LLVMMemoryBufferRef ObjBuffer); + LLVMMemoryBufferRef ObjBuffer, + LLVMOrcResourceTrackerRef RT); /** * Add an IR module to the given JITDylib of the given LLJIT instance. This * operation transfers ownership of the TSM argument to the LLJIT instance. - * The TSM argument should not be 3disposed of or referenced once this - * function returns. + * The TSM argument should not be disposed of or referenced once this + * function returns. RT may be null, in which case the dylib's main resource + * tracker will be used. */ LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, - LLVMOrcThreadSafeModuleRef TSM); + LLVMOrcThreadSafeModuleRef TSM, + LLVMOrcResourceTrackerRef RT); + +/* + * Calls remove on all trackers associated with this JITDylib, see + * JITDylib::clear(). + */ +LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD); + +/* + * Remove all resources associated with this key. See + * ResourceTracker::remove(). + */ +LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT); + /** * Look up the given symbol in the main JITDylib of the given LLJIT instance. * diff --git a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h index 46c91ecbcd782..5d150ad1cf6ce 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/LLJIT.h @@ -103,18 +103,20 @@ class LLJIT { } /// Adds an IR module to the given JITDylib. - Error addIRModule(JITDylib &JD, ThreadSafeModule TSM); + Error addIRModule(JITDylib &JD, ThreadSafeModule TSM, + ResourceTrackerSP RT = nullptr); /// Adds an IR module to the Main JITDylib. - Error addIRModule(ThreadSafeModule TSM) { + Error addIRModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) { return addIRModule(*Main, std::move(TSM)); } /// Adds an object file to the given JITDylib. - Error addObjectFile(JITDylib &JD, std::unique_ptr Obj); + Error addObjectFile(JITDylib &JD, std::unique_ptr Obj, + ResourceTrackerSP RT = nullptr); /// Adds an object file to the given JITDylib. - Error addObjectFile(std::unique_ptr Obj) { + Error addObjectFile(std::unique_ptr Obj, ResourceTrackerSP RT = nullptr) { return addObjectFile(*Main, std::move(Obj)); } diff --git a/llvm/include/llvm/ExecutionEngine/Orc/RPC/RPCUtils.h b/llvm/include/llvm/ExecutionEngine/Orc/RPC/RPCUtils.h index 306a5eeec3841..ca6f56a00c860 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/RPC/RPCUtils.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/RPC/RPCUtils.h @@ -1525,20 +1525,20 @@ class SingleThreadedRPCEndpoint Args...)) { detail::ResultTraits::consumeAbandoned( std::move(Result)); - return std::move(Err); + return Err; } if (auto Err = this->C.send()) { detail::ResultTraits::consumeAbandoned( std::move(Result)); - return std::move(Err); + return Err; } while (!ReceivedResponse) { if (auto Err = this->handleOne()) { detail::ResultTraits::consumeAbandoned( std::move(Result)); - return std::move(Err); + return Err; } } diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp index 2a1a68b58fa21..57f8b76d5fa13 100644 --- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp +++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp @@ -978,20 +978,25 @@ LLJIT::~LLJIT() { ES->reportError(ES->endSession()); } -Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) { +Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM, ResourceTrackerSP RT) { assert(TSM && "Can not add null module"); if (auto Err = TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); })) return Err; - return InitHelperTransformLayer->add(JD, std::move(TSM)); + if (!RT) + RT = JD.getDefaultResourceTracker(); + return InitHelperTransformLayer->add(RT, std::move(TSM)); } -Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr Obj) { +Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr Obj, ResourceTrackerSP RT) { assert(Obj && "Can not add null object"); - return ObjTransformLayer.add(JD, std::move(Obj)); + if (!RT) + RT = JD.getDefaultResourceTracker(); + + return ObjTransformLayer.add(RT, std::move(Obj)); } Expected LLJIT::lookupLinkerMangled(JITDylib &JD, diff --git a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp index 85a33c98a2bb8..1344659bef18d 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp +++ b/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp @@ -55,6 +55,7 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder, LLVMOrcJITTargetMachineBuilderRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder, LLVMOrcLLJITBuilderRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTrackerSP, LLVMOrcResourceTrackerRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef) @@ -150,7 +151,7 @@ LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost( } LLVMOrcJITTargetMachineBuilderRef -LLVMOrcJITTargetMachineBuilderFromTargetMachine(LLVMTargetMachineRef TM) { +LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM) { auto *TemplateTM = unwrap(TM); auto JTMB = @@ -219,6 +220,20 @@ LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J) { return wrap(&unwrap(J)->getMainJITDylib()); } +LLVMOrcResourceTrackerRef +LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD) +{ + ResourceTrackerSP *RT = new ResourceTrackerSP(unwrap(JD)->createResourceTracker()); + + return wrap(RT); +} + +void +LLVMOrcResourceTrackerDispose(LLVMOrcResourceTrackerRef RT) +{ + delete unwrap(RT); +} + const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J) { return unwrap(J)->getTargetTriple().str().c_str(); } @@ -234,15 +249,29 @@ LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName) { } LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, - LLVMMemoryBufferRef ObjBuffer) { + LLVMMemoryBufferRef ObjBuffer, + LLVMOrcResourceTrackerRef RT) { return wrap(unwrap(J)->addObjectFile( - *unwrap(JD), std::unique_ptr(unwrap(ObjBuffer)))); + *unwrap(JD), std::unique_ptr(unwrap(ObjBuffer)), + *unwrap(RT))); } LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD, - LLVMOrcThreadSafeModuleRef TSM) { - return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*unwrap(TSM)))); + LLVMOrcThreadSafeModuleRef TSM, + LLVMOrcResourceTrackerRef RT) { + return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*unwrap(TSM)), + *unwrap(RT))); +} + +LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) +{ + return wrap(unwrap(JD)->clear()); +} + +LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) +{ + return wrap((*unwrap(RT))->remove()); } LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,