LLVM 20.0.0git
SIMachineFunctionInfo.cpp
Go to the documentation of this file.
1//===- SIMachineFunctionInfo.cpp - SI Machine Function Info ---------------===//
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
10#include "AMDGPUSubtarget.h"
11#include "GCNSubtarget.h"
13#include "SIRegisterInfo.h"
21#include "llvm/IR/CallingConv.h"
23#include "llvm/IR/Function.h"
24#include <cassert>
25#include <optional>
26#include <vector>
27
28enum { MAX_LANES = 64 };
29
30using namespace llvm;
31
33 const SITargetLowering *TLI = STI->getTargetLowering();
34 return static_cast<const GCNTargetMachine &>(TLI->getTargetMachine());
35}
36
38 const GCNSubtarget *STI)
39 : AMDGPUMachineFunction(F, *STI), Mode(F, *STI), GWSResourcePSV(getTM(STI)),
40 UserSGPRInfo(F, *STI), WorkGroupIDX(false), WorkGroupIDY(false),
41 WorkGroupIDZ(false), WorkGroupInfo(false), LDSKernelId(false),
42 PrivateSegmentWaveByteOffset(false), WorkItemIDX(false),
43 WorkItemIDY(false), WorkItemIDZ(false), ImplicitArgPtr(false),
44 GITPtrHigh(0xffffffff), HighBitsOf32BitAddress(0) {
45 const GCNSubtarget &ST = *static_cast<const GCNSubtarget *>(STI);
46 FlatWorkGroupSizes = ST.getFlatWorkGroupSizes(F);
47 WavesPerEU = ST.getWavesPerEU(F);
48 MaxNumWorkGroups = ST.getMaxNumWorkGroups(F);
49 assert(MaxNumWorkGroups.size() == 3);
50
51 Occupancy = ST.computeOccupancy(F, getLDSSize()).second;
52 CallingConv::ID CC = F.getCallingConv();
53
54 VRegFlags.reserve(1024);
55
56 const bool IsKernel = CC == CallingConv::AMDGPU_KERNEL ||
58
59 if (IsKernel) {
60 WorkGroupIDX = true;
61 WorkItemIDX = true;
62 } else if (CC == CallingConv::AMDGPU_PS) {
63 PSInputAddr = AMDGPU::getInitialPSInputAddr(F);
64 }
65
66 MayNeedAGPRs = ST.hasMAIInsts();
67
68 if (AMDGPU::isChainCC(CC)) {
69 // Chain functions don't receive an SP from their caller, but are free to
70 // set one up. For now, we can use s32 to match what amdgpu_gfx functions
71 // would use if called, but this can be revisited.
72 // FIXME: Only reserve this if we actually need it.
73 StackPtrOffsetReg = AMDGPU::SGPR32;
74
75 ScratchRSrcReg = AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51;
76
77 ArgInfo.PrivateSegmentBuffer =
78 ArgDescriptor::createRegister(ScratchRSrcReg);
79
80 ImplicitArgPtr = false;
81 } else if (!isEntryFunction()) {
84
85 FrameOffsetReg = AMDGPU::SGPR33;
86 StackPtrOffsetReg = AMDGPU::SGPR32;
87
88 if (!ST.enableFlatScratch()) {
89 // Non-entry functions have no special inputs for now, other registers
90 // required for scratch access.
91 ScratchRSrcReg = AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3;
92
93 ArgInfo.PrivateSegmentBuffer =
94 ArgDescriptor::createRegister(ScratchRSrcReg);
95 }
96
97 if (!F.hasFnAttribute("amdgpu-no-implicitarg-ptr"))
98 ImplicitArgPtr = true;
99 } else {
100 ImplicitArgPtr = false;
101 MaxKernArgAlign = std::max(ST.getAlignmentForImplicitArgPtr(),
103
104 if (ST.hasGFX90AInsts() &&
105 ST.getMaxNumVGPRs(F) <= AMDGPU::VGPR_32RegClass.getNumRegs() &&
106 !mayUseAGPRs(F))
107 MayNeedAGPRs = false; // We will select all MAI with VGPR operands.
108 }
109
110 if (!AMDGPU::isGraphics(CC) ||
112 ST.hasArchitectedSGPRs())) {
113 if (IsKernel || !F.hasFnAttribute("amdgpu-no-workgroup-id-x"))
114 WorkGroupIDX = true;
115
116 if (!F.hasFnAttribute("amdgpu-no-workgroup-id-y"))
117 WorkGroupIDY = true;
118
119 if (!F.hasFnAttribute("amdgpu-no-workgroup-id-z"))
120 WorkGroupIDZ = true;
121 }
122
123 if (!AMDGPU::isGraphics(CC)) {
124 if (IsKernel || !F.hasFnAttribute("amdgpu-no-workitem-id-x"))
125 WorkItemIDX = true;
126
127 if (!F.hasFnAttribute("amdgpu-no-workitem-id-y") &&
128 ST.getMaxWorkitemID(F, 1) != 0)
129 WorkItemIDY = true;
130
131 if (!F.hasFnAttribute("amdgpu-no-workitem-id-z") &&
132 ST.getMaxWorkitemID(F, 2) != 0)
133 WorkItemIDZ = true;
134
135 if (!IsKernel && !F.hasFnAttribute("amdgpu-no-lds-kernel-id"))
136 LDSKernelId = true;
137 }
138
139 if (isEntryFunction()) {
140 // X, XY, and XYZ are the only supported combinations, so make sure Y is
141 // enabled if Z is.
142 if (WorkItemIDZ)
143 WorkItemIDY = true;
144
145 if (!ST.flatScratchIsArchitected()) {
146 PrivateSegmentWaveByteOffset = true;
147
148 // HS and GS always have the scratch wave offset in SGPR5 on GFX9.
149 if (ST.getGeneration() >= AMDGPUSubtarget::GFX9 &&
151 ArgInfo.PrivateSegmentWaveByteOffset =
152 ArgDescriptor::createRegister(AMDGPU::SGPR5);
153 }
154 }
155
156 Attribute A = F.getFnAttribute("amdgpu-git-ptr-high");
157 StringRef S = A.getValueAsString();
158 if (!S.empty())
159 S.consumeInteger(0, GITPtrHigh);
160
161 A = F.getFnAttribute("amdgpu-32bit-address-high-bits");
162 S = A.getValueAsString();
163 if (!S.empty())
164 S.consumeInteger(0, HighBitsOf32BitAddress);
165
166 MaxMemoryClusterDWords = F.getFnAttributeAsParsedInteger(
167 "amdgpu-max-memory-cluster-dwords", DefaultMemoryClusterDWordsLimit);
168
169 // On GFX908, in order to guarantee copying between AGPRs, we need a scratch
170 // VGPR available at all times. For now, reserve highest available VGPR. After
171 // RA, shift it to the lowest available unused VGPR if the one exist.
172 if (ST.hasMAIInsts() && !ST.hasGFX90AInsts()) {
173 VGPRForAGPRCopy =
174 AMDGPU::VGPR_32RegClass.getRegister(ST.getMaxNumVGPRs(F) - 1);
175 }
176}
177
179 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
181 const {
182 return DestMF.cloneInfo<SIMachineFunctionInfo>(*this);
183}
184
187 const GCNSubtarget& ST = MF.getSubtarget<GCNSubtarget>();
188 limitOccupancy(ST.getOccupancyWithWorkGroupSizes(MF).second);
189}
190
192 const SIRegisterInfo &TRI) {
193 ArgInfo.PrivateSegmentBuffer =
194 ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
195 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SGPR_128RegClass));
196 NumUserSGPRs += 4;
197 return ArgInfo.PrivateSegmentBuffer.getRegister();
198}
199
201 ArgInfo.DispatchPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
202 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
203 NumUserSGPRs += 2;
204 return ArgInfo.DispatchPtr.getRegister();
205}
206
208 ArgInfo.QueuePtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
209 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
210 NumUserSGPRs += 2;
211 return ArgInfo.QueuePtr.getRegister();
212}
213
215 ArgInfo.KernargSegmentPtr
216 = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
217 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
218 NumUserSGPRs += 2;
219 return ArgInfo.KernargSegmentPtr.getRegister();
220}
221
223 ArgInfo.DispatchID = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
224 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
225 NumUserSGPRs += 2;
226 return ArgInfo.DispatchID.getRegister();
227}
228
230 ArgInfo.FlatScratchInit = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
231 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
232 NumUserSGPRs += 2;
233 return ArgInfo.FlatScratchInit.getRegister();
234}
235
237 ArgInfo.PrivateSegmentSize = ArgDescriptor::createRegister(getNextUserSGPR());
238 NumUserSGPRs += 1;
239 return ArgInfo.PrivateSegmentSize.getRegister();
240}
241
243 ArgInfo.ImplicitBufferPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
244 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
245 NumUserSGPRs += 2;
246 return ArgInfo.ImplicitBufferPtr.getRegister();
247}
248
250 ArgInfo.LDSKernelId = ArgDescriptor::createRegister(getNextUserSGPR());
251 NumUserSGPRs += 1;
252 return ArgInfo.LDSKernelId.getRegister();
253}
254
256 const SIRegisterInfo &TRI, const TargetRegisterClass *RC,
257 unsigned AllocSizeDWord, int KernArgIdx, int PaddingSGPRs) {
258 assert(!ArgInfo.PreloadKernArgs.count(KernArgIdx) &&
259 "Preload kernel argument allocated twice.");
260 NumUserSGPRs += PaddingSGPRs;
261 // If the available register tuples are aligned with the kernarg to be
262 // preloaded use that register, otherwise we need to use a set of SGPRs and
263 // merge them.
264 if (!ArgInfo.FirstKernArgPreloadReg)
265 ArgInfo.FirstKernArgPreloadReg = getNextUserSGPR();
266 Register PreloadReg =
267 TRI.getMatchingSuperReg(getNextUserSGPR(), AMDGPU::sub0, RC);
268 if (PreloadReg &&
269 (RC == &AMDGPU::SReg_32RegClass || RC == &AMDGPU::SReg_64RegClass)) {
270 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(PreloadReg);
271 NumUserSGPRs += AllocSizeDWord;
272 } else {
273 for (unsigned I = 0; I < AllocSizeDWord; ++I) {
274 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(getNextUserSGPR());
275 NumUserSGPRs++;
276 }
277 }
278
279 // Track the actual number of SGPRs that HW will preload to.
280 UserSGPRInfo.allocKernargPreloadSGPRs(AllocSizeDWord + PaddingSGPRs);
281 return &ArgInfo.PreloadKernArgs[KernArgIdx].Regs;
282}
283
285 uint64_t Size, Align Alignment) {
286 // Skip if it is an entry function or the register is already added.
287 if (isEntryFunction() || WWMSpills.count(VGPR))
288 return;
289
290 // Skip if this is a function with the amdgpu_cs_chain or
291 // amdgpu_cs_chain_preserve calling convention and this is a scratch register.
292 // We never need to allocate a spill for these because we don't even need to
293 // restore the inactive lanes for them (they're scratchier than the usual
294 // scratch registers). We only need to do this if we have calls to
295 // llvm.amdgcn.cs.chain (otherwise there's no one to save them for, since
296 // chain functions do not return) and the function did not contain a call to
297 // llvm.amdgcn.init.whole.wave (since in that case there are no inactive lanes
298 // when entering the function).
299 if (isChainFunction() &&
302 return;
303
304 WWMSpills.insert(std::make_pair(
305 VGPR, MF.getFrameInfo().CreateSpillStackObject(Size, Alignment)));
306}
307
308// Separate out the callee-saved and scratch registers.
310 MachineFunction &MF,
311 SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs,
312 SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const {
313 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
314 for (auto &Reg : WWMSpills) {
315 if (isCalleeSavedReg(CSRegs, Reg.first))
316 CalleeSavedRegs.push_back(Reg);
317 else
318 ScratchRegs.push_back(Reg);
319 }
320}
321
323 MCPhysReg Reg) const {
324 for (unsigned I = 0; CSRegs[I]; ++I) {
325 if (CSRegs[I] == Reg)
326 return true;
327 }
328
329 return false;
330}
331
334 BitVector &SavedVGPRs) {
335 const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
337 for (unsigned I = 0, E = WWMVGPRs.size(); I < E; ++I) {
338 Register Reg = WWMVGPRs[I];
339 Register NewReg =
340 TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF);
341 if (!NewReg || NewReg >= Reg)
342 break;
343
344 MRI.replaceRegWith(Reg, NewReg);
345
346 // Update various tables with the new VGPR.
347 WWMVGPRs[I] = NewReg;
348 WWMReservedRegs.remove(Reg);
349 WWMReservedRegs.insert(NewReg);
350 MRI.reserveReg(NewReg, TRI);
351
352 // Replace the register in SpillPhysVGPRs. This is needed to look for free
353 // lanes while spilling special SGPRs like FP, BP, etc. during PEI.
354 auto *RegItr = std::find(SpillPhysVGPRs.begin(), SpillPhysVGPRs.end(), Reg);
355 if (RegItr != SpillPhysVGPRs.end()) {
356 unsigned Idx = std::distance(SpillPhysVGPRs.begin(), RegItr);
357 SpillPhysVGPRs[Idx] = NewReg;
358 }
359
360 // The generic `determineCalleeSaves` might have set the old register if it
361 // is in the CSR range.
362 SavedVGPRs.reset(Reg);
363
364 for (MachineBasicBlock &MBB : MF) {
365 MBB.removeLiveIn(Reg);
367 }
368
369 Reg = NewReg;
370 }
371}
372
373bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills(
374 MachineFunction &MF, int FI, unsigned LaneIndex) {
376 Register LaneVGPR;
377 if (!LaneIndex) {
378 LaneVGPR = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
379 SpillVGPRs.push_back(LaneVGPR);
380 } else {
381 LaneVGPR = SpillVGPRs.back();
382 }
383
384 SGPRSpillsToVirtualVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
385 return true;
386}
387
388bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills(
389 MachineFunction &MF, int FI, unsigned LaneIndex, bool IsPrologEpilog) {
391 const SIRegisterInfo *TRI = ST.getRegisterInfo();
393 Register LaneVGPR;
394 if (!LaneIndex) {
395 // Find the highest available register if called before RA to ensure the
396 // lowest registers are available for allocation. The LaneVGPR, in that
397 // case, will be shifted back to the lowest range after VGPR allocation.
398 LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF,
399 !IsPrologEpilog);
400 if (LaneVGPR == AMDGPU::NoRegister) {
401 // We have no VGPRs left for spilling SGPRs. Reset because we will not
402 // partially spill the SGPR to VGPRs.
403 SGPRSpillsToPhysicalVGPRLanes.erase(FI);
404 return false;
405 }
406
407 if (IsPrologEpilog)
408 allocateWWMSpill(MF, LaneVGPR);
409
410 reserveWWMRegister(LaneVGPR);
411 for (MachineBasicBlock &MBB : MF) {
412 MBB.addLiveIn(LaneVGPR);
414 }
415 SpillPhysVGPRs.push_back(LaneVGPR);
416 } else {
417 LaneVGPR = SpillPhysVGPRs.back();
418 }
419
420 SGPRSpillsToPhysicalVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
421 return true;
422}
423
425 MachineFunction &MF, int FI, bool SpillToPhysVGPRLane,
426 bool IsPrologEpilog) {
427 std::vector<SIRegisterInfo::SpilledReg> &SpillLanes =
428 SpillToPhysVGPRLane ? SGPRSpillsToPhysicalVGPRLanes[FI]
429 : SGPRSpillsToVirtualVGPRLanes[FI];
430
431 // This has already been allocated.
432 if (!SpillLanes.empty())
433 return true;
434
435 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
436 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
437 unsigned WaveSize = ST.getWavefrontSize();
438
439 unsigned Size = FrameInfo.getObjectSize(FI);
440 unsigned NumLanes = Size / 4;
441
442 if (NumLanes > WaveSize)
443 return false;
444
445 assert(Size >= 4 && "invalid sgpr spill size");
446 assert(ST.getRegisterInfo()->spillSGPRToVGPR() &&
447 "not spilling SGPRs to VGPRs");
448
449 unsigned &NumSpillLanes = SpillToPhysVGPRLane ? NumPhysicalVGPRSpillLanes
450 : NumVirtualVGPRSpillLanes;
451
452 for (unsigned I = 0; I < NumLanes; ++I, ++NumSpillLanes) {
453 unsigned LaneIndex = (NumSpillLanes % WaveSize);
454
455 bool Allocated = SpillToPhysVGPRLane
456 ? allocatePhysicalVGPRForSGPRSpills(MF, FI, LaneIndex,
457 IsPrologEpilog)
458 : allocateVirtualVGPRForSGPRSpills(MF, FI, LaneIndex);
459 if (!Allocated) {
460 NumSpillLanes -= I;
461 return false;
462 }
463 }
464
465 return true;
466}
467
468/// Reserve AGPRs or VGPRs to support spilling for FrameIndex \p FI.
469/// Either AGPR is spilled to VGPR to vice versa.
470/// Returns true if a \p FI can be eliminated completely.
472 int FI,
473 bool isAGPRtoVGPR) {
475 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
476 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
477
478 assert(ST.hasMAIInsts() && FrameInfo.isSpillSlotObjectIndex(FI));
479
480 auto &Spill = VGPRToAGPRSpills[FI];
481
482 // This has already been allocated.
483 if (!Spill.Lanes.empty())
484 return Spill.FullyAllocated;
485
486 unsigned Size = FrameInfo.getObjectSize(FI);
487 unsigned NumLanes = Size / 4;
488 Spill.Lanes.resize(NumLanes, AMDGPU::NoRegister);
489
490 const TargetRegisterClass &RC =
491 isAGPRtoVGPR ? AMDGPU::VGPR_32RegClass : AMDGPU::AGPR_32RegClass;
492 auto Regs = RC.getRegisters();
493
494 auto &SpillRegs = isAGPRtoVGPR ? SpillAGPR : SpillVGPR;
495 const SIRegisterInfo *TRI = ST.getRegisterInfo();
496 Spill.FullyAllocated = true;
497
498 // FIXME: Move allocation logic out of MachineFunctionInfo and initialize
499 // once.
500 BitVector OtherUsedRegs;
501 OtherUsedRegs.resize(TRI->getNumRegs());
502
503 const uint32_t *CSRMask =
504 TRI->getCallPreservedMask(MF, MF.getFunction().getCallingConv());
505 if (CSRMask)
506 OtherUsedRegs.setBitsInMask(CSRMask);
507
508 // TODO: Should include register tuples, but doesn't matter with current
509 // usage.
510 for (MCPhysReg Reg : SpillAGPR)
511 OtherUsedRegs.set(Reg);
512 for (MCPhysReg Reg : SpillVGPR)
513 OtherUsedRegs.set(Reg);
514
515 SmallVectorImpl<MCPhysReg>::const_iterator NextSpillReg = Regs.begin();
516 for (int I = NumLanes - 1; I >= 0; --I) {
517 NextSpillReg = std::find_if(
518 NextSpillReg, Regs.end(), [&MRI, &OtherUsedRegs](MCPhysReg Reg) {
519 return MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg) &&
520 !OtherUsedRegs[Reg];
521 });
522
523 if (NextSpillReg == Regs.end()) { // Registers exhausted
524 Spill.FullyAllocated = false;
525 break;
526 }
527
528 OtherUsedRegs.set(*NextSpillReg);
529 SpillRegs.push_back(*NextSpillReg);
530 MRI.reserveReg(*NextSpillReg, TRI);
531 Spill.Lanes[I] = *NextSpillReg++;
532 }
533
534 return Spill.FullyAllocated;
535}
536
538 MachineFrameInfo &MFI, bool ResetSGPRSpillStackIDs) {
539 // Remove dead frame indices from function frame, however keep FP & BP since
540 // spills for them haven't been inserted yet. And also make sure to remove the
541 // frame indices from `SGPRSpillsToVirtualVGPRLanes` data structure,
542 // otherwise, it could result in an unexpected side effect and bug, in case of
543 // any re-mapping of freed frame indices by later pass(es) like "stack slot
544 // coloring".
545 for (auto &R : make_early_inc_range(SGPRSpillsToVirtualVGPRLanes)) {
546 MFI.RemoveStackObject(R.first);
547 SGPRSpillsToVirtualVGPRLanes.erase(R.first);
548 }
549
550 // Remove the dead frame indices of CSR SGPRs which are spilled to physical
551 // VGPR lanes during SILowerSGPRSpills pass.
552 if (!ResetSGPRSpillStackIDs) {
553 for (auto &R : make_early_inc_range(SGPRSpillsToPhysicalVGPRLanes)) {
554 MFI.RemoveStackObject(R.first);
555 SGPRSpillsToPhysicalVGPRLanes.erase(R.first);
556 }
557 }
558 bool HaveSGPRToMemory = false;
559
560 if (ResetSGPRSpillStackIDs) {
561 // All other SGPRs must be allocated on the default stack, so reset the
562 // stack ID.
563 for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); I != E;
564 ++I) {
568 HaveSGPRToMemory = true;
569 }
570 }
571 }
572 }
573
574 for (auto &R : VGPRToAGPRSpills) {
575 if (R.second.IsDead)
576 MFI.RemoveStackObject(R.first);
577 }
578
579 return HaveSGPRToMemory;
580}
581
583 const SIRegisterInfo &TRI) {
584 if (ScavengeFI)
585 return *ScavengeFI;
586
587 ScavengeFI =
588 MFI.CreateStackObject(TRI.getSpillSize(AMDGPU::SGPR_32RegClass),
589 TRI.getSpillAlign(AMDGPU::SGPR_32RegClass), false);
590 return *ScavengeFI;
591}
592
593MCPhysReg SIMachineFunctionInfo::getNextUserSGPR() const {
594 assert(NumSystemSGPRs == 0 && "System SGPRs must be added after user SGPRs");
595 return AMDGPU::SGPR0 + NumUserSGPRs;
596}
597
598MCPhysReg SIMachineFunctionInfo::getNextSystemSGPR() const {
599 return AMDGPU::SGPR0 + NumUserSGPRs + NumSystemSGPRs;
600}
601
602void SIMachineFunctionInfo::MRI_NoteNewVirtualRegister(Register Reg) {
603 VRegFlags.grow(Reg);
604}
605
606void SIMachineFunctionInfo::MRI_NoteCloneVirtualRegister(Register NewReg,
607 Register SrcReg) {
608 VRegFlags.grow(NewReg);
609 VRegFlags[NewReg] = VRegFlags[SrcReg];
610}
611
614 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
615 if (!ST.isAmdPalOS())
616 return Register();
617 Register GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in
618 if (ST.hasMergedShaders()) {
619 switch (MF.getFunction().getCallingConv()) {
622 // Low GIT address is passed in s8 rather than s0 for an LS+HS or
623 // ES+GS merged shader on gfx9+.
624 GitPtrLo = AMDGPU::SGPR8;
625 return GitPtrLo;
626 default:
627 return GitPtrLo;
628 }
629 }
630 return GitPtrLo;
631}
632
634 const TargetRegisterInfo &TRI) {
636 {
638 OS << printReg(Reg, &TRI);
639 }
640 return Dest;
641}
642
643static std::optional<yaml::SIArgumentInfo>
645 const TargetRegisterInfo &TRI) {
647
648 auto convertArg = [&](std::optional<yaml::SIArgument> &A,
649 const ArgDescriptor &Arg) {
650 if (!Arg)
651 return false;
652
653 // Create a register or stack argument.
655 if (Arg.isRegister()) {
657 OS << printReg(Arg.getRegister(), &TRI);
658 } else
659 SA.StackOffset = Arg.getStackOffset();
660 // Check and update the optional mask.
661 if (Arg.isMasked())
662 SA.Mask = Arg.getMask();
663
664 A = SA;
665 return true;
666 };
667
668 // TODO: Need to serialize kernarg preloads.
669 bool Any = false;
670 Any |= convertArg(AI.PrivateSegmentBuffer, ArgInfo.PrivateSegmentBuffer);
671 Any |= convertArg(AI.DispatchPtr, ArgInfo.DispatchPtr);
672 Any |= convertArg(AI.QueuePtr, ArgInfo.QueuePtr);
673 Any |= convertArg(AI.KernargSegmentPtr, ArgInfo.KernargSegmentPtr);
674 Any |= convertArg(AI.DispatchID, ArgInfo.DispatchID);
675 Any |= convertArg(AI.FlatScratchInit, ArgInfo.FlatScratchInit);
676 Any |= convertArg(AI.LDSKernelId, ArgInfo.LDSKernelId);
677 Any |= convertArg(AI.PrivateSegmentSize, ArgInfo.PrivateSegmentSize);
678 Any |= convertArg(AI.WorkGroupIDX, ArgInfo.WorkGroupIDX);
679 Any |= convertArg(AI.WorkGroupIDY, ArgInfo.WorkGroupIDY);
680 Any |= convertArg(AI.WorkGroupIDZ, ArgInfo.WorkGroupIDZ);
681 Any |= convertArg(AI.WorkGroupInfo, ArgInfo.WorkGroupInfo);
682 Any |= convertArg(AI.PrivateSegmentWaveByteOffset,
683 ArgInfo.PrivateSegmentWaveByteOffset);
684 Any |= convertArg(AI.ImplicitArgPtr, ArgInfo.ImplicitArgPtr);
685 Any |= convertArg(AI.ImplicitBufferPtr, ArgInfo.ImplicitBufferPtr);
686 Any |= convertArg(AI.WorkItemIDX, ArgInfo.WorkItemIDX);
687 Any |= convertArg(AI.WorkItemIDY, ArgInfo.WorkItemIDY);
688 Any |= convertArg(AI.WorkItemIDZ, ArgInfo.WorkItemIDZ);
689
690 if (Any)
691 return AI;
692
693 return std::nullopt;
694}
695
698 const llvm::MachineFunction &MF)
699 : ExplicitKernArgSize(MFI.getExplicitKernArgSize()),
700 MaxKernArgAlign(MFI.getMaxKernArgAlign()), LDSSize(MFI.getLDSSize()),
701 GDSSize(MFI.getGDSSize()), DynLDSAlign(MFI.getDynLDSAlign()),
702 IsEntryFunction(MFI.isEntryFunction()),
703 NoSignedZerosFPMath(MFI.hasNoSignedZerosFPMath()),
704 MemoryBound(MFI.isMemoryBound()), WaveLimiter(MFI.needsWaveLimiter()),
705 HasSpilledSGPRs(MFI.hasSpilledSGPRs()),
706 HasSpilledVGPRs(MFI.hasSpilledVGPRs()),
707 HighBitsOf32BitAddress(MFI.get32BitAddressHighBits()),
708 Occupancy(MFI.getOccupancy()),
709 ScratchRSrcReg(regToString(MFI.getScratchRSrcReg(), TRI)),
710 FrameOffsetReg(regToString(MFI.getFrameOffsetReg(), TRI)),
711 StackPtrOffsetReg(regToString(MFI.getStackPtrOffsetReg(), TRI)),
712 BytesInStackArgArea(MFI.getBytesInStackArgArea()),
713 ReturnsVoid(MFI.returnsVoid()),
714 ArgInfo(convertArgumentInfo(MFI.getArgInfo(), TRI)),
715 PSInputAddr(MFI.getPSInputAddr()), PSInputEnable(MFI.getPSInputEnable()),
716 MaxMemoryClusterDWords(MFI.getMaxMemoryClusterDWords()),
717 Mode(MFI.getMode()), HasInitWholeWave(MFI.hasInitWholeWave()) {
718 for (Register Reg : MFI.getSGPRSpillPhysVGPRs())
719 SpillPhysVGPRS.push_back(regToString(Reg, TRI));
720
721 for (Register Reg : MFI.getWWMReservedRegs())
722 WWMReservedRegs.push_back(regToString(Reg, TRI));
723
724 if (MFI.getLongBranchReservedReg())
726 if (MFI.getVGPRForAGPRCopy())
728
729 if (MFI.getSGPRForEXECCopy())
731
732 auto SFI = MFI.getOptionalScavengeFI();
733 if (SFI)
735}
736
739}
740
742 const yaml::SIMachineFunctionInfo &YamlMFI, const MachineFunction &MF,
746 LDSSize = YamlMFI.LDSSize;
747 GDSSize = YamlMFI.GDSSize;
748 DynLDSAlign = YamlMFI.DynLDSAlign;
749 PSInputAddr = YamlMFI.PSInputAddr;
753 Occupancy = YamlMFI.Occupancy;
756 MemoryBound = YamlMFI.MemoryBound;
757 WaveLimiter = YamlMFI.WaveLimiter;
761 ReturnsVoid = YamlMFI.ReturnsVoid;
762
763 if (YamlMFI.ScavengeFI) {
764 auto FIOrErr = YamlMFI.ScavengeFI->getFI(MF.getFrameInfo());
765 if (!FIOrErr) {
766 // Create a diagnostic for a the frame index.
767 const MemoryBuffer &Buffer =
768 *PFS.SM->getMemoryBuffer(PFS.SM->getMainFileID());
769
770 Error = SMDiagnostic(*PFS.SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 1,
771 SourceMgr::DK_Error, toString(FIOrErr.takeError()),
772 "", {}, {});
773 SourceRange = YamlMFI.ScavengeFI->SourceRange;
774 return true;
775 }
776 ScavengeFI = *FIOrErr;
777 } else {
778 ScavengeFI = std::nullopt;
779 }
780 return false;
781}
782
784 return !F.hasFnAttribute("amdgpu-no-agpr");
785}
786
788 if (UsesAGPRs)
789 return *UsesAGPRs;
790
791 if (!mayNeedAGPRs()) {
792 UsesAGPRs = false;
793 return false;
794 }
795
797 MF.getFrameInfo().hasCalls()) {
798 UsesAGPRs = true;
799 return true;
800 }
801
802 const MachineRegisterInfo &MRI = MF.getRegInfo();
803
804 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
806 const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
807 if (RC && SIRegisterInfo::isAGPRClass(RC)) {
808 UsesAGPRs = true;
809 return true;
810 }
811 if (!RC && !MRI.use_empty(Reg) && MRI.getType(Reg).isValid()) {
812 // Defer caching UsesAGPRs, function might not yet been regbank selected.
813 return true;
814 }
815 }
816
817 for (MCRegister Reg : AMDGPU::AGPR_32RegClass) {
818 if (MRI.isPhysRegUsed(Reg)) {
819 UsesAGPRs = true;
820 return true;
821 }
822 }
823
824 UsesAGPRs = false;
825 return false;
826}
unsigned const MachineRegisterInfo * MRI
Provides AMDGPU specific target descriptions.
Base class for AMDGPU specific classes of TargetSubtarget.
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Size
IO & YamlIO
Definition: ELFYAML.cpp:1314
AMD GCN specific subclass of TargetSubtarget.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
static std::optional< yaml::SIArgumentInfo > convertArgumentInfo(const AMDGPUFunctionArgInfo &ArgInfo, const TargetRegisterInfo &TRI)
static yaml::StringValue regToString(Register Reg, const TargetRegisterInfo &TRI)
Interface definition for SIRegisterInfo.
raw_pwrite_stream & OS
static const AMDGPUFunctionArgInfo FixedABIFunctionInfo
Definition: Any.h:28
BitVector & reset()
Definition: BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
BitVector & set()
Definition: BitVector.h:351
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
setBitsInMask - Add '1' bits from Mask to this vector.
Definition: BitVector.h:707
void push_back(bool Val)
Definition: BitVector.h:466
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:277
const SITargetLowering * getTargetLowering() const override
Definition: GCNSubtarget.h:287
void allocKernargPreloadSGPRs(unsigned NumSGPRs)
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasCalls() const
Return true if the current function has any function calls.
int CreateSpillStackObject(uint64_t Size, Align Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
void setStackID(int ObjectIdx, uint8_t ID)
bool hasTailCall() const
Returns true if the function contains a tail call.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
uint8_t getStackID(int ObjectIdx) const
int getObjectIndexBegin() const
Return the minimum frame object index.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * cloneInfo(const Ty &Old)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
size_type count(const KeyT &Key) const
Definition: MapVector.h:165
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:141
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:76
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:84
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
bool usesAGPRs(const MachineFunction &MF) const
bool initializeBaseYamlFields(const yaml::SIMachineFunctionInfo &YamlMFI, const MachineFunction &MF, PerFunctionMIParsingState &PFS, SMDiagnostic &Error, SMRange &SourceRange)
void shiftWwmVGPRsToLowestRange(MachineFunction &MF, SmallVectorImpl< Register > &WWMVGPRs, BitVector &SavedVGPRs)
Register addPrivateSegmentSize(const SIRegisterInfo &TRI)
void allocateWWMSpill(MachineFunction &MF, Register VGPR, uint64_t Size=4, Align Alignment=Align(4))
Register addDispatchPtr(const SIRegisterInfo &TRI)
Register addFlatScratchInit(const SIRegisterInfo &TRI)
ArrayRef< Register > getSGPRSpillPhysVGPRs() const
int getScavengeFI(MachineFrameInfo &MFI, const SIRegisterInfo &TRI)
Register addQueuePtr(const SIRegisterInfo &TRI)
SIMachineFunctionInfo(const SIMachineFunctionInfo &MFI)=default
Register getGITPtrLoReg(const MachineFunction &MF) const
bool allocateVGPRSpillToAGPR(MachineFunction &MF, int FI, bool isAGPRtoVGPR)
Reserve AGPRs or VGPRs to support spilling for FrameIndex FI.
void splitWWMSpillRegisters(MachineFunction &MF, SmallVectorImpl< std::pair< Register, int > > &CalleeSavedRegs, SmallVectorImpl< std::pair< Register, int > > &ScratchRegs) const
bool mayUseAGPRs(const Function &F) const
bool isCalleeSavedReg(const MCPhysReg *CSRegs, MCPhysReg Reg) const
bool allocateSGPRSpillToVGPRLane(MachineFunction &MF, int FI, bool SpillToPhysVGPRLane=false, bool IsPrologEpilog=false)
Register addKernargSegmentPtr(const SIRegisterInfo &TRI)
Register addDispatchID(const SIRegisterInfo &TRI)
bool removeDeadFrameIndices(MachineFrameInfo &MFI, bool ResetSGPRSpillStackIDs)
If ResetSGPRSpillStackIDs is true, reset the stack ID from sgpr-spill to the default stack.
MachineFunctionInfo * clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF, const DenseMap< MachineBasicBlock *, MachineBasicBlock * > &Src2DstMBB) const override
Make a functionally equivalent copy of this MachineFunctionInfo in MF.
bool checkIndexInPrologEpilogSGPRSpills(int FI) const
Register addPrivateSegmentBuffer(const SIRegisterInfo &TRI)
const ReservedRegSet & getWWMReservedRegs() const
std::optional< int > getOptionalScavengeFI() const
Register addImplicitBufferPtr(const SIRegisterInfo &TRI)
void limitOccupancy(const MachineFunction &MF)
SmallVectorImpl< MCRegister > * addPreloadedKernArg(const SIRegisterInfo &TRI, const TargetRegisterClass *RC, unsigned AllocSizeDWord, int KernArgIdx, int PaddingSGPRs)
static bool isChainScratchRegister(Register VGPR)
static bool isAGPRClass(const TargetRegisterClass *RC)
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
Represents a location in source code.
Definition: SMLoc.h:23
Represents a range in source code.
Definition: SMLoc.h:48
bool remove(const value_type &X)
Remove an item from the set vector.
Definition: SetVector.h:188
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:578
unsigned getMainFileID() const
Definition: SourceMgr.h:132
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:125
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:499
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
const TargetMachine & getTargetMachine() const
ArrayRef< MCPhysReg > getRegisters() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
bool isEntryFunctionCC(CallingConv::ID CC)
bool isChainCC(CallingConv::ID CC)
unsigned getInitialPSInputAddr(const Function &F)
bool isGraphics(CallingConv::ID cc)
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:197
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:200
@ AMDGPU_Gfx
Used for AMD graphics targets.
Definition: CallingConv.h:232
@ AMDGPU_HS
Used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:206
@ AMDGPU_GS
Used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:191
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:194
@ SPIR_KERNEL
Used for SPIR kernel functions.
Definition: CallingConv.h:144
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition: MCRegister.h:21
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
constexpr unsigned DefaultMemoryClusterDWordsLimit
Definition: SIInstrInfo.h:39
const char * toString(DWARFSectionKind Kind)
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
static ArgDescriptor createRegister(Register Reg, unsigned Mask=~0u)
Helper struct shared between Function Specialization and SCCP Solver.
Definition: SCCPSolver.h:41
MachineFunctionInfo - This class can be derived from and used by targets to hold private target-speci...
A serializaable representation of a reference to a stack object or fixed stack object.
std::optional< SIArgument > PrivateSegmentWaveByteOffset
std::optional< SIArgument > WorkGroupIDY
std::optional< SIArgument > FlatScratchInit
std::optional< SIArgument > DispatchPtr
std::optional< SIArgument > DispatchID
std::optional< SIArgument > WorkItemIDY
std::optional< SIArgument > WorkGroupIDX
std::optional< SIArgument > ImplicitArgPtr
std::optional< SIArgument > QueuePtr
std::optional< SIArgument > WorkGroupInfo
std::optional< SIArgument > LDSKernelId
std::optional< SIArgument > ImplicitBufferPtr
std::optional< SIArgument > WorkItemIDX
std::optional< SIArgument > KernargSegmentPtr
std::optional< SIArgument > WorkItemIDZ
std::optional< SIArgument > PrivateSegmentSize
std::optional< SIArgument > PrivateSegmentBuffer
std::optional< SIArgument > WorkGroupIDZ
std::optional< unsigned > Mask
static SIArgument createArgument(bool IsReg)
SmallVector< StringValue > WWMReservedRegs
void mappingImpl(yaml::IO &YamlIO) override
SmallVector< StringValue, 2 > SpillPhysVGPRS
std::optional< FrameIndex > ScavengeFI
A wrapper around std::string which contains a source range that's being set during parsing.