Repository navigation
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
|
||
| // If the target doesn't support subreg reload, fallback to restoring the | ||
| // full tuple. | ||
| if (SubReg && !TRI.shouldEnableSubRegReload(SubReg)) |
There was a problem hiding this comment.
Check this before going through all the trouble of computing SubReg.
There was a problem hiding this comment.
The check MRI.subRegLivenessEnabled() added above skips it for the unintended targets. This is mainly to restrict when targets further want to constrain this optimization for the identified SubReg. For instance, we don't want this optimization for 16-bit subreg accesses. I think the comment I added here is misleading. I need to refine that to include the point I just mentioned. Will do.
There was a problem hiding this comment.
I don't get why we need to check that. The target can decide to ignore the subreg if it doesn't support it.
There was a problem hiding this comment.
Yes, the targets can choose to ignore the subreg field passed to them within their loadRegFromStackSlot. However, that info (whether the target implemented subreg reload or not) should be returned to this callsite as we remove the subreg field in the use instruction as part of this optimization when the target truly implements the partial reload by constructing a concrete class for the subreg access. See the transition explained here.
%tuple:VReg_128 = ... ; 128-bit tuple (4x32-bit)
SPILL_V128 %tuple to stack
...
; Later, only need sub1 (second 32-bit component)
; Current implementation - restore full.
%reload:Vreg_128 = RESTORE_V128, ofst:0
%val = USE %reload.sub1
; With subreg reload implemented.
%reload:VGPR_32 = RESTORE_V32, ofst:4
%val = USE %reload // drop the subreg.
The subreg fields are dropped in the InlineSpiller at https://github.com/llvm/llvm-project/pull/175002/files#diff-855df7e3f96ef7f3f499fdafba308dde780d710f717f19158a2f39059c8a6f5dR1305.
To pass the subreg info always inside loadRegFromStackSlot and let the targets decide whether to implement it or not, requires some changes in the way how InlineSpiller and this target hook interact.
There was a problem hiding this comment.
I've updated the API name and documentation to reflect its use better.
2a12cb5 to
986a1a5
Compare
7676097 to
2b2772b
Compare
986a1a5 to
b221906
Compare
…for subreg-reload This preparatory patch introduces an additional argument to the target hook loadRegFromStackSlot. Ths is essential for targets to handle subregister-specific reload in the future. See how this is used for AMDGPU target with PR #175002.
2b2772b to
12d1aa0
Compare
b221906 to
070b3e9
Compare
|
Rebased the code after merging PR #175581. |
| /// To enable subreg reload of register tuples during RA. This would | ||
| /// eventually improve the register allocation for the functions that involve | ||
| /// subreg uses instead of the entire tuple. | ||
| virtual bool shouldEnableSubRegReload(unsigned SubReg) const { return false; } |
There was a problem hiding this comment.
I'll have to look at the details later in the PR, but can't we always produce the subreg info and that's on the targets to decide whether they want to leverage it?
|
|
||
| // If the target doesn't support subreg reload, fallback to restoring the | ||
| // full tuple. | ||
| if (SubReg && !TRI.shouldEnableSubRegReload(SubReg)) |
There was a problem hiding this comment.
I don't get why we need to check that. The target can decide to ignore the subreg if it doesn't support it.
|
|
||
| const TargetRegisterClass *OrigRC = MRI.getRegClass(Reg); | ||
| const TargetRegisterClass *NewRC = | ||
| SubReg ? TRI.getSubRegisterClass(OrigRC, SubReg) : nullptr; |
There was a problem hiding this comment.
Could the target figure that out on their own within insertReload?
There was a problem hiding this comment.
I guess my previous comment justifies why I want to handle it here instead of the insertReload or the target hook itself. They both need to return a status whether the subreg reload is truly implemented or not.
There was a problem hiding this comment.
What I'm saying is: Assume we always reload the full tuple so that we don't have to handle the update of the subreg uses here.
The question is can this be easily folded later on?
To get back to your example.
Instead of generating:
; Later, only need sub1 (second 32-bit component)
; Current implementation - restore full.
%reload:Vreg_128 = RESTORE_V128, ofst:0
%val = USE %reload.sub1
This patch wants to generate:
%reload32:Vreg_32 = RESTORE_V32, ofst:Y
%val = USE %reload32
What I'm saying is could we generate:
%reload32:Vreg_32 = RESTORE_V32, ofst:Y
%reload = INSERT_SUBREG undef, %reload32, sub1 <-- At the API boundary assume we fully restore
%val = USE %reload.sub1 <-- this part doesn't need to be updated here
What I'm saying here is a lot of the "complexity" in this patch is changing %reload in %reload32, i.e., change the register class and the subreg idx. I'm wondering if we can do that clean-up later.
I'm trying to get a read of the trade-off between ease to understand the code vs. doing the whole transformation in one-shot/several-shots.
There was a problem hiding this comment.
FWIW, the reason I'm trying to split the update of the subreg idx from the insertReload and so on is because the handling of the LaneMask is hacky and although it works for AMDGPU, that may not be generally correct.
Hence, at least for now, I would rather this piece of code to live in the backend-specific part.
There was a problem hiding this comment.
The subreg reload brings two advantages.
- Currently, when a tuple is reloaded, the full tuple becomes live at the reload point, even if only a subset of its components is actually needed. On targets like AMDGPU, this creates difficulties later during the expansion of the reload pseudo-instruction into individual reload operations, because the unused or undefined subregisters still appear live. They are often patched with ad hoc fixups such as inserting implicit-def or implicit operands for the unneeded tuple components to avoid miscompilations. The subreg reload fixes this broken liveness info for partial uses of tuples chosen for spilling. It avoids introducing spurious undef subregs and eliminates the need for such hacky post-RA workarounds.
- Trimming down the registers really helps improve the allocation. Instead of the full tuple, we ensure RA reloads only the relevant subregs.
It is not clear to me how RA will see %reload = INSERT_SUBREG undef, .. (the one you suggested). We may miss the two advantages I mentioned here.
There was a problem hiding this comment.
The subreg reload fixes this broken liveness info for partial uses of tuples chosen for spilling
I agree, but I'm saying let's do the subreg reload but from the rest of the code this is as if the full register is reloaded.
Trimming down the registers really helps improve the allocation. Instead of the full tuple, we ensure RA reloads only the relevant subregs.
I agree as well. What I'm trying to push for is move the complexity of dealing with the subreg/regclass on the target itself (if possible) and in a dedicated helper for the cleanup (next sentence).
After %reload = INSERT_SUBREG undef, .. is generated, we can peephole it to generate the simplified sequence that you generate in one-shot here. That would separate the update of the register class and the subregister index in two different part.
| if (MO.isUse()) { | ||
| if (!OpPair.first->isRegTiedToDefOperand(OpPair.second)) | ||
| if (SubReg && !MI.isBundled()) | ||
| MO.setSubReg(0); |
There was a problem hiding this comment.
Just a thought that goes in the same direction as hiding the whole target complexity within insertReload (https://github.com/llvm/llvm-project/pull/175002/changes#r2723162638).
Could we expect target to return a full register?
E.g.,
; mir pseudo code
subreg = reload subreg
NewVReg = insert_subreg undef, subreg, subidx
= use NewVReg.subidx
That way we don't have to deal with the register class updates and the subregidx adjustment.
The question would be is this copy (insert_subreg) get properly folded later?
The reason I'm suggesting that is that I don't like the logic that this creates (maybe I need to stare at it longer), because I feel it is counter intuitive to do if subreg ==> setSubReg(0).
There was a problem hiding this comment.
I didn't quite follow what you proposed here with the pseudo code. Can you give an example, as I stated with the other comment?
There was a problem hiding this comment.
070b3e9 to
829c8e9
Compare
12d1aa0 to
841b269
Compare
829c8e9 to
ee9bc03
Compare
841b269 to
4bc23d4
Compare
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
ee9bc03 to
7a5fcfb
Compare
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
Currently, when a virtual register is partially used, the entire tuple is restored from the spilled location, even if only a subset of its sub-registers is needed. This patch introduces support for partial reloads by analyzing actual register usage and restoring only the required sub-registers. This improvement enhances register allocation efficiency, particularly for cases involving tuple virtual registers. For AMDGPU, this change brings considerable improvements in workloads that involve matrix operations, large vectors, and complex control flows.
Avoiding Lanebitemask manipulations. Moved most of generic the calculations to use TargetRegisterInfo APIs. Added a new API to get the covering subreg index given a lanebitmask.
4bc23d4 to
a23820b
Compare
8653a63 to
94a6d82
Compare
|
Ping. |

Currently, when a virtual register is partially used, the
entire tuple is restored from the spilled location, even if
only a subset of its sub-registers is needed. This patch
introduces support for partial reloads by analyzing actual
register usage and restoring only the required sub-registers.
This improvement enhances register allocation efficiency,
particularly for cases involving tuple virtual registers.
For AMDGPU, this change brings considerable improvements
in workloads that involve matrix operations, large vectors,
and complex control flows.