[clang] f06731e - [Binary] Make the OffloadingImage type own the memory

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 12:56:51 PDT 2022


Author: Joseph Huber
Date: 2022-06-07T15:56:09-04:00
New Revision: f06731e3c367c481acc8bc6b1f8244039992fa65

URL: https://github.com/llvm/llvm-project/commit/f06731e3c367c481acc8bc6b1f8244039992fa65
DIFF: https://github.com/llvm/llvm-project/commit/f06731e3c367c481acc8bc6b1f8244039992fa65.diff

LOG: [Binary] Make the OffloadingImage type own the memory

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.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp b/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
index eb348d05e3bb0..7ef258396e989 100644
--- a/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
+++ b/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
@@ -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") {

diff  --git a/llvm/include/llvm/Object/OffloadBinary.h b/llvm/include/llvm/Object/OffloadBinary.h
index 3a8c98105c93b..3ff82ec86aa8c 100644
--- a/llvm/include/llvm/Object/OffloadBinary.h
+++ b/llvm/include/llvm/Object/OffloadBinary.h
@@ -65,7 +65,7 @@ class OffloadBinary : public Binary {
     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.

diff  --git a/llvm/lib/Object/OffloadBinary.cpp b/llvm/lib/Object/OffloadBinary.cpp
index fe5cc093d7874..ae01efdb7f1f3 100644
--- a/llvm/lib/Object/OffloadBinary.cpp
+++ b/llvm/lib/Object/OffloadBinary.cpp
@@ -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?");

diff  --git a/llvm/unittests/Object/OffloadingTest.cpp b/llvm/unittests/Object/OffloadingTest.cpp
index 97c89ec450c3c..b1be3bd97e1a7 100644
--- a/llvm/unittests/Object/OffloadingTest.cpp
+++ b/llvm/unittests/Object/OffloadingTest.cpp
@@ -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);


        


More information about the cfe-commits mailing list