Issue 243706
Summary RISC-V oggenc loop vectorized at too high of an LMUL
Labels backend:RISC-V, llvm:loopvectorize
Assignees
Reporter lukel97
    After #208621 there's some unprofitable vectorisation `oggenc`. Previously we didn't vectorize this accumulate_fit loop but now we do:

```c
typedef struct {
  long x0, x1, xa, ya, x2a, y2a, xya, an;
} lsfit_acc;

static int quant(const float *x) {
  int i = *x * 7.3142857f + 1023.5f;
  if (i > 1023) return 1023;
  if (i < 0) return 0;
  return i;
}

int accumulate_fit(const float *flr, const float *mdct, int x0, int x1,
 lsfit_acc *a, float atten, int fitweight) {
  long xa = 0, ya = 0, x2a = 0, y2a = 0, xya = 0, na = 0;
  long xb = 0, yb = 0, x2b = 0, y2b = 0, xyb = 0, nb = 0;
  for (long i = x0; i <= x1; i++) {
    int q = quant(flr + i);
    if (q) {
      if (mdct[i] + atten >= flr[i]) {
        xa += i; ya += q; x2a += i * i; y2a += q * q; xya += i * q; na++;
      } else {
 xb += i; yb += q; x2b += i * i; y2b += q * q; xyb += i * q; nb++;
 }
    }
  }
  xb += xa; yb += ya; x2b += x2a; y2b += y2a; xyb += xya; nb += na;
  int weight = nb * fitweight / (na + 1);
  a->xa = xa * weight + xb;
  a->ya = ya * weight + yb;
  a->x2a = x2a * weight + x2b;
  a->y2a = y2a * weight + y2b;
  a->xya = xya * weight + xyb;
  a->an = na * weight + nb;
  return na;
}
```

It unfortunately spills a good few vector registers which makes it overall slower

```asm
.LBB0_3: # %vector.body
                                        # =>This Inner Loop Header: Depth=1
	add	t1, a6, a2
	sh2add	t2, t1, a0
	vsetvli	t0, a3, e32, m1, ta, ma
	vle32.v	v9, (t2)
	vmv.v.v	v8, v9
	addi	t2, sp, 16
	vl1r.v	v10, (t2)                       # vscale x 8-byte Folded Reload
	vfmadd.vf	v8, fa5, v10
	vfcvt.rtz.x.f.v	v10, v8
	vmsgt.vi	v8, v10, 0
	vmv.v.v	v0, v8
	sh2add	t1, t1, a1
	vle32.v	v11, (t1), v0.t
	vfadd.vf	v11, v11, fa0
	vmfle.vv	v11, v9, v11
	vminu.vx	v1, v10, a7
	vmand.mm	v9, v8, v11
	vsetvli	zero, zero, e64, m2, ta, ma
	vzext.vf2	v2, v1
	vmv1r.v	v0, v9
	vmandn.mm	v8, v8, v11
	vsetvli	zero, zero, e64, m2, tu, mu
	vadd.vv	v12, v12, v4, v0.t
	csrr	t1, vlenb
	add	t1, t1, sp
	addi	t1, t1, 16
	vs2r.v	v12, (t1)                       # vscale x 16-byte Folded Spill
	vmv1r.v	v0, v8
	vsetvli	zero, zero, e64, m2, ta, ma
	vmul.vv	v12, v4, v2
	vmv2r.v	v10, v22
	vmv2r.v	v22, v20
	vmv2r.v	v20, v18
	vmv2r.v	v18, v16
	csrr	t1, vlenb
	sh2add	t1, t1, t1
	add	t1, t1, sp
	addi	t1, t1, 16
	vl2r.v	v16, (t1)                       # vscale x 16-byte Folded Reload
	vsetvli	zero, zero, e64, m2, tu, mu
	vadd.vi	v16, v16, 1, v0.t
	csrr	t1, vlenb
	sh2add	t1, t1, t1
	add	t1, t1, sp
	addi	t1, t1, 16
	vs2r.v	v16, (t1)                       # vscale x 16-byte Folded Spill
	vmv2r.v	v16, v18
	vmv2r.v	v18, v20
	vmv2r.v	v20, v22
	vmv2r.v	v22, v10
	vsetvli	zero, zero, e32, m1, ta, ma
	vmul.vv	v1, v1, v1
	vsetvli	zero, zero, e64, m2, ta, ma
	vmul.vv	v10, v4, v4
	vsetvli	zero, zero, e64, m2, tu, mu
	vadd.vv	v16, v12, v16, v0.t
	vsetvli	zero, zero, e32, m1, tu, mu
	vwaddu.wv	v18, v18, v1, v0.t
	vsetvli	zero, zero, e64, m2, tu, mu
	vadd.vv	v20, v10, v20, v0.t
	vadd.vv	v22, v22, v2, v0.t
	csrr	t1, vlenb
	sh1add	t1, t1, t1
	add	t1, t1, sp
	addi	t1, t1, 16
	vl2r.v	v14, (t1)                       # vscale x 16-byte Folded Reload
	vadd.vv	v14, v4, v14, v0.t
	csrr	t1, vlenb
	sh1add	t1, t1, t1
	add	t1, t1, sp
	addi	t1, t1, 16
	vs2r.v	v14, (t1) # vscale x 16-byte Folded Spill
	vmv1r.v	v0, v9
	vadd.vi	v24, v24, 1, v0.t
	vadd.vv	v26, v12, v26, v0.t
	csrr	t1, vlenb
	add	t1, t1, sp
	addi	t1, t1, 16
	vl2r.v	v12, (t1)                       # vscale x 16-byte Folded Reload
	vsetvli	zero, zero, e32, m1, tu, mu
	vwaddu.wv	v28, v28, v1, v0.t
	vsetvli	zero, zero, e64, m2, tu, mu
	vadd.vv	v30, v10, v30, v0.t
	vadd.vv	v6, v6, v2, v0.t
	vsetvli	zero, zero, e64, m2, ta, ma
	vadd.vx	v4, v4, t0
	sub	a3, a3, t0
	add	a6, a6, t0
	bnez	a3, .LBB0_3
```

I don't think #208621 is necessarily doing anything wrong but we're probably not estimating the VPlan register pressure correctly. I think we should be choosing a VF for LMUL=1 here. 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to