1 //===- AMDGPUBaseInfo.cpp - AMDGPU Base encoding information --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "AMDGPUBaseInfo.h" 10 #include "AMDGPU.h" 11 #include "AMDGPUAsmUtils.h" 12 #include "AMDKernelCodeT.h" 13 #include "GCNSubtarget.h" 14 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/IR/Attributes.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/GlobalValue.h" 19 #include "llvm/IR/IntrinsicsAMDGPU.h" 20 #include "llvm/IR/IntrinsicsR600.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/MC/MCSubtargetInfo.h" 23 #include "llvm/Support/AMDHSAKernelDescriptor.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/TargetParser.h" 26 27 #define GET_INSTRINFO_NAMED_OPS 28 #define GET_INSTRMAP_INFO 29 #include "AMDGPUGenInstrInfo.inc" 30 31 static llvm::cl::opt<unsigned> AmdhsaCodeObjectVersion( 32 "amdhsa-code-object-version", llvm::cl::Hidden, 33 llvm::cl::desc("AMDHSA Code Object Version"), llvm::cl::init(4), 34 llvm::cl::ZeroOrMore); 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 /// 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 /// 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 (lower bits). 60 unsigned getVmcntBitShiftLo() { return 0; } 61 62 /// \returns Vmcnt bit width (lower bits). 63 unsigned getVmcntBitWidthLo() { 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(unsigned VersionMajor) { 76 return (VersionMajor >= 10) ? 6 : 4; 77 } 78 79 /// \returns Vmcnt bit shift (higher bits). 80 unsigned getVmcntBitShiftHi() { return 14; } 81 82 /// \returns Vmcnt bit width (higher bits). 83 unsigned getVmcntBitWidthHi() { return 2; } 84 85 } // end namespace anonymous 86 87 namespace llvm { 88 89 namespace AMDGPU { 90 91 Optional<uint8_t> getHsaAbiVersion(const MCSubtargetInfo *STI) { 92 if (STI && STI->getTargetTriple().getOS() != Triple::AMDHSA) 93 return None; 94 95 switch (AmdhsaCodeObjectVersion) { 96 case 2: 97 return ELF::ELFABIVERSION_AMDGPU_HSA_V2; 98 case 3: 99 return ELF::ELFABIVERSION_AMDGPU_HSA_V3; 100 case 4: 101 return ELF::ELFABIVERSION_AMDGPU_HSA_V4; 102 case 5: 103 return ELF::ELFABIVERSION_AMDGPU_HSA_V5; 104 default: 105 report_fatal_error(Twine("Unsupported AMDHSA Code Object Version ") + 106 Twine(AmdhsaCodeObjectVersion)); 107 } 108 } 109 110 bool isHsaAbiVersion2(const MCSubtargetInfo *STI) { 111 if (Optional<uint8_t> HsaAbiVer = getHsaAbiVersion(STI)) 112 return *HsaAbiVer == ELF::ELFABIVERSION_AMDGPU_HSA_V2; 113 return false; 114 } 115 116 bool isHsaAbiVersion3(const MCSubtargetInfo *STI) { 117 if (Optional<uint8_t> HsaAbiVer = getHsaAbiVersion(STI)) 118 return *HsaAbiVer == ELF::ELFABIVERSION_AMDGPU_HSA_V3; 119 return false; 120 } 121 122 bool isHsaAbiVersion4(const MCSubtargetInfo *STI) { 123 if (Optional<uint8_t> HsaAbiVer = getHsaAbiVersion(STI)) 124 return *HsaAbiVer == ELF::ELFABIVERSION_AMDGPU_HSA_V4; 125 return false; 126 } 127 128 bool isHsaAbiVersion5(const MCSubtargetInfo *STI) { 129 if (Optional<uint8_t> HsaAbiVer = getHsaAbiVersion(STI)) 130 return *HsaAbiVer == ELF::ELFABIVERSION_AMDGPU_HSA_V5; 131 return false; 132 } 133 134 bool isHsaAbiVersion3AndAbove(const MCSubtargetInfo *STI) { 135 return isHsaAbiVersion3(STI) || isHsaAbiVersion4(STI) || 136 isHsaAbiVersion5(STI); 137 } 138 139 unsigned getAmdhsaCodeObjectVersion() { 140 return AmdhsaCodeObjectVersion; 141 } 142 143 // FIXME: All such magic numbers about the ABI should be in a 144 // central TD file. 145 unsigned getHostcallImplicitArgPosition() { 146 switch (AmdhsaCodeObjectVersion) { 147 case 2: 148 case 3: 149 case 4: 150 return 24; 151 case 5: 152 return AMDGPU::ImplicitArg::HOSTCALL_PTR_OFFSET; 153 default: 154 llvm_unreachable("Unexpected code object version"); 155 return 0; 156 } 157 } 158 159 #define GET_MIMGBaseOpcodesTable_IMPL 160 #define GET_MIMGDimInfoTable_IMPL 161 #define GET_MIMGInfoTable_IMPL 162 #define GET_MIMGLZMappingTable_IMPL 163 #define GET_MIMGMIPMappingTable_IMPL 164 #define GET_MIMGBiasMappingTable_IMPL 165 #define GET_MIMGOffsetMappingTable_IMPL 166 #define GET_MIMGG16MappingTable_IMPL 167 #include "AMDGPUGenSearchableTables.inc" 168 169 int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, 170 unsigned VDataDwords, unsigned VAddrDwords) { 171 const MIMGInfo *Info = getMIMGOpcodeHelper(BaseOpcode, MIMGEncoding, 172 VDataDwords, VAddrDwords); 173 return Info ? Info->Opcode : -1; 174 } 175 176 const MIMGBaseOpcodeInfo *getMIMGBaseOpcode(unsigned Opc) { 177 const MIMGInfo *Info = getMIMGInfo(Opc); 178 return Info ? getMIMGBaseOpcodeInfo(Info->BaseOpcode) : nullptr; 179 } 180 181 int getMaskedMIMGOp(unsigned Opc, unsigned NewChannels) { 182 const MIMGInfo *OrigInfo = getMIMGInfo(Opc); 183 const MIMGInfo *NewInfo = 184 getMIMGOpcodeHelper(OrigInfo->BaseOpcode, OrigInfo->MIMGEncoding, 185 NewChannels, OrigInfo->VAddrDwords); 186 return NewInfo ? NewInfo->Opcode : -1; 187 } 188 189 unsigned getAddrSizeMIMGOp(const MIMGBaseOpcodeInfo *BaseOpcode, 190 const MIMGDimInfo *Dim, bool IsA16, 191 bool IsG16Supported) { 192 unsigned AddrWords = BaseOpcode->NumExtraArgs; 193 unsigned AddrComponents = (BaseOpcode->Coordinates ? Dim->NumCoords : 0) + 194 (BaseOpcode->LodOrClampOrMip ? 1 : 0); 195 if (IsA16) 196 AddrWords += divideCeil(AddrComponents, 2); 197 else 198 AddrWords += AddrComponents; 199 200 // Note: For subtargets that support A16 but not G16, enabling A16 also 201 // enables 16 bit gradients. 202 // For subtargets that support A16 (operand) and G16 (done with a different 203 // instruction encoding), they are independent. 204 205 if (BaseOpcode->Gradients) { 206 if ((IsA16 && !IsG16Supported) || BaseOpcode->G16) 207 // There are two gradients per coordinate, we pack them separately. 208 // For the 3d case, 209 // we get (dy/du, dx/du) (-, dz/du) (dy/dv, dx/dv) (-, dz/dv) 210 AddrWords += alignTo<2>(Dim->NumGradients / 2); 211 else 212 AddrWords += Dim->NumGradients; 213 } 214 return AddrWords; 215 } 216 217 struct MUBUFInfo { 218 uint16_t Opcode; 219 uint16_t BaseOpcode; 220 uint8_t elements; 221 bool has_vaddr; 222 bool has_srsrc; 223 bool has_soffset; 224 bool IsBufferInv; 225 }; 226 227 struct MTBUFInfo { 228 uint16_t Opcode; 229 uint16_t BaseOpcode; 230 uint8_t elements; 231 bool has_vaddr; 232 bool has_srsrc; 233 bool has_soffset; 234 }; 235 236 struct SMInfo { 237 uint16_t Opcode; 238 bool IsBuffer; 239 }; 240 241 struct VOPInfo { 242 uint16_t Opcode; 243 bool IsSingle; 244 }; 245 246 #define GET_MTBUFInfoTable_DECL 247 #define GET_MTBUFInfoTable_IMPL 248 #define GET_MUBUFInfoTable_DECL 249 #define GET_MUBUFInfoTable_IMPL 250 #define GET_SMInfoTable_DECL 251 #define GET_SMInfoTable_IMPL 252 #define GET_VOP1InfoTable_DECL 253 #define GET_VOP1InfoTable_IMPL 254 #define GET_VOP2InfoTable_DECL 255 #define GET_VOP2InfoTable_IMPL 256 #define GET_VOP3InfoTable_DECL 257 #define GET_VOP3InfoTable_IMPL 258 #include "AMDGPUGenSearchableTables.inc" 259 260 int getMTBUFBaseOpcode(unsigned Opc) { 261 const MTBUFInfo *Info = getMTBUFInfoFromOpcode(Opc); 262 return Info ? Info->BaseOpcode : -1; 263 } 264 265 int getMTBUFOpcode(unsigned BaseOpc, unsigned Elements) { 266 const MTBUFInfo *Info = getMTBUFInfoFromBaseOpcodeAndElements(BaseOpc, Elements); 267 return Info ? Info->Opcode : -1; 268 } 269 270 int getMTBUFElements(unsigned Opc) { 271 const MTBUFInfo *Info = getMTBUFOpcodeHelper(Opc); 272 return Info ? Info->elements : 0; 273 } 274 275 bool getMTBUFHasVAddr(unsigned Opc) { 276 const MTBUFInfo *Info = getMTBUFOpcodeHelper(Opc); 277 return Info ? Info->has_vaddr : false; 278 } 279 280 bool getMTBUFHasSrsrc(unsigned Opc) { 281 const MTBUFInfo *Info = getMTBUFOpcodeHelper(Opc); 282 return Info ? Info->has_srsrc : false; 283 } 284 285 bool getMTBUFHasSoffset(unsigned Opc) { 286 const MTBUFInfo *Info = getMTBUFOpcodeHelper(Opc); 287 return Info ? Info->has_soffset : false; 288 } 289 290 int getMUBUFBaseOpcode(unsigned Opc) { 291 const MUBUFInfo *Info = getMUBUFInfoFromOpcode(Opc); 292 return Info ? Info->BaseOpcode : -1; 293 } 294 295 int getMUBUFOpcode(unsigned BaseOpc, unsigned Elements) { 296 const MUBUFInfo *Info = getMUBUFInfoFromBaseOpcodeAndElements(BaseOpc, Elements); 297 return Info ? Info->Opcode : -1; 298 } 299 300 int getMUBUFElements(unsigned Opc) { 301 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc); 302 return Info ? Info->elements : 0; 303 } 304 305 bool getMUBUFHasVAddr(unsigned Opc) { 306 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc); 307 return Info ? Info->has_vaddr : false; 308 } 309 310 bool getMUBUFHasSrsrc(unsigned Opc) { 311 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc); 312 return Info ? Info->has_srsrc : false; 313 } 314 315 bool getMUBUFHasSoffset(unsigned Opc) { 316 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc); 317 return Info ? Info->has_soffset : false; 318 } 319 320 bool getMUBUFIsBufferInv(unsigned Opc) { 321 const MUBUFInfo *Info = getMUBUFOpcodeHelper(Opc); 322 return Info ? Info->IsBufferInv : false; 323 } 324 325 bool getSMEMIsBuffer(unsigned Opc) { 326 const SMInfo *Info = getSMEMOpcodeHelper(Opc); 327 return Info ? Info->IsBuffer : false; 328 } 329 330 bool getVOP1IsSingle(unsigned Opc) { 331 const VOPInfo *Info = getVOP1OpcodeHelper(Opc); 332 return Info ? Info->IsSingle : false; 333 } 334 335 bool getVOP2IsSingle(unsigned Opc) { 336 const VOPInfo *Info = getVOP2OpcodeHelper(Opc); 337 return Info ? Info->IsSingle : false; 338 } 339 340 bool getVOP3IsSingle(unsigned Opc) { 341 const VOPInfo *Info = getVOP3OpcodeHelper(Opc); 342 return Info ? Info->IsSingle : false; 343 } 344 345 // Wrapper for Tablegen'd function. enum Subtarget is not defined in any 346 // header files, so we need to wrap it in a function that takes unsigned 347 // instead. 348 int getMCOpcode(uint16_t Opcode, unsigned Gen) { 349 return getMCOpcodeGen(Opcode, static_cast<Subtarget>(Gen)); 350 } 351 352 namespace IsaInfo { 353 354 AMDGPUTargetID::AMDGPUTargetID(const MCSubtargetInfo &STI) 355 : STI(STI), XnackSetting(TargetIDSetting::Any), 356 SramEccSetting(TargetIDSetting::Any) { 357 if (!STI.getFeatureBits().test(FeatureSupportsXNACK)) 358 XnackSetting = TargetIDSetting::Unsupported; 359 if (!STI.getFeatureBits().test(FeatureSupportsSRAMECC)) 360 SramEccSetting = TargetIDSetting::Unsupported; 361 } 362 363 void AMDGPUTargetID::setTargetIDFromFeaturesString(StringRef FS) { 364 // Check if xnack or sramecc is explicitly enabled or disabled. In the 365 // absence of the target features we assume we must generate code that can run 366 // in any environment. 367 SubtargetFeatures Features(FS); 368 Optional<bool> XnackRequested; 369 Optional<bool> SramEccRequested; 370 371 for (const std::string &Feature : Features.getFeatures()) { 372 if (Feature == "+xnack") 373 XnackRequested = true; 374 else if (Feature == "-xnack") 375 XnackRequested = false; 376 else if (Feature == "+sramecc") 377 SramEccRequested = true; 378 else if (Feature == "-sramecc") 379 SramEccRequested = false; 380 } 381 382 bool XnackSupported = isXnackSupported(); 383 bool SramEccSupported = isSramEccSupported(); 384 385 if (XnackRequested) { 386 if (XnackSupported) { 387 XnackSetting = 388 *XnackRequested ? TargetIDSetting::On : TargetIDSetting::Off; 389 } else { 390 // If a specific xnack setting was requested and this GPU does not support 391 // xnack emit a warning. Setting will remain set to "Unsupported". 392 if (*XnackRequested) { 393 errs() << "warning: xnack 'On' was requested for a processor that does " 394 "not support it!\n"; 395 } else { 396 errs() << "warning: xnack 'Off' was requested for a processor that " 397 "does not support it!\n"; 398 } 399 } 400 } 401 402 if (SramEccRequested) { 403 if (SramEccSupported) { 404 SramEccSetting = 405 *SramEccRequested ? TargetIDSetting::On : TargetIDSetting::Off; 406 } else { 407 // If a specific sramecc setting was requested and this GPU does not 408 // support sramecc emit a warning. Setting will remain set to 409 // "Unsupported". 410 if (*SramEccRequested) { 411 errs() << "warning: sramecc 'On' was requested for a processor that " 412 "does not support it!\n"; 413 } else { 414 errs() << "warning: sramecc 'Off' was requested for a processor that " 415 "does not support it!\n"; 416 } 417 } 418 } 419 } 420 421 static TargetIDSetting 422 getTargetIDSettingFromFeatureString(StringRef FeatureString) { 423 if (FeatureString.endswith("-")) 424 return TargetIDSetting::Off; 425 if (FeatureString.endswith("+")) 426 return TargetIDSetting::On; 427 428 llvm_unreachable("Malformed feature string"); 429 } 430 431 void AMDGPUTargetID::setTargetIDFromTargetIDStream(StringRef TargetID) { 432 SmallVector<StringRef, 3> TargetIDSplit; 433 TargetID.split(TargetIDSplit, ':'); 434 435 for (const auto &FeatureString : TargetIDSplit) { 436 if (FeatureString.startswith("xnack")) 437 XnackSetting = getTargetIDSettingFromFeatureString(FeatureString); 438 if (FeatureString.startswith("sramecc")) 439 SramEccSetting = getTargetIDSettingFromFeatureString(FeatureString); 440 } 441 } 442 443 std::string AMDGPUTargetID::toString() const { 444 std::string StringRep; 445 raw_string_ostream StreamRep(StringRep); 446 447 auto TargetTriple = STI.getTargetTriple(); 448 auto Version = getIsaVersion(STI.getCPU()); 449 450 StreamRep << TargetTriple.getArchName() << '-' 451 << TargetTriple.getVendorName() << '-' 452 << TargetTriple.getOSName() << '-' 453 << TargetTriple.getEnvironmentName() << '-'; 454 455 std::string Processor; 456 // TODO: Following else statement is present here because we used various 457 // alias names for GPUs up until GFX9 (e.g. 'fiji' is same as 'gfx803'). 458 // Remove once all aliases are removed from GCNProcessors.td. 459 if (Version.Major >= 9) 460 Processor = STI.getCPU().str(); 461 else 462 Processor = (Twine("gfx") + Twine(Version.Major) + Twine(Version.Minor) + 463 Twine(Version.Stepping)) 464 .str(); 465 466 std::string Features; 467 if (Optional<uint8_t> HsaAbiVersion = getHsaAbiVersion(&STI)) { 468 switch (*HsaAbiVersion) { 469 case ELF::ELFABIVERSION_AMDGPU_HSA_V2: 470 // Code object V2 only supported specific processors and had fixed 471 // settings for the XNACK. 472 if (Processor == "gfx600") { 473 } else if (Processor == "gfx601") { 474 } else if (Processor == "gfx602") { 475 } else if (Processor == "gfx700") { 476 } else if (Processor == "gfx701") { 477 } else if (Processor == "gfx702") { 478 } else if (Processor == "gfx703") { 479 } else if (Processor == "gfx704") { 480 } else if (Processor == "gfx705") { 481 } else if (Processor == "gfx801") { 482 if (!isXnackOnOrAny()) 483 report_fatal_error( 484 "AMD GPU code object V2 does not support processor " + 485 Twine(Processor) + " without XNACK"); 486 } else if (Processor == "gfx802") { 487 } else if (Processor == "gfx803") { 488 } else if (Processor == "gfx805") { 489 } else if (Processor == "gfx810") { 490 if (!isXnackOnOrAny()) 491 report_fatal_error( 492 "AMD GPU code object V2 does not support processor " + 493 Twine(Processor) + " without XNACK"); 494 } else if (Processor == "gfx900") { 495 if (isXnackOnOrAny()) 496 Processor = "gfx901"; 497 } else if (Processor == "gfx902") { 498 if (isXnackOnOrAny()) 499 Processor = "gfx903"; 500 } else if (Processor == "gfx904") { 501 if (isXnackOnOrAny()) 502 Processor = "gfx905"; 503 } else if (Processor == "gfx906") { 504 if (isXnackOnOrAny()) 505 Processor = "gfx907"; 506 } else if (Processor == "gfx90c") { 507 if (isXnackOnOrAny()) 508 report_fatal_error( 509 "AMD GPU code object V2 does not support processor " + 510 Twine(Processor) + " with XNACK being ON or ANY"); 511 } else { 512 report_fatal_error( 513 "AMD GPU code object V2 does not support processor " + 514 Twine(Processor)); 515 } 516 break; 517 case ELF::ELFABIVERSION_AMDGPU_HSA_V3: 518 // xnack. 519 if (isXnackOnOrAny()) 520 Features += "+xnack"; 521 // In code object v2 and v3, "sramecc" feature was spelled with a 522 // hyphen ("sram-ecc"). 523 if (isSramEccOnOrAny()) 524 Features += "+sram-ecc"; 525 break; 526 case ELF::ELFABIVERSION_AMDGPU_HSA_V4: 527 case ELF::ELFABIVERSION_AMDGPU_HSA_V5: 528 // sramecc. 529 if (getSramEccSetting() == TargetIDSetting::Off) 530 Features += ":sramecc-"; 531 else if (getSramEccSetting() == TargetIDSetting::On) 532 Features += ":sramecc+"; 533 // xnack. 534 if (getXnackSetting() == TargetIDSetting::Off) 535 Features += ":xnack-"; 536 else if (getXnackSetting() == TargetIDSetting::On) 537 Features += ":xnack+"; 538 break; 539 default: 540 break; 541 } 542 } 543 544 StreamRep << Processor << Features; 545 546 StreamRep.flush(); 547 return StringRep; 548 } 549 550 unsigned getWavefrontSize(const MCSubtargetInfo *STI) { 551 if (STI->getFeatureBits().test(FeatureWavefrontSize16)) 552 return 16; 553 if (STI->getFeatureBits().test(FeatureWavefrontSize32)) 554 return 32; 555 556 return 64; 557 } 558 559 unsigned getLocalMemorySize(const MCSubtargetInfo *STI) { 560 if (STI->getFeatureBits().test(FeatureLocalMemorySize32768)) 561 return 32768; 562 if (STI->getFeatureBits().test(FeatureLocalMemorySize65536)) 563 return 65536; 564 565 return 0; 566 } 567 568 unsigned getEUsPerCU(const MCSubtargetInfo *STI) { 569 // "Per CU" really means "per whatever functional block the waves of a 570 // workgroup must share". For gfx10 in CU mode this is the CU, which contains 571 // two SIMDs. 572 if (isGFX10Plus(*STI) && STI->getFeatureBits().test(FeatureCuMode)) 573 return 2; 574 // Pre-gfx10 a CU contains four SIMDs. For gfx10 in WGP mode the WGP contains 575 // two CUs, so a total of four SIMDs. 576 return 4; 577 } 578 579 unsigned getMaxWorkGroupsPerCU(const MCSubtargetInfo *STI, 580 unsigned FlatWorkGroupSize) { 581 assert(FlatWorkGroupSize != 0); 582 if (STI->getTargetTriple().getArch() != Triple::amdgcn) 583 return 8; 584 unsigned N = getWavesPerWorkGroup(STI, FlatWorkGroupSize); 585 if (N == 1) 586 return 40; 587 N = 40 / N; 588 return std::min(N, 16u); 589 } 590 591 unsigned getMinWavesPerEU(const MCSubtargetInfo *STI) { 592 return 1; 593 } 594 595 unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI) { 596 // FIXME: Need to take scratch memory into account. 597 if (isGFX90A(*STI)) 598 return 8; 599 if (!isGFX10Plus(*STI)) 600 return 10; 601 return hasGFX10_3Insts(*STI) ? 16 : 20; 602 } 603 604 unsigned getWavesPerEUForWorkGroup(const MCSubtargetInfo *STI, 605 unsigned FlatWorkGroupSize) { 606 return divideCeil(getWavesPerWorkGroup(STI, FlatWorkGroupSize), 607 getEUsPerCU(STI)); 608 } 609 610 unsigned getMinFlatWorkGroupSize(const MCSubtargetInfo *STI) { 611 return 1; 612 } 613 614 unsigned getMaxFlatWorkGroupSize(const MCSubtargetInfo *STI) { 615 // Some subtargets allow encoding 2048, but this isn't tested or supported. 616 return 1024; 617 } 618 619 unsigned getWavesPerWorkGroup(const MCSubtargetInfo *STI, 620 unsigned FlatWorkGroupSize) { 621 return divideCeil(FlatWorkGroupSize, getWavefrontSize(STI)); 622 } 623 624 unsigned getSGPRAllocGranule(const MCSubtargetInfo *STI) { 625 IsaVersion Version = getIsaVersion(STI->getCPU()); 626 if (Version.Major >= 10) 627 return getAddressableNumSGPRs(STI); 628 if (Version.Major >= 8) 629 return 16; 630 return 8; 631 } 632 633 unsigned getSGPREncodingGranule(const MCSubtargetInfo *STI) { 634 return 8; 635 } 636 637 unsigned getTotalNumSGPRs(const MCSubtargetInfo *STI) { 638 IsaVersion Version = getIsaVersion(STI->getCPU()); 639 if (Version.Major >= 8) 640 return 800; 641 return 512; 642 } 643 644 unsigned getAddressableNumSGPRs(const MCSubtargetInfo *STI) { 645 if (STI->getFeatureBits().test(FeatureSGPRInitBug)) 646 return FIXED_NUM_SGPRS_FOR_INIT_BUG; 647 648 IsaVersion Version = getIsaVersion(STI->getCPU()); 649 if (Version.Major >= 10) 650 return 106; 651 if (Version.Major >= 8) 652 return 102; 653 return 104; 654 } 655 656 unsigned getMinNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) { 657 assert(WavesPerEU != 0); 658 659 IsaVersion Version = getIsaVersion(STI->getCPU()); 660 if (Version.Major >= 10) 661 return 0; 662 663 if (WavesPerEU >= getMaxWavesPerEU(STI)) 664 return 0; 665 666 unsigned MinNumSGPRs = getTotalNumSGPRs(STI) / (WavesPerEU + 1); 667 if (STI->getFeatureBits().test(FeatureTrapHandler)) 668 MinNumSGPRs -= std::min(MinNumSGPRs, (unsigned)TRAP_NUM_SGPRS); 669 MinNumSGPRs = alignDown(MinNumSGPRs, getSGPRAllocGranule(STI)) + 1; 670 return std::min(MinNumSGPRs, getAddressableNumSGPRs(STI)); 671 } 672 673 unsigned getMaxNumSGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU, 674 bool Addressable) { 675 assert(WavesPerEU != 0); 676 677 unsigned AddressableNumSGPRs = getAddressableNumSGPRs(STI); 678 IsaVersion Version = getIsaVersion(STI->getCPU()); 679 if (Version.Major >= 10) 680 return Addressable ? AddressableNumSGPRs : 108; 681 if (Version.Major >= 8 && !Addressable) 682 AddressableNumSGPRs = 112; 683 unsigned MaxNumSGPRs = getTotalNumSGPRs(STI) / WavesPerEU; 684 if (STI->getFeatureBits().test(FeatureTrapHandler)) 685 MaxNumSGPRs -= std::min(MaxNumSGPRs, (unsigned)TRAP_NUM_SGPRS); 686 MaxNumSGPRs = alignDown(MaxNumSGPRs, getSGPRAllocGranule(STI)); 687 return std::min(MaxNumSGPRs, AddressableNumSGPRs); 688 } 689 690 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed, 691 bool FlatScrUsed, bool XNACKUsed) { 692 unsigned ExtraSGPRs = 0; 693 if (VCCUsed) 694 ExtraSGPRs = 2; 695 696 IsaVersion Version = getIsaVersion(STI->getCPU()); 697 if (Version.Major >= 10) 698 return ExtraSGPRs; 699 700 if (Version.Major < 8) { 701 if (FlatScrUsed) 702 ExtraSGPRs = 4; 703 } else { 704 if (XNACKUsed) 705 ExtraSGPRs = 4; 706 707 if (FlatScrUsed || 708 STI->getFeatureBits().test(AMDGPU::FeatureArchitectedFlatScratch)) 709 ExtraSGPRs = 6; 710 } 711 712 return ExtraSGPRs; 713 } 714 715 unsigned getNumExtraSGPRs(const MCSubtargetInfo *STI, bool VCCUsed, 716 bool FlatScrUsed) { 717 return getNumExtraSGPRs(STI, VCCUsed, FlatScrUsed, 718 STI->getFeatureBits().test(AMDGPU::FeatureXNACK)); 719 } 720 721 unsigned getNumSGPRBlocks(const MCSubtargetInfo *STI, unsigned NumSGPRs) { 722 NumSGPRs = alignTo(std::max(1u, NumSGPRs), getSGPREncodingGranule(STI)); 723 // SGPRBlocks is actual number of SGPR blocks minus 1. 724 return NumSGPRs / getSGPREncodingGranule(STI) - 1; 725 } 726 727 unsigned getVGPRAllocGranule(const MCSubtargetInfo *STI, 728 Optional<bool> EnableWavefrontSize32) { 729 if (STI->getFeatureBits().test(FeatureGFX90AInsts)) 730 return 8; 731 732 bool IsWave32 = EnableWavefrontSize32 ? 733 *EnableWavefrontSize32 : 734 STI->getFeatureBits().test(FeatureWavefrontSize32); 735 736 if (hasGFX10_3Insts(*STI)) 737 return IsWave32 ? 16 : 8; 738 739 return IsWave32 ? 8 : 4; 740 } 741 742 unsigned getVGPREncodingGranule(const MCSubtargetInfo *STI, 743 Optional<bool> EnableWavefrontSize32) { 744 if (STI->getFeatureBits().test(FeatureGFX90AInsts)) 745 return 8; 746 747 bool IsWave32 = EnableWavefrontSize32 ? 748 *EnableWavefrontSize32 : 749 STI->getFeatureBits().test(FeatureWavefrontSize32); 750 751 return IsWave32 ? 8 : 4; 752 } 753 754 unsigned getTotalNumVGPRs(const MCSubtargetInfo *STI) { 755 if (STI->getFeatureBits().test(FeatureGFX90AInsts)) 756 return 512; 757 if (!isGFX10Plus(*STI)) 758 return 256; 759 return STI->getFeatureBits().test(FeatureWavefrontSize32) ? 1024 : 512; 760 } 761 762 unsigned getAddressableNumVGPRs(const MCSubtargetInfo *STI) { 763 if (STI->getFeatureBits().test(FeatureGFX90AInsts)) 764 return 512; 765 return 256; 766 } 767 768 unsigned getMinNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) { 769 assert(WavesPerEU != 0); 770 771 if (WavesPerEU >= getMaxWavesPerEU(STI)) 772 return 0; 773 unsigned MinNumVGPRs = 774 alignDown(getTotalNumVGPRs(STI) / (WavesPerEU + 1), 775 getVGPRAllocGranule(STI)) + 1; 776 return std::min(MinNumVGPRs, getAddressableNumVGPRs(STI)); 777 } 778 779 unsigned getMaxNumVGPRs(const MCSubtargetInfo *STI, unsigned WavesPerEU) { 780 assert(WavesPerEU != 0); 781 782 unsigned MaxNumVGPRs = alignDown(getTotalNumVGPRs(STI) / WavesPerEU, 783 getVGPRAllocGranule(STI)); 784 unsigned AddressableNumVGPRs = getAddressableNumVGPRs(STI); 785 return std::min(MaxNumVGPRs, AddressableNumVGPRs); 786 } 787 788 unsigned getNumVGPRBlocks(const MCSubtargetInfo *STI, unsigned NumVGPRs, 789 Optional<bool> EnableWavefrontSize32) { 790 NumVGPRs = alignTo(std::max(1u, NumVGPRs), 791 getVGPREncodingGranule(STI, EnableWavefrontSize32)); 792 // VGPRBlocks is actual number of VGPR blocks minus 1. 793 return NumVGPRs / getVGPREncodingGranule(STI, EnableWavefrontSize32) - 1; 794 } 795 796 } // end namespace IsaInfo 797 798 void initDefaultAMDKernelCodeT(amd_kernel_code_t &Header, 799 const MCSubtargetInfo *STI) { 800 IsaVersion Version = getIsaVersion(STI->getCPU()); 801 802 memset(&Header, 0, sizeof(Header)); 803 804 Header.amd_kernel_code_version_major = 1; 805 Header.amd_kernel_code_version_minor = 2; 806 Header.amd_machine_kind = 1; // AMD_MACHINE_KIND_AMDGPU 807 Header.amd_machine_version_major = Version.Major; 808 Header.amd_machine_version_minor = Version.Minor; 809 Header.amd_machine_version_stepping = Version.Stepping; 810 Header.kernel_code_entry_byte_offset = sizeof(Header); 811 Header.wavefront_size = 6; 812 813 // If the code object does not support indirect functions, then the value must 814 // be 0xffffffff. 815 Header.call_convention = -1; 816 817 // These alignment values are specified in powers of two, so alignment = 818 // 2^n. The minimum alignment is 2^4 = 16. 819 Header.kernarg_segment_alignment = 4; 820 Header.group_segment_alignment = 4; 821 Header.private_segment_alignment = 4; 822 823 if (Version.Major >= 10) { 824 if (STI->getFeatureBits().test(FeatureWavefrontSize32)) { 825 Header.wavefront_size = 5; 826 Header.code_properties |= AMD_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32; 827 } 828 Header.compute_pgm_resource_registers |= 829 S_00B848_WGP_MODE(STI->getFeatureBits().test(FeatureCuMode) ? 0 : 1) | 830 S_00B848_MEM_ORDERED(1); 831 } 832 } 833 834 amdhsa::kernel_descriptor_t getDefaultAmdhsaKernelDescriptor( 835 const MCSubtargetInfo *STI) { 836 IsaVersion Version = getIsaVersion(STI->getCPU()); 837 838 amdhsa::kernel_descriptor_t KD; 839 memset(&KD, 0, sizeof(KD)); 840 841 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1, 842 amdhsa::COMPUTE_PGM_RSRC1_FLOAT_DENORM_MODE_16_64, 843 amdhsa::FLOAT_DENORM_MODE_FLUSH_NONE); 844 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1, 845 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_DX10_CLAMP, 1); 846 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1, 847 amdhsa::COMPUTE_PGM_RSRC1_ENABLE_IEEE_MODE, 1); 848 AMDHSA_BITS_SET(KD.compute_pgm_rsrc2, 849 amdhsa::COMPUTE_PGM_RSRC2_ENABLE_SGPR_WORKGROUP_ID_X, 1); 850 if (Version.Major >= 10) { 851 AMDHSA_BITS_SET(KD.kernel_code_properties, 852 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32, 853 STI->getFeatureBits().test(FeatureWavefrontSize32) ? 1 : 0); 854 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1, 855 amdhsa::COMPUTE_PGM_RSRC1_WGP_MODE, 856 STI->getFeatureBits().test(FeatureCuMode) ? 0 : 1); 857 AMDHSA_BITS_SET(KD.compute_pgm_rsrc1, 858 amdhsa::COMPUTE_PGM_RSRC1_MEM_ORDERED, 1); 859 } 860 if (AMDGPU::isGFX90A(*STI)) { 861 AMDHSA_BITS_SET(KD.compute_pgm_rsrc3, 862 amdhsa::COMPUTE_PGM_RSRC3_GFX90A_TG_SPLIT, 863 STI->getFeatureBits().test(FeatureTgSplit) ? 1 : 0); 864 } 865 return KD; 866 } 867 868 bool isGroupSegment(const GlobalValue *GV) { 869 return GV->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS; 870 } 871 872 bool isGlobalSegment(const GlobalValue *GV) { 873 return GV->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS; 874 } 875 876 bool isReadOnlySegment(const GlobalValue *GV) { 877 unsigned AS = GV->getAddressSpace(); 878 return AS == AMDGPUAS::CONSTANT_ADDRESS || 879 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT; 880 } 881 882 bool shouldEmitConstantsToTextSection(const Triple &TT) { 883 return TT.getArch() == Triple::r600; 884 } 885 886 int getIntegerAttribute(const Function &F, StringRef Name, int Default) { 887 Attribute A = F.getFnAttribute(Name); 888 int Result = Default; 889 890 if (A.isStringAttribute()) { 891 StringRef Str = A.getValueAsString(); 892 if (Str.getAsInteger(0, Result)) { 893 LLVMContext &Ctx = F.getContext(); 894 Ctx.emitError("can't parse integer attribute " + Name); 895 } 896 } 897 898 return Result; 899 } 900 901 std::pair<int, int> getIntegerPairAttribute(const Function &F, 902 StringRef Name, 903 std::pair<int, int> Default, 904 bool OnlyFirstRequired) { 905 Attribute A = F.getFnAttribute(Name); 906 if (!A.isStringAttribute()) 907 return Default; 908 909 LLVMContext &Ctx = F.getContext(); 910 std::pair<int, int> Ints = Default; 911 std::pair<StringRef, StringRef> Strs = A.getValueAsString().split(','); 912 if (Strs.first.trim().getAsInteger(0, Ints.first)) { 913 Ctx.emitError("can't parse first integer attribute " + Name); 914 return Default; 915 } 916 if (Strs.second.trim().getAsInteger(0, Ints.second)) { 917 if (!OnlyFirstRequired || !Strs.second.trim().empty()) { 918 Ctx.emitError("can't parse second integer attribute " + Name); 919 return Default; 920 } 921 } 922 923 return Ints; 924 } 925 926 unsigned getVmcntBitMask(const IsaVersion &Version) { 927 unsigned VmcntLo = (1 << getVmcntBitWidthLo()) - 1; 928 if (Version.Major < 9) 929 return VmcntLo; 930 931 unsigned VmcntHi = ((1 << getVmcntBitWidthHi()) - 1) << getVmcntBitWidthLo(); 932 return VmcntLo | VmcntHi; 933 } 934 935 unsigned getExpcntBitMask(const IsaVersion &Version) { 936 return (1 << getExpcntBitWidth()) - 1; 937 } 938 939 unsigned getLgkmcntBitMask(const IsaVersion &Version) { 940 return (1 << getLgkmcntBitWidth(Version.Major)) - 1; 941 } 942 943 unsigned getWaitcntBitMask(const IsaVersion &Version) { 944 unsigned VmcntLo = getBitMask(getVmcntBitShiftLo(), getVmcntBitWidthLo()); 945 unsigned Expcnt = getBitMask(getExpcntBitShift(), getExpcntBitWidth()); 946 unsigned Lgkmcnt = getBitMask(getLgkmcntBitShift(), 947 getLgkmcntBitWidth(Version.Major)); 948 unsigned Waitcnt = VmcntLo | Expcnt | Lgkmcnt; 949 if (Version.Major < 9) 950 return Waitcnt; 951 952 unsigned VmcntHi = getBitMask(getVmcntBitShiftHi(), getVmcntBitWidthHi()); 953 return Waitcnt | VmcntHi; 954 } 955 956 unsigned decodeVmcnt(const IsaVersion &Version, unsigned Waitcnt) { 957 unsigned VmcntLo = 958 unpackBits(Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo()); 959 if (Version.Major < 9) 960 return VmcntLo; 961 962 unsigned VmcntHi = 963 unpackBits(Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi()); 964 VmcntHi <<= getVmcntBitWidthLo(); 965 return VmcntLo | VmcntHi; 966 } 967 968 unsigned decodeExpcnt(const IsaVersion &Version, unsigned Waitcnt) { 969 return unpackBits(Waitcnt, getExpcntBitShift(), getExpcntBitWidth()); 970 } 971 972 unsigned decodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt) { 973 return unpackBits(Waitcnt, getLgkmcntBitShift(), 974 getLgkmcntBitWidth(Version.Major)); 975 } 976 977 void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt, 978 unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt) { 979 Vmcnt = decodeVmcnt(Version, Waitcnt); 980 Expcnt = decodeExpcnt(Version, Waitcnt); 981 Lgkmcnt = decodeLgkmcnt(Version, Waitcnt); 982 } 983 984 Waitcnt decodeWaitcnt(const IsaVersion &Version, unsigned Encoded) { 985 Waitcnt Decoded; 986 Decoded.VmCnt = decodeVmcnt(Version, Encoded); 987 Decoded.ExpCnt = decodeExpcnt(Version, Encoded); 988 Decoded.LgkmCnt = decodeLgkmcnt(Version, Encoded); 989 return Decoded; 990 } 991 992 unsigned encodeVmcnt(const IsaVersion &Version, unsigned Waitcnt, 993 unsigned Vmcnt) { 994 Waitcnt = 995 packBits(Vmcnt, Waitcnt, getVmcntBitShiftLo(), getVmcntBitWidthLo()); 996 if (Version.Major < 9) 997 return Waitcnt; 998 999 Vmcnt >>= getVmcntBitWidthLo(); 1000 return packBits(Vmcnt, Waitcnt, getVmcntBitShiftHi(), getVmcntBitWidthHi()); 1001 } 1002 1003 unsigned encodeExpcnt(const IsaVersion &Version, unsigned Waitcnt, 1004 unsigned Expcnt) { 1005 return packBits(Expcnt, Waitcnt, getExpcntBitShift(), getExpcntBitWidth()); 1006 } 1007 1008 unsigned encodeLgkmcnt(const IsaVersion &Version, unsigned Waitcnt, 1009 unsigned Lgkmcnt) { 1010 return packBits(Lgkmcnt, Waitcnt, getLgkmcntBitShift(), 1011 getLgkmcntBitWidth(Version.Major)); 1012 } 1013 1014 unsigned encodeWaitcnt(const IsaVersion &Version, 1015 unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt) { 1016 unsigned Waitcnt = getWaitcntBitMask(Version); 1017 Waitcnt = encodeVmcnt(Version, Waitcnt, Vmcnt); 1018 Waitcnt = encodeExpcnt(Version, Waitcnt, Expcnt); 1019 Waitcnt = encodeLgkmcnt(Version, Waitcnt, Lgkmcnt); 1020 return Waitcnt; 1021 } 1022 1023 unsigned encodeWaitcnt(const IsaVersion &Version, const Waitcnt &Decoded) { 1024 return encodeWaitcnt(Version, Decoded.VmCnt, Decoded.ExpCnt, Decoded.LgkmCnt); 1025 } 1026 1027 //===----------------------------------------------------------------------===// 1028 // Custom Operands. 1029 // 1030 // A table of custom operands shall describe "primary" operand names 1031 // first followed by aliases if any. It is not required but recommended 1032 // to arrange operands so that operand encoding match operand position 1033 // in the table. This will make disassembly a bit more efficient. 1034 // Unused slots in the table shall have an empty name. 1035 // 1036 //===----------------------------------------------------------------------===// 1037 1038 template <class T> 1039 static bool isValidOpr(int Idx, const CustomOperand<T> OpInfo[], int OpInfoSize, 1040 T Context) { 1041 return 0 <= Idx && Idx < OpInfoSize && !OpInfo[Idx].Name.empty() && 1042 (!OpInfo[Idx].Cond || OpInfo[Idx].Cond(Context)); 1043 } 1044 1045 template <class T> 1046 static int getOprIdx(std::function<bool(const CustomOperand<T> &)> Test, 1047 const CustomOperand<T> OpInfo[], int OpInfoSize, 1048 T Context) { 1049 int InvalidIdx = OPR_ID_UNKNOWN; 1050 for (int Idx = 0; Idx < OpInfoSize; ++Idx) { 1051 if (Test(OpInfo[Idx])) { 1052 if (!OpInfo[Idx].Cond || OpInfo[Idx].Cond(Context)) 1053 return Idx; 1054 InvalidIdx = OPR_ID_UNSUPPORTED; 1055 } 1056 } 1057 return InvalidIdx; 1058 } 1059 1060 template <class T> 1061 static int getOprIdx(const StringRef Name, const CustomOperand<T> OpInfo[], 1062 int OpInfoSize, T Context) { 1063 auto Test = [=](const CustomOperand<T> &Op) { return Op.Name == Name; }; 1064 return getOprIdx<T>(Test, OpInfo, OpInfoSize, Context); 1065 } 1066 1067 template <class T> 1068 static int getOprIdx(int Id, const CustomOperand<T> OpInfo[], int OpInfoSize, 1069 T Context, bool QuickCheck = true) { 1070 auto Test = [=](const CustomOperand<T> &Op) { 1071 return Op.Encoding == Id && !Op.Name.empty(); 1072 }; 1073 // This is an optimization that should work in most cases. 1074 // As a side effect, it may cause selection of an alias 1075 // instead of a primary operand name in case of sparse tables. 1076 if (QuickCheck && isValidOpr<T>(Id, OpInfo, OpInfoSize, Context) && 1077 OpInfo[Id].Encoding == Id) { 1078 return Id; 1079 } 1080 return getOprIdx<T>(Test, OpInfo, OpInfoSize, Context); 1081 } 1082 1083 //===----------------------------------------------------------------------===// 1084 // hwreg 1085 //===----------------------------------------------------------------------===// 1086 1087 namespace Hwreg { 1088 1089 int64_t getHwregId(const StringRef Name, const MCSubtargetInfo &STI) { 1090 int Idx = getOprIdx<const MCSubtargetInfo &>(Name, Opr, OPR_SIZE, STI); 1091 return (Idx < 0) ? Idx : Opr[Idx].Encoding; 1092 } 1093 1094 bool isValidHwreg(int64_t Id) { 1095 return 0 <= Id && isUInt<ID_WIDTH_>(Id); 1096 } 1097 1098 bool isValidHwregOffset(int64_t Offset) { 1099 return 0 <= Offset && isUInt<OFFSET_WIDTH_>(Offset); 1100 } 1101 1102 bool isValidHwregWidth(int64_t Width) { 1103 return 0 <= (Width - 1) && isUInt<WIDTH_M1_WIDTH_>(Width - 1); 1104 } 1105 1106 uint64_t encodeHwreg(uint64_t Id, uint64_t Offset, uint64_t Width) { 1107 return (Id << ID_SHIFT_) | 1108 (Offset << OFFSET_SHIFT_) | 1109 ((Width - 1) << WIDTH_M1_SHIFT_); 1110 } 1111 1112 StringRef getHwreg(unsigned Id, const MCSubtargetInfo &STI) { 1113 int Idx = getOprIdx<const MCSubtargetInfo &>(Id, Opr, OPR_SIZE, STI); 1114 return (Idx < 0) ? "" : Opr[Idx].Name; 1115 } 1116 1117 void decodeHwreg(unsigned Val, unsigned &Id, unsigned &Offset, unsigned &Width) { 1118 Id = (Val & ID_MASK_) >> ID_SHIFT_; 1119 Offset = (Val & OFFSET_MASK_) >> OFFSET_SHIFT_; 1120 Width = ((Val & WIDTH_M1_MASK_) >> WIDTH_M1_SHIFT_) + 1; 1121 } 1122 1123 } // namespace Hwreg 1124 1125 //===----------------------------------------------------------------------===// 1126 // exp tgt 1127 //===----------------------------------------------------------------------===// 1128 1129 namespace Exp { 1130 1131 struct ExpTgt { 1132 StringLiteral Name; 1133 unsigned Tgt; 1134 unsigned MaxIndex; 1135 }; 1136 1137 static constexpr ExpTgt ExpTgtInfo[] = { 1138 {{"null"}, ET_NULL, ET_NULL_MAX_IDX}, 1139 {{"mrtz"}, ET_MRTZ, ET_MRTZ_MAX_IDX}, 1140 {{"prim"}, ET_PRIM, ET_PRIM_MAX_IDX}, 1141 {{"mrt"}, ET_MRT0, ET_MRT_MAX_IDX}, 1142 {{"pos"}, ET_POS0, ET_POS_MAX_IDX}, 1143 {{"param"}, ET_PARAM0, ET_PARAM_MAX_IDX}, 1144 }; 1145 1146 bool getTgtName(unsigned Id, StringRef &Name, int &Index) { 1147 for (const ExpTgt &Val : ExpTgtInfo) { 1148 if (Val.Tgt <= Id && Id <= Val.Tgt + Val.MaxIndex) { 1149 Index = (Val.MaxIndex == 0) ? -1 : (Id - Val.Tgt); 1150 Name = Val.Name; 1151 return true; 1152 } 1153 } 1154 return false; 1155 } 1156 1157 unsigned getTgtId(const StringRef Name) { 1158 1159 for (const ExpTgt &Val : ExpTgtInfo) { 1160 if (Val.MaxIndex == 0 && Name == Val.Name) 1161 return Val.Tgt; 1162 1163 if (Val.MaxIndex > 0 && Name.startswith(Val.Name)) { 1164 StringRef Suffix = Name.drop_front(Val.Name.size()); 1165 1166 unsigned Id; 1167 if (Suffix.getAsInteger(10, Id) || Id > Val.MaxIndex) 1168 return ET_INVALID; 1169 1170 // Disable leading zeroes 1171 if (Suffix.size() > 1 && Suffix[0] == '0') 1172 return ET_INVALID; 1173 1174 return Val.Tgt + Id; 1175 } 1176 } 1177 return ET_INVALID; 1178 } 1179 1180 bool isSupportedTgtId(unsigned Id, const MCSubtargetInfo &STI) { 1181 return (Id != ET_POS4 && Id != ET_PRIM) || isGFX10Plus(STI); 1182 } 1183 1184 } // namespace Exp 1185 1186 //===----------------------------------------------------------------------===// 1187 // MTBUF Format 1188 //===----------------------------------------------------------------------===// 1189 1190 namespace MTBUFFormat { 1191 1192 int64_t getDfmt(const StringRef Name) { 1193 for (int Id = DFMT_MIN; Id <= DFMT_MAX; ++Id) { 1194 if (Name == DfmtSymbolic[Id]) 1195 return Id; 1196 } 1197 return DFMT_UNDEF; 1198 } 1199 1200 StringRef getDfmtName(unsigned Id) { 1201 assert(Id <= DFMT_MAX); 1202 return DfmtSymbolic[Id]; 1203 } 1204 1205 static StringLiteral const *getNfmtLookupTable(const MCSubtargetInfo &STI) { 1206 if (isSI(STI) || isCI(STI)) 1207 return NfmtSymbolicSICI; 1208 if (isVI(STI) || isGFX9(STI)) 1209 return NfmtSymbolicVI; 1210 return NfmtSymbolicGFX10; 1211 } 1212 1213 int64_t getNfmt(const StringRef Name, const MCSubtargetInfo &STI) { 1214 auto lookupTable = getNfmtLookupTable(STI); 1215 for (int Id = NFMT_MIN; Id <= NFMT_MAX; ++Id) { 1216 if (Name == lookupTable[Id]) 1217 return Id; 1218 } 1219 return NFMT_UNDEF; 1220 } 1221 1222 StringRef getNfmtName(unsigned Id, const MCSubtargetInfo &STI) { 1223 assert(Id <= NFMT_MAX); 1224 return getNfmtLookupTable(STI)[Id]; 1225 } 1226 1227 bool isValidDfmtNfmt(unsigned Id, const MCSubtargetInfo &STI) { 1228 unsigned Dfmt; 1229 unsigned Nfmt; 1230 decodeDfmtNfmt(Id, Dfmt, Nfmt); 1231 return isValidNfmt(Nfmt, STI); 1232 } 1233 1234 bool isValidNfmt(unsigned Id, const MCSubtargetInfo &STI) { 1235 return !getNfmtName(Id, STI).empty(); 1236 } 1237 1238 int64_t encodeDfmtNfmt(unsigned Dfmt, unsigned Nfmt) { 1239 return (Dfmt << DFMT_SHIFT) | (Nfmt << NFMT_SHIFT); 1240 } 1241 1242 void decodeDfmtNfmt(unsigned Format, unsigned &Dfmt, unsigned &Nfmt) { 1243 Dfmt = (Format >> DFMT_SHIFT) & DFMT_MASK; 1244 Nfmt = (Format >> NFMT_SHIFT) & NFMT_MASK; 1245 } 1246 1247 int64_t getUnifiedFormat(const StringRef Name) { 1248 for (int Id = UFMT_FIRST; Id <= UFMT_LAST; ++Id) { 1249 if (Name == UfmtSymbolic[Id]) 1250 return Id; 1251 } 1252 return UFMT_UNDEF; 1253 } 1254 1255 StringRef getUnifiedFormatName(unsigned Id) { 1256 return isValidUnifiedFormat(Id) ? UfmtSymbolic[Id] : ""; 1257 } 1258 1259 bool isValidUnifiedFormat(unsigned Id) { 1260 return Id <= UFMT_LAST; 1261 } 1262 1263 int64_t convertDfmtNfmt2Ufmt(unsigned Dfmt, unsigned Nfmt) { 1264 int64_t Fmt = encodeDfmtNfmt(Dfmt, Nfmt); 1265 for (int Id = UFMT_FIRST; Id <= UFMT_LAST; ++Id) { 1266 if (Fmt == DfmtNfmt2UFmt[Id]) 1267 return Id; 1268 } 1269 return UFMT_UNDEF; 1270 } 1271 1272 bool isValidFormatEncoding(unsigned Val, const MCSubtargetInfo &STI) { 1273 return isGFX10Plus(STI) ? (Val <= UFMT_MAX) : (Val <= DFMT_NFMT_MAX); 1274 } 1275 1276 unsigned getDefaultFormatEncoding(const MCSubtargetInfo &STI) { 1277 if (isGFX10Plus(STI)) 1278 return UFMT_DEFAULT; 1279 return DFMT_NFMT_DEFAULT; 1280 } 1281 1282 } // namespace MTBUFFormat 1283 1284 //===----------------------------------------------------------------------===// 1285 // SendMsg 1286 //===----------------------------------------------------------------------===// 1287 1288 namespace SendMsg { 1289 1290 int64_t getMsgId(const StringRef Name, const MCSubtargetInfo &STI) { 1291 int Idx = getOprIdx<const MCSubtargetInfo &>(Name, Msg, MSG_SIZE, STI); 1292 return (Idx < 0) ? Idx : Msg[Idx].Encoding; 1293 } 1294 1295 bool isValidMsgId(int64_t MsgId) { 1296 return 0 <= MsgId && isUInt<ID_WIDTH_>(MsgId); 1297 } 1298 1299 StringRef getMsgName(int64_t MsgId, const MCSubtargetInfo &STI) { 1300 int Idx = getOprIdx<const MCSubtargetInfo &>(MsgId, Msg, MSG_SIZE, STI); 1301 return (Idx < 0) ? "" : Msg[Idx].Name; 1302 } 1303 1304 int64_t getMsgOpId(int64_t MsgId, const StringRef Name) { 1305 const char* const *S = (MsgId == ID_SYSMSG) ? OpSysSymbolic : OpGsSymbolic; 1306 const int F = (MsgId == ID_SYSMSG) ? OP_SYS_FIRST_ : OP_GS_FIRST_; 1307 const int L = (MsgId == ID_SYSMSG) ? OP_SYS_LAST_ : OP_GS_LAST_; 1308 for (int i = F; i < L; ++i) { 1309 if (Name == S[i]) { 1310 return i; 1311 } 1312 } 1313 return OP_UNKNOWN_; 1314 } 1315 1316 bool isValidMsgOp(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI, 1317 bool Strict) { 1318 assert(isValidMsgId(MsgId)); 1319 1320 if (!Strict) 1321 return 0 <= OpId && isUInt<OP_WIDTH_>(OpId); 1322 1323 switch(MsgId) 1324 { 1325 case ID_GS: 1326 return (OP_GS_FIRST_ <= OpId && OpId < OP_GS_LAST_) && OpId != OP_GS_NOP; 1327 case ID_GS_DONE: 1328 return OP_GS_FIRST_ <= OpId && OpId < OP_GS_LAST_; 1329 case ID_SYSMSG: 1330 return OP_SYS_FIRST_ <= OpId && OpId < OP_SYS_LAST_; 1331 default: 1332 return OpId == OP_NONE_; 1333 } 1334 } 1335 1336 StringRef getMsgOpName(int64_t MsgId, int64_t OpId) { 1337 assert(msgRequiresOp(MsgId)); 1338 return (MsgId == ID_SYSMSG)? OpSysSymbolic[OpId] : OpGsSymbolic[OpId]; 1339 } 1340 1341 bool isValidMsgStream(int64_t MsgId, int64_t OpId, int64_t StreamId, 1342 const MCSubtargetInfo &STI, bool Strict) { 1343 assert(isValidMsgOp(MsgId, OpId, STI, Strict)); 1344 1345 if (!Strict) 1346 return 0 <= StreamId && isUInt<STREAM_ID_WIDTH_>(StreamId); 1347 1348 switch(MsgId) 1349 { 1350 case ID_GS: 1351 return STREAM_ID_FIRST_ <= StreamId && StreamId < STREAM_ID_LAST_; 1352 case ID_GS_DONE: 1353 return (OpId == OP_GS_NOP)? 1354 (StreamId == STREAM_ID_NONE_) : 1355 (STREAM_ID_FIRST_ <= StreamId && StreamId < STREAM_ID_LAST_); 1356 default: 1357 return StreamId == STREAM_ID_NONE_; 1358 } 1359 } 1360 1361 bool msgRequiresOp(int64_t MsgId) { 1362 return MsgId == ID_GS || MsgId == ID_GS_DONE || MsgId == ID_SYSMSG; 1363 } 1364 1365 bool msgSupportsStream(int64_t MsgId, int64_t OpId) { 1366 return (MsgId == ID_GS || MsgId == ID_GS_DONE) && OpId != OP_GS_NOP; 1367 } 1368 1369 void decodeMsg(unsigned Val, 1370 uint16_t &MsgId, 1371 uint16_t &OpId, 1372 uint16_t &StreamId) { 1373 MsgId = Val & ID_MASK_; 1374 OpId = (Val & OP_MASK_) >> OP_SHIFT_; 1375 StreamId = (Val & STREAM_ID_MASK_) >> STREAM_ID_SHIFT_; 1376 } 1377 1378 uint64_t encodeMsg(uint64_t MsgId, 1379 uint64_t OpId, 1380 uint64_t StreamId) { 1381 return (MsgId << ID_SHIFT_) | 1382 (OpId << OP_SHIFT_) | 1383 (StreamId << STREAM_ID_SHIFT_); 1384 } 1385 1386 } // namespace SendMsg 1387 1388 //===----------------------------------------------------------------------===// 1389 // 1390 //===----------------------------------------------------------------------===// 1391 1392 unsigned getInitialPSInputAddr(const Function &F) { 1393 return getIntegerAttribute(F, "InitialPSInputAddr", 0); 1394 } 1395 1396 bool getHasColorExport(const Function &F) { 1397 // As a safe default always respond as if PS has color exports. 1398 return getIntegerAttribute( 1399 F, "amdgpu-color-export", 1400 F.getCallingConv() == CallingConv::AMDGPU_PS ? 1 : 0) != 0; 1401 } 1402 1403 bool getHasDepthExport(const Function &F) { 1404 return getIntegerAttribute(F, "amdgpu-depth-export", 0) != 0; 1405 } 1406 1407 bool isShader(CallingConv::ID cc) { 1408 switch(cc) { 1409 case CallingConv::AMDGPU_VS: 1410 case CallingConv::AMDGPU_LS: 1411 case CallingConv::AMDGPU_HS: 1412 case CallingConv::AMDGPU_ES: 1413 case CallingConv::AMDGPU_GS: 1414 case CallingConv::AMDGPU_PS: 1415 case CallingConv::AMDGPU_CS: 1416 return true; 1417 default: 1418 return false; 1419 } 1420 } 1421 1422 bool isGraphics(CallingConv::ID cc) { 1423 return isShader(cc) || cc == CallingConv::AMDGPU_Gfx; 1424 } 1425 1426 bool isCompute(CallingConv::ID cc) { 1427 return !isGraphics(cc) || cc == CallingConv::AMDGPU_CS; 1428 } 1429 1430 bool isEntryFunctionCC(CallingConv::ID CC) { 1431 switch (CC) { 1432 case CallingConv::AMDGPU_KERNEL: 1433 case CallingConv::SPIR_KERNEL: 1434 case CallingConv::AMDGPU_VS: 1435 case CallingConv::AMDGPU_GS: 1436 case CallingConv::AMDGPU_PS: 1437 case CallingConv::AMDGPU_CS: 1438 case CallingConv::AMDGPU_ES: 1439 case CallingConv::AMDGPU_HS: 1440 case CallingConv::AMDGPU_LS: 1441 return true; 1442 default: 1443 return false; 1444 } 1445 } 1446 1447 bool isModuleEntryFunctionCC(CallingConv::ID CC) { 1448 switch (CC) { 1449 case CallingConv::AMDGPU_Gfx: 1450 return true; 1451 default: 1452 return isEntryFunctionCC(CC); 1453 } 1454 } 1455 1456 bool isKernelCC(const Function *Func) { 1457 return AMDGPU::isModuleEntryFunctionCC(Func->getCallingConv()); 1458 } 1459 1460 bool hasXNACK(const MCSubtargetInfo &STI) { 1461 return STI.getFeatureBits()[AMDGPU::FeatureXNACK]; 1462 } 1463 1464 bool hasSRAMECC(const MCSubtargetInfo &STI) { 1465 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC]; 1466 } 1467 1468 bool hasMIMG_R128(const MCSubtargetInfo &STI) { 1469 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128] && !STI.getFeatureBits()[AMDGPU::FeatureR128A16]; 1470 } 1471 1472 bool hasGFX10A16(const MCSubtargetInfo &STI) { 1473 return STI.getFeatureBits()[AMDGPU::FeatureGFX10A16]; 1474 } 1475 1476 bool hasG16(const MCSubtargetInfo &STI) { 1477 return STI.getFeatureBits()[AMDGPU::FeatureG16]; 1478 } 1479 1480 bool hasPackedD16(const MCSubtargetInfo &STI) { 1481 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem]; 1482 } 1483 1484 bool isSI(const MCSubtargetInfo &STI) { 1485 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands]; 1486 } 1487 1488 bool isCI(const MCSubtargetInfo &STI) { 1489 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands]; 1490 } 1491 1492 bool isVI(const MCSubtargetInfo &STI) { 1493 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands]; 1494 } 1495 1496 bool isGFX9(const MCSubtargetInfo &STI) { 1497 return STI.getFeatureBits()[AMDGPU::FeatureGFX9]; 1498 } 1499 1500 bool isGFX9_GFX10(const MCSubtargetInfo &STI) { 1501 return isGFX9(STI) || isGFX10(STI); 1502 } 1503 1504 bool isGFX8Plus(const MCSubtargetInfo &STI) { 1505 return isVI(STI) || isGFX9Plus(STI); 1506 } 1507 1508 bool isGFX9Plus(const MCSubtargetInfo &STI) { 1509 return isGFX9(STI) || isGFX10Plus(STI); 1510 } 1511 1512 bool isGFX10(const MCSubtargetInfo &STI) { 1513 return STI.getFeatureBits()[AMDGPU::FeatureGFX10]; 1514 } 1515 1516 bool isGFX10Plus(const MCSubtargetInfo &STI) { return isGFX10(STI); } 1517 1518 bool isNotGFX10Plus(const MCSubtargetInfo &STI) { 1519 return isSI(STI) || isCI(STI) || isVI(STI) || isGFX9(STI); 1520 } 1521 1522 bool isGFX10Before1030(const MCSubtargetInfo &STI) { 1523 return isGFX10(STI) && !AMDGPU::isGFX10_BEncoding(STI); 1524 } 1525 1526 bool isGCN3Encoding(const MCSubtargetInfo &STI) { 1527 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding]; 1528 } 1529 1530 bool isGFX10_AEncoding(const MCSubtargetInfo &STI) { 1531 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_AEncoding]; 1532 } 1533 1534 bool isGFX10_BEncoding(const MCSubtargetInfo &STI) { 1535 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_BEncoding]; 1536 } 1537 1538 bool hasGFX10_3Insts(const MCSubtargetInfo &STI) { 1539 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_3Insts]; 1540 } 1541 1542 bool isGFX90A(const MCSubtargetInfo &STI) { 1543 return STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts]; 1544 } 1545 1546 bool isGFX940(const MCSubtargetInfo &STI) { 1547 return STI.getFeatureBits()[AMDGPU::FeatureGFX940Insts]; 1548 } 1549 1550 bool hasArchitectedFlatScratch(const MCSubtargetInfo &STI) { 1551 return STI.getFeatureBits()[AMDGPU::FeatureArchitectedFlatScratch]; 1552 } 1553 1554 bool hasMAIInsts(const MCSubtargetInfo &STI) { 1555 return STI.getFeatureBits()[AMDGPU::FeatureMAIInsts]; 1556 } 1557 1558 int32_t getTotalNumVGPRs(bool has90AInsts, int32_t ArgNumAGPR, 1559 int32_t ArgNumVGPR) { 1560 if (has90AInsts && ArgNumAGPR) 1561 return alignTo(ArgNumVGPR, 4) + ArgNumAGPR; 1562 return std::max(ArgNumVGPR, ArgNumAGPR); 1563 } 1564 1565 bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) { 1566 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID); 1567 const unsigned FirstSubReg = TRI->getSubReg(Reg, AMDGPU::sub0); 1568 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) || 1569 Reg == AMDGPU::SCC; 1570 } 1571 1572 #define MAP_REG2REG \ 1573 using namespace AMDGPU; \ 1574 switch(Reg) { \ 1575 default: return Reg; \ 1576 CASE_CI_VI(FLAT_SCR) \ 1577 CASE_CI_VI(FLAT_SCR_LO) \ 1578 CASE_CI_VI(FLAT_SCR_HI) \ 1579 CASE_VI_GFX9PLUS(TTMP0) \ 1580 CASE_VI_GFX9PLUS(TTMP1) \ 1581 CASE_VI_GFX9PLUS(TTMP2) \ 1582 CASE_VI_GFX9PLUS(TTMP3) \ 1583 CASE_VI_GFX9PLUS(TTMP4) \ 1584 CASE_VI_GFX9PLUS(TTMP5) \ 1585 CASE_VI_GFX9PLUS(TTMP6) \ 1586 CASE_VI_GFX9PLUS(TTMP7) \ 1587 CASE_VI_GFX9PLUS(TTMP8) \ 1588 CASE_VI_GFX9PLUS(TTMP9) \ 1589 CASE_VI_GFX9PLUS(TTMP10) \ 1590 CASE_VI_GFX9PLUS(TTMP11) \ 1591 CASE_VI_GFX9PLUS(TTMP12) \ 1592 CASE_VI_GFX9PLUS(TTMP13) \ 1593 CASE_VI_GFX9PLUS(TTMP14) \ 1594 CASE_VI_GFX9PLUS(TTMP15) \ 1595 CASE_VI_GFX9PLUS(TTMP0_TTMP1) \ 1596 CASE_VI_GFX9PLUS(TTMP2_TTMP3) \ 1597 CASE_VI_GFX9PLUS(TTMP4_TTMP5) \ 1598 CASE_VI_GFX9PLUS(TTMP6_TTMP7) \ 1599 CASE_VI_GFX9PLUS(TTMP8_TTMP9) \ 1600 CASE_VI_GFX9PLUS(TTMP10_TTMP11) \ 1601 CASE_VI_GFX9PLUS(TTMP12_TTMP13) \ 1602 CASE_VI_GFX9PLUS(TTMP14_TTMP15) \ 1603 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3) \ 1604 CASE_VI_GFX9PLUS(TTMP4_TTMP5_TTMP6_TTMP7) \ 1605 CASE_VI_GFX9PLUS(TTMP8_TTMP9_TTMP10_TTMP11) \ 1606 CASE_VI_GFX9PLUS(TTMP12_TTMP13_TTMP14_TTMP15) \ 1607 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \ 1608 CASE_VI_GFX9PLUS(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \ 1609 CASE_VI_GFX9PLUS(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \ 1610 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \ 1611 } 1612 1613 #define CASE_CI_VI(node) \ 1614 assert(!isSI(STI)); \ 1615 case node: return isCI(STI) ? node##_ci : node##_vi; 1616 1617 #define CASE_VI_GFX9PLUS(node) \ 1618 case node: return isGFX9Plus(STI) ? node##_gfx9plus : node##_vi; 1619 1620 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) { 1621 if (STI.getTargetTriple().getArch() == Triple::r600) 1622 return Reg; 1623 MAP_REG2REG 1624 } 1625 1626 #undef CASE_CI_VI 1627 #undef CASE_VI_GFX9PLUS 1628 1629 #define CASE_CI_VI(node) case node##_ci: case node##_vi: return node; 1630 #define CASE_VI_GFX9PLUS(node) case node##_vi: case node##_gfx9plus: return node; 1631 1632 unsigned mc2PseudoReg(unsigned Reg) { 1633 MAP_REG2REG 1634 } 1635 1636 #undef CASE_CI_VI 1637 #undef CASE_VI_GFX9PLUS 1638 #undef MAP_REG2REG 1639 1640 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1641 assert(OpNo < Desc.NumOperands); 1642 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1643 return OpType >= AMDGPU::OPERAND_SRC_FIRST && 1644 OpType <= AMDGPU::OPERAND_SRC_LAST; 1645 } 1646 1647 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1648 assert(OpNo < Desc.NumOperands); 1649 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1650 switch (OpType) { 1651 case AMDGPU::OPERAND_REG_IMM_FP32: 1652 case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED: 1653 case AMDGPU::OPERAND_REG_IMM_FP64: 1654 case AMDGPU::OPERAND_REG_IMM_FP16: 1655 case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED: 1656 case AMDGPU::OPERAND_REG_IMM_V2FP16: 1657 case AMDGPU::OPERAND_REG_IMM_V2INT16: 1658 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 1659 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 1660 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 1661 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: 1662 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16: 1663 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 1664 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 1665 case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: 1666 case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16: 1667 case AMDGPU::OPERAND_REG_IMM_V2FP32: 1668 case AMDGPU::OPERAND_REG_INLINE_C_V2FP32: 1669 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: 1670 return true; 1671 default: 1672 return false; 1673 } 1674 } 1675 1676 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1677 assert(OpNo < Desc.NumOperands); 1678 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1679 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST && 1680 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST; 1681 } 1682 1683 // Avoid using MCRegisterClass::getSize, since that function will go away 1684 // (move from MC* level to Target* level). Return size in bits. 1685 unsigned getRegBitWidth(unsigned RCID) { 1686 switch (RCID) { 1687 case AMDGPU::VGPR_LO16RegClassID: 1688 case AMDGPU::VGPR_HI16RegClassID: 1689 case AMDGPU::SGPR_LO16RegClassID: 1690 case AMDGPU::AGPR_LO16RegClassID: 1691 return 16; 1692 case AMDGPU::SGPR_32RegClassID: 1693 case AMDGPU::VGPR_32RegClassID: 1694 case AMDGPU::VRegOrLds_32RegClassID: 1695 case AMDGPU::AGPR_32RegClassID: 1696 case AMDGPU::VS_32RegClassID: 1697 case AMDGPU::AV_32RegClassID: 1698 case AMDGPU::SReg_32RegClassID: 1699 case AMDGPU::SReg_32_XM0RegClassID: 1700 case AMDGPU::SRegOrLds_32RegClassID: 1701 return 32; 1702 case AMDGPU::SGPR_64RegClassID: 1703 case AMDGPU::VS_64RegClassID: 1704 case AMDGPU::SReg_64RegClassID: 1705 case AMDGPU::VReg_64RegClassID: 1706 case AMDGPU::AReg_64RegClassID: 1707 case AMDGPU::SReg_64_XEXECRegClassID: 1708 case AMDGPU::VReg_64_Align2RegClassID: 1709 case AMDGPU::AReg_64_Align2RegClassID: 1710 case AMDGPU::AV_64RegClassID: 1711 case AMDGPU::AV_64_Align2RegClassID: 1712 return 64; 1713 case AMDGPU::SGPR_96RegClassID: 1714 case AMDGPU::SReg_96RegClassID: 1715 case AMDGPU::VReg_96RegClassID: 1716 case AMDGPU::AReg_96RegClassID: 1717 case AMDGPU::VReg_96_Align2RegClassID: 1718 case AMDGPU::AReg_96_Align2RegClassID: 1719 case AMDGPU::AV_96RegClassID: 1720 case AMDGPU::AV_96_Align2RegClassID: 1721 return 96; 1722 case AMDGPU::SGPR_128RegClassID: 1723 case AMDGPU::SReg_128RegClassID: 1724 case AMDGPU::VReg_128RegClassID: 1725 case AMDGPU::AReg_128RegClassID: 1726 case AMDGPU::VReg_128_Align2RegClassID: 1727 case AMDGPU::AReg_128_Align2RegClassID: 1728 case AMDGPU::AV_128RegClassID: 1729 case AMDGPU::AV_128_Align2RegClassID: 1730 return 128; 1731 case AMDGPU::SGPR_160RegClassID: 1732 case AMDGPU::SReg_160RegClassID: 1733 case AMDGPU::VReg_160RegClassID: 1734 case AMDGPU::AReg_160RegClassID: 1735 case AMDGPU::VReg_160_Align2RegClassID: 1736 case AMDGPU::AReg_160_Align2RegClassID: 1737 case AMDGPU::AV_160RegClassID: 1738 case AMDGPU::AV_160_Align2RegClassID: 1739 return 160; 1740 case AMDGPU::SGPR_192RegClassID: 1741 case AMDGPU::SReg_192RegClassID: 1742 case AMDGPU::VReg_192RegClassID: 1743 case AMDGPU::AReg_192RegClassID: 1744 case AMDGPU::VReg_192_Align2RegClassID: 1745 case AMDGPU::AReg_192_Align2RegClassID: 1746 case AMDGPU::AV_192RegClassID: 1747 case AMDGPU::AV_192_Align2RegClassID: 1748 return 192; 1749 case AMDGPU::SGPR_224RegClassID: 1750 case AMDGPU::SReg_224RegClassID: 1751 case AMDGPU::VReg_224RegClassID: 1752 case AMDGPU::AReg_224RegClassID: 1753 case AMDGPU::VReg_224_Align2RegClassID: 1754 case AMDGPU::AReg_224_Align2RegClassID: 1755 case AMDGPU::AV_224RegClassID: 1756 case AMDGPU::AV_224_Align2RegClassID: 1757 return 224; 1758 case AMDGPU::SGPR_256RegClassID: 1759 case AMDGPU::SReg_256RegClassID: 1760 case AMDGPU::VReg_256RegClassID: 1761 case AMDGPU::AReg_256RegClassID: 1762 case AMDGPU::VReg_256_Align2RegClassID: 1763 case AMDGPU::AReg_256_Align2RegClassID: 1764 case AMDGPU::AV_256RegClassID: 1765 case AMDGPU::AV_256_Align2RegClassID: 1766 return 256; 1767 case AMDGPU::SGPR_512RegClassID: 1768 case AMDGPU::SReg_512RegClassID: 1769 case AMDGPU::VReg_512RegClassID: 1770 case AMDGPU::AReg_512RegClassID: 1771 case AMDGPU::VReg_512_Align2RegClassID: 1772 case AMDGPU::AReg_512_Align2RegClassID: 1773 case AMDGPU::AV_512RegClassID: 1774 case AMDGPU::AV_512_Align2RegClassID: 1775 return 512; 1776 case AMDGPU::SGPR_1024RegClassID: 1777 case AMDGPU::SReg_1024RegClassID: 1778 case AMDGPU::VReg_1024RegClassID: 1779 case AMDGPU::AReg_1024RegClassID: 1780 case AMDGPU::VReg_1024_Align2RegClassID: 1781 case AMDGPU::AReg_1024_Align2RegClassID: 1782 case AMDGPU::AV_1024RegClassID: 1783 case AMDGPU::AV_1024_Align2RegClassID: 1784 return 1024; 1785 default: 1786 llvm_unreachable("Unexpected register class"); 1787 } 1788 } 1789 1790 unsigned getRegBitWidth(const MCRegisterClass &RC) { 1791 return getRegBitWidth(RC.getID()); 1792 } 1793 1794 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc, 1795 unsigned OpNo) { 1796 assert(OpNo < Desc.NumOperands); 1797 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 1798 return getRegBitWidth(MRI->getRegClass(RCID)) / 8; 1799 } 1800 1801 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) { 1802 if (isInlinableIntLiteral(Literal)) 1803 return true; 1804 1805 uint64_t Val = static_cast<uint64_t>(Literal); 1806 return (Val == DoubleToBits(0.0)) || 1807 (Val == DoubleToBits(1.0)) || 1808 (Val == DoubleToBits(-1.0)) || 1809 (Val == DoubleToBits(0.5)) || 1810 (Val == DoubleToBits(-0.5)) || 1811 (Val == DoubleToBits(2.0)) || 1812 (Val == DoubleToBits(-2.0)) || 1813 (Val == DoubleToBits(4.0)) || 1814 (Val == DoubleToBits(-4.0)) || 1815 (Val == 0x3fc45f306dc9c882 && HasInv2Pi); 1816 } 1817 1818 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) { 1819 if (isInlinableIntLiteral(Literal)) 1820 return true; 1821 1822 // The actual type of the operand does not seem to matter as long 1823 // as the bits match one of the inline immediate values. For example: 1824 // 1825 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal, 1826 // so it is a legal inline immediate. 1827 // 1828 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in 1829 // floating-point, so it is a legal inline immediate. 1830 1831 uint32_t Val = static_cast<uint32_t>(Literal); 1832 return (Val == FloatToBits(0.0f)) || 1833 (Val == FloatToBits(1.0f)) || 1834 (Val == FloatToBits(-1.0f)) || 1835 (Val == FloatToBits(0.5f)) || 1836 (Val == FloatToBits(-0.5f)) || 1837 (Val == FloatToBits(2.0f)) || 1838 (Val == FloatToBits(-2.0f)) || 1839 (Val == FloatToBits(4.0f)) || 1840 (Val == FloatToBits(-4.0f)) || 1841 (Val == 0x3e22f983 && HasInv2Pi); 1842 } 1843 1844 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) { 1845 if (!HasInv2Pi) 1846 return false; 1847 1848 if (isInlinableIntLiteral(Literal)) 1849 return true; 1850 1851 uint16_t Val = static_cast<uint16_t>(Literal); 1852 return Val == 0x3C00 || // 1.0 1853 Val == 0xBC00 || // -1.0 1854 Val == 0x3800 || // 0.5 1855 Val == 0xB800 || // -0.5 1856 Val == 0x4000 || // 2.0 1857 Val == 0xC000 || // -2.0 1858 Val == 0x4400 || // 4.0 1859 Val == 0xC400 || // -4.0 1860 Val == 0x3118; // 1/2pi 1861 } 1862 1863 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) { 1864 assert(HasInv2Pi); 1865 1866 if (isInt<16>(Literal) || isUInt<16>(Literal)) { 1867 int16_t Trunc = static_cast<int16_t>(Literal); 1868 return AMDGPU::isInlinableLiteral16(Trunc, HasInv2Pi); 1869 } 1870 if (!(Literal & 0xffff)) 1871 return AMDGPU::isInlinableLiteral16(Literal >> 16, HasInv2Pi); 1872 1873 int16_t Lo16 = static_cast<int16_t>(Literal); 1874 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1875 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi); 1876 } 1877 1878 bool isInlinableIntLiteralV216(int32_t Literal) { 1879 int16_t Lo16 = static_cast<int16_t>(Literal); 1880 if (isInt<16>(Literal) || isUInt<16>(Literal)) 1881 return isInlinableIntLiteral(Lo16); 1882 1883 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1884 if (!(Literal & 0xffff)) 1885 return isInlinableIntLiteral(Hi16); 1886 return Lo16 == Hi16 && isInlinableIntLiteral(Lo16); 1887 } 1888 1889 bool isFoldableLiteralV216(int32_t Literal, bool HasInv2Pi) { 1890 assert(HasInv2Pi); 1891 1892 int16_t Lo16 = static_cast<int16_t>(Literal); 1893 if (isInt<16>(Literal) || isUInt<16>(Literal)) 1894 return true; 1895 1896 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1897 if (!(Literal & 0xffff)) 1898 return true; 1899 return Lo16 == Hi16; 1900 } 1901 1902 bool isArgPassedInSGPR(const Argument *A) { 1903 const Function *F = A->getParent(); 1904 1905 // Arguments to compute shaders are never a source of divergence. 1906 CallingConv::ID CC = F->getCallingConv(); 1907 switch (CC) { 1908 case CallingConv::AMDGPU_KERNEL: 1909 case CallingConv::SPIR_KERNEL: 1910 return true; 1911 case CallingConv::AMDGPU_VS: 1912 case CallingConv::AMDGPU_LS: 1913 case CallingConv::AMDGPU_HS: 1914 case CallingConv::AMDGPU_ES: 1915 case CallingConv::AMDGPU_GS: 1916 case CallingConv::AMDGPU_PS: 1917 case CallingConv::AMDGPU_CS: 1918 case CallingConv::AMDGPU_Gfx: 1919 // For non-compute shaders, SGPR inputs are marked with either inreg or byval. 1920 // Everything else is in VGPRs. 1921 return F->getAttributes().hasParamAttr(A->getArgNo(), Attribute::InReg) || 1922 F->getAttributes().hasParamAttr(A->getArgNo(), Attribute::ByVal); 1923 default: 1924 // TODO: Should calls support inreg for SGPR inputs? 1925 return false; 1926 } 1927 } 1928 1929 static bool hasSMEMByteOffset(const MCSubtargetInfo &ST) { 1930 return isGCN3Encoding(ST) || isGFX10Plus(ST); 1931 } 1932 1933 static bool hasSMRDSignedImmOffset(const MCSubtargetInfo &ST) { 1934 return isGFX9Plus(ST); 1935 } 1936 1937 bool isLegalSMRDEncodedUnsignedOffset(const MCSubtargetInfo &ST, 1938 int64_t EncodedOffset) { 1939 return hasSMEMByteOffset(ST) ? isUInt<20>(EncodedOffset) 1940 : isUInt<8>(EncodedOffset); 1941 } 1942 1943 bool isLegalSMRDEncodedSignedOffset(const MCSubtargetInfo &ST, 1944 int64_t EncodedOffset, 1945 bool IsBuffer) { 1946 return !IsBuffer && 1947 hasSMRDSignedImmOffset(ST) && 1948 isInt<21>(EncodedOffset); 1949 } 1950 1951 static bool isDwordAligned(uint64_t ByteOffset) { 1952 return (ByteOffset & 3) == 0; 1953 } 1954 1955 uint64_t convertSMRDOffsetUnits(const MCSubtargetInfo &ST, 1956 uint64_t ByteOffset) { 1957 if (hasSMEMByteOffset(ST)) 1958 return ByteOffset; 1959 1960 assert(isDwordAligned(ByteOffset)); 1961 return ByteOffset >> 2; 1962 } 1963 1964 Optional<int64_t> getSMRDEncodedOffset(const MCSubtargetInfo &ST, 1965 int64_t ByteOffset, bool IsBuffer) { 1966 // The signed version is always a byte offset. 1967 if (!IsBuffer && hasSMRDSignedImmOffset(ST)) { 1968 assert(hasSMEMByteOffset(ST)); 1969 return isInt<20>(ByteOffset) ? Optional<int64_t>(ByteOffset) : None; 1970 } 1971 1972 if (!isDwordAligned(ByteOffset) && !hasSMEMByteOffset(ST)) 1973 return None; 1974 1975 int64_t EncodedOffset = convertSMRDOffsetUnits(ST, ByteOffset); 1976 return isLegalSMRDEncodedUnsignedOffset(ST, EncodedOffset) 1977 ? Optional<int64_t>(EncodedOffset) 1978 : None; 1979 } 1980 1981 Optional<int64_t> getSMRDEncodedLiteralOffset32(const MCSubtargetInfo &ST, 1982 int64_t ByteOffset) { 1983 if (!isCI(ST) || !isDwordAligned(ByteOffset)) 1984 return None; 1985 1986 int64_t EncodedOffset = convertSMRDOffsetUnits(ST, ByteOffset); 1987 return isUInt<32>(EncodedOffset) ? Optional<int64_t>(EncodedOffset) : None; 1988 } 1989 1990 unsigned getNumFlatOffsetBits(const MCSubtargetInfo &ST, bool Signed) { 1991 // Address offset is 12-bit signed for GFX10, 13-bit for GFX9. 1992 if (AMDGPU::isGFX10(ST)) 1993 return Signed ? 12 : 11; 1994 1995 return Signed ? 13 : 12; 1996 } 1997 1998 // Given Imm, split it into the values to put into the SOffset and ImmOffset 1999 // fields in an MUBUF instruction. Return false if it is not possible (due to a 2000 // hardware bug needing a workaround). 2001 // 2002 // The required alignment ensures that individual address components remain 2003 // aligned if they are aligned to begin with. It also ensures that additional 2004 // offsets within the given alignment can be added to the resulting ImmOffset. 2005 bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset, 2006 const GCNSubtarget *Subtarget, Align Alignment) { 2007 const uint32_t MaxImm = alignDown(4095, Alignment.value()); 2008 uint32_t Overflow = 0; 2009 2010 if (Imm > MaxImm) { 2011 if (Imm <= MaxImm + 64) { 2012 // Use an SOffset inline constant for 4..64 2013 Overflow = Imm - MaxImm; 2014 Imm = MaxImm; 2015 } else { 2016 // Try to keep the same value in SOffset for adjacent loads, so that 2017 // the corresponding register contents can be re-used. 2018 // 2019 // Load values with all low-bits (except for alignment bits) set into 2020 // SOffset, so that a larger range of values can be covered using 2021 // s_movk_i32. 2022 // 2023 // Atomic operations fail to work correctly when individual address 2024 // components are unaligned, even if their sum is aligned. 2025 uint32_t High = (Imm + Alignment.value()) & ~4095; 2026 uint32_t Low = (Imm + Alignment.value()) & 4095; 2027 Imm = Low; 2028 Overflow = High - Alignment.value(); 2029 } 2030 } 2031 2032 // There is a hardware bug in SI and CI which prevents address clamping in 2033 // MUBUF instructions from working correctly with SOffsets. The immediate 2034 // offset is unaffected. 2035 if (Overflow > 0 && 2036 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 2037 return false; 2038 2039 ImmOffset = Imm; 2040 SOffset = Overflow; 2041 return true; 2042 } 2043 2044 SIModeRegisterDefaults::SIModeRegisterDefaults(const Function &F) { 2045 *this = getDefaultForCallingConv(F.getCallingConv()); 2046 2047 StringRef IEEEAttr = F.getFnAttribute("amdgpu-ieee").getValueAsString(); 2048 if (!IEEEAttr.empty()) 2049 IEEE = IEEEAttr == "true"; 2050 2051 StringRef DX10ClampAttr 2052 = F.getFnAttribute("amdgpu-dx10-clamp").getValueAsString(); 2053 if (!DX10ClampAttr.empty()) 2054 DX10Clamp = DX10ClampAttr == "true"; 2055 2056 StringRef DenormF32Attr = F.getFnAttribute("denormal-fp-math-f32").getValueAsString(); 2057 if (!DenormF32Attr.empty()) { 2058 DenormalMode DenormMode = parseDenormalFPAttribute(DenormF32Attr); 2059 FP32InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2060 FP32OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2061 } 2062 2063 StringRef DenormAttr = F.getFnAttribute("denormal-fp-math").getValueAsString(); 2064 if (!DenormAttr.empty()) { 2065 DenormalMode DenormMode = parseDenormalFPAttribute(DenormAttr); 2066 2067 if (DenormF32Attr.empty()) { 2068 FP32InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2069 FP32OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2070 } 2071 2072 FP64FP16InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2073 FP64FP16OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2074 } 2075 } 2076 2077 namespace { 2078 2079 struct SourceOfDivergence { 2080 unsigned Intr; 2081 }; 2082 const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr); 2083 2084 #define GET_SourcesOfDivergence_IMPL 2085 #define GET_Gfx9BufferFormat_IMPL 2086 #define GET_Gfx10PlusBufferFormat_IMPL 2087 #include "AMDGPUGenSearchableTables.inc" 2088 2089 } // end anonymous namespace 2090 2091 bool isIntrinsicSourceOfDivergence(unsigned IntrID) { 2092 return lookupSourceOfDivergence(IntrID); 2093 } 2094 2095 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t BitsPerComp, 2096 uint8_t NumComponents, 2097 uint8_t NumFormat, 2098 const MCSubtargetInfo &STI) { 2099 return isGFX10Plus(STI) 2100 ? getGfx10PlusBufferFormatInfo(BitsPerComp, NumComponents, 2101 NumFormat) 2102 : getGfx9BufferFormatInfo(BitsPerComp, NumComponents, NumFormat); 2103 } 2104 2105 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t Format, 2106 const MCSubtargetInfo &STI) { 2107 return isGFX10Plus(STI) ? getGfx10PlusBufferFormatInfo(Format) 2108 : getGfx9BufferFormatInfo(Format); 2109 } 2110 2111 } // namespace AMDGPU 2112 2113 raw_ostream &operator<<(raw_ostream &OS, 2114 const AMDGPU::IsaInfo::TargetIDSetting S) { 2115 switch (S) { 2116 case (AMDGPU::IsaInfo::TargetIDSetting::Unsupported): 2117 OS << "Unsupported"; 2118 break; 2119 case (AMDGPU::IsaInfo::TargetIDSetting::Any): 2120 OS << "Any"; 2121 break; 2122 case (AMDGPU::IsaInfo::TargetIDSetting::Off): 2123 OS << "Off"; 2124 break; 2125 case (AMDGPU::IsaInfo::TargetIDSetting::On): 2126 OS << "On"; 2127 break; 2128 } 2129 return OS; 2130 } 2131 2132 } // namespace llvm 2133