[llvm] [AMDGPU] Break WAR Hazards Using Scavenged Registers to Assist Load Clustering (PR #216343)
Patrick Simmons via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 31 09:14:30 PDT 2026
linuxrocks123 wrote:
@arsenm I've addressed most of your comments. I didn't switch to `BitVector` because `bitset` is more efficient as it doesn't need runtime logic to deal with a variable size. It also allows cleaner code since it has overloads for `&`, `|`, and `~`, which `BitVector` does not have. (I wish it did.) I agree with you that the bitsets were too large, so I did make the `bitset` smaller by offsetting it against `VGPR0` and limiting it to the 1024 VGPR32 registers we currently have. We'll have to change that if we ever get more than 1024 VGPR32 registers.
Regarding some design comments, the scheduler's anti-dependence breaking doesn't cover this because the post-RA MachineScheduler's load-cluster mutation can reorder the loads but can't rename registers, so it can't remove the WAR/WAW anti-dependencies that register allocation introduced on the address computations. It just fails to cluster them. The generic anti-dependence breakers only run under the legacy PostRASchedulerList, and AMDGPU uses the MachineScheduler-based post-RA path, which has no AntiDepBreaker wired in. Even if it did, that breaker renames along the scheduler's critical path rather than targeting the address chains feeding clusterable loads, and it isn't occupancy-aware. This pass is the narrow, occupancy-bounded version of exactly that: rename the reused registers on those address chains so the existing cluster mutation can then do its job.
Regarding your comment about whether this should be a pre-rewrite optimization before liveness is discarded, the dependencies this breaks are an artifact of physical-register assignment. Before the virtual-register rewriter runs, those values are still distinct vregs and the anti-dependency doesn't exist yet, so there's nothing to break pre-rewrite. That forces it post-rewrite, where the vreg LiveIntervals are already gone, which is why it reconstructs physreg liveness locally with LiveRegUnits. If there's an existing post-rewrite insertion point that still maintains physical-register liveness, I'd rather hook into that and drop the ad-hoc reconstruction. Please suggest something if you have an idea.
Finally, you asked why `renameRegister` computes a subregister. The reason for this is that `OldReg` is usually a register tuple. The operands rewritten in the window don't all reference the whole tuple: many reference only a subregister of it. When renaming, an operand that used lane `From.sub0` must become `To.sub0`, not the full `To`. `renameRegister` finds the subreg index where the operand sits inside `From`, applies that same index to `To`, and returns `To` directly when the operand is exactly From. Assigning the candidate directly would only be correct if every operand referenced the full tuple. If there's a tidier helper for "map a subreg of A to the same subreg of B," I'm happy to switch to it.
https://github.com/llvm/llvm-project/pull/216343
More information about the llvm-commits
mailing list