This PR updates the OffloadBinary format from version 1 to version 2,
enabling support for multiple offloading entries in a single binary.
This allows combining multiple device images into a single binary with
common global metadata while maintaining backwards compatibility with
version 1 binaries.
# Key Changes
## Binary Format Enhancements
**Version 2 Format Changes:**
- Changed from single-entry to multi-entry design
- Updated `Header` structure:
- Renamed `EntryOffset` → `EntriesOffset` (offset to entries array)
- Renamed `EntrySize` → `EntriesCount` (number of entries)
- Added `StringEntry::ValueSize` field to support explicit string value
sizes (enables non-null-terminated strings)
- Introduced `OffloadEntryFlags` enum with `OIF_Metadata` flag for
metadata-only entries (entries without binary images)
**API Changes:**
- `OffloadBinary::create()` now returns
`Expected<SmallVector<std::unique_ptr<OffloadBinary>>>` instead of
single binary
- Added optional `Index` parameter to extract specific entry:
`create(Buffer, std::optional<uint64_t> Index)`
- `OffloadBinary::write()` now accepts `ArrayRef<OffloadingImage>`
instead of single image
- Added `OffloadBinary::extractHeader()` for header extraction
**Memory Management:**
- Implemented `SharedMemoryBuffer` class to enable memory sharing across
multiple `OffloadBinary` instances from the same file
- Multiple entries from a single serialized binary share the underlying
buffer
## Testing
**Unit Tests (`unittests/Object/OffloadingTest.cpp`):**
- `checkMultiEntryBinaryExtraction`: Tests extracting all entries from a
multi-entry binary
- `checkIndexBasedExtraction`: Tests extracting specific entries by
index, including out-of-bounds validation
- `checkEdgeCases`: Tests edge cases including:
- Empty string metadata
- Empty image data
- Large string values (4KB)
**Other Tests:**
- Updated `test/ObjectYAML/Offload/multiple_members.yaml` to include
metadata-only entry
---------
Co-authored-by: Joseph Huber <huberjn@outlook.com>
83 lines
2.6 KiB
C++
83 lines
2.6 KiB
C++
//===------ offload2yaml.cpp - obj2yaml conversion tool ---*- C++ -------*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "obj2yaml.h"
|
|
#include "llvm/BinaryFormat/Magic.h"
|
|
#include "llvm/Object/OffloadBinary.h"
|
|
#include "llvm/ObjectYAML/OffloadYAML.h"
|
|
#include "llvm/Support/StringSaver.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
void populateYAML(OffloadYAML::Binary &YAMLBinary,
|
|
ArrayRef<std::unique_ptr<object::OffloadBinary>> OBinaries,
|
|
UniqueStringSaver Saver) {
|
|
for (const auto &OBinaryPtr : OBinaries) {
|
|
object::OffloadBinary &OB = *OBinaryPtr;
|
|
|
|
YAMLBinary.Members.emplace_back();
|
|
auto &Member = YAMLBinary.Members.back();
|
|
Member.ImageKind = OB.getImageKind();
|
|
Member.OffloadKind = OB.getOffloadKind();
|
|
Member.Flags = OB.getFlags();
|
|
if (!OB.strings().empty()) {
|
|
Member.StringEntries = std::vector<OffloadYAML::Binary::StringEntry>();
|
|
for (const auto &StringEntry : OB.strings())
|
|
Member.StringEntries->emplace_back(OffloadYAML::Binary::StringEntry(
|
|
{Saver.save(StringEntry.first), Saver.save(StringEntry.second)}));
|
|
}
|
|
|
|
if (!OB.getImage().empty())
|
|
Member.Content = arrayRefFromStringRef(OB.getImage());
|
|
}
|
|
}
|
|
|
|
Expected<OffloadYAML::Binary *> dump(MemoryBufferRef Source,
|
|
UniqueStringSaver Saver) {
|
|
std::unique_ptr<OffloadYAML::Binary> YAMLBinary =
|
|
std::make_unique<OffloadYAML::Binary>();
|
|
|
|
YAMLBinary->Members = std::vector<OffloadYAML::Binary::Member>();
|
|
|
|
uint64_t Offset = 0;
|
|
while (Offset < Source.getBufferSize()) {
|
|
MemoryBufferRef Buffer = MemoryBufferRef(
|
|
Source.getBuffer().drop_front(Offset), Source.getBufferIdentifier());
|
|
auto BinariesOrErr = object::OffloadBinary::create(Buffer);
|
|
if (!BinariesOrErr)
|
|
return BinariesOrErr.takeError();
|
|
|
|
SmallVector<std::unique_ptr<object::OffloadBinary>> &Binaries =
|
|
*BinariesOrErr;
|
|
populateYAML(*YAMLBinary, Binaries, Saver);
|
|
|
|
Offset += Binaries[0]->getSize();
|
|
}
|
|
|
|
return YAMLBinary.release();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Error offload2yaml(raw_ostream &Out, MemoryBufferRef Source) {
|
|
BumpPtrAllocator Alloc;
|
|
UniqueStringSaver Saver(Alloc);
|
|
|
|
Expected<OffloadYAML::Binary *> YAMLOrErr = dump(Source, Saver);
|
|
if (!YAMLOrErr)
|
|
return YAMLOrErr.takeError();
|
|
|
|
std::unique_ptr<OffloadYAML::Binary> YAML(YAMLOrErr.get());
|
|
yaml::Output Yout(Out);
|
|
Yout << *YAML;
|
|
|
|
return Error::success();
|
|
}
|