================
@@ -82,76 +116,79 @@ class OffloadBinary : public Binary {
LLVM_ABI static Expected<std::unique_ptr<OffloadBinary>>
create(MemoryBufferRef);
- /// Serialize the contents of \p File to a binary buffer to be read later.
- LLVM_ABI static SmallString<0> write(const OffloadingImage &);
+ /// Serialize the contents of \p OffloadingData to a binary buffer to be read
+ /// later.
+ LLVM_ABI static SmallString<0>
+ write(ArrayRef<OffloadingImage> OffloadingData);
static uint64_t getAlignment() { return 8; }
- ImageKind getImageKind() const { return TheEntry->TheImageKind; }
- OffloadKind getOffloadKind() const { return TheEntry->TheOffloadKind; }
+ ImageKind getOnlyImageKind() const {
+ assert(getEntriesCount() == 1 && "Expected exactly one entry.");
+ return Entries[0].first->TheImageKind;
+ }
+
+ OffloadKind getOffloadKind() const { return
Entries[0].first->TheOffloadKind; }
uint32_t getVersion() const { return TheHeader->Version; }
- uint32_t getFlags() const { return TheEntry->Flags; }
+ uint32_t getFlags() const { return Entries[0].first->Flags; }
uint64_t getSize() const { return TheHeader->Size; }
+ uint64_t getEntriesCount() const { return TheHeader->EntriesCount; }
StringRef getTriple() const { return getString("triple"); }
StringRef getArch() const { return getString("arch"); }
StringRef getImage() const {
- return StringRef(&Buffer[TheEntry->ImageOffset], TheEntry->ImageSize);
+ return StringRef(&Buffer[Entries[0].first->ImageOffset],
Entries[0].first->ImageSize);
}
- // Iterator over all the key and value pairs in the binary.
- string_iterator_range strings() const { return StringData; }
+ // Iterator access to all entries in the binary
+ entry_iterator_range entries() const {
+ return make_range(Entries.begin(), Entries.end());
+ }
+ entry_iterator entries_begin() const { return Entries.begin(); }
+ entry_iterator entries_end() const { return Entries.end(); }
- StringRef getString(StringRef Key) const { return StringData.lookup(Key); }
+ // Access specific entry by index.
+ const std::pair<const Entry *, StringMap> &getEntry(size_t Index) const {
+ assert(Index < Entries.size() && "Entry index out of bounds");
+ return Entries[Index];
+ }
- static bool classof(const Binary *V) { return V->isOffloadFile(); }
+ // Iterator over all the key and value pairs in the binary.
+ string_iterator_range strings() const { return Entries[0].second; }
- struct Header {
- uint8_t Magic[4] = {0x10, 0xFF, 0x10, 0xAD}; // 0x10FF10AD magic bytes.
- uint32_t Version = OffloadBinary::Version; // Version identifier.
- uint64_t Size; // Size in bytes of this entire binary.
- uint64_t EntryOffset; // Offset of the metadata entry in bytes.
- uint64_t EntrySize; // Size of the metadata entry in bytes.
- };
+ StringRef getString(StringRef Key) const { return
Entries[0].second.lookup(Key); }
- struct Entry {
- ImageKind TheImageKind; // The kind of the image stored.
- OffloadKind TheOffloadKind; // The producer of this image.
- uint32_t Flags; // Additional flags associated with the image.
- uint64_t StringOffset; // Offset in bytes to the string map.
- uint64_t NumStrings; // Number of entries in the string map.
- uint64_t ImageOffset; // Offset in bytes of the actual binary image.
- uint64_t ImageSize; // Size in bytes of the binary image.
- };
-
- struct StringEntry {
- uint64_t KeyOffset;
- uint64_t ValueOffset;
- };
+ static bool classof(const Binary *V) { return V->isOffloadFile(); }
private:
OffloadBinary(MemoryBufferRef Source, const Header *TheHeader,
- const Entry *TheEntry)
+ const Entry *EntriesBegin)
: Binary(Binary::ID_Offload, Source), Buffer(Source.getBufferStart()),
- TheHeader(TheHeader), TheEntry(TheEntry) {
- const StringEntry *StringMapBegin =
- reinterpret_cast<const StringEntry *>(&Buffer[TheEntry->StringOffset]);
- for (uint64_t I = 0, E = TheEntry->NumStrings; I != E; ++I) {
- StringRef Key = &Buffer[StringMapBegin[I].KeyOffset];
- StringData[Key] = &Buffer[StringMapBegin[I].ValueOffset];
+ TheHeader(TheHeader) {
+ for (uint64_t EI = 0, EE = TheHeader->EntriesCount; EI != EE; ++EI) {
+ const Entry *TheEntry = &EntriesBegin[EI];
+ const StringEntry *StringMapBegin = reinterpret_cast<const StringEntry
*>(
+ &Buffer[TheEntry->StringOffset]);
+ StringMap Strings;
+ for (uint64_t SI = 0, SE = TheEntry->NumStrings; SI != SE; ++SI) {
+ StringRef Key = &Buffer[StringMapBegin[SI].KeyOffset];
+ StringRef Value = StringRef(
+ &Buffer[StringMapBegin[SI].ValueOffset],
StringMapBegin[SI].ValueSize);
+ Strings.insert({Key, Value});
+ }
+ Entries.push_back(std::make_pair(TheEntry, std::move(Strings)));
}
}
OffloadBinary(const OffloadBinary &Other) = delete;
- /// Map from keys to offsets in the binary.
- MapVector<StringRef, StringRef> StringData;
+ /// Location of the metadata entries within the binary mapped to
+ /// the key-value string data.
+ SmallVector<std::pair<const Entry *, StringMap>, 1> Entries;
----------------
jhuber6 wrote:
Right now I think we return references, but there's an explicit copy method? In
the copy method would need to copy the header, but references can just use the
existing one or something. The extracted interface isn't the binary serialized
one so we can probably do a little magic if needed
https://github.com/llvm/llvm-project/pull/169425
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits