Skip to content

Commit 7145a56

Browse files
committed
Allow AGPRs to be used as spill slots for ArchVGPRs based on flag
1 parent fc80c20 commit 7145a56

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ struct ExcessRP {
16961696
unsigned ArchVGPRs = 0;
16971697
/// Number of excess AGPRs.
16981698
unsigned AGPRs = 0;
1699-
/// For unified register files, number of excess VGPRs.
1699+
/// For unified register files, number of excess VGPRs. 0 otherwise.
17001700
unsigned VGPRs = 0;
17011701
/// For unified register files with AGPR usage, number of excess ArchVGPRs to
17021702
/// save before we are able to save a whole allocation granule.
@@ -1705,29 +1705,26 @@ struct ExcessRP {
17051705
bool HasAGPRs = false;
17061706
/// Whether the subtarget has a unified RF.
17071707
bool UnifiedRF;
1708+
/// Whether we consider that ArchVGPRs can be spilled to AGPRs and the other
1709+
/// way around.
1710+
bool AllowVGPRToVGPRSpill;
17081711

17091712
/// Constructs the excess RP model; determines the excess pressure w.r.t. a
17101713
/// maximum number of allowed SGPRs/VGPRs.
17111714
ExcessRP(const GCNSubtarget &ST, const GCNRegPressure &RP, unsigned MaxSGPRs,
1712-
unsigned MaxVGPRs);
1715+
unsigned MaxVGPRs, bool AllowVGPRToVGPRSpill);
17131716

17141717
/// Accounts for \p NumRegs saved SGPRs in the model. Returns whether saving
17151718
/// these SGPRs helped reduce excess pressure.
17161719
bool saveSGPRs(unsigned NumRegs) { return saveRegs(SGPRs, NumRegs); }
17171720

1718-
/// Accounts for \p NumRegs saved ArchVGPRs in the model. If \p
1719-
/// UseArchVGPRForAGPRSpill is true, saved ArchVGPRs are used to save excess
1720-
/// AGPRs once excess ArchVGPR pressure has been eliminated. Returns whether
1721-
/// saving these ArchVGPRs helped reduce excess pressure.
1722-
bool saveArchVGPRs(unsigned NumRegs, bool UseArchVGPRForAGPRSpill);
1721+
/// Accounts for \p NumRegs saved ArchVGPRs in the model. Returns whether
1722+
/// saving these ArchGPRs helped reduce excess pressure.
1723+
bool saveArchVGPRs(unsigned NumRegs);
17231724

17241725
/// Accounts for \p NumRegs saved AGPRs in the model. Returns whether saving
17251726
/// these AGPRs helped reduce excess pressure.
1726-
bool saveAGPRs(unsigned NumRegs) {
1727-
return saveRegs(AGPRs, NumRegs) ||
1728-
(UnifiedRF && saveRegs(VGPRs, NumRegs)) ||
1729-
saveRegs(ArchVGPRs, NumRegs);
1730-
}
1727+
bool saveAGPRs(unsigned NumRegs);
17311728

17321729
/// Returns whether there is any excess register pressure.
17331730
operator bool() const { return SGPRs || ArchVGPRs || AGPRs || VGPRs; }
@@ -1753,8 +1750,10 @@ struct ExcessRP {
17531750
} // namespace
17541751

17551752
ExcessRP::ExcessRP(const GCNSubtarget &ST, const GCNRegPressure &RP,
1756-
unsigned MaxSGPRs, unsigned MaxVGPRs)
1757-
: UnifiedRF(ST.hasGFX90AInsts()) {
1753+
unsigned MaxSGPRs, unsigned MaxVGPRs,
1754+
bool AllowVGPRToVGPRSpill)
1755+
: UnifiedRF(ST.hasGFX90AInsts()),
1756+
AllowVGPRToVGPRSpill(AllowVGPRToVGPRSpill) {
17581757
// Compute excess SGPR pressure.
17591758
unsigned NumSGPRs = RP.getSGPRNum();
17601759
if (NumSGPRs > MaxSGPRs)
@@ -1799,15 +1798,15 @@ ExcessRP::ExcessRP(const GCNSubtarget &ST, const GCNRegPressure &RP,
17991798
}
18001799
}
18011800

1802-
bool ExcessRP::saveArchVGPRs(unsigned NumRegs, bool UseArchVGPRForAGPRSpill) {
1801+
bool ExcessRP::saveArchVGPRs(unsigned NumRegs) {
18031802
bool Progress = saveRegs(ArchVGPRs, NumRegs);
18041803
if (!NumRegs)
18051804
return Progress;
18061805

18071806
if (!UnifiedRF) {
1808-
if (UseArchVGPRForAGPRSpill)
1807+
if (AllowVGPRToVGPRSpill)
18091808
Progress |= saveRegs(AGPRs, NumRegs);
1810-
} else if (HasAGPRs && (VGPRs || (UseArchVGPRForAGPRSpill && AGPRs))) {
1809+
} else if (HasAGPRs && (VGPRs || (AllowVGPRToVGPRSpill && AGPRs))) {
18111810
// There is progress as long as there are VGPRs left to save, even if the
18121811
// save induced by this particular call does not cross an ArchVGPR alignment
18131812
// barrier.
@@ -1834,13 +1833,21 @@ bool ExcessRP::saveArchVGPRs(unsigned NumRegs, bool UseArchVGPRForAGPRSpill) {
18341833
// Prioritize saving generic VGPRs, then AGPRs if we allow AGPR-to-ArchVGPR
18351834
// spilling and have some free ArchVGPR slots.
18361835
saveRegs(VGPRs, NumSavedRegs);
1837-
if (UseArchVGPRForAGPRSpill)
1836+
if (AllowVGPRToVGPRSpill)
18381837
saveRegs(AGPRs, NumSavedRegs);
18391838
} else {
18401839
// No AGPR usage in the region i.e., no allocation granule to worry about.
18411840
Progress |= saveRegs(VGPRs, NumRegs);
18421841
}
1842+
return Progress;
1843+
}
18431844

1845+
bool ExcessRP::saveAGPRs(unsigned NumRegs) {
1846+
bool Progress = saveRegs(AGPRs, NumRegs);
1847+
if (UnifiedRF)
1848+
Progress |= saveRegs(VGPRs, NumRegs);
1849+
if (AllowVGPRToVGPRSpill)
1850+
Progress |= saveRegs(ArchVGPRs, NumRegs);
18441851
return Progress;
18451852
}
18461853

@@ -1871,15 +1878,22 @@ bool PreRARematStage::canIncreaseOccupancyOrReduceSpill() {
18711878
// one in the whole function.
18721879
for (unsigned I = 0, E = DAG.Regions.size(); I != E; ++I) {
18731880
GCNRegPressure &RP = DAG.Pressure[I];
1874-
ExcessRP Excess(ST, RP, MaxSGPRsNoSpill, MaxVGPRsNoSpill);
1881+
// We allow saved VGPRs of one category (ArchVGPR or AGPR) to be considered
1882+
// as free spill slots for the other category only when we are just trying
1883+
// to eliminate spilling. At this point we err on the conservative side and
1884+
// do not increase register-to-register spilling for the sake of increasing
1885+
// occupancy.
1886+
ExcessRP Excess(ST, RP, MaxSGPRsNoSpill, MaxVGPRsNoSpill,
1887+
/*AllowVGPRToVGPRSpill=*/true);
18751888
if (Excess && IncreaseOccupancy) {
18761889
// There is spilling in the region and we were so far trying to increase
18771890
// occupancy. Strop trying that and focus on reducing spilling.
18781891
IncreaseOccupancy = false;
18791892
OptRegions.clear();
18801893
} else if (IncreaseOccupancy) {
18811894
// There is no spilling in the region, try to increase occupancy.
1882-
Excess = ExcessRP(ST, RP, MaxSGPRsIncOcc, MaxVGPRsIncOcc);
1895+
Excess = ExcessRP(ST, RP, MaxSGPRsIncOcc, MaxVGPRsIncOcc,
1896+
/*AllowVGPRToVGPRSpill=*/false);
18831897
}
18841898
if (Excess)
18851899
OptRegions.insert({I, Excess});
@@ -1914,19 +1928,12 @@ bool PreRARematStage::canIncreaseOccupancyOrReduceSpill() {
19141928
bool &Progress) -> bool {
19151929
ExcessRP &Excess = OptIt->getSecond();
19161930
unsigned NumRegs = SIRegisterInfo::getNumCoveredRegs(Mask);
1917-
if (SRI->isSGPRClass(RC)) {
1931+
if (SRI->isSGPRClass(RC))
19181932
Progress |= Excess.saveSGPRs(NumRegs);
1919-
} else if (SRI->isAGPRClass(RC)) {
1933+
else if (SRI->isAGPRClass(RC))
19201934
Progress |= Excess.saveAGPRs(NumRegs);
1921-
} else {
1922-
// We allow saved ArchVGPRs to be considered as free spill slots for AGPRs
1923-
// only when we are just trying to eliminate spilling to memory. At this
1924-
// point we err on the conservative side and do not increase
1925-
// register-to-register spilling for the sake of increasing occupancy.
1926-
Progress |=
1927-
Excess.saveArchVGPRs(NumRegs,
1928-
/*UseArchVGPRForAGPRSpill=*/!IncreaseOccupancy);
1929-
}
1935+
else
1936+
Progress |= Excess.saveArchVGPRs(NumRegs);
19301937
if (!Excess)
19311938
OptRegions.erase(OptIt->getFirst());
19321939
return OptRegions.empty();

0 commit comments

Comments
 (0)