================
@@ -161,28 +183,36 @@ class OffloadFile : public OwningBinary<OffloadBinary> {
   using TargetID = std::pair<StringRef, StringRef>;
 
   OffloadFile(std::unique_ptr<OffloadBinary> Binary,
-              std::unique_ptr<MemoryBuffer> Buffer)
-      : OwningBinary<OffloadBinary>(std::move(Binary), std::move(Buffer)) {}
+              std::shared_ptr<MemoryBuffer> SharedBuffer)
+      : OwningBinary<OffloadBinary>(std::move(Binary), nullptr),
+        SharedBuffer(std::move(SharedBuffer)) {}
 
-  /// Make a deep copy of this offloading file.
-  OffloadFile copy() const {
-    std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBufferCopy(
-        getBinary()->getMemoryBufferRef().getBuffer(),
-        getBinary()->getMemoryBufferRef().getBufferIdentifier());
+  /// Create a new OffloadFile with a new Binary but reuse SharedBuffer from
+  /// another OffloadFile.
+  OffloadFile(std::unique_ptr<OffloadBinary> Binary,
+              const OffloadFile &Other)
+      : OwningBinary<OffloadBinary>(std::move(Binary), nullptr),
+        SharedBuffer(Other.SharedBuffer) {}
 
+  /// Make a deep copy of this offloading file.
+  OffloadFile copy(uint64_t Index = 0) const {
     // This parsing should never fail because it has already been parsed.
-    auto NewBinaryOrErr = OffloadBinary::create(*Buffer);
+    auto NewBinaryOrErr = 
OffloadBinary::create(MemoryBufferRef(*SharedBuffer), Index);
     assert(NewBinaryOrErr && "Failed to parse a copy of the binary?");
     if (!NewBinaryOrErr)
       llvm::consumeError(NewBinaryOrErr.takeError());
-    return OffloadFile(std::move(*NewBinaryOrErr), std::move(Buffer));
+    return OffloadFile(std::move(*NewBinaryOrErr), SharedBuffer);
   }
 
   /// We use the Triple and Architecture pair to group linker inputs together.
   /// This conversion function lets us use these inputs in a hash-map.
   operator TargetID() const {
     return std::make_pair(getBinary()->getTriple(), getBinary()->getArch());
   }
+
+private:
+  /// Shared buffer for binaries with multiple entries
+  std::shared_ptr<MemoryBuffer> SharedBuffer;
----------------
jhuber6 wrote:

I don't actually remember why I made them owning here, I think it was just a 
much simpler interface since we pass them around all over the place. My 
preference would be to have the non-owning version keep a reference. Basically 
each OffloadBinary contains a reference to the header and the entries. One 
serialized binary which contains 'N' entries under a single header will create 
`N` separate OffloadBinary objects that share the same memory buffer. If we 
then want to copy that to an owning binary, we need to copy both the entry data 
and the header.

https://github.com/llvm/llvm-project/pull/169425
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to