blob: 4698cb9a2be6ca50e2be950a89071bac0702a5ea [file] [log] [blame]
Tom Stellard347ac792015-06-26 21:15:07 +00001//===-- AMDGPUBaseInfo.cpp - AMDGPU Base encoding information--------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "AMDGPUBaseInfo.h"
Tom Stellarde3b5aea2015-12-02 17:00:42 +000010#include "AMDGPU.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000011#include "SIDefines.h"
Tom Stellardac00eb52015-12-15 16:26:16 +000012#include "llvm/IR/LLVMContext.h"
13#include "llvm/IR/Function.h"
Tom Stellarde3b5aea2015-12-02 17:00:42 +000014#include "llvm/IR/GlobalValue.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000015#include "llvm/MC/MCContext.h"
Sam Kolton1eeb11b2016-09-09 14:44:04 +000016#include "llvm/MC/MCInstrInfo.h"
17#include "llvm/MC/MCRegisterInfo.h"
Tom Stellarde135ffd2015-09-25 21:41:28 +000018#include "llvm/MC/MCSectionELF.h"
Tom Stellard2b65ed32015-12-21 18:44:27 +000019#include "llvm/MC/MCSubtargetInfo.h"
Tom Stellard347ac792015-06-26 21:15:07 +000020#include "llvm/MC/SubtargetFeature.h"
21
22#define GET_SUBTARGETINFO_ENUM
23#include "AMDGPUGenSubtargetInfo.inc"
24#undef GET_SUBTARGETINFO_ENUM
25
Tom Stellard2b65ed32015-12-21 18:44:27 +000026#define GET_REGINFO_ENUM
27#include "AMDGPUGenRegisterInfo.inc"
28#undef GET_REGINFO_ENUM
29
Sam Koltona3ec5c12016-10-07 14:46:06 +000030#define GET_INSTRINFO_NAMED_OPS
31#define GET_INSTRINFO_ENUM
32#include "AMDGPUGenInstrInfo.inc"
33#undef GET_INSTRINFO_NAMED_OPS
34#undef GET_INSTRINFO_ENUM
35
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +000036namespace {
37
38/// \returns Bit mask for given bit \p Shift and bit \p Width.
39unsigned getBitMask(unsigned Shift, unsigned Width) {
40 return ((1 << Width) - 1) << Shift;
41}
42
43/// \brief Packs \p Src into \p Dst for given bit \p Shift and bit \p Width.
44///
45/// \returns Packed \p Dst.
46unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) {
47 Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width);
48 Dst |= (Src << Shift) & getBitMask(Shift, Width);
49 return Dst;
50}
51
52/// \brief Unpacks bits from \p Src for given bit \p Shift and bit \p Width.
53///
54/// \returns Unpacked bits.
55unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) {
56 return (Src & getBitMask(Shift, Width)) >> Shift;
57}
58
59/// \returns Vmcnt bit shift.
60unsigned getVmcntBitShift() { return 0; }
61
62/// \returns Vmcnt bit width.
63unsigned getVmcntBitWidth() { return 4; }
64
65/// \returns Expcnt bit shift.
66unsigned getExpcntBitShift() { return 4; }
67
68/// \returns Expcnt bit width.
69unsigned getExpcntBitWidth() { return 3; }
70
71/// \returns Lgkmcnt bit shift.
72unsigned getLgkmcntBitShift() { return 8; }
73
74/// \returns Lgkmcnt bit width.
75unsigned getLgkmcntBitWidth() { return 4; }
76
77} // anonymous namespace
78
Tom Stellard347ac792015-06-26 21:15:07 +000079namespace llvm {
80namespace AMDGPU {
81
82IsaVersion getIsaVersion(const FeatureBitset &Features) {
83
84 if (Features.test(FeatureISAVersion7_0_0))
85 return {7, 0, 0};
86
87 if (Features.test(FeatureISAVersion7_0_1))
88 return {7, 0, 1};
89
90 if (Features.test(FeatureISAVersion8_0_0))
91 return {8, 0, 0};
92
93 if (Features.test(FeatureISAVersion8_0_1))
94 return {8, 0, 1};
95
Changpeng Fang98317d22016-10-11 16:00:47 +000096 if (Features.test(FeatureISAVersion8_0_2))
97 return {8, 0, 2};
98
Changpeng Fangc16be002016-01-13 20:39:25 +000099 if (Features.test(FeatureISAVersion8_0_3))
100 return {8, 0, 3};
101
Tom Stellard347ac792015-06-26 21:15:07 +0000102 return {0, 0, 0};
103}
104
Tom Stellardff7416b2015-06-26 21:58:31 +0000105void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
106 const FeatureBitset &Features) {
107
108 IsaVersion ISA = getIsaVersion(Features);
109
110 memset(&Header, 0, sizeof(Header));
111
112 Header.amd_kernel_code_version_major = 1;
113 Header.amd_kernel_code_version_minor = 0;
114 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
115 Header.amd_machine_version_major = ISA.Major;
116 Header.amd_machine_version_minor = ISA.Minor;
117 Header.amd_machine_version_stepping = ISA.Stepping;
118 Header.kernel_code_entry_byte_offset = sizeof(Header);
119 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
120 Header.wavefront_size = 6;
121 // These alignment values are specified in powers of two, so alignment =
122 // 2^n. The minimum alignment is 2^4 = 16.
123 Header.kernarg_segment_alignment = 4;
124 Header.group_segment_alignment = 4;
125 Header.private_segment_alignment = 4;
126}
127
Tom Stellarde135ffd2015-09-25 21:41:28 +0000128MCSection *getHSATextSection(MCContext &Ctx) {
129 return Ctx.getELFSection(".hsatext", ELF::SHT_PROGBITS,
130 ELF::SHF_ALLOC | ELF::SHF_WRITE |
131 ELF::SHF_EXECINSTR |
132 ELF::SHF_AMDGPU_HSA_AGENT |
133 ELF::SHF_AMDGPU_HSA_CODE);
134}
135
Tom Stellard00f2f912015-12-02 19:47:57 +0000136MCSection *getHSADataGlobalAgentSection(MCContext &Ctx) {
137 return Ctx.getELFSection(".hsadata_global_agent", ELF::SHT_PROGBITS,
138 ELF::SHF_ALLOC | ELF::SHF_WRITE |
139 ELF::SHF_AMDGPU_HSA_GLOBAL |
140 ELF::SHF_AMDGPU_HSA_AGENT);
141}
142
143MCSection *getHSADataGlobalProgramSection(MCContext &Ctx) {
144 return Ctx.getELFSection(".hsadata_global_program", ELF::SHT_PROGBITS,
145 ELF::SHF_ALLOC | ELF::SHF_WRITE |
146 ELF::SHF_AMDGPU_HSA_GLOBAL);
147}
148
Tom Stellard9760f032015-12-03 03:34:32 +0000149MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx) {
150 return Ctx.getELFSection(".hsarodata_readonly_agent", ELF::SHT_PROGBITS,
151 ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY |
152 ELF::SHF_AMDGPU_HSA_AGENT);
153}
154
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000155bool isGroupSegment(const GlobalValue *GV) {
156 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
157}
158
Tom Stellard00f2f912015-12-02 19:47:57 +0000159bool isGlobalSegment(const GlobalValue *GV) {
160 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
161}
162
163bool isReadOnlySegment(const GlobalValue *GV) {
164 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS;
165}
166
Matt Arsenault83002722016-05-12 02:45:18 +0000167int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000168 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000169 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000170
171 if (A.isStringAttribute()) {
172 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000173 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000174 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000175 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000176 }
177 }
Matt Arsenault83002722016-05-12 02:45:18 +0000178
Marek Olsakfccabaf2016-01-13 11:45:36 +0000179 return Result;
180}
181
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000182std::pair<int, int> getIntegerPairAttribute(const Function &F,
183 StringRef Name,
184 std::pair<int, int> Default,
185 bool OnlyFirstRequired) {
186 Attribute A = F.getFnAttribute(Name);
187 if (!A.isStringAttribute())
188 return Default;
189
190 LLVMContext &Ctx = F.getContext();
191 std::pair<int, int> Ints = Default;
192 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
193 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
194 Ctx.emitError("can't parse first integer attribute " + Name);
195 return Default;
196 }
197 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
198 if (!OnlyFirstRequired || Strs.second.trim().size()) {
199 Ctx.emitError("can't parse second integer attribute " + Name);
200 return Default;
201 }
202 }
203
204 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000205}
206
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000207unsigned getWaitcntBitMask(IsaVersion Version) {
208 unsigned Vmcnt = getBitMask(getVmcntBitShift(), getVmcntBitWidth());
209 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth());
210 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth());
211 return Vmcnt | Expcnt | Lgkmcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000212}
213
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000214unsigned getVmcntBitMask(IsaVersion Version) {
215 return (1 << getVmcntBitWidth()) - 1;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000216}
217
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000218unsigned getExpcntBitMask(IsaVersion Version) {
219 return (1 << getExpcntBitWidth()) - 1;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000220}
221
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000222unsigned getLgkmcntBitMask(IsaVersion Version) {
223 return (1 << getLgkmcntBitWidth()) - 1;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000224}
225
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000226unsigned decodeVmcnt(IsaVersion Version, unsigned Waitcnt) {
227 return unpackBits(Waitcnt, getVmcntBitShift(), getVmcntBitWidth());
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000228}
229
Konstantin Zhuravlyovcdd45472016-10-11 18:58:22 +0000230unsigned decodeExpcnt(IsaVersion Version, unsigned Waitcnt) {
231 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
232}
233
234unsigned decodeLgkmcnt(IsaVersion Version, unsigned Waitcnt) {
235 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
236}
237
238void decodeWaitcnt(IsaVersion Version, unsigned Waitcnt,
239 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) {
240 Vmcnt = decodeVmcnt(Version, Waitcnt);
241 Expcnt = decodeExpcnt(Version, Waitcnt);
242 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt);
243}
244
245unsigned encodeVmcnt(IsaVersion Version, unsigned Waitcnt, unsigned Vmcnt) {
246 return packBits(Vmcnt, Waitcnt, getVmcntBitShift(), getVmcntBitWidth());
247}
248
249unsigned encodeExpcnt(IsaVersion Version, unsigned Waitcnt, unsigned Expcnt) {
250 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth());
251}
252
253unsigned encodeLgkmcnt(IsaVersion Version, unsigned Waitcnt, unsigned Lgkmcnt) {
254 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth());
255}
256
257unsigned encodeWaitcnt(IsaVersion Version,
258 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) {
259 unsigned Waitcnt = getWaitcntBitMask(Version);;
260 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt);
261 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt);
262 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt);
263 return Waitcnt;
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000264}
265
Marek Olsakfccabaf2016-01-13 11:45:36 +0000266unsigned getInitialPSInputAddr(const Function &F) {
267 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000268}
269
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000270bool isShader(CallingConv::ID cc) {
271 switch(cc) {
272 case CallingConv::AMDGPU_VS:
273 case CallingConv::AMDGPU_GS:
274 case CallingConv::AMDGPU_PS:
275 case CallingConv::AMDGPU_CS:
276 return true;
277 default:
278 return false;
279 }
280}
281
282bool isCompute(CallingConv::ID cc) {
283 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
284}
285
Tom Stellard2b65ed32015-12-21 18:44:27 +0000286bool isSI(const MCSubtargetInfo &STI) {
287 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
288}
289
290bool isCI(const MCSubtargetInfo &STI) {
291 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
292}
293
294bool isVI(const MCSubtargetInfo &STI) {
295 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
296}
297
298unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
299
300 switch(Reg) {
301 default: break;
302 case AMDGPU::FLAT_SCR:
303 assert(!isSI(STI));
304 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
305
306 case AMDGPU::FLAT_SCR_LO:
307 assert(!isSI(STI));
308 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
309
310 case AMDGPU::FLAT_SCR_HI:
311 assert(!isSI(STI));
312 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
313 }
314 return Reg;
315}
316
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000317bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
318 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
319
320 return OpType == AMDGPU::OPERAND_REG_IMM32_INT ||
321 OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
322 OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
323 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
324}
325
326bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
327 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
328
329 return OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
330 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
331}
332
333bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
334 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
335
336 return OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
337 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
338}
339
340unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
341 unsigned OpNo) {
342 int RCID = Desc.OpInfo[OpNo].RegClass;
343 const MCRegisterClass &RC = MRI->getRegClass(RCID);
344 return RC.getSize();
345}
346
347bool isInlinableLiteral64(int64_t Literal, bool IsVI) {
348 if (Literal >= -16 && Literal <= 64)
349 return true;
350
351 double D = BitsToDouble(Literal);
352
353 if (D == 0.5 || D == -0.5 ||
354 D == 1.0 || D == -1.0 ||
355 D == 2.0 || D == -2.0 ||
356 D == 4.0 || D == -4.0)
357 return true;
358
359 if (IsVI && Literal == 0x3fc45f306dc9c882)
360 return true;
361
362 return false;
363}
364
365bool isInlinableLiteral32(int32_t Literal, bool IsVI) {
366 if (Literal >= -16 && Literal <= 64)
367 return true;
368
369 float F = BitsToFloat(Literal);
370
371 if (F == 0.5 || F == -0.5 ||
372 F == 1.0 || F == -1.0 ||
373 F == 2.0 || F == -2.0 ||
374 F == 4.0 || F == -4.0)
375 return true;
376
377 if (IsVI && Literal == 0x3e22f983)
378 return true;
379
380 return false;
381}
382
383
Tom Stellard347ac792015-06-26 21:15:07 +0000384} // End namespace AMDGPU
385} // End namespace llvm