1 //==- SIMachineFunctionInfo.h - SIMachineFunctionInfo interface --*- C++ -*-==// 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 /// \file 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H 14 #define LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H 15 16 #include "AMDGPUArgumentUsageInfo.h" 17 #include "AMDGPUMachineFunction.h" 18 #include "AMDGPUTargetMachine.h" 19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 20 #include "SIInstrInfo.h" 21 #include "llvm/ADT/SetVector.h" 22 #include "llvm/CodeGen/MIRYamlMapping.h" 23 #include "llvm/CodeGen/PseudoSourceValue.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <optional> 26 27 namespace llvm { 28 29 class MachineFrameInfo; 30 class MachineFunction; 31 class SIMachineFunctionInfo; 32 class SIRegisterInfo; 33 class TargetRegisterClass; 34 35 class AMDGPUPseudoSourceValue : public PseudoSourceValue { 36 public: 37 enum AMDGPUPSVKind : unsigned { 38 PSVImage = PseudoSourceValue::TargetCustom, 39 GWSResource 40 }; 41 42 protected: 43 AMDGPUPseudoSourceValue(unsigned Kind, const AMDGPUTargetMachine &TM) 44 : PseudoSourceValue(Kind, TM) {} 45 46 public: 47 bool isConstant(const MachineFrameInfo *) const override { 48 // This should probably be true for most images, but we will start by being 49 // conservative. 50 return false; 51 } 52 53 bool isAliased(const MachineFrameInfo *) const override { 54 return true; 55 } 56 57 bool mayAlias(const MachineFrameInfo *) const override { 58 return true; 59 } 60 }; 61 62 class AMDGPUGWSResourcePseudoSourceValue final : public AMDGPUPseudoSourceValue { 63 public: 64 explicit AMDGPUGWSResourcePseudoSourceValue(const AMDGPUTargetMachine &TM) 65 : AMDGPUPseudoSourceValue(GWSResource, TM) {} 66 67 static bool classof(const PseudoSourceValue *V) { 68 return V->kind() == GWSResource; 69 } 70 71 // These are inaccessible memory from IR. 72 bool isAliased(const MachineFrameInfo *) const override { 73 return false; 74 } 75 76 // These are inaccessible memory from IR. 77 bool mayAlias(const MachineFrameInfo *) const override { 78 return false; 79 } 80 81 void printCustom(raw_ostream &OS) const override { 82 OS << "GWSResource"; 83 } 84 }; 85 86 namespace yaml { 87 88 struct SIArgument { 89 bool IsRegister; 90 union { 91 StringValue RegisterName; 92 unsigned StackOffset; 93 }; 94 std::optional<unsigned> Mask; 95 96 // Default constructor, which creates a stack argument. 97 SIArgument() : IsRegister(false), StackOffset(0) {} 98 SIArgument(const SIArgument &Other) { 99 IsRegister = Other.IsRegister; 100 if (IsRegister) { 101 ::new ((void *)std::addressof(RegisterName)) 102 StringValue(Other.RegisterName); 103 } else 104 StackOffset = Other.StackOffset; 105 Mask = Other.Mask; 106 } 107 SIArgument &operator=(const SIArgument &Other) { 108 IsRegister = Other.IsRegister; 109 if (IsRegister) { 110 ::new ((void *)std::addressof(RegisterName)) 111 StringValue(Other.RegisterName); 112 } else 113 StackOffset = Other.StackOffset; 114 Mask = Other.Mask; 115 return *this; 116 } 117 ~SIArgument() { 118 if (IsRegister) 119 RegisterName.~StringValue(); 120 } 121 122 // Helper to create a register or stack argument. 123 static inline SIArgument createArgument(bool IsReg) { 124 if (IsReg) 125 return SIArgument(IsReg); 126 return SIArgument(); 127 } 128 129 private: 130 // Construct a register argument. 131 SIArgument(bool) : IsRegister(true), RegisterName() {} 132 }; 133 134 template <> struct MappingTraits<SIArgument> { 135 static void mapping(IO &YamlIO, SIArgument &A) { 136 if (YamlIO.outputting()) { 137 if (A.IsRegister) 138 YamlIO.mapRequired("reg", A.RegisterName); 139 else 140 YamlIO.mapRequired("offset", A.StackOffset); 141 } else { 142 auto Keys = YamlIO.keys(); 143 if (is_contained(Keys, "reg")) { 144 A = SIArgument::createArgument(true); 145 YamlIO.mapRequired("reg", A.RegisterName); 146 } else if (is_contained(Keys, "offset")) 147 YamlIO.mapRequired("offset", A.StackOffset); 148 else 149 YamlIO.setError("missing required key 'reg' or 'offset'"); 150 } 151 YamlIO.mapOptional("mask", A.Mask); 152 } 153 static const bool flow = true; 154 }; 155 156 struct SIArgumentInfo { 157 std::optional<SIArgument> PrivateSegmentBuffer; 158 std::optional<SIArgument> DispatchPtr; 159 std::optional<SIArgument> QueuePtr; 160 std::optional<SIArgument> KernargSegmentPtr; 161 std::optional<SIArgument> DispatchID; 162 std::optional<SIArgument> FlatScratchInit; 163 std::optional<SIArgument> PrivateSegmentSize; 164 165 std::optional<SIArgument> WorkGroupIDX; 166 std::optional<SIArgument> WorkGroupIDY; 167 std::optional<SIArgument> WorkGroupIDZ; 168 std::optional<SIArgument> WorkGroupInfo; 169 std::optional<SIArgument> LDSKernelId; 170 std::optional<SIArgument> PrivateSegmentWaveByteOffset; 171 172 std::optional<SIArgument> ImplicitArgPtr; 173 std::optional<SIArgument> ImplicitBufferPtr; 174 175 std::optional<SIArgument> WorkItemIDX; 176 std::optional<SIArgument> WorkItemIDY; 177 std::optional<SIArgument> WorkItemIDZ; 178 }; 179 180 template <> struct MappingTraits<SIArgumentInfo> { 181 static void mapping(IO &YamlIO, SIArgumentInfo &AI) { 182 YamlIO.mapOptional("privateSegmentBuffer", AI.PrivateSegmentBuffer); 183 YamlIO.mapOptional("dispatchPtr", AI.DispatchPtr); 184 YamlIO.mapOptional("queuePtr", AI.QueuePtr); 185 YamlIO.mapOptional("kernargSegmentPtr", AI.KernargSegmentPtr); 186 YamlIO.mapOptional("dispatchID", AI.DispatchID); 187 YamlIO.mapOptional("flatScratchInit", AI.FlatScratchInit); 188 YamlIO.mapOptional("privateSegmentSize", AI.PrivateSegmentSize); 189 190 YamlIO.mapOptional("workGroupIDX", AI.WorkGroupIDX); 191 YamlIO.mapOptional("workGroupIDY", AI.WorkGroupIDY); 192 YamlIO.mapOptional("workGroupIDZ", AI.WorkGroupIDZ); 193 YamlIO.mapOptional("workGroupInfo", AI.WorkGroupInfo); 194 YamlIO.mapOptional("LDSKernelId", AI.LDSKernelId); 195 YamlIO.mapOptional("privateSegmentWaveByteOffset", 196 AI.PrivateSegmentWaveByteOffset); 197 198 YamlIO.mapOptional("implicitArgPtr", AI.ImplicitArgPtr); 199 YamlIO.mapOptional("implicitBufferPtr", AI.ImplicitBufferPtr); 200 201 YamlIO.mapOptional("workItemIDX", AI.WorkItemIDX); 202 YamlIO.mapOptional("workItemIDY", AI.WorkItemIDY); 203 YamlIO.mapOptional("workItemIDZ", AI.WorkItemIDZ); 204 } 205 }; 206 207 // Default to default mode for default calling convention. 208 struct SIMode { 209 bool IEEE = true; 210 bool DX10Clamp = true; 211 bool FP32InputDenormals = true; 212 bool FP32OutputDenormals = true; 213 bool FP64FP16InputDenormals = true; 214 bool FP64FP16OutputDenormals = true; 215 216 SIMode() = default; 217 218 SIMode(const AMDGPU::SIModeRegisterDefaults &Mode) { 219 IEEE = Mode.IEEE; 220 DX10Clamp = Mode.DX10Clamp; 221 FP32InputDenormals = Mode.FP32InputDenormals; 222 FP32OutputDenormals = Mode.FP32OutputDenormals; 223 FP64FP16InputDenormals = Mode.FP64FP16InputDenormals; 224 FP64FP16OutputDenormals = Mode.FP64FP16OutputDenormals; 225 } 226 227 bool operator ==(const SIMode Other) const { 228 return IEEE == Other.IEEE && 229 DX10Clamp == Other.DX10Clamp && 230 FP32InputDenormals == Other.FP32InputDenormals && 231 FP32OutputDenormals == Other.FP32OutputDenormals && 232 FP64FP16InputDenormals == Other.FP64FP16InputDenormals && 233 FP64FP16OutputDenormals == Other.FP64FP16OutputDenormals; 234 } 235 }; 236 237 template <> struct MappingTraits<SIMode> { 238 static void mapping(IO &YamlIO, SIMode &Mode) { 239 YamlIO.mapOptional("ieee", Mode.IEEE, true); 240 YamlIO.mapOptional("dx10-clamp", Mode.DX10Clamp, true); 241 YamlIO.mapOptional("fp32-input-denormals", Mode.FP32InputDenormals, true); 242 YamlIO.mapOptional("fp32-output-denormals", Mode.FP32OutputDenormals, true); 243 YamlIO.mapOptional("fp64-fp16-input-denormals", Mode.FP64FP16InputDenormals, true); 244 YamlIO.mapOptional("fp64-fp16-output-denormals", Mode.FP64FP16OutputDenormals, true); 245 } 246 }; 247 248 struct SIMachineFunctionInfo final : public yaml::MachineFunctionInfo { 249 uint64_t ExplicitKernArgSize = 0; 250 Align MaxKernArgAlign; 251 uint32_t LDSSize = 0; 252 uint32_t GDSSize = 0; 253 Align DynLDSAlign; 254 bool IsEntryFunction = false; 255 bool NoSignedZerosFPMath = false; 256 bool MemoryBound = false; 257 bool WaveLimiter = false; 258 bool HasSpilledSGPRs = false; 259 bool HasSpilledVGPRs = false; 260 uint32_t HighBitsOf32BitAddress = 0; 261 262 // TODO: 10 may be a better default since it's the maximum. 263 unsigned Occupancy = 0; 264 265 SmallVector<StringValue> WWMReservedRegs; 266 267 StringValue ScratchRSrcReg = "$private_rsrc_reg"; 268 StringValue FrameOffsetReg = "$fp_reg"; 269 StringValue StackPtrOffsetReg = "$sp_reg"; 270 271 unsigned BytesInStackArgArea = 0; 272 bool ReturnsVoid = true; 273 274 std::optional<SIArgumentInfo> ArgInfo; 275 SIMode Mode; 276 std::optional<FrameIndex> ScavengeFI; 277 StringValue VGPRForAGPRCopy; 278 279 SIMachineFunctionInfo() = default; 280 SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo &, 281 const TargetRegisterInfo &TRI, 282 const llvm::MachineFunction &MF); 283 284 void mappingImpl(yaml::IO &YamlIO) override; 285 ~SIMachineFunctionInfo() = default; 286 }; 287 288 template <> struct MappingTraits<SIMachineFunctionInfo> { 289 static void mapping(IO &YamlIO, SIMachineFunctionInfo &MFI) { 290 YamlIO.mapOptional("explicitKernArgSize", MFI.ExplicitKernArgSize, 291 UINT64_C(0)); 292 YamlIO.mapOptional("maxKernArgAlign", MFI.MaxKernArgAlign); 293 YamlIO.mapOptional("ldsSize", MFI.LDSSize, 0u); 294 YamlIO.mapOptional("gdsSize", MFI.GDSSize, 0u); 295 YamlIO.mapOptional("dynLDSAlign", MFI.DynLDSAlign, Align()); 296 YamlIO.mapOptional("isEntryFunction", MFI.IsEntryFunction, false); 297 YamlIO.mapOptional("noSignedZerosFPMath", MFI.NoSignedZerosFPMath, false); 298 YamlIO.mapOptional("memoryBound", MFI.MemoryBound, false); 299 YamlIO.mapOptional("waveLimiter", MFI.WaveLimiter, false); 300 YamlIO.mapOptional("hasSpilledSGPRs", MFI.HasSpilledSGPRs, false); 301 YamlIO.mapOptional("hasSpilledVGPRs", MFI.HasSpilledVGPRs, false); 302 YamlIO.mapOptional("scratchRSrcReg", MFI.ScratchRSrcReg, 303 StringValue("$private_rsrc_reg")); 304 YamlIO.mapOptional("frameOffsetReg", MFI.FrameOffsetReg, 305 StringValue("$fp_reg")); 306 YamlIO.mapOptional("stackPtrOffsetReg", MFI.StackPtrOffsetReg, 307 StringValue("$sp_reg")); 308 YamlIO.mapOptional("bytesInStackArgArea", MFI.BytesInStackArgArea, 0u); 309 YamlIO.mapOptional("returnsVoid", MFI.ReturnsVoid, true); 310 YamlIO.mapOptional("argumentInfo", MFI.ArgInfo); 311 YamlIO.mapOptional("mode", MFI.Mode, SIMode()); 312 YamlIO.mapOptional("highBitsOf32BitAddress", 313 MFI.HighBitsOf32BitAddress, 0u); 314 YamlIO.mapOptional("occupancy", MFI.Occupancy, 0); 315 YamlIO.mapOptional("wwmReservedRegs", MFI.WWMReservedRegs); 316 YamlIO.mapOptional("scavengeFI", MFI.ScavengeFI); 317 YamlIO.mapOptional("vgprForAGPRCopy", MFI.VGPRForAGPRCopy, 318 StringValue()); // Don't print out when it's empty. 319 } 320 }; 321 322 } // end namespace yaml 323 324 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which 325 /// tells the hardware which interpolation parameters to load. 326 class SIMachineFunctionInfo final : public AMDGPUMachineFunction { 327 friend class GCNTargetMachine; 328 329 // State of MODE register, assumed FP mode. 330 AMDGPU::SIModeRegisterDefaults Mode; 331 332 // Registers that may be reserved for spilling purposes. These may be the same 333 // as the input registers. 334 Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG; 335 336 // This is the unswizzled offset from the current dispatch's scratch wave 337 // base to the beginning of the current function's frame. 338 Register FrameOffsetReg = AMDGPU::FP_REG; 339 340 // This is an ABI register used in the non-entry calling convention to 341 // communicate the unswizzled offset from the current dispatch's scratch wave 342 // base to the beginning of the new function's frame. 343 Register StackPtrOffsetReg = AMDGPU::SP_REG; 344 345 AMDGPUFunctionArgInfo ArgInfo; 346 347 // Graphics info. 348 unsigned PSInputAddr = 0; 349 unsigned PSInputEnable = 0; 350 351 /// Number of bytes of arguments this function has on the stack. If the callee 352 /// is expected to restore the argument stack this should be a multiple of 16, 353 /// all usable during a tail call. 354 /// 355 /// The alternative would forbid tail call optimisation in some cases: if we 356 /// want to transfer control from a function with 8-bytes of stack-argument 357 /// space to a function with 16-bytes then misalignment of this value would 358 /// make a stack adjustment necessary, which could not be undone by the 359 /// callee. 360 unsigned BytesInStackArgArea = 0; 361 362 bool ReturnsVoid = true; 363 364 // A pair of default/requested minimum/maximum flat work group sizes. 365 // Minimum - first, maximum - second. 366 std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0}; 367 368 // A pair of default/requested minimum/maximum number of waves per execution 369 // unit. Minimum - first, maximum - second. 370 std::pair<unsigned, unsigned> WavesPerEU = {0, 0}; 371 372 const AMDGPUGWSResourcePseudoSourceValue GWSResourcePSV; 373 374 private: 375 unsigned NumUserSGPRs = 0; 376 unsigned NumSystemSGPRs = 0; 377 378 bool HasSpilledSGPRs = false; 379 bool HasSpilledVGPRs = false; 380 bool HasNonSpillStackObjects = false; 381 bool IsStackRealigned = false; 382 383 unsigned NumSpilledSGPRs = 0; 384 unsigned NumSpilledVGPRs = 0; 385 386 // Feature bits required for inputs passed in user SGPRs. 387 bool PrivateSegmentBuffer : 1; 388 bool DispatchPtr : 1; 389 bool QueuePtr : 1; 390 bool KernargSegmentPtr : 1; 391 bool DispatchID : 1; 392 bool FlatScratchInit : 1; 393 394 // Feature bits required for inputs passed in system SGPRs. 395 bool WorkGroupIDX : 1; // Always initialized. 396 bool WorkGroupIDY : 1; 397 bool WorkGroupIDZ : 1; 398 bool WorkGroupInfo : 1; 399 bool LDSKernelId : 1; 400 bool PrivateSegmentWaveByteOffset : 1; 401 402 bool WorkItemIDX : 1; // Always initialized. 403 bool WorkItemIDY : 1; 404 bool WorkItemIDZ : 1; 405 406 // Private memory buffer 407 // Compute directly in sgpr[0:1] 408 // Other shaders indirect 64-bits at sgpr[0:1] 409 bool ImplicitBufferPtr : 1; 410 411 // Pointer to where the ABI inserts special kernel arguments separate from the 412 // user arguments. This is an offset from the KernargSegmentPtr. 413 bool ImplicitArgPtr : 1; 414 415 bool MayNeedAGPRs : 1; 416 417 // The hard-wired high half of the address of the global information table 418 // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since 419 // current hardware only allows a 16 bit value. 420 unsigned GITPtrHigh; 421 422 unsigned HighBitsOf32BitAddress; 423 424 // Current recorded maximum possible occupancy. 425 unsigned Occupancy; 426 427 mutable std::optional<bool> UsesAGPRs; 428 429 MCPhysReg getNextUserSGPR() const; 430 431 MCPhysReg getNextSystemSGPR() const; 432 433 public: 434 struct SGPRSpillVGPR { 435 // VGPR used for SGPR spills 436 Register VGPR; 437 438 // If the VGPR is used for SGPR spills in a non-entrypoint function, the 439 // stack slot used to save/restore it in the prolog/epilog. 440 std::optional<int> FI; 441 442 SGPRSpillVGPR(Register V, std::optional<int> F) : VGPR(V), FI(F) {} 443 }; 444 445 struct VGPRSpillToAGPR { 446 SmallVector<MCPhysReg, 32> Lanes; 447 bool FullyAllocated = false; 448 bool IsDead = false; 449 }; 450 451 // Track VGPRs reserved for WWM. 452 SmallSetVector<Register, 8> WWMReservedRegs; 453 454 /// Track stack slots used for save/restore of reserved WWM VGPRs in the 455 /// prolog/epilog. 456 457 /// FIXME: This is temporary state only needed in PrologEpilogInserter, and 458 /// doesn't really belong here. It does not require serialization 459 SmallVector<int, 8> WWMReservedFrameIndexes; 460 461 void allocateWWMReservedSpillSlots(MachineFrameInfo &MFI, 462 const SIRegisterInfo &TRI); 463 464 auto wwmAllocation() const { 465 assert(WWMReservedRegs.size() == WWMReservedFrameIndexes.size()); 466 return zip(WWMReservedRegs, WWMReservedFrameIndexes); 467 } 468 469 private: 470 // Track VGPR + wave index for each subregister of the SGPR spilled to 471 // frameindex key. 472 DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>> SGPRToVGPRSpills; 473 unsigned NumVGPRSpillLanes = 0; 474 SmallVector<SGPRSpillVGPR, 2> SpillVGPRs; 475 476 DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills; 477 478 // AGPRs used for VGPR spills. 479 SmallVector<MCPhysReg, 32> SpillAGPR; 480 481 // VGPRs used for AGPR spills. 482 SmallVector<MCPhysReg, 32> SpillVGPR; 483 484 // Emergency stack slot. Sometimes, we create this before finalizing the stack 485 // frame, so save it here and add it to the RegScavenger later. 486 std::optional<int> ScavengeFI; 487 488 private: 489 Register VGPRForAGPRCopy; 490 491 public: 492 Register getVGPRForAGPRCopy() const { 493 return VGPRForAGPRCopy; 494 } 495 496 void setVGPRForAGPRCopy(Register NewVGPRForAGPRCopy) { 497 VGPRForAGPRCopy = NewVGPRForAGPRCopy; 498 } 499 500 public: // FIXME 501 /// If this is set, an SGPR used for save/restore of the register used for the 502 /// frame pointer. 503 Register SGPRForFPSaveRestoreCopy; 504 std::optional<int> FramePointerSaveIndex; 505 506 /// If this is set, an SGPR used for save/restore of the register used for the 507 /// base pointer. 508 Register SGPRForBPSaveRestoreCopy; 509 std::optional<int> BasePointerSaveIndex; 510 511 bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg); 512 513 public: 514 SIMachineFunctionInfo(const MachineFunction &MF); 515 SIMachineFunctionInfo(const SIMachineFunctionInfo &MFI) = default; 516 517 MachineFunctionInfo * 518 clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, 519 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) 520 const override; 521 522 bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI, 523 const MachineFunction &MF, 524 PerFunctionMIParsingState &PFS, 525 SMDiagnostic &Error, SMRange &SourceRange); 526 527 void reserveWWMRegister(Register Reg) { 528 WWMReservedRegs.insert(Reg); 529 } 530 531 AMDGPU::SIModeRegisterDefaults getMode() const { 532 return Mode; 533 } 534 535 ArrayRef<SIRegisterInfo::SpilledReg> 536 getSGPRToVGPRSpills(int FrameIndex) const { 537 auto I = SGPRToVGPRSpills.find(FrameIndex); 538 return (I == SGPRToVGPRSpills.end()) 539 ? ArrayRef<SIRegisterInfo::SpilledReg>() 540 : makeArrayRef(I->second); 541 } 542 543 ArrayRef<SGPRSpillVGPR> getSGPRSpillVGPRs() const { return SpillVGPRs; } 544 545 ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const { 546 return SpillAGPR; 547 } 548 549 ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const { 550 return SpillVGPR; 551 } 552 553 MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const { 554 auto I = VGPRToAGPRSpills.find(FrameIndex); 555 return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister 556 : I->second.Lanes[Lane]; 557 } 558 559 void setVGPRToAGPRSpillDead(int FrameIndex) { 560 auto I = VGPRToAGPRSpills.find(FrameIndex); 561 if (I != VGPRToAGPRSpills.end()) 562 I->second.IsDead = true; 563 } 564 565 bool haveFreeLanesForSGPRSpill(const MachineFunction &MF, 566 unsigned NumLane) const; 567 bool allocateSGPRSpillToVGPR(MachineFunction &MF, int FI); 568 bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR); 569 570 /// If \p ResetSGPRSpillStackIDs is true, reset the stack ID from sgpr-spill 571 /// to the default stack. 572 bool removeDeadFrameIndices(MachineFrameInfo &MFI, 573 bool ResetSGPRSpillStackIDs); 574 575 int getScavengeFI(MachineFrameInfo &MFI, const SIRegisterInfo &TRI); 576 std::optional<int> getOptionalScavengeFI() const { return ScavengeFI; } 577 578 unsigned getBytesInStackArgArea() const { 579 return BytesInStackArgArea; 580 } 581 582 void setBytesInStackArgArea(unsigned Bytes) { 583 BytesInStackArgArea = Bytes; 584 } 585 586 // Add user SGPRs. 587 Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI); 588 Register addDispatchPtr(const SIRegisterInfo &TRI); 589 Register addQueuePtr(const SIRegisterInfo &TRI); 590 Register addKernargSegmentPtr(const SIRegisterInfo &TRI); 591 Register addDispatchID(const SIRegisterInfo &TRI); 592 Register addFlatScratchInit(const SIRegisterInfo &TRI); 593 Register addImplicitBufferPtr(const SIRegisterInfo &TRI); 594 Register addLDSKernelId(); 595 596 /// Increment user SGPRs used for padding the argument list only. 597 Register addReservedUserSGPR() { 598 Register Next = getNextUserSGPR(); 599 ++NumUserSGPRs; 600 return Next; 601 } 602 603 // Add system SGPRs. 604 Register addWorkGroupIDX() { 605 ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR()); 606 NumSystemSGPRs += 1; 607 return ArgInfo.WorkGroupIDX.getRegister(); 608 } 609 610 Register addWorkGroupIDY() { 611 ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR()); 612 NumSystemSGPRs += 1; 613 return ArgInfo.WorkGroupIDY.getRegister(); 614 } 615 616 Register addWorkGroupIDZ() { 617 ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR()); 618 NumSystemSGPRs += 1; 619 return ArgInfo.WorkGroupIDZ.getRegister(); 620 } 621 622 Register addWorkGroupInfo() { 623 ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR()); 624 NumSystemSGPRs += 1; 625 return ArgInfo.WorkGroupInfo.getRegister(); 626 } 627 628 // Add special VGPR inputs 629 void setWorkItemIDX(ArgDescriptor Arg) { 630 ArgInfo.WorkItemIDX = Arg; 631 } 632 633 void setWorkItemIDY(ArgDescriptor Arg) { 634 ArgInfo.WorkItemIDY = Arg; 635 } 636 637 void setWorkItemIDZ(ArgDescriptor Arg) { 638 ArgInfo.WorkItemIDZ = Arg; 639 } 640 641 Register addPrivateSegmentWaveByteOffset() { 642 ArgInfo.PrivateSegmentWaveByteOffset 643 = ArgDescriptor::createRegister(getNextSystemSGPR()); 644 NumSystemSGPRs += 1; 645 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 646 } 647 648 void setPrivateSegmentWaveByteOffset(Register Reg) { 649 ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg); 650 } 651 652 bool hasPrivateSegmentBuffer() const { 653 return PrivateSegmentBuffer; 654 } 655 656 bool hasDispatchPtr() const { 657 return DispatchPtr; 658 } 659 660 bool hasQueuePtr() const { 661 return QueuePtr; 662 } 663 664 bool hasKernargSegmentPtr() const { 665 return KernargSegmentPtr; 666 } 667 668 bool hasDispatchID() const { 669 return DispatchID; 670 } 671 672 bool hasFlatScratchInit() const { 673 return FlatScratchInit; 674 } 675 676 bool hasWorkGroupIDX() const { 677 return WorkGroupIDX; 678 } 679 680 bool hasWorkGroupIDY() const { 681 return WorkGroupIDY; 682 } 683 684 bool hasWorkGroupIDZ() const { 685 return WorkGroupIDZ; 686 } 687 688 bool hasWorkGroupInfo() const { 689 return WorkGroupInfo; 690 } 691 692 bool hasLDSKernelId() const { return LDSKernelId; } 693 694 bool hasPrivateSegmentWaveByteOffset() const { 695 return PrivateSegmentWaveByteOffset; 696 } 697 698 bool hasWorkItemIDX() const { 699 return WorkItemIDX; 700 } 701 702 bool hasWorkItemIDY() const { 703 return WorkItemIDY; 704 } 705 706 bool hasWorkItemIDZ() const { 707 return WorkItemIDZ; 708 } 709 710 bool hasImplicitArgPtr() const { 711 return ImplicitArgPtr; 712 } 713 714 bool hasImplicitBufferPtr() const { 715 return ImplicitBufferPtr; 716 } 717 718 AMDGPUFunctionArgInfo &getArgInfo() { 719 return ArgInfo; 720 } 721 722 const AMDGPUFunctionArgInfo &getArgInfo() const { 723 return ArgInfo; 724 } 725 726 std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT> 727 getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 728 return ArgInfo.getPreloadedValue(Value); 729 } 730 731 MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 732 auto Arg = std::get<0>(ArgInfo.getPreloadedValue(Value)); 733 return Arg ? Arg->getRegister() : MCRegister(); 734 } 735 736 unsigned getGITPtrHigh() const { 737 return GITPtrHigh; 738 } 739 740 Register getGITPtrLoReg(const MachineFunction &MF) const; 741 742 uint32_t get32BitAddressHighBits() const { 743 return HighBitsOf32BitAddress; 744 } 745 746 unsigned getNumUserSGPRs() const { 747 return NumUserSGPRs; 748 } 749 750 unsigned getNumPreloadedSGPRs() const { 751 return NumUserSGPRs + NumSystemSGPRs; 752 } 753 754 Register getPrivateSegmentWaveByteOffsetSystemSGPR() const { 755 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 756 } 757 758 /// Returns the physical register reserved for use as the resource 759 /// descriptor for scratch accesses. 760 Register getScratchRSrcReg() const { 761 return ScratchRSrcReg; 762 } 763 764 void setScratchRSrcReg(Register Reg) { 765 assert(Reg != 0 && "Should never be unset"); 766 ScratchRSrcReg = Reg; 767 } 768 769 Register getFrameOffsetReg() const { 770 return FrameOffsetReg; 771 } 772 773 void setFrameOffsetReg(Register Reg) { 774 assert(Reg != 0 && "Should never be unset"); 775 FrameOffsetReg = Reg; 776 } 777 778 void setStackPtrOffsetReg(Register Reg) { 779 assert(Reg != 0 && "Should never be unset"); 780 StackPtrOffsetReg = Reg; 781 } 782 783 // Note the unset value for this is AMDGPU::SP_REG rather than 784 // NoRegister. This is mostly a workaround for MIR tests where state that 785 // can't be directly computed from the function is not preserved in serialized 786 // MIR. 787 Register getStackPtrOffsetReg() const { 788 return StackPtrOffsetReg; 789 } 790 791 Register getQueuePtrUserSGPR() const { 792 return ArgInfo.QueuePtr.getRegister(); 793 } 794 795 Register getImplicitBufferPtrUserSGPR() const { 796 return ArgInfo.ImplicitBufferPtr.getRegister(); 797 } 798 799 bool hasSpilledSGPRs() const { 800 return HasSpilledSGPRs; 801 } 802 803 void setHasSpilledSGPRs(bool Spill = true) { 804 HasSpilledSGPRs = Spill; 805 } 806 807 bool hasSpilledVGPRs() const { 808 return HasSpilledVGPRs; 809 } 810 811 void setHasSpilledVGPRs(bool Spill = true) { 812 HasSpilledVGPRs = Spill; 813 } 814 815 bool hasNonSpillStackObjects() const { 816 return HasNonSpillStackObjects; 817 } 818 819 void setHasNonSpillStackObjects(bool StackObject = true) { 820 HasNonSpillStackObjects = StackObject; 821 } 822 823 bool isStackRealigned() const { 824 return IsStackRealigned; 825 } 826 827 void setIsStackRealigned(bool Realigned = true) { 828 IsStackRealigned = Realigned; 829 } 830 831 unsigned getNumSpilledSGPRs() const { 832 return NumSpilledSGPRs; 833 } 834 835 unsigned getNumSpilledVGPRs() const { 836 return NumSpilledVGPRs; 837 } 838 839 void addToSpilledSGPRs(unsigned num) { 840 NumSpilledSGPRs += num; 841 } 842 843 void addToSpilledVGPRs(unsigned num) { 844 NumSpilledVGPRs += num; 845 } 846 847 unsigned getPSInputAddr() const { 848 return PSInputAddr; 849 } 850 851 unsigned getPSInputEnable() const { 852 return PSInputEnable; 853 } 854 855 bool isPSInputAllocated(unsigned Index) const { 856 return PSInputAddr & (1 << Index); 857 } 858 859 void markPSInputAllocated(unsigned Index) { 860 PSInputAddr |= 1 << Index; 861 } 862 863 void markPSInputEnabled(unsigned Index) { 864 PSInputEnable |= 1 << Index; 865 } 866 867 bool returnsVoid() const { 868 return ReturnsVoid; 869 } 870 871 void setIfReturnsVoid(bool Value) { 872 ReturnsVoid = Value; 873 } 874 875 /// \returns A pair of default/requested minimum/maximum flat work group sizes 876 /// for this function. 877 std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const { 878 return FlatWorkGroupSizes; 879 } 880 881 /// \returns Default/requested minimum flat work group size for this function. 882 unsigned getMinFlatWorkGroupSize() const { 883 return FlatWorkGroupSizes.first; 884 } 885 886 /// \returns Default/requested maximum flat work group size for this function. 887 unsigned getMaxFlatWorkGroupSize() const { 888 return FlatWorkGroupSizes.second; 889 } 890 891 /// \returns A pair of default/requested minimum/maximum number of waves per 892 /// execution unit. 893 std::pair<unsigned, unsigned> getWavesPerEU() const { 894 return WavesPerEU; 895 } 896 897 /// \returns Default/requested minimum number of waves per execution unit. 898 unsigned getMinWavesPerEU() const { 899 return WavesPerEU.first; 900 } 901 902 /// \returns Default/requested maximum number of waves per execution unit. 903 unsigned getMaxWavesPerEU() const { 904 return WavesPerEU.second; 905 } 906 907 /// \returns SGPR used for \p Dim's work group ID. 908 Register getWorkGroupIDSGPR(unsigned Dim) const { 909 switch (Dim) { 910 case 0: 911 assert(hasWorkGroupIDX()); 912 return ArgInfo.WorkGroupIDX.getRegister(); 913 case 1: 914 assert(hasWorkGroupIDY()); 915 return ArgInfo.WorkGroupIDY.getRegister(); 916 case 2: 917 assert(hasWorkGroupIDZ()); 918 return ArgInfo.WorkGroupIDZ.getRegister(); 919 } 920 llvm_unreachable("unexpected dimension"); 921 } 922 923 const AMDGPUGWSResourcePseudoSourceValue * 924 getGWSPSV(const AMDGPUTargetMachine &TM) { 925 return &GWSResourcePSV; 926 } 927 928 unsigned getOccupancy() const { 929 return Occupancy; 930 } 931 932 unsigned getMinAllowedOccupancy() const { 933 if (!isMemoryBound() && !needsWaveLimiter()) 934 return Occupancy; 935 return (Occupancy < 4) ? Occupancy : 4; 936 } 937 938 void limitOccupancy(const MachineFunction &MF); 939 940 void limitOccupancy(unsigned Limit) { 941 if (Occupancy > Limit) 942 Occupancy = Limit; 943 } 944 945 void increaseOccupancy(const MachineFunction &MF, unsigned Limit) { 946 if (Occupancy < Limit) 947 Occupancy = Limit; 948 limitOccupancy(MF); 949 } 950 951 bool mayNeedAGPRs() const { 952 return MayNeedAGPRs; 953 } 954 955 // \returns true if a function has a use of AGPRs via inline asm or 956 // has a call which may use it. 957 bool mayUseAGPRs(const Function &F) const; 958 959 // \returns true if a function needs or may need AGPRs. 960 bool usesAGPRs(const MachineFunction &MF) const; 961 }; 962 963 } // end namespace llvm 964 965 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H 966