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