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) { 1291 for (int i = ID_GAPS_FIRST_; i < ID_GAPS_LAST_; ++i) { 1292 if (IdSymbolic[i] && Name == IdSymbolic[i]) 1293 return i; 1294 } 1295 return ID_UNKNOWN_; 1296 } 1297 1298 bool isValidMsgId(int64_t MsgId, const MCSubtargetInfo &STI, bool Strict) { 1299 if (Strict) { 1300 switch (MsgId) { 1301 case ID_SAVEWAVE: 1302 return isVI(STI) || isGFX9Plus(STI); 1303 case ID_STALL_WAVE_GEN: 1304 case ID_HALT_WAVES: 1305 case ID_ORDERED_PS_DONE: 1306 case ID_GS_ALLOC_REQ: 1307 case ID_GET_DOORBELL: 1308 return isGFX9Plus(STI); 1309 case ID_EARLY_PRIM_DEALLOC: 1310 return isGFX9(STI); 1311 case ID_GET_DDID: 1312 return isGFX10Plus(STI); 1313 default: 1314 return 0 <= MsgId && MsgId < ID_GAPS_LAST_ && IdSymbolic[MsgId]; 1315 } 1316 } else { 1317 return 0 <= MsgId && isUInt<ID_WIDTH_>(MsgId); 1318 } 1319 } 1320 1321 StringRef getMsgName(int64_t MsgId) { 1322 assert(0 <= MsgId && MsgId < ID_GAPS_LAST_); 1323 return IdSymbolic[MsgId]; 1324 } 1325 1326 int64_t getMsgOpId(int64_t MsgId, const StringRef Name) { 1327 const char* const *S = (MsgId == ID_SYSMSG) ? OpSysSymbolic : OpGsSymbolic; 1328 const int F = (MsgId == ID_SYSMSG) ? OP_SYS_FIRST_ : OP_GS_FIRST_; 1329 const int L = (MsgId == ID_SYSMSG) ? OP_SYS_LAST_ : OP_GS_LAST_; 1330 for (int i = F; i < L; ++i) { 1331 if (Name == S[i]) { 1332 return i; 1333 } 1334 } 1335 return OP_UNKNOWN_; 1336 } 1337 1338 bool isValidMsgOp(int64_t MsgId, int64_t OpId, const MCSubtargetInfo &STI, 1339 bool Strict) { 1340 assert(isValidMsgId(MsgId, STI, Strict)); 1341 1342 if (!Strict) 1343 return 0 <= OpId && isUInt<OP_WIDTH_>(OpId); 1344 1345 switch(MsgId) 1346 { 1347 case ID_GS: 1348 return (OP_GS_FIRST_ <= OpId && OpId < OP_GS_LAST_) && OpId != OP_GS_NOP; 1349 case ID_GS_DONE: 1350 return OP_GS_FIRST_ <= OpId && OpId < OP_GS_LAST_; 1351 case ID_SYSMSG: 1352 return OP_SYS_FIRST_ <= OpId && OpId < OP_SYS_LAST_; 1353 default: 1354 return OpId == OP_NONE_; 1355 } 1356 } 1357 1358 StringRef getMsgOpName(int64_t MsgId, int64_t OpId) { 1359 assert(msgRequiresOp(MsgId)); 1360 return (MsgId == ID_SYSMSG)? OpSysSymbolic[OpId] : OpGsSymbolic[OpId]; 1361 } 1362 1363 bool isValidMsgStream(int64_t MsgId, int64_t OpId, int64_t StreamId, 1364 const MCSubtargetInfo &STI, bool Strict) { 1365 assert(isValidMsgOp(MsgId, OpId, STI, Strict)); 1366 1367 if (!Strict) 1368 return 0 <= StreamId && isUInt<STREAM_ID_WIDTH_>(StreamId); 1369 1370 switch(MsgId) 1371 { 1372 case ID_GS: 1373 return STREAM_ID_FIRST_ <= StreamId && StreamId < STREAM_ID_LAST_; 1374 case ID_GS_DONE: 1375 return (OpId == OP_GS_NOP)? 1376 (StreamId == STREAM_ID_NONE_) : 1377 (STREAM_ID_FIRST_ <= StreamId && StreamId < STREAM_ID_LAST_); 1378 default: 1379 return StreamId == STREAM_ID_NONE_; 1380 } 1381 } 1382 1383 bool msgRequiresOp(int64_t MsgId) { 1384 return MsgId == ID_GS || MsgId == ID_GS_DONE || MsgId == ID_SYSMSG; 1385 } 1386 1387 bool msgSupportsStream(int64_t MsgId, int64_t OpId) { 1388 return (MsgId == ID_GS || MsgId == ID_GS_DONE) && OpId != OP_GS_NOP; 1389 } 1390 1391 void decodeMsg(unsigned Val, 1392 uint16_t &MsgId, 1393 uint16_t &OpId, 1394 uint16_t &StreamId) { 1395 MsgId = Val & ID_MASK_; 1396 OpId = (Val & OP_MASK_) >> OP_SHIFT_; 1397 StreamId = (Val & STREAM_ID_MASK_) >> STREAM_ID_SHIFT_; 1398 } 1399 1400 uint64_t encodeMsg(uint64_t MsgId, 1401 uint64_t OpId, 1402 uint64_t StreamId) { 1403 return (MsgId << ID_SHIFT_) | 1404 (OpId << OP_SHIFT_) | 1405 (StreamId << STREAM_ID_SHIFT_); 1406 } 1407 1408 } // namespace SendMsg 1409 1410 //===----------------------------------------------------------------------===// 1411 // 1412 //===----------------------------------------------------------------------===// 1413 1414 unsigned getInitialPSInputAddr(const Function &F) { 1415 return getIntegerAttribute(F, "InitialPSInputAddr", 0); 1416 } 1417 1418 bool getHasColorExport(const Function &F) { 1419 // As a safe default always respond as if PS has color exports. 1420 return getIntegerAttribute( 1421 F, "amdgpu-color-export", 1422 F.getCallingConv() == CallingConv::AMDGPU_PS ? 1 : 0) != 0; 1423 } 1424 1425 bool getHasDepthExport(const Function &F) { 1426 return getIntegerAttribute(F, "amdgpu-depth-export", 0) != 0; 1427 } 1428 1429 bool isShader(CallingConv::ID cc) { 1430 switch(cc) { 1431 case CallingConv::AMDGPU_VS: 1432 case CallingConv::AMDGPU_LS: 1433 case CallingConv::AMDGPU_HS: 1434 case CallingConv::AMDGPU_ES: 1435 case CallingConv::AMDGPU_GS: 1436 case CallingConv::AMDGPU_PS: 1437 case CallingConv::AMDGPU_CS: 1438 return true; 1439 default: 1440 return false; 1441 } 1442 } 1443 1444 bool isGraphics(CallingConv::ID cc) { 1445 return isShader(cc) || cc == CallingConv::AMDGPU_Gfx; 1446 } 1447 1448 bool isCompute(CallingConv::ID cc) { 1449 return !isGraphics(cc) || cc == CallingConv::AMDGPU_CS; 1450 } 1451 1452 bool isEntryFunctionCC(CallingConv::ID CC) { 1453 switch (CC) { 1454 case CallingConv::AMDGPU_KERNEL: 1455 case CallingConv::SPIR_KERNEL: 1456 case CallingConv::AMDGPU_VS: 1457 case CallingConv::AMDGPU_GS: 1458 case CallingConv::AMDGPU_PS: 1459 case CallingConv::AMDGPU_CS: 1460 case CallingConv::AMDGPU_ES: 1461 case CallingConv::AMDGPU_HS: 1462 case CallingConv::AMDGPU_LS: 1463 return true; 1464 default: 1465 return false; 1466 } 1467 } 1468 1469 bool isModuleEntryFunctionCC(CallingConv::ID CC) { 1470 switch (CC) { 1471 case CallingConv::AMDGPU_Gfx: 1472 return true; 1473 default: 1474 return isEntryFunctionCC(CC); 1475 } 1476 } 1477 1478 bool isKernelCC(const Function *Func) { 1479 return AMDGPU::isModuleEntryFunctionCC(Func->getCallingConv()); 1480 } 1481 1482 bool hasXNACK(const MCSubtargetInfo &STI) { 1483 return STI.getFeatureBits()[AMDGPU::FeatureXNACK]; 1484 } 1485 1486 bool hasSRAMECC(const MCSubtargetInfo &STI) { 1487 return STI.getFeatureBits()[AMDGPU::FeatureSRAMECC]; 1488 } 1489 1490 bool hasMIMG_R128(const MCSubtargetInfo &STI) { 1491 return STI.getFeatureBits()[AMDGPU::FeatureMIMG_R128] && !STI.getFeatureBits()[AMDGPU::FeatureR128A16]; 1492 } 1493 1494 bool hasGFX10A16(const MCSubtargetInfo &STI) { 1495 return STI.getFeatureBits()[AMDGPU::FeatureGFX10A16]; 1496 } 1497 1498 bool hasG16(const MCSubtargetInfo &STI) { 1499 return STI.getFeatureBits()[AMDGPU::FeatureG16]; 1500 } 1501 1502 bool hasPackedD16(const MCSubtargetInfo &STI) { 1503 return !STI.getFeatureBits()[AMDGPU::FeatureUnpackedD16VMem]; 1504 } 1505 1506 bool isSI(const MCSubtargetInfo &STI) { 1507 return STI.getFeatureBits()[AMDGPU::FeatureSouthernIslands]; 1508 } 1509 1510 bool isCI(const MCSubtargetInfo &STI) { 1511 return STI.getFeatureBits()[AMDGPU::FeatureSeaIslands]; 1512 } 1513 1514 bool isVI(const MCSubtargetInfo &STI) { 1515 return STI.getFeatureBits()[AMDGPU::FeatureVolcanicIslands]; 1516 } 1517 1518 bool isGFX9(const MCSubtargetInfo &STI) { 1519 return STI.getFeatureBits()[AMDGPU::FeatureGFX9]; 1520 } 1521 1522 bool isGFX9_GFX10(const MCSubtargetInfo &STI) { 1523 return isGFX9(STI) || isGFX10(STI); 1524 } 1525 1526 bool isGFX9Plus(const MCSubtargetInfo &STI) { 1527 return isGFX9(STI) || isGFX10Plus(STI); 1528 } 1529 1530 bool isGFX10(const MCSubtargetInfo &STI) { 1531 return STI.getFeatureBits()[AMDGPU::FeatureGFX10]; 1532 } 1533 1534 bool isGFX10Plus(const MCSubtargetInfo &STI) { return isGFX10(STI); } 1535 1536 bool isGCN3Encoding(const MCSubtargetInfo &STI) { 1537 return STI.getFeatureBits()[AMDGPU::FeatureGCN3Encoding]; 1538 } 1539 1540 bool isGFX10_AEncoding(const MCSubtargetInfo &STI) { 1541 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_AEncoding]; 1542 } 1543 1544 bool isGFX10_BEncoding(const MCSubtargetInfo &STI) { 1545 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_BEncoding]; 1546 } 1547 1548 bool hasGFX10_3Insts(const MCSubtargetInfo &STI) { 1549 return STI.getFeatureBits()[AMDGPU::FeatureGFX10_3Insts]; 1550 } 1551 1552 bool isGFX90A(const MCSubtargetInfo &STI) { 1553 return STI.getFeatureBits()[AMDGPU::FeatureGFX90AInsts]; 1554 } 1555 1556 bool isGFX940(const MCSubtargetInfo &STI) { 1557 return STI.getFeatureBits()[AMDGPU::FeatureGFX940Insts]; 1558 } 1559 1560 bool hasArchitectedFlatScratch(const MCSubtargetInfo &STI) { 1561 return STI.getFeatureBits()[AMDGPU::FeatureArchitectedFlatScratch]; 1562 } 1563 1564 bool hasMAIInsts(const MCSubtargetInfo &STI) { 1565 return STI.getFeatureBits()[AMDGPU::FeatureMAIInsts]; 1566 } 1567 1568 int32_t getTotalNumVGPRs(bool has90AInsts, int32_t ArgNumAGPR, 1569 int32_t ArgNumVGPR) { 1570 if (has90AInsts && ArgNumAGPR) 1571 return alignTo(ArgNumVGPR, 4) + ArgNumAGPR; 1572 return std::max(ArgNumVGPR, ArgNumAGPR); 1573 } 1574 1575 bool isSGPR(unsigned Reg, const MCRegisterInfo* TRI) { 1576 const MCRegisterClass SGPRClass = TRI->getRegClass(AMDGPU::SReg_32RegClassID); 1577 const unsigned FirstSubReg = TRI->getSubReg(Reg, AMDGPU::sub0); 1578 return SGPRClass.contains(FirstSubReg != 0 ? FirstSubReg : Reg) || 1579 Reg == AMDGPU::SCC; 1580 } 1581 1582 #define MAP_REG2REG \ 1583 using namespace AMDGPU; \ 1584 switch(Reg) { \ 1585 default: return Reg; \ 1586 CASE_CI_VI(FLAT_SCR) \ 1587 CASE_CI_VI(FLAT_SCR_LO) \ 1588 CASE_CI_VI(FLAT_SCR_HI) \ 1589 CASE_VI_GFX9PLUS(TTMP0) \ 1590 CASE_VI_GFX9PLUS(TTMP1) \ 1591 CASE_VI_GFX9PLUS(TTMP2) \ 1592 CASE_VI_GFX9PLUS(TTMP3) \ 1593 CASE_VI_GFX9PLUS(TTMP4) \ 1594 CASE_VI_GFX9PLUS(TTMP5) \ 1595 CASE_VI_GFX9PLUS(TTMP6) \ 1596 CASE_VI_GFX9PLUS(TTMP7) \ 1597 CASE_VI_GFX9PLUS(TTMP8) \ 1598 CASE_VI_GFX9PLUS(TTMP9) \ 1599 CASE_VI_GFX9PLUS(TTMP10) \ 1600 CASE_VI_GFX9PLUS(TTMP11) \ 1601 CASE_VI_GFX9PLUS(TTMP12) \ 1602 CASE_VI_GFX9PLUS(TTMP13) \ 1603 CASE_VI_GFX9PLUS(TTMP14) \ 1604 CASE_VI_GFX9PLUS(TTMP15) \ 1605 CASE_VI_GFX9PLUS(TTMP0_TTMP1) \ 1606 CASE_VI_GFX9PLUS(TTMP2_TTMP3) \ 1607 CASE_VI_GFX9PLUS(TTMP4_TTMP5) \ 1608 CASE_VI_GFX9PLUS(TTMP6_TTMP7) \ 1609 CASE_VI_GFX9PLUS(TTMP8_TTMP9) \ 1610 CASE_VI_GFX9PLUS(TTMP10_TTMP11) \ 1611 CASE_VI_GFX9PLUS(TTMP12_TTMP13) \ 1612 CASE_VI_GFX9PLUS(TTMP14_TTMP15) \ 1613 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3) \ 1614 CASE_VI_GFX9PLUS(TTMP4_TTMP5_TTMP6_TTMP7) \ 1615 CASE_VI_GFX9PLUS(TTMP8_TTMP9_TTMP10_TTMP11) \ 1616 CASE_VI_GFX9PLUS(TTMP12_TTMP13_TTMP14_TTMP15) \ 1617 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7) \ 1618 CASE_VI_GFX9PLUS(TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11) \ 1619 CASE_VI_GFX9PLUS(TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \ 1620 CASE_VI_GFX9PLUS(TTMP0_TTMP1_TTMP2_TTMP3_TTMP4_TTMP5_TTMP6_TTMP7_TTMP8_TTMP9_TTMP10_TTMP11_TTMP12_TTMP13_TTMP14_TTMP15) \ 1621 } 1622 1623 #define CASE_CI_VI(node) \ 1624 assert(!isSI(STI)); \ 1625 case node: return isCI(STI) ? node##_ci : node##_vi; 1626 1627 #define CASE_VI_GFX9PLUS(node) \ 1628 case node: return isGFX9Plus(STI) ? node##_gfx9plus : node##_vi; 1629 1630 unsigned getMCReg(unsigned Reg, const MCSubtargetInfo &STI) { 1631 if (STI.getTargetTriple().getArch() == Triple::r600) 1632 return Reg; 1633 MAP_REG2REG 1634 } 1635 1636 #undef CASE_CI_VI 1637 #undef CASE_VI_GFX9PLUS 1638 1639 #define CASE_CI_VI(node) case node##_ci: case node##_vi: return node; 1640 #define CASE_VI_GFX9PLUS(node) case node##_vi: case node##_gfx9plus: return node; 1641 1642 unsigned mc2PseudoReg(unsigned Reg) { 1643 MAP_REG2REG 1644 } 1645 1646 #undef CASE_CI_VI 1647 #undef CASE_VI_GFX9PLUS 1648 #undef MAP_REG2REG 1649 1650 bool isSISrcOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1651 assert(OpNo < Desc.NumOperands); 1652 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1653 return OpType >= AMDGPU::OPERAND_SRC_FIRST && 1654 OpType <= AMDGPU::OPERAND_SRC_LAST; 1655 } 1656 1657 bool isSISrcFPOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1658 assert(OpNo < Desc.NumOperands); 1659 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1660 switch (OpType) { 1661 case AMDGPU::OPERAND_REG_IMM_FP32: 1662 case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED: 1663 case AMDGPU::OPERAND_REG_IMM_FP64: 1664 case AMDGPU::OPERAND_REG_IMM_FP16: 1665 case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED: 1666 case AMDGPU::OPERAND_REG_IMM_V2FP16: 1667 case AMDGPU::OPERAND_REG_IMM_V2INT16: 1668 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 1669 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 1670 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 1671 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: 1672 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16: 1673 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 1674 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 1675 case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: 1676 case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16: 1677 case AMDGPU::OPERAND_REG_IMM_V2FP32: 1678 case AMDGPU::OPERAND_REG_INLINE_C_V2FP32: 1679 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: 1680 return true; 1681 default: 1682 return false; 1683 } 1684 } 1685 1686 bool isSISrcInlinableOperand(const MCInstrDesc &Desc, unsigned OpNo) { 1687 assert(OpNo < Desc.NumOperands); 1688 unsigned OpType = Desc.OpInfo[OpNo].OperandType; 1689 return OpType >= AMDGPU::OPERAND_REG_INLINE_C_FIRST && 1690 OpType <= AMDGPU::OPERAND_REG_INLINE_C_LAST; 1691 } 1692 1693 // Avoid using MCRegisterClass::getSize, since that function will go away 1694 // (move from MC* level to Target* level). Return size in bits. 1695 unsigned getRegBitWidth(unsigned RCID) { 1696 switch (RCID) { 1697 case AMDGPU::VGPR_LO16RegClassID: 1698 case AMDGPU::VGPR_HI16RegClassID: 1699 case AMDGPU::SGPR_LO16RegClassID: 1700 case AMDGPU::AGPR_LO16RegClassID: 1701 return 16; 1702 case AMDGPU::SGPR_32RegClassID: 1703 case AMDGPU::VGPR_32RegClassID: 1704 case AMDGPU::VRegOrLds_32RegClassID: 1705 case AMDGPU::AGPR_32RegClassID: 1706 case AMDGPU::VS_32RegClassID: 1707 case AMDGPU::AV_32RegClassID: 1708 case AMDGPU::SReg_32RegClassID: 1709 case AMDGPU::SReg_32_XM0RegClassID: 1710 case AMDGPU::SRegOrLds_32RegClassID: 1711 return 32; 1712 case AMDGPU::SGPR_64RegClassID: 1713 case AMDGPU::VS_64RegClassID: 1714 case AMDGPU::SReg_64RegClassID: 1715 case AMDGPU::VReg_64RegClassID: 1716 case AMDGPU::AReg_64RegClassID: 1717 case AMDGPU::SReg_64_XEXECRegClassID: 1718 case AMDGPU::VReg_64_Align2RegClassID: 1719 case AMDGPU::AReg_64_Align2RegClassID: 1720 case AMDGPU::AV_64RegClassID: 1721 case AMDGPU::AV_64_Align2RegClassID: 1722 return 64; 1723 case AMDGPU::SGPR_96RegClassID: 1724 case AMDGPU::SReg_96RegClassID: 1725 case AMDGPU::VReg_96RegClassID: 1726 case AMDGPU::AReg_96RegClassID: 1727 case AMDGPU::VReg_96_Align2RegClassID: 1728 case AMDGPU::AReg_96_Align2RegClassID: 1729 case AMDGPU::AV_96RegClassID: 1730 case AMDGPU::AV_96_Align2RegClassID: 1731 return 96; 1732 case AMDGPU::SGPR_128RegClassID: 1733 case AMDGPU::SReg_128RegClassID: 1734 case AMDGPU::VReg_128RegClassID: 1735 case AMDGPU::AReg_128RegClassID: 1736 case AMDGPU::VReg_128_Align2RegClassID: 1737 case AMDGPU::AReg_128_Align2RegClassID: 1738 case AMDGPU::AV_128RegClassID: 1739 case AMDGPU::AV_128_Align2RegClassID: 1740 return 128; 1741 case AMDGPU::SGPR_160RegClassID: 1742 case AMDGPU::SReg_160RegClassID: 1743 case AMDGPU::VReg_160RegClassID: 1744 case AMDGPU::AReg_160RegClassID: 1745 case AMDGPU::VReg_160_Align2RegClassID: 1746 case AMDGPU::AReg_160_Align2RegClassID: 1747 case AMDGPU::AV_160RegClassID: 1748 case AMDGPU::AV_160_Align2RegClassID: 1749 return 160; 1750 case AMDGPU::SGPR_192RegClassID: 1751 case AMDGPU::SReg_192RegClassID: 1752 case AMDGPU::VReg_192RegClassID: 1753 case AMDGPU::AReg_192RegClassID: 1754 case AMDGPU::VReg_192_Align2RegClassID: 1755 case AMDGPU::AReg_192_Align2RegClassID: 1756 case AMDGPU::AV_192RegClassID: 1757 case AMDGPU::AV_192_Align2RegClassID: 1758 return 192; 1759 case AMDGPU::SGPR_224RegClassID: 1760 case AMDGPU::SReg_224RegClassID: 1761 case AMDGPU::VReg_224RegClassID: 1762 case AMDGPU::AReg_224RegClassID: 1763 case AMDGPU::VReg_224_Align2RegClassID: 1764 case AMDGPU::AReg_224_Align2RegClassID: 1765 case AMDGPU::AV_224RegClassID: 1766 case AMDGPU::AV_224_Align2RegClassID: 1767 return 224; 1768 case AMDGPU::SGPR_256RegClassID: 1769 case AMDGPU::SReg_256RegClassID: 1770 case AMDGPU::VReg_256RegClassID: 1771 case AMDGPU::AReg_256RegClassID: 1772 case AMDGPU::VReg_256_Align2RegClassID: 1773 case AMDGPU::AReg_256_Align2RegClassID: 1774 case AMDGPU::AV_256RegClassID: 1775 case AMDGPU::AV_256_Align2RegClassID: 1776 return 256; 1777 case AMDGPU::SGPR_512RegClassID: 1778 case AMDGPU::SReg_512RegClassID: 1779 case AMDGPU::VReg_512RegClassID: 1780 case AMDGPU::AReg_512RegClassID: 1781 case AMDGPU::VReg_512_Align2RegClassID: 1782 case AMDGPU::AReg_512_Align2RegClassID: 1783 case AMDGPU::AV_512RegClassID: 1784 case AMDGPU::AV_512_Align2RegClassID: 1785 return 512; 1786 case AMDGPU::SGPR_1024RegClassID: 1787 case AMDGPU::SReg_1024RegClassID: 1788 case AMDGPU::VReg_1024RegClassID: 1789 case AMDGPU::AReg_1024RegClassID: 1790 case AMDGPU::VReg_1024_Align2RegClassID: 1791 case AMDGPU::AReg_1024_Align2RegClassID: 1792 case AMDGPU::AV_1024RegClassID: 1793 case AMDGPU::AV_1024_Align2RegClassID: 1794 return 1024; 1795 default: 1796 llvm_unreachable("Unexpected register class"); 1797 } 1798 } 1799 1800 unsigned getRegBitWidth(const MCRegisterClass &RC) { 1801 return getRegBitWidth(RC.getID()); 1802 } 1803 1804 unsigned getRegOperandSize(const MCRegisterInfo *MRI, const MCInstrDesc &Desc, 1805 unsigned OpNo) { 1806 assert(OpNo < Desc.NumOperands); 1807 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 1808 return getRegBitWidth(MRI->getRegClass(RCID)) / 8; 1809 } 1810 1811 bool isInlinableLiteral64(int64_t Literal, bool HasInv2Pi) { 1812 if (isInlinableIntLiteral(Literal)) 1813 return true; 1814 1815 uint64_t Val = static_cast<uint64_t>(Literal); 1816 return (Val == DoubleToBits(0.0)) || 1817 (Val == DoubleToBits(1.0)) || 1818 (Val == DoubleToBits(-1.0)) || 1819 (Val == DoubleToBits(0.5)) || 1820 (Val == DoubleToBits(-0.5)) || 1821 (Val == DoubleToBits(2.0)) || 1822 (Val == DoubleToBits(-2.0)) || 1823 (Val == DoubleToBits(4.0)) || 1824 (Val == DoubleToBits(-4.0)) || 1825 (Val == 0x3fc45f306dc9c882 && HasInv2Pi); 1826 } 1827 1828 bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi) { 1829 if (isInlinableIntLiteral(Literal)) 1830 return true; 1831 1832 // The actual type of the operand does not seem to matter as long 1833 // as the bits match one of the inline immediate values. For example: 1834 // 1835 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal, 1836 // so it is a legal inline immediate. 1837 // 1838 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in 1839 // floating-point, so it is a legal inline immediate. 1840 1841 uint32_t Val = static_cast<uint32_t>(Literal); 1842 return (Val == FloatToBits(0.0f)) || 1843 (Val == FloatToBits(1.0f)) || 1844 (Val == FloatToBits(-1.0f)) || 1845 (Val == FloatToBits(0.5f)) || 1846 (Val == FloatToBits(-0.5f)) || 1847 (Val == FloatToBits(2.0f)) || 1848 (Val == FloatToBits(-2.0f)) || 1849 (Val == FloatToBits(4.0f)) || 1850 (Val == FloatToBits(-4.0f)) || 1851 (Val == 0x3e22f983 && HasInv2Pi); 1852 } 1853 1854 bool isInlinableLiteral16(int16_t Literal, bool HasInv2Pi) { 1855 if (!HasInv2Pi) 1856 return false; 1857 1858 if (isInlinableIntLiteral(Literal)) 1859 return true; 1860 1861 uint16_t Val = static_cast<uint16_t>(Literal); 1862 return Val == 0x3C00 || // 1.0 1863 Val == 0xBC00 || // -1.0 1864 Val == 0x3800 || // 0.5 1865 Val == 0xB800 || // -0.5 1866 Val == 0x4000 || // 2.0 1867 Val == 0xC000 || // -2.0 1868 Val == 0x4400 || // 4.0 1869 Val == 0xC400 || // -4.0 1870 Val == 0x3118; // 1/2pi 1871 } 1872 1873 bool isInlinableLiteralV216(int32_t Literal, bool HasInv2Pi) { 1874 assert(HasInv2Pi); 1875 1876 if (isInt<16>(Literal) || isUInt<16>(Literal)) { 1877 int16_t Trunc = static_cast<int16_t>(Literal); 1878 return AMDGPU::isInlinableLiteral16(Trunc, HasInv2Pi); 1879 } 1880 if (!(Literal & 0xffff)) 1881 return AMDGPU::isInlinableLiteral16(Literal >> 16, HasInv2Pi); 1882 1883 int16_t Lo16 = static_cast<int16_t>(Literal); 1884 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1885 return Lo16 == Hi16 && isInlinableLiteral16(Lo16, HasInv2Pi); 1886 } 1887 1888 bool isInlinableIntLiteralV216(int32_t Literal) { 1889 int16_t Lo16 = static_cast<int16_t>(Literal); 1890 if (isInt<16>(Literal) || isUInt<16>(Literal)) 1891 return isInlinableIntLiteral(Lo16); 1892 1893 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1894 if (!(Literal & 0xffff)) 1895 return isInlinableIntLiteral(Hi16); 1896 return Lo16 == Hi16 && isInlinableIntLiteral(Lo16); 1897 } 1898 1899 bool isFoldableLiteralV216(int32_t Literal, bool HasInv2Pi) { 1900 assert(HasInv2Pi); 1901 1902 int16_t Lo16 = static_cast<int16_t>(Literal); 1903 if (isInt<16>(Literal) || isUInt<16>(Literal)) 1904 return true; 1905 1906 int16_t Hi16 = static_cast<int16_t>(Literal >> 16); 1907 if (!(Literal & 0xffff)) 1908 return true; 1909 return Lo16 == Hi16; 1910 } 1911 1912 bool isArgPassedInSGPR(const Argument *A) { 1913 const Function *F = A->getParent(); 1914 1915 // Arguments to compute shaders are never a source of divergence. 1916 CallingConv::ID CC = F->getCallingConv(); 1917 switch (CC) { 1918 case CallingConv::AMDGPU_KERNEL: 1919 case CallingConv::SPIR_KERNEL: 1920 return true; 1921 case CallingConv::AMDGPU_VS: 1922 case CallingConv::AMDGPU_LS: 1923 case CallingConv::AMDGPU_HS: 1924 case CallingConv::AMDGPU_ES: 1925 case CallingConv::AMDGPU_GS: 1926 case CallingConv::AMDGPU_PS: 1927 case CallingConv::AMDGPU_CS: 1928 case CallingConv::AMDGPU_Gfx: 1929 // For non-compute shaders, SGPR inputs are marked with either inreg or byval. 1930 // Everything else is in VGPRs. 1931 return F->getAttributes().hasParamAttr(A->getArgNo(), Attribute::InReg) || 1932 F->getAttributes().hasParamAttr(A->getArgNo(), Attribute::ByVal); 1933 default: 1934 // TODO: Should calls support inreg for SGPR inputs? 1935 return false; 1936 } 1937 } 1938 1939 static bool hasSMEMByteOffset(const MCSubtargetInfo &ST) { 1940 return isGCN3Encoding(ST) || isGFX10Plus(ST); 1941 } 1942 1943 static bool hasSMRDSignedImmOffset(const MCSubtargetInfo &ST) { 1944 return isGFX9Plus(ST); 1945 } 1946 1947 bool isLegalSMRDEncodedUnsignedOffset(const MCSubtargetInfo &ST, 1948 int64_t EncodedOffset) { 1949 return hasSMEMByteOffset(ST) ? isUInt<20>(EncodedOffset) 1950 : isUInt<8>(EncodedOffset); 1951 } 1952 1953 bool isLegalSMRDEncodedSignedOffset(const MCSubtargetInfo &ST, 1954 int64_t EncodedOffset, 1955 bool IsBuffer) { 1956 return !IsBuffer && 1957 hasSMRDSignedImmOffset(ST) && 1958 isInt<21>(EncodedOffset); 1959 } 1960 1961 static bool isDwordAligned(uint64_t ByteOffset) { 1962 return (ByteOffset & 3) == 0; 1963 } 1964 1965 uint64_t convertSMRDOffsetUnits(const MCSubtargetInfo &ST, 1966 uint64_t ByteOffset) { 1967 if (hasSMEMByteOffset(ST)) 1968 return ByteOffset; 1969 1970 assert(isDwordAligned(ByteOffset)); 1971 return ByteOffset >> 2; 1972 } 1973 1974 Optional<int64_t> getSMRDEncodedOffset(const MCSubtargetInfo &ST, 1975 int64_t ByteOffset, bool IsBuffer) { 1976 // The signed version is always a byte offset. 1977 if (!IsBuffer && hasSMRDSignedImmOffset(ST)) { 1978 assert(hasSMEMByteOffset(ST)); 1979 return isInt<20>(ByteOffset) ? Optional<int64_t>(ByteOffset) : None; 1980 } 1981 1982 if (!isDwordAligned(ByteOffset) && !hasSMEMByteOffset(ST)) 1983 return None; 1984 1985 int64_t EncodedOffset = convertSMRDOffsetUnits(ST, ByteOffset); 1986 return isLegalSMRDEncodedUnsignedOffset(ST, EncodedOffset) 1987 ? Optional<int64_t>(EncodedOffset) 1988 : None; 1989 } 1990 1991 Optional<int64_t> getSMRDEncodedLiteralOffset32(const MCSubtargetInfo &ST, 1992 int64_t ByteOffset) { 1993 if (!isCI(ST) || !isDwordAligned(ByteOffset)) 1994 return None; 1995 1996 int64_t EncodedOffset = convertSMRDOffsetUnits(ST, ByteOffset); 1997 return isUInt<32>(EncodedOffset) ? Optional<int64_t>(EncodedOffset) : None; 1998 } 1999 2000 unsigned getNumFlatOffsetBits(const MCSubtargetInfo &ST, bool Signed) { 2001 // Address offset is 12-bit signed for GFX10, 13-bit for GFX9. 2002 if (AMDGPU::isGFX10(ST)) 2003 return Signed ? 12 : 11; 2004 2005 return Signed ? 13 : 12; 2006 } 2007 2008 // Given Imm, split it into the values to put into the SOffset and ImmOffset 2009 // fields in an MUBUF instruction. Return false if it is not possible (due to a 2010 // hardware bug needing a workaround). 2011 // 2012 // The required alignment ensures that individual address components remain 2013 // aligned if they are aligned to begin with. It also ensures that additional 2014 // offsets within the given alignment can be added to the resulting ImmOffset. 2015 bool splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, uint32_t &ImmOffset, 2016 const GCNSubtarget *Subtarget, Align Alignment) { 2017 const uint32_t MaxImm = alignDown(4095, Alignment.value()); 2018 uint32_t Overflow = 0; 2019 2020 if (Imm > MaxImm) { 2021 if (Imm <= MaxImm + 64) { 2022 // Use an SOffset inline constant for 4..64 2023 Overflow = Imm - MaxImm; 2024 Imm = MaxImm; 2025 } else { 2026 // Try to keep the same value in SOffset for adjacent loads, so that 2027 // the corresponding register contents can be re-used. 2028 // 2029 // Load values with all low-bits (except for alignment bits) set into 2030 // SOffset, so that a larger range of values can be covered using 2031 // s_movk_i32. 2032 // 2033 // Atomic operations fail to work correctly when individual address 2034 // components are unaligned, even if their sum is aligned. 2035 uint32_t High = (Imm + Alignment.value()) & ~4095; 2036 uint32_t Low = (Imm + Alignment.value()) & 4095; 2037 Imm = Low; 2038 Overflow = High - Alignment.value(); 2039 } 2040 } 2041 2042 // There is a hardware bug in SI and CI which prevents address clamping in 2043 // MUBUF instructions from working correctly with SOffsets. The immediate 2044 // offset is unaffected. 2045 if (Overflow > 0 && 2046 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 2047 return false; 2048 2049 ImmOffset = Imm; 2050 SOffset = Overflow; 2051 return true; 2052 } 2053 2054 SIModeRegisterDefaults::SIModeRegisterDefaults(const Function &F) { 2055 *this = getDefaultForCallingConv(F.getCallingConv()); 2056 2057 StringRef IEEEAttr = F.getFnAttribute("amdgpu-ieee").getValueAsString(); 2058 if (!IEEEAttr.empty()) 2059 IEEE = IEEEAttr == "true"; 2060 2061 StringRef DX10ClampAttr 2062 = F.getFnAttribute("amdgpu-dx10-clamp").getValueAsString(); 2063 if (!DX10ClampAttr.empty()) 2064 DX10Clamp = DX10ClampAttr == "true"; 2065 2066 StringRef DenormF32Attr = F.getFnAttribute("denormal-fp-math-f32").getValueAsString(); 2067 if (!DenormF32Attr.empty()) { 2068 DenormalMode DenormMode = parseDenormalFPAttribute(DenormF32Attr); 2069 FP32InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2070 FP32OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2071 } 2072 2073 StringRef DenormAttr = F.getFnAttribute("denormal-fp-math").getValueAsString(); 2074 if (!DenormAttr.empty()) { 2075 DenormalMode DenormMode = parseDenormalFPAttribute(DenormAttr); 2076 2077 if (DenormF32Attr.empty()) { 2078 FP32InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2079 FP32OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2080 } 2081 2082 FP64FP16InputDenormals = DenormMode.Input == DenormalMode::IEEE; 2083 FP64FP16OutputDenormals = DenormMode.Output == DenormalMode::IEEE; 2084 } 2085 } 2086 2087 namespace { 2088 2089 struct SourceOfDivergence { 2090 unsigned Intr; 2091 }; 2092 const SourceOfDivergence *lookupSourceOfDivergence(unsigned Intr); 2093 2094 #define GET_SourcesOfDivergence_IMPL 2095 #define GET_Gfx9BufferFormat_IMPL 2096 #define GET_Gfx10PlusBufferFormat_IMPL 2097 #include "AMDGPUGenSearchableTables.inc" 2098 2099 } // end anonymous namespace 2100 2101 bool isIntrinsicSourceOfDivergence(unsigned IntrID) { 2102 return lookupSourceOfDivergence(IntrID); 2103 } 2104 2105 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t BitsPerComp, 2106 uint8_t NumComponents, 2107 uint8_t NumFormat, 2108 const MCSubtargetInfo &STI) { 2109 return isGFX10Plus(STI) 2110 ? getGfx10PlusBufferFormatInfo(BitsPerComp, NumComponents, 2111 NumFormat) 2112 : getGfx9BufferFormatInfo(BitsPerComp, NumComponents, NumFormat); 2113 } 2114 2115 const GcnBufferFormatInfo *getGcnBufferFormatInfo(uint8_t Format, 2116 const MCSubtargetInfo &STI) { 2117 return isGFX10Plus(STI) ? getGfx10PlusBufferFormatInfo(Format) 2118 : getGfx9BufferFormatInfo(Format); 2119 } 2120 2121 } // namespace AMDGPU 2122 2123 raw_ostream &operator<<(raw_ostream &OS, 2124 const AMDGPU::IsaInfo::TargetIDSetting S) { 2125 switch (S) { 2126 case (AMDGPU::IsaInfo::TargetIDSetting::Unsupported): 2127 OS << "Unsupported"; 2128 break; 2129 case (AMDGPU::IsaInfo::TargetIDSetting::Any): 2130 OS << "Any"; 2131 break; 2132 case (AMDGPU::IsaInfo::TargetIDSetting::Off): 2133 OS << "Off"; 2134 break; 2135 case (AMDGPU::IsaInfo::TargetIDSetting::On): 2136 OS << "On"; 2137 break; 2138 } 2139 return OS; 2140 } 2141 2142 } // namespace llvm 2143