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