tqchen commented on code in PR #658:
URL: https://github.com/apache/tvm-ffi/pull/658#discussion_r3537185939


##########
addons/tvm_ffi_orcjit/src/ffi/orcjit_dylib.cc:
##########
@@ -97,18 +202,75 @@ ORCJITDynamicLibraryObj::~ORCJITDynamicLibraryObj() {
 }
 
 void ORCJITDynamicLibraryObj::AddObjectFile(const String& path) {
-  // Read object file
+  // Read object file from disk into an owned MemoryBuffer.
   auto buffer_or_err = llvm::MemoryBuffer::getFile(path.c_str());
   if (!buffer_or_err) {
     TVM_FFI_THROW(IOError) << "Failed to read object file: " << path;
   }
+  AddObjectBuffer(std::move(*buffer_or_err));
+}
 
-  // AddObjectFile is not thread-safe and must not race GetFunction.
-  TVM_FFI_ORCJIT_LLVM_CALL(jit_->addObjectFile(*dylib_, 
std::move(*buffer_or_err)));
+void ORCJITDynamicLibraryObj::AddObjectBytes(const Bytes& bytes) {
+  // Copy the bytes into an owned MemoryBuffer: LLVM takes ownership and the
+  // source bytes may not outlive linking.
+  auto buffer = 
llvm::MemoryBuffer::getMemBufferCopy(llvm::StringRef(bytes.data(), 
bytes.size()),
+                                                     name_.operator 
std::string());
+  AddObjectBuffer(std::move(buffer));
+}
+
+void 
ORCJITDynamicLibraryObj::AddObjectBuffer(std::unique_ptr<llvm::MemoryBuffer> 
buffer) {
+  // Compound topology op: adding an object mutates shared linker state and 
must
+  // not race concurrent create / add / lookup / teardown on this session.
+  std::lock_guard<std::recursive_mutex> lock(session_->mutex());
+  TVM_FFI_ORCJIT_LLVM_CALL(jit_->addObjectFile(*dylib_, std::move(buffer)));
   context_symbol_refreshed_.store(false, std::memory_order_release);
 }
 
+Module ORCJITExecutionSessionObj::LoadModule(const Array<Any>& objects, const 
String& name) {

Review Comment:
   ffi::Variant<String, Bytes>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to