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