[llvm] [VectorCombine] Scalarize bin ops and cmps with two splatted operands (PR #137786)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 29 18:51:47 PDT 2025


lukel97 wrote:

> > In fact, I'm confused why the block which starts with "If one argument is a shuffle within one vector and the other is a constant" doesn't transform this case.
> 
> I think the reason is relatively subtle: because that rule currently matches the constant with `m_ImmConstant` which does _not_ seem to recognize ConstantExpr. And when a constant splat has scalable vector type, LLVM actually uses ConstantExpr under the hood

Yeah, and on top of that `scalarizeBinopOrCmp` also currently doesn't handle any form of shuffle, only inserts, so we need that canonicalization from instcombine where it pulls the shuffle out.

I agree that cramming two patterns into one transform is less than ideal. I really want to avoid making a separate transform since to handle the actual cases I'm seeing in the wild we need to extend scalarization to not just handle bin ops, but unary ops and intrinsics too, potentially with 3 operands e.g. in the case of `@llvm.fmuladd`. And this seems like a lot of duplication, with the potential for discrepancies between the two.

It actually looks like the canonicalization only fails on scalable vectors. E.g. 

```llvm
define <4 x i32> @fixed(i32 %x) {
  %x.insert = insertelement <4 x i32> poison, i32 %x, i64 0
  %x.splat = shufflevector <4 x i32> %x.insert, <4 x i32> poison, <4 x i32> zeroinitializer
  %res = add <4 x i32> %x.splat, splat (i32 42)
  ret <4 x i32> %res
}
```

Will get canonicalized to this:

```llvm
define <4 x i32> @fixed(i32 %x) {
  %x.insert = insertelement <4 x i32> poison, i32 %x, i64 0
  %1 = add <4 x i32> %x.insert, <i32 42, i32 poison, i32 poison, i32 poison>
  %res = shufflevector <4 x i32> %1, <4 x i32> poison, <4 x i32> zeroinitializer
  ret <4 x i32> %res
}
```

I will try canonicalizing scalable vectors this way in InstCombine and see what breaks. If that works then we don't need this PR.

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


More information about the llvm-commits mailing list