[Binary] Make the OffloadingImage type own the memory
authorJoseph Huber <jhuber6@vols.utk.edu>
Mon, 6 Jun 2022 17:08:01 +0000 (13:08 -0400)
committerJoseph Huber <jhuber6@vols.utk.edu>
Tue, 7 Jun 2022 19:56:09 +0000 (15:56 -0400)
Summary:
The OffloadingBinary uses a convenience struct to help manage the memory
that will be serialized using the binary format. This currently uses a
reference to an existing buffer, but this should own the memory instead
so it is easier to work with seeing as its only current use requires
saving the buffer anyway.

clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
llvm/include/llvm/Object/OffloadBinary.h
llvm/lib/Object/OffloadBinary.cpp
llvm/unittests/Object/OffloadingTest.cpp

index eb348d0..7ef2583 100644 (file)
@@ -89,8 +89,7 @@ int main(int argc, const char **argv) {
             llvm::MemoryBuffer::getFileOrSTDIN(KeyAndValue.getValue());
         if (std::error_code EC = ObjectOrErr.getError())
           return reportError(errorCodeToError(EC));
-        DeviceImage = std::move(*ObjectOrErr);
-        ImageBinary.Image = *DeviceImage;
+        ImageBinary.Image = std::move(*ObjectOrErr);
         ImageBinary.TheImageKind = getImageKind(
             sys::path::extension(KeyAndValue.getValue()).drop_front());
       } else if (Key == "kind") {
index 3a8c981..3ff82ec 100644 (file)
@@ -65,7 +65,7 @@ public:
     OffloadKind TheOffloadKind;
     uint32_t Flags;
     StringMap<StringRef> StringData;
-    MemoryBufferRef Image;
+    std::unique_ptr<MemoryBuffer> Image;
   };
 
   /// Attempt to parse the offloading binary stored in \p Data.
index fe5cc09..ae01efd 100644 (file)
@@ -58,7 +58,7 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
   // header so this can be placed contiguously in a single section.
   Header TheHeader;
   TheHeader.Size = alignTo(
-      BinaryDataSize + OffloadingData.Image.getBufferSize(), getAlignment());
+      BinaryDataSize + OffloadingData.Image->getBufferSize(), getAlignment());
   TheHeader.EntryOffset = sizeof(Header);
   TheHeader.EntrySize = sizeof(Entry);
 
@@ -72,7 +72,7 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
   TheEntry.NumStrings = OffloadingData.StringData.size();
 
   TheEntry.ImageOffset = BinaryDataSize;
-  TheEntry.ImageSize = OffloadingData.Image.getBufferSize();
+  TheEntry.ImageSize = OffloadingData.Image->getBufferSize();
 
   SmallVector<char, 1024> Data;
   raw_svector_ostream OS(Data);
@@ -87,7 +87,7 @@ OffloadBinary::write(const OffloadingImage &OffloadingData) {
   StrTab.write(OS);
   // Add padding to required image alignment.
   OS.write_zeros(TheEntry.ImageOffset - OS.tell());
-  OS << OffloadingData.Image.getBuffer();
+  OS << OffloadingData.Image->getBuffer();
 
   // Add final padding to required alignment.
   assert(TheHeader.Size >= OS.tell() && "Too much data written?");
index 97c89ec..b1be3bd 100644 (file)
@@ -41,7 +41,7 @@ TEST(OffloadingTest, checkOffloadingBinary) {
   Data.TheOffloadKind = static_cast<OffloadKind>(KindDist(Rng));
   Data.Flags = KindDist(Rng);
   Data.StringData = StringData;
-  Data.Image = *ImageData;
+  Data.Image = std::move(ImageData);
 
   auto BinaryBuffer = OffloadBinary::write(Data);
 
@@ -59,7 +59,7 @@ TEST(OffloadingTest, checkOffloadingBinary) {
     ASSERT_TRUE(StringData[KeyAndValue.first] ==
                 Binary.getString(KeyAndValue.first));
 
-  EXPECT_TRUE(Data.Image.getBuffer() == Binary.getImage());
+  EXPECT_TRUE(Data.Image->getBuffer() == Binary.getImage());
 
   // Ensure the size and alignment of the data is correct.
   EXPECT_TRUE(Binary.getSize() % OffloadBinary::getAlignment() == 0);