================
@@ -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;
----------------
YuriPlyakhin wrote:

What's your idea on how we can share the same buffer between multiple 
OffloadFile objects?
Or do you suggest having a separate copy of the entire buffer in each 
OffloadFile object?
My understanding is that OffloadFile is an owning object for OffloadBinary.
OffloadBinary is:
- pointer to the entire original buffer with multiple entries/images
- pointer to Header
- pointer to entry represented by current OffloadBinary object
- map of references to StringData corresponding to the current OffloadBinary 
object
OffloadFile is:
- Original buffer with multiple entries/images
- OffloadBinary representing single entry/image described above.

So, question is: do we want to share the same buffer between multiple 
OffloadFile objects (since multiple OffloadFile objects would actually be 
represented by the same buffer) or copy entire buffer with multiple 
entries/images to each new OffloadFile object?


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