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