LLVM 23.0.0git
Utility.h
Go to the documentation of this file.
1//===- Utility.h - Collection of geneirc offloading utilities -------------===//
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#ifndef LLVM_FRONTEND_OFFLOADING_UTILITY_H
10#define LLVM_FRONTEND_OFFLOADING_UTILITY_H
11
13#include <cstdint>
14#include <memory>
15
16#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Module.h"
23#include "llvm/Support/Error.h"
25
26namespace llvm {
27namespace offloading {
28
29/// This is the record of an object that just be registered with the offloading
30/// runtime.
31struct EntryTy {
32 /// Reserved bytes used to detect an older version of the struct, always zero.
34 /// The current version of the struct for runtime forward compatibility.
36 /// The expected consumer of this entry, e.g. CUDA or OpenMP.
38 /// Flags associated with the global.
40 /// The address of the global to be registered by the runtime.
41 void *Address;
42 /// The name of the symbol in the device image.
44 /// The number of bytes the symbol takes.
46 /// Extra generic data used to register this entry.
48 /// An extra pointer, usually null.
49 void *AuxAddr;
50};
51
52/// Offloading entry flags for CUDA / HIP. The first three bits indicate the
53/// type of entry while the others are a bit field for additional information.
55 /// Mark the entry as a global entry. This indicates the presense of a
56 /// kernel if the size size field is zero and a variable otherwise.
58 /// Mark the entry as a managed global variable.
60 /// Mark the entry as a surface variable.
62 /// Mark the entry as a texture variable.
64 /// Mark the entry as being extern.
66 /// Mark the entry as being constant.
68 /// Mark the entry as being a normalized surface.
70};
71
72/// Returns the type of the offloading entry we use to store kernels and
73/// globals that will be registered with the offloading runtime.
75
76/// Create an offloading section struct used to register this global at
77/// runtime.
78///
79/// \param M The module to be used
80/// \param Addr The pointer to the global being registered.
81/// \param Kind The offloading language expected to consume this.
82/// \param Name The symbol name associated with the global.
83/// \param Size The size in bytes of the global (0 for functions).
84/// \param Flags Flags associated with the entry.
85/// \param Data Extra data storage associated with the entry.
86/// \param SectionName The section this entry will be placed at.
87/// \param AuxAddr An extra pointer if needed.
88/// Returns the section name for offloading entries based on the target triple.
89/// ELF: "llvm_offload_entries", COFF: "llvm_offload_entries",
90/// Mach-O: "__LLVM,offload_entries".
92
93/// \return The emitted global variable containing the offloading entry.
96 StringRef Name, uint64_t Size, uint32_t Flags,
97 uint64_t Data, Constant *AuxAddr = nullptr);
98
99/// Create a constant struct initializer used to register this global at
100/// runtime.
101/// \return the constant struct and the global variable holding the symbol name.
102LLVM_ABI std::pair<Constant *, GlobalVariable *>
104 Constant *Addr, StringRef Name, uint64_t Size,
105 uint32_t Flags, uint64_t Data, Constant *AuxAddr);
106
107/// Creates a pair of globals used to iterate the array of offloading entries by
108/// accessing the section variables provided by the linker.
109LLVM_ABI std::pair<GlobalVariable *, GlobalVariable *>
111
112namespace amdgpu {
113/// Check if an image is compatible with current system's environment. The
114/// system environment is given as a 'target-id' which has the form:
115///
116/// <target-id> := <processor> ( ":" <target-feature> ( "+" | "-" ) )*
117///
118/// If a feature is not specific as '+' or '-' it is assumed to be in an 'any'
119/// and is compatible with either '+' or '-'. The HSA runtime returns this
120/// information using the target-id, while we use the ELF header to determine
121/// these features.
122LLVM_ABI bool isImageCompatibleWithEnv(StringRef ImageArch, uint32_t ImageFlags,
123 StringRef EnvTargetID);
124
125/// Struct for holding metadata related to AMDGPU kernels, for more information
126/// about the metadata and its meaning see:
127/// https://llvm.org/docs/AMDGPUUsage.html#code-object-v3
129 /// Constant indicating that a value is invalid.
130 static constexpr uint32_t KInvalidValue =
131 std::numeric_limits<uint32_t>::max();
132 /// The amount of group segment memory required by a work-group in bytes.
134 /// The amount of fixed private address space memory required for a work-item
135 /// in bytes.
137 /// Number of scalar registers required by a wavefront.
139 /// Number of vector registers required by each work-item.
141 /// Number of stores from a scalar register to a register allocator created
142 /// spill location.
144 /// Number of stores from a vector register to a register allocator created
145 /// spill location.
147 /// Number of accumulator registers required by each work-item.
149 /// Corresponds to the OpenCL reqd_work_group_size attribute.
152 /// Corresponds to the OpenCL work_group_size_hint attribute.
154 /// Wavefront size.
156 /// Maximum flat work-group size supported by the kernel in work-items.
158 /// Per-argument {offset, size} in bytes, read from the ".args" array in code
159 /// object metadata. Explicit user arguments are first, followed by
160 /// hidden arguments.
162};
163
164/// Reads AMDGPU specific metadata from the ELF file and propagates the
165/// KernelInfoMap.
167 MemoryBufferRef MemBuffer, StringMap<AMDGPUKernelMetaData> &KernelInfoMap,
168 uint16_t &ELFABIVersion);
169} // namespace amdgpu
170
171/// Containerizes an image within an OffloadBinary image.
172/// Creates a nested OffloadBinary structure where the inner binary contains
173/// the raw image and associated metadata (version, format, triple, etc.).
174/// \param Binary The image to containerize.
175/// \param Triple The target triple to be associated with the image.
176/// \param ImageKind The format of the image, e.g. SPIR-V or CUBIN.
177/// \param OffloadKind The expected consuming runtime of the image, e.g. CUDA or
178/// OpenMP.
179/// \param ImageFlags Flags associated with the image, e.g. for AMDGPU the
180/// features.
181/// \param MetaData The key-value map of metadata to be associated with the
182/// image.
183LLVM_ABI Error containerizeImage(std::unique_ptr<MemoryBuffer> &Binary,
185 object::ImageKind ImageKind,
186 object::OffloadKind OffloadKind,
187 int32_t ImageFlags,
189
190namespace sycl {
191
192/// Serialized symbol table stored in the "symbols" entry of a SYCL
193/// OffloadBinary. The in-memory layout of the blob is:
194/// [ SymbolTableHeader ]
195/// [ SymbolTableEntry Entries[N] ] -- N == Header.Count
196/// [ char StringData[] ] -- packed null-terminated names
197/// Use writeSymbolTable() to produce the blob and forEachSymbol() to consume
198/// it; both encapsulate all pointer arithmetic.
199
201 uint32_t Count; ///< Number of symbol entries.
202};
204 uint32_t OffsetToSymbol; ///< Byte offset from blob start to the symbol name.
205 uint32_t SymbolSize; ///< Length of the symbol name in bytes, excluding
206 ///< the null terminator.
207};
208
209/// Serialize \p Names into \p Out.
211
212/// Invoke \p Callback with a \c StringRef for each symbol in \p Symbols,
213/// the raw serialized symbol-table blob.
214template <typename Fn> void forEachSymbol(StringRef Symbols, Fn &&Callback) {
215 assert(Symbols.size() >= sizeof(SymbolTableHeader) &&
216 "symbols blob smaller than header");
217 const char *Base = Symbols.data();
218 const auto &Header = *reinterpret_cast<const SymbolTableHeader *>(Base);
219 const auto *Entries = reinterpret_cast<const SymbolTableEntry *>(&Header + 1);
220 for (uint32_t I = 0; I < Header.Count; ++I)
221 Callback(
222 StringRef(Base + Entries[I].OffsetToSymbol, Entries[I].SymbolSize));
223}
224
225} // namespace sycl
226
227namespace intel {
228/// Containerizes an OpenMP SPIR-V image into an OffloadBinary image.
229/// \param Binary The SPIR-V binary to containerize.
230/// \param Triple The target triple to be associated with the image.
231/// \param CompileOpts Optional compilation options.
232/// \param LinkOpts Optional linking options.
234 std::unique_ptr<MemoryBuffer> &Binary, llvm::Triple Triple,
235 StringRef CompileOpts = "", StringRef LinkOpts = "");
236} // namespace intel
237} // namespace offloading
238} // namespace llvm
239
240#endif // LLVM_FRONTEND_OFFLOADING_UTILITY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:213
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallString class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is an important base class in LLVM.
Definition Constant.h:43
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Class to represent struct types.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
OffloadKind
The producer of the associated offloading image.
ImageKind
The type of contents the offloading image contains.
LLVM_ABI Error getAMDGPUMetaDataFromImage(MemoryBufferRef MemBuffer, StringMap< AMDGPUKernelMetaData > &KernelInfoMap, uint16_t &ELFABIVersion)
Reads AMDGPU specific metadata from the ELF file and propagates the KernelInfoMap.
Definition Utility.cpp:398
LLVM_ABI bool isImageCompatibleWithEnv(StringRef ImageArch, uint32_t ImageFlags, StringRef EnvTargetID)
Check if an image is compatible with current system's environment.
Definition Utility.cpp:185
LLVM_ABI Error containerizeOpenMPSPIRVImage(std::unique_ptr< MemoryBuffer > &Binary, llvm::Triple Triple, StringRef CompileOpts="", StringRef LinkOpts="")
Containerizes an OpenMP SPIR-V image into an OffloadBinary image.
Definition Utility.cpp:458
LLVM_ABI void writeSymbolTable(ArrayRef< StringRef > Names, SmallString< 0 > &Out)
Serialize Names into Out.
Definition Utility.cpp:478
void forEachSymbol(StringRef Symbols, Fn &&Callback)
Invoke Callback with a StringRef for each symbol in Symbols, the raw serialized symbol-table blob.
Definition Utility.h:214
LLVM_ABI Error containerizeImage(std::unique_ptr< MemoryBuffer > &Binary, llvm::Triple Triple, object::ImageKind ImageKind, object::OffloadKind OffloadKind, int32_t ImageFlags, MapVector< StringRef, StringRef > &MetaData)
Containerizes an image within an OffloadBinary image.
Definition Utility.cpp:432
LLVM_ABI std::pair< Constant *, GlobalVariable * > getOffloadingEntryInitializer(Module &M, object::OffloadKind Kind, Constant *Addr, StringRef Name, uint64_t Size, uint32_t Flags, uint64_t Data, Constant *AuxAddr)
Create a constant struct initializer used to register this global at runtime.
Definition Utility.cpp:42
LLVM_ABI StructType * getEntryTy(Module &M)
Returns the type of the offloading entry we use to store kernels and globals that will be registered ...
Definition Utility.cpp:28
OffloadEntryKindFlag
Offloading entry flags for CUDA / HIP.
Definition Utility.h:54
@ OffloadGlobalSurfaceEntry
Mark the entry as a surface variable.
Definition Utility.h:61
@ OffloadGlobalTextureEntry
Mark the entry as a texture variable.
Definition Utility.h:63
@ OffloadGlobalNormalized
Mark the entry as being a normalized surface.
Definition Utility.h:69
@ OffloadGlobalEntry
Mark the entry as a global entry.
Definition Utility.h:57
@ OffloadGlobalManagedEntry
Mark the entry as a managed global variable.
Definition Utility.h:59
@ OffloadGlobalExtern
Mark the entry as being extern.
Definition Utility.h:65
@ OffloadGlobalConstant
Mark the entry as being constant.
Definition Utility.h:67
LLVM_ABI GlobalVariable * emitOffloadingEntry(Module &M, object::OffloadKind Kind, Constant *Addr, StringRef Name, uint64_t Size, uint32_t Flags, uint64_t Data, Constant *AuxAddr=nullptr)
Definition Utility.cpp:105
LLVM_ABI std::pair< GlobalVariable *, GlobalVariable * > getOffloadEntryArray(Module &M)
Creates a pair of globals used to iterate the array of offloading entries by accessing the section va...
Definition Utility.cpp:132
LLVM_ABI StringRef getOffloadEntrySection(Module &M)
Create an offloading section struct used to register this global at runtime.
Definition Utility.cpp:87
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
This is the record of an object that just be registered with the offloading runtime.
Definition Utility.h:31
void * Address
The address of the global to be registered by the runtime.
Definition Utility.h:41
uint64_t Size
The number of bytes the symbol takes.
Definition Utility.h:45
uint16_t Kind
The expected consumer of this entry, e.g. CUDA or OpenMP.
Definition Utility.h:37
char * SymbolName
The name of the symbol in the device image.
Definition Utility.h:43
uint32_t Flags
Flags associated with the global.
Definition Utility.h:39
uint64_t Reserved
Reserved bytes used to detect an older version of the struct, always zero.
Definition Utility.h:33
uint16_t Version
The current version of the struct for runtime forward compatibility.
Definition Utility.h:35
void * AuxAddr
An extra pointer, usually null.
Definition Utility.h:49
uint64_t Data
Extra generic data used to register this entry.
Definition Utility.h:47
Struct for holding metadata related to AMDGPU kernels, for more information about the metadata and it...
Definition Utility.h:128
uint32_t SGPRSpillCount
Number of stores from a scalar register to a register allocator created spill location.
Definition Utility.h:143
uint32_t SGPRCount
Number of scalar registers required by a wavefront.
Definition Utility.h:138
SmallVector< std::pair< uint32_t, uint32_t >, 8 > ArgMDs
Per-argument {offset, size} in bytes, read from the ".args" array in code object metadata.
Definition Utility.h:161
static constexpr uint32_t KInvalidValue
Constant indicating that a value is invalid.
Definition Utility.h:130
uint32_t VGPRSpillCount
Number of stores from a vector register to a register allocator created spill location.
Definition Utility.h:146
uint32_t VGPRCount
Number of vector registers required by each work-item.
Definition Utility.h:140
uint32_t PrivateSegmentSize
The amount of fixed private address space memory required for a work-item in bytes.
Definition Utility.h:136
uint32_t GroupSegmentList
The amount of group segment memory required by a work-group in bytes.
Definition Utility.h:133
uint32_t MaxFlatWorkgroupSize
Maximum flat work-group size supported by the kernel in work-items.
Definition Utility.h:157
uint32_t WorkgroupSizeHint[3]
Corresponds to the OpenCL work_group_size_hint attribute.
Definition Utility.h:153
uint32_t AGPRCount
Number of accumulator registers required by each work-item.
Definition Utility.h:148
uint32_t RequestedWorkgroupSize[3]
Corresponds to the OpenCL reqd_work_group_size attribute.
Definition Utility.h:150
uint32_t OffsetToSymbol
Byte offset from blob start to the symbol name.
Definition Utility.h:204
uint32_t SymbolSize
Length of the symbol name in bytes, excluding the null terminator.
Definition Utility.h:205
Serialized symbol table stored in the "symbols" entry of a SYCL OffloadBinary.
Definition Utility.h:200
uint32_t Count
Number of symbol entries.
Definition Utility.h:201