LLVM 20.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
12#include <cstdint>
13
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/IR/Module.h"
18#include "llvm/Support/Error.h"
20
21namespace llvm {
22namespace offloading {
23
24/// This is the record of an object that just be registered with the offloading
25/// runtime.
26struct EntryTy {
27 /// Reserved bytes used to detect an older version of the struct, always zero.
29 /// The current version of the struct for runtime forward compatibility.
31 /// The expected consumer of this entry, e.g. CUDA or OpenMP.
33 /// Flags associated with the global.
35 /// The address of the global to be registered by the runtime.
36 void *Address;
37 /// The name of the symbol in the device image.
39 /// The number of bytes the symbol takes.
41 /// Extra generic data used to register this entry.
43 /// An extra pointer, usually null.
44 void *AuxAddr;
45};
46
47/// Offloading entry flags for CUDA / HIP. The first three bits indicate the
48/// type of entry while the others are a bit field for additional information.
50 /// Mark the entry as a global entry. This indicates the presense of a
51 /// kernel if the size size field is zero and a variable otherwise.
53 /// Mark the entry as a managed global variable.
55 /// Mark the entry as a surface variable.
57 /// Mark the entry as a texture variable.
59 /// Mark the entry as being extern.
61 /// Mark the entry as being constant.
63 /// Mark the entry as being a normalized surface.
65};
66
67/// Returns the type of the offloading entry we use to store kernels and
68/// globals that will be registered with the offloading runtime.
70
71/// Create an offloading section struct used to register this global at
72/// runtime.
73///
74/// \param M The module to be used
75/// \param Addr The pointer to the global being registered.
76/// \param Kind The offloading language expected to consume this.
77/// \param Name The symbol name associated with the global.
78/// \param Size The size in bytes of the global (0 for functions).
79/// \param Flags Flags associated with the entry.
80/// \param Data Extra data storage associated with the entry.
81/// \param SectionName The section this entry will be placed at.
82/// \param AuxAddr An extra pointer if needed.
86 Constant *AuxAddr = nullptr);
87
88/// Create a constant struct initializer used to register this global at
89/// runtime.
90/// \return the constant struct and the global variable holding the symbol name.
91std::pair<Constant *, GlobalVariable *>
94 uint32_t Flags, uint64_t Data, Constant *AuxAddr);
95
96/// Creates a pair of globals used to iterate the array of offloading entries by
97/// accessing the section variables provided by the linker.
98std::pair<GlobalVariable *, GlobalVariable *>
100
101namespace amdgpu {
102/// Check if an image is compatible with current system's environment. The
103/// system environment is given as a 'target-id' which has the form:
104///
105/// <target-id> := <processor> ( ":" <target-feature> ( "+" | "-" ) )*
106///
107/// If a feature is not specific as '+' or '-' it is assumed to be in an 'any'
108/// and is compatible with either '+' or '-'. The HSA runtime returns this
109/// information using the target-id, while we use the ELF header to determine
110/// these features.
111bool isImageCompatibleWithEnv(StringRef ImageArch, uint32_t ImageFlags,
112 StringRef EnvTargetID);
113
114/// Struct for holding metadata related to AMDGPU kernels, for more information
115/// about the metadata and its meaning see:
116/// https://llvm.org/docs/AMDGPUUsage.html#code-object-v3
118 /// Constant indicating that a value is invalid.
119 static constexpr uint32_t KInvalidValue =
120 std::numeric_limits<uint32_t>::max();
121 /// The amount of group segment memory required by a work-group in bytes.
123 /// The amount of fixed private address space memory required for a work-item
124 /// in bytes.
126 /// Number of scalar registers required by a wavefront.
128 /// Number of vector registers required by each work-item.
130 /// Number of stores from a scalar register to a register allocator created
131 /// spill location.
133 /// Number of stores from a vector register to a register allocator created
134 /// spill location.
136 /// Number of accumulator registers required by each work-item.
138 /// Corresponds to the OpenCL reqd_work_group_size attribute.
141 /// Corresponds to the OpenCL work_group_size_hint attribute.
143 /// Wavefront size.
145 /// Maximum flat work-group size supported by the kernel in work-items.
147};
148
149/// Reads AMDGPU specific metadata from the ELF file and propagates the
150/// KernelInfoMap.
152 StringMap<AMDGPUKernelMetaData> &KernelInfoMap,
153 uint16_t &ELFABIVersion);
154} // namespace amdgpu
155} // namespace offloading
156} // namespace llvm
157
158#endif // LLVM_FRONTEND_OFFLOADING_UTILITY_H
This file defines the StringMap class.
uint64_t Addr
std::string Name
uint64_t Size
Module.h This file contains the declarations for the Module class.
This is an important base class in LLVM.
Definition: Constant.h:42
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Class to represent struct types.
Definition: DerivedTypes.h:218
OffloadKind
The producer of the associated offloading image.
Definition: OffloadBinary.h:33
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:343
bool isImageCompatibleWithEnv(StringRef ImageArch, uint32_t ImageFlags, StringRef EnvTargetID)
Check if an image is compatible with current system's environment.
Definition: Utility.cpp:151
void emitOffloadingEntry(Module &M, object::OffloadKind Kind, Constant *Addr, StringRef Name, uint64_t Size, uint32_t Flags, uint64_t Data, StringRef SectionName, Constant *AuxAddr=nullptr)
Create an offloading section struct used to register this global at runtime.
Definition: Utility.cpp:83
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:38
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:24
OffloadEntryKindFlag
Offloading entry flags for CUDA / HIP.
Definition: Utility.h:49
@ OffloadGlobalSurfaceEntry
Mark the entry as a surface variable.
Definition: Utility.h:56
@ OffloadGlobalTextureEntry
Mark the entry as a texture variable.
Definition: Utility.h:58
@ OffloadGlobalNormalized
Mark the entry as being a normalized surface.
Definition: Utility.h:64
@ OffloadGlobalEntry
Mark the entry as a global entry.
Definition: Utility.h:52
@ OffloadGlobalManagedEntry
Mark the entry as a managed global variable.
Definition: Utility.h:54
@ OffloadGlobalExtern
Mark the entry as being extern.
Definition: Utility.h:60
@ OffloadGlobalConstant
Mark the entry as being constant.
Definition: Utility.h:62
std::pair< GlobalVariable *, GlobalVariable * > getOffloadEntryArray(Module &M, StringRef SectionName)
Creates a pair of globals used to iterate the array of offloading entries by accessing the section va...
Definition: Utility.cpp:110
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This is the record of an object that just be registered with the offloading runtime.
Definition: Utility.h:26
void * Address
The address of the global to be registered by the runtime.
Definition: Utility.h:36
uint64_t Size
The number of bytes the symbol takes.
Definition: Utility.h:40
uint16_t Kind
The expected consumer of this entry, e.g. CUDA or OpenMP.
Definition: Utility.h:32
char * SymbolName
The name of the symbol in the device image.
Definition: Utility.h:38
uint32_t Flags
Flags associated with the global.
Definition: Utility.h:34
uint64_t Reserved
Reserved bytes used to detect an older version of the struct, always zero.
Definition: Utility.h:28
uint16_t Version
The current version of the struct for runtime forward compatibility.
Definition: Utility.h:30
void * AuxAddr
An extra pointer, usually null.
Definition: Utility.h:44
uint64_t Data
Extra generic data used to register this entry.
Definition: Utility.h:42
Struct for holding metadata related to AMDGPU kernels, for more information about the metadata and it...
Definition: Utility.h:117
uint32_t SGPRSpillCount
Number of stores from a scalar register to a register allocator created spill location.
Definition: Utility.h:132
uint32_t SGPRCount
Number of scalar registers required by a wavefront.
Definition: Utility.h:127
static constexpr uint32_t KInvalidValue
Constant indicating that a value is invalid.
Definition: Utility.h:119
uint32_t VGPRSpillCount
Number of stores from a vector register to a register allocator created spill location.
Definition: Utility.h:135
uint32_t VGPRCount
Number of vector registers required by each work-item.
Definition: Utility.h:129
uint32_t PrivateSegmentSize
The amount of fixed private address space memory required for a work-item in bytes.
Definition: Utility.h:125
uint32_t GroupSegmentList
The amount of group segment memory required by a work-group in bytes.
Definition: Utility.h:122
uint32_t MaxFlatWorkgroupSize
Maximum flat work-group size supported by the kernel in work-items.
Definition: Utility.h:146
uint32_t WorkgroupSizeHint[3]
Corresponds to the OpenCL work_group_size_hint attribute.
Definition: Utility.h:142
uint32_t AGPRCount
Number of accumulator registers required by each work-item.
Definition: Utility.h:137
uint32_t RequestedWorkgroupSize[3]
Corresponds to the OpenCL reqd_work_group_size attribute.
Definition: Utility.h:139