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