llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Teach RISCVVLOptimizer to reduce the VL of reduction pseudo's input vectors

Open lukel97 opened this issue 9 months ago • 7 comments

This is from the discussion in https://github.com/llvm/llvm-project/pull/179377#discussion_r2760501434

The RISCVVLOptimizer should be able to reduce the VL of a reduction pseudo's inputs. For example in this case below, %x's VL can be reduced to 2 and %y's VL can be reduced to 1 (only the first lane is read):

---
name: vredsum_vv_reduce_input
body: |
  bb.0:
    liveins: $x1
    ; CHECK-LABEL: name: vredsum_vv_reduce_input
    ; CHECK: liveins: $x1
    ; CHECK-NEXT: {{  $}}
    ; CHECK-NEXT: %vl:gprnox0 = COPY $x1
    ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1 /* vl=VLMAX */, 5 /* e32 */, 0 /* tu, mu */
    ; CHECK-NEXT: %y:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1 /* vl=VLMAX */, 5 /* e32 */, 0 /* tu, mu */
    ; CHECK-NEXT: %z:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, $noreg, 2 /* vl */, 6 /* e64 */, 0 /* tu, mu */
    ; CHECK-NEXT: $v8 = COPY %z
    %vl:gprnox0 = COPY $x1
    %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
    %y:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
    %z:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, %y, 2, 6 /* e64 */, 0 /* tu, mu */
    $v8 = COPY %z
...

Teaching the RISCVVLOptimizer to handle this would help with removing tryToReduceVL in RISCVVectorPeephole.

lukel97 avatar Feb 12 '26 17:02 lukel97

@llvm/issue-subscribers-backend-risc-v

Author: Luke Lau (lukel97)

This is from the discussion in https://github.com/llvm/llvm-project/pull/179377#discussion_r2760501434

The RISCVVLOptimizer should be able to reduce the VL of a reduction pseudo's inputs. For example in this case below, %x's VL can be reduced to 2 and %y's VL can be reduced to 1 (only the first lane is read):

---
name: vredsum_vv_reduce_input
body: |
  bb.0:
    liveins: $x1
    ; CHECK-LABEL: name: vredsum_vv_reduce_input
    ; CHECK: liveins: $x1
    ; CHECK-NEXT: {{  $}}
    ; CHECK-NEXT: %vl:gprnox0 = COPY $x1
    ; CHECK-NEXT: %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1 /* vl=VLMAX */, 5 /* e32 */, 0 /* tu, mu */
    ; CHECK-NEXT: %y:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1 /* vl=VLMAX */, 5 /* e32 */, 0 /* tu, mu */
    ; CHECK-NEXT: %z:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, $noreg, 2 /* vl */, 6 /* e64 */, 0 /* tu, mu */
    ; CHECK-NEXT: $v8 = COPY %z
    %vl:gprnox0 = COPY $x1
    %x:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
    %y:vr = PseudoVADD_VV_M1 $noreg, $noreg, $noreg, -1, 5 /* e32 */, 0 /* tu, mu */
    %z:vr = PseudoVREDSUM_VS_M1_E64 $noreg, %x, %y, 2, 6 /* e64 */, 0 /* tu, mu */
    $v8 = COPY %z
...

Teaching the RISCVVLOptimizer to handle this would help with removing tryToReduceVL in RISCVVectorPeephole.

llvmbot avatar Feb 12 '26 17:02 llvmbot

Hey, I'm new to the RISC-V backend but I would like to get involved. May I be assigned this issue?

KavinTheG avatar Feb 12 '26 17:02 KavinTheG

@KavinTheG I think the LLVM policy on assigning issues to newer contributors is changing - I suggest if you're interested that you get started on a PR without this being assigned.

lenary avatar Feb 12 '26 19:02 lenary

Thanks for the advice, lenary. I'll try giving it a go.

KavinTheG avatar Feb 12 '26 21:02 KavinTheG

Quick question, but were the EEWs for the PseudoVREDSUM and PseudoVADD intended to be different in the example? I believe that the peephole optimizer is also skipping if the EEWs are not the same.

KavinTheG avatar Feb 14 '26 01:02 KavinTheG

Quick question, but were the EEWs for the PseudoVREDSUM and PseudoVADD intended to be different in the example? I believe that the peephole optimizer is also skipping if the EEWs are not the same.

Woops, yes they should be the same. Trying out the correct EEW though it looks like VLOptimizer can already reduce its input operands. I think RISCVVLOptimizer::getMinimumVLForUser can already propagate vl for vs1 and 1 for vs2, because of isVectorOpUsedAsScalarOp.

But I can see one test case in llvm/test/CodeGen/RISCV/rvv/combine-reduce-add-to-vcpop.ll where we're not reducing vl if we comment out RISCVectorPeephole::tryToReduceVL, I guess this is the case that @mshockwave was mentioning:

--- a/llvm/test/CodeGen/RISCV/rvv/combine-reduce-add-to-vcpop.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/combine-reduce-add-to-vcpop.ll
@@ -194,10 +194,8 @@ define i32 @test_nxv1i1(<vscale x 1 x i1> %x) {
 ; ZVE-NEXT:    vsetvli a0, zero, e32, m1, ta, ma
 ; ZVE-NEXT:    vmv.v.i v8, 0
 ; ZVE-NEXT:    csrr a0, vlenb
-; ZVE-NEXT:    srli a0, a0, 3
-; ZVE-NEXT:    vsetvli zero, a0, e32, m1, ta, ma
 ; ZVE-NEXT:    vmerge.vim v8, v8, 1, v0
-; ZVE-NEXT:    vsetivli zero, 1, e32, m1, ta, ma
+; ZVE-NEXT:    srli a0, a0, 3
 ; ZVE-NEXT:    vmv.s.x v9, zero
 ; ZVE-NEXT:    vsetvli zero, a0, e32, m1, ta, ma
 ; ZVE-NEXT:    vredsum.vs v9, v8, v9

lukel97 avatar Feb 15 '26 03:02 lukel97

The case above doesn't get optimized because we don't sink instructions if the VL doesn't dominate them, unlike RISCVVectorPeephole. But that's a separate issue so closing for now

lukel97 avatar Feb 16 '26 03:02 lukel97