Skip to content

Commit

Permalink
[Binary] Make the OffloadingImage type own the memory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhuber6 committed Jun 7, 2022
1 parent 9db2f32 commit f06731e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
3 changes: 1 addition & 2 deletions clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Object/OffloadBinary.h
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Object/OffloadBinary.cpp
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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?");
Expand Down
4 changes: 2 additions & 2 deletions llvm/unittests/Object/OffloadingTest.cpp
Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit f06731e

Please sign in to comment.