blob: 63c0c8851a7f71669824f4c78f3df3d68edf0990 [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
Tom Stellard347ac792015-06-26 21:15:07 +000036namespace llvm {
37namespace AMDGPU {
38
39IsaVersion getIsaVersion(const FeatureBitset &Features) {
40
41 if (Features.test(FeatureISAVersion7_0_0))
42 return {7, 0, 0};
43
44 if (Features.test(FeatureISAVersion7_0_1))
45 return {7, 0, 1};
46
47 if (Features.test(FeatureISAVersion8_0_0))
48 return {8, 0, 0};
49
50 if (Features.test(FeatureISAVersion8_0_1))
51 return {8, 0, 1};
52
Changpeng Fangc16be002016-01-13 20:39:25 +000053 if (Features.test(FeatureISAVersion8_0_3))
54 return {8, 0, 3};
55
Tom Stellard347ac792015-06-26 21:15:07 +000056 return {0, 0, 0};
57}
58
Tom Stellardff7416b2015-06-26 21:58:31 +000059void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header,
60 const FeatureBitset &Features) {
61
62 IsaVersion ISA = getIsaVersion(Features);
63
64 memset(&Header, 0, sizeof(Header));
65
66 Header.amd_kernel_code_version_major = 1;
67 Header.amd_kernel_code_version_minor = 0;
68 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU
69 Header.amd_machine_version_major = ISA.Major;
70 Header.amd_machine_version_minor = ISA.Minor;
71 Header.amd_machine_version_stepping = ISA.Stepping;
72 Header.kernel_code_entry_byte_offset = sizeof(Header);
73 // wavefront_size is specified as a power of 2: 2^6 = 64 threads.
74 Header.wavefront_size = 6;
75 // These alignment values are specified in powers of two, so alignment =
76 // 2^n. The minimum alignment is 2^4 = 16.
77 Header.kernarg_segment_alignment = 4;
78 Header.group_segment_alignment = 4;
79 Header.private_segment_alignment = 4;
80}
81
Tom Stellarde135ffd2015-09-25 21:41:28 +000082MCSection *getHSATextSection(MCContext &Ctx) {
83 return Ctx.getELFSection(".hsatext", ELF::SHT_PROGBITS,
84 ELF::SHF_ALLOC | ELF::SHF_WRITE |
85 ELF::SHF_EXECINSTR |
86 ELF::SHF_AMDGPU_HSA_AGENT |
87 ELF::SHF_AMDGPU_HSA_CODE);
88}
89
Tom Stellard00f2f912015-12-02 19:47:57 +000090MCSection *getHSADataGlobalAgentSection(MCContext &Ctx) {
91 return Ctx.getELFSection(".hsadata_global_agent", ELF::SHT_PROGBITS,
92 ELF::SHF_ALLOC | ELF::SHF_WRITE |
93 ELF::SHF_AMDGPU_HSA_GLOBAL |
94 ELF::SHF_AMDGPU_HSA_AGENT);
95}
96
97MCSection *getHSADataGlobalProgramSection(MCContext &Ctx) {
98 return Ctx.getELFSection(".hsadata_global_program", ELF::SHT_PROGBITS,
99 ELF::SHF_ALLOC | ELF::SHF_WRITE |
100 ELF::SHF_AMDGPU_HSA_GLOBAL);
101}
102
Tom Stellard9760f032015-12-03 03:34:32 +0000103MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx) {
104 return Ctx.getELFSection(".hsarodata_readonly_agent", ELF::SHT_PROGBITS,
105 ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY |
106 ELF::SHF_AMDGPU_HSA_AGENT);
107}
108
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000109bool isGroupSegment(const GlobalValue *GV) {
110 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
111}
112
Tom Stellard00f2f912015-12-02 19:47:57 +0000113bool isGlobalSegment(const GlobalValue *GV) {
114 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
115}
116
117bool isReadOnlySegment(const GlobalValue *GV) {
118 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS;
119}
120
Matt Arsenault83002722016-05-12 02:45:18 +0000121int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
Marek Olsakfccabaf2016-01-13 11:45:36 +0000122 Attribute A = F.getFnAttribute(Name);
Matt Arsenault83002722016-05-12 02:45:18 +0000123 int Result = Default;
Tom Stellardac00eb52015-12-15 16:26:16 +0000124
125 if (A.isStringAttribute()) {
126 StringRef Str = A.getValueAsString();
Marek Olsakfccabaf2016-01-13 11:45:36 +0000127 if (Str.getAsInteger(0, Result)) {
Tom Stellardac00eb52015-12-15 16:26:16 +0000128 LLVMContext &Ctx = F.getContext();
Matt Arsenault83002722016-05-12 02:45:18 +0000129 Ctx.emitError("can't parse integer attribute " + Name);
Tom Stellardac00eb52015-12-15 16:26:16 +0000130 }
131 }
Matt Arsenault83002722016-05-12 02:45:18 +0000132
Marek Olsakfccabaf2016-01-13 11:45:36 +0000133 return Result;
134}
135
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000136std::pair<int, int> getIntegerPairAttribute(const Function &F,
137 StringRef Name,
138 std::pair<int, int> Default,
139 bool OnlyFirstRequired) {
140 Attribute A = F.getFnAttribute(Name);
141 if (!A.isStringAttribute())
142 return Default;
143
144 LLVMContext &Ctx = F.getContext();
145 std::pair<int, int> Ints = Default;
146 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(',');
147 if (Strs.first.trim().getAsInteger(0, Ints.first)) {
148 Ctx.emitError("can't parse first integer attribute " + Name);
149 return Default;
150 }
151 if (Strs.second.trim().getAsInteger(0, Ints.second)) {
152 if (!OnlyFirstRequired || Strs.second.trim().size()) {
153 Ctx.emitError("can't parse second integer attribute " + Name);
154 return Default;
155 }
156 }
157
158 return Ints;
Tom Stellard79a1fd72016-04-14 16:27:07 +0000159}
160
Konstantin Zhuravlyov836cbff2016-09-30 17:01:40 +0000161unsigned getVmcntMask(IsaVersion Version) {
162 return 0xf;
163}
164
165unsigned getVmcntShift(IsaVersion Version) {
166 return 0;
167}
168
169unsigned getExpcntMask(IsaVersion Version) {
170 return 0x7;
171}
172
173unsigned getExpcntShift(IsaVersion Version) {
174 return 4;
175}
176
177unsigned getLgkmcntMask(IsaVersion Version) {
178 return 0xf;
179}
180
181unsigned getLgkmcntShift(IsaVersion Version) {
182 return 8;
183}
184
Marek Olsakfccabaf2016-01-13 11:45:36 +0000185unsigned getInitialPSInputAddr(const Function &F) {
186 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000187}
188
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000189bool isShader(CallingConv::ID cc) {
190 switch(cc) {
191 case CallingConv::AMDGPU_VS:
192 case CallingConv::AMDGPU_GS:
193 case CallingConv::AMDGPU_PS:
194 case CallingConv::AMDGPU_CS:
195 return true;
196 default:
197 return false;
198 }
199}
200
201bool isCompute(CallingConv::ID cc) {
202 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
203}
204
Tom Stellard2b65ed32015-12-21 18:44:27 +0000205bool isSI(const MCSubtargetInfo &STI) {
206 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
207}
208
209bool isCI(const MCSubtargetInfo &STI) {
210 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
211}
212
213bool isVI(const MCSubtargetInfo &STI) {
214 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
215}
216
217unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
218
219 switch(Reg) {
220 default: break;
221 case AMDGPU::FLAT_SCR:
222 assert(!isSI(STI));
223 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
224
225 case AMDGPU::FLAT_SCR_LO:
226 assert(!isSI(STI));
227 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
228
229 case AMDGPU::FLAT_SCR_HI:
230 assert(!isSI(STI));
231 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
232 }
233 return Reg;
234}
235
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000236bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
237 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
238
239 return OpType == AMDGPU::OPERAND_REG_IMM32_INT ||
240 OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
241 OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
242 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
243}
244
245bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
246 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
247
248 return OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
249 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
250}
251
252bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
253 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
254
255 return OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
256 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
257}
258
259unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
260 unsigned OpNo) {
261 int RCID = Desc.OpInfo[OpNo].RegClass;
262 const MCRegisterClass &RC = MRI->getRegClass(RCID);
263 return RC.getSize();
264}
265
266bool isInlinableLiteral64(int64_t Literal, bool IsVI) {
267 if (Literal >= -16 && Literal <= 64)
268 return true;
269
270 double D = BitsToDouble(Literal);
271
272 if (D == 0.5 || D == -0.5 ||
273 D == 1.0 || D == -1.0 ||
274 D == 2.0 || D == -2.0 ||
275 D == 4.0 || D == -4.0)
276 return true;
277
278 if (IsVI && Literal == 0x3fc45f306dc9c882)
279 return true;
280
281 return false;
282}
283
284bool isInlinableLiteral32(int32_t Literal, bool IsVI) {
285 if (Literal >= -16 && Literal <= 64)
286 return true;
287
288 float F = BitsToFloat(Literal);
289
290 if (F == 0.5 || F == -0.5 ||
291 F == 1.0 || F == -1.0 ||
292 F == 2.0 || F == -2.0 ||
293 F == 4.0 || F == -4.0)
294 return true;
295
296 if (IsVI && Literal == 0x3e22f983)
297 return true;
298
299 return false;
300}
301
302
Tom Stellard347ac792015-06-26 21:15:07 +0000303} // End namespace AMDGPU
304} // End namespace llvm