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 "GCNSubtarget.h" 20 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 21 #include "SIInstrInfo.h" 22 #include "SIModeRegisterDefaults.h" 23 #include "llvm/ADT/SetVector.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/CodeGen/MIRYamlMapping.h" 26 #include "llvm/CodeGen/PseudoSourceValue.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <optional> 29 30 namespace llvm { 31 32 class MachineFrameInfo; 33 class MachineFunction; 34 class SIMachineFunctionInfo; 35 class SIRegisterInfo; 36 class TargetRegisterClass; 37 38 class AMDGPUPseudoSourceValue : public PseudoSourceValue { 39 public: 40 enum AMDGPUPSVKind : unsigned { 41 PSVImage = PseudoSourceValue::TargetCustom, 42 GWSResource 43 }; 44 45 protected: 46 AMDGPUPseudoSourceValue(unsigned Kind, const AMDGPUTargetMachine &TM) 47 : PseudoSourceValue(Kind, TM) {} 48 49 public: 50 bool isConstant(const MachineFrameInfo *) const override { 51 // This should probably be true for most images, but we will start by being 52 // conservative. 53 return false; 54 } 55 56 bool isAliased(const MachineFrameInfo *) const override { 57 return true; 58 } 59 60 bool mayAlias(const MachineFrameInfo *) const override { 61 return true; 62 } 63 }; 64 65 class AMDGPUGWSResourcePseudoSourceValue final : public AMDGPUPseudoSourceValue { 66 public: 67 explicit AMDGPUGWSResourcePseudoSourceValue(const AMDGPUTargetMachine &TM) 68 : AMDGPUPseudoSourceValue(GWSResource, TM) {} 69 70 static bool classof(const PseudoSourceValue *V) { 71 return V->kind() == GWSResource; 72 } 73 74 // These are inaccessible memory from IR. 75 bool isAliased(const MachineFrameInfo *) const override { 76 return false; 77 } 78 79 // These are inaccessible memory from IR. 80 bool mayAlias(const MachineFrameInfo *) const override { 81 return false; 82 } 83 84 void printCustom(raw_ostream &OS) const override { 85 OS << "GWSResource"; 86 } 87 }; 88 89 namespace yaml { 90 91 struct SIArgument { 92 bool IsRegister; 93 union { 94 StringValue RegisterName; 95 unsigned StackOffset; 96 }; 97 std::optional<unsigned> Mask; 98 99 // Default constructor, which creates a stack argument. 100 SIArgument() : IsRegister(false), StackOffset(0) {} 101 SIArgument(const SIArgument &Other) { 102 IsRegister = Other.IsRegister; 103 if (IsRegister) 104 new (&RegisterName) StringValue(Other.RegisterName); 105 else 106 StackOffset = Other.StackOffset; 107 Mask = Other.Mask; 108 } 109 SIArgument &operator=(const SIArgument &Other) { 110 // Default-construct or destruct the old RegisterName in case of switching 111 // union members 112 if (IsRegister != Other.IsRegister) { 113 if (Other.IsRegister) 114 new (&RegisterName) StringValue(); 115 else 116 RegisterName.~StringValue(); 117 } 118 IsRegister = Other.IsRegister; 119 if (IsRegister) 120 RegisterName = Other.RegisterName; 121 else 122 StackOffset = Other.StackOffset; 123 Mask = Other.Mask; 124 return *this; 125 } 126 ~SIArgument() { 127 if (IsRegister) 128 RegisterName.~StringValue(); 129 } 130 131 // Helper to create a register or stack argument. 132 static inline SIArgument createArgument(bool IsReg) { 133 if (IsReg) 134 return SIArgument(IsReg); 135 return SIArgument(); 136 } 137 138 private: 139 // Construct a register argument. 140 SIArgument(bool) : IsRegister(true), RegisterName() {} 141 }; 142 143 template <> struct MappingTraits<SIArgument> { 144 static void mapping(IO &YamlIO, SIArgument &A) { 145 if (YamlIO.outputting()) { 146 if (A.IsRegister) 147 YamlIO.mapRequired("reg", A.RegisterName); 148 else 149 YamlIO.mapRequired("offset", A.StackOffset); 150 } else { 151 auto Keys = YamlIO.keys(); 152 if (is_contained(Keys, "reg")) { 153 A = SIArgument::createArgument(true); 154 YamlIO.mapRequired("reg", A.RegisterName); 155 } else if (is_contained(Keys, "offset")) 156 YamlIO.mapRequired("offset", A.StackOffset); 157 else 158 YamlIO.setError("missing required key 'reg' or 'offset'"); 159 } 160 YamlIO.mapOptional("mask", A.Mask); 161 } 162 static const bool flow = true; 163 }; 164 165 struct SIArgumentInfo { 166 std::optional<SIArgument> PrivateSegmentBuffer; 167 std::optional<SIArgument> DispatchPtr; 168 std::optional<SIArgument> QueuePtr; 169 std::optional<SIArgument> KernargSegmentPtr; 170 std::optional<SIArgument> DispatchID; 171 std::optional<SIArgument> FlatScratchInit; 172 std::optional<SIArgument> PrivateSegmentSize; 173 174 std::optional<SIArgument> WorkGroupIDX; 175 std::optional<SIArgument> WorkGroupIDY; 176 std::optional<SIArgument> WorkGroupIDZ; 177 std::optional<SIArgument> WorkGroupInfo; 178 std::optional<SIArgument> LDSKernelId; 179 std::optional<SIArgument> PrivateSegmentWaveByteOffset; 180 181 std::optional<SIArgument> ImplicitArgPtr; 182 std::optional<SIArgument> ImplicitBufferPtr; 183 184 std::optional<SIArgument> WorkItemIDX; 185 std::optional<SIArgument> WorkItemIDY; 186 std::optional<SIArgument> WorkItemIDZ; 187 }; 188 189 template <> struct MappingTraits<SIArgumentInfo> { 190 static void mapping(IO &YamlIO, SIArgumentInfo &AI) { 191 YamlIO.mapOptional("privateSegmentBuffer", AI.PrivateSegmentBuffer); 192 YamlIO.mapOptional("dispatchPtr", AI.DispatchPtr); 193 YamlIO.mapOptional("queuePtr", AI.QueuePtr); 194 YamlIO.mapOptional("kernargSegmentPtr", AI.KernargSegmentPtr); 195 YamlIO.mapOptional("dispatchID", AI.DispatchID); 196 YamlIO.mapOptional("flatScratchInit", AI.FlatScratchInit); 197 YamlIO.mapOptional("privateSegmentSize", AI.PrivateSegmentSize); 198 199 YamlIO.mapOptional("workGroupIDX", AI.WorkGroupIDX); 200 YamlIO.mapOptional("workGroupIDY", AI.WorkGroupIDY); 201 YamlIO.mapOptional("workGroupIDZ", AI.WorkGroupIDZ); 202 YamlIO.mapOptional("workGroupInfo", AI.WorkGroupInfo); 203 YamlIO.mapOptional("LDSKernelId", AI.LDSKernelId); 204 YamlIO.mapOptional("privateSegmentWaveByteOffset", 205 AI.PrivateSegmentWaveByteOffset); 206 207 YamlIO.mapOptional("implicitArgPtr", AI.ImplicitArgPtr); 208 YamlIO.mapOptional("implicitBufferPtr", AI.ImplicitBufferPtr); 209 210 YamlIO.mapOptional("workItemIDX", AI.WorkItemIDX); 211 YamlIO.mapOptional("workItemIDY", AI.WorkItemIDY); 212 YamlIO.mapOptional("workItemIDZ", AI.WorkItemIDZ); 213 } 214 }; 215 216 // Default to default mode for default calling convention. 217 struct SIMode { 218 bool IEEE = true; 219 bool DX10Clamp = true; 220 bool FP32InputDenormals = true; 221 bool FP32OutputDenormals = true; 222 bool FP64FP16InputDenormals = true; 223 bool FP64FP16OutputDenormals = true; 224 225 SIMode() = default; 226 227 SIMode(const SIModeRegisterDefaults &Mode) { 228 IEEE = Mode.IEEE; 229 DX10Clamp = Mode.DX10Clamp; 230 FP32InputDenormals = Mode.FP32Denormals.Input != DenormalMode::PreserveSign; 231 FP32OutputDenormals = 232 Mode.FP32Denormals.Output != DenormalMode::PreserveSign; 233 FP64FP16InputDenormals = 234 Mode.FP64FP16Denormals.Input != DenormalMode::PreserveSign; 235 FP64FP16OutputDenormals = 236 Mode.FP64FP16Denormals.Output != DenormalMode::PreserveSign; 237 } 238 239 bool operator ==(const SIMode Other) const { 240 return IEEE == Other.IEEE && 241 DX10Clamp == Other.DX10Clamp && 242 FP32InputDenormals == Other.FP32InputDenormals && 243 FP32OutputDenormals == Other.FP32OutputDenormals && 244 FP64FP16InputDenormals == Other.FP64FP16InputDenormals && 245 FP64FP16OutputDenormals == Other.FP64FP16OutputDenormals; 246 } 247 }; 248 249 template <> struct MappingTraits<SIMode> { 250 static void mapping(IO &YamlIO, SIMode &Mode) { 251 YamlIO.mapOptional("ieee", Mode.IEEE, true); 252 YamlIO.mapOptional("dx10-clamp", Mode.DX10Clamp, true); 253 YamlIO.mapOptional("fp32-input-denormals", Mode.FP32InputDenormals, true); 254 YamlIO.mapOptional("fp32-output-denormals", Mode.FP32OutputDenormals, true); 255 YamlIO.mapOptional("fp64-fp16-input-denormals", Mode.FP64FP16InputDenormals, true); 256 YamlIO.mapOptional("fp64-fp16-output-denormals", Mode.FP64FP16OutputDenormals, true); 257 } 258 }; 259 260 struct SIMachineFunctionInfo final : public yaml::MachineFunctionInfo { 261 uint64_t ExplicitKernArgSize = 0; 262 Align MaxKernArgAlign; 263 uint32_t LDSSize = 0; 264 uint32_t GDSSize = 0; 265 Align DynLDSAlign; 266 bool IsEntryFunction = false; 267 bool IsChainFunction = false; 268 bool NoSignedZerosFPMath = false; 269 bool MemoryBound = false; 270 bool WaveLimiter = false; 271 bool HasSpilledSGPRs = false; 272 bool HasSpilledVGPRs = false; 273 uint32_t HighBitsOf32BitAddress = 0; 274 275 // TODO: 10 may be a better default since it's the maximum. 276 unsigned Occupancy = 0; 277 278 SmallVector<StringValue> WWMReservedRegs; 279 280 StringValue ScratchRSrcReg = "$private_rsrc_reg"; 281 StringValue FrameOffsetReg = "$fp_reg"; 282 StringValue StackPtrOffsetReg = "$sp_reg"; 283 284 unsigned BytesInStackArgArea = 0; 285 bool ReturnsVoid = true; 286 287 std::optional<SIArgumentInfo> ArgInfo; 288 289 unsigned PSInputAddr = 0; 290 unsigned PSInputEnable = 0; 291 292 SIMode Mode; 293 std::optional<FrameIndex> ScavengeFI; 294 StringValue VGPRForAGPRCopy; 295 StringValue SGPRForEXECCopy; 296 StringValue LongBranchReservedReg; 297 298 SIMachineFunctionInfo() = default; 299 SIMachineFunctionInfo(const llvm::SIMachineFunctionInfo &, 300 const TargetRegisterInfo &TRI, 301 const llvm::MachineFunction &MF); 302 303 void mappingImpl(yaml::IO &YamlIO) override; 304 ~SIMachineFunctionInfo() = default; 305 }; 306 307 template <> struct MappingTraits<SIMachineFunctionInfo> { 308 static void mapping(IO &YamlIO, SIMachineFunctionInfo &MFI) { 309 YamlIO.mapOptional("explicitKernArgSize", MFI.ExplicitKernArgSize, 310 UINT64_C(0)); 311 YamlIO.mapOptional("maxKernArgAlign", MFI.MaxKernArgAlign); 312 YamlIO.mapOptional("ldsSize", MFI.LDSSize, 0u); 313 YamlIO.mapOptional("gdsSize", MFI.GDSSize, 0u); 314 YamlIO.mapOptional("dynLDSAlign", MFI.DynLDSAlign, Align()); 315 YamlIO.mapOptional("isEntryFunction", MFI.IsEntryFunction, false); 316 YamlIO.mapOptional("isChainFunction", MFI.IsChainFunction, false); 317 YamlIO.mapOptional("noSignedZerosFPMath", MFI.NoSignedZerosFPMath, false); 318 YamlIO.mapOptional("memoryBound", MFI.MemoryBound, false); 319 YamlIO.mapOptional("waveLimiter", MFI.WaveLimiter, false); 320 YamlIO.mapOptional("hasSpilledSGPRs", MFI.HasSpilledSGPRs, false); 321 YamlIO.mapOptional("hasSpilledVGPRs", MFI.HasSpilledVGPRs, false); 322 YamlIO.mapOptional("scratchRSrcReg", MFI.ScratchRSrcReg, 323 StringValue("$private_rsrc_reg")); 324 YamlIO.mapOptional("frameOffsetReg", MFI.FrameOffsetReg, 325 StringValue("$fp_reg")); 326 YamlIO.mapOptional("stackPtrOffsetReg", MFI.StackPtrOffsetReg, 327 StringValue("$sp_reg")); 328 YamlIO.mapOptional("bytesInStackArgArea", MFI.BytesInStackArgArea, 0u); 329 YamlIO.mapOptional("returnsVoid", MFI.ReturnsVoid, true); 330 YamlIO.mapOptional("argumentInfo", MFI.ArgInfo); 331 YamlIO.mapOptional("psInputAddr", MFI.PSInputAddr, 0u); 332 YamlIO.mapOptional("psInputEnable", MFI.PSInputEnable, 0u); 333 YamlIO.mapOptional("mode", MFI.Mode, SIMode()); 334 YamlIO.mapOptional("highBitsOf32BitAddress", 335 MFI.HighBitsOf32BitAddress, 0u); 336 YamlIO.mapOptional("occupancy", MFI.Occupancy, 0); 337 YamlIO.mapOptional("wwmReservedRegs", MFI.WWMReservedRegs); 338 YamlIO.mapOptional("scavengeFI", MFI.ScavengeFI); 339 YamlIO.mapOptional("vgprForAGPRCopy", MFI.VGPRForAGPRCopy, 340 StringValue()); // Don't print out when it's empty. 341 YamlIO.mapOptional("sgprForEXECCopy", MFI.SGPRForEXECCopy, 342 StringValue()); // Don't print out when it's empty. 343 YamlIO.mapOptional("longBranchReservedReg", MFI.LongBranchReservedReg, 344 StringValue()); 345 } 346 }; 347 348 } // end namespace yaml 349 350 // A CSR SGPR value can be preserved inside a callee using one of the following 351 // methods. 352 // 1. Copy to an unused scratch SGPR. 353 // 2. Spill to a VGPR lane. 354 // 3. Spill to memory via. a scratch VGPR. 355 // class PrologEpilogSGPRSaveRestoreInfo represents the save/restore method used 356 // for an SGPR at function prolog/epilog. 357 enum class SGPRSaveKind : uint8_t { 358 COPY_TO_SCRATCH_SGPR, 359 SPILL_TO_VGPR_LANE, 360 SPILL_TO_MEM 361 }; 362 363 class PrologEpilogSGPRSaveRestoreInfo { 364 SGPRSaveKind Kind; 365 union { 366 int Index; 367 Register Reg; 368 }; 369 370 public: 371 PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, int I) : Kind(K), Index(I) {} 372 PrologEpilogSGPRSaveRestoreInfo(SGPRSaveKind K, Register R) 373 : Kind(K), Reg(R) {} 374 Register getReg() const { return Reg; } 375 int getIndex() const { return Index; } 376 SGPRSaveKind getKind() const { return Kind; } 377 }; 378 379 /// This class keeps track of the SPI_SP_INPUT_ADDR config register, which 380 /// tells the hardware which interpolation parameters to load. 381 class SIMachineFunctionInfo final : public AMDGPUMachineFunction, 382 private MachineRegisterInfo::Delegate { 383 friend class GCNTargetMachine; 384 385 // State of MODE register, assumed FP mode. 386 SIModeRegisterDefaults Mode; 387 388 // Registers that may be reserved for spilling purposes. These may be the same 389 // as the input registers. 390 Register ScratchRSrcReg = AMDGPU::PRIVATE_RSRC_REG; 391 392 // This is the unswizzled offset from the current dispatch's scratch wave 393 // base to the beginning of the current function's frame. 394 Register FrameOffsetReg = AMDGPU::FP_REG; 395 396 // This is an ABI register used in the non-entry calling convention to 397 // communicate the unswizzled offset from the current dispatch's scratch wave 398 // base to the beginning of the new function's frame. 399 Register StackPtrOffsetReg = AMDGPU::SP_REG; 400 401 // Registers that may be reserved when RA doesn't allocate enough 402 // registers to plan for the case where an indirect branch ends up 403 // being needed during branch relaxation. 404 Register LongBranchReservedReg; 405 406 AMDGPUFunctionArgInfo ArgInfo; 407 408 // Graphics info. 409 unsigned PSInputAddr = 0; 410 unsigned PSInputEnable = 0; 411 412 /// Number of bytes of arguments this function has on the stack. If the callee 413 /// is expected to restore the argument stack this should be a multiple of 16, 414 /// all usable during a tail call. 415 /// 416 /// The alternative would forbid tail call optimisation in some cases: if we 417 /// want to transfer control from a function with 8-bytes of stack-argument 418 /// space to a function with 16-bytes then misalignment of this value would 419 /// make a stack adjustment necessary, which could not be undone by the 420 /// callee. 421 unsigned BytesInStackArgArea = 0; 422 423 bool ReturnsVoid = true; 424 425 // A pair of default/requested minimum/maximum flat work group sizes. 426 // Minimum - first, maximum - second. 427 std::pair<unsigned, unsigned> FlatWorkGroupSizes = {0, 0}; 428 429 // A pair of default/requested minimum/maximum number of waves per execution 430 // unit. Minimum - first, maximum - second. 431 std::pair<unsigned, unsigned> WavesPerEU = {0, 0}; 432 433 const AMDGPUGWSResourcePseudoSourceValue GWSResourcePSV; 434 435 // Default/requested number of work groups for the function. 436 SmallVector<unsigned> MaxNumWorkGroups = {0, 0, 0}; 437 438 private: 439 unsigned NumUserSGPRs = 0; 440 unsigned NumSystemSGPRs = 0; 441 442 bool HasSpilledSGPRs = false; 443 bool HasSpilledVGPRs = false; 444 bool HasNonSpillStackObjects = false; 445 bool IsStackRealigned = false; 446 447 unsigned NumSpilledSGPRs = 0; 448 unsigned NumSpilledVGPRs = 0; 449 450 // Tracks information about user SGPRs that will be setup by hardware which 451 // will apply to all wavefronts of the grid. 452 GCNUserSGPRUsageInfo UserSGPRInfo; 453 454 // Feature bits required for inputs passed in system SGPRs. 455 bool WorkGroupIDX : 1; // Always initialized. 456 bool WorkGroupIDY : 1; 457 bool WorkGroupIDZ : 1; 458 bool WorkGroupInfo : 1; 459 bool LDSKernelId : 1; 460 bool PrivateSegmentWaveByteOffset : 1; 461 462 bool WorkItemIDX : 1; // Always initialized. 463 bool WorkItemIDY : 1; 464 bool WorkItemIDZ : 1; 465 466 // Pointer to where the ABI inserts special kernel arguments separate from the 467 // user arguments. This is an offset from the KernargSegmentPtr. 468 bool ImplicitArgPtr : 1; 469 470 bool MayNeedAGPRs : 1; 471 472 // The hard-wired high half of the address of the global information table 473 // for AMDPAL OS type. 0xffffffff represents no hard-wired high half, since 474 // current hardware only allows a 16 bit value. 475 unsigned GITPtrHigh; 476 477 unsigned HighBitsOf32BitAddress; 478 479 // Flags associated with the virtual registers. 480 IndexedMap<uint8_t, VirtReg2IndexFunctor> VRegFlags; 481 482 // Current recorded maximum possible occupancy. 483 unsigned Occupancy; 484 485 mutable std::optional<bool> UsesAGPRs; 486 487 MCPhysReg getNextUserSGPR() const; 488 489 MCPhysReg getNextSystemSGPR() const; 490 491 // MachineRegisterInfo callback functions to notify events. 492 void MRI_NoteNewVirtualRegister(Register Reg) override; 493 void MRI_NoteCloneVirtualRegister(Register NewReg, Register SrcReg) override; 494 495 public: 496 struct VGPRSpillToAGPR { 497 SmallVector<MCPhysReg, 32> Lanes; 498 bool FullyAllocated = false; 499 bool IsDead = false; 500 }; 501 502 private: 503 // To track virtual VGPR + lane index for each subregister of the SGPR spilled 504 // to frameindex key during SILowerSGPRSpills pass. 505 DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>> 506 SGPRSpillsToVirtualVGPRLanes; 507 // To track physical VGPR + lane index for CSR SGPR spills and special SGPRs 508 // like Frame Pointer identified during PrologEpilogInserter. 509 DenseMap<int, std::vector<SIRegisterInfo::SpilledReg>> 510 SGPRSpillsToPhysicalVGPRLanes; 511 unsigned NumVirtualVGPRSpillLanes = 0; 512 unsigned NumPhysicalVGPRSpillLanes = 0; 513 SmallVector<Register, 2> SpillVGPRs; 514 SmallVector<Register, 2> SpillPhysVGPRs; 515 using WWMSpillsMap = MapVector<Register, int>; 516 // To track the registers used in instructions that can potentially modify the 517 // inactive lanes. The WWM instructions and the writelane instructions for 518 // spilling SGPRs to VGPRs fall under such category of operations. The VGPRs 519 // modified by them should be spilled/restored at function prolog/epilog to 520 // avoid any undesired outcome. Each entry in this map holds a pair of values, 521 // the VGPR and its stack slot index. 522 WWMSpillsMap WWMSpills; 523 524 using ReservedRegSet = SmallSetVector<Register, 8>; 525 // To track the VGPRs reserved for WWM instructions. They get stack slots 526 // later during PrologEpilogInserter and get added into the superset WWMSpills 527 // for actual spilling. A separate set makes the register reserved part and 528 // the serialization easier. 529 ReservedRegSet WWMReservedRegs; 530 531 using PrologEpilogSGPRSpill = 532 std::pair<Register, PrologEpilogSGPRSaveRestoreInfo>; 533 // To track the SGPR spill method used for a CSR SGPR register during 534 // frame lowering. Even though the SGPR spills are handled during 535 // SILowerSGPRSpills pass, some special handling needed later during the 536 // PrologEpilogInserter. 537 SmallVector<PrologEpilogSGPRSpill, 3> PrologEpilogSGPRSpills; 538 539 // To save/restore EXEC MASK around WWM spills and copies. 540 Register SGPRForEXECCopy; 541 542 DenseMap<int, VGPRSpillToAGPR> VGPRToAGPRSpills; 543 544 // AGPRs used for VGPR spills. 545 SmallVector<MCPhysReg, 32> SpillAGPR; 546 547 // VGPRs used for AGPR spills. 548 SmallVector<MCPhysReg, 32> SpillVGPR; 549 550 // Emergency stack slot. Sometimes, we create this before finalizing the stack 551 // frame, so save it here and add it to the RegScavenger later. 552 std::optional<int> ScavengeFI; 553 554 private: 555 Register VGPRForAGPRCopy; 556 557 bool allocateVirtualVGPRForSGPRSpills(MachineFunction &MF, int FI, 558 unsigned LaneIndex); 559 bool allocatePhysicalVGPRForSGPRSpills(MachineFunction &MF, int FI, 560 unsigned LaneIndex, 561 bool IsPrologEpilog); 562 563 public: 564 Register getVGPRForAGPRCopy() const { 565 return VGPRForAGPRCopy; 566 } 567 568 void setVGPRForAGPRCopy(Register NewVGPRForAGPRCopy) { 569 VGPRForAGPRCopy = NewVGPRForAGPRCopy; 570 } 571 572 bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg) const; 573 574 public: 575 SIMachineFunctionInfo(const SIMachineFunctionInfo &MFI) = default; 576 SIMachineFunctionInfo(const Function &F, const GCNSubtarget *STI); 577 578 MachineFunctionInfo * 579 clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, 580 const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB) 581 const override; 582 583 bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI, 584 const MachineFunction &MF, 585 PerFunctionMIParsingState &PFS, 586 SMDiagnostic &Error, SMRange &SourceRange); 587 588 void reserveWWMRegister(Register Reg) { WWMReservedRegs.insert(Reg); } 589 590 SIModeRegisterDefaults getMode() const { return Mode; } 591 592 ArrayRef<SIRegisterInfo::SpilledReg> 593 getSGPRSpillToVirtualVGPRLanes(int FrameIndex) const { 594 auto I = SGPRSpillsToVirtualVGPRLanes.find(FrameIndex); 595 return (I == SGPRSpillsToVirtualVGPRLanes.end()) 596 ? ArrayRef<SIRegisterInfo::SpilledReg>() 597 : ArrayRef(I->second); 598 } 599 600 ArrayRef<Register> getSGPRSpillVGPRs() const { return SpillVGPRs; } 601 602 const WWMSpillsMap &getWWMSpills() const { return WWMSpills; } 603 const ReservedRegSet &getWWMReservedRegs() const { return WWMReservedRegs; } 604 605 ArrayRef<PrologEpilogSGPRSpill> getPrologEpilogSGPRSpills() const { 606 assert(is_sorted(PrologEpilogSGPRSpills, llvm::less_first())); 607 return PrologEpilogSGPRSpills; 608 } 609 610 GCNUserSGPRUsageInfo &getUserSGPRInfo() { return UserSGPRInfo; } 611 612 const GCNUserSGPRUsageInfo &getUserSGPRInfo() const { return UserSGPRInfo; } 613 614 void addToPrologEpilogSGPRSpills(Register Reg, 615 PrologEpilogSGPRSaveRestoreInfo SI) { 616 assert(!hasPrologEpilogSGPRSpillEntry(Reg)); 617 618 // Insert a new entry in the right place to keep the vector in sorted order. 619 // This should be cheap since the vector is expected to be very short. 620 PrologEpilogSGPRSpills.insert( 621 upper_bound( 622 PrologEpilogSGPRSpills, Reg, 623 [](const auto &LHS, const auto &RHS) { return LHS < RHS.first; }), 624 std::make_pair(Reg, SI)); 625 } 626 627 // Check if an entry created for \p Reg in PrologEpilogSGPRSpills. Return true 628 // on success and false otherwise. 629 bool hasPrologEpilogSGPRSpillEntry(Register Reg) const { 630 auto I = find_if(PrologEpilogSGPRSpills, 631 [&Reg](const auto &Spill) { return Spill.first == Reg; }); 632 return I != PrologEpilogSGPRSpills.end(); 633 } 634 635 // Get the scratch SGPR if allocated to save/restore \p Reg. 636 Register getScratchSGPRCopyDstReg(Register Reg) const { 637 auto I = find_if(PrologEpilogSGPRSpills, 638 [&Reg](const auto &Spill) { return Spill.first == Reg; }); 639 if (I != PrologEpilogSGPRSpills.end() && 640 I->second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR) 641 return I->second.getReg(); 642 643 return AMDGPU::NoRegister; 644 } 645 646 // Get all scratch SGPRs allocated to copy/restore the SGPR spills. 647 void getAllScratchSGPRCopyDstRegs(SmallVectorImpl<Register> &Regs) const { 648 for (const auto &SI : PrologEpilogSGPRSpills) { 649 if (SI.second.getKind() == SGPRSaveKind::COPY_TO_SCRATCH_SGPR) 650 Regs.push_back(SI.second.getReg()); 651 } 652 } 653 654 // Check if \p FI is allocated for any SGPR spill to a VGPR lane during PEI. 655 bool checkIndexInPrologEpilogSGPRSpills(int FI) const { 656 return find_if(PrologEpilogSGPRSpills, 657 [FI](const std::pair<Register, 658 PrologEpilogSGPRSaveRestoreInfo> &SI) { 659 return SI.second.getKind() == 660 SGPRSaveKind::SPILL_TO_VGPR_LANE && 661 SI.second.getIndex() == FI; 662 }) != PrologEpilogSGPRSpills.end(); 663 } 664 665 const PrologEpilogSGPRSaveRestoreInfo & 666 getPrologEpilogSGPRSaveRestoreInfo(Register Reg) const { 667 auto I = find_if(PrologEpilogSGPRSpills, 668 [&Reg](const auto &Spill) { return Spill.first == Reg; }); 669 assert(I != PrologEpilogSGPRSpills.end()); 670 671 return I->second; 672 } 673 674 ArrayRef<SIRegisterInfo::SpilledReg> 675 getSGPRSpillToPhysicalVGPRLanes(int FrameIndex) const { 676 auto I = SGPRSpillsToPhysicalVGPRLanes.find(FrameIndex); 677 return (I == SGPRSpillsToPhysicalVGPRLanes.end()) 678 ? ArrayRef<SIRegisterInfo::SpilledReg>() 679 : ArrayRef(I->second); 680 } 681 682 void setFlag(Register Reg, uint8_t Flag) { 683 assert(Reg.isVirtual()); 684 if (VRegFlags.inBounds(Reg)) 685 VRegFlags[Reg] |= Flag; 686 } 687 688 bool checkFlag(Register Reg, uint8_t Flag) const { 689 if (Reg.isPhysical()) 690 return false; 691 692 return VRegFlags.inBounds(Reg) && VRegFlags[Reg] & Flag; 693 } 694 695 bool hasVRegFlags() { return VRegFlags.size(); } 696 697 void allocateWWMSpill(MachineFunction &MF, Register VGPR, uint64_t Size = 4, 698 Align Alignment = Align(4)); 699 700 void splitWWMSpillRegisters( 701 MachineFunction &MF, 702 SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs, 703 SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const; 704 705 ArrayRef<MCPhysReg> getAGPRSpillVGPRs() const { 706 return SpillAGPR; 707 } 708 709 Register getSGPRForEXECCopy() const { return SGPRForEXECCopy; } 710 711 void setSGPRForEXECCopy(Register Reg) { SGPRForEXECCopy = Reg; } 712 713 ArrayRef<MCPhysReg> getVGPRSpillAGPRs() const { 714 return SpillVGPR; 715 } 716 717 MCPhysReg getVGPRToAGPRSpill(int FrameIndex, unsigned Lane) const { 718 auto I = VGPRToAGPRSpills.find(FrameIndex); 719 return (I == VGPRToAGPRSpills.end()) ? (MCPhysReg)AMDGPU::NoRegister 720 : I->second.Lanes[Lane]; 721 } 722 723 void setVGPRToAGPRSpillDead(int FrameIndex) { 724 auto I = VGPRToAGPRSpills.find(FrameIndex); 725 if (I != VGPRToAGPRSpills.end()) 726 I->second.IsDead = true; 727 } 728 729 // To bring the Physical VGPRs in the highest range allocated for CSR SGPR 730 // spilling into the lowest available range. 731 void shiftSpillPhysVGPRsToLowestRange(MachineFunction &MF); 732 733 bool allocateSGPRSpillToVGPRLane(MachineFunction &MF, int FI, 734 bool SpillToPhysVGPRLane = false, 735 bool IsPrologEpilog = false); 736 bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR); 737 738 /// If \p ResetSGPRSpillStackIDs is true, reset the stack ID from sgpr-spill 739 /// to the default stack. 740 bool removeDeadFrameIndices(MachineFrameInfo &MFI, 741 bool ResetSGPRSpillStackIDs); 742 743 int getScavengeFI(MachineFrameInfo &MFI, const SIRegisterInfo &TRI); 744 std::optional<int> getOptionalScavengeFI() const { return ScavengeFI; } 745 746 unsigned getBytesInStackArgArea() const { 747 return BytesInStackArgArea; 748 } 749 750 void setBytesInStackArgArea(unsigned Bytes) { 751 BytesInStackArgArea = Bytes; 752 } 753 754 // Add user SGPRs. 755 Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI); 756 Register addDispatchPtr(const SIRegisterInfo &TRI); 757 Register addQueuePtr(const SIRegisterInfo &TRI); 758 Register addKernargSegmentPtr(const SIRegisterInfo &TRI); 759 Register addDispatchID(const SIRegisterInfo &TRI); 760 Register addFlatScratchInit(const SIRegisterInfo &TRI); 761 Register addPrivateSegmentSize(const SIRegisterInfo &TRI); 762 Register addImplicitBufferPtr(const SIRegisterInfo &TRI); 763 Register addLDSKernelId(); 764 SmallVectorImpl<MCRegister> * 765 addPreloadedKernArg(const SIRegisterInfo &TRI, const TargetRegisterClass *RC, 766 unsigned AllocSizeDWord, int KernArgIdx, 767 int PaddingSGPRs); 768 769 /// Increment user SGPRs used for padding the argument list only. 770 Register addReservedUserSGPR() { 771 Register Next = getNextUserSGPR(); 772 ++NumUserSGPRs; 773 return Next; 774 } 775 776 // Add system SGPRs. 777 Register addWorkGroupIDX() { 778 ArgInfo.WorkGroupIDX = ArgDescriptor::createRegister(getNextSystemSGPR()); 779 NumSystemSGPRs += 1; 780 return ArgInfo.WorkGroupIDX.getRegister(); 781 } 782 783 Register addWorkGroupIDY() { 784 ArgInfo.WorkGroupIDY = ArgDescriptor::createRegister(getNextSystemSGPR()); 785 NumSystemSGPRs += 1; 786 return ArgInfo.WorkGroupIDY.getRegister(); 787 } 788 789 Register addWorkGroupIDZ() { 790 ArgInfo.WorkGroupIDZ = ArgDescriptor::createRegister(getNextSystemSGPR()); 791 NumSystemSGPRs += 1; 792 return ArgInfo.WorkGroupIDZ.getRegister(); 793 } 794 795 Register addWorkGroupInfo() { 796 ArgInfo.WorkGroupInfo = ArgDescriptor::createRegister(getNextSystemSGPR()); 797 NumSystemSGPRs += 1; 798 return ArgInfo.WorkGroupInfo.getRegister(); 799 } 800 801 bool hasLDSKernelId() const { return LDSKernelId; } 802 803 // Add special VGPR inputs 804 void setWorkItemIDX(ArgDescriptor Arg) { 805 ArgInfo.WorkItemIDX = Arg; 806 } 807 808 void setWorkItemIDY(ArgDescriptor Arg) { 809 ArgInfo.WorkItemIDY = Arg; 810 } 811 812 void setWorkItemIDZ(ArgDescriptor Arg) { 813 ArgInfo.WorkItemIDZ = Arg; 814 } 815 816 Register addPrivateSegmentWaveByteOffset() { 817 ArgInfo.PrivateSegmentWaveByteOffset 818 = ArgDescriptor::createRegister(getNextSystemSGPR()); 819 NumSystemSGPRs += 1; 820 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 821 } 822 823 void setPrivateSegmentWaveByteOffset(Register Reg) { 824 ArgInfo.PrivateSegmentWaveByteOffset = ArgDescriptor::createRegister(Reg); 825 } 826 827 bool hasWorkGroupIDX() const { 828 return WorkGroupIDX; 829 } 830 831 bool hasWorkGroupIDY() const { 832 return WorkGroupIDY; 833 } 834 835 bool hasWorkGroupIDZ() const { 836 return WorkGroupIDZ; 837 } 838 839 bool hasWorkGroupInfo() const { 840 return WorkGroupInfo; 841 } 842 843 bool hasPrivateSegmentWaveByteOffset() const { 844 return PrivateSegmentWaveByteOffset; 845 } 846 847 bool hasWorkItemIDX() const { 848 return WorkItemIDX; 849 } 850 851 bool hasWorkItemIDY() const { 852 return WorkItemIDY; 853 } 854 855 bool hasWorkItemIDZ() const { 856 return WorkItemIDZ; 857 } 858 859 bool hasImplicitArgPtr() const { 860 return ImplicitArgPtr; 861 } 862 863 AMDGPUFunctionArgInfo &getArgInfo() { 864 return ArgInfo; 865 } 866 867 const AMDGPUFunctionArgInfo &getArgInfo() const { 868 return ArgInfo; 869 } 870 871 std::tuple<const ArgDescriptor *, const TargetRegisterClass *, LLT> 872 getPreloadedValue(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 873 return ArgInfo.getPreloadedValue(Value); 874 } 875 876 MCRegister getPreloadedReg(AMDGPUFunctionArgInfo::PreloadedValue Value) const { 877 auto Arg = std::get<0>(ArgInfo.getPreloadedValue(Value)); 878 return Arg ? Arg->getRegister() : MCRegister(); 879 } 880 881 unsigned getGITPtrHigh() const { 882 return GITPtrHigh; 883 } 884 885 Register getGITPtrLoReg(const MachineFunction &MF) const; 886 887 uint32_t get32BitAddressHighBits() const { 888 return HighBitsOf32BitAddress; 889 } 890 891 unsigned getNumUserSGPRs() const { 892 return NumUserSGPRs; 893 } 894 895 unsigned getNumPreloadedSGPRs() const { 896 return NumUserSGPRs + NumSystemSGPRs; 897 } 898 899 unsigned getNumKernargPreloadedSGPRs() const { 900 return UserSGPRInfo.getNumKernargPreloadSGPRs(); 901 } 902 903 Register getPrivateSegmentWaveByteOffsetSystemSGPR() const { 904 return ArgInfo.PrivateSegmentWaveByteOffset.getRegister(); 905 } 906 907 /// Returns the physical register reserved for use as the resource 908 /// descriptor for scratch accesses. 909 Register getScratchRSrcReg() const { 910 return ScratchRSrcReg; 911 } 912 913 void setScratchRSrcReg(Register Reg) { 914 assert(Reg != 0 && "Should never be unset"); 915 ScratchRSrcReg = Reg; 916 } 917 918 Register getFrameOffsetReg() const { 919 return FrameOffsetReg; 920 } 921 922 void setFrameOffsetReg(Register Reg) { 923 assert(Reg != 0 && "Should never be unset"); 924 FrameOffsetReg = Reg; 925 } 926 927 void setStackPtrOffsetReg(Register Reg) { 928 assert(Reg != 0 && "Should never be unset"); 929 StackPtrOffsetReg = Reg; 930 } 931 932 void setLongBranchReservedReg(Register Reg) { LongBranchReservedReg = Reg; } 933 934 // Note the unset value for this is AMDGPU::SP_REG rather than 935 // NoRegister. This is mostly a workaround for MIR tests where state that 936 // can't be directly computed from the function is not preserved in serialized 937 // MIR. 938 Register getStackPtrOffsetReg() const { 939 return StackPtrOffsetReg; 940 } 941 942 Register getLongBranchReservedReg() const { return LongBranchReservedReg; } 943 944 Register getQueuePtrUserSGPR() const { 945 return ArgInfo.QueuePtr.getRegister(); 946 } 947 948 Register getImplicitBufferPtrUserSGPR() const { 949 return ArgInfo.ImplicitBufferPtr.getRegister(); 950 } 951 952 bool hasSpilledSGPRs() const { 953 return HasSpilledSGPRs; 954 } 955 956 void setHasSpilledSGPRs(bool Spill = true) { 957 HasSpilledSGPRs = Spill; 958 } 959 960 bool hasSpilledVGPRs() const { 961 return HasSpilledVGPRs; 962 } 963 964 void setHasSpilledVGPRs(bool Spill = true) { 965 HasSpilledVGPRs = Spill; 966 } 967 968 bool hasNonSpillStackObjects() const { 969 return HasNonSpillStackObjects; 970 } 971 972 void setHasNonSpillStackObjects(bool StackObject = true) { 973 HasNonSpillStackObjects = StackObject; 974 } 975 976 bool isStackRealigned() const { 977 return IsStackRealigned; 978 } 979 980 void setIsStackRealigned(bool Realigned = true) { 981 IsStackRealigned = Realigned; 982 } 983 984 unsigned getNumSpilledSGPRs() const { 985 return NumSpilledSGPRs; 986 } 987 988 unsigned getNumSpilledVGPRs() const { 989 return NumSpilledVGPRs; 990 } 991 992 void addToSpilledSGPRs(unsigned num) { 993 NumSpilledSGPRs += num; 994 } 995 996 void addToSpilledVGPRs(unsigned num) { 997 NumSpilledVGPRs += num; 998 } 999 1000 unsigned getPSInputAddr() const { 1001 return PSInputAddr; 1002 } 1003 1004 unsigned getPSInputEnable() const { 1005 return PSInputEnable; 1006 } 1007 1008 bool isPSInputAllocated(unsigned Index) const { 1009 return PSInputAddr & (1 << Index); 1010 } 1011 1012 void markPSInputAllocated(unsigned Index) { 1013 PSInputAddr |= 1 << Index; 1014 } 1015 1016 void markPSInputEnabled(unsigned Index) { 1017 PSInputEnable |= 1 << Index; 1018 } 1019 1020 bool returnsVoid() const { 1021 return ReturnsVoid; 1022 } 1023 1024 void setIfReturnsVoid(bool Value) { 1025 ReturnsVoid = Value; 1026 } 1027 1028 /// \returns A pair of default/requested minimum/maximum flat work group sizes 1029 /// for this function. 1030 std::pair<unsigned, unsigned> getFlatWorkGroupSizes() const { 1031 return FlatWorkGroupSizes; 1032 } 1033 1034 /// \returns Default/requested minimum flat work group size for this function. 1035 unsigned getMinFlatWorkGroupSize() const { 1036 return FlatWorkGroupSizes.first; 1037 } 1038 1039 /// \returns Default/requested maximum flat work group size for this function. 1040 unsigned getMaxFlatWorkGroupSize() const { 1041 return FlatWorkGroupSizes.second; 1042 } 1043 1044 /// \returns A pair of default/requested minimum/maximum number of waves per 1045 /// execution unit. 1046 std::pair<unsigned, unsigned> getWavesPerEU() const { 1047 return WavesPerEU; 1048 } 1049 1050 /// \returns Default/requested minimum number of waves per execution unit. 1051 unsigned getMinWavesPerEU() const { 1052 return WavesPerEU.first; 1053 } 1054 1055 /// \returns Default/requested maximum number of waves per execution unit. 1056 unsigned getMaxWavesPerEU() const { 1057 return WavesPerEU.second; 1058 } 1059 1060 const AMDGPUGWSResourcePseudoSourceValue * 1061 getGWSPSV(const AMDGPUTargetMachine &TM) { 1062 return &GWSResourcePSV; 1063 } 1064 1065 unsigned getOccupancy() const { 1066 return Occupancy; 1067 } 1068 1069 unsigned getMinAllowedOccupancy() const { 1070 if (!isMemoryBound() && !needsWaveLimiter()) 1071 return Occupancy; 1072 return (Occupancy < 4) ? Occupancy : 4; 1073 } 1074 1075 void limitOccupancy(const MachineFunction &MF); 1076 1077 void limitOccupancy(unsigned Limit) { 1078 if (Occupancy > Limit) 1079 Occupancy = Limit; 1080 } 1081 1082 void increaseOccupancy(const MachineFunction &MF, unsigned Limit) { 1083 if (Occupancy < Limit) 1084 Occupancy = Limit; 1085 limitOccupancy(MF); 1086 } 1087 1088 bool mayNeedAGPRs() const { 1089 return MayNeedAGPRs; 1090 } 1091 1092 // \returns true if a function has a use of AGPRs via inline asm or 1093 // has a call which may use it. 1094 bool mayUseAGPRs(const Function &F) const; 1095 1096 // \returns true if a function needs or may need AGPRs. 1097 bool usesAGPRs(const MachineFunction &MF) const; 1098 1099 /// \returns Default/requested number of work groups for this function. 1100 SmallVector<unsigned> getMaxNumWorkGroups() const { return MaxNumWorkGroups; } 1101 1102 unsigned getMaxNumWorkGroupsX() const { return MaxNumWorkGroups[0]; } 1103 unsigned getMaxNumWorkGroupsY() const { return MaxNumWorkGroups[1]; } 1104 unsigned getMaxNumWorkGroupsZ() const { return MaxNumWorkGroups[2]; } 1105 }; 1106 1107 } // end namespace llvm 1108 1109 #endif // LLVM_LIB_TARGET_AMDGPU_SIMACHINEFUNCTIONINFO_H 1110