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

[RISCV] Vector binary ops of splats aren't scalarized on fixed vectors

Open lukel97 opened this issue 3 years ago • 4 comments

The functions below multiply a vector with a splatted scalar, computed from an add:

define <4 x i64> @f_v4i64(<4 x i64> %x, i64 %y) {
  %1 = insertelement <4 x i64> poison, i64 %y, i32 0
  %2 = shufflevector <4 x i64> %1, <4 x i64> poison, <4 x i32> zeroinitializer
  %3 = add <4 x i64> %2, <i64 3, i64 3, i64 3, i64 3>
  %4 = mul <4 x i64> %x, %3
  ret <4 x i64> %4
}

define <vscale x 4 x i64> @f_nxv4i64(<vscale x 4 x i64> %x, i64 %y) {
  %1 = insertelement <vscale x 4 x i64> poison, i64 %y, i32 0
  %2 = shufflevector <vscale x 4 x i64> %1, <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
  %3 = add <vscale x 4 x i64> %2, shufflevector(<vscale x 4 x i64> insertelement(<vscale x 4 x i64> poison, i64 3, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer)
  %4 = mul <vscale x 4 x i64> %x, %3
  ret <vscale x 4 x i64> %4
}

When compiled with llc -o - -mattr=+v, the splatted add is scalarized for the scalable version:

f_nxv4i64:
	addi	a0, a0, 3
	vsetvli	a1, zero, e64, m4, ta, ma
	vmul.vx	v8, v8, a0
	ret

But the fixed version doesn't:

f_v4i64:
	vsetivli	zero, 4, e64, m2, ta, ma
	vmv.v.x	v10, a0
	vadd.vi	v10, v10, 3
	vmul.vv	v8, v8, v10
	ret

lukel97 avatar Aug 29 '23 13:08 lukel97

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

llvmbot avatar Aug 29 '23 13:08 llvmbot

candidate patch : https://reviews.llvm.org/D159190

ChunyuLiao avatar Aug 30 '23 13:08 ChunyuLiao

candidate patch : https://reviews.llvm.org/D159190 This patch is not a good solution.

Looking forward to a more generalized solution.

ChunyuLiao avatar Sep 08 '23 03:09 ChunyuLiao

Closing since VectorCombine will scalarize the fixed-length version in the middle end, so hopefully not a problem in practice

lukel97 avatar May 09 '25 14:05 lukel97