[llvm] [InstCombine][RISCV] Convert VPIntrinsics with splat operands to splats (PR #65706)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 12 03:55:17 PDT 2023


https://github.com/lukel97 commented:

This is looking pretty good. Just a note, not related to your patch, but about a missed scalarization in the existing non-vp scalarization: It only catches binops where both operands aren't constant, e.g. like this:

```llvm
define <vscale x 1 x i64> @f(i64 %x, i64 %y) {
  %head.x = insertelement <vscale x 1 x i64> poison, i64 %x, i32 0
  %splat.x = shufflevector <vscale x 1 x i64> %head.x, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
  %head.y = insertelement <vscale x 1 x i64> poison, i64 %y, i32 0
  %splat.y = shufflevector <vscale x 1 x i64> %head.y, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
  %v = add <vscale x 1 x i64> %splat.x, %splat.y
  ret <vscale x 1 x i64> %v
}
```

Because this happens to get canonicalised by instcombine into:

```llvm
define <vscale x 1 x i64> @f(i64 %x, i64 %y) #0 {
  %head.x = insertelement <vscale x 1 x i64> poison, i64 %x, i64 0
  %head.y = insertelement <vscale x 1 x i64> poison, i64 %y, i64 0
  %1 = add <vscale x 1 x i64> %head.x, %head.y
  %v = shufflevector <vscale x 1 x i64> %1, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
  ret <vscale x 1 x i64> %v
}
```

And scalarizeBinopOrCmp only looks for `insertelement`s. 

But if one of the operands of the binop is a constant:


```llvm
define <vscale x 1 x i64> @g(i64 %x) {
  %head.x = insertelement <vscale x 1 x i64> poison, i64 %x, i64 0
  %splat.x = shufflevector <vscale x 1 x i64> %head.x, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
  %splat.y = shufflevector <vscale x 1 x i64> insertelement(<vscale x 1 x i64> poison, i64 42, i32 0), <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
  %v = add <vscale x 1 x i64> %splat.x, %splat.y
  ret <vscale x 1 x i64> %v
}
```

Then the above canonicalisation doesn't happen, and it stays in the `shufflevector %x, poison, zeroinitializer` form. Which `scalarizeBinopOrCmp` doesn't handle.

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


More information about the llvm-commits mailing list