Skip to content

Commit 6206f54

Browse files
authored
[AMDGPU] Occupancy w.r.t. workgroup size range is also a range (#123748)
Occupancy (i.e., the number of waves per EU) depends, in addition to register usage, on per-workgroup LDS usage as well as on the range of possible workgroup sizes. Mirroring the latter, occupancy should therefore be expressed as a range since different group sizes generally yield different achievable occupancies. `getOccupancyWithLocalMemSize` currently returns a scalar occupancy based on the maximum workgroup size and LDS usage. With respect to the workgroup size range, this scalar can be the minimum, the maximum, or neither of the two of the range of achievable occupancies. This commit fixes the function by making it compute and return the range of achievable occupancies w.r.t. workgroup size and LDS usage; it also renames it to `getOccupancyWithWorkGroupSizes` since it is the range of workgroup sizes that produces the range of achievable occupancies. Computing the achievable occupancy range is surprisingly involved. Minimum/maximum workgroup sizes do not necessarily yield maximum/minimum occupancies i.e., sometimes workgroup sizes inside the range yield the occupancy bounds. The implementation finds these sizes in constant time; heavy documentation explains the rationale behind the sometimes relatively obscure calculations. As a justifying example, consider a target with 10 waves / EU, 4 EUs/CU, 64-wide waves. Also consider a function with no LDS usage and a flat workgroup size range of [513,1024]. - A group of 513 items requires 9 waves per group. Only 4 groups made up of 9 waves each can fit fully on a CU at any given time, for a total of 36 waves on the CU, or 9 per EU. However, filling as much as possible the remaining 40-36=4 wave slots without decreasing the number of groups reveals that a larger group of 640 items yields 40 waves on the CU, or 10 per EU. - Similarly, a group of 1024 items requires 16 waves per group. Only 2 groups made up of 16 waves each can fit fully on a CU ay any given time, for a total of 32 waves on the CU, or 8 per EU. However, removing as many waves as possible from the groups without being able to fit another equal-sized group on the CU reveals that a smaller group of 896 items yields 28 waves on the CU, or 7 per EU. Therefore the achievable occupancy range for this function is not [8,9] as the group size bounds directly yield, but [7,10]. Naturally this change causes a lot of test churn as instruction scheduling is driven by achievable occupancy estimates. In most unit tests the flat workgroup size range is the default [1,1024] which, ignoring potential LDS limitations, would previously produce a scalar occupancy of 8 (derived from 1024) on a lot of targets, whereas we now consider the maximum occupancy to be 10 in such cases. Most tests are updated automatically and checked manually for sanity. I also manually changed some non-automatically generated assertions when necessary. Fixes #118220.
1 parent 26b61e1 commit 6206f54

File tree

105 files changed

+16507
-16905
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+16507
-16905
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ void AMDGPUAsmPrinter::validateMCResourceInfo(Function &F) {
456456
uint64_t NumSGPRsForWavesPerEU = std::max(
457457
{NumSgpr, (uint64_t)1, (uint64_t)STM.getMinNumSGPRs(MaxWaves)});
458458
const MCExpr *OccupancyExpr = AMDGPUMCExpr::createOccupancy(
459-
STM.computeOccupancy(F, MFI.getLDSSize()),
459+
STM.getOccupancyWithWorkGroupSizes(*MF).second,
460460
MCConstantExpr::create(NumSGPRsForWavesPerEU, OutContext),
461461
MCConstantExpr::create(NumVGPRsForWavesPerEU, OutContext), STM,
462462
OutContext);
@@ -1272,8 +1272,8 @@ void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
12721272
}
12731273

12741274
ProgInfo.Occupancy = AMDGPUMCExpr::createOccupancy(
1275-
STM.computeOccupancy(F, ProgInfo.LDSSize), ProgInfo.NumSGPRsForWavesPerEU,
1276-
ProgInfo.NumVGPRsForWavesPerEU, STM, Ctx);
1275+
STM.computeOccupancy(F, ProgInfo.LDSSize).second,
1276+
ProgInfo.NumSGPRsForWavesPerEU, ProgInfo.NumVGPRsForWavesPerEU, STM, Ctx);
12771277

12781278
const auto [MinWEU, MaxWEU] =
12791279
AMDGPU::getIntegerPairAttribute(F, "amdgpu-waves-per-eu", {0, 0}, true);

llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ bool AMDGPUPromoteAllocaImpl::hasSufficientLocalMem(const Function &F) {
13441344
}
13451345

13461346
unsigned MaxOccupancy =
1347-
ST.getOccupancyWithLocalMemSize(CurrentLocalMemUsage, F);
1347+
ST.getOccupancyWithWorkGroupSizes(CurrentLocalMemUsage, F).second;
13481348

13491349
// Restrict local memory usage so that we don't drastically reduce occupancy,
13501350
// unless it is already significantly reduced.

llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,90 @@ AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves,
5555
return getLocalMemorySize() / WorkGroupsPerCU;
5656
}
5757

58-
// FIXME: Should return min,max range.
59-
//
60-
// Returns the maximum occupancy, in number of waves per SIMD / EU, that can
61-
// be achieved when only the given function is running on the machine; and
62-
// taking into account the overall number of wave slots, the (maximum) workgroup
63-
// size, and the per-workgroup LDS allocation size.
64-
unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes,
65-
const Function &F) const {
66-
const unsigned MaxWorkGroupSize = getFlatWorkGroupSizes(F).second;
67-
const unsigned MaxWorkGroupsPerCu = getMaxWorkGroupsPerCU(MaxWorkGroupSize);
68-
if (!MaxWorkGroupsPerCu)
69-
return 0;
70-
71-
const unsigned WaveSize = getWavefrontSize();
72-
73-
// FIXME: Do we need to account for alignment requirement of LDS rounding the
74-
// size up?
75-
// Compute restriction based on LDS usage
76-
unsigned NumGroups = getLocalMemorySize() / (Bytes ? Bytes : 1u);
77-
78-
// This can be queried with more LDS than is possible, so just assume the
79-
// worst.
80-
if (NumGroups == 0)
81-
return 1;
82-
83-
NumGroups = std::min(MaxWorkGroupsPerCu, NumGroups);
84-
85-
// Round to the number of waves per CU.
86-
const unsigned MaxGroupNumWaves = divideCeil(MaxWorkGroupSize, WaveSize);
87-
unsigned MaxWaves = NumGroups * MaxGroupNumWaves;
88-
89-
// Number of waves per EU (SIMD).
90-
MaxWaves = divideCeil(MaxWaves, getEUsPerCU());
91-
92-
// Clamp to the maximum possible number of waves.
93-
MaxWaves = std::min(MaxWaves, getMaxWavesPerEU());
58+
std::pair<unsigned, unsigned>
59+
AMDGPUSubtarget::getOccupancyWithWorkGroupSizes(uint32_t LDSBytes,
60+
const Function &F) const {
61+
// FIXME: We should take into account the LDS allocation granularity.
62+
const unsigned MaxWGsLDS = getLocalMemorySize() / std::max(LDSBytes, 1u);
63+
64+
// Queried LDS size may be larger than available on a CU, in which case we
65+
// consider the only achievable occupancy to be 1, in line with what we
66+
// consider the occupancy to be when the number of requested registers in a
67+
// particular bank is higher than the number of available ones in that bank.
68+
if (!MaxWGsLDS)
69+
return {1, 1};
70+
71+
const unsigned WaveSize = getWavefrontSize(), WavesPerEU = getMaxWavesPerEU();
72+
73+
auto PropsFromWGSize = [=](unsigned WGSize)
74+
-> std::tuple<const unsigned, const unsigned, unsigned> {
75+
unsigned WavesPerWG = divideCeil(WGSize, WaveSize);
76+
unsigned WGsPerCU = std::min(getMaxWorkGroupsPerCU(WGSize), MaxWGsLDS);
77+
return {WavesPerWG, WGsPerCU, WavesPerWG * WGsPerCU};
78+
};
79+
80+
// The maximum group size will generally yield the minimum number of
81+
// workgroups, maximum number of waves, and minimum occupancy. The opposite is
82+
// generally true for the minimum group size. LDS or barrier ressource
83+
// limitations can flip those minimums/maximums.
84+
const auto [MinWGSize, MaxWGSize] = getFlatWorkGroupSizes(F);
85+
auto [MinWavesPerWG, MaxWGsPerCU, MaxWavesPerCU] = PropsFromWGSize(MinWGSize);
86+
auto [MaxWavesPerWG, MinWGsPerCU, MinWavesPerCU] = PropsFromWGSize(MaxWGSize);
87+
88+
// It is possible that we end up with flipped minimum and maximum number of
89+
// waves per CU when the number of minimum/maximum concurrent groups on the CU
90+
// is limited by LDS usage or barrier resources.
91+
if (MinWavesPerCU >= MaxWavesPerCU) {
92+
std::swap(MinWavesPerCU, MaxWavesPerCU);
93+
} else {
94+
const unsigned WaveSlotsPerCU = WavesPerEU * getEUsPerCU();
95+
96+
// Look for a potential smaller group size than the maximum which decreases
97+
// the concurrent number of waves on the CU for the same number of
98+
// concurrent workgroups on the CU.
99+
unsigned MinWavesPerCUForWGSize =
100+
divideCeil(WaveSlotsPerCU, MinWGsPerCU + 1) * MinWGsPerCU;
101+
if (MinWavesPerCU > MinWavesPerCUForWGSize) {
102+
unsigned ExcessSlots = MinWavesPerCU - MinWavesPerCUForWGSize;
103+
if (unsigned ExcessSlotsPerWG = ExcessSlots / MinWGsPerCU) {
104+
// There may exist a smaller group size than the maximum that achieves
105+
// the minimum number of waves per CU. This group size is the largest
106+
// possible size that requires MaxWavesPerWG - E waves where E is
107+
// maximized under the following constraints.
108+
// 1. 0 <= E <= ExcessSlotsPerWG
109+
// 2. (MaxWavesPerWG - E) * WaveSize >= MinWGSize
110+
MinWavesPerCU -= MinWGsPerCU * std::min(ExcessSlotsPerWG,
111+
MaxWavesPerWG - MinWavesPerWG);
112+
}
113+
}
94114

95-
// FIXME: Needs to be a multiple of the group size?
96-
//MaxWaves = MaxGroupNumWaves * (MaxWaves / MaxGroupNumWaves);
115+
// Look for a potential larger group size than the minimum which increases
116+
// the concurrent number of waves on the CU for the same number of
117+
// concurrent workgroups on the CU.
118+
unsigned LeftoverSlots = WaveSlotsPerCU - MaxWGsPerCU * MinWavesPerWG;
119+
if (unsigned LeftoverSlotsPerWG = LeftoverSlots / MaxWGsPerCU) {
120+
// There may exist a larger group size than the minimum that achieves the
121+
// maximum number of waves per CU. This group size is the smallest
122+
// possible size that requires MinWavesPerWG + L waves where L is
123+
// maximized under the following constraints.
124+
// 1. 0 <= L <= LeftoverSlotsPerWG
125+
// 2. (MinWavesPerWG + L - 1) * WaveSize <= MaxWGSize
126+
MaxWavesPerCU += MaxWGsPerCU * std::min(LeftoverSlotsPerWG,
127+
((MaxWGSize - 1) / WaveSize) + 1 -
128+
MinWavesPerWG);
129+
}
130+
}
97131

98-
assert(MaxWaves > 0 && MaxWaves <= getMaxWavesPerEU() &&
99-
"computed invalid occupancy");
100-
return MaxWaves;
132+
// Return the minimum/maximum number of waves on any EU, assuming that all
133+
// wavefronts are spread across all EUs as evenly as possible.
134+
return {std::clamp(MinWavesPerCU / getEUsPerCU(), 1U, WavesPerEU),
135+
std::clamp(divideCeil(MaxWavesPerCU, getEUsPerCU()), 1U, WavesPerEU)};
101136
}
102137

103-
unsigned
104-
AMDGPUSubtarget::getOccupancyWithLocalMemSize(const MachineFunction &MF) const {
138+
std::pair<unsigned, unsigned> AMDGPUSubtarget::getOccupancyWithWorkGroupSizes(
139+
const MachineFunction &MF) const {
105140
const auto *MFI = MF.getInfo<SIMachineFunctionInfo>();
106-
return getOccupancyWithLocalMemSize(MFI->getLDSSize(), MF.getFunction());
141+
return getOccupancyWithWorkGroupSizes(MFI->getLDSSize(), MF.getFunction());
107142
}
108143

109144
std::pair<unsigned, unsigned>

llvm/lib/Target/AMDGPU/AMDGPUSubtarget.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,21 @@ class AMDGPUSubtarget {
127127
unsigned getMaxLocalMemSizeWithWaveCount(unsigned WaveCount,
128128
const Function &) const;
129129

130-
/// Inverse of getMaxLocalMemWithWaveCount. Return the maximum wavecount if
131-
/// the given LDS memory size is the only constraint.
132-
unsigned getOccupancyWithLocalMemSize(uint32_t Bytes, const Function &) const;
130+
/// Subtarget's minimum/maximum occupancy, in number of waves per EU, that can
131+
/// be achieved when the only function running on a CU is \p F and each
132+
/// workgroup running the function requires \p LDSBytes bytes of LDS space.
133+
/// This notably depends on the range of allowed flat group sizes for the
134+
/// function and hardware characteristics.
135+
std::pair<unsigned, unsigned>
136+
getOccupancyWithWorkGroupSizes(uint32_t LDSBytes, const Function &F) const;
133137

134-
unsigned getOccupancyWithLocalMemSize(const MachineFunction &MF) const;
138+
/// Subtarget's minimum/maximum occupancy, in number of waves per EU, that can
139+
/// be achieved when the only function running on a CU is \p MF. This notably
140+
/// depends on the range of allowed flat group sizes for the function, the
141+
/// amount of per-workgroup LDS space required by the function, and hardware
142+
/// characteristics.
143+
std::pair<unsigned, unsigned>
144+
getOccupancyWithWorkGroupSizes(const MachineFunction &MF) const;
135145

136146
bool isAmdHsaOS() const {
137147
return TargetTriple.getOS() == Triple::AMDHSA;

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ bool GCNTargetMachine::parseMachineFunctionInfo(
17211721

17221722
if (MFI->Occupancy == 0) {
17231723
// Fixup the subtarget dependent default value.
1724-
MFI->Occupancy = ST.computeOccupancy(MF.getFunction(), MFI->getLDSSize());
1724+
MFI->Occupancy = ST.getOccupancyWithWorkGroupSizes(MF).second;
17251725
}
17261726

17271727
auto parseRegister = [&](const yaml::StringValue &RegName, Register &RegVal) {

llvm/lib/Target/AMDGPU/GCNSchedStrategy.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,8 @@ bool PreRARematStage::initGCNSchedStage() {
10891089
return false;
10901090

10911091
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1092-
// Check maximum occupancy
1093-
if (ST.computeOccupancy(MF.getFunction(), MFI.getLDSSize()) ==
1094-
DAG.MinOccupancy)
1092+
// Rematerialization will not help if occupancy is not limited by reg usage.
1093+
if (ST.getOccupancyWithWorkGroupSizes(MF).second == DAG.MinOccupancy)
10951094
return false;
10961095

10971096
// FIXME: This pass will invalidate cached MBBLiveIns for regions
@@ -1272,8 +1271,8 @@ void GCNSchedStage::checkScheduling() {
12721271
return;
12731272
}
12741273

1275-
unsigned TargetOccupancy =
1276-
std::min(S.getTargetOccupancy(), ST.getOccupancyWithLocalMemSize(MF));
1274+
unsigned TargetOccupancy = std::min(
1275+
S.getTargetOccupancy(), ST.getOccupancyWithWorkGroupSizes(MF).second);
12771276
unsigned WavesAfter =
12781277
std::min(TargetOccupancy, PressureAfter.getOccupancy(ST));
12791278
unsigned WavesBefore =

llvm/lib/Target/AMDGPU/GCNSubtarget.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,16 @@ unsigned GCNSubtarget::getReservedNumSGPRs(const Function &F) const {
400400
return getBaseReservedNumSGPRs(KernelUsesFlatScratch);
401401
}
402402

403-
unsigned GCNSubtarget::computeOccupancy(const Function &F, unsigned LDSSize,
404-
unsigned NumSGPRs,
405-
unsigned NumVGPRs) const {
406-
unsigned Occupancy =
407-
std::min(getMaxWavesPerEU(), getOccupancyWithLocalMemSize(LDSSize, F));
408-
if (NumSGPRs)
409-
Occupancy = std::min(Occupancy, getOccupancyWithNumSGPRs(NumSGPRs));
410-
if (NumVGPRs)
411-
Occupancy = std::min(Occupancy, getOccupancyWithNumVGPRs(NumVGPRs));
412-
return Occupancy;
403+
std::pair<unsigned, unsigned>
404+
GCNSubtarget::computeOccupancy(const Function &F, unsigned LDSSize,
405+
unsigned NumSGPRs, unsigned NumVGPRs) const {
406+
auto [MinOcc, MaxOcc] = getOccupancyWithWorkGroupSizes(LDSSize, F);
407+
unsigned SGPROcc = getOccupancyWithNumSGPRs(NumSGPRs);
408+
unsigned VGPROcc = getOccupancyWithNumVGPRs(NumVGPRs);
409+
410+
// Maximum occupancy may be further limited by high SGPR/VGPR usage.
411+
MaxOcc = std::min(MaxOcc, std::min(SGPROcc, VGPROcc));
412+
return {std::min(MinOcc, MaxOcc), MaxOcc};
413413
}
414414

415415
unsigned GCNSubtarget::getBaseMaxNumSGPRs(

llvm/lib/Target/AMDGPU/GCNSubtarget.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,12 +1368,18 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
13681368
/// VGPRs
13691369
unsigned getOccupancyWithNumVGPRs(unsigned VGPRs) const;
13701370

1371-
/// Return occupancy for the given function. Used LDS and a number of
1372-
/// registers if provided.
1373-
/// Note, occupancy can be affected by the scratch allocation as well, but
1371+
/// Subtarget's minimum/maximum occupancy, in number of waves per EU, that can
1372+
/// be achieved when the only function running on a CU is \p F, each workgroup
1373+
/// uses \p LDSSize bytes of LDS, and each wave uses \p NumSGPRs SGPRs and \p
1374+
/// NumVGPRs VGPRs. The flat workgroup sizes associated to the function are a
1375+
/// range, so this returns a range as well.
1376+
///
1377+
/// Note that occupancy can be affected by the scratch allocation as well, but
13741378
/// we do not have enough information to compute it.
1375-
unsigned computeOccupancy(const Function &F, unsigned LDSSize = 0,
1376-
unsigned NumSGPRs = 0, unsigned NumVGPRs = 0) const;
1379+
std::pair<unsigned, unsigned> computeOccupancy(const Function &F,
1380+
unsigned LDSSize = 0,
1381+
unsigned NumSGPRs = 0,
1382+
unsigned NumVGPRs = 0) const;
13771383

13781384
/// \returns true if the flat_scratch register should be initialized with the
13791385
/// pointer to the wave's scratch memory rather than a size and offset.

llvm/lib/Target/AMDGPU/SIMachineFunctionInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ SIMachineFunctionInfo::SIMachineFunctionInfo(const Function &F,
4848
MaxNumWorkGroups = ST.getMaxNumWorkGroups(F);
4949
assert(MaxNumWorkGroups.size() == 3);
5050

51-
Occupancy = ST.computeOccupancy(F, getLDSSize());
51+
Occupancy = ST.computeOccupancy(F, getLDSSize()).second;
5252
CallingConv::ID CC = F.getCallingConv();
5353

5454
VRegFlags.reserve(1024);
@@ -185,8 +185,7 @@ MachineFunctionInfo *SIMachineFunctionInfo::clone(
185185
void SIMachineFunctionInfo::limitOccupancy(const MachineFunction &MF) {
186186
limitOccupancy(getMaxWavesPerEU());
187187
const GCNSubtarget& ST = MF.getSubtarget<GCNSubtarget>();
188-
limitOccupancy(ST.getOccupancyWithLocalMemSize(getLDSSize(),
189-
MF.getFunction()));
188+
limitOccupancy(ST.getOccupancyWithWorkGroupSizes(MF).second);
190189
}
191190

192191
Register SIMachineFunctionInfo::addPrivateSegmentBuffer(

llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,18 +3642,15 @@ bool SIRegisterInfo::shouldCoalesce(MachineInstr *MI,
36423642

36433643
unsigned SIRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
36443644
MachineFunction &MF) const {
3645-
const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3646-
3647-
unsigned Occupancy = ST.getOccupancyWithLocalMemSize(MFI->getLDSSize(),
3648-
MF.getFunction());
3645+
unsigned MinOcc = ST.getOccupancyWithWorkGroupSizes(MF).first;
36493646
switch (RC->getID()) {
36503647
default:
36513648
return AMDGPUGenRegisterInfo::getRegPressureLimit(RC, MF);
36523649
case AMDGPU::VGPR_32RegClassID:
3653-
return std::min(ST.getMaxNumVGPRs(Occupancy), ST.getMaxNumVGPRs(MF));
3650+
return std::min(ST.getMaxNumVGPRs(MinOcc), ST.getMaxNumVGPRs(MF));
36543651
case AMDGPU::SGPR_32RegClassID:
36553652
case AMDGPU::SGPR_LO16RegClassID:
3656-
return std::min(ST.getMaxNumSGPRs(Occupancy, true), ST.getMaxNumSGPRs(MF));
3653+
return std::min(ST.getMaxNumSGPRs(MinOcc, true), ST.getMaxNumSGPRs(MF));
36573654
}
36583655
}
36593656

0 commit comments

Comments
 (0)