Files
llvm-project/llvm/lib/ObjectYAML/OffloadYAML.cpp
Yury Plyakhin 4d27530c69 [Offloading] Offload Binary Format V2: Support Multiple Entries (#169425)
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>
2026-02-05 07:46:57 -08:00

80 lines
2.4 KiB
C++

//===- OffloadYAML.cpp - Offload Binary YAMLIO implementation -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines classes for handling the YAML representation of offload
// binaries.
//
//===----------------------------------------------------------------------===//
#include <llvm/ObjectYAML/OffloadYAML.h>
namespace llvm {
namespace yaml {
void ScalarEnumerationTraits<object::ImageKind>::enumeration(
IO &IO, object::ImageKind &Value) {
#define ECase(X) IO.enumCase(Value, #X, object::X)
ECase(IMG_None);
ECase(IMG_Object);
ECase(IMG_Bitcode);
ECase(IMG_Cubin);
ECase(IMG_Fatbinary);
ECase(IMG_PTX);
ECase(IMG_LAST);
#undef ECase
IO.enumFallback<Hex16>(Value);
}
void ScalarEnumerationTraits<object::OffloadKind>::enumeration(
IO &IO, object::OffloadKind &Value) {
#define ECase(X) IO.enumCase(Value, #X, object::X)
ECase(OFK_None);
ECase(OFK_OpenMP);
ECase(OFK_Cuda);
ECase(OFK_HIP);
ECase(OFK_SYCL);
ECase(OFK_LAST);
#undef ECase
IO.enumFallback<Hex16>(Value);
}
void MappingTraits<OffloadYAML::Binary>::mapping(IO &IO,
OffloadYAML::Binary &O) {
assert(!IO.getContext() && "The IO context is initialized already");
IO.setContext(&O);
IO.mapTag("!Offload", true);
IO.mapOptional("Version", O.Version);
IO.mapOptional("Size", O.Size);
IO.mapOptional("EntriesOffset", O.EntriesOffset);
IO.mapOptional("EntriesCount", O.EntriesCount);
IO.mapRequired("Members", O.Members);
IO.setContext(nullptr);
}
void MappingTraits<OffloadYAML::Binary::StringEntry>::mapping(
IO &IO, OffloadYAML::Binary::StringEntry &SE) {
assert(IO.getContext() && "The IO context is not initialized");
IO.mapRequired("Key", SE.Key);
IO.mapRequired("Value", SE.Value);
}
void MappingTraits<OffloadYAML::Binary::Member>::mapping(
IO &IO, OffloadYAML::Binary::Member &M) {
assert(IO.getContext() && "The IO context is not initialized");
IO.mapOptional("ImageKind", M.ImageKind);
IO.mapOptional("OffloadKind", M.OffloadKind);
IO.mapOptional("Flags", M.Flags);
IO.mapOptional("String", M.StringEntries);
IO.mapOptional("Content", M.Content);
}
} // namespace yaml
} // namespace llvm