blob: b397554e76dc9a38979b8eef17998ba0d5379182 [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) {
Matt Arsenaultd7047272019-02-08 19:18:01 +0000232 assert(FlatWorkGroupSize != 0);
233 if (STI->getTargetTriple().getArch() != Triple::amdgcn)
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000234 return 8;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000235 unsigned N = getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Stanislav Mekhanoshin19f98c62017-02-15 01:03:59 +0000236 if (N == 1)
237 return 40;
238 N = 40 / N;
239 return std::min(N, 16u);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000240}
241
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000242unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI) {
243 return getMaxWavesPerEU() * getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000244}
245
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000246unsigned getMaxWavesPerCU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000247 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000248 return getWavesPerWorkGroup(STI, FlatWorkGroupSize);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000249}
250
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000251unsigned getMinWavesPerEU(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000252 return 1;
253}
254
Tom Stellardc5a154d2018-06-28 23:47:12 +0000255unsigned getMaxWavesPerEU() {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000256 // FIXME: Need to take scratch memory into account.
257 return 10;
258}
259
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000260unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000261 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000262 return alignTo(getMaxWavesPerCU(STI, FlatWorkGroupSize),
263 getEUsPerCU(STI)) / getEUsPerCU(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000264}
265
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000266unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000267 return 1;
268}
269
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000270unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000271 return 2048;
272}
273
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000274unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000275 unsigned FlatWorkGroupSize) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000276 return alignTo(FlatWorkGroupSize, getWavefrontSize(STI)) /
277 getWavefrontSize(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000278}
279
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000280unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI) {
281 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000282 if (Version.Major >= 8)
283 return 16;
284 return 8;
285}
286
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000287unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000288 return 8;
289}
290
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000291unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI) {
292 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000293 if (Version.Major >= 8)
294 return 800;
295 return 512;
296}
297
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000298unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI) {
299 if (STI->getFeatureBits().test(FeatureSGPRInitBug))
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000300 return FIXED_NUM_SGPRS_FOR_INIT_BUG;
301
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000302 IsaVersion Version = getIsaVersion(STI->getCPU());
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000303 if (Version.Major >= 8)
304 return 102;
305 return 104;
306}
307
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000308unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000309 assert(WavesPerEU != 0);
310
Tom Stellardc5a154d2018-06-28 23:47:12 +0000311 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000312 return 0;
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000313
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000314 unsigned MinNumSGPRs = getTotalNumSGPRs(STI) / (WavesPerEU + 1);
315 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000316 MinNumSGPRs -= std::min(MinNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000317 MinNumSGPRs = alignDown(MinNumSGPRs, getSGPRAllocGranule(STI)) + 1;
318 return std::min(MinNumSGPRs, getAddressableNumSGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000319}
320
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000321unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000322 bool Addressable) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000323 assert(WavesPerEU != 0);
324
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000325 IsaVersion Version = getIsaVersion(STI->getCPU());
326 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000327 if (Version.Major >= 8 && !Addressable)
328 AddressableNumSGPRs = 112;
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000329 unsigned MaxNumSGPRs = getTotalNumSGPRs(STI) / WavesPerEU;
330 if (STI->getFeatureBits().test(FeatureTrapHandler))
Konstantin Zhuravlyovc72ece62018-05-16 20:47:48 +0000331 MaxNumSGPRs -= std::min(MaxNumSGPRs, (unsigned)TRAP_NUM_SGPRS);
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000332 MaxNumSGPRs = alignDown(MaxNumSGPRs, getSGPRAllocGranule(STI));
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000333 return std::min(MaxNumSGPRs, AddressableNumSGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000334}
335
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000336unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000337 bool FlatScrUsed, bool XNACKUsed) {
338 unsigned ExtraSGPRs = 0;
339 if (VCCUsed)
340 ExtraSGPRs = 2;
341
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000342 IsaVersion Version = getIsaVersion(STI->getCPU());
Scott Linder1e8c2c72018-06-21 19:38:56 +0000343 if (Version.Major < 8) {
344 if (FlatScrUsed)
345 ExtraSGPRs = 4;
346 } else {
347 if (XNACKUsed)
348 ExtraSGPRs = 4;
349
350 if (FlatScrUsed)
351 ExtraSGPRs = 6;
352 }
353
354 return ExtraSGPRs;
355}
356
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000357unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000358 bool FlatScrUsed) {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000359 return getNumExtraSGPRs(STI, VCCUsed, FlatScrUsed,
360 STI->getFeatureBits().test(AMDGPU::FeatureXNACK));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000361}
362
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000363unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs) {
364 NumSGPRs = alignTo(std::max(1u, NumSGPRs), getSGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000365 // SGPRBlocks is actual number of SGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000366 return NumSGPRs / getSGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000367}
368
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000369unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000370 return 4;
371}
372
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000373unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI) {
374 return getVGPRAllocGranule(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000375}
376
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000377unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000378 return 256;
379}
380
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000381unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI) {
382 return getTotalNumVGPRs(STI);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000383}
384
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000385unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000386 assert(WavesPerEU != 0);
387
Tom Stellardc5a154d2018-06-28 23:47:12 +0000388 if (WavesPerEU >= getMaxWavesPerEU())
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000389 return 0;
390 unsigned MinNumVGPRs =
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000391 alignDown(getTotalNumVGPRs(STI) / (WavesPerEU + 1),
392 getVGPRAllocGranule(STI)) + 1;
393 return std::min(MinNumVGPRs, getAddressableNumVGPRs(STI));
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000394}
395
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000396unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) {
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000397 assert(WavesPerEU != 0);
398
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000399 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(STI) / WavesPerEU,
400 getVGPRAllocGranule(STI));
401 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(STI);
Konstantin Zhuravlyovfd871372017-02-09 21:33:23 +0000402 return std::min(MaxNumVGPRs, AddressableNumVGPRs);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000403}
404
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000405unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumVGPRs) {
406 NumVGPRs = alignTo(std::max(1u, NumVGPRs), getVGPREncodingGranule(STI));
Scott Linder1e8c2c72018-06-21 19:38:56 +0000407 // VGPRBlocks is actual number of VGPR blocks minus 1.
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000408 return NumVGPRs / getVGPREncodingGranule(STI) - 1;
Scott Linder1e8c2c72018-06-21 19:38:56 +0000409}
410
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000411} // end namespace IsaInfo
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000412
Tom Stellardff7416b2015-06-26 21:58:31 +0000413void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000414 const MCSubtargetInfo *STI) {
415 IsaVersion Version = getIsaVersion(STI->getCPU());
Tom Stellardff7416b2015-06-26 21:58:31 +0000416
417 memset(&Header, 0, sizeof(Header));
418
419 Header.amd_kernel_code_version_major = 1;
Konstantin Zhuravlyov61830652018-04-09 20:47:22 +0000420 Header.amd_kernel_code_version_minor = 2;
Tom Stellardff7416b2015-06-26 21:58:31 +0000421 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000422 Header.amd_machine_version_major = Version.Major;
423 Header.amd_machine_version_minor = Version.Minor;
424 Header.amd_machine_version_stepping = Version.Stepping;
Tom Stellardff7416b2015-06-26 21:58:31 +0000425 Header.kernel_code_entry_byte_offset = sizeof(Header);
426 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
427 Header.wavefront_size = 6;
Matt Arsenault5d910192017-01-25 20:21:57 +0000428
429 // If the code object does not support indirect functions, then the value must
430 // be 0xffffffff.
431 Header.call_convention = -1;
432
Tom Stellardff7416b2015-06-26 21:58:31 +0000433 // These alignment values are specified in powers of two, so alignment =
434 // 2^n. The minimum alignment is 2^4 = 16.
435 Header.kernarg_segment_alignment = 4;
436 Header.group_segment_alignment = 4;
437 Header.private_segment_alignment = 4;
438}
439
Scott Linder1e8c2c72018-06-21 19:38:56 +0000440amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor() {
441 amdhsa::kernel_descriptor_t KD;
442 memset(&KD, 0, sizeof(KD));
443 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
444 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64,
445 amdhsa::FLOAT_DENORM_MODE_FLUSH_NONE);
446 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
447 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP, 1);
448 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1,
449 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE, 1);
450 AMDHSA_BITS_SET(KD.compute_pgm_rsrc2,
451 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X, 1);
452 return KD;
453}
454
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000455bool isGroupSegment(const GlobalValue *GV) {
456 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000457}
458
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000459bool isGlobalSegment(const GlobalValue *GV) {
460 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
Tom Stellard00f2f912015-12-02 19:47:57 +0000461}
462
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000463bool isReadOnlySegment(const GlobalValue *GV) {
Matt Arsenault923712b2018-02-09 16:57:57 +0000464 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
465 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT;
Tom Stellard00f2f912015-12-02 19:47:57 +0000466}
467
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +0000468bool shouldEmitConstantsToTextSection(const Triple &TT) {
469 return TT.getOS() != Triple::AMDHSA;
470}
471
Matt Arsenault83002722016-05-12 02:45:18 +0000472int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000473 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000474 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000475
476 if (A.isStringAttribute()) {
477 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000478 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000479 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000480 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000481 }
482 }
Matt Arsenault83002722016-05-12 02:45:18 +0000483
Marek Olsakfccabaf2016-01-13 11:45:36 +0000484 return Result;
485}
486
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000487std::pair<int, int> getIntegerPairAttribute(const Function &F,
488 StringRef Name,
489 std::pair<int, int> Default,
490 bool OnlyFirstRequired) {
491 Attribute A = F.getFnAttribute(Name);
492 if (!A.isStringAttribute())
493 return Default;
494
495 LLVMContext &Ctx = F.getContext();
496 std::pair<int, int> Ints = Default;
497 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
498 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
499 Ctx.emitError("can't parse first integer attribute " + Name);
500 return Default;
501 }
502 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000503 if (!OnlyFirstRequired || !Strs.second.trim().empty()) {
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000504 Ctx.emitError("can't parse second integer attribute " + Name);
505 return Default;
506 }
507 }
508
509 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000510}
511
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000512unsigned getVmcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000513 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1;
514 if (Version.Major < 9)
515 return VmcntLo;
516
517 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo();
518 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000519}
520
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000521unsigned getExpcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000522 return (1 << getExpcntBitWidth()) - 1;
523}
524
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000525unsigned getLgkmcntBitMask(const IsaVersion &Version) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000526 return (1 << getLgkmcntBitWidth()) - 1;
527}
528
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000529unsigned getWaitcntBitMask(const IsaVersion &Version) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000530 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000531 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
532 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
Matt Arsenaulte823d922017-02-18 18:29:53 +0000533 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt;
534 if (Version.Major < 9)
535 return Waitcnt;
536
537 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi());
538 return Waitcnt | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000539}
540
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000541unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000542 unsigned VmcntLo =
543 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
544 if (Version.Major < 9)
545 return VmcntLo;
546
547 unsigned VmcntHi =
548 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
549 VmcntHi <<= getVmcntBitWidthLo();
550 return VmcntLo | VmcntHi;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000551}
552
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000553unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000554 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
555}
556
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000557unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000558 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
559}
560
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000561void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000562 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
563 Vmcnt = decodeVmcnt(Version, Waitcnt);
564 Expcnt = decodeExpcnt(Version, Waitcnt);
565 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
566}
567
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000568Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded) {
569 Waitcnt Decoded;
570 Decoded.VmCnt = decodeVmcnt(Version, Encoded);
571 Decoded.ExpCnt = decodeExpcnt(Version, Encoded);
572 Decoded.LgkmCnt = decodeLgkmcnt(Version, Encoded);
573 return Decoded;
574}
575
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000576unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000577 unsigned Vmcnt) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000578 Waitcnt =
579 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo());
580 if (Version.Major < 9)
581 return Waitcnt;
582
583 Vmcnt >>= getVmcntBitWidthLo();
584 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi());
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000585}
586
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000587unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000588 unsigned Expcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000589 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
590}
591
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000592unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000593 unsigned Lgkmcnt) {
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000594 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
595}
596
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000597unsigned encodeWaitcnt(const IsaVersion &Version,
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000598 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
Konstantin Zhuravlyov31dbb032017-01-06 17:23:21 +0000599 unsigned Waitcnt = getWaitcntBitMask(Version);
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000600 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
601 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
602 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
603 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000604}
605
Nicolai Haehnle1a94cbb2018-11-29 11:06:06 +0000606unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded) {
607 return encodeWaitcnt(Version, Decoded.VmCnt, Decoded.ExpCnt, Decoded.LgkmCnt);
608}
609
Marek Olsakfccabaf2016-01-13 11:45:36 +0000610unsigned getInitialPSInputAddr(const Function &F) {
611 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000612}
613
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000614bool isShader(CallingConv::ID cc) {
615 switch(cc) {
616 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000617 case CallingConv::AMDGPU_LS:
Marek Olsaka302a7362017-05-02 15:41:10 +0000618 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000619 case CallingConv::AMDGPU_ES:
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000620 case CallingConv::AMDGPU_GS:
621 case CallingConv::AMDGPU_PS:
622 case CallingConv::AMDGPU_CS:
623 return true;
624 default:
625 return false;
626 }
627}
628
629bool isCompute(CallingConv::ID cc) {
630 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
631}
632
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000633bool isEntryFunctionCC(CallingConv::ID CC) {
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000634 switch (CC) {
635 case CallingConv::AMDGPU_KERNEL:
636 case CallingConv::SPIR_KERNEL:
637 case CallingConv::AMDGPU_VS:
638 case CallingConv::AMDGPU_GS:
639 case CallingConv::AMDGPU_PS:
640 case CallingConv::AMDGPU_CS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000641 case CallingConv::AMDGPU_ES:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000642 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000643 case CallingConv::AMDGPU_LS:
Matt Arsenault2b1f9aa2017-05-17 21:56:25 +0000644 return true;
645 default:
646 return false;
647 }
Matt Arsenaulte622dc32017-04-11 22:29:24 +0000648}
649
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000650bool hasXNACK(const MCSubtargetInfo &STI) {
651 return STI.getFeatureBits()[AMDGPU::FeatureXNACK];
652}
653
Konstantin Zhuravlyov108927b2018-11-05 22:44:19 +0000654bool hasSRAMECC(const MCSubtargetInfo &STI) {
655 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC];
656}
657
Dmitry Preobrazhenskye3271ae2018-02-05 12:45:43 +0000658bool hasMIMG_R128(const MCSubtargetInfo &STI) {
659 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128];
660}
661
Dmitry Preobrazhensky0a1ff462018-02-05 14:18:53 +0000662bool hasPackedD16(const MCSubtargetInfo &STI) {
663 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem];
664}
665
Tom Stellard2b65ed32015-12-21 18:44:27 +0000666bool isSI(const MCSubtargetInfo &STI) {
667 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
668}
669
670bool isCI(const MCSubtargetInfo &STI) {
671 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
672}
673
674bool isVI(const MCSubtargetInfo &STI) {
675 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
676}
677
Sam Koltonf7659d712017-05-23 10:08:55 +0000678bool isGFX9(const MCSubtargetInfo &STI) {
679 return STI.getFeatureBits()[AMDGPU::FeatureGFX9];
680}
681
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000682bool isGCN3Encoding(const MCSubtargetInfo &STI) {
683 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding];
684}
685
Sam Koltonf7659d712017-05-23 10:08:55 +0000686bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) {
687 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID);
688 const unsigned FirstSubReg = TRI->getSubReg(Reg, 1);
689 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) ||
690 Reg == AMDGPU::SCC;
691}
692
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000693bool isRegIntersect(unsigned Reg0, unsigned Reg1, const MCRegisterInfo* TRI) {
Dmitry Preobrazhensky00deef82017-07-18 11:14:02 +0000694 for (MCRegAliasIterator R(Reg0, TRI, true); R.isValid(); ++R) {
695 if (*R == Reg1) return true;
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000696 }
Dmitry Preobrazhenskydc4ac822017-06-21 14:41:34 +0000697 return false;
698}
699
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000700#define MAP_REG2REG \
701 using namespace AMDGPU; \
702 switch(Reg) { \
703 default: return Reg; \
704 CASE_CI_VI(FLAT_SCR) \
705 CASE_CI_VI(FLAT_SCR_LO) \
706 CASE_CI_VI(FLAT_SCR_HI) \
707 CASE_VI_GFX9(TTMP0) \
708 CASE_VI_GFX9(TTMP1) \
709 CASE_VI_GFX9(TTMP2) \
710 CASE_VI_GFX9(TTMP3) \
711 CASE_VI_GFX9(TTMP4) \
712 CASE_VI_GFX9(TTMP5) \
713 CASE_VI_GFX9(TTMP6) \
714 CASE_VI_GFX9(TTMP7) \
715 CASE_VI_GFX9(TTMP8) \
716 CASE_VI_GFX9(TTMP9) \
717 CASE_VI_GFX9(TTMP10) \
718 CASE_VI_GFX9(TTMP11) \
719 CASE_VI_GFX9(TTMP12) \
720 CASE_VI_GFX9(TTMP13) \
721 CASE_VI_GFX9(TTMP14) \
722 CASE_VI_GFX9(TTMP15) \
723 CASE_VI_GFX9(TTMP0_TTMP1) \
724 CASE_VI_GFX9(TTMP2_TTMP3) \
725 CASE_VI_GFX9(TTMP4_TTMP5) \
726 CASE_VI_GFX9(TTMP6_TTMP7) \
727 CASE_VI_GFX9(TTMP8_TTMP9) \
728 CASE_VI_GFX9(TTMP10_TTMP11) \
729 CASE_VI_GFX9(TTMP12_TTMP13) \
730 CASE_VI_GFX9(TTMP14_TTMP15) \
731 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3) \
732 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7) \
733 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11) \
734 CASE_VI_GFX9(TTMP12_TTMP13_TTMP14_TTMP15) \
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000735 CASE_VI_GFX9(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \
736 CASE_VI_GFX9(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \
737 CASE_VI_GFX9(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \
738 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 +0000739 }
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000740
741#define CASE_CI_VI(node) \
742 assert(!isSI(STI)); \
743 case node: return isCI(STI) ? node##_ci : node##_vi;
744
745#define CASE_VI_GFX9(node) \
746 case node: return isGFX9(STI) ? node##_gfx9 : node##_vi;
747
748unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
Tom Stellardc5a154d2018-06-28 23:47:12 +0000749 if (STI.getTargetTriple().getArch() == Triple::r600)
750 return Reg;
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000751 MAP_REG2REG
Tom Stellard2b65ed32015-12-21 18:44:27 +0000752}
753
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000754#undef CASE_CI_VI
755#undef CASE_VI_GFX9
756
757#define CASE_CI_VI(node) case node##_ci: case node##_vi: return node;
758#define CASE_VI_GFX9(node) case node##_vi: case node##_gfx9: return node;
759
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000760unsigned mc2PseudoReg(unsigned Reg) {
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000761 MAP_REG2REG
Dmitry Preobrazhensky03880f82017-03-03 14:31:06 +0000762}
763
Dmitry Preobrazhenskyac2b0262017-12-11 15:23:20 +0000764#undef CASE_CI_VI
765#undef CASE_VI_GFX9
766#undef MAP_REG2REG
767
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000768bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000769 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000770 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000771 return OpType >= AMDGPU::OPERAND_SRC_FIRST &&
772 OpType <= AMDGPU::OPERAND_SRC_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000773}
774
775bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000776 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000777 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000778 switch (OpType) {
779 case AMDGPU::OPERAND_REG_IMM_FP32:
780 case AMDGPU::OPERAND_REG_IMM_FP64:
781 case AMDGPU::OPERAND_REG_IMM_FP16:
782 case AMDGPU::OPERAND_REG_INLINE_C_FP32:
783 case AMDGPU::OPERAND_REG_INLINE_C_FP64:
784 case AMDGPU::OPERAND_REG_INLINE_C_FP16:
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000785 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16:
Matt Arsenault4bd72362016-12-10 00:39:12 +0000786 return true;
787 default:
788 return false;
789 }
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000790}
791
792bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000793 assert(OpNo < Desc.NumOperands);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000794 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000795 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST &&
796 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000797}
798
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000799// Avoid using MCRegisterClass::getSize, since that function will go away
800// (move from MC* level to Target* level). Return size in bits.
Tom Stellardb133fbb2016-10-27 23:05:31 +0000801unsigned getRegBitWidth(unsigned RCID) {
802 switch (RCID) {
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000803 case AMDGPU::SGPR_32RegClassID:
804 case AMDGPU::VGPR_32RegClassID:
Dmitry Preobrazhensky6023d592019-03-04 12:48:32 +0000805 case AMDGPU::VRegOrLds_32RegClassID:
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000806 case AMDGPU::VS_32RegClassID:
807 case AMDGPU::SReg_32RegClassID:
808 case AMDGPU::SReg_32_XM0RegClassID:
Dmitry Preobrazhensky6023d592019-03-04 12:48:32 +0000809 case AMDGPU::SRegOrLds_32RegClassID:
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000810 return 32;
811 case AMDGPU::SGPR_64RegClassID:
812 case AMDGPU::VS_64RegClassID:
813 case AMDGPU::SReg_64RegClassID:
814 case AMDGPU::VReg_64RegClassID:
Ron Liebermancac749a2018-11-16 01:13:34 +0000815 case AMDGPU::SReg_64_XEXECRegClassID:
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000816 return 64;
Tim Renouf361b5b22019-03-21 12:01:21 +0000817 case AMDGPU::SGPR_96RegClassID:
818 case AMDGPU::SReg_96RegClassID:
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000819 case AMDGPU::VReg_96RegClassID:
820 return 96;
821 case AMDGPU::SGPR_128RegClassID:
822 case AMDGPU::SReg_128RegClassID:
823 case AMDGPU::VReg_128RegClassID:
824 return 128;
Tim Renouf033f99a2019-03-22 10:11:21 +0000825 case AMDGPU::SGPR_160RegClassID:
826 case AMDGPU::SReg_160RegClassID:
827 case AMDGPU::VReg_160RegClassID:
828 return 160;
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000829 case AMDGPU::SReg_256RegClassID:
830 case AMDGPU::VReg_256RegClassID:
831 return 256;
832 case AMDGPU::SReg_512RegClassID:
833 case AMDGPU::VReg_512RegClassID:
834 return 512;
835 default:
836 llvm_unreachable("Unexpected register class");
837 }
838}
839
Tom Stellardb133fbb2016-10-27 23:05:31 +0000840unsigned getRegBitWidth(const MCRegisterClass &RC) {
841 return getRegBitWidth(RC.getID());
842}
843
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000844unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
845 unsigned OpNo) {
Artem Tamazov43b61562017-02-03 12:47:30 +0000846 assert(OpNo < Desc.NumOperands);
Krzysztof Parzyszekc8715502016-10-19 17:40:36 +0000847 unsigned RCID = Desc.OpInfo[OpNo].RegClass;
848 return getRegBitWidth(MRI->getRegClass(RCID)) / 8;
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000849}
850
Matt Arsenault26faed32016-12-05 22:26:17 +0000851bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000852 if (Literal >= -16 && Literal <= 64)
853 return true;
854
Matt Arsenault26faed32016-12-05 22:26:17 +0000855 uint64_t Val = static_cast<uint64_t>(Literal);
856 return (Val == DoubleToBits(0.0)) ||
857 (Val == DoubleToBits(1.0)) ||
858 (Val == DoubleToBits(-1.0)) ||
859 (Val == DoubleToBits(0.5)) ||
860 (Val == DoubleToBits(-0.5)) ||
861 (Val == DoubleToBits(2.0)) ||
862 (Val == DoubleToBits(-2.0)) ||
863 (Val == DoubleToBits(4.0)) ||
864 (Val == DoubleToBits(-4.0)) ||
865 (Val == 0x3fc45f306dc9c882 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000866}
867
Matt Arsenault26faed32016-12-05 22:26:17 +0000868bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) {
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000869 if (Literal >= -16 && Literal <= 64)
870 return true;
871
Matt Arsenault4bd72362016-12-10 00:39:12 +0000872 // The actual type of the operand does not seem to matter as long
873 // as the bits match one of the inline immediate values. For example:
874 //
875 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal,
876 // so it is a legal inline immediate.
877 //
878 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in
879 // floating-point, so it is a legal inline immediate.
880
Matt Arsenault26faed32016-12-05 22:26:17 +0000881 uint32_t Val = static_cast<uint32_t>(Literal);
882 return (Val == FloatToBits(0.0f)) ||
883 (Val == FloatToBits(1.0f)) ||
884 (Val == FloatToBits(-1.0f)) ||
885 (Val == FloatToBits(0.5f)) ||
886 (Val == FloatToBits(-0.5f)) ||
887 (Val == FloatToBits(2.0f)) ||
888 (Val == FloatToBits(-2.0f)) ||
889 (Val == FloatToBits(4.0f)) ||
890 (Val == FloatToBits(-4.0f)) ||
891 (Val == 0x3e22f983 && HasInv2Pi);
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000892}
893
Matt Arsenault4bd72362016-12-10 00:39:12 +0000894bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) {
Sam Kolton9dffada2017-01-17 15:26:02 +0000895 if (!HasInv2Pi)
896 return false;
Matt Arsenault4bd72362016-12-10 00:39:12 +0000897
898 if (Literal >= -16 && Literal <= 64)
899 return true;
900
901 uint16_t Val = static_cast<uint16_t>(Literal);
902 return Val == 0x3C00 || // 1.0
903 Val == 0xBC00 || // -1.0
904 Val == 0x3800 || // 0.5
905 Val == 0xB800 || // -0.5
906 Val == 0x4000 || // 2.0
907 Val == 0xC000 || // -2.0
908 Val == 0x4400 || // 4.0
909 Val == 0xC400 || // -4.0
910 Val == 0x3118; // 1/2pi
911}
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000912
Matt Arsenault9be7b0d2017-02-27 18:49:11 +0000913bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) {
914 assert(HasInv2Pi);
915
916 int16_t Lo16 = static_cast<int16_t>(Literal);
917 int16_t Hi16 = static_cast<int16_t>(Literal >> 16);
918 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi);
919}
920
Matt Arsenault894e53d2017-07-26 20:39:42 +0000921bool isArgPassedInSGPR(const Argument *A) {
922 const Function *F = A->getParent();
923
924 // Arguments to compute shaders are never a source of divergence.
925 CallingConv::ID CC = F->getCallingConv();
926 switch (CC) {
927 case CallingConv::AMDGPU_KERNEL:
928 case CallingConv::SPIR_KERNEL:
929 return true;
930 case CallingConv::AMDGPU_VS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000931 case CallingConv::AMDGPU_LS:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000932 case CallingConv::AMDGPU_HS:
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000933 case CallingConv::AMDGPU_ES:
Matt Arsenault894e53d2017-07-26 20:39:42 +0000934 case CallingConv::AMDGPU_GS:
935 case CallingConv::AMDGPU_PS:
936 case CallingConv::AMDGPU_CS:
937 // For non-compute shaders, SGPR inputs are marked with either inreg or byval.
938 // Everything else is in VGPRs.
939 return F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::InReg) ||
940 F->getAttributes().hasParamAttribute(A->getArgNo(), Attribute::ByVal);
941 default:
942 // TODO: Should calls support inreg for SGPR inputs?
943 return false;
944 }
945}
946
Tom Stellard08efb7e2017-01-27 18:41:14 +0000947int64_t getSMRDEncodedOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000948 if (isGCN3Encoding(ST))
949 return ByteOffset;
950 return ByteOffset >> 2;
Tom Stellard08efb7e2017-01-27 18:41:14 +0000951}
952
953bool isLegalSMRDImmOffset(const MCSubtargetInfo &ST, int64_t ByteOffset) {
954 int64_t EncodedOffset = getSMRDEncodedOffset(ST, ByteOffset);
Matt Arsenault8728c5f2017-08-07 14:58:04 +0000955 return isGCN3Encoding(ST) ?
956 isUInt<20>(EncodedOffset) : isUInt<8>(EncodedOffset);
Tom Stellard08efb7e2017-01-27 18:41:14 +0000957}
Matt Arsenaultcad7fa82017-12-13 21:07:51 +0000958
Tim Renouf4f703f52018-08-21 11:07:10 +0000959// Given Imm, split it into the values to put into the SOffset and ImmOffset
960// fields in an MUBUF instruction. Return false if it is not possible (due to a
961// hardware bug needing a workaround).
Nicolai Haehnlea7b00052018-11-30 22:55:38 +0000962//
963// The required alignment ensures that individual address components remain
964// aligned if they are aligned to begin with. It also ensures that additional
965// offsets within the given alignment can be added to the resulting ImmOffset.
Tim Renouf4f703f52018-08-21 11:07:10 +0000966bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset,
Nicolai Haehnlea7b00052018-11-30 22:55:38 +0000967 const GCNSubtarget *Subtarget, uint32_t Align) {
Tim Renouf4f703f52018-08-21 11:07:10 +0000968 const uint32_t MaxImm = alignDown(4095, Align);
969 uint32_t Overflow = 0;
970
971 if (Imm > MaxImm) {
972 if (Imm <= MaxImm + 64) {
973 // Use an SOffset inline constant for 4..64
974 Overflow = Imm - MaxImm;
975 Imm = MaxImm;
976 } else {
977 // Try to keep the same value in SOffset for adjacent loads, so that
978 // the corresponding register contents can be re-used.
979 //
980 // Load values with all low-bits (except for alignment bits) set into
981 // SOffset, so that a larger range of values can be covered using
982 // s_movk_i32.
983 //
984 // Atomic operations fail to work correctly when individual address
985 // components are unaligned, even if their sum is aligned.
986 uint32_t High = (Imm + Align) & ~4095;
987 uint32_t Low = (Imm + Align) & 4095;
988 Imm = Low;
989 Overflow = High - Align;
990 }
991 }
992
993 // There is a hardware bug in SI and CI which prevents address clamping in
994 // MUBUF instructions from working correctly with SOffsets. The immediate
995 // offset is unaffected.
996 if (Overflow > 0 &&
997 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS)
998 return false;
999
1000 ImmOffset = Imm;
1001 SOffset = Overflow;
1002 return true;
1003}
1004
Nicolai Haehnle4254d452018-04-01 17:09:14 +00001005namespace {
1006
1007struct SourceOfDivergence {
1008 unsigned Intr;
1009};
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001010const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr);
Nicolai Haehnle4254d452018-04-01 17:09:14 +00001011
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001012#define GET_SourcesOfDivergence_IMPL
Nicolai Haehnle4254d452018-04-01 17:09:14 +00001013#include "AMDGPUGenSearchableTables.inc"
1014
1015} // end anonymous namespace
1016
Alexander Timofeev2e5eece2018-03-05 15:12:21 +00001017bool isIntrinsicSourceOfDivergence(unsigned IntrID) {
Nicolai Haehnlee741d7e2018-06-21 13:36:33 +00001018 return lookupSourceOfDivergence(IntrID);
Alexander Timofeev2e5eece2018-03-05 15:12:21 +00001019}
Yaxun Liu1a14bfa2017-03-27 14:04:01 +00001020} // namespace AMDGPU
1021} // namespace llvm