1 //===-- 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"
10 #include "AMDGPU.h"
11 #include "SIDefines.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/GlobalValue.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCInstrInfo.h"
17 #include "llvm/MC/MCRegisterInfo.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/MC/SubtargetFeature.h"
21 
22 #define GET_SUBTARGETINFO_ENUM
23 #include "AMDGPUGenSubtargetInfo.inc"
24 #undef GET_SUBTARGETINFO_ENUM
25 
26 #define GET_REGINFO_ENUM
27 #include "AMDGPUGenRegisterInfo.inc"
28 #undef GET_REGINFO_ENUM
29 
30 namespace llvm {
31 namespace AMDGPU {
32 
33 IsaVersion 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 
47   if (Features.test(FeatureISAVersion8_0_3))
48     return {8, 0, 3};
49 
50   return {0, 0, 0};
51 }
52 
53 void 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 
76 MCSection *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 
84 MCSection *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 
91 MCSection *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 
97 MCSection *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 
103 bool isGroupSegment(const GlobalValue *GV) {
104   return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
105 }
106 
107 bool isGlobalSegment(const GlobalValue *GV) {
108   return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
109 }
110 
111 bool isReadOnlySegment(const GlobalValue *GV) {
112   return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS;
113 }
114 
115 int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
116   Attribute A = F.getFnAttribute(Name);
117   int Result = Default;
118 
119   if (A.isStringAttribute()) {
120     StringRef Str = A.getValueAsString();
121     if (Str.getAsInteger(0, Result)) {
122       LLVMContext &Ctx = F.getContext();
123       Ctx.emitError("can't parse integer attribute " + Name);
124     }
125   }
126 
127   return Result;
128 }
129 
130 std::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;
153 }
154 
155 unsigned getVmcntMask(IsaVersion Version) {
156   return 0xf;
157 }
158 
159 unsigned getVmcntShift(IsaVersion Version) {
160   return 0;
161 }
162 
163 unsigned getExpcntMask(IsaVersion Version) {
164   return 0x7;
165 }
166 
167 unsigned getExpcntShift(IsaVersion Version) {
168   return 4;
169 }
170 
171 unsigned getLgkmcntMask(IsaVersion Version) {
172   return 0xf;
173 }
174 
175 unsigned getLgkmcntShift(IsaVersion Version) {
176   return 8;
177 }
178 
179 unsigned getInitialPSInputAddr(const Function &F) {
180   return getIntegerAttribute(F, "InitialPSInputAddr", 0);
181 }
182 
183 bool 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 
195 bool isCompute(CallingConv::ID cc) {
196   return !isShader(cc) || cc == CallingConv::AMDGPU_CS;
197 }
198 
199 bool isSI(const MCSubtargetInfo &STI) {
200   return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands];
201 }
202 
203 bool isCI(const MCSubtargetInfo &STI) {
204   return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands];
205 }
206 
207 bool isVI(const MCSubtargetInfo &STI) {
208   return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
209 }
210 
211 unsigned 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 
230 bool 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 
239 bool 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 
246 bool 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 
253 unsigned 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 
260 bool 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 
278 bool 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 
297 } // End namespace AMDGPU
298 } // End namespace llvm
299