1//===--- Offloading.h - Utilities for handling offloading code -*- C++ -*-===//
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
7//===----------------------------------------------------------------------===//
9// This file contains the binary format used for budingling 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.
15//===----------------------------------------------------------------------===//
17#ifndef LLVM_OBJECT_OFFLOADBINARY_H
18#define LLVM_OBJECT_OFFLOADBINARY_H
33/// The producer of the associated offloading image.
43/// The type of contents the offloading image contains.
55/// A simple binary serialization of an offloading file. We use this format to
56/// embed the offloading image into the host executable so it can be extracted
57/// and used by the linker.
59/// Many of these could be stored in the same section by the time the linker
60/// sees it so we mark this information with a header. The version is used to
61/// detect ABI stability and the size is used to find other offloading entries
62/// that may exist in the same section. All offsets are given as absolute byte
63/// offsets from the beginning of the file.
64 class OffloadBinary :
public Binary {
69 /// The current version of the binary used for backwards compatibility.
72 /// The offloading metadata that will be serialized to a memory buffer.
78 std::unique_ptr<MemoryBuffer>
Image;
81 /// Attempt to parse the offloading binary stored in \p Data.
85 /// Serialize the contents of \p File to a binary buffer to be read later.
99 return StringRef(&Buffer[TheEntry->ImageOffset], TheEntry->ImageSize);
102 // Iterator over all the key and value pairs in the binary.
112 uint8_t Magic[4] = {0x10, 0xFF, 0x10, 0xAD};
// 0x10FF10AD magic bytes.
136 const Entry *TheEntry)
138 TheHeader(TheHeader), TheEntry(TheEntry) {
140 reinterpret_cast<const StringEntry *
>(&Buffer[TheEntry->StringOffset]);
141 for (
uint64_t I = 0,
E = TheEntry->NumStrings;
I !=
E; ++
I) {
142 StringRef Key = &Buffer[StringMapBegin[I].KeyOffset];
143 StringData[Key] = &Buffer[StringMapBegin[I].ValueOffset];
149 /// Map from keys to offsets in the binary.
151 /// Raw pointer to the MemoryBufferRef for convenience.
153 /// Location of the header within the binary.
154 const Header *TheHeader;
155 /// Location of the metadata entries within the binary.
156 const Entry *TheEntry;
159/// A class to contain the binary information for a single OffloadBinary that
166 std::unique_ptr<MemoryBuffer> Buffer)
169 /// Make a deep copy of this offloading file.
172 getBinary()->getMemoryBufferRef().getBuffer(),
173 getBinary()->getMemoryBufferRef().getBufferIdentifier());
175 // This parsing should never fail because it has already been parsed.
177 assert(NewBinaryOrErr &&
"Failed to parse a copy of the binary?");
180 return OffloadFile(std::move(*NewBinaryOrErr), std::move(Buffer));
183 /// We use the Triple and Architecture pair to group linker inputs together.
184 /// This conversion function lets us use these inputs in a hash-map.
190/// Extracts embedded device offloading code from a memory \p Buffer to a list
195/// Convert a string \p Name to an image kind.
198/// Convert an image kind to its string representation.
201/// Convert a string \p Name to an offload kind.
204/// Convert an offload kind to its string representation.
207/// If the target is AMD we check the target IDs for mutual compatibility. A
208/// target id is a string conforming to the folowing BNF syntax:
210/// target-id ::= '<arch> ( : <feature> ( '+' | '-' ) )*'
212/// The features 'xnack' and 'sramecc' are currently supported. These can be in
213/// the state of on, off, and any when unspecified. A target marked as any can
214/// bind with either on or off. This is used to link mutually compatible
215/// architectures together. Returns false in the case of an exact match.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file implements a map that provides insertion order iteration.
This file defines the SmallString class.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
This class implements a map that also provides access to all stored values in a deterministic order.
typename VectorType::const_iterator const_iterator
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...
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.
A range adaptor for a pair of iterators.
OffloadBinary * getBinary()
Binary(unsigned int Type, MemoryBufferRef Source)
A simple binary serialization of an offloading file.
static uint64_t getAlignment()
OffloadKind getOffloadKind() const
iterator_range< string_iterator > string_iterator_range
StringRef getString(StringRef Key) const
uint32_t getVersion() const
StringRef getImage() const
static LLVM_ABI SmallString< 0 > write(const OffloadingImage &)
Serialize the contents of File to a binary buffer to be read later.
uint32_t getFlags() const
StringRef getTriple() const
ImageKind getImageKind() const
static LLVM_ABI Expected< std::unique_ptr< OffloadBinary > > create(MemoryBufferRef)
Attempt to parse the offloading binary stored in Data.
static bool classof(const Binary *V)
string_iterator_range strings() const
StringRef getArch() 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)
LLVM_ABI Error extractOffloadBinaries(MemoryBufferRef Buffer, SmallVectorImpl< OffloadFile > &Binaries)
Extracts embedded device offloading code from a memory Buffer to a list of Binaries.
LLVM_ABI ImageKind getImageKind(StringRef Name)
Convert a string Name to an image kind.
LLVM_ABI bool areTargetsCompatible(const OffloadFile::TargetID &LHS, const OffloadFile::TargetID &RHS)
If the target is AMD we check the target IDs for mutual compatibility.
OffloadKind
The producer of the associated offloading image.
LLVM_ABI OffloadKind getOffloadKind(StringRef Name)
Convert a string Name to an offload kind.
LLVM_ABI StringRef getImageKindName(ImageKind Name)
Convert an image kind to its string representation.
ImageKind
The type of contents the offloading image contains.
LLVM_ABI StringRef getOffloadKindName(OffloadKind Name)
Convert an offload kind to its string representation.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void consumeError(Error Err)
Consume a Error without doing anything.
Implement std::hash so that hash_code can be used in STL containers.
OffloadKind TheOffloadKind
The offloading metadata that will be serialized to a memory buffer.
OffloadKind TheOffloadKind
std::unique_ptr< MemoryBuffer > Image
MapVector< StringRef, StringRef > StringData