-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[AMDGPU] Occupancy w.r.t. workgroup size range is also a range #123748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3bf55b
[AMDGPU] Occupancy w.r.t. WG size is now a range
lucas-rami 5f6c5bb
Merge branch 'main' into occupancy-is-range
lucas-rami 28600c0
Fix formatting issues
lucas-rami a191fec
Fix failing MIR tests
lucas-rami 7d3f944
Address feedback
lucas-rami 260463f
Revert changes related to LDS alloc. granularity
lucas-rami 18ce96a
Revert spurious modification
lucas-rami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,55 +55,92 @@ AMDGPUSubtarget::getMaxLocalMemSizeWithWaveCount(unsigned NWaves, | |
| return getLocalMemorySize() / WorkGroupsPerCU; | ||
| } | ||
|
|
||
| // FIXME: Should return min,max range. | ||
| // | ||
| // Returns the maximum occupancy, in number of waves per SIMD / EU, that can | ||
| // be achieved when only the given function is running on the machine; and | ||
| // taking into account the overall number of wave slots, the (maximum) workgroup | ||
| // size, and the per-workgroup LDS allocation size. | ||
| unsigned AMDGPUSubtarget::getOccupancyWithLocalMemSize(uint32_t Bytes, | ||
| const Function &F) const { | ||
| const unsigned MaxWorkGroupSize = getFlatWorkGroupSizes(F).second; | ||
| const unsigned MaxWorkGroupsPerCu = getMaxWorkGroupsPerCU(MaxWorkGroupSize); | ||
| if (!MaxWorkGroupsPerCu) | ||
| return 0; | ||
|
|
||
| const unsigned WaveSize = getWavefrontSize(); | ||
|
|
||
| // FIXME: Do we need to account for alignment requirement of LDS rounding the | ||
| // size up? | ||
| // Compute restriction based on LDS usage | ||
| unsigned NumGroups = getLocalMemorySize() / (Bytes ? Bytes : 1u); | ||
|
|
||
| // This can be queried with more LDS than is possible, so just assume the | ||
| // worst. | ||
| if (NumGroups == 0) | ||
| return 1; | ||
|
|
||
| NumGroups = std::min(MaxWorkGroupsPerCu, NumGroups); | ||
|
|
||
| // Round to the number of waves per CU. | ||
| const unsigned MaxGroupNumWaves = divideCeil(MaxWorkGroupSize, WaveSize); | ||
| unsigned MaxWaves = NumGroups * MaxGroupNumWaves; | ||
|
|
||
| // Number of waves per EU (SIMD). | ||
| MaxWaves = divideCeil(MaxWaves, getEUsPerCU()); | ||
|
|
||
| // Clamp to the maximum possible number of waves. | ||
| MaxWaves = std::min(MaxWaves, getMaxWavesPerEU()); | ||
| std::pair<unsigned, unsigned> | ||
| AMDGPUSubtarget::getOccupancyWithWorkGroupSizes(uint32_t LDSBytes, | ||
| const Function &F) const { | ||
| // FIXME: Is there an allocation granularity for the LDS? If so we would need | ||
| // to make sure the amount of bytes is aligned on that granularity. | ||
|
|
||
| // Compute occupancy restriction based on LDS usage. | ||
| const unsigned MaxWGsLDS = getLocalMemorySize() / std::max(LDSBytes, 1u); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably should try to account for the "amdgpu-lds-size" on the function, but that's beyond the scope of this patch |
||
|
|
||
| // Queried LDS size may be larger than available on a CU, in which case we | ||
| // consider the only achievable occupancy to be 1, in line with what we | ||
| // consider the occupancy to be when the number of requested registers in a | ||
| // particular bank is higher than the number of available ones in that bank. | ||
| if (!MaxWGsLDS) | ||
| return {1, 1}; | ||
|
|
||
| const unsigned WaveSize = getWavefrontSize(), WavesPerEU = getMaxWavesPerEU(); | ||
| const unsigned WaveSlotsPerCU = WavesPerEU * getEUsPerCU(); | ||
lucas-rami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| auto PropsFromWGSize = [&](unsigned WGSize) | ||
lucas-rami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| -> std::tuple<const unsigned, const unsigned, unsigned> { | ||
| unsigned WavesPerWG = divideCeil(WGSize, WaveSize); | ||
| unsigned WGsPerCU = std::min(getMaxWorkGroupsPerCU(WGSize), MaxWGsLDS); | ||
| return {WavesPerWG, WGsPerCU, WavesPerWG * WGsPerCU}; | ||
| }; | ||
|
|
||
| // The maximum group size will generally yield the minimum number of | ||
| // workgroups, maximum number of waves, and minimum occupancy. The opposite is | ||
| // generally true for the minimum group size. LDS or barrier ressource | ||
| // limitations can flip those minimums/maximums. | ||
| const auto [MinWGSize, MaxWGSize] = getFlatWorkGroupSizes(F); | ||
| auto [MinWavesPerWG, MaxWGsPerCU, MaxWavesPerCU] = PropsFromWGSize(MinWGSize); | ||
| auto [MaxWavesPerWG, MinWGsPerCU, MinWavesPerCU] = PropsFromWGSize(MaxWGSize); | ||
|
|
||
| // It is possible that we end up with flipped minimum and maximum number of | ||
| // waves per CU when the number of minimum/maximum concurrent groups on the CU | ||
| // is limited by LDS usage or barrier ressources. | ||
lucas-rami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (MinWavesPerCU >= MaxWavesPerCU) { | ||
| std::swap(MinWavesPerCU, MaxWavesPerCU); | ||
| } else { | ||
| // Look for a potential smaller group size than the maximum which decreases | ||
| // the concurrent number of waves on the CU for the same number of | ||
| // concurrent workgroups on the CU. | ||
| unsigned MinWavesPerCUForWGSize = | ||
| divideCeil(WaveSlotsPerCU, MinWGsPerCU + 1) * MinWGsPerCU; | ||
| if (MinWavesPerCU > MinWavesPerCUForWGSize) { | ||
| unsigned ExcessSlots = MinWavesPerCU - MinWavesPerCUForWGSize; | ||
| if (unsigned ExcessSlotsPerWG = ExcessSlots / MinWGsPerCU) { | ||
| // There may exist a smaller group size than the maximum that achieves | ||
| // the minimum number of waves per CU. This group size is the largest | ||
| // possible size that requires MaxWavesPerWG - E waves where E is | ||
| // maximized under the following constraints. | ||
| // 1. 0 <= E <= ExcessSlotsPerWG | ||
| // 2. (MaxWavesPerWG - E) * WaveSize >= MinWGSize | ||
| MinWavesPerCU -= MinWGsPerCU * std::min(ExcessSlotsPerWG, | ||
| MaxWavesPerWG - MinWavesPerWG); | ||
| } | ||
| } | ||
|
|
||
| // FIXME: Needs to be a multiple of the group size? | ||
| //MaxWaves = MaxGroupNumWaves * (MaxWaves / MaxGroupNumWaves); | ||
| // Look for a potential larger group size than the minimum which increases | ||
| // the concurrent number of waves on the CU for the same number of | ||
| // concurrent workgroups on the CU. | ||
| unsigned LeftoverSlots = WaveSlotsPerCU - MaxWGsPerCU * MinWavesPerWG; | ||
| if (unsigned LeftoverSlotsPerWG = LeftoverSlots / MaxWGsPerCU) { | ||
| // There may exist a larger group size than the minimum that achieves the | ||
| // maximum number of waves per CU. This group size is the smallest | ||
| // possible size that requires MinWavesPerWG + L waves where L is | ||
| // maximized under the following constraints. | ||
| // 1. 0 <= L <= LeftoverSlotsPerWG | ||
| // 2. (MinWavesPerWG + L - 1) * WaveSize <= MaxWGSize | ||
| MaxWavesPerCU += MaxWGsPerCU * std::min(LeftoverSlotsPerWG, | ||
| ((MaxWGSize - 1) / WaveSize) + 1 - | ||
| MinWavesPerWG); | ||
| } | ||
| } | ||
|
|
||
| assert(MaxWaves > 0 && MaxWaves <= getMaxWavesPerEU() && | ||
| "computed invalid occupancy"); | ||
| return MaxWaves; | ||
| // Return the minimum/maximum number of waves on any EU, assuming that all | ||
| // wavefronts are spread across all EUs as evenly as possible. | ||
| return {std::clamp(MinWavesPerCU / getEUsPerCU(), 1U, WavesPerEU), | ||
| std::clamp(divideCeil(MaxWavesPerCU, getEUsPerCU()), 1U, WavesPerEU)}; | ||
| } | ||
|
|
||
| unsigned | ||
| AMDGPUSubtarget::getOccupancyWithLocalMemSize(const MachineFunction &MF) const { | ||
| std::pair<unsigned, unsigned> AMDGPUSubtarget::getOccupancyWithWorkGroupSizes( | ||
| const MachineFunction &MF) const { | ||
| const auto *MFI = MF.getInfo<SIMachineFunctionInfo>(); | ||
| return getOccupancyWithLocalMemSize(MFI->getLDSSize(), MF.getFunction()); | ||
| return getOccupancyWithWorkGroupSizes(MFI->getLDSSize(), MF.getFunction()); | ||
| } | ||
|
|
||
| std::pair<unsigned, unsigned> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.