[llvm] [LV] Reduce register pressure of RISC-V outer loop reduction. (PR #208621)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 12 22:57:11 PDT 2026


lukel97 wrote:

> > Having this specific workaround in calculateRegisterUsage seems a bit hacky.
> > I wonder if scheduling the vp.merges to be immediately after their true operand would help reduce the live ranges instead?
> 
> This issue of the current register model is that the live range of the reduction PHI is too long. It should be end at the first user in the reduction chain. Even with this method (unless I missed something) the usage of the last reduction PHI still across entire vector loop which should be ended at the last in-loop reduction op (`%9`).

My understanding is that the register pressures are inaccurate because we have an extra live register across the smin/smax + vp.merges, when in reality this gets folded. Because all the smin/smax instructions are clustered together, followed by all the vp.merges, we end up increasing the maximum number of live registers:

```llvm
%x1 = smin ; live: x1, phi1, phi2, phi3, phi4
%x2 = smax ; live: x1, x2, phi1, phi2, phi3, phi4
%x3 = smin ; live: x1, x2, x3, phi1, phi2, phi3, phi4
%x4 = smax ; live: x1, x2, x3, x4, phi1, phi2, phi3, phi4
%y1 = vp.merge %x1, %phi1 ; live y1, x2, x3, x4, phi2, phi3, phi4 <--- max live regs = 7
%y2 = vp.merge %x2, %phi2 ; live y1, y2, x3, x4, phi3, phi4  
%y3 = vp.merge %x3, %phi3 ; live y1, y2, y3, x4, phi4
%y4 = vp.merge %x4, %phi4 ; live y1, y2, y3, y4 
```

But if we reschedule the vp.merges that extra live register won't be alive across the other live ranges, so we decrease the maximum number of live registers:

```llvm
%x1 = smin ; live: x1, phi1, phi2, phi3, phi4
%y1 = vp.merge %x1, %phi1 ; live y1, phi2, phi3, phi4 <---- max live regs = 4
%x2 = smax ; live: x2, y1, phi2, phi3, phi4
%y2 = vp.merge %x2, %phi2 ; live y1, y2, phi3, phi4
%x3 = smin ; live: x3, y1, y2, phi3, phi4
%y3 = vp.merge %x3, %phi3 ; live: y1, y2, y3, phi4
%x4 = smin ; live: x4, y1, y2, y3, phi4
%y4 = vp.merge %x4, %phi4 ; live: y1, y2, y3, y4
```

https://github.com/llvm/llvm-project/pull/208621


More information about the llvm-commits mailing list