blob: 51b56e04e522e2eb658c0341a4cdd890f107c014 [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
Marek Olsakfccabaf2016-01-13 11:45:36 +0000155unsigned getInitialPSInputAddr(const Function &F) {
156 return getIntegerAttribute(F, "InitialPSInputAddr", 0);
Tom Stellardac00eb52015-12-15 16:26:16 +0000157}
158
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000159bool isShader(CallingConv::ID cc) {
160 switch(cc) {
161 case CallingConv::AMDGPU_VS:
162 case CallingConv::AMDGPU_GS:
163 case CallingConv::AMDGPU_PS:
164 case CallingConv::AMDGPU_CS:
165 return true;
166 default:
167 return false;
168 }
169}
170
171bool isCompute(CallingConv::ID cc) {
172 return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
173}
174
Tom Stellard2b65ed32015-12-21 18:44:27 +0000175bool isSI(const MCSubtargetInfo &STI) {
176 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
177}
178
179bool isCI(const MCSubtargetInfo &STI) {
180 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
181}
182
183bool isVI(const MCSubtargetInfo &STI) {
184 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
185}
186
187unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) {
188
189 switch(Reg) {
190 default: break;
191 case AMDGPU::FLAT_SCR:
192 assert(!isSI(STI));
193 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi;
194
195 case AMDGPU::FLAT_SCR_LO:
196 assert(!isSI(STI));
197 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi;
198
199 case AMDGPU::FLAT_SCR_HI:
200 assert(!isSI(STI));
201 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi;
202 }
203 return Reg;
204}
205
Sam Kolton1eeb11b2016-09-09 14:44:04 +0000206bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) {
207 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
208
209 return OpType == AMDGPU::OPERAND_REG_IMM32_INT ||
210 OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
211 OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
212 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
213}
214
215bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) {
216 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
217
218 return OpType == AMDGPU::OPERAND_REG_IMM32_FP ||
219 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
220}
221
222bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) {
223 unsigned OpType = Desc.OpInfo[OpNo].OperandType;
224
225 return OpType == AMDGPU::OPERAND_REG_INLINE_C_INT ||
226 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP;
227}
228
229unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc,
230 unsigned OpNo) {
231 int RCID = Desc.OpInfo[OpNo].RegClass;
232 const MCRegisterClass &RC = MRI->getRegClass(RCID);
233 return RC.getSize();
234}
235
236bool isInlinableLiteral64(int64_t Literal, bool IsVI) {
237 if (Literal >= -16 && Literal <= 64)
238 return true;
239
240 double D = BitsToDouble(Literal);
241
242 if (D == 0.5 || D == -0.5 ||
243 D == 1.0 || D == -1.0 ||
244 D == 2.0 || D == -2.0 ||
245 D == 4.0 || D == -4.0)
246 return true;
247
248 if (IsVI && Literal == 0x3fc45f306dc9c882)
249 return true;
250
251 return false;
252}
253
254bool isInlinableLiteral32(int32_t Literal, bool IsVI) {
255 if (Literal >= -16 && Literal <= 64)
256 return true;
257
258 float F = BitsToFloat(Literal);
259
260 if (F == 0.5 || F == -0.5 ||
261 F == 1.0 || F == -1.0 ||
262 F == 2.0 || F == -2.0 ||
263 F == 4.0 || F == -4.0)
264 return true;
265
266 if (IsVI && Literal == 0x3e22f983)
267 return true;
268
269 return false;
270}
271
272
Tom Stellard347ac792015-06-26 21:15:07 +0000273} // End namespace AMDGPU
274} // End namespace llvm