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