1//=====-- AMDGPUSubtarget.h - Define Subtarget for AMDGPU -------*- C++ -*-===//
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/// \file
10/// Base class for AMDGPU specific classes of TargetSubtarget.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
15#define LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/IR/CallingConv.h"
19#include "llvm/Support/Alignment.h"
20#include "llvm/TargetParser/Triple.h"
21
22namespace llvm {
23
24enum AMDGPUDwarfFlavour : unsigned;
25class Function;
26class Instruction;
27class MachineFunction;
28class TargetMachine;
29
30class AMDGPUSubtarget {
31public:
32 enum Generation {
33 INVALID = 0,
34 R600 = 1,
35 R700 = 2,
36 EVERGREEN = 3,
37 NORTHERN_ISLANDS = 4,
38 SOUTHERN_ISLANDS = 5,
39 SEA_ISLANDS = 6,
40 VOLCANIC_ISLANDS = 7,
41 GFX9 = 8,
42 GFX10 = 9,
43 GFX11 = 10,
44 GFX12 = 11,
45 GFX13 = 12,
46 };
47
48private:
49 const Triple &TargetTriple;
50
51protected:
52 bool HasMulI24 = true;
53 bool HasMulU24 = true;
54 bool HasSMulHi = false;
55 bool HasFminFmaxLegacy = true;
56
57 unsigned EUsPerCU = 4;
58 unsigned MaxWavesPerEU = 10;
59 unsigned LocalMemorySize = 0;
60 unsigned AddressableLocalMemorySize = 0;
61 unsigned LDSAllocationGranularity = 0;
62 char WavefrontSizeLog2 = 0;
63 unsigned FlatOffsetBitWidth = 0;
64
65public:
66 AMDGPUSubtarget(const Triple &TT) : TargetTriple(TT) {}
67
68 static const AMDGPUSubtarget &get(const MachineFunction &MF);
69 static const AMDGPUSubtarget &get(const TargetMachine &TM,
70 const Function &F);
71
72 /// \returns Default range flat work group size for a calling convention.
73 std::pair<unsigned, unsigned> getDefaultFlatWorkGroupSize(CallingConv::ID CC) const;
74
75 /// \returns Subtarget's default pair of minimum/maximum flat work group sizes
76 /// for function \p F, or minimum/maximum flat work group sizes explicitly
77 /// requested using "amdgpu-flat-work-group-size" attribute attached to
78 /// function \p F.
79 ///
80 /// \returns Subtarget's default values if explicitly requested values cannot
81 /// be converted to integer, or violate subtarget's specifications.
82 std::pair<unsigned, unsigned> getFlatWorkGroupSizes(const Function &F) const;
83
84 /// \returns The required size of workgroups that will be used to execute \p F
85 /// in the \p Dim dimension, if it is known (from `!reqd_work_group_size`
86 /// metadata. Otherwise, returns std::nullopt.
87 std::optional<unsigned> getReqdWorkGroupSize(const Function &F,
88 unsigned Dim) const;
89
90 /// \returns true if \p F will execute in a manner that leaves the X
91 /// dimensions of the workitem ID evenly tiling wavefronts - that is, if X /
92 /// wavefrontsize is uniform. This is true if either the Y and Z block
93 /// dimensions are known to always be 1 or if the X dimension will always be a
94 /// power of 2. If \p RequireUniformYZ is true, it also ensures that the Y and
95 /// Z workitem IDs will be uniform (so, while a (32, 2, 1) launch with
96 /// wavesize64 would ordinarily pass this test, it won't with
97 /// \pRequiresUniformYZ).
98 ///
99 /// This information is currently only gathered from the !reqd_work_group_size
100 /// metadata on \p F, but this may be improved in the future.
101 bool hasWavefrontsEvenlySplittingXDim(const Function &F,
102 bool REquiresUniformYZ = false) const;
103
104 /// \returns Subtarget's default pair of minimum/maximum number of waves per
105 /// execution unit for function \p F, or minimum/maximum number of waves per
106 /// execution unit explicitly requested using "amdgpu-waves-per-eu" attribute
107 /// attached to function \p F.
108 ///
109 /// \returns Subtarget's default values if explicitly requested values cannot
110 /// be converted to integer, violate subtarget's specifications, or are not
111 /// compatible with minimum/maximum number of waves limited by flat work group
112 /// size, register usage, and/or lds usage.
113 std::pair<unsigned, unsigned> getWavesPerEU(const Function &F) const;
114
115 /// Overload which uses the specified values for the flat workgroup sizes and
116 /// LDS space rather than querying the function itself. \p FlatWorkGroupSizes
117 /// should correspond to the function's value for getFlatWorkGroupSizes and \p
118 /// LDSBytes to the per-workgroup LDS allocation.
119 std::pair<unsigned, unsigned>
120 getWavesPerEU(std::pair<unsigned, unsigned> FlatWorkGroupSizes,
121 unsigned LDSBytes, const Function &F) const;
122
123 /// Returns the target minimum/maximum number of waves per EU. This is based
124 /// on the minimum/maximum number of \p RequestedWavesPerEU and further
125 /// limited by the maximum achievable occupancy derived from the range of \p
126 /// FlatWorkGroupSizes and number of \p LDSBytes per workgroup.
127 std::pair<unsigned, unsigned>
128 getEffectiveWavesPerEU(std::pair<unsigned, unsigned> RequestedWavesPerEU,
129 std::pair<unsigned, unsigned> FlatWorkGroupSizes,
130 unsigned LDSBytes) const;
131
132 /// Return the amount of LDS that can be used that will not restrict the
133 /// occupancy lower than WaveCount.
134 unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount,
135 const Function &) const;
136
137 /// Subtarget's minimum/maximum occupancy, in number of waves per EU, that can
138 /// be achieved when the only function running on a CU is \p F and each
139 /// workgroup running the function requires \p LDSBytes bytes of LDS space.
140 /// This notably depends on the range of allowed flat group sizes for the
141 /// function and hardware characteristics.
142 std::pair<unsigned, unsigned>
143 getOccupancyWithWorkGroupSizes(uint32_t LDSBytes, const Function &F) const {
144 return getOccupancyWithWorkGroupSizes(LDSBytes, FlatWorkGroupSizes: getFlatWorkGroupSizes(F));
145 }
146
147 /// Overload which uses the specified values for the flat work group sizes,
148 /// rather than querying the function itself. \p FlatWorkGroupSizes should
149 /// correspond to the function's value for getFlatWorkGroupSizes.
150 std::pair<unsigned, unsigned> getOccupancyWithWorkGroupSizes(
151 uint32_t LDSBytes,
152 std::pair<unsigned, unsigned> FlatWorkGroupSizes) const;
153
154 /// Subtarget's minimum/maximum occupancy, in number of waves per EU, that can
155 /// be achieved when the only function running on a CU is \p MF. This notably
156 /// depends on the range of allowed flat group sizes for the function, the
157 /// amount of per-workgroup LDS space required by the function, and hardware
158 /// characteristics.
159 std::pair<unsigned, unsigned>
160 getOccupancyWithWorkGroupSizes(const MachineFunction &MF) const;
161
162 bool isAmdHsaOS() const {
163 return TargetTriple.getOS() == Triple::AMDHSA;
164 }
165
166 bool isAmdPalOS() const {
167 return TargetTriple.getOS() == Triple::AMDPAL;
168 }
169
170 bool isMesa3DOS() const {
171 return TargetTriple.getOS() == Triple::Mesa3D;
172 }
173
174 bool isMesaKernel(const Function &F) const;
175
176 bool isAmdHsaOrMesa(const Function &F) const {
177 return isAmdHsaOS() || isMesaKernel(F);
178 }
179
180 bool isGCN() const { return TargetTriple.isAMDGCN(); }
181
182 //==---------------------------------------------------------------------===//
183 // TableGen-generated feature getters.
184 //==---------------------------------------------------------------------===//
185#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
186 virtual bool GETTER() const { return false; }
187#include "AMDGPUGenSubtargetInfo.inc"
188 //==---------------------------------------------------------------------===//
189
190 /// Return true if real (non-fake) variants of True16 instructions using
191 /// 16-bit registers should be code-generated. Fake True16 instructions are
192 /// identical to non-fake ones except that they take 32-bit registers as
193 /// operands and always use their low halves.
194 // TODO: Remove and use hasTrue16BitInsts() instead once True16 is fully
195 // supported and the support for fake True16 instructions is removed.
196 bool useRealTrue16Insts() const {
197 return hasTrue16BitInsts() && enableRealTrue16Insts();
198 }
199
200 bool hasMulI24() const {
201 return HasMulI24;
202 }
203
204 bool hasMulU24() const {
205 return HasMulU24;
206 }
207
208 bool hasSMulHi() const {
209 return HasSMulHi;
210 }
211
212 bool hasFminFmaxLegacy() const {
213 return HasFminFmaxLegacy;
214 }
215
216 unsigned getWavefrontSize() const {
217 return 1 << WavefrontSizeLog2;
218 }
219
220 unsigned getWavefrontSizeLog2() const {
221 return WavefrontSizeLog2;
222 }
223
224 /// Return the maximum number of bytes of LDS available for all workgroups
225 /// running on the same WGP or CU.
226 /// For GFX10-GFX12 in WGP mode this is 128k even though each workgroup is
227 /// limited to 64k.
228 unsigned getLocalMemorySize() const {
229 return LocalMemorySize;
230 }
231
232 /// Return the maximum number of bytes of LDS that can be allocated to a
233 /// single workgroup.
234 /// For GFX10-GFX12 in WGP mode this is limited to 64k even though the WGP has
235 /// 128k in total.
236 unsigned getAddressableLocalMemorySize() const {
237 return AddressableLocalMemorySize;
238 }
239
240 /// Number of SIMDs/EUs (execution units) per "CU" ("compute unit"), where the
241 /// "CU" is the unit onto which workgroups are mapped. This takes WGP mode vs.
242 /// CU mode into account.
243 unsigned getEUsPerCU() const { return EUsPerCU; }
244
245 Align getAlignmentForImplicitArgPtr() const {
246 return isAmdHsaOS() ? Align(8) : Align(4);
247 }
248
249 /// Returns the offset in bytes from the start of the input buffer
250 /// of the first explicit kernel argument.
251 unsigned getExplicitKernelArgOffset() const {
252 switch (TargetTriple.getOS()) {
253 case Triple::AMDHSA:
254 case Triple::AMDPAL:
255 case Triple::Mesa3D:
256 return 0;
257 case Triple::UnknownOS:
258 default:
259 // For legacy reasons unknown/other is treated as a different version of
260 // mesa.
261 return 36;
262 }
263
264 llvm_unreachable("invalid triple OS");
265 }
266
267 /// \returns Maximum number of work groups per compute unit supported by the
268 /// subtarget and limited by given \p FlatWorkGroupSize.
269 virtual unsigned getMaxWorkGroupsPerCU(unsigned FlatWorkGroupSize) const = 0;
270
271 /// \returns Minimum flat work group size supported by the subtarget.
272 virtual unsigned getMinFlatWorkGroupSize() const = 0;
273
274 /// \returns Maximum flat work group size supported by the subtarget.
275 virtual unsigned getMaxFlatWorkGroupSize() const = 0;
276
277 /// \returns Number of waves per execution unit required to support the given
278 /// \p FlatWorkGroupSize.
279 virtual unsigned
280 getWavesPerEUForWorkGroup(unsigned FlatWorkGroupSize) const = 0;
281
282 /// \returns Minimum number of waves per execution unit supported by the
283 /// subtarget.
284 virtual unsigned getMinWavesPerEU() const = 0;
285
286 /// \returns Maximum number of waves per execution unit supported by the
287 /// subtarget without any kind of limitation.
288 unsigned getMaxWavesPerEU() const { return MaxWavesPerEU; }
289
290 /// Return the maximum workitem ID value in the function, for the given (0, 1,
291 /// 2) dimension.
292 unsigned getMaxWorkitemID(const Function &Kernel, unsigned Dimension) const;
293
294 /// Return true if only a single workitem can be active in a wave.
295 bool isSingleLaneExecution(const Function &Kernel) const;
296
297 /// Creates value range metadata on an workitemid.* intrinsic call or load.
298 bool makeLIDRangeMetadata(Instruction *I) const;
299
300 /// \returns Number of bytes of arguments that are passed to a shader or
301 /// kernel in addition to the explicit ones declared for the function.
302 unsigned getImplicitArgNumBytes(const Function &F) const;
303 uint64_t getExplicitKernArgSize(const Function &F, Align &MaxAlign) const;
304 unsigned getKernArgSegmentSize(const Function &F, Align &MaxAlign) const;
305
306 /// \returns Corresponding DWARF register number mapping flavour for the
307 /// \p WavefrontSize.
308 AMDGPUDwarfFlavour getAMDGPUDwarfFlavour() const;
309
310 virtual ~AMDGPUSubtarget() = default;
311};
312
313} // end namespace llvm
314
315#endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUSUBTARGET_H
316