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 #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 36 namespace { 37 38 /// \returns Bit mask for given bit \p Shift and bit \p Width. 39 unsigned getBitMask(unsigned Shift, unsigned Width) { 40 return ((1 << Width) - 1) << Shift; 41 } 42 43 /// \brief Packs \p Src into \p Dst for given bit \p Shift and bit \p Width. 44 /// 45 /// \returns Packed \p Dst. 46 unsigned packBits(unsigned Src, unsigned Dst, unsigned Shift, unsigned Width) { 47 Dst &= ~(1 << Shift) & ~getBitMask(Shift, Width); 48 Dst |= (Src << Shift) & getBitMask(Shift, Width); 49 return Dst; 50 } 51 52 /// \brief Unpacks bits from \p Src for given bit \p Shift and bit \p Width. 53 /// 54 /// \returns Unpacked bits. 55 unsigned unpackBits(unsigned Src, unsigned Shift, unsigned Width) { 56 return (Src & getBitMask(Shift, Width)) >> Shift; 57 } 58 59 /// \returns Vmcnt bit shift. 60 unsigned getVmcntBitShift() { return 0; } 61 62 /// \returns Vmcnt bit width. 63 unsigned getVmcntBitWidth() { return 4; } 64 65 /// \returns Expcnt bit shift. 66 unsigned getExpcntBitShift() { return 4; } 67 68 /// \returns Expcnt bit width. 69 unsigned getExpcntBitWidth() { return 3; } 70 71 /// \returns Lgkmcnt bit shift. 72 unsigned getLgkmcntBitShift() { return 8; } 73 74 /// \returns Lgkmcnt bit width. 75 unsigned getLgkmcntBitWidth() { return 4; } 76 77 } // anonymous namespace 78 79 namespace llvm { 80 namespace AMDGPU { 81 82 IsaVersion getIsaVersion(const FeatureBitset &Features) { 83 84 if (Features.test(FeatureISAVersion7_0_0)) 85 return {7, 0, 0}; 86 87 if (Features.test(FeatureISAVersion7_0_1)) 88 return {7, 0, 1}; 89 90 if (Features.test(FeatureISAVersion8_0_0)) 91 return {8, 0, 0}; 92 93 if (Features.test(FeatureISAVersion8_0_1)) 94 return {8, 0, 1}; 95 96 if (Features.test(FeatureISAVersion8_0_2)) 97 return {8, 0, 2}; 98 99 if (Features.test(FeatureISAVersion8_0_3)) 100 return {8, 0, 3}; 101 102 return {0, 0, 0}; 103 } 104 105 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header, 106 const FeatureBitset &Features) { 107 108 IsaVersion ISA = getIsaVersion(Features); 109 110 memset(&Header, 0, sizeof(Header)); 111 112 Header.amd_kernel_code_version_major = 1; 113 Header.amd_kernel_code_version_minor = 0; 114 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU 115 Header.amd_machine_version_major = ISA.Major; 116 Header.amd_machine_version_minor = ISA.Minor; 117 Header.amd_machine_version_stepping = ISA.Stepping; 118 Header.kernel_code_entry_byte_offset = sizeof(Header); 119 // wavefront_size is specified as a power of 2: 2^6 = 64 threads. 120 Header.wavefront_size = 6; 121 // These alignment values are specified in powers of two, so alignment = 122 // 2^n. The minimum alignment is 2^4 = 16. 123 Header.kernarg_segment_alignment = 4; 124 Header.group_segment_alignment = 4; 125 Header.private_segment_alignment = 4; 126 } 127 128 MCSection *getHSATextSection(MCContext &Ctx) { 129 return Ctx.getELFSection(".hsatext", ELF::SHT_PROGBITS, 130 ELF::SHF_ALLOC | ELF::SHF_WRITE | 131 ELF::SHF_EXECINSTR | 132 ELF::SHF_AMDGPU_HSA_AGENT | 133 ELF::SHF_AMDGPU_HSA_CODE); 134 } 135 136 MCSection *getHSADataGlobalAgentSection(MCContext &Ctx) { 137 return Ctx.getELFSection(".hsadata_global_agent", ELF::SHT_PROGBITS, 138 ELF::SHF_ALLOC | ELF::SHF_WRITE | 139 ELF::SHF_AMDGPU_HSA_GLOBAL | 140 ELF::SHF_AMDGPU_HSA_AGENT); 141 } 142 143 MCSection *getHSADataGlobalProgramSection(MCContext &Ctx) { 144 return Ctx.getELFSection(".hsadata_global_program", ELF::SHT_PROGBITS, 145 ELF::SHF_ALLOC | ELF::SHF_WRITE | 146 ELF::SHF_AMDGPU_HSA_GLOBAL); 147 } 148 149 MCSection *getHSARodataReadonlyAgentSection(MCContext &Ctx) { 150 return Ctx.getELFSection(".hsarodata_readonly_agent", ELF::SHT_PROGBITS, 151 ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY | 152 ELF::SHF_AMDGPU_HSA_AGENT); 153 } 154 155 bool isGroupSegment(const GlobalValue *GV) { 156 return GV->getType()->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS; 157 } 158 159 bool isGlobalSegment(const GlobalValue *GV) { 160 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS; 161 } 162 163 bool isReadOnlySegment(const GlobalValue *GV) { 164 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS; 165 } 166 167 int getIntegerAttribute(const Function &F, StringRef Name, int Default) { 168 Attribute A = F.getFnAttribute(Name); 169 int Result = Default; 170 171 if (A.isStringAttribute()) { 172 StringRef Str = A.getValueAsString(); 173 if (Str.getAsInteger(0, Result)) { 174 LLVMContext &Ctx = F.getContext(); 175 Ctx.emitError("can't parse integer attribute " + Name); 176 } 177 } 178 179 return Result; 180 } 181 182 std::pair<int, int> getIntegerPairAttribute(const Function &F, 183 StringRef Name, 184 std::pair<int, int> Default, 185 bool OnlyFirstRequired) { 186 Attribute A = F.getFnAttribute(Name); 187 if (!A.isStringAttribute()) 188 return Default; 189 190 LLVMContext &Ctx = F.getContext(); 191 std::pair<int, int> Ints = Default; 192 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(','); 193 if (Strs.first.trim().getAsInteger(0, Ints.first)) { 194 Ctx.emitError("can't parse first integer attribute " + Name); 195 return Default; 196 } 197 if (Strs.second.trim().getAsInteger(0, Ints.second)) { 198 if (!OnlyFirstRequired || Strs.second.trim().size()) { 199 Ctx.emitError("can't parse second integer attribute " + Name); 200 return Default; 201 } 202 } 203 204 return Ints; 205 } 206 207 unsigned getWaitcntBitMask(IsaVersion Version) { 208 unsigned Vmcnt = getBitMask(getVmcntBitShift(), getVmcntBitWidth()); 209 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth()); 210 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), getLgkmcntBitWidth()); 211 return Vmcnt | Expcnt | Lgkmcnt; 212 } 213 214 unsigned getVmcntBitMask(IsaVersion Version) { 215 return (1 << getVmcntBitWidth()) - 1; 216 } 217 218 unsigned getExpcntBitMask(IsaVersion Version) { 219 return (1 << getExpcntBitWidth()) - 1; 220 } 221 222 unsigned getLgkmcntBitMask(IsaVersion Version) { 223 return (1 << getLgkmcntBitWidth()) - 1; 224 } 225 226 unsigned decodeVmcnt(IsaVersion Version, unsigned Waitcnt) { 227 return unpackBits(Waitcnt, getVmcntBitShift(), getVmcntBitWidth()); 228 } 229 230 unsigned decodeExpcnt(IsaVersion Version, unsigned Waitcnt) { 231 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth()); 232 } 233 234 unsigned decodeLgkmcnt(IsaVersion Version, unsigned Waitcnt) { 235 return unpackBits(Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth()); 236 } 237 238 void decodeWaitcnt(IsaVersion Version, unsigned Waitcnt, 239 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) { 240 Vmcnt = decodeVmcnt(Version, Waitcnt); 241 Expcnt = decodeExpcnt(Version, Waitcnt); 242 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt); 243 } 244 245 unsigned encodeVmcnt(IsaVersion Version, unsigned Waitcnt, unsigned Vmcnt) { 246 return packBits(Vmcnt, Waitcnt, getVmcntBitShift(), getVmcntBitWidth()); 247 } 248 249 unsigned encodeExpcnt(IsaVersion Version, unsigned Waitcnt, unsigned Expcnt) { 250 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth()); 251 } 252 253 unsigned encodeLgkmcnt(IsaVersion Version, unsigned Waitcnt, unsigned Lgkmcnt) { 254 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), getLgkmcntBitWidth()); 255 } 256 257 unsigned encodeWaitcnt(IsaVersion Version, 258 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) { 259 unsigned Waitcnt = getWaitcntBitMask(Version);; 260 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt); 261 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt); 262 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt); 263 return Waitcnt; 264 } 265 266 unsigned getInitialPSInputAddr(const Function &F) { 267 return getIntegerAttribute(F, "InitialPSInputAddr", 0); 268 } 269 270 bool isShader(CallingConv::ID cc) { 271 switch(cc) { 272 case CallingConv::AMDGPU_VS: 273 case CallingConv::AMDGPU_GS: 274 case CallingConv::AMDGPU_PS: 275 case CallingConv::AMDGPU_CS: 276 return true; 277 default: 278 return false; 279 } 280 } 281 282 bool isCompute(CallingConv::ID cc) { 283 return !isShader(cc) || cc == CallingConv::AMDGPU_CS; 284 } 285 286 bool isSI(const MCSubtargetInfo &STI) { 287 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands]; 288 } 289 290 bool isCI(const MCSubtargetInfo &STI) { 291 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands]; 292 } 293 294 bool isVI(const MCSubtargetInfo &STI) { 295 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands]; 296 } 297 298 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) { 299 300 switch(Reg) { 301 default: break; 302 case AMDGPU::FLAT_SCR: 303 assert(!isSI(STI)); 304 return isCI(STI) ? AMDGPU::FLAT_SCR_ci : AMDGPU::FLAT_SCR_vi; 305 306 case AMDGPU::FLAT_SCR_LO: 307 assert(!isSI(STI)); 308 return isCI(STI) ? AMDGPU::FLAT_SCR_LO_ci : AMDGPU::FLAT_SCR_LO_vi; 309 310 case AMDGPU::FLAT_SCR_HI: 311 assert(!isSI(STI)); 312 return isCI(STI) ? AMDGPU::FLAT_SCR_HI_ci : AMDGPU::FLAT_SCR_HI_vi; 313 } 314 return Reg; 315 } 316 317 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) { 318 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 319 320 return OpType == AMDGPU::OPERAND_REG_IMM32_INT || 321 OpType == AMDGPU::OPERAND_REG_IMM32_FP || 322 OpType == AMDGPU::OPERAND_REG_INLINE_C_INT || 323 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP; 324 } 325 326 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) { 327 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 328 329 return OpType == AMDGPU::OPERAND_REG_IMM32_FP || 330 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP; 331 } 332 333 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) { 334 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 335 336 return OpType == AMDGPU::OPERAND_REG_INLINE_C_INT || 337 OpType == AMDGPU::OPERAND_REG_INLINE_C_FP; 338 } 339 340 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc, 341 unsigned OpNo) { 342 int RCID = Desc.OpInfo[OpNo].RegClass; 343 const MCRegisterClass &RC = MRI->getRegClass(RCID); 344 return RC.getSize(); 345 } 346 347 bool isInlinableLiteral64(int64_t Literal, bool IsVI) { 348 if (Literal >= -16 && Literal <= 64) 349 return true; 350 351 double D = BitsToDouble(Literal); 352 353 if (D == 0.5 || D == -0.5 || 354 D == 1.0 || D == -1.0 || 355 D == 2.0 || D == -2.0 || 356 D == 4.0 || D == -4.0) 357 return true; 358 359 if (IsVI && Literal == 0x3fc45f306dc9c882) 360 return true; 361 362 return false; 363 } 364 365 bool isInlinableLiteral32(int32_t Literal, bool IsVI) { 366 if (Literal >= -16 && Literal <= 64) 367 return true; 368 369 float F = BitsToFloat(Literal); 370 371 if (F == 0.5 || F == -0.5 || 372 F == 1.0 || F == -1.0 || 373 F == 2.0 || F == -2.0 || 374 F == 4.0 || F == -4.0) 375 return true; 376 377 if (IsVI && Literal == 0x3e22f983) 378 return true; 379 380 return false; 381 } 382 383 384 } // End namespace AMDGPU 385 } // End namespace llvm 386