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 // A CSR SGPR value can be preserved inside a callee using one of the following 325 // methods. 326 // 1. Copy to an unused scratch SGPR. 327 // 2. Spill to a VGPR lane. 328 // 3. Spill to memory via. a scratch VGPR. 329 // class PrologEpilogSGPRSaveRestoreInfo represents the save/restore method used 330 // for an SGPR at function prolog/epilog. 331 enum class SGPRSaveKind : uint8_t { 332 COPY_TO_SCRATCH_SGPR, 333 SPILL_TO_VGPR_LANE, 334 SPILL_TO_MEM 335 }; 336 337 class PrologEpilogSGPRSaveRestoreInfo { 338 SGPRSaveKind Kind; 339 union { 340 int Index; 341 Register Reg; 342 }; 343 344 public: 345 PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, int I) : Kind(K), Index(I) {} 346 PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, Register R) 347 : Kind(K), Reg(R) {} 348 Register getReg() const { return Reg; } 349 int getIndex() const { return Index; } 350 SGPRSaveKind getKind() const { return Kind; } 351 }; 352 353 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which 354 /// tells the hardware which interpolation parameters to load. 355 class SIMachineFunctionInfo final : public AMDGPUMachineFunction { 356 friend class GCNTargetMachine; 357 358 // State of MODE register, assumed FP mode. 359 AMDGPU::SIModeRegisterDefaults Mode; 360 361 // Registers that may be reserved for spilling purposes. These may be the same 362 // as the input registers. 363 Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG; 364 365 // This is the unswizzled offset from the current dispatch's scratch wave 366 // base to the beginning of the current function's frame. 367 Register FrameOffsetReg = AMDGPU::FP_REG; 368 369 // This is an ABI register used in the non-entry calling convention to 370 // communicate the unswizzled offset from the current dispatch's scratch wave 371 // base to the beginning of the new function's frame. 372 Register StackPtrOffsetReg = AMDGPU::SP_REG; 373 374 AMDGPUFunctionArgInfo ArgInfo; 375 376 // Graphics info. 377 unsigned PSInputAddr = 0; 378 unsigned PSInputEnable = 0; 379 380 /// Number of bytes of arguments this function has on the stack. If the callee 381 /// is expected to restore the argument stack this should be a multiple of 16, 382 /// all usable during a tail call. 383 /// 384 /// The alternative would forbid tail call optimisation in some cases: if we 385 /// want to transfer control from a function with 8-bytes of stack-argument 386 /// space to a function with 16-bytes then misalignment of this value would 387 /// make a stack adjustment necessary, which could not be undone by the 388 /// callee. 389 unsigned BytesInStackArgArea = 0; 390 391 bool ReturnsVoid = true; 392 393 // A pair of default/requested minimum/maximum flat work group sizes. 394 // Minimum - first, maximum - second. 395 std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0}; 396 397 // A pair of default/requested minimum/maximum number of waves per execution 398 // unit. Minimum - first, maximum - second. 399 std::pair<unsigned, unsigned> WavesPerEU = {0, 0}; 400 401 const AMDGPUGWSResourcePseudoSourceValue GWSResourcePSV; 402 403 private: 404 unsigned NumUserSGPRs = 0; 405 unsigned NumSystemSGPRs = 0; 406 407 bool HasSpilledSGPRs = false; 408 bool HasSpilledVGPRs = false; 409 bool HasNonSpillStackObjects = false; 410 bool IsStackRealigned = false; 411 412 unsigned NumSpilledSGPRs = 0; 413 unsigned NumSpilledVGPRs = 0; 414 415 // Feature bits required for inputs passed in user SGPRs. 416 bool PrivateSegmentBuffer : 1; 417 bool DispatchPtr : 1; 418 bool QueuePtr : 1; 419 bool KernargSegmentPtr : 1; 420 bool DispatchID : 1; 421 bool FlatScratchInit : 1; 422 423 // Feature bits required for inputs passed in system SGPRs. 424 bool WorkGroupIDX : 1; // Always initialized. 425 bool WorkGroupIDY : 1; 426 bool WorkGroupIDZ : 1; 427 bool WorkGroupInfo : 1; 428 bool LDSKernelId : 1; 429 bool PrivateSegmentWaveByteOffset : 1; 430 431 bool WorkItemIDX : 1; // Always initialized. 432 bool WorkItemIDY : 1; 433 bool WorkItemIDZ : 1; 434 435 // Private memory buffer 436 // Compute directly in sgpr[0:1] 437 // Other shaders indirect 64-bits at sgpr[0:1] 438 bool ImplicitBufferPtr : 1; 439 440 // Pointer to where the ABI inserts special kernel arguments separate from the 441 // user arguments. This is an offset from the KernargSegmentPtr. 442 bool ImplicitArgPtr : 1; 443 444 bool MayNeedAGPRs : 1; 445 446 // The hard-wired high half of the address of the global information table 447 // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since 448 // current hardware only allows a 16 bit value. 449 unsigned GITPtrHigh; 450 451 unsigned HighBitsOf32BitAddress; 452 453 // Current recorded maximum possible occupancy. 454 unsigned Occupancy; 455 456 mutable std::optional<bool> UsesAGPRs; 457 458 MCPhysReg getNextUserSGPR() const; 459 460 MCPhysReg getNextSystemSGPR() const; 461 462 public: 463 struct VGPRSpillToAGPR { 464 SmallVector<MCPhysReg, 32> Lanes; 465 bool FullyAllocated = false; 466 bool IsDead = false; 467 }; 468 469 private: 470 // To track VGPR + lane index for each subregister of the SGPR spilled to 471 // frameindex key during SILowerSGPRSpills pass. 472 DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>> SGPRSpillToVGPRLanes; 473 // To track VGPR + lane index for spilling special SGPRs like Frame Pointer 474 // identified during PrologEpilogInserter. 475 DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>> 476 PrologEpilogSGPRSpillToVGPRLanes; 477 unsigned NumVGPRSpillLanes = 0; 478 unsigned NumVGPRPrologEpilogSpillLanes = 0; 479 SmallVector<Register, 2> SpillVGPRs; 480 using WWMSpillsMap = MapVector<Register, int>; 481 // To track the registers used in instructions that can potentially modify the 482 // inactive lanes. The WWM instructions and the writelane instructions for 483 // spilling SGPRs to VGPRs fall under such category of operations. The VGPRs 484 // modified by them should be spilled/restored at function prolog/epilog to 485 // avoid any undesired outcome. Each entry in this map holds a pair of values, 486 // the VGPR and its stack slot index. 487 WWMSpillsMap WWMSpills; 488 489 using ReservedRegSet = SmallSetVector<Register, 8>; 490 // To track the VGPRs reserved for WWM instructions. They get stack slots 491 // later during PrologEpilogInserter and get added into the superset WWMSpills 492 // for actual spilling. A separate set makes the register reserved part and 493 // the serialization easier. 494 ReservedRegSet WWMReservedRegs; 495 496 using PrologEpilogSGPRSpillsMap = 497 DenseMap<Register, PrologEpilogSGPRSaveRestoreInfo>; 498 // To track the SGPR spill method used for a CSR SGPR register during 499 // frame lowering. Even though the SGPR spills are handled during 500 // SILowerSGPRSpills pass, some special handling needed later during the 501 // PrologEpilogInserter. 502 PrologEpilogSGPRSpillsMap PrologEpilogSGPRSpills; 503 504 DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills; 505 506 // AGPRs used for VGPR spills. 507 SmallVector<MCPhysReg, 32> SpillAGPR; 508 509 // VGPRs used for AGPR spills. 510 SmallVector<MCPhysReg, 32> SpillVGPR; 511 512 // Emergency stack slot. Sometimes, we create this before finalizing the stack 513 // frame, so save it here and add it to the RegScavenger later. 514 std::optional<int> ScavengeFI; 515 516 private: 517 Register VGPRForAGPRCopy; 518 519 bool allocateVGPRForSGPRSpills(MachineFunction &MF, int FI, 520 unsigned LaneIndex); 521 bool allocateVGPRForPrologEpilogSGPRSpills(MachineFunction &MF, int FI, 522 unsigned LaneIndex); 523 524 public: 525 Register getVGPRForAGPRCopy() const { 526 return VGPRForAGPRCopy; 527 } 528 529 void setVGPRForAGPRCopy(Register NewVGPRForAGPRCopy) { 530 VGPRForAGPRCopy = NewVGPRForAGPRCopy; 531 } 532 533 bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg); 534 535 public: 536 SIMachineFunctionInfo(const MachineFunction &MF); 537 SIMachineFunctionInfo(const SIMachineFunctionInfo &MFI) = default; 538 539 MachineFunctionInfo * 540 clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, 541 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) 542 const override; 543 544 bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI, 545 const MachineFunction &MF, 546 PerFunctionMIParsingState &PFS, 547 SMDiagnostic &Error, SMRange &SourceRange); 548 549 void reserveWWMRegister(Register Reg) { WWMReservedRegs.insert(Reg); } 550 551 AMDGPU::SIModeRegisterDefaults getMode() const { 552 return Mode; 553 } 554 555 ArrayRef<SIRegisterInfo::SpilledReg> 556 getSGPRSpillToVGPRLanes(int FrameIndex) const { 557 auto I = SGPRSpillToVGPRLanes.find(FrameIndex); 558 return (I == SGPRSpillToVGPRLanes.end()) 559 ? ArrayRef<SIRegisterInfo::SpilledReg>() 560 : makeArrayRef(I->second); 561 } 562 563 ArrayRef<Register> getSGPRSpillVGPRs() const { return SpillVGPRs; } 564 const WWMSpillsMap &getWWMSpills() const { return WWMSpills; } 565 const ReservedRegSet &getWWMReservedRegs() const { return WWMReservedRegs; } 566 567 const PrologEpilogSGPRSpillsMap &getPrologEpilogSGPRSpills() const { 568 return PrologEpilogSGPRSpills; 569 } 570 571 void addToPrologEpilogSGPRSpills(Register Reg, 572 PrologEpilogSGPRSaveRestoreInfo SI) { 573 PrologEpilogSGPRSpills.insert(std::make_pair(Reg, SI)); 574 } 575 576 // Check if an entry created for \p Reg in PrologEpilogSGPRSpills. Return true 577 // on success and false otherwise. 578 bool hasPrologEpilogSGPRSpillEntry(Register Reg) const { 579 return PrologEpilogSGPRSpills.find(Reg) != PrologEpilogSGPRSpills.end(); 580 } 581 582 // Get the scratch SGPR if allocated to save/restore \p Reg. 583 Register getScratchSGPRCopyDstReg(Register Reg) const { 584 auto I = PrologEpilogSGPRSpills.find(Reg); 585 if (I != PrologEpilogSGPRSpills.end() && 586 I->second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR) 587 return I->second.getReg(); 588 589 return AMDGPU::NoRegister; 590 } 591 592 // Get all scratch SGPRs allocated to copy/restore the SGPR spills. 593 void getAllScratchSGPRCopyDstRegs(SmallVectorImpl<Register> &Regs) const { 594 for (const auto &SI : PrologEpilogSGPRSpills) { 595 if (SI.second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR) 596 Regs.push_back(SI.second.getReg()); 597 } 598 } 599 600 // Check if \p FI is allocated for any SGPR spill to a VGPR lane during PEI. 601 bool checkIndexInPrologEpilogSGPRSpills(int FI) const { 602 return find_if(PrologEpilogSGPRSpills, 603 [FI](const std::pair<Register, 604 PrologEpilogSGPRSaveRestoreInfo> &SI) { 605 return SI.second.getKind() == 606 SGPRSaveKind::SPILL_TO_VGPR_LANE && 607 SI.second.getIndex() == FI; 608 }) != PrologEpilogSGPRSpills.end(); 609 } 610 611 ArrayRef<SIRegisterInfo::SpilledReg> 612 getPrologEpilogSGPRSpillToVGPRLanes(int FrameIndex) const { 613 auto I = PrologEpilogSGPRSpillToVGPRLanes.find(FrameIndex); 614 return (I == PrologEpilogSGPRSpillToVGPRLanes.end()) 615 ? ArrayRef<SIRegisterInfo::SpilledReg>() 616 : makeArrayRef(I->second); 617 } 618 619 void allocateWWMSpill(MachineFunction &MF, Register VGPR, uint64_t Size = 4, 620 Align Alignment = Align(4)); 621 622 ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const { 623 return SpillAGPR; 624 } 625 626 ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const { 627 return SpillVGPR; 628 } 629 630 MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const { 631 auto I = VGPRToAGPRSpills.find(FrameIndex); 632 return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister 633 : I->second.Lanes[Lane]; 634 } 635 636 void setVGPRToAGPRSpillDead(int FrameIndex) { 637 auto I = VGPRToAGPRSpills.find(FrameIndex); 638 if (I != VGPRToAGPRSpills.end()) 639 I->second.IsDead = true; 640 } 641 642 bool allocateSGPRSpillToVGPRLane(MachineFunction &MF, int FI, 643 bool IsPrologEpilog = false); 644 bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR); 645 646 /// If \p ResetSGPRSpillStackIDs is true, reset the stack ID from sgpr-spill 647 /// to the default stack. 648 bool removeDeadFrameIndices(MachineFrameInfo &MFI, 649 bool ResetSGPRSpillStackIDs); 650 651 int getScavengeFI(MachineFrameInfo &MFI, const SIRegisterInfo &TRI); 652 std::optional<int> getOptionalScavengeFI() const { return ScavengeFI; } 653 654 unsigned getBytesInStackArgArea() const { 655 return BytesInStackArgArea; 656 } 657 658 void setBytesInStackArgArea(unsigned Bytes) { 659 BytesInStackArgArea = Bytes; 660 } 661 662 // Add user SGPRs. 663 Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI); 664 Register addDispatchPtr(const SIRegisterInfo &TRI); 665 Register addQueuePtr(const SIRegisterInfo &TRI); 666 Register addKernargSegmentPtr(const SIRegisterInfo &TRI); 667 Register addDispatchID(const SIRegisterInfo &TRI); 668 Register addFlatScratchInit(const SIRegisterInfo &TRI); 669 Register addImplicitBufferPtr(const SIRegisterInfo &TRI); 670 Register addLDSKernelId(); 671 672 /// Increment user SGPRs used for padding the argument list only. 673 Register addReservedUserSGPR() { 674 Register Next = getNextUserSGPR(); 675 ++NumUserSGPRs; 676 return Next; 677 } 678 679 // Add system SGPRs. 680 Register addWorkGroupIDX() { 681 ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR()); 682 NumSystemSGPRs += 1; 683 return ArgInfo.WorkGroupIDX.getRegister(); 684 } 685 686 Register addWorkGroupIDY() { 687 ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR()); 688 NumSystemSGPRs += 1; 689 return ArgInfo.WorkGroupIDY.getRegister(); 690 } 691 692 Register addWorkGroupIDZ() { 693 ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR()); 694 NumSystemSGPRs += 1; 695 return ArgInfo.WorkGroupIDZ.getRegister(); 696 } 697 698 Register addWorkGroupInfo() { 699 ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR()); 700 NumSystemSGPRs += 1; 701 return ArgInfo.WorkGroupInfo.getRegister(); 702 } 703 704 // Add special VGPR inputs 705 void setWorkItemIDX(ArgDescriptor Arg) { 706 ArgInfo.WorkItemIDX = Arg; 707 } 708 709 void setWorkItemIDY(ArgDescriptor Arg) { 710 ArgInfo.WorkItemIDY = Arg; 711 } 712 713 void setWorkItemIDZ(ArgDescriptor Arg) { 714 ArgInfo.WorkItemIDZ = Arg; 715 } 716 717 Register addPrivateSegmentWaveByteOffset() { 718 ArgInfo.PrivateSegmentWaveByteOffset 719 = ArgDescriptor::createRegister(getNextSystemSGPR()); 720 NumSystemSGPRs += 1; 721 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 722 } 723 724 void setPrivateSegmentWaveByteOffset(Register Reg) { 725 ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg); 726 } 727 728 bool hasPrivateSegmentBuffer() const { 729 return PrivateSegmentBuffer; 730 } 731 732 bool hasDispatchPtr() const { 733 return DispatchPtr; 734 } 735 736 bool hasQueuePtr() const { 737 return QueuePtr; 738 } 739 740 bool hasKernargSegmentPtr() const { 741 return KernargSegmentPtr; 742 } 743 744 bool hasDispatchID() const { 745 return DispatchID; 746 } 747 748 bool hasFlatScratchInit() const { 749 return FlatScratchInit; 750 } 751 752 bool hasWorkGroupIDX() const { 753 return WorkGroupIDX; 754 } 755 756 bool hasWorkGroupIDY() const { 757 return WorkGroupIDY; 758 } 759 760 bool hasWorkGroupIDZ() const { 761 return WorkGroupIDZ; 762 } 763 764 bool hasWorkGroupInfo() const { 765 return WorkGroupInfo; 766 } 767 768 bool hasLDSKernelId() const { return LDSKernelId; } 769 770 bool hasPrivateSegmentWaveByteOffset() const { 771 return PrivateSegmentWaveByteOffset; 772 } 773 774 bool hasWorkItemIDX() const { 775 return WorkItemIDX; 776 } 777 778 bool hasWorkItemIDY() const { 779 return WorkItemIDY; 780 } 781 782 bool hasWorkItemIDZ() const { 783 return WorkItemIDZ; 784 } 785 786 bool hasImplicitArgPtr() const { 787 return ImplicitArgPtr; 788 } 789 790 bool hasImplicitBufferPtr() const { 791 return ImplicitBufferPtr; 792 } 793 794 AMDGPUFunctionArgInfo &getArgInfo() { 795 return ArgInfo; 796 } 797 798 const AMDGPUFunctionArgInfo &getArgInfo() const { 799 return ArgInfo; 800 } 801 802 std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT> 803 getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 804 return ArgInfo.getPreloadedValue(Value); 805 } 806 807 MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 808 auto Arg = std::get<0>(ArgInfo.getPreloadedValue(Value)); 809 return Arg ? Arg->getRegister() : MCRegister(); 810 } 811 812 unsigned getGITPtrHigh() const { 813 return GITPtrHigh; 814 } 815 816 Register getGITPtrLoReg(const MachineFunction &MF) const; 817 818 uint32_t get32BitAddressHighBits() const { 819 return HighBitsOf32BitAddress; 820 } 821 822 unsigned getNumUserSGPRs() const { 823 return NumUserSGPRs; 824 } 825 826 unsigned getNumPreloadedSGPRs() const { 827 return NumUserSGPRs + NumSystemSGPRs; 828 } 829 830 Register getPrivateSegmentWaveByteOffsetSystemSGPR() const { 831 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 832 } 833 834 /// Returns the physical register reserved for use as the resource 835 /// descriptor for scratch accesses. 836 Register getScratchRSrcReg() const { 837 return ScratchRSrcReg; 838 } 839 840 void setScratchRSrcReg(Register Reg) { 841 assert(Reg != 0 && "Should never be unset"); 842 ScratchRSrcReg = Reg; 843 } 844 845 Register getFrameOffsetReg() const { 846 return FrameOffsetReg; 847 } 848 849 void setFrameOffsetReg(Register Reg) { 850 assert(Reg != 0 && "Should never be unset"); 851 FrameOffsetReg = Reg; 852 } 853 854 void setStackPtrOffsetReg(Register Reg) { 855 assert(Reg != 0 && "Should never be unset"); 856 StackPtrOffsetReg = Reg; 857 } 858 859 // Note the unset value for this is AMDGPU::SP_REG rather than 860 // NoRegister. This is mostly a workaround for MIR tests where state that 861 // can't be directly computed from the function is not preserved in serialized 862 // MIR. 863 Register getStackPtrOffsetReg() const { 864 return StackPtrOffsetReg; 865 } 866 867 Register getQueuePtrUserSGPR() const { 868 return ArgInfo.QueuePtr.getRegister(); 869 } 870 871 Register getImplicitBufferPtrUserSGPR() const { 872 return ArgInfo.ImplicitBufferPtr.getRegister(); 873 } 874 875 bool hasSpilledSGPRs() const { 876 return HasSpilledSGPRs; 877 } 878 879 void setHasSpilledSGPRs(bool Spill = true) { 880 HasSpilledSGPRs = Spill; 881 } 882 883 bool hasSpilledVGPRs() const { 884 return HasSpilledVGPRs; 885 } 886 887 void setHasSpilledVGPRs(bool Spill = true) { 888 HasSpilledVGPRs = Spill; 889 } 890 891 bool hasNonSpillStackObjects() const { 892 return HasNonSpillStackObjects; 893 } 894 895 void setHasNonSpillStackObjects(bool StackObject = true) { 896 HasNonSpillStackObjects = StackObject; 897 } 898 899 bool isStackRealigned() const { 900 return IsStackRealigned; 901 } 902 903 void setIsStackRealigned(bool Realigned = true) { 904 IsStackRealigned = Realigned; 905 } 906 907 unsigned getNumSpilledSGPRs() const { 908 return NumSpilledSGPRs; 909 } 910 911 unsigned getNumSpilledVGPRs() const { 912 return NumSpilledVGPRs; 913 } 914 915 void addToSpilledSGPRs(unsigned num) { 916 NumSpilledSGPRs += num; 917 } 918 919 void addToSpilledVGPRs(unsigned num) { 920 NumSpilledVGPRs += num; 921 } 922 923 unsigned getPSInputAddr() const { 924 return PSInputAddr; 925 } 926 927 unsigned getPSInputEnable() const { 928 return PSInputEnable; 929 } 930 931 bool isPSInputAllocated(unsigned Index) const { 932 return PSInputAddr & (1 << Index); 933 } 934 935 void markPSInputAllocated(unsigned Index) { 936 PSInputAddr |= 1 << Index; 937 } 938 939 void markPSInputEnabled(unsigned Index) { 940 PSInputEnable |= 1 << Index; 941 } 942 943 bool returnsVoid() const { 944 return ReturnsVoid; 945 } 946 947 void setIfReturnsVoid(bool Value) { 948 ReturnsVoid = Value; 949 } 950 951 /// \returns A pair of default/requested minimum/maximum flat work group sizes 952 /// for this function. 953 std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const { 954 return FlatWorkGroupSizes; 955 } 956 957 /// \returns Default/requested minimum flat work group size for this function. 958 unsigned getMinFlatWorkGroupSize() const { 959 return FlatWorkGroupSizes.first; 960 } 961 962 /// \returns Default/requested maximum flat work group size for this function. 963 unsigned getMaxFlatWorkGroupSize() const { 964 return FlatWorkGroupSizes.second; 965 } 966 967 /// \returns A pair of default/requested minimum/maximum number of waves per 968 /// execution unit. 969 std::pair<unsigned, unsigned> getWavesPerEU() const { 970 return WavesPerEU; 971 } 972 973 /// \returns Default/requested minimum number of waves per execution unit. 974 unsigned getMinWavesPerEU() const { 975 return WavesPerEU.first; 976 } 977 978 /// \returns Default/requested maximum number of waves per execution unit. 979 unsigned getMaxWavesPerEU() const { 980 return WavesPerEU.second; 981 } 982 983 /// \returns SGPR used for \p Dim's work group ID. 984 Register getWorkGroupIDSGPR(unsigned Dim) const { 985 switch (Dim) { 986 case 0: 987 assert(hasWorkGroupIDX()); 988 return ArgInfo.WorkGroupIDX.getRegister(); 989 case 1: 990 assert(hasWorkGroupIDY()); 991 return ArgInfo.WorkGroupIDY.getRegister(); 992 case 2: 993 assert(hasWorkGroupIDZ()); 994 return ArgInfo.WorkGroupIDZ.getRegister(); 995 } 996 llvm_unreachable("unexpected dimension"); 997 } 998 999 const AMDGPUGWSResourcePseudoSourceValue * 1000 getGWSPSV(const AMDGPUTargetMachine &TM) { 1001 return &GWSResourcePSV; 1002 } 1003 1004 unsigned getOccupancy() const { 1005 return Occupancy; 1006 } 1007 1008 unsigned getMinAllowedOccupancy() const { 1009 if (!isMemoryBound() && !needsWaveLimiter()) 1010 return Occupancy; 1011 return (Occupancy < 4) ? Occupancy : 4; 1012 } 1013 1014 void limitOccupancy(const MachineFunction &MF); 1015 1016 void limitOccupancy(unsigned Limit) { 1017 if (Occupancy > Limit) 1018 Occupancy = Limit; 1019 } 1020 1021 void increaseOccupancy(const MachineFunction &MF, unsigned Limit) { 1022 if (Occupancy < Limit) 1023 Occupancy = Limit; 1024 limitOccupancy(MF); 1025 } 1026 1027 bool mayNeedAGPRs() const { 1028 return MayNeedAGPRs; 1029 } 1030 1031 // \returns true if a function has a use of AGPRs via inline asm or 1032 // has a call which may use it. 1033 bool mayUseAGPRs(const Function &F) const; 1034 1035 // \returns true if a function needs or may need AGPRs. 1036 bool usesAGPRs(const MachineFunction &MF) const; 1037 }; 1038 1039 } // end namespace llvm 1040 1041 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H 1042