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());
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.getOccupancyWithLocalMemSize(getLDSSize(),
189 MF.getFunction()));
190}
191
193 const SIRegisterInfo &TRI) {
194 ArgInfo.PrivateSegmentBuffer =
195 ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
196 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SGPR_128RegClass));
197 NumUserSGPRs += 4;
198 return ArgInfo.PrivateSegmentBuffer.getRegister();
199}
200
202 ArgInfo.DispatchPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
203 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
204 NumUserSGPRs += 2;
205 return ArgInfo.DispatchPtr.getRegister();
206}
207
209 ArgInfo.QueuePtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
210 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
211 NumUserSGPRs += 2;
212 return ArgInfo.QueuePtr.getRegister();
213}
214
216 ArgInfo.KernargSegmentPtr
217 = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
218 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
219 NumUserSGPRs += 2;
220 return ArgInfo.KernargSegmentPtr.getRegister();
221}
222
224 ArgInfo.DispatchID = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
225 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
226 NumUserSGPRs += 2;
227 return ArgInfo.DispatchID.getRegister();
228}
229
231 ArgInfo.FlatScratchInit = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
232 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
233 NumUserSGPRs += 2;
234 return ArgInfo.FlatScratchInit.getRegister();
235}
236
238 ArgInfo.PrivateSegmentSize = ArgDescriptor::createRegister(getNextUserSGPR());
239 NumUserSGPRs += 1;
240 return ArgInfo.PrivateSegmentSize.getRegister();
241}
242
244 ArgInfo.ImplicitBufferPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
245 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
246 NumUserSGPRs += 2;
247 return ArgInfo.ImplicitBufferPtr.getRegister();
248}
249
251 ArgInfo.LDSKernelId = ArgDescriptor::createRegister(getNextUserSGPR());
252 NumUserSGPRs += 1;
253 return ArgInfo.LDSKernelId.getRegister();
254}
255
257 const SIRegisterInfo &TRI, const TargetRegisterClass *RC,
258 unsigned AllocSizeDWord, int KernArgIdx, int PaddingSGPRs) {
259 assert(!ArgInfo.PreloadKernArgs.count(KernArgIdx) &&
260 "Preload kernel argument allocated twice.");
261 NumUserSGPRs += PaddingSGPRs;
262 // If the available register tuples are aligned with the kernarg to be
263 // preloaded use that register, otherwise we need to use a set of SGPRs and
264 // merge them.
265 Register PreloadReg =
266 TRI.getMatchingSuperReg(getNextUserSGPR(), AMDGPU::sub0, RC);
267 if (PreloadReg &&
268 (RC == &AMDGPU::SReg_32RegClass || RC == &AMDGPU::SReg_64RegClass)) {
269 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(PreloadReg);
270 NumUserSGPRs += AllocSizeDWord;
271 } else {
272 for (unsigned I = 0; I < AllocSizeDWord; ++I) {
273 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(getNextUserSGPR());
274 NumUserSGPRs++;
275 }
276 }
277
278 // Track the actual number of SGPRs that HW will preload to.
279 UserSGPRInfo.allocKernargPreloadSGPRs(AllocSizeDWord + PaddingSGPRs);
280 return &ArgInfo.PreloadKernArgs[KernArgIdx].Regs;
281}
282
284 uint64_t Size, Align Alignment) {
285 // Skip if it is an entry function or the register is already added.
286 if (isEntryFunction() || WWMSpills.count(VGPR))
287 return;
288
289 // Skip if this is a function with the amdgpu_cs_chain or
290 // amdgpu_cs_chain_preserve calling convention and this is a scratch register.
291 // We never need to allocate a spill for these because we don't even need to
292 // restore the inactive lanes for them (they're scratchier than the usual
293 // scratch registers). We only need to do this if we have calls to
294 // llvm.amdgcn.cs.chain (otherwise there's no one to save them for, since
295 // chain functions do not return) and the function did not contain a call to
296 // llvm.amdgcn.init.whole.wave (since in that case there are no inactive lanes
297 // when entering the function).
298 if (isChainFunction() &&
301 return;
302
303 WWMSpills.insert(std::make_pair(
304 VGPR, MF.getFrameInfo().CreateSpillStackObject(Size, Alignment)));
305}
306
307// Separate out the callee-saved and scratch registers.
309 MachineFunction &MF,
310 SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs,
311 SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const {
312 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
313 for (auto &Reg : WWMSpills) {
314 if (isCalleeSavedReg(CSRegs, Reg.first))
315 CalleeSavedRegs.push_back(Reg);
316 else
317 ScratchRegs.push_back(Reg);
318 }
319}
320
322 MCPhysReg Reg) const {
323 for (unsigned I = 0; CSRegs[I]; ++I) {
324 if (CSRegs[I] == Reg)
325 return true;
326 }
327
328 return false;
329}
330
333 BitVector &SavedVGPRs) {
334 const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
336 for (unsigned I = 0, E = WWMVGPRs.size(); I < E; ++I) {
337 Register Reg = WWMVGPRs[I];
338 Register NewReg =
339 TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF);
340 if (!NewReg || NewReg >= Reg)
341 break;
342
343 MRI.replaceRegWith(Reg, NewReg);
344
345 // Update various tables with the new VGPR.
346 WWMVGPRs[I] = NewReg;
347 WWMReservedRegs.remove(Reg);
348 WWMReservedRegs.insert(NewReg);
349 MRI.reserveReg(NewReg, TRI);
350
351 // Replace the register in SpillPhysVGPRs. This is needed to look for free
352 // lanes while spilling special SGPRs like FP, BP, etc. during PEI.
353 auto *RegItr = std::find(SpillPhysVGPRs.begin(), SpillPhysVGPRs.end(), Reg);
354 if (RegItr != SpillPhysVGPRs.end()) {
355 unsigned Idx = std::distance(SpillPhysVGPRs.begin(), RegItr);
356 SpillPhysVGPRs[Idx] = NewReg;
357 }
358
359 // The generic `determineCalleeSaves` might have set the old register if it
360 // is in the CSR range.
361 SavedVGPRs.reset(Reg);
362
363 for (MachineBasicBlock &MBB : MF) {
364 MBB.removeLiveIn(Reg);
366 }
367
368 Reg = NewReg;
369 }
370}
371
372bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills(
373 MachineFunction &MF, int FI, unsigned LaneIndex) {
375 Register LaneVGPR;
376 if (!LaneIndex) {
377 LaneVGPR = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
378 SpillVGPRs.push_back(LaneVGPR);
379 } else {
380 LaneVGPR = SpillVGPRs.back();
381 }
382
383 SGPRSpillsToVirtualVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
384 return true;
385}
386
387bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills(
388 MachineFunction &MF, int FI, unsigned LaneIndex, bool IsPrologEpilog) {
390 const SIRegisterInfo *TRI = ST.getRegisterInfo();
392 Register LaneVGPR;
393 if (!LaneIndex) {
394 // Find the highest available register if called before RA to ensure the
395 // lowest registers are available for allocation. The LaneVGPR, in that
396 // case, will be shifted back to the lowest range after VGPR allocation.
397 LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF,
398 !IsPrologEpilog);
399 if (LaneVGPR == AMDGPU::NoRegister) {
400 // We have no VGPRs left for spilling SGPRs. Reset because we will not
401 // partially spill the SGPR to VGPRs.
402 SGPRSpillsToPhysicalVGPRLanes.erase(FI);
403 return false;
404 }
405
406 if (IsPrologEpilog)
407 allocateWWMSpill(MF, LaneVGPR);
408
409 reserveWWMRegister(LaneVGPR);
410 for (MachineBasicBlock &MBB : MF) {
411 MBB.addLiveIn(LaneVGPR);
413 }
414 SpillPhysVGPRs.push_back(LaneVGPR);
415 } else {
416 LaneVGPR = SpillPhysVGPRs.back();
417 }
418
419 SGPRSpillsToPhysicalVGPRLanes[FI].emplace_back(LaneVGPR, LaneIndex);
420 return true;
421}
422
424 MachineFunction &MF, int FI, bool SpillToPhysVGPRLane,
425 bool IsPrologEpilog) {
426 std::vector<SIRegisterInfo::SpilledReg> &SpillLanes =
427 SpillToPhysVGPRLane ? SGPRSpillsToPhysicalVGPRLanes[FI]
428 : SGPRSpillsToVirtualVGPRLanes[FI];
429
430 // This has already been allocated.
431 if (!SpillLanes.empty())
432 return true;
433
434 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
435 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
436 unsigned WaveSize = ST.getWavefrontSize();
437
438 unsigned Size = FrameInfo.getObjectSize(FI);
439 unsigned NumLanes = Size / 4;
440
441 if (NumLanes > WaveSize)
442 return false;
443
444 assert(Size >= 4 && "invalid sgpr spill size");
445 assert(ST.getRegisterInfo()->spillSGPRToVGPR() &&
446 "not spilling SGPRs to VGPRs");
447
448 unsigned &NumSpillLanes = SpillToPhysVGPRLane ? NumPhysicalVGPRSpillLanes
449 : NumVirtualVGPRSpillLanes;
450
451 for (unsigned I = 0; I < NumLanes; ++I, ++NumSpillLanes) {
452 unsigned LaneIndex = (NumSpillLanes % WaveSize);
453
454 bool Allocated = SpillToPhysVGPRLane
455 ? allocatePhysicalVGPRForSGPRSpills(MF, FI, LaneIndex,
456 IsPrologEpilog)
457 : allocateVirtualVGPRForSGPRSpills(MF, FI, LaneIndex);
458 if (!Allocated) {
459 NumSpillLanes -= I;
460 return false;
461 }
462 }
463
464 return true;
465}
466
467/// Reserve AGPRs or VGPRs to support spilling for FrameIndex \p FI.
468/// Either AGPR is spilled to VGPR to vice versa.
469/// Returns true if a \p FI can be eliminated completely.
471 int FI,
472 bool isAGPRtoVGPR) {
474 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
475 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
476
477 assert(ST.hasMAIInsts() && FrameInfo.isSpillSlotObjectIndex(FI));
478
479 auto &Spill = VGPRToAGPRSpills[FI];
480
481 // This has already been allocated.
482 if (!Spill.Lanes.empty())
483 return Spill.FullyAllocated;
484
485 unsigned Size = FrameInfo.getObjectSize(FI);
486 unsigned NumLanes = Size / 4;
487 Spill.Lanes.resize(NumLanes, AMDGPU::NoRegister);
488
489 const TargetRegisterClass &RC =
490 isAGPRtoVGPR ? AMDGPU::VGPR_32RegClass : AMDGPU::AGPR_32RegClass;
491 auto Regs = RC.getRegisters();
492
493 auto &SpillRegs = isAGPRtoVGPR ? SpillAGPR : SpillVGPR;
494 const SIRegisterInfo *TRI = ST.getRegisterInfo();
495 Spill.FullyAllocated = true;
496
497 // FIXME: Move allocation logic out of MachineFunctionInfo and initialize
498 // once.
499 BitVector OtherUsedRegs;
500 OtherUsedRegs.resize(TRI->getNumRegs());
501
502 const uint32_t *CSRMask =
503 TRI->getCallPreservedMask(MF, MF.getFunction().getCallingConv());
504 if (CSRMask)
505 OtherUsedRegs.setBitsInMask(CSRMask);
506
507 // TODO: Should include register tuples, but doesn't matter with current
508 // usage.
509 for (MCPhysReg Reg : SpillAGPR)
510 OtherUsedRegs.set(Reg);
511 for (MCPhysReg Reg : SpillVGPR)
512 OtherUsedRegs.set(Reg);
513
514 SmallVectorImpl<MCPhysReg>::const_iterator NextSpillReg = Regs.begin();
515 for (int I = NumLanes - 1; I >= 0; --I) {
516 NextSpillReg = std::find_if(
517 NextSpillReg, Regs.end(), [&MRI, &OtherUsedRegs](MCPhysReg Reg) {
518 return MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg) &&
519 !OtherUsedRegs[Reg];
520 });
521
522 if (NextSpillReg == Regs.end()) { // Registers exhausted
523 Spill.FullyAllocated = false;
524 break;
525 }
526
527 OtherUsedRegs.set(*NextSpillReg);
528 SpillRegs.push_back(*NextSpillReg);
529 MRI.reserveReg(*NextSpillReg, TRI);
530 Spill.Lanes[I] = *NextSpillReg++;
531 }
532
533 return Spill.FullyAllocated;
534}
535
537 MachineFrameInfo &MFI, bool ResetSGPRSpillStackIDs) {
538 // Remove dead frame indices from function frame, however keep FP & BP since
539 // spills for them haven't been inserted yet. And also make sure to remove the
540 // frame indices from `SGPRSpillsToVirtualVGPRLanes` data structure,
541 // otherwise, it could result in an unexpected side effect and bug, in case of
542 // any re-mapping of freed frame indices by later pass(es) like "stack slot
543 // coloring".
544 for (auto &R : make_early_inc_range(SGPRSpillsToVirtualVGPRLanes)) {
545 MFI.RemoveStackObject(R.first);
546 SGPRSpillsToVirtualVGPRLanes.erase(R.first);
547 }
548
549 // Remove the dead frame indices of CSR SGPRs which are spilled to physical
550 // VGPR lanes during SILowerSGPRSpills pass.
551 if (!ResetSGPRSpillStackIDs) {
552 for (auto &R : make_early_inc_range(SGPRSpillsToPhysicalVGPRLanes)) {
553 MFI.RemoveStackObject(R.first);
554 SGPRSpillsToPhysicalVGPRLanes.erase(R.first);
555 }
556 }
557 bool HaveSGPRToMemory = false;
558
559 if (ResetSGPRSpillStackIDs) {
560 // All other SGPRs must be allocated on the default stack, so reset the
561 // stack ID.
562 for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); I != E;
563 ++I) {
567 HaveSGPRToMemory = true;
568 }
569 }
570 }
571 }
572
573 for (auto &R : VGPRToAGPRSpills) {
574 if (R.second.IsDead)
575 MFI.RemoveStackObject(R.first);
576 }
577
578 return HaveSGPRToMemory;
579}
580
582 const SIRegisterInfo &TRI) {
583 if (ScavengeFI)
584 return *ScavengeFI;
585
586 ScavengeFI =
587 MFI.CreateStackObject(TRI.getSpillSize(AMDGPU::SGPR_32RegClass),
588 TRI.getSpillAlign(AMDGPU::SGPR_32RegClass), false);
589 return *ScavengeFI;
590}
591
592MCPhysReg SIMachineFunctionInfo::getNextUserSGPR() const {
593 assert(NumSystemSGPRs == 0 && "System SGPRs must be added after user SGPRs");
594 return AMDGPU::SGPR0 + NumUserSGPRs;
595}
596
597MCPhysReg SIMachineFunctionInfo::getNextSystemSGPR() const {
598 return AMDGPU::SGPR0 + NumUserSGPRs + NumSystemSGPRs;
599}
600
601void SIMachineFunctionInfo::MRI_NoteNewVirtualRegister(Register Reg) {
602 VRegFlags.grow(Reg);
603}
604
605void SIMachineFunctionInfo::MRI_NoteCloneVirtualRegister(Register NewReg,
606 Register SrcReg) {
607 VRegFlags.grow(NewReg);
608 VRegFlags[NewReg] = VRegFlags[SrcReg];
609}
610
613 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
614 if (!ST.isAmdPalOS())
615 return Register();
616 Register GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in
617 if (ST.hasMergedShaders()) {
618 switch (MF.getFunction().getCallingConv()) {
621 // Low GIT address is passed in s8 rather than s0 for an LS+HS or
622 // ES+GS merged shader on gfx9+.
623 GitPtrLo = AMDGPU::SGPR8;
624 return GitPtrLo;
625 default:
626 return GitPtrLo;
627 }
628 }
629 return GitPtrLo;
630}
631
633 const TargetRegisterInfo &TRI) {
635 {
637 OS << printReg(Reg, &TRI);
638 }
639 return Dest;
640}
641
642static std::optional<yaml::SIArgumentInfo>
644 const TargetRegisterInfo &TRI) {
646
647 auto convertArg = [&](std::optional<yaml::SIArgument> &A,
648 const ArgDescriptor &Arg) {
649 if (!Arg)
650 return false;
651
652 // Create a register or stack argument.
654 if (Arg.isRegister()) {
656 OS << printReg(Arg.getRegister(), &TRI);
657 } else
658 SA.StackOffset = Arg.getStackOffset();
659 // Check and update the optional mask.
660 if (Arg.isMasked())
661 SA.Mask = Arg.getMask();
662
663 A = SA;
664 return true;
665 };
666
667 // TODO: Need to serialize kernarg preloads.
668 bool Any = false;
669 Any |= convertArg(AI.PrivateSegmentBuffer, ArgInfo.PrivateSegmentBuffer);
670 Any |= convertArg(AI.DispatchPtr, ArgInfo.DispatchPtr);
671 Any |= convertArg(AI.QueuePtr, ArgInfo.QueuePtr);
672 Any |= convertArg(AI.KernargSegmentPtr, ArgInfo.KernargSegmentPtr);
673 Any |= convertArg(AI.DispatchID, ArgInfo.DispatchID);
674 Any |= convertArg(AI.FlatScratchInit, ArgInfo.FlatScratchInit);
675 Any |= convertArg(AI.LDSKernelId, ArgInfo.LDSKernelId);
676 Any |= convertArg(AI.PrivateSegmentSize, ArgInfo.PrivateSegmentSize);
677 Any |= convertArg(AI.WorkGroupIDX, ArgInfo.WorkGroupIDX);
678 Any |= convertArg(AI.WorkGroupIDY, ArgInfo.WorkGroupIDY);
679 Any |= convertArg(AI.WorkGroupIDZ, ArgInfo.WorkGroupIDZ);
680 Any |= convertArg(AI.WorkGroupInfo, ArgInfo.WorkGroupInfo);
681 Any |= convertArg(AI.PrivateSegmentWaveByteOffset,
682 ArgInfo.PrivateSegmentWaveByteOffset);
683 Any |= convertArg(AI.ImplicitArgPtr, ArgInfo.ImplicitArgPtr);
684 Any |= convertArg(AI.ImplicitBufferPtr, ArgInfo.ImplicitBufferPtr);
685 Any |= convertArg(AI.WorkItemIDX, ArgInfo.WorkItemIDX);
686 Any |= convertArg(AI.WorkItemIDY, ArgInfo.WorkItemIDY);
687 Any |= convertArg(AI.WorkItemIDZ, ArgInfo.WorkItemIDZ);
688
689 if (Any)
690 return AI;
691
692 return std::nullopt;
693}
694
697 const llvm::MachineFunction &MF)
698 : ExplicitKernArgSize(MFI.getExplicitKernArgSize()),
699 MaxKernArgAlign(MFI.getMaxKernArgAlign()), LDSSize(MFI.getLDSSize()),
700 GDSSize(MFI.getGDSSize()), DynLDSAlign(MFI.getDynLDSAlign()),
701 IsEntryFunction(MFI.isEntryFunction()),
702 NoSignedZerosFPMath(MFI.hasNoSignedZerosFPMath()),
703 MemoryBound(MFI.isMemoryBound()), WaveLimiter(MFI.needsWaveLimiter()),
704 HasSpilledSGPRs(MFI.hasSpilledSGPRs()),
705 HasSpilledVGPRs(MFI.hasSpilledVGPRs()),
706 HighBitsOf32BitAddress(MFI.get32BitAddressHighBits()),
707 Occupancy(MFI.getOccupancy()),
708 ScratchRSrcReg(regToString(MFI.getScratchRSrcReg(), TRI)),
709 FrameOffsetReg(regToString(MFI.getFrameOffsetReg(), TRI)),
710 StackPtrOffsetReg(regToString(MFI.getStackPtrOffsetReg(), TRI)),
711 BytesInStackArgArea(MFI.getBytesInStackArgArea()),
712 ReturnsVoid(MFI.returnsVoid()),
713 ArgInfo(convertArgumentInfo(MFI.getArgInfo(), TRI)),
714 PSInputAddr(MFI.getPSInputAddr()), PSInputEnable(MFI.getPSInputEnable()),
715 MaxMemoryClusterDWords(MFI.getMaxMemoryClusterDWords()),
716 Mode(MFI.getMode()) {
717 for (Register Reg : MFI.getSGPRSpillPhysVGPRs())
718 SpillPhysVGPRS.push_back(regToString(Reg, TRI));
719
720 for (Register Reg : MFI.getWWMReservedRegs())
721 WWMReservedRegs.push_back(regToString(Reg, TRI));
722
723 if (MFI.getLongBranchReservedReg())
725 if (MFI.getVGPRForAGPRCopy())
727
728 if (MFI.getSGPRForEXECCopy())
730
731 auto SFI = MFI.getOptionalScavengeFI();
732 if (SFI)
734}
735
738}
739
741 const yaml::SIMachineFunctionInfo &YamlMFI, const MachineFunction &MF,
745 LDSSize = YamlMFI.LDSSize;
746 GDSSize = YamlMFI.GDSSize;
747 DynLDSAlign = YamlMFI.DynLDSAlign;
748 PSInputAddr = YamlMFI.PSInputAddr;
752 Occupancy = YamlMFI.Occupancy;
755 MemoryBound = YamlMFI.MemoryBound;
756 WaveLimiter = YamlMFI.WaveLimiter;
760 ReturnsVoid = YamlMFI.ReturnsVoid;
761
762 if (YamlMFI.ScavengeFI) {
763 auto FIOrErr = YamlMFI.ScavengeFI->getFI(MF.getFrameInfo());
764 if (!FIOrErr) {
765 // Create a diagnostic for a the frame index.
766 const MemoryBuffer &Buffer =
767 *PFS.SM->getMemoryBuffer(PFS.SM->getMainFileID());
768
769 Error = SMDiagnostic(*PFS.SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 1,
770 SourceMgr::DK_Error, toString(FIOrErr.takeError()),
771 "", {}, {});
772 SourceRange = YamlMFI.ScavengeFI->SourceRange;
773 return true;
774 }
775 ScavengeFI = *FIOrErr;
776 } else {
777 ScavengeFI = std::nullopt;
778 }
779 return false;
780}
781
783 return !F.hasFnAttribute("amdgpu-no-agpr");
784}
785
787 if (UsesAGPRs)
788 return *UsesAGPRs;
789
790 if (!mayNeedAGPRs()) {
791 UsesAGPRs = false;
792 return false;
793 }
794
796 MF.getFrameInfo().hasCalls()) {
797 UsesAGPRs = true;
798 return true;
799 }
800
801 const MachineRegisterInfo &MRI = MF.getRegInfo();
802
803 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
805 const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
806 if (RC && SIRegisterInfo::isAGPRClass(RC)) {
807 UsesAGPRs = true;
808 return true;
809 }
810 if (!RC && !MRI.use_empty(Reg) && MRI.getType(Reg).isValid()) {
811 // Defer caching UsesAGPRs, function might not yet been regbank selected.
812 return true;
813 }
814 }
815
816 for (MCRegister Reg : AMDGPU::AGPR_32RegClass) {
817 if (MRI.isPhysRegUsed(Reg)) {
818 UsesAGPRs = true;
819 return true;
820 }
821 }
822
823 UsesAGPRs = false;
824 return false;
825}
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:1312
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.