LLVM 23.0.0git
OffloadBinary.h
Go to the documentation of this file.
1//===--- Offloading.h - Utilities for handling offloading code -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the binary format used for bundling device metadata with
10// an associated device image. The data can then be stored inside a host object
11// file to create a fat binary and read by the linker. This is intended to be a
12// thin wrapper around the image itself. If this format becomes sufficiently
13// complex it should be moved to a standard binary format like msgpack or ELF.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_OBJECT_OFFLOADBINARY_H
18#define LLVM_OBJECT_OFFLOADBINARY_H
19
20#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Object/Binary.h"
25#include "llvm/Support/Error.h"
27#include <memory>
28
29namespace llvm {
30
31namespace object {
32
33/// The producer of the associated offloading image.
36 OFK_OpenMP = (1 << 0),
37 OFK_Cuda = (1 << 1),
38 OFK_HIP = (1 << 2),
39 OFK_SYCL = (1 << 3),
40 OFK_LAST = (1 << 4),
41};
42
43/// The type of contents the offloading image contains.
54
55/// Flags associated with the Entry.
58 // Entry doesn't contain an image. Used to keep metadata only entries.
59 OIF_Metadata = (1 << 0),
60};
61
62/// A simple binary serialization of an offloading file. We use this format to
63/// embed the offloading image into the host executable so it can be extracted
64/// and used by the linker.
65///
66/// Many of these could be stored in the same section by the time the linker
67/// sees it so we mark this information with a header. The version is used to
68/// detect ABI stability and the size is used to find other offloading entries
69/// that may exist in the same section. All offsets are given as absolute byte
70/// offsets from the beginning of the file.
71class OffloadBinary : public Binary {
72public:
75
76 /// The current version of the binary used for backwards compatibility.
77 static const uint32_t Version = 2;
78
79 /// The offloading metadata that will be serialized to a memory buffer.
87
88 struct Header {
89 uint8_t Magic[4] = {0x10, 0xFF, 0x10, 0xAD}; // 0x10FF10AD magic bytes.
90 uint32_t Version = OffloadBinary::Version; // Version identifier.
91 uint64_t Size; // Size in bytes of this entire binary.
92 uint64_t EntriesOffset; // Offset in bytes to the start of entries block.
93 uint64_t EntriesCount; // Number of metadata entries in the binary.
94 };
95
96 struct Entry {
97 ImageKind TheImageKind; // The kind of the image stored.
98 OffloadKind TheOffloadKind; // The producer of this image.
99 uint32_t Flags; // Additional flags associated with the entry.
100 uint64_t StringOffset; // Offset in bytes to the string map.
101 uint64_t NumStrings; // Number of entries in the string map.
102 uint64_t ImageOffset; // Offset in bytes of the actual binary image.
103 uint64_t ImageSize; // Size in bytes of the binary image.
104 };
105
106 struct StringEntry {
109 uint64_t ValueSize; // Size of the value in bytes.
110 };
111
116
117 /// Attempt to extract and validate the header from the offloading binary in
118 /// \p Buf.
121
122 /// Attempt to parse the offloading binary stored in \p Buf.
123 /// For version 1 binaries, always returns a single OffloadBinary.
124 /// For version 2+ binaries:
125 /// - If \p Index is provided, returns the OffloadBinary at that index.
126 /// - If \p Index is std::nullopt, returns all OffloadBinary entries.
127 /// \param Buf The memory buffer containing the offload binary.
128 /// \param Index Optional index to select a specific entry. If not provided,
129 /// all entries are returned (version 2+ only).
130 /// \returns An array of unique pointers to OffloadBinary objects, or an
131 /// error.
133 create(MemoryBufferRef Buf, std::optional<uint64_t> Index = std::nullopt);
134
135 /// Serialize the contents of \p OffloadingData to a binary buffer to be read
136 /// later.
138 write(ArrayRef<OffloadingImage> OffloadingData);
139
140 static uint64_t getAlignment() { return 8; }
141
142 ImageKind getImageKind() const { return TheEntry->TheImageKind; }
143 OffloadKind getOffloadKind() const { return TheEntry->TheOffloadKind; }
144 uint32_t getVersion() const { return TheHeader->Version; }
145 uint32_t getFlags() const { return TheEntry->Flags; }
146 uint64_t getSize() const { return TheHeader->Size; }
147 uint64_t getIndex() const { return Index; }
148
149 StringRef getTriple() const { return getString("triple"); }
150 StringRef getArch() const { return getString("arch"); }
152 return StringRef(&Buffer[TheEntry->ImageOffset], TheEntry->ImageSize);
153 }
154
155 // Iterator over all the key and value pairs in the binary.
156 string_iterator_range strings() const { return StringData; }
157
158 StringRef getString(StringRef Key) const { return StringData.lookup(Key); }
159
160 static bool classof(const Binary *V) { return V->isOffloadFile(); }
161
162private:
163 OffloadBinary(MemoryBufferRef Source, const Header *TheHeader,
164 const Entry *TheEntry, const uint64_t Index = 0)
165 : Binary(Binary::ID_Offload, Source), Buffer(Source.getBufferStart()),
166 TheHeader(TheHeader), TheEntry(TheEntry), Index(Index) {
167 // StringEntryV1 and StringEntry have ABI compatible Key/ValueOffset fields,
168 // but different sizes, so we need to manually calculate offset.
169 const char *StringMapBegin = &Buffer[TheEntry->StringOffset];
170 const size_t StringEntrySize =
171 TheHeader->Version == 1 ? sizeof(StringEntryV1) : sizeof(StringEntry);
172 for (uint64_t I = 0, E = TheEntry->NumStrings; I != E; ++I) {
173 const char *StringEntryPtr = StringMapBegin + I * StringEntrySize;
174 const StringEntryV1 *EntryV1 =
175 reinterpret_cast<const StringEntryV1 *>(StringEntryPtr);
176 StringRef Key = &Buffer[EntryV1->KeyOffset];
177 if (TheHeader->Version == 1) {
178 StringData[Key] = &Buffer[EntryV1->ValueOffset];
179 } else {
180 const StringEntry *Entry =
181 reinterpret_cast<const StringEntry *>(StringEntryPtr);
182 StringData[Key] =
183 StringRef(&Buffer[Entry->ValueOffset], Entry->ValueSize);
184 }
185 }
186 }
187
188 OffloadBinary(const OffloadBinary &Other) = delete;
189
190 /// Map from keys to offsets in the binary.
191 MapVector<StringRef, StringRef> StringData;
192 /// Raw pointer to the MemoryBufferRef for convenience.
193 const char *Buffer;
194 /// Location of the header within the binary.
195 const Header *TheHeader;
196 /// Location of the metadata entries within the binary.
197 const Entry *TheEntry;
198 /// Index of the entry in the list of entries serialized in the Buffer.
199 const uint64_t Index;
200};
201
202/// A class to contain the binary information for a single OffloadBinary.
203/// Memory is shared between multiple OffloadBinary instances read from
204/// the single serialized offload binary.
205class OffloadFile : public OwningBinary<OffloadBinary> {
206public:
207 using TargetID = std::pair<StringRef, StringRef>;
208
209 OffloadFile(std::unique_ptr<OffloadBinary> Binary,
210 std::unique_ptr<MemoryBuffer> Buffer)
211 : OwningBinary<OffloadBinary>(std::move(Binary), std::move(Buffer)) {}
212
213 /// Make a deep copy of this offloading file.
215 std::unique_ptr<MemoryBuffer> Buffer = MemoryBuffer::getMemBufferCopy(
216 getBinary()->getMemoryBufferRef().getBuffer(),
217 getBinary()->getMemoryBufferRef().getBufferIdentifier());
218
219 // This parsing should never fail because it has already been parsed.
220 auto NewBinaryOrErr =
221 OffloadBinary::create(*Buffer, getBinary()->getIndex());
222 assert(NewBinaryOrErr && "Failed to parse a copy of the binary?");
223 if (!NewBinaryOrErr)
224 llvm::consumeError(NewBinaryOrErr.takeError());
225 return OffloadFile(std::move((*NewBinaryOrErr)[0]), std::move(Buffer));
226 }
227
228 /// We use the Triple and Architecture pair to group linker inputs together.
229 /// This conversion function lets us use these inputs in a hash-map.
230 operator TargetID() const {
231 return std::make_pair(getBinary()->getTriple(), getBinary()->getArch());
232 }
233};
234
235/// Extracts embedded device offloading code from a memory \p Buffer to a list
236/// of \p Binaries.
237LLVM_ABI Error extractOffloadBinaries(MemoryBufferRef Buffer,
239
240/// Convert a string \p Name to an image kind.
241LLVM_ABI ImageKind getImageKind(StringRef Name);
242
243/// Convert an image kind to its string representation.
244LLVM_ABI StringRef getImageKindName(ImageKind Name);
245
246/// Convert a string \p Name to an offload kind.
247LLVM_ABI OffloadKind getOffloadKind(StringRef Name);
248
249/// Convert an offload kind to its string representation.
250LLVM_ABI StringRef getOffloadKindName(OffloadKind Name);
251
252/// If the target is AMD we check the target IDs for mutual compatibility. A
253/// target id is a string conforming to the folowing BNF syntax:
254///
255/// target-id ::= '<arch> ( : <feature> ( '+' | '-' ) )*'
256///
257/// The features 'xnack' and 'sramecc' are currently supported. These can be in
258/// the state of on, off, and any when unspecified. A target marked as any can
259/// bind with either on or off. This is used to link mutually compatible
260/// architectures together. Returns false in the case of an exact match.
261LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &LHS,
262 const OffloadFile::TargetID &RHS);
263
264} // namespace object
265
266} // namespace llvm
267#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
This file defines the SmallString class.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
typename VectorType::const_iterator const_iterator
Definition MapVector.h:43
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
A range adaptor for a pair of iterators.
Binary(unsigned int Type, MemoryBufferRef Source)
Definition Binary.cpp:36
A simple binary serialization of an offloading file.
static uint64_t getAlignment()
OffloadKind getOffloadKind() const
iterator_range< string_iterator > string_iterator_range
static LLVM_ABI Expected< SmallVector< std::unique_ptr< OffloadBinary > > > create(MemoryBufferRef Buf, std::optional< uint64_t > Index=std::nullopt)
Attempt to parse the offloading binary stored in Buf.
static LLVM_ABI SmallString< 0 > write(ArrayRef< OffloadingImage > OffloadingData)
Serialize the contents of OffloadingData to a binary buffer to be read later.
StringRef getString(StringRef Key) const
static LLVM_ABI Expected< const Header * > extractHeader(MemoryBufferRef Buf)
Attempt to extract and validate the header from the offloading binary in Buf.
StringRef getTriple() const
ImageKind getImageKind() const
static bool classof(const Binary *V)
string_iterator_range strings() const
static const uint32_t Version
The current version of the binary used for backwards compatibility.
MapVector< StringRef, StringRef >::const_iterator string_iterator
std::pair< StringRef, StringRef > TargetID
OffloadFile copy() const
Make a deep copy of this offloading file.
OffloadFile(std::unique_ptr< OffloadBinary > Binary, std::unique_ptr< MemoryBuffer > Buffer)
StringMapEntry< EmptyStringSetTag > StringEntry
StringEntry keeps data of the string: the length, external offset and a string body which is placed r...
Definition StringPool.h:23
OffloadEntryFlags
Flags associated with the Entry.
OffloadKind
The producer of the associated offloading image.
ImageKind
The type of contents the offloading image contains.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
The offloading metadata that will be serialized to a memory buffer.
std::unique_ptr< MemoryBuffer > Image
MapVector< StringRef, StringRef > StringData