blob: 4a43ecda1778593b50cb785149340f64100b0c77 [file] [log] [blame]
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001//===- AMDGPUBaseInfo.h - Top level definitions for AMDGPU ------*- C++ -*-===//
Tom Stellard347ac792015-06-26 21:15:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
11#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H
12
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000013#include "AMDGPU.h"
Tom Stellard347ac792015-06-26 21:15:07 +000014#include "AMDKernelCodeT.h"
Matt Arsenault4bd72362016-12-10 00:39:12 +000015#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000016#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"
21#include <cstdint>
22#include <utility>
Matt Arsenault4bd72362016-12-10 00:39:12 +000023
Tom Stellard347ac792015-06-26 21:15:07 +000024namespace llvm {
25
26class FeatureBitset;
Tom Stellardac00eb52015-12-15 16:26:16 +000027class Function;
Tom Stellarde3b5aea2015-12-02 17:00:42 +000028class GlobalValue;
Tom Stellard08efb7e2017-01-27 18:41:14 +000029class MachineMemOperand;
Tom Stellarde135ffd2015-09-25 21:41:28 +000030class MCContext;
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +000031class MCRegisterClass;
Sam Kolton1eeb11b2016-09-09 14:44:04 +000032class MCRegisterInfo;
Tom Stellarde135ffd2015-09-25 21:41:28 +000033class MCSection;
Tom Stellard2b65ed32015-12-21 18:44:27 +000034class MCSubtargetInfo;
Eugene Zelenkod96089b2017-02-14 00:33:36 +000035class Triple;
Tom Stellard347ac792015-06-26 21:15:07 +000036
37namespace AMDGPU {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000038namespace IsaInfo {
Sam Koltona3ec5c12016-10-07 14:46:06 +000039
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000040enum {
41 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
42 // doesn't spill SGPRs as much as when 80 is set.
43 FIXED_NUM_SGPRS_FOR_INIT_BUG = 96
44};
45
46/// \brief Instruction set architecture version.
Tom Stellard347ac792015-06-26 21:15:07 +000047struct IsaVersion {
48 unsigned Major;
49 unsigned Minor;
50 unsigned Stepping;
51};
52
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000053/// \returns Isa version for given subtarget \p Features.
Tom Stellard347ac792015-06-26 21:15:07 +000054IsaVersion getIsaVersion(const FeatureBitset &Features);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000055
56/// \returns Wavefront size for given subtarget \p Features.
57unsigned getWavefrontSize(const FeatureBitset &Features);
58
59/// \returns Local memory size in bytes for given subtarget \p Features.
60unsigned getLocalMemorySize(const FeatureBitset &Features);
61
62/// \returns Number of execution units per compute unit for given subtarget \p
63/// Features.
64unsigned getEUsPerCU(const FeatureBitset &Features);
65
66/// \returns Maximum number of work groups per compute unit for given subtarget
67/// \p Features and limited by given \p FlatWorkGroupSize.
68unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
69 unsigned FlatWorkGroupSize);
70
71/// \returns Maximum number of waves per compute unit for given subtarget \p
72/// Features without any kind of limitation.
73unsigned getMaxWavesPerCU(const FeatureBitset &Features);
74
75/// \returns Maximum number of waves per compute unit for given subtarget \p
76/// Features and limited by given \p FlatWorkGroupSize.
77unsigned getMaxWavesPerCU(const FeatureBitset &Features,
78 unsigned FlatWorkGroupSize);
79
80/// \returns Minimum number of waves per execution unit for given subtarget \p
81/// Features.
82unsigned getMinWavesPerEU(const FeatureBitset &Features);
83
84/// \returns Maximum number of waves per execution unit for given subtarget \p
85/// Features without any kind of limitation.
86unsigned getMaxWavesPerEU(const FeatureBitset &Features);
87
88/// \returns Maximum number of waves per execution unit for given subtarget \p
89/// Features and limited by given \p FlatWorkGroupSize.
90unsigned getMaxWavesPerEU(const FeatureBitset &Features,
91 unsigned FlatWorkGroupSize);
92
93/// \returns Minimum flat work group size for given subtarget \p Features.
94unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features);
95
96/// \returns Maximum flat work group size for given subtarget \p Features.
97unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features);
98
99/// \returns Number of waves per work group for given subtarget \p Features and
100/// limited by given \p FlatWorkGroupSize.
101unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
102 unsigned FlatWorkGroupSize);
103
104/// \returns SGPR allocation granularity for given subtarget \p Features.
105unsigned getSGPRAllocGranule(const FeatureBitset &Features);
106
107/// \returns SGPR encoding granularity for given subtarget \p Features.
108unsigned getSGPREncodingGranule(const FeatureBitset &Features);
109
110/// \returns Total number of SGPRs for given subtarget \p Features.
111unsigned getTotalNumSGPRs(const FeatureBitset &Features);
112
113/// \returns Addressable number of SGPRs for given subtarget \p Features.
114unsigned getAddressableNumSGPRs(const FeatureBitset &Features);
115
116/// \returns Minimum number of SGPRs that meets the given number of waves per
117/// execution unit requirement for given subtarget \p Features.
118unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
119
120/// \returns Maximum number of SGPRs that meets the given number of waves per
121/// execution unit requirement for given subtarget \p Features.
122unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
123 bool Addressable);
124
125/// \returns VGPR allocation granularity for given subtarget \p Features.
126unsigned getVGPRAllocGranule(const FeatureBitset &Features);
127
128/// \returns VGPR encoding granularity for given subtarget \p Features.
129unsigned getVGPREncodingGranule(const FeatureBitset &Features);
130
131/// \returns Total number of VGPRs for given subtarget \p Features.
132unsigned getTotalNumVGPRs(const FeatureBitset &Features);
133
134/// \returns Addressable number of VGPRs for given subtarget \p Features.
135unsigned getAddressableNumVGPRs(const FeatureBitset &Features);
136
137/// \returns Minimum number of VGPRs that meets given number of waves per
138/// execution unit requirement for given subtarget \p Features.
139unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
140
141/// \returns Maximum number of VGPRs that meets given number of waves per
142/// execution unit requirement for given subtarget \p Features.
143unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
144
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000145} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000146
147LLVM_READONLY
148int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
149
Tom Stellardff7416b2015-06-26 21:58:31 +0000150void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
151 const FeatureBitset &Features);
Tom Stellarde135ffd2015-09-25 21:41:28 +0000152MCSection *getHSATextSection(MCContext &Ctx);
Tom Stellard347ac792015-06-26 21:15:07 +0000153
Tom Stellard00f2f912015-12-02 19:47:57 +0000154MCSection *getHSADataGlobalAgentSection(MCContext &Ctx);
155
156MCSection *getHSADataGlobalProgramSection(MCContext &Ctx);
157
Tom Stellard9760f032015-12-03 03:34:32 +0000158MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx);
159
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000160bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS);
161bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS);
162bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000163
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000164/// \returns True if constants should be emitted to .text section for given
165/// target triple \p TT, false otherwise.
166bool shouldEmitConstantsToTextSection(const Triple &TT);
167
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000168/// \returns Integer value requested using \p F's \p Name attribute.
169///
170/// \returns \p Default if attribute is not present.
171///
172/// \returns \p Default and emits error if requested value cannot be converted
173/// to integer.
Matt Arsenault83002722016-05-12 02:45:18 +0000174int getIntegerAttribute(const Function &F, StringRef Name, int Default);
175
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000176/// \returns A pair of integer values requested using \p F's \p Name attribute
177/// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
178/// is false).
179///
180/// \returns \p Default if attribute is not present.
181///
182/// \returns \p Default and emits error if one of the requested values cannot be
183/// converted to integer, or \p OnlyFirstRequired is false and "second" value is
184/// not present.
185std::pair<int, int> getIntegerPairAttribute(const Function &F,
186 StringRef Name,
187 std::pair<int, int> Default,
188 bool OnlyFirstRequired = false);
189
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000190/// \returns Vmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000191unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000192
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000193/// \returns Expcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000194unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000195
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000196/// \returns Lgkmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000197unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version);
198
199/// \returns Waitcnt bit mask for given isa \p Version.
200unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000201
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000202/// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000203unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000204
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000205/// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000206unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000207
208/// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000209unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000210
211/// \brief Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
212/// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
213/// \p Lgkmcnt respectively.
214///
215/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000216/// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9 only)
217/// \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000218/// \p Expcnt = \p Waitcnt[6:4]
219/// \p Lgkmcnt = \p Waitcnt[11:8]
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000220void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000221 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
222
223/// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000224unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
225 unsigned Vmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000226
227/// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000228unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
229 unsigned Expcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000230
231/// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000232unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
233 unsigned Lgkmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000234
235/// \brief Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
236/// \p Version.
237///
238/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000239/// Waitcnt[3:0] = \p Vmcnt (pre-gfx9 only)
240/// Waitcnt[3:0] = \p Vmcnt[3:0] (gfx9+ only)
241/// Waitcnt[6:4] = \p Expcnt
242/// Waitcnt[11:8] = \p Lgkmcnt
243/// Waitcnt[15:14] = \p Vmcnt[5:4] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000244///
245/// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
246/// isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000247unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000248 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000249
Marek Olsakfccabaf2016-01-13 11:45:36 +0000250unsigned getInitialPSInputAddr(const Function &F);
251
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000252LLVM_READNONE
253bool isShader(CallingConv::ID CC);
254
255LLVM_READNONE
256bool isCompute(CallingConv::ID CC);
257
258LLVM_READNONE
259bool isEntryFunctionCC(CallingConv::ID CC);
260
Tom Stellardac00eb52015-12-15 16:26:16 +0000261
Tom Stellard2b65ed32015-12-21 18:44:27 +0000262bool isSI(const MCSubtargetInfo &STI);
263bool isCI(const MCSubtargetInfo &STI);
264bool isVI(const MCSubtargetInfo &STI);
265
266/// If \p Reg is a pseudo reg, return the correct hardware register given
267/// \p STI otherwise return \p Reg.
268unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
269
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000270/// \brief Convert hardware register \p Reg to a pseudo register
271LLVM_READNONE
272unsigned mc2PseudoReg(unsigned Reg);
273
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000274/// \brief Can this operand also contain immediate values?
275bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
276
277/// \brief Is this floating-point operand?
278bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
279
280/// \brief Does this opearnd support only inlinable literals?
281bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
282
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000283/// \brief Get the size in bits of a register from the register class \p RC.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000284unsigned getRegBitWidth(unsigned RCID);
285
286/// \brief Get the size in bits of a register from the register class \p RC.
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000287unsigned getRegBitWidth(const MCRegisterClass &RC);
288
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000289/// \brief Get size of register operand
290unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
291 unsigned OpNo);
292
Matt Arsenault4bd72362016-12-10 00:39:12 +0000293LLVM_READNONE
294inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
295 switch (OpInfo.OperandType) {
296 case AMDGPU::OPERAND_REG_IMM_INT32:
297 case AMDGPU::OPERAND_REG_IMM_FP32:
298 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
299 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
300 return 4;
301
302 case AMDGPU::OPERAND_REG_IMM_INT64:
303 case AMDGPU::OPERAND_REG_IMM_FP64:
304 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
305 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
306 return 8;
307
308 case AMDGPU::OPERAND_REG_IMM_INT16:
309 case AMDGPU::OPERAND_REG_IMM_FP16:
310 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
311 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000312 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
313 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000314 return 2;
315
316 default:
317 llvm_unreachable("unhandled operand type");
318 }
319}
320
321LLVM_READNONE
322inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
323 return getOperandSize(Desc.OpInfo[OpNo]);
324}
325
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000326/// \brief Is this literal inlinable
Matt Arsenault26faed32016-12-05 22:26:17 +0000327LLVM_READNONE
328bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
329
330LLVM_READNONE
331bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
332
Matt Arsenault4bd72362016-12-10 00:39:12 +0000333LLVM_READNONE
334bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000335
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000336LLVM_READNONE
337bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
338
Tom Stellard08efb7e2017-01-27 18:41:14 +0000339bool isUniformMMO(const MachineMemOperand *MMO);
340
341/// \returns The encoding that will be used for \p ByteOffset in the SMRD
342/// offset field.
343int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
344
345/// \returns true if this offset is small enough to fit in the SMRD
346/// offset field. \p ByteOffset should be the offset in bytes and
347/// not the encoded offset.
348bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
349
Tom Stellard347ac792015-06-26 21:15:07 +0000350} // end namespace AMDGPU
351} // end namespace llvm
352
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000353#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H