blob: 8fcac3fdb693b6ce2f3cef36784e23c2ad3c90ac [file] [log] [blame]
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001//===- AMDGPUBaseInfo.cpp - AMDGPU Base encoding information --------------===//
Tom Stellard347ac792015-06-26 21:15:07 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellard347ac792015-06-26 21:15:07 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenkod96089b2017-02-14 00:33:36 +00008
Eugene Zelenkod96089b2017-02-14 00:33:36 +00009#include "AMDGPUBaseInfo.h"
Alexander Timofeev2e5eece2018-03-05 15:12:21 +000010#include "AMDGPUTargetTransformInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "AMDGPU.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000012#include "SIDefines.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000013#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/ELF.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000016#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/IR/Attributes.h"
Tom Stellard08efb7e2017-01-27 18:41:14 +000018#include "llvm/IR/Constants.h"
Tom Stellardac00eb52015-12-15 16:26:16 +000019#include "llvm/IR/Function.h"
Tom Stellarde3b5aea2015-12-02 17:00:42 +000020#include "llvm/IR/GlobalValue.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000021#include "llvm/IR/Instruction.h"
Tom Stellardca166212017-01-30 21:56:46 +000022#include "llvm/IR/LLVMContext.h"
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000023#include "llvm/IR/Module.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000024#include "llvm/MC/MCContext.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000025#include "llvm/MC/MCInstrDesc.h"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000026#include "llvm/MC/MCInstrInfo.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000027#include "llvm/MC/MCRegisterInfo.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000028#include "llvm/MC/MCSectionELF.h"
Tom Stellard2b65ed32015-12-21 18:44:27 +000029#include "llvm/MC/MCSubtargetInfo.h"
Tom Stellard347ac792015-06-26 21:15:07 +000030#include "llvm/MC/SubtargetFeature.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/MathExtras.h"
34#include <algorithm>
35#include <cassert>
36#include <cstdint>
37#include <cstring>
38#include <utility>
Tom Stellard347ac792015-06-26 21:15:07 +000039
Matt Arsenault678e1112017-04-10 17:58:06 +000040#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Tom Stellard347ac792015-06-26 21:15:07 +000041
Sam Koltona3ec5c12016-10-07 14:46:06 +000042#define GET_INSTRINFO_NAMED_OPS
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000043#define GET_INSTRMAP_INFO
Sam Koltona3ec5c12016-10-07 14:46:06 +000044#include "AMDGPUGenInstrInfo.inc"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +000045#undef GET_INSTRMAP_INFO
Sam Koltona3ec5c12016-10-07 14:46:06 +000046#undef GET_INSTRINFO_NAMED_OPS
Sam Koltona3ec5c12016-10-07 14:46:06 +000047
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000048namespace {
49
50/// \returns Bit mask for given bit \p Shift and bit \p Width.
51unsigned getBitMask(unsigned Shift, unsigned Width) {
52 return ((1 << Width) - 1) << Shift;
53}
54
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000055/// Packs \p Src into \p Dst for given bit \p Shift and bit \p Width.
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000056///
57/// \returns Packed \p Dst.
58unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) {
59 Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width);
60 Dst |= (Src << Shift) & getBitMask(Shift, Width);
61 return Dst;
62}
63
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000064/// Unpacks bits from \p Src for given bit \p Shift and bit \p Width.
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000065///
66/// \returns Unpacked bits.
67unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) {
68 return (Src & getBitMask(Shift, Width)) >> Shift;
69}
70
Matt Arsenaulte823d922017-02-18 18:29:53 +000071/// \returns Vmcnt bit shift (lower bits).
72unsigned getVmcntBitShiftLo() { return 0; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000073
Matt Arsenaulte823d922017-02-18 18:29:53 +000074/// \returns Vmcnt bit width (lower bits).
75unsigned getVmcntBitWidthLo() { return 4; }
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000076
77/// \returns Expcnt bit shift.
78unsigned getExpcntBitShift() { return 4; }
79
80/// \returns Expcnt bit width.
81unsigned getExpcntBitWidth() { return 3; }
82
83/// \returns Lgkmcnt bit shift.
84unsigned getLgkmcntBitShift() { return 8; }
85
86/// \returns Lgkmcnt bit width.
87unsigned getLgkmcntBitWidth() { return 4; }
88
Matt Arsenaulte823d922017-02-18 18:29:53 +000089/// \returns Vmcnt bit shift (higher bits).
90unsigned getVmcntBitShiftHi() { return 14; }
91
92/// \returns Vmcnt bit width (higher bits).
93unsigned getVmcntBitWidthHi() { return 2; }
94
Eugene Zelenkod96089b2017-02-14 00:33:36 +000095} // end namespace anonymous
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000096
Tom Stellard347ac792015-06-26 21:15:07 +000097namespace llvm {
Konstantin Zhuravlyov3d1cc882017-04-21 19:45:22 +000098
Tom Stellard347ac792015-06-26 21:15:07 +000099namespace AMDGPU {
100
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000101struct MIMGInfo {
102 uint16_t Opcode;
103 uint16_t BaseOpcode;
104 uint8_t MIMGEncoding;
105 uint8_t VDataDwords;
106 uint8_t VAddrDwords;
107};
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000108
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000109#define GET_MIMGBaseOpcodesTable_IMPL
110#define GET_MIMGDimInfoTable_IMPL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000111#define GET_MIMGInfoTable_IMPL
Ryan Taylor894c8fd2018-08-01 12:12:01 +0000112#define GET_MIMGLZMappingTable_IMPL
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000113#include "AMDGPUGenSearchableTables.inc"
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000114
Nicolai Haehnle7a9c03f2018-06-21 13:36:57 +0000115int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding,
116 unsigned VDataDwords, unsigned VAddrDwords) {
117 const MIMGInfo *Info = getMIMGOpcodeHelper(BaseOpcode, MIMGEncoding,
118 VDataDwords, VAddrDwords);
119 return Info ? Info->Opcode : -1;
120}
121
Nicolai Haehnle0ab200b2018-06-21 13:36:44 +0000122int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels) {
123 const MIMGInfo *OrigInfo = getMIMGInfo(Opc);
124 const MIMGInfo *NewInfo =
125 getMIMGOpcodeHelper(OrigInfo->BaseOpcode, OrigInfo->MIMGEncoding,
126 NewChannels, OrigInfo->VAddrDwords);
127 return NewInfo ? NewInfo->Opcode : -1;
Dmitry Preobrazhensky0b4eb1e2018-01-26 15:43:29 +0000128}
129
Neil Henning76504a42018-12-12 16:15:21 +0000130struct MUBUFInfo {
131 uint16_t Opcode;
132 uint16_t BaseOpcode;
133 uint8_t dwords;
134 bool has_vaddr;
135 bool has_srsrc;
136 bool has_soffset;
137};
138
139#define GET_MUBUFInfoTable_DECL
140#define GET_MUBUFInfoTable_IMPL
141#include "AMDGPUGenSearchableTables.inc"
142
143int getMUBUFBaseOpcode(unsigned Opc) {
144 const MUBUFInfo *Info = getMUBUFInfoFromOpcode(Opc);
145 return Info ? Info->BaseOpcode : -1;
146}
147
148int getMUBUFOpcode(unsigned BaseOpc, unsigned Dwords) {
149 const MUBUFInfo *Info = getMUBUFInfoFromBaseOpcodeAndDwords(BaseOpc, Dwords);
150 return Info ? Info->Opcode : -1;
151}
152
153int getMUBUFDwords(unsigned Opc) {
154 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc);
155 return Info ? Info->dwords : 0;
156}
157
158bool getMUBUFHasVAddr(unsigned Opc) {
159 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc);
160 return Info ? Info->has_vaddr : false;
161}
162
163bool getMUBUFHasSrsrc(unsigned Opc) {
164 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc);
165 return Info ? Info->has_srsrc : false;
166}
167
168bool getMUBUFHasSoffset(unsigned Opc) {
169 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc);
170 return Info ? Info->has_soffset : false;
171}
172
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000173// Wrapper for Tablegen'd function. enum Subtarget is not defined in any
174// header files, so we need to wrap it in a function that takes unsigned
175// instead.
176int getMCOpcode(uint16_t Opcode, unsigned Gen) {
177 return getMCOpcodeGen(Opcode, static_cast<Subtarget>(Gen));
178}
179
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000180namespace IsaInfo {
Tom Stellard347ac792015-06-26 21:15:07 +0000181
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000182void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream) {
183 auto TargetTriple = STI->getTargetTriple();
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000184 auto Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000185
186 Stream << TargetTriple.getArchName() << '-'
187 << TargetTriple.getVendorName() << '-'
188 << TargetTriple.getOSName() << '-'
189 << TargetTriple.getEnvironmentName() << '-'
190 << "gfx"
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000191 << Version.Major
192 << Version.Minor
193 << Version.Stepping;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000194
195 if (hasXNACK(*STI))
196 Stream << "+xnack";
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000197 if (hasSRAMECC(*STI))
198 Stream << "+sram-ecc";
Scott Linder1e8c2c72018-06-21 19:38:56 +0000199
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000200 Stream.flush();
201}
202
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000203bool hasCodeObjectV3(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyovaf7b5d72018-11-15 23:14:23 +0000204 return STI->getTargetTriple().getOS() == Triple::AMDHSA &&
205 STI->getFeatureBits().test(FeatureCodeObjectV3);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000206}
207
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000208unsigned getWavefrontSize(const MCSubtargetInfo *STI) {
209 if (STI->getFeatureBits().test(FeatureWavefrontSize16))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000210 return 16;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000211 if (STI->getFeatureBits().test(FeatureWavefrontSize32))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000212 return 32;
213
214 return 64;
215}
216
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000217unsigned getLocalMemorySize(const MCSubtargetInfo *STI) {
218 if (STI->getFeatureBits().test(FeatureLocalMemorySize32768))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000219 return 32768;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000220 if (STI->getFeatureBits().test(FeatureLocalMemorySize65536))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000221 return 65536;
222
223 return 0;
224}
225
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000226unsigned getEUsPerCU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000227 return 4;
228}
229
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000230unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000231 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000232 if (!STI->getFeatureBits().test(FeatureGCN))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000233 return 8;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000234 unsigned N = getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000235 if (N == 1)
236 return 40;
237 N = 40 / N;
238 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000239}
240
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000241unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI) {
242 return getMaxWavesPerEU() * getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000243}
244
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000245unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000246 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000247 return getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000248}
249
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000250unsigned getMinWavesPerEU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000251 return 1;
252}
253
Tom Stellardc5a154d2018-06-28 23:47:12 +0000254unsigned getMaxWavesPerEU() {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000255 // FIXME: Need to take scratch memory into account.
256 return 10;
257}
258
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000259unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000260 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000261 return alignTo(getMaxWavesPerCU(STI, FlatWorkGroupSize),
262 getEUsPerCU(STI)) / getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000263}
264
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000265unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000266 return 1;
267}
268
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000269unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000270 return 2048;
271}
272
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000273unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000274 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000275 return alignTo(FlatWorkGroupSize, getWavefrontSize(STI)) /
276 getWavefrontSize(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000277}
278
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000279unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI) {
280 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000281 if (Version.Major >= 8)
282 return 16;
283 return 8;
284}
285
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000286unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000287 return 8;
288}
289
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000290unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI) {
291 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000292 if (Version.Major >= 8)
293 return 800;
294 return 512;
295}
296
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000297unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI) {
298 if (STI->getFeatureBits().test(FeatureSGPRInitBug))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000299 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
300
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000301 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000302 if (Version.Major >= 8)
303 return 102;
304 return 104;
305}
306
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000307unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000308 assert(WavesPerEU != 0);
309
Tom Stellardc5a154d2018-06-28 23:47:12 +0000310 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000311 return 0;
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000312
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000313 unsigned MinNumSGPRs = getTotalNumSGPRs(STI) / (WavesPerEU + 1);
314 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000315 MinNumSGPRs -= std::min(MinNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000316 MinNumSGPRs = alignDown(MinNumSGPRs, getSGPRAllocGranule(STI)) + 1;
317 return std::min(MinNumSGPRs, getAddressableNumSGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000318}
319
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000320unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000321 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000322 assert(WavesPerEU != 0);
323
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000324 IsaVersion Version = getIsaVersion(STI->getCPU());
325 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000326 if (Version.Major >= 8 && !Addressable)
327 AddressableNumSGPRs = 112;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000328 unsigned MaxNumSGPRs = getTotalNumSGPRs(STI) / WavesPerEU;
329 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000330 MaxNumSGPRs -= std::min(MaxNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000331 MaxNumSGPRs = alignDown(MaxNumSGPRs, getSGPRAllocGranule(STI));
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000332 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000333}
334
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000335unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000336 bool FlatScrUsed, bool XNACKUsed) {
337 unsigned ExtraSGPRs = 0;
338 if (VCCUsed)
339 ExtraSGPRs = 2;
340
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000341 IsaVersion Version = getIsaVersion(STI->getCPU());
Scott Linder1e8c2c72018-06-21 19:38:56 +0000342 if (Version.Major < 8) {
343 if (FlatScrUsed)
344 ExtraSGPRs = 4;
345 } else {
346 if (XNACKUsed)
347 ExtraSGPRs = 4;
348
349 if (FlatScrUsed)
350 ExtraSGPRs = 6;
351 }
352
353 return ExtraSGPRs;
354}
355
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000356unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000357 bool FlatScrUsed) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000358 return getNumExtraSGPRs(STI, VCCUsed, FlatScrUsed,
359 STI->getFeatureBits().test(AMDGPU::FeatureXNACK));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000360}
361
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000362unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs) {
363 NumSGPRs = alignTo(std::max(1u, NumSGPRs), getSGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000364 // SGPRBlocks is actual number of SGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000365 return NumSGPRs / getSGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000366}
367
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000368unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000369 return 4;
370}
371
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000372unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI) {
373 return getVGPRAllocGranule(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000374}
375
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000376unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000377 return 256;
378}
379
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000380unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI) {
381 return getTotalNumVGPRs(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000382}
383
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000384unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000385 assert(WavesPerEU != 0);
386
Tom Stellardc5a154d2018-06-28 23:47:12 +0000387 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000388 return 0;
389 unsigned MinNumVGPRs =
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000390 alignDown(getTotalNumVGPRs(STI) / (WavesPerEU + 1),
391 getVGPRAllocGranule(STI)) + 1;
392 return std::min(MinNumVGPRs, getAddressableNumVGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000393}
394
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000395unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000396 assert(WavesPerEU != 0);
397
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000398 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(STI) / WavesPerEU,
399 getVGPRAllocGranule(STI));
400 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000401 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000402}
403
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000404unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumVGPRs) {
405 NumVGPRs = alignTo(std::max(1u, NumVGPRs), getVGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000406 // VGPRBlocks is actual number of VGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000407 return NumVGPRs / getVGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000408}
409
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000410} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000411
Tom Stellardff7416b2015-06-26 21:58:31 +0000412void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000413 const MCSubtargetInfo *STI) {
414 IsaVersion Version = getIsaVersion(STI->getCPU());
Tom Stellardff7416b2015-06-26 21:58:31 +0000415
416 memset(&Header, 0, sizeof(Header));
417
418 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov61830652018-04-09 20:47:22 +0000419 Header.amd_kernel_code_version_minor = 2;
Tom Stellardff7416b2015-06-26 21:58:31 +0000420 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000421 Header.amd_machine_version_major = Version.Major;
422 Header.amd_machine_version_minor = Version.Minor;
423 Header.amd_machine_version_stepping = Version.Stepping;
Tom Stellardff7416b2015-06-26 21:58:31 +0000424 Header.kernel_code_entry_byte_offset = sizeof(Header);
425 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
426 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000427
428 // If the code object does not support indirect functions, then the value must
429 // be 0xffffffff.
430 Header.call_convention = -1;
431
Tom Stellardff7416b2015-06-26 21:58:31 +0000432 // These alignment values are specified in powers of two, so alignment =
433 // 2^n. The minimum alignment is 2^4 = 16.
434 Header.kernarg_segment_alignment = 4;
435 Header.group_segment_alignment = 4;
436 Header.private_segment_alignment = 4;
437}
438
Scott Linder1e8c2c72018-06-21 19:38:56 +0000439amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor() {
440 amdhsa::kernel_descriptor_t KD;
441 memset(&KD, 0, sizeof(KD));
442 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
443 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64,
444 amdhsa::FLOAT_DENORM_MODE_FLUSH_NONE);
445 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
446 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP, 1);
447 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
448 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE, 1);
449 AMDHSA_BITS_SET(KD.compute_pgm_rsrc2,
450 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X, 1);
451 return KD;
452}
453
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000454bool isGroupSegment(const GlobalValue *GV) {
455 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000456}
457
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000458bool isGlobalSegment(const GlobalValue *GV) {
459 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000460}
461
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000462bool isReadOnlySegment(const GlobalValue *GV) {
Matt Arsenault923712b2018-02-09 16:57:57 +0000463 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
464 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellard00f2f912015-12-02 19:47:57 +0000465}
466
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000467bool shouldEmitConstantsToTextSection(const Triple &TT) {
468 return TT.getOS() != Triple::AMDHSA;
469}
470
Matt Arsenault83002722016-05-12 02:45:18 +0000471int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000472 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000473 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000474
475 if (A.isStringAttribute()) {
476 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000477 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000478 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000479 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000480 }
481 }
Matt Arsenault83002722016-05-12 02:45:18 +0000482
Marek Olsakfccabaf2016-01-13 11:45:36 +0000483 return Result;
484}
485
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000486std::pair<int, int> getIntegerPairAttribute(const Function &F,
487 StringRef Name,
488 std::pair<int, int> Default,
489 bool OnlyFirstRequired) {
490 Attribute A = F.getFnAttribute(Name);
491 if (!A.isStringAttribute())
492 return Default;
493
494 LLVMContext &Ctx = F.getContext();
495 std::pair<int, int> Ints = Default;
496 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
497 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
498 Ctx.emitError("can't parse first integer attribute " + Name);
499 return Default;
500 }
501 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000502 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000503 Ctx.emitError("can't parse second integer attribute " + Name);
504 return Default;
505 }
506 }
507
508 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000509}
510
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000511unsigned getVmcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000512 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
513 if (Version.Major < 9)
514 return VmcntLo;
515
516 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
517 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000518}
519
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000520unsigned getExpcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000521 return (1 << getExpcntBitWidth()) - 1;
522}
523
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000524unsigned getLgkmcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000525 return (1 << getLgkmcntBitWidth()) - 1;
526}
527
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000528unsigned getWaitcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000529 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000530 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
531 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000532 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
533 if (Version.Major < 9)
534 return Waitcnt;
535
536 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
537 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000538}
539
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000540unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000541 unsigned VmcntLo =
542 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
543 if (Version.Major < 9)
544 return VmcntLo;
545
546 unsigned VmcntHi =
547 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
548 VmcntHi <<= getVmcntBitWidthLo();
549 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000550}
551
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000552unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000553 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
554}
555
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000556unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000557 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
558}
559
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000560void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000561 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
562 Vmcnt = decodeVmcnt(Version, Waitcnt);
563 Expcnt = decodeExpcnt(Version, Waitcnt);
564 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
565}
566
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000567Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded) {
568 Waitcnt Decoded;
569 Decoded.VmCnt = decodeVmcnt(Version, Encoded);
570 Decoded.ExpCnt = decodeExpcnt(Version, Encoded);
571 Decoded.LgkmCnt = decodeLgkmcnt(Version, Encoded);
572 return Decoded;
573}
574
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000575unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000576 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000577 Waitcnt =
578 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
579 if (Version.Major < 9)
580 return Waitcnt;
581
582 Vmcnt >>= getVmcntBitWidthLo();
583 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000584}
585
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000586unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000587 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000588 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
589}
590
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000591unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000592 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000593 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
594}
595
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000596unsigned encodeWaitcnt(const IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000597 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000598 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000599 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
600 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
601 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
602 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000603}
604
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000605unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded) {
606 return encodeWaitcnt(Version, Decoded.VmCnt, Decoded.ExpCnt, Decoded.LgkmCnt);
607}
608
Marek Olsakfccabaf2016-01-13 11:45:36 +0000609unsigned getInitialPSInputAddr(const Function &F) {
610 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000611}
612
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000613bool isShader(CallingConv::ID cc) {
614 switch(cc) {
615 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000616 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000617 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000618 case CallingConv::AMDGPU_ES:
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000619 case CallingConv::AMDGPU_GS:
620 case CallingConv::AMDGPU_PS:
621 case CallingConv::AMDGPU_CS:
622 return true;
623 default:
624 return false;
625 }
626}
627
628bool isCompute(CallingConv::ID cc) {
629 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
630}
631
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000632bool isEntryFunctionCC(CallingConv::ID CC) {
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000633 switch (CC) {
634 case CallingConv::AMDGPU_KERNEL:
635 case CallingConv::SPIR_KERNEL:
636 case CallingConv::AMDGPU_VS:
637 case CallingConv::AMDGPU_GS:
638 case CallingConv::AMDGPU_PS:
639 case CallingConv::AMDGPU_CS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000640 case CallingConv::AMDGPU_ES:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000641 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000642 case CallingConv::AMDGPU_LS:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000643 return true;
644 default:
645 return false;
646 }
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000647}
648
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000649bool hasXNACK(const MCSubtargetInfo &STI) {
650 return STI.getFeatureBits()[AMDGPU::FeatureXNACK];
651}
652
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000653bool hasSRAMECC(const MCSubtargetInfo &STI) {
654 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC];
655}
656
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000657bool hasMIMG_R128(const MCSubtargetInfo &STI) {
658 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128];
659}
660
Dmitry Preobrazhensky0a1ff462018-02-05 14:18:53 +0000661bool hasPackedD16(const MCSubtargetInfo &STI) {
662 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem];
663}
664
Tom Stellard2b65ed32015-12-21 18:44:27 +0000665bool isSI(const MCSubtargetInfo &STI) {
666 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
667}
668
669bool isCI(const MCSubtargetInfo &STI) {
670 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
671}
672
673bool isVI(const MCSubtargetInfo &STI) {
674 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
675}
676
Sam Koltonf7659d712017-05-23 10:08:55 +0000677bool isGFX9(const MCSubtargetInfo &STI) {
678 return STI.getFeatureBits()[AMDGPU::FeatureGFX9];
679}
680
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000681bool isGCN3Encoding(const MCSubtargetInfo &STI) {
682 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding];
683}
684
Sam Koltonf7659d712017-05-23 10:08:55 +0000685bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) {
686 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID);
687 const unsigned FirstSubReg = TRI->getSubReg(Reg, 1);
688 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) ||
689 Reg == AMDGPU::SCC;
690}
691
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000692bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI) {
Dmitry Preobrazhensky00deef82017-07-18 11:14:02 +0000693 for (MCRegAliasIterator R(Reg0, TRI, true); R.isValid(); ++R) {
694 if (*R == Reg1) return true;
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000695 }
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000696 return false;
697}
698
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000699#define MAP_REG2REG \
700 using namespace AMDGPU; \
701 switch(Reg) { \
702 default: return Reg; \
703 CASE_CI_VI(FLAT_SCR) \
704 CASE_CI_VI(FLAT_SCR_LO) \
705 CASE_CI_VI(FLAT_SCR_HI) \
706 CASE_VI_GFX9(TTMP0) \
707 CASE_VI_GFX9(TTMP1) \
708 CASE_VI_GFX9(TTMP2) \
709 CASE_VI_GFX9(TTMP3) \
710 CASE_VI_GFX9(TTMP4) \
711 CASE_VI_GFX9(TTMP5) \
712 CASE_VI_GFX9(TTMP6) \
713 CASE_VI_GFX9(TTMP7) \
714 CASE_VI_GFX9(TTMP8) \
715 CASE_VI_GFX9(TTMP9) \
716 CASE_VI_GFX9(TTMP10) \
717 CASE_VI_GFX9(TTMP11) \
718 CASE_VI_GFX9(TTMP12) \
719 CASE_VI_GFX9(TTMP13) \
720 CASE_VI_GFX9(TTMP14) \
721 CASE_VI_GFX9(TTMP15) \
722 CASE_VI_GFX9(TTMP0_TTMP1) \
723 CASE_VI_GFX9(TTMP2_TTMP3) \
724 CASE_VI_GFX9(TTMP4_TTMP5) \
725 CASE_VI_GFX9(TTMP6_TTMP7) \
726 CASE_VI_GFX9(TTMP8_TTMP9) \
727 CASE_VI_GFX9(TTMP10_TTMP11) \
728 CASE_VI_GFX9(TTMP12_TTMP13) \
729 CASE_VI_GFX9(TTMP14_TTMP15) \
730 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3) \
731 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7) \
732 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11) \
733 CASE_VI_GFX9(TTMP12_TTMP13_TTMP14_TTMP15) \
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000734 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \
735 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \
736 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
737 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
Tom Stellard2b65ed32015-12-21 18:44:27 +0000738 }
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000739
740#define CASE_CI_VI(node) \
741 assert(!isSI(STI)); \
742 case node: return isCI(STI) ? node##_ci : node##_vi;
743
744#define CASE_VI_GFX9(node) \
745 case node: return isGFX9(STI) ? node##_gfx9 : node##_vi;
746
747unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000748 if (STI.getTargetTriple().getArch() == Triple::r600)
749 return Reg;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000750 MAP_REG2REG
Tom Stellard2b65ed32015-12-21 18:44:27 +0000751}
752
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000753#undef CASE_CI_VI
754#undef CASE_VI_GFX9
755
756#define CASE_CI_VI(node) case node##_ci: case node##_vi: return node;
757#define CASE_VI_GFX9(node) case node##_vi: case node##_gfx9: return node;
758
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000759unsigned mc2PseudoReg(unsigned Reg) {
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000760 MAP_REG2REG
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000761}
762
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000763#undef CASE_CI_VI
764#undef CASE_VI_GFX9
765#undef MAP_REG2REG
766
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000767bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000768 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000769 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000770 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
771 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000772}
773
774bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000775 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000776 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000777 switch (OpType) {
778 case AMDGPU::OPERAND_REG_IMM_FP32:
779 case AMDGPU::OPERAND_REG_IMM_FP64:
780 case AMDGPU::OPERAND_REG_IMM_FP16:
781 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
782 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
783 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000784 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000785 return true;
786 default:
787 return false;
788 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000789}
790
791bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000792 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000793 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000794 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
795 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000796}
797
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000798// Avoid using MCRegisterClass::getSize, since that function will go away
799// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000800unsigned getRegBitWidth(unsigned RCID) {
801 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000802 case AMDGPU::SGPR_32RegClassID:
803 case AMDGPU::VGPR_32RegClassID:
804 case AMDGPU::VS_32RegClassID:
805 case AMDGPU::SReg_32RegClassID:
806 case AMDGPU::SReg_32_XM0RegClassID:
807 return 32;
808 case AMDGPU::SGPR_64RegClassID:
809 case AMDGPU::VS_64RegClassID:
810 case AMDGPU::SReg_64RegClassID:
811 case AMDGPU::VReg_64RegClassID:
Ron Liebermancac749a2018-11-16 01:13:34 +0000812 case AMDGPU::SReg_64_XEXECRegClassID:
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000813 return 64;
814 case AMDGPU::VReg_96RegClassID:
815 return 96;
816 case AMDGPU::SGPR_128RegClassID:
817 case AMDGPU::SReg_128RegClassID:
818 case AMDGPU::VReg_128RegClassID:
819 return 128;
820 case AMDGPU::SReg_256RegClassID:
821 case AMDGPU::VReg_256RegClassID:
822 return 256;
823 case AMDGPU::SReg_512RegClassID:
824 case AMDGPU::VReg_512RegClassID:
825 return 512;
826 default:
827 llvm_unreachable("Unexpected register class");
828 }
829}
830
Tom Stellardb133fbb2016-10-27 23:05:31 +0000831unsigned getRegBitWidth(const MCRegisterClass &RC) {
832 return getRegBitWidth(RC.getID());
833}
834
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000835unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
836 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000837 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000838 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
839 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000840}
841
Matt Arsenault26faed32016-12-05 22:26:17 +0000842bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000843 if (Literal >= -16 && Literal <= 64)
844 return true;
845
Matt Arsenault26faed32016-12-05 22:26:17 +0000846 uint64_t Val = static_cast<uint64_t>(Literal);
847 return (Val == DoubleToBits(0.0)) ||
848 (Val == DoubleToBits(1.0)) ||
849 (Val == DoubleToBits(-1.0)) ||
850 (Val == DoubleToBits(0.5)) ||
851 (Val == DoubleToBits(-0.5)) ||
852 (Val == DoubleToBits(2.0)) ||
853 (Val == DoubleToBits(-2.0)) ||
854 (Val == DoubleToBits(4.0)) ||
855 (Val == DoubleToBits(-4.0)) ||
856 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000857}
858
Matt Arsenault26faed32016-12-05 22:26:17 +0000859bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000860 if (Literal >= -16 && Literal <= 64)
861 return true;
862
Matt Arsenault4bd72362016-12-10 00:39:12 +0000863 // The actual type of the operand does not seem to matter as long
864 // as the bits match one of the inline immediate values. For example:
865 //
866 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
867 // so it is a legal inline immediate.
868 //
869 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
870 // floating-point, so it is a legal inline immediate.
871
Matt Arsenault26faed32016-12-05 22:26:17 +0000872 uint32_t Val = static_cast<uint32_t>(Literal);
873 return (Val == FloatToBits(0.0f)) ||
874 (Val == FloatToBits(1.0f)) ||
875 (Val == FloatToBits(-1.0f)) ||
876 (Val == FloatToBits(0.5f)) ||
877 (Val == FloatToBits(-0.5f)) ||
878 (Val == FloatToBits(2.0f)) ||
879 (Val == FloatToBits(-2.0f)) ||
880 (Val == FloatToBits(4.0f)) ||
881 (Val == FloatToBits(-4.0f)) ||
882 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000883}
884
Matt Arsenault4bd72362016-12-10 00:39:12 +0000885bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000886 if (!HasInv2Pi)
887 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000888
889 if (Literal >= -16 && Literal <= 64)
890 return true;
891
892 uint16_t Val = static_cast<uint16_t>(Literal);
893 return Val == 0x3C00 || // 1.0
894 Val == 0xBC00 || // -1.0
895 Val == 0x3800 || // 0.5
896 Val == 0xB800 || // -0.5
897 Val == 0x4000 || // 2.0
898 Val == 0xC000 || // -2.0
899 Val == 0x4400 || // 4.0
900 Val == 0xC400 || // -4.0
901 Val == 0x3118; // 1/2pi
902}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000903
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000904bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
905 assert(HasInv2Pi);
906
907 int16_t Lo16 = static_cast<int16_t>(Literal);
908 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
909 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
910}
911
Matt Arsenault894e53d2017-07-26 20:39:42 +0000912bool isArgPassedInSGPR(const Argument *A) {
913 const Function *F = A->getParent();
914
915 // Arguments to compute shaders are never a source of divergence.
916 CallingConv::ID CC = F->getCallingConv();
917 switch (CC) {
918 case CallingConv::AMDGPU_KERNEL:
919 case CallingConv::SPIR_KERNEL:
920 return true;
921 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000922 case CallingConv::AMDGPU_LS:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000923 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000924 case CallingConv::AMDGPU_ES:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000925 case CallingConv::AMDGPU_GS:
926 case CallingConv::AMDGPU_PS:
927 case CallingConv::AMDGPU_CS:
928 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
929 // Everything else is in VGPRs.
930 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
931 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
932 default:
933 // TODO: Should calls support inreg for SGPR inputs?
934 return false;
935 }
936}
937
Tom Stellard08efb7e2017-01-27 18:41:14 +0000938int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000939 if (isGCN3Encoding(ST))
940 return ByteOffset;
941 return ByteOffset >> 2;
Tom Stellard08efb7e2017-01-27 18:41:14 +0000942}
943
944bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
945 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000946 return isGCN3Encoding(ST) ?
947 isUInt<20>(EncodedOffset) : isUInt<8>(EncodedOffset);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000948}
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000949
Tim Renouf4f703f52018-08-21 11:07:10 +0000950// Given Imm, split it into the values to put into the SOffset and ImmOffset
951// fields in an MUBUF instruction. Return false if it is not possible (due to a
952// hardware bug needing a workaround).
Nicolai Haehnlea7b00052018-11-30 22:55:38 +0000953//
954// The required alignment ensures that individual address components remain
955// aligned if they are aligned to begin with. It also ensures that additional
956// offsets within the given alignment can be added to the resulting ImmOffset.
Tim Renouf4f703f52018-08-21 11:07:10 +0000957bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
Nicolai Haehnlea7b00052018-11-30 22:55:38 +0000958 const GCNSubtarget *Subtarget, uint32_t Align) {
Tim Renouf4f703f52018-08-21 11:07:10 +0000959 const uint32_t MaxImm = alignDown(4095, Align);
960 uint32_t Overflow = 0;
961
962 if (Imm > MaxImm) {
963 if (Imm <= MaxImm + 64) {
964 // Use an SOffset inline constant for 4..64
965 Overflow = Imm - MaxImm;
966 Imm = MaxImm;
967 } else {
968 // Try to keep the same value in SOffset for adjacent loads, so that
969 // the corresponding register contents can be re-used.
970 //
971 // Load values with all low-bits (except for alignment bits) set into
972 // SOffset, so that a larger range of values can be covered using
973 // s_movk_i32.
974 //
975 // Atomic operations fail to work correctly when individual address
976 // components are unaligned, even if their sum is aligned.
977 uint32_t High = (Imm + Align) & ~4095;
978 uint32_t Low = (Imm + Align) & 4095;
979 Imm = Low;
980 Overflow = High - Align;
981 }
982 }
983
984 // There is a hardware bug in SI and CI which prevents address clamping in
985 // MUBUF instructions from working correctly with SOffsets. The immediate
986 // offset is unaffected.
987 if (Overflow > 0 &&
988 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
989 return false;
990
991 ImmOffset = Imm;
992 SOffset = Overflow;
993 return true;
994}
995
Nicolai Haehnle4254d452018-04-01 17:09:14 +0000996namespace {
997
998struct SourceOfDivergence {
999 unsigned Intr;
1000};
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001001const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr);
Nicolai Haehnle4254d452018-04-01 17:09:14 +00001002
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001003#define GET_SourcesOfDivergence_IMPL
Nicolai Haehnle4254d452018-04-01 17:09:14 +00001004#include "AMDGPUGenSearchableTables.inc"
1005
1006} // end anonymous namespace
1007
Alexander Timofeev2e5eece2018-03-05 15:12:21 +00001008bool isIntrinsicSourceOfDivergence(unsigned IntrID) {
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001009 return lookupSourceOfDivergence(IntrID);
Alexander Timofeev2e5eece2018-03-05 15:12:21 +00001010}
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001011} // namespace AMDGPU
1012} // namespace llvm