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