[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:47 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) &&
+ "expect element bitwidth to be power-of-two");
+ unsigned HalfElementWidth = ElementWidth / 2;
----------------
cardigan1008 wrote:
For i1 cases, it will produce 0 half width and cause a crash:
```llvm
define <2 x i1> @i1_zero_half(<4 x i1> %v) {
%d = call { <2 x i1>, <2 x i1> } @llvm.vector.deinterleave2.v4i1(<4 x i1> %v)
%f0 = extractvalue { <2 x i1>, <2 x i1> } %d, 0
%f1 = extractvalue { <2 x i1>, <2 x i1> } %d, 1
%low0 = and <2 x i1> %f0, zeroinitializer
%low1 = shl <2 x i1> %f1, zeroinitializer
%merge0 = or disjoint <2 x i1> %low0, %low1
%high0 = and <2 x i1> %f1, zeroinitializer
%high1 = lshr <2 x i1> %f0, zeroinitializer
%merge1 = or disjoint <2 x i1> %high0, %high1
%r = xor <2 x i1> %merge0, %merge1
ret <2 x i1> %r
}
declare { <2 x i1>, <2 x i1> } @llvm.vector.deinterleave2.v4i1(<4 x i1>)
```
Crash:
```sh
opt: llvm/lib/IR/Type.cpp:355: static llvm::IntegerType* llvm::IntegerType::get(llvm::LLVMContext&, unsigned int): Assertion `NumBits >= MIN_INT_BITS && "bitwidth too small"' failed.
```
> 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