LLVM 19.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 "AMDGPUTargetMachine.h"
12#include "GCNSubtarget.h"
14#include "SIRegisterInfo.h"
22#include "llvm/IR/CallingConv.h"
24#include "llvm/IR/Function.h"
25#include <cassert>
26#include <optional>
27#include <vector>
28
29#define MAX_LANES 64
30
31using namespace llvm;
32
34 const SITargetLowering *TLI = STI->getTargetLowering();
35 return static_cast<const GCNTargetMachine &>(TLI->getTargetMachine());
36}
37
39 const GCNSubtarget *STI)
40 : AMDGPUMachineFunction(F, *STI), Mode(F, *STI), GWSResourcePSV(getTM(STI)),
41 UserSGPRInfo(F, *STI), WorkGroupIDX(false), WorkGroupIDY(false),
42 WorkGroupIDZ(false), WorkGroupInfo(false), LDSKernelId(false),
43 PrivateSegmentWaveByteOffset(false), WorkItemIDX(false),
44 WorkItemIDY(false), WorkItemIDZ(false), ImplicitArgPtr(false),
45 GITPtrHigh(0xffffffff), HighBitsOf32BitAddress(0) {
46 const GCNSubtarget &ST = *static_cast<const GCNSubtarget *>(STI);
47 FlatWorkGroupSizes = ST.getFlatWorkGroupSizes(F);
48 WavesPerEU = ST.getWavesPerEU(F);
49 MaxNumWorkGroups = ST.getMaxNumWorkGroups(F);
50 assert(MaxNumWorkGroups.size() == 3);
51
52 Occupancy = ST.computeOccupancy(F, getLDSSize());
53 CallingConv::ID CC = F.getCallingConv();
54
55 VRegFlags.reserve(1024);
56
57 const bool IsKernel = CC == CallingConv::AMDGPU_KERNEL ||
59
60 if (IsKernel) {
61 WorkGroupIDX = true;
62 WorkItemIDX = true;
63 } else if (CC == CallingConv::AMDGPU_PS) {
64 PSInputAddr = AMDGPU::getInitialPSInputAddr(F);
65 }
66
67 MayNeedAGPRs = ST.hasMAIInsts();
68
69 if (AMDGPU::isChainCC(CC)) {
70 // Chain functions don't receive an SP from their caller, but are free to
71 // set one up. For now, we can use s32 to match what amdgpu_gfx functions
72 // would use if called, but this can be revisited.
73 // FIXME: Only reserve this if we actually need it.
74 StackPtrOffsetReg = AMDGPU::SGPR32;
75
76 ScratchRSrcReg = AMDGPU::SGPR48_SGPR49_SGPR50_SGPR51;
77
78 ArgInfo.PrivateSegmentBuffer =
79 ArgDescriptor::createRegister(ScratchRSrcReg);
80
81 ImplicitArgPtr = false;
82 } else if (!isEntryFunction()) {
85
86 FrameOffsetReg = AMDGPU::SGPR33;
87 StackPtrOffsetReg = AMDGPU::SGPR32;
88
89 if (!ST.enableFlatScratch()) {
90 // Non-entry functions have no special inputs for now, other registers
91 // required for scratch access.
92 ScratchRSrcReg = AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3;
93
94 ArgInfo.PrivateSegmentBuffer =
95 ArgDescriptor::createRegister(ScratchRSrcReg);
96 }
97
98 if (!F.hasFnAttribute("amdgpu-no-implicitarg-ptr"))
99 ImplicitArgPtr = true;
100 } else {
101 ImplicitArgPtr = false;
102 MaxKernArgAlign = std::max(ST.getAlignmentForImplicitArgPtr(),
104
105 if (ST.hasGFX90AInsts() &&
106 ST.getMaxNumVGPRs(F) <= AMDGPU::VGPR_32RegClass.getNumRegs() &&
107 !mayUseAGPRs(F))
108 MayNeedAGPRs = false; // We will select all MAI with VGPR operands.
109 }
110
111 if (!AMDGPU::isGraphics(CC) ||
113 ST.hasArchitectedSGPRs())) {
114 if (IsKernel || !F.hasFnAttribute("amdgpu-no-workgroup-id-x"))
115 WorkGroupIDX = true;
116
117 if (!F.hasFnAttribute("amdgpu-no-workgroup-id-y"))
118 WorkGroupIDY = true;
119
120 if (!F.hasFnAttribute("amdgpu-no-workgroup-id-z"))
121 WorkGroupIDZ = true;
122 }
123
124 if (!AMDGPU::isGraphics(CC)) {
125 if (IsKernel || !F.hasFnAttribute("amdgpu-no-workitem-id-x"))
126 WorkItemIDX = true;
127
128 if (!F.hasFnAttribute("amdgpu-no-workitem-id-y") &&
129 ST.getMaxWorkitemID(F, 1) != 0)
130 WorkItemIDY = true;
131
132 if (!F.hasFnAttribute("amdgpu-no-workitem-id-z") &&
133 ST.getMaxWorkitemID(F, 2) != 0)
134 WorkItemIDZ = true;
135
136 if (!IsKernel && !F.hasFnAttribute("amdgpu-no-lds-kernel-id"))
137 LDSKernelId = true;
138 }
139
140 if (isEntryFunction()) {
141 // X, XY, and XYZ are the only supported combinations, so make sure Y is
142 // enabled if Z is.
143 if (WorkItemIDZ)
144 WorkItemIDY = true;
145
146 if (!ST.flatScratchIsArchitected()) {
147 PrivateSegmentWaveByteOffset = true;
148
149 // HS and GS always have the scratch wave offset in SGPR5 on GFX9.
150 if (ST.getGeneration() >= AMDGPUSubtarget::GFX9 &&
152 ArgInfo.PrivateSegmentWaveByteOffset =
153 ArgDescriptor::createRegister(AMDGPU::SGPR5);
154 }
155 }
156
157 Attribute A = F.getFnAttribute("amdgpu-git-ptr-high");
158 StringRef S = A.getValueAsString();
159 if (!S.empty())
160 S.consumeInteger(0, GITPtrHigh);
161
162 A = F.getFnAttribute("amdgpu-32bit-address-high-bits");
163 S = A.getValueAsString();
164 if (!S.empty())
165 S.consumeInteger(0, HighBitsOf32BitAddress);
166
167 // On GFX908, in order to guarantee copying between AGPRs, we need a scratch
168 // VGPR available at all times. For now, reserve highest available VGPR. After
169 // RA, shift it to the lowest available unused VGPR if the one exist.
170 if (ST.hasMAIInsts() && !ST.hasGFX90AInsts()) {
171 VGPRForAGPRCopy =
172 AMDGPU::VGPR_32RegClass.getRegister(ST.getMaxNumVGPRs(F) - 1);
173 }
174}
175
177 BumpPtrAllocator &Allocator, MachineFunction &DestMF,
179 const {
180 return DestMF.cloneInfo<SIMachineFunctionInfo>(*this);
181}
182
185 const GCNSubtarget& ST = MF.getSubtarget<GCNSubtarget>();
186 limitOccupancy(ST.getOccupancyWithLocalMemSize(getLDSSize(),
187 MF.getFunction()));
188}
189
191 const SIRegisterInfo &TRI) {
192 ArgInfo.PrivateSegmentBuffer =
193 ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
194 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SGPR_128RegClass));
195 NumUserSGPRs += 4;
196 return ArgInfo.PrivateSegmentBuffer.getRegister();
197}
198
200 ArgInfo.DispatchPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
201 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
202 NumUserSGPRs += 2;
203 return ArgInfo.DispatchPtr.getRegister();
204}
205
207 ArgInfo.QueuePtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
208 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
209 NumUserSGPRs += 2;
210 return ArgInfo.QueuePtr.getRegister();
211}
212
214 ArgInfo.KernargSegmentPtr
215 = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
216 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
217 NumUserSGPRs += 2;
218 return ArgInfo.KernargSegmentPtr.getRegister();
219}
220
222 ArgInfo.DispatchID = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
223 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
224 NumUserSGPRs += 2;
225 return ArgInfo.DispatchID.getRegister();
226}
227
229 ArgInfo.FlatScratchInit = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
230 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
231 NumUserSGPRs += 2;
232 return ArgInfo.FlatScratchInit.getRegister();
233}
234
236 ArgInfo.PrivateSegmentSize = ArgDescriptor::createRegister(getNextUserSGPR());
237 NumUserSGPRs += 1;
238 return ArgInfo.PrivateSegmentSize.getRegister();
239}
240
242 ArgInfo.ImplicitBufferPtr = ArgDescriptor::createRegister(TRI.getMatchingSuperReg(
243 getNextUserSGPR(), AMDGPU::sub0, &AMDGPU::SReg_64RegClass));
244 NumUserSGPRs += 2;
245 return ArgInfo.ImplicitBufferPtr.getRegister();
246}
247
249 ArgInfo.LDSKernelId = ArgDescriptor::createRegister(getNextUserSGPR());
250 NumUserSGPRs += 1;
251 return ArgInfo.LDSKernelId.getRegister();
252}
253
255 const SIRegisterInfo &TRI, const TargetRegisterClass *RC,
256 unsigned AllocSizeDWord, int KernArgIdx, int PaddingSGPRs) {
257 assert(!ArgInfo.PreloadKernArgs.count(KernArgIdx) &&
258 "Preload kernel argument allocated twice.");
259 NumUserSGPRs += PaddingSGPRs;
260 // If the available register tuples are aligned with the kernarg to be
261 // preloaded use that register, otherwise we need to use a set of SGPRs and
262 // merge them.
263 Register PreloadReg =
264 TRI.getMatchingSuperReg(getNextUserSGPR(), AMDGPU::sub0, RC);
265 if (PreloadReg &&
266 (RC == &AMDGPU::SReg_32RegClass || RC == &AMDGPU::SReg_64RegClass)) {
267 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(PreloadReg);
268 NumUserSGPRs += AllocSizeDWord;
269 } else {
270 for (unsigned I = 0; I < AllocSizeDWord; ++I) {
271 ArgInfo.PreloadKernArgs[KernArgIdx].Regs.push_back(getNextUserSGPR());
272 NumUserSGPRs++;
273 }
274 }
275
276 // Track the actual number of SGPRs that HW will preload to.
277 UserSGPRInfo.allocKernargPreloadSGPRs(AllocSizeDWord + PaddingSGPRs);
278 return &ArgInfo.PreloadKernArgs[KernArgIdx].Regs;
279}
280
282 uint64_t Size, Align Alignment) {
283 // Skip if it is an entry function or the register is already added.
284 if (isEntryFunction() || WWMSpills.count(VGPR))
285 return;
286
287 // Skip if this is a function with the amdgpu_cs_chain or
288 // amdgpu_cs_chain_preserve calling convention and this is a scratch register.
289 // We never need to allocate a spill for these because we don't even need to
290 // restore the inactive lanes for them (they're scratchier than the usual
291 // scratch registers).
293 return;
294
295 WWMSpills.insert(std::make_pair(
296 VGPR, MF.getFrameInfo().CreateSpillStackObject(Size, Alignment)));
297}
298
299// Separate out the callee-saved and scratch registers.
301 MachineFunction &MF,
302 SmallVectorImpl<std::pair<Register, int>> &CalleeSavedRegs,
303 SmallVectorImpl<std::pair<Register, int>> &ScratchRegs) const {
304 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
305 for (auto &Reg : WWMSpills) {
306 if (isCalleeSavedReg(CSRegs, Reg.first))
307 CalleeSavedRegs.push_back(Reg);
308 else
309 ScratchRegs.push_back(Reg);
310 }
311}
312
314 MCPhysReg Reg) const {
315 for (unsigned I = 0; CSRegs[I]; ++I) {
316 if (CSRegs[I] == Reg)
317 return true;
318 }
319
320 return false;
321}
322
324 MachineFunction &MF) {
325 const SIRegisterInfo *TRI = MF.getSubtarget<GCNSubtarget>().getRegisterInfo();
327 for (unsigned I = 0, E = SpillPhysVGPRs.size(); I < E; ++I) {
328 Register Reg = SpillPhysVGPRs[I];
329 Register NewReg =
330 TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF);
331 if (!NewReg || NewReg >= Reg)
332 break;
333
334 MRI.replaceRegWith(Reg, NewReg);
335
336 // Update various tables with the new VGPR.
337 SpillPhysVGPRs[I] = NewReg;
338 WWMReservedRegs.remove(Reg);
339 WWMReservedRegs.insert(NewReg);
340 WWMSpills.insert(std::make_pair(NewReg, WWMSpills[Reg]));
341 WWMSpills.erase(Reg);
342
343 for (MachineBasicBlock &MBB : MF) {
344 MBB.removeLiveIn(Reg);
346 }
347 }
348}
349
350bool SIMachineFunctionInfo::allocateVirtualVGPRForSGPRSpills(
351 MachineFunction &MF, int FI, unsigned LaneIndex) {
353 Register LaneVGPR;
354 if (!LaneIndex) {
355 LaneVGPR = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
356 SpillVGPRs.push_back(LaneVGPR);
357 } else {
358 LaneVGPR = SpillVGPRs.back();
359 }
360
361 SGPRSpillsToVirtualVGPRLanes[FI].push_back(
362 SIRegisterInfo::SpilledReg(LaneVGPR, LaneIndex));
363 return true;
364}
365
366bool SIMachineFunctionInfo::allocatePhysicalVGPRForSGPRSpills(
367 MachineFunction &MF, int FI, unsigned LaneIndex, bool IsPrologEpilog) {
369 const SIRegisterInfo *TRI = ST.getRegisterInfo();
371 Register LaneVGPR;
372 if (!LaneIndex) {
373 // Find the highest available register if called before RA to ensure the
374 // lowest registers are available for allocation. The LaneVGPR, in that
375 // case, will be shifted back to the lowest range after VGPR allocation.
376 LaneVGPR = TRI->findUnusedRegister(MRI, &AMDGPU::VGPR_32RegClass, MF,
377 !IsPrologEpilog);
378 if (LaneVGPR == AMDGPU::NoRegister) {
379 // We have no VGPRs left for spilling SGPRs. Reset because we will not
380 // partially spill the SGPR to VGPRs.
381 SGPRSpillsToPhysicalVGPRLanes.erase(FI);
382 return false;
383 }
384
385 allocateWWMSpill(MF, LaneVGPR);
386 reserveWWMRegister(LaneVGPR);
387 for (MachineBasicBlock &MBB : MF) {
388 MBB.addLiveIn(LaneVGPR);
390 }
391 SpillPhysVGPRs.push_back(LaneVGPR);
392 } else {
393 LaneVGPR = SpillPhysVGPRs.back();
394 }
395
396 SGPRSpillsToPhysicalVGPRLanes[FI].push_back(
397 SIRegisterInfo::SpilledReg(LaneVGPR, LaneIndex));
398 return true;
399}
400
402 MachineFunction &MF, int FI, bool SpillToPhysVGPRLane,
403 bool IsPrologEpilog) {
404 std::vector<SIRegisterInfo::SpilledReg> &SpillLanes =
405 SpillToPhysVGPRLane ? SGPRSpillsToPhysicalVGPRLanes[FI]
406 : SGPRSpillsToVirtualVGPRLanes[FI];
407
408 // This has already been allocated.
409 if (!SpillLanes.empty())
410 return true;
411
412 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
413 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
414 unsigned WaveSize = ST.getWavefrontSize();
415
416 unsigned Size = FrameInfo.getObjectSize(FI);
417 unsigned NumLanes = Size / 4;
418
419 if (NumLanes > WaveSize)
420 return false;
421
422 assert(Size >= 4 && "invalid sgpr spill size");
423 assert(ST.getRegisterInfo()->spillSGPRToVGPR() &&
424 "not spilling SGPRs to VGPRs");
425
426 unsigned &NumSpillLanes = SpillToPhysVGPRLane ? NumPhysicalVGPRSpillLanes
427 : NumVirtualVGPRSpillLanes;
428
429 for (unsigned I = 0; I < NumLanes; ++I, ++NumSpillLanes) {
430 unsigned LaneIndex = (NumSpillLanes % WaveSize);
431
432 bool Allocated = SpillToPhysVGPRLane
433 ? allocatePhysicalVGPRForSGPRSpills(MF, FI, LaneIndex,
434 IsPrologEpilog)
435 : allocateVirtualVGPRForSGPRSpills(MF, FI, LaneIndex);
436 if (!Allocated) {
437 NumSpillLanes -= I;
438 return false;
439 }
440 }
441
442 return true;
443}
444
445/// Reserve AGPRs or VGPRs to support spilling for FrameIndex \p FI.
446/// Either AGPR is spilled to VGPR to vice versa.
447/// Returns true if a \p FI can be eliminated completely.
449 int FI,
450 bool isAGPRtoVGPR) {
452 MachineFrameInfo &FrameInfo = MF.getFrameInfo();
453 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
454
455 assert(ST.hasMAIInsts() && FrameInfo.isSpillSlotObjectIndex(FI));
456
457 auto &Spill = VGPRToAGPRSpills[FI];
458
459 // This has already been allocated.
460 if (!Spill.Lanes.empty())
461 return Spill.FullyAllocated;
462
463 unsigned Size = FrameInfo.getObjectSize(FI);
464 unsigned NumLanes = Size / 4;
465 Spill.Lanes.resize(NumLanes, AMDGPU::NoRegister);
466
467 const TargetRegisterClass &RC =
468 isAGPRtoVGPR ? AMDGPU::VGPR_32RegClass : AMDGPU::AGPR_32RegClass;
469 auto Regs = RC.getRegisters();
470
471 auto &SpillRegs = isAGPRtoVGPR ? SpillAGPR : SpillVGPR;
472 const SIRegisterInfo *TRI = ST.getRegisterInfo();
473 Spill.FullyAllocated = true;
474
475 // FIXME: Move allocation logic out of MachineFunctionInfo and initialize
476 // once.
477 BitVector OtherUsedRegs;
478 OtherUsedRegs.resize(TRI->getNumRegs());
479
480 const uint32_t *CSRMask =
481 TRI->getCallPreservedMask(MF, MF.getFunction().getCallingConv());
482 if (CSRMask)
483 OtherUsedRegs.setBitsInMask(CSRMask);
484
485 // TODO: Should include register tuples, but doesn't matter with current
486 // usage.
487 for (MCPhysReg Reg : SpillAGPR)
488 OtherUsedRegs.set(Reg);
489 for (MCPhysReg Reg : SpillVGPR)
490 OtherUsedRegs.set(Reg);
491
492 SmallVectorImpl<MCPhysReg>::const_iterator NextSpillReg = Regs.begin();
493 for (int I = NumLanes - 1; I >= 0; --I) {
494 NextSpillReg = std::find_if(
495 NextSpillReg, Regs.end(), [&MRI, &OtherUsedRegs](MCPhysReg Reg) {
496 return MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg) &&
497 !OtherUsedRegs[Reg];
498 });
499
500 if (NextSpillReg == Regs.end()) { // Registers exhausted
501 Spill.FullyAllocated = false;
502 break;
503 }
504
505 OtherUsedRegs.set(*NextSpillReg);
506 SpillRegs.push_back(*NextSpillReg);
507 MRI.reserveReg(*NextSpillReg, TRI);
508 Spill.Lanes[I] = *NextSpillReg++;
509 }
510
511 return Spill.FullyAllocated;
512}
513
515 MachineFrameInfo &MFI, bool ResetSGPRSpillStackIDs) {
516 // Remove dead frame indices from function frame, however keep FP & BP since
517 // spills for them haven't been inserted yet. And also make sure to remove the
518 // frame indices from `SGPRSpillsToVirtualVGPRLanes` data structure,
519 // otherwise, it could result in an unexpected side effect and bug, in case of
520 // any re-mapping of freed frame indices by later pass(es) like "stack slot
521 // coloring".
522 for (auto &R : make_early_inc_range(SGPRSpillsToVirtualVGPRLanes)) {
523 MFI.RemoveStackObject(R.first);
524 SGPRSpillsToVirtualVGPRLanes.erase(R.first);
525 }
526
527 // Remove the dead frame indices of CSR SGPRs which are spilled to physical
528 // VGPR lanes during SILowerSGPRSpills pass.
529 if (!ResetSGPRSpillStackIDs) {
530 for (auto &R : make_early_inc_range(SGPRSpillsToPhysicalVGPRLanes)) {
531 MFI.RemoveStackObject(R.first);
532 SGPRSpillsToPhysicalVGPRLanes.erase(R.first);
533 }
534 }
535 bool HaveSGPRToMemory = false;
536
537 if (ResetSGPRSpillStackIDs) {
538 // All other SGPRs must be allocated on the default stack, so reset the
539 // stack ID.
540 for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd(); I != E;
541 ++I) {
545 HaveSGPRToMemory = true;
546 }
547 }
548 }
549 }
550
551 for (auto &R : VGPRToAGPRSpills) {
552 if (R.second.IsDead)
553 MFI.RemoveStackObject(R.first);
554 }
555
556 return HaveSGPRToMemory;
557}
558
560 const SIRegisterInfo &TRI) {
561 if (ScavengeFI)
562 return *ScavengeFI;
563
564 ScavengeFI =
565 MFI.CreateStackObject(TRI.getSpillSize(AMDGPU::SGPR_32RegClass),
566 TRI.getSpillAlign(AMDGPU::SGPR_32RegClass), false);
567 return *ScavengeFI;
568}
569
570MCPhysReg SIMachineFunctionInfo::getNextUserSGPR() const {
571 assert(NumSystemSGPRs == 0 && "System SGPRs must be added after user SGPRs");
572 return AMDGPU::SGPR0 + NumUserSGPRs;
573}
574
575MCPhysReg SIMachineFunctionInfo::getNextSystemSGPR() const {
576 return AMDGPU::SGPR0 + NumUserSGPRs + NumSystemSGPRs;
577}
578
579void SIMachineFunctionInfo::MRI_NoteNewVirtualRegister(Register Reg) {
580 VRegFlags.grow(Reg);
581}
582
583void SIMachineFunctionInfo::MRI_NoteCloneVirtualRegister(Register NewReg,
584 Register SrcReg) {
585 VRegFlags.grow(NewReg);
586 VRegFlags[NewReg] = VRegFlags[SrcReg];
587}
588
591 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
592 if (!ST.isAmdPalOS())
593 return Register();
594 Register GitPtrLo = AMDGPU::SGPR0; // Low GIT address passed in
595 if (ST.hasMergedShaders()) {
596 switch (MF.getFunction().getCallingConv()) {
599 // Low GIT address is passed in s8 rather than s0 for an LS+HS or
600 // ES+GS merged shader on gfx9+.
601 GitPtrLo = AMDGPU::SGPR8;
602 return GitPtrLo;
603 default:
604 return GitPtrLo;
605 }
606 }
607 return GitPtrLo;
608}
609
611 const TargetRegisterInfo &TRI) {
613 {
615 OS << printReg(Reg, &TRI);
616 }
617 return Dest;
618}
619
620static std::optional<yaml::SIArgumentInfo>
622 const TargetRegisterInfo &TRI) {
624
625 auto convertArg = [&](std::optional<yaml::SIArgument> &A,
626 const ArgDescriptor &Arg) {
627 if (!Arg)
628 return false;
629
630 // Create a register or stack argument.
632 if (Arg.isRegister()) {
634 OS << printReg(Arg.getRegister(), &TRI);
635 } else
636 SA.StackOffset = Arg.getStackOffset();
637 // Check and update the optional mask.
638 if (Arg.isMasked())
639 SA.Mask = Arg.getMask();
640
641 A = SA;
642 return true;
643 };
644
645 // TODO: Need to serialize kernarg preloads.
646 bool Any = false;
647 Any |= convertArg(AI.PrivateSegmentBuffer, ArgInfo.PrivateSegmentBuffer);
648 Any |= convertArg(AI.DispatchPtr, ArgInfo.DispatchPtr);
649 Any |= convertArg(AI.QueuePtr, ArgInfo.QueuePtr);
650 Any |= convertArg(AI.KernargSegmentPtr, ArgInfo.KernargSegmentPtr);
651 Any |= convertArg(AI.DispatchID, ArgInfo.DispatchID);
652 Any |= convertArg(AI.FlatScratchInit, ArgInfo.FlatScratchInit);
653 Any |= convertArg(AI.LDSKernelId, ArgInfo.LDSKernelId);
654 Any |= convertArg(AI.PrivateSegmentSize, ArgInfo.PrivateSegmentSize);
655 Any |= convertArg(AI.WorkGroupIDX, ArgInfo.WorkGroupIDX);
656 Any |= convertArg(AI.WorkGroupIDY, ArgInfo.WorkGroupIDY);
657 Any |= convertArg(AI.WorkGroupIDZ, ArgInfo.WorkGroupIDZ);
658 Any |= convertArg(AI.WorkGroupInfo, ArgInfo.WorkGroupInfo);
659 Any |= convertArg(AI.PrivateSegmentWaveByteOffset,
660 ArgInfo.PrivateSegmentWaveByteOffset);
661 Any |= convertArg(AI.ImplicitArgPtr, ArgInfo.ImplicitArgPtr);
662 Any |= convertArg(AI.ImplicitBufferPtr, ArgInfo.ImplicitBufferPtr);
663 Any |= convertArg(AI.WorkItemIDX, ArgInfo.WorkItemIDX);
664 Any |= convertArg(AI.WorkItemIDY, ArgInfo.WorkItemIDY);
665 Any |= convertArg(AI.WorkItemIDZ, ArgInfo.WorkItemIDZ);
666
667 if (Any)
668 return AI;
669
670 return std::nullopt;
671}
672
675 const llvm::MachineFunction &MF)
676 : ExplicitKernArgSize(MFI.getExplicitKernArgSize()),
677 MaxKernArgAlign(MFI.getMaxKernArgAlign()), LDSSize(MFI.getLDSSize()),
678 GDSSize(MFI.getGDSSize()),
679 DynLDSAlign(MFI.getDynLDSAlign()), IsEntryFunction(MFI.isEntryFunction()),
680 NoSignedZerosFPMath(MFI.hasNoSignedZerosFPMath()),
681 MemoryBound(MFI.isMemoryBound()), WaveLimiter(MFI.needsWaveLimiter()),
682 HasSpilledSGPRs(MFI.hasSpilledSGPRs()),
683 HasSpilledVGPRs(MFI.hasSpilledVGPRs()),
684 HighBitsOf32BitAddress(MFI.get32BitAddressHighBits()),
685 Occupancy(MFI.getOccupancy()),
686 ScratchRSrcReg(regToString(MFI.getScratchRSrcReg(), TRI)),
687 FrameOffsetReg(regToString(MFI.getFrameOffsetReg(), TRI)),
688 StackPtrOffsetReg(regToString(MFI.getStackPtrOffsetReg(), TRI)),
689 BytesInStackArgArea(MFI.getBytesInStackArgArea()),
690 ReturnsVoid(MFI.returnsVoid()),
691 ArgInfo(convertArgumentInfo(MFI.getArgInfo(), TRI)),
692 PSInputAddr(MFI.getPSInputAddr()),
693 PSInputEnable(MFI.getPSInputEnable()),
694 Mode(MFI.getMode()) {
695 for (Register Reg : MFI.getWWMReservedRegs())
696 WWMReservedRegs.push_back(regToString(Reg, TRI));
697
698 if (MFI.getLongBranchReservedReg())
700 if (MFI.getVGPRForAGPRCopy())
702
703 if (MFI.getSGPRForEXECCopy())
705
706 auto SFI = MFI.getOptionalScavengeFI();
707 if (SFI)
709}
710
713}
714
716 const yaml::SIMachineFunctionInfo &YamlMFI, const MachineFunction &MF,
720 LDSSize = YamlMFI.LDSSize;
721 GDSSize = YamlMFI.GDSSize;
722 DynLDSAlign = YamlMFI.DynLDSAlign;
723 PSInputAddr = YamlMFI.PSInputAddr;
726 Occupancy = YamlMFI.Occupancy;
729 MemoryBound = YamlMFI.MemoryBound;
730 WaveLimiter = YamlMFI.WaveLimiter;
734 ReturnsVoid = YamlMFI.ReturnsVoid;
735
736 if (YamlMFI.ScavengeFI) {
737 auto FIOrErr = YamlMFI.ScavengeFI->getFI(MF.getFrameInfo());
738 if (!FIOrErr) {
739 // Create a diagnostic for a the frame index.
740 const MemoryBuffer &Buffer =
741 *PFS.SM->getMemoryBuffer(PFS.SM->getMainFileID());
742
743 Error = SMDiagnostic(*PFS.SM, SMLoc(), Buffer.getBufferIdentifier(), 1, 1,
744 SourceMgr::DK_Error, toString(FIOrErr.takeError()),
745 "", std::nullopt, std::nullopt);
746 SourceRange = YamlMFI.ScavengeFI->SourceRange;
747 return true;
748 }
749 ScavengeFI = *FIOrErr;
750 } else {
751 ScavengeFI = std::nullopt;
752 }
753 return false;
754}
755
757 return !F.hasFnAttribute("amdgpu-no-agpr");
758}
759
761 if (UsesAGPRs)
762 return *UsesAGPRs;
763
764 if (!mayNeedAGPRs()) {
765 UsesAGPRs = false;
766 return false;
767 }
768
770 MF.getFrameInfo().hasCalls()) {
771 UsesAGPRs = true;
772 return true;
773 }
774
775 const MachineRegisterInfo &MRI = MF.getRegInfo();
776
777 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
779 const TargetRegisterClass *RC = MRI.getRegClassOrNull(Reg);
780 if (RC && SIRegisterInfo::isAGPRClass(RC)) {
781 UsesAGPRs = true;
782 return true;
783 } else if (!RC && !MRI.use_empty(Reg) && MRI.getType(Reg).isValid()) {
784 // Defer caching UsesAGPRs, function might not yet been regbank selected.
785 return true;
786 }
787 }
788
789 for (MCRegister Reg : AMDGPU::AGPR_32RegClass) {
790 if (MRI.isPhysRegUsed(Reg)) {
791 UsesAGPRs = true;
792 return true;
793 }
794 }
795
796 UsesAGPRs = false;
797 return false;
798}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
Provides AMDGPU specific target descriptions.
Base class for AMDGPU specific classes of TargetSubtarget.
The AMDGPU TargetMachine interface definition for hw codegen targets.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
uint64_t Size
IO & YamlIO
Definition: ELFYAML.cpp:1292
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
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:274
const SITargetLowering * getTargetLowering() const override
Definition: GCNSubtarget.h:270
void allocKernargPreloadSGPRs(unsigned NumSGPRs)
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
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 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
VectorType::iterator erase(typename VectorType::iterator Iterator)
Remove the element given by Iterator.
Definition: MapVector.h:193
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)
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)
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
void shiftSpillPhysVGPRsToLowestRange(MachineFunction &MF)
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:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:591
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:50
bool consumeInteger(unsigned Radix, T &Result)
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:492
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
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:656
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
std::optional< FrameIndex > ScavengeFI
A wrapper around std::string which contains a source range that's being set during parsing.