[llvm] [VectorCombine] Fold deinterleave2 with smaller effective element size (PR #192121)

Yunbo Ni via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 20:22:48 PDT 2026


================
@@ -5531,6 +5532,133 @@ bool VectorCombine::foldInterleaveIntrinsics(Instruction &I) {
   return true;
 }
 
+/// Given this sequence:
+/// ```
+/// %d = llvm.vector.deinterleave2 <vscale x 16 x i32> %v
+/// %f0 = extractvalue { <vscale x 8 x i32>, <vscale x 8 x i32> } %d, 0
+/// %f1 = extractvalue { <vscale x 8 x i32>, <vscale x 8 x i32> } %d, 1
+///
+/// %low0 = and <vscale x 8 x i32> %f0, splat (i32 65535)
+/// %low1 = shl <vscale x 8 x i32> %f1, splat (i32 16)
+/// %merge0 = or disjoint <vscale x 8 x i32> %low0, %low1
+///
+/// %high0 = and <vscale x 8 x i32> %f1, splat (i32 -65536)
+/// %high1 = lshr <vscale x 8 x i32> %f0, splat (i32 16)
+/// %merge1 = or disjoint <vscale x 8 x i32> %high0, %high1
+/// ```
+/// It is actually just de-interleaving a 16-bit vector with double the
+/// vector length. More generally speaking, it's de-interleaving on a vector
+/// with half the element width as the original vector.
+///
+/// Therefore, we can turn it into:
+/// ```
+/// %narrow.v = bitcast <vscale x 16 x i32> %v to <vscale x 32 x i16>
+/// %d = llvm.vector.deinterleave2 <vscale x 32 x i16> %narrow.v
+/// %f0 = extractvalue { <vscale x 16 x i16>, <vscale x 16 x i16> } %d, 0
+/// %f1 = extractvalue { <vscale x 16 x i16>, <vscale x 16 x i16> } %d, 1
+///
+/// %merge0 = bitcast <vscale x 16 x i16> %f0 to <vscale x 8 x i32>
+/// %merge1 = bitcast <vscale x 16 x i16> %f1 to <vscale x 8 x i32>
+/// ```
+bool VectorCombine::foldDeinterleaveIntrinsics(Instruction &I) {
+  using namespace PatternMatch;
+  Value *DeinterleavedVal;
+  if (!match(&I, m_Deinterleave2(m_Value(DeinterleavedVal))))
+    return false;
+
+  VectorType *VecTy = cast<VectorType>(DeinterleavedVal->getType());
+  IntegerType *ElementTy = dyn_cast<IntegerType>(VecTy->getElementType());
+  if (!ElementTy)
+    return false;
+  unsigned ElementWidth = ElementTy->getBitWidth();
+  assert(isPowerOf2_32(ElementWidth) &&
----------------
cardigan1008 wrote:

Should we use assertion here? Here is a triggered crash case:

```llvm
define <2 x i24> @i24_nonpow2(<4 x i24> %v) {
  %d = call { <2 x i24>, <2 x i24> } @llvm.vector.deinterleave2.v4i24(<4 x i24> %v)
  %f0 = extractvalue { <2 x i24>, <2 x i24> } %d, 0
  %f1 = extractvalue { <2 x i24>, <2 x i24> } %d, 1
  %low0 = and <2 x i24> %f0, <i24 4095, i24 4095>
  %low1 = shl <2 x i24> %f1, <i24 12, i24 12>
  %merge0 = or disjoint <2 x i24> %low0, %low1
  %high0 = and <2 x i24> %f1, <i24 -4096, i24 -4096>
  %high1 = lshr <2 x i24> %f0, <i24 12, i24 12>
  %merge1 = or disjoint <2 x i24> %high0, %high1
  %r = xor <2 x i24> %merge0, %merge1
  ret <2 x i24> %r
}
declare { <2 x i24>, <2 x i24> } @llvm.vector.deinterleave2.v4i24(<4 x i24>)
```

> Found with [Archer](https://github.com/cuhk-s3/Archer). Please let me know if anything is wrong.

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


More information about the llvm-commits mailing list