1 //===- AMDGPUBaseInfo.h - Top level definitions for AMDGPU ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11 #define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
14 #include "AMDKernelCodeT.h"
15 #include "SIDefines.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/CallingConv.h"
18 #include "llvm/MC/MCInstrDesc.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ErrorHandling.h"
24 #define GET_INSTRINFO_OPERAND_ENUM
25 #include "AMDGPUGenInstrInfo.inc"
26 #undef GET_INSTRINFO_OPERAND_ENUM
33 class MachineMemOperand;
35 class MCRegisterClass;
38 class MCSubtargetInfo;
45 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
46 // doesn't spill SGPRs as much as when 80 is set.
47 FIXED_NUM_SGPRS_FOR_INIT_BUG = 96
50 /// \brief Instruction set architecture version.
57 /// \returns Isa version for given subtarget \p Features.
58 IsaVersion getIsaVersion(const FeatureBitset &Features);
60 /// \returns Wavefront size for given subtarget \p Features.
61 unsigned getWavefrontSize(const FeatureBitset &Features);
63 /// \returns Local memory size in bytes for given subtarget \p Features.
64 unsigned getLocalMemorySize(const FeatureBitset &Features);
66 /// \returns Number of execution units per compute unit for given subtarget \p
68 unsigned getEUsPerCU(const FeatureBitset &Features);
70 /// \returns Maximum number of work groups per compute unit for given subtarget
71 /// \p Features and limited by given \p FlatWorkGroupSize.
72 unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
73 unsigned FlatWorkGroupSize);
75 /// \returns Maximum number of waves per compute unit for given subtarget \p
76 /// Features without any kind of limitation.
77 unsigned getMaxWavesPerCU(const FeatureBitset &Features);
79 /// \returns Maximum number of waves per compute unit for given subtarget \p
80 /// Features and limited by given \p FlatWorkGroupSize.
81 unsigned getMaxWavesPerCU(const FeatureBitset &Features,
82 unsigned FlatWorkGroupSize);
84 /// \returns Minimum number of waves per execution unit for given subtarget \p
86 unsigned getMinWavesPerEU(const FeatureBitset &Features);
88 /// \returns Maximum number of waves per execution unit for given subtarget \p
89 /// Features without any kind of limitation.
90 unsigned getMaxWavesPerEU(const FeatureBitset &Features);
92 /// \returns Maximum number of waves per execution unit for given subtarget \p
93 /// Features and limited by given \p FlatWorkGroupSize.
94 unsigned getMaxWavesPerEU(const FeatureBitset &Features,
95 unsigned FlatWorkGroupSize);
97 /// \returns Minimum flat work group size for given subtarget \p Features.
98 unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features);
100 /// \returns Maximum flat work group size for given subtarget \p Features.
101 unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features);
103 /// \returns Number of waves per work group for given subtarget \p Features and
104 /// limited by given \p FlatWorkGroupSize.
105 unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
106 unsigned FlatWorkGroupSize);
108 /// \returns SGPR allocation granularity for given subtarget \p Features.
109 unsigned getSGPRAllocGranule(const FeatureBitset &Features);
111 /// \returns SGPR encoding granularity for given subtarget \p Features.
112 unsigned getSGPREncodingGranule(const FeatureBitset &Features);
114 /// \returns Total number of SGPRs for given subtarget \p Features.
115 unsigned getTotalNumSGPRs(const FeatureBitset &Features);
117 /// \returns Addressable number of SGPRs for given subtarget \p Features.
118 unsigned getAddressableNumSGPRs(const FeatureBitset &Features);
120 /// \returns Minimum number of SGPRs that meets the given number of waves per
121 /// execution unit requirement for given subtarget \p Features.
122 unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
124 /// \returns Maximum number of SGPRs that meets the given number of waves per
125 /// execution unit requirement for given subtarget \p Features.
126 unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
129 /// \returns VGPR allocation granularity for given subtarget \p Features.
130 unsigned getVGPRAllocGranule(const FeatureBitset &Features);
132 /// \returns VGPR encoding granularity for given subtarget \p Features.
133 unsigned getVGPREncodingGranule(const FeatureBitset &Features);
135 /// \returns Total number of VGPRs for given subtarget \p Features.
136 unsigned getTotalNumVGPRs(const FeatureBitset &Features);
138 /// \returns Addressable number of VGPRs for given subtarget \p Features.
139 unsigned getAddressableNumVGPRs(const FeatureBitset &Features);
141 /// \returns Minimum number of VGPRs that meets given number of waves per
142 /// execution unit requirement for given subtarget \p Features.
143 unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
145 /// \returns Maximum number of VGPRs that meets given number of waves per
146 /// execution unit requirement for given subtarget \p Features.
147 unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
149 } // end namespace IsaInfo
152 int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
154 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
155 const FeatureBitset &Features);
156 MCSection *getHSATextSection(MCContext &Ctx);
158 MCSection *getHSADataGlobalAgentSection(MCContext &Ctx);
160 MCSection *getHSADataGlobalProgramSection(MCContext &Ctx);
162 MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx);
164 bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS);
165 bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS);
166 bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS);
168 /// \returns True if constants should be emitted to .text section for given
169 /// target triple \p TT, false otherwise.
170 bool shouldEmitConstantsToTextSection(const Triple &TT);
172 /// \returns Integer value requested using \p F's \p Name attribute.
174 /// \returns \p Default if attribute is not present.
176 /// \returns \p Default and emits error if requested value cannot be converted
178 int getIntegerAttribute(const Function &F, StringRef Name, int Default);
180 /// \returns A pair of integer values requested using \p F's \p Name attribute
181 /// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
184 /// \returns \p Default if attribute is not present.
186 /// \returns \p Default and emits error if one of the requested values cannot be
187 /// converted to integer, or \p OnlyFirstRequired is false and "second" value is
189 std::pair<int, int> getIntegerPairAttribute(const Function &F,
191 std::pair<int, int> Default,
192 bool OnlyFirstRequired = false);
194 /// \returns Vmcnt bit mask for given isa \p Version.
195 unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version);
197 /// \returns Expcnt bit mask for given isa \p Version.
198 unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version);
200 /// \returns Lgkmcnt bit mask for given isa \p Version.
201 unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version);
203 /// \returns Waitcnt bit mask for given isa \p Version.
204 unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version);
206 /// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
207 unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
209 /// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
210 unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
212 /// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
213 unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
215 /// \brief Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
216 /// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
217 /// \p Lgkmcnt respectively.
219 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
220 /// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9 only)
221 /// \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14] (gfx9+ only)
222 /// \p Expcnt = \p Waitcnt[6:4]
223 /// \p Lgkmcnt = \p Waitcnt[11:8]
224 void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
225 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
227 /// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
228 unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
231 /// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
232 unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
235 /// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
236 unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
239 /// \brief Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
242 /// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
243 /// Waitcnt[3:0] = \p Vmcnt (pre-gfx9 only)
244 /// Waitcnt[3:0] = \p Vmcnt[3:0] (gfx9+ only)
245 /// Waitcnt[6:4] = \p Expcnt
246 /// Waitcnt[11:8] = \p Lgkmcnt
247 /// Waitcnt[15:14] = \p Vmcnt[5:4] (gfx9+ only)
249 /// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
251 unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
252 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
254 unsigned getInitialPSInputAddr(const Function &F);
256 bool isShader(CallingConv::ID cc);
257 bool isCompute(CallingConv::ID cc);
259 bool isSI(const MCSubtargetInfo &STI);
260 bool isCI(const MCSubtargetInfo &STI);
261 bool isVI(const MCSubtargetInfo &STI);
263 /// If \p Reg is a pseudo reg, return the correct hardware register given
264 /// \p STI otherwise return \p Reg.
265 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
267 /// \brief Convert hardware register \p Reg to a pseudo register
269 unsigned mc2PseudoReg(unsigned Reg);
271 /// \brief Can this operand also contain immediate values?
272 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
274 /// \brief Is this floating-point operand?
275 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
277 /// \brief Does this opearnd support only inlinable literals?
278 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
280 /// \brief Get the size in bits of a register from the register class \p RC.
281 unsigned getRegBitWidth(unsigned RCID);
283 /// \brief Get the size in bits of a register from the register class \p RC.
284 unsigned getRegBitWidth(const MCRegisterClass &RC);
286 /// \brief Get size of register operand
287 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
291 inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
292 switch (OpInfo.OperandType) {
293 case AMDGPU::OPERAND_REG_IMM_INT32:
294 case AMDGPU::OPERAND_REG_IMM_FP32:
295 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
296 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
299 case AMDGPU::OPERAND_REG_IMM_INT64:
300 case AMDGPU::OPERAND_REG_IMM_FP64:
301 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
302 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
305 case AMDGPU::OPERAND_REG_IMM_INT16:
306 case AMDGPU::OPERAND_REG_IMM_FP16:
307 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
308 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
309 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
310 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
314 llvm_unreachable("unhandled operand type");
319 inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
320 return getOperandSize(Desc.OpInfo[OpNo]);
323 /// \brief Is this literal inlinable
325 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
328 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
331 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
334 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
336 bool isUniformMMO(const MachineMemOperand *MMO);
338 /// \returns The encoding that will be used for \p ByteOffset in the SMRD
340 int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
342 /// \returns true if this offset is small enough to fit in the SMRD
343 /// offset field. \p ByteOffset should be the offset in bytes and
344 /// not the encoded offset.
345 bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
347 } // end namespace AMDGPU
348 } // end namespace llvm
350 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H