blob: ea9dcdf8f3c2a745648939d5b854d9e7f59de6be [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
Matt Arsenault894e53d2017-07-26 20:39:42 +000026class Argument;
Tom Stellard347ac792015-06-26 21:15:07 +000027class FeatureBitset;
Tom Stellardac00eb52015-12-15 16:26:16 +000028class Function;
Tom Stellarde3b5aea2015-12-02 17:00:42 +000029class GlobalValue;
Tom Stellard08efb7e2017-01-27 18:41:14 +000030class MachineMemOperand;
Tom Stellarde135ffd2015-09-25 21:41:28 +000031class MCContext;
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +000032class MCRegisterClass;
Sam Kolton1eeb11b2016-09-09 14:44:04 +000033class MCRegisterInfo;
Tom Stellarde135ffd2015-09-25 21:41:28 +000034class MCSection;
Tom Stellard2b65ed32015-12-21 18:44:27 +000035class MCSubtargetInfo;
Eugene Zelenkod96089b2017-02-14 00:33:36 +000036class Triple;
Tom Stellard347ac792015-06-26 21:15:07 +000037
38namespace AMDGPU {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000039namespace IsaInfo {
Sam Koltona3ec5c12016-10-07 14:46:06 +000040
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000041enum {
42 // The closed Vulkan driver sets 96, which limits the wave count to 8 but
43 // doesn't spill SGPRs as much as when 80 is set.
44 FIXED_NUM_SGPRS_FOR_INIT_BUG = 96
45};
46
47/// \brief Instruction set architecture version.
Tom Stellard347ac792015-06-26 21:15:07 +000048struct IsaVersion {
49 unsigned Major;
50 unsigned Minor;
51 unsigned Stepping;
52};
53
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000054/// \returns Isa version for given subtarget \p Features.
Tom Stellard347ac792015-06-26 21:15:07 +000055IsaVersion getIsaVersion(const FeatureBitset &Features);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +000056
57/// \returns Wavefront size for given subtarget \p Features.
58unsigned getWavefrontSize(const FeatureBitset &Features);
59
60/// \returns Local memory size in bytes for given subtarget \p Features.
61unsigned getLocalMemorySize(const FeatureBitset &Features);
62
63/// \returns Number of execution units per compute unit for given subtarget \p
64/// Features.
65unsigned getEUsPerCU(const FeatureBitset &Features);
66
67/// \returns Maximum number of work groups per compute unit for given subtarget
68/// \p Features and limited by given \p FlatWorkGroupSize.
69unsigned getMaxWorkGroupsPerCU(const FeatureBitset &Features,
70 unsigned FlatWorkGroupSize);
71
72/// \returns Maximum number of waves per compute unit for given subtarget \p
73/// Features without any kind of limitation.
74unsigned getMaxWavesPerCU(const FeatureBitset &Features);
75
76/// \returns Maximum number of waves per compute unit for given subtarget \p
77/// Features and limited by given \p FlatWorkGroupSize.
78unsigned getMaxWavesPerCU(const FeatureBitset &Features,
79 unsigned FlatWorkGroupSize);
80
81/// \returns Minimum number of waves per execution unit for given subtarget \p
82/// Features.
83unsigned getMinWavesPerEU(const FeatureBitset &Features);
84
85/// \returns Maximum number of waves per execution unit for given subtarget \p
86/// Features without any kind of limitation.
87unsigned getMaxWavesPerEU(const FeatureBitset &Features);
88
89/// \returns Maximum number of waves per execution unit for given subtarget \p
90/// Features and limited by given \p FlatWorkGroupSize.
91unsigned getMaxWavesPerEU(const FeatureBitset &Features,
92 unsigned FlatWorkGroupSize);
93
94/// \returns Minimum flat work group size for given subtarget \p Features.
95unsigned getMinFlatWorkGroupSize(const FeatureBitset &Features);
96
97/// \returns Maximum flat work group size for given subtarget \p Features.
98unsigned getMaxFlatWorkGroupSize(const FeatureBitset &Features);
99
100/// \returns Number of waves per work group for given subtarget \p Features and
101/// limited by given \p FlatWorkGroupSize.
102unsigned getWavesPerWorkGroup(const FeatureBitset &Features,
103 unsigned FlatWorkGroupSize);
104
105/// \returns SGPR allocation granularity for given subtarget \p Features.
106unsigned getSGPRAllocGranule(const FeatureBitset &Features);
107
108/// \returns SGPR encoding granularity for given subtarget \p Features.
109unsigned getSGPREncodingGranule(const FeatureBitset &Features);
110
111/// \returns Total number of SGPRs for given subtarget \p Features.
112unsigned getTotalNumSGPRs(const FeatureBitset &Features);
113
114/// \returns Addressable number of SGPRs for given subtarget \p Features.
115unsigned getAddressableNumSGPRs(const FeatureBitset &Features);
116
117/// \returns Minimum number of SGPRs that meets the given number of waves per
118/// execution unit requirement for given subtarget \p Features.
119unsigned getMinNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
120
121/// \returns Maximum number of SGPRs that meets the given number of waves per
122/// execution unit requirement for given subtarget \p Features.
123unsigned getMaxNumSGPRs(const FeatureBitset &Features, unsigned WavesPerEU,
124 bool Addressable);
125
126/// \returns VGPR allocation granularity for given subtarget \p Features.
127unsigned getVGPRAllocGranule(const FeatureBitset &Features);
128
129/// \returns VGPR encoding granularity for given subtarget \p Features.
130unsigned getVGPREncodingGranule(const FeatureBitset &Features);
131
132/// \returns Total number of VGPRs for given subtarget \p Features.
133unsigned getTotalNumVGPRs(const FeatureBitset &Features);
134
135/// \returns Addressable number of VGPRs for given subtarget \p Features.
136unsigned getAddressableNumVGPRs(const FeatureBitset &Features);
137
138/// \returns Minimum number of VGPRs that meets given number of waves per
139/// execution unit requirement for given subtarget \p Features.
140unsigned getMinNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
141
142/// \returns Maximum number of VGPRs that meets given number of waves per
143/// execution unit requirement for given subtarget \p Features.
144unsigned getMaxNumVGPRs(const FeatureBitset &Features, unsigned WavesPerEU);
145
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000146} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000147
148LLVM_READONLY
149int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx);
150
Tom Stellardff7416b2015-06-26 21:58:31 +0000151void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
152 const FeatureBitset &Features);
Tom Stellard9760f032015-12-03 03:34:32 +0000153
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000154bool isGroupSegment(const GlobalValue *GV, AMDGPUAS AS);
155bool isGlobalSegment(const GlobalValue *GV, AMDGPUAS AS);
156bool isReadOnlySegment(const GlobalValue *GV, AMDGPUAS AS);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000157
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000158/// \returns True if constants should be emitted to .text section for given
159/// target triple \p TT, false otherwise.
160bool shouldEmitConstantsToTextSection(const Triple &TT);
161
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000162/// \returns Integer value requested using \p F's \p Name attribute.
163///
164/// \returns \p Default if attribute is not present.
165///
166/// \returns \p Default and emits error if requested value cannot be converted
167/// to integer.
Matt Arsenault83002722016-05-12 02:45:18 +0000168int getIntegerAttribute(const Function &F, StringRef Name, int Default);
169
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000170/// \returns A pair of integer values requested using \p F's \p Name attribute
171/// in "first[,second]" format ("second" is optional unless \p OnlyFirstRequired
172/// is false).
173///
174/// \returns \p Default if attribute is not present.
175///
176/// \returns \p Default and emits error if one of the requested values cannot be
177/// converted to integer, or \p OnlyFirstRequired is false and "second" value is
178/// not present.
179std::pair<int, int> getIntegerPairAttribute(const Function &F,
180 StringRef Name,
181 std::pair<int, int> Default,
182 bool OnlyFirstRequired = false);
183
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000184/// \returns Vmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000185unsigned getVmcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000186
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000187/// \returns Expcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000188unsigned getExpcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000189
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000190/// \returns Lgkmcnt bit mask for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000191unsigned getLgkmcntBitMask(const IsaInfo::IsaVersion &Version);
192
193/// \returns Waitcnt bit mask for given isa \p Version.
194unsigned getWaitcntBitMask(const IsaInfo::IsaVersion &Version);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000195
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000196/// \returns Decoded Vmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000197unsigned decodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000198
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000199/// \returns Decoded Expcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000200unsigned decodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000201
202/// \returns Decoded Lgkmcnt from given \p Waitcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000203unsigned decodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000204
205/// \brief Decodes Vmcnt, Expcnt and Lgkmcnt from given \p Waitcnt for given isa
206/// \p Version, and writes decoded values into \p Vmcnt, \p Expcnt and
207/// \p Lgkmcnt respectively.
208///
209/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are decoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000210/// \p Vmcnt = \p Waitcnt[3:0] (pre-gfx9 only)
211/// \p Vmcnt = \p Waitcnt[3:0] | \p Waitcnt[15:14] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000212/// \p Expcnt = \p Waitcnt[6:4]
213/// \p Lgkmcnt = \p Waitcnt[11:8]
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000214void decodeWaitcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000215 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt);
216
217/// \returns \p Waitcnt with encoded \p Vmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000218unsigned encodeVmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
219 unsigned Vmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000220
221/// \returns \p Waitcnt with encoded \p Expcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000222unsigned encodeExpcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
223 unsigned Expcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000224
225/// \returns \p Waitcnt with encoded \p Lgkmcnt for given isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000226unsigned encodeLgkmcnt(const IsaInfo::IsaVersion &Version, unsigned Waitcnt,
227 unsigned Lgkmcnt);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000228
229/// \brief Encodes \p Vmcnt, \p Expcnt and \p Lgkmcnt into Waitcnt for given isa
230/// \p Version.
231///
232/// \details \p Vmcnt, \p Expcnt and \p Lgkmcnt are encoded as follows:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000233/// Waitcnt[3:0] = \p Vmcnt (pre-gfx9 only)
234/// Waitcnt[3:0] = \p Vmcnt[3:0] (gfx9+ only)
235/// Waitcnt[6:4] = \p Expcnt
236/// Waitcnt[11:8] = \p Lgkmcnt
237/// Waitcnt[15:14] = \p Vmcnt[5:4] (gfx9+ only)
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000238///
239/// \returns Waitcnt with encoded \p Vmcnt, \p Expcnt and \p Lgkmcnt for given
240/// isa \p Version.
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000241unsigned encodeWaitcnt(const IsaInfo::IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000242 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt);
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000243
Marek Olsakfccabaf2016-01-13 11:45:36 +0000244unsigned getInitialPSInputAddr(const Function &F);
245
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000246LLVM_READNONE
247bool isShader(CallingConv::ID CC);
248
249LLVM_READNONE
250bool isCompute(CallingConv::ID CC);
251
252LLVM_READNONE
253bool isEntryFunctionCC(CallingConv::ID CC);
254
Matt Arsenaultefa9f4b2017-04-11 22:29:28 +0000255// FIXME: Remove this when calling conventions cleaned up
256LLVM_READNONE
257inline bool isKernel(CallingConv::ID CC) {
258 switch (CC) {
Matt Arsenaultefa9f4b2017-04-11 22:29:28 +0000259 case CallingConv::AMDGPU_KERNEL:
260 case CallingConv::SPIR_KERNEL:
261 return true;
262 default:
263 return false;
264 }
265}
Tom Stellardac00eb52015-12-15 16:26:16 +0000266
Tom Stellard2b65ed32015-12-21 18:44:27 +0000267bool isSI(const MCSubtargetInfo &STI);
268bool isCI(const MCSubtargetInfo &STI);
269bool isVI(const MCSubtargetInfo &STI);
Sam Koltonf7659d712017-05-23 10:08:55 +0000270bool isGFX9(const MCSubtargetInfo &STI);
271
272/// \brief Is Reg - scalar register
273bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI);
Tom Stellard2b65ed32015-12-21 18:44:27 +0000274
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000275/// \brief Is there any intersection between registers
276bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI);
277
Tom Stellard2b65ed32015-12-21 18:44:27 +0000278/// If \p Reg is a pseudo reg, return the correct hardware register given
279/// \p STI otherwise return \p Reg.
280unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI);
281
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000282/// \brief Convert hardware register \p Reg to a pseudo register
283LLVM_READNONE
284unsigned mc2PseudoReg(unsigned Reg);
285
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000286/// \brief Can this operand also contain immediate values?
287bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo);
288
289/// \brief Is this floating-point operand?
290bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo);
291
292/// \brief Does this opearnd support only inlinable literals?
293bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo);
294
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000295/// \brief Get the size in bits of a register from the register class \p RC.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000296unsigned getRegBitWidth(unsigned RCID);
297
298/// \brief Get the size in bits of a register from the register class \p RC.
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000299unsigned getRegBitWidth(const MCRegisterClass &RC);
300
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000301/// \brief Get size of register operand
302unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
303 unsigned OpNo);
304
Matt Arsenault4bd72362016-12-10 00:39:12 +0000305LLVM_READNONE
306inline unsigned getOperandSize(const MCOperandInfo &OpInfo) {
307 switch (OpInfo.OperandType) {
308 case AMDGPU::OPERAND_REG_IMM_INT32:
309 case AMDGPU::OPERAND_REG_IMM_FP32:
310 case AMDGPU::OPERAND_REG_INLINE_C_INT32:
311 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
312 return 4;
313
314 case AMDGPU::OPERAND_REG_IMM_INT64:
315 case AMDGPU::OPERAND_REG_IMM_FP64:
316 case AMDGPU::OPERAND_REG_INLINE_C_INT64:
317 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
318 return 8;
319
320 case AMDGPU::OPERAND_REG_IMM_INT16:
321 case AMDGPU::OPERAND_REG_IMM_FP16:
322 case AMDGPU::OPERAND_REG_INLINE_C_INT16:
323 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000324 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16:
325 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000326 return 2;
327
328 default:
329 llvm_unreachable("unhandled operand type");
330 }
331}
332
333LLVM_READNONE
334inline unsigned getOperandSize(const MCInstrDesc &Desc, unsigned OpNo) {
335 return getOperandSize(Desc.OpInfo[OpNo]);
336}
337
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000338/// \brief Is this literal inlinable
Matt Arsenault26faed32016-12-05 22:26:17 +0000339LLVM_READNONE
340bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi);
341
342LLVM_READNONE
343bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi);
344
Matt Arsenault4bd72362016-12-10 00:39:12 +0000345LLVM_READNONE
346bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000347
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000348LLVM_READNONE
349bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi);
350
Matt Arsenault894e53d2017-07-26 20:39:42 +0000351bool isArgPassedInSGPR(const Argument *Arg);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000352bool isUniformMMO(const MachineMemOperand *MMO);
353
354/// \returns The encoding that will be used for \p ByteOffset in the SMRD
355/// offset field.
356int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
357
358/// \returns true if this offset is small enough to fit in the SMRD
359/// offset field. \p ByteOffset should be the offset in bytes and
360/// not the encoded offset.
361bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset);
362
Tom Stellard347ac792015-06-26 21:15:07 +0000363} // end namespace AMDGPU
364} // end namespace llvm
365
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000366#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUBASEINFO_H