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

Elvis Wang via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 19:38:20 PDT 2026


ElvisWang123 wrote:


> 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:

This method can reduce the register pressure but it's not quite accurate. The max usage for reductions in following example should be 4. The root cause of this case is that the live range of the reduction-phi is too long (until `vp.merge`) that overlap the live range of the in-loop op (`smin` in this case)

 ```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. <---- max live regs = 8
%y1 = vp.merge %x1, %phi1 ; live y1, x2, x3, x4, phi2, phi3, phi4  <--- max live regs = 7 (not the peak usage).
%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 
```

After reorder
```llvm
%x1 = smin ; live: x1, phi1, phi2, phi3, phi4 <---- max live regs = 5
%y1 = vp.merge %x1, %phi1 ; live y1, phi2, phi3, phi4 <---- max live regs = 4 (not the peak usage)
%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
```

> What I mean is, the chain tail of VPExpressionRecipe shouldn't be limited to VPReductionRecipe. We should be able to extend it so that vp.merge can also serve as the chain tail.
> By folding the in-loop operation and vp.merge into a single VPExpressionRecipe, we can ignore the cost of vp.merge and address the register pressure issue.

This is a possible approach but might needs lots of works. Currently the `VPExpressionRecipe` only accept the `VPReductionRecipe` as the last sub-recipes which is for the in-loop reductions.



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


More information about the llvm-commits mailing list