Skip to content

Commit ea3d0db

Browse files
committed
[VectorCombine] foldShuffleOfCastops - ensure we can scale shuffle masks between bitcasted vector types
Don't just assert that the src/dst vector element counts are multiples of one another - in general IR this can actually happen. Reported by @mikaelholmen
1 parent 7e7468c commit ea3d0db

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,12 @@ bool VectorCombine::foldShuffleOfCastops(Instruction &I) {
14831483
assert((NumDstElts == NumSrcElts || Opcode == Instruction::BitCast) &&
14841484
"Only bitcasts expected to alter src/dst element counts");
14851485

1486+
// Check for bitcasting of unscalable vector types.
1487+
// e.g. <32 x i40> -> <40 x i32>
1488+
if (NumDstElts != NumSrcElts && (NumSrcElts % NumDstElts) != 0 &&
1489+
(NumDstElts % NumSrcElts) != 0)
1490+
return false;
1491+
14861492
SmallVector<int, 16> NewMask;
14871493
if (NumSrcElts >= NumDstElts) {
14881494
// The bitcast is from wide to narrow/equal elements. The shuffle mask can

llvm/test/Transforms/VectorCombine/X86/shuffle-of-casts.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,21 @@ define <16 x i16> @revpair_bitcast_v4i32_v16i16(<4 x i32> %a0, <4 x i32> %a1) {
239239
ret <16 x i16> %r
240240
}
241241

242+
; negative - bitcasts (unscalable element counts)
243+
244+
define <4 x i32> @shuffle_bitcast_v32i40_v4i32(<32 x i40> %a0, <32 x i40> %a1) {
245+
; CHECK-LABEL: @shuffle_bitcast_v32i40_v4i32(
246+
; CHECK-NEXT: [[X0:%.*]] = bitcast <32 x i40> [[A0:%.*]] to <40 x i32>
247+
; CHECK-NEXT: [[X1:%.*]] = bitcast <32 x i40> [[A1:%.*]] to <40 x i32>
248+
; CHECK-NEXT: [[R:%.*]] = shufflevector <40 x i32> [[X0]], <40 x i32> [[X1]], <4 x i32> <i32 0, i32 42, i32 poison, i32 poison>
249+
; CHECK-NEXT: ret <4 x i32> [[R]]
250+
;
251+
%x0 = bitcast <32 x i40> %a0 to <40 x i32>
252+
%x1 = bitcast <32 x i40> %a1 to <40 x i32>
253+
%r = shufflevector <40 x i32> %x0, <40 x i32> %x1, <4 x i32> <i32 0, i32 42, i32 poison, i32 poison>
254+
ret <4 x i32> %r
255+
}
256+
242257
; negative - src type mismatch
243258

244259
define <8 x i32> @concat_sext_v4i8_v4i16_v8i32(<4 x i8> %a0, <4 x i16> %a1) {

0 commit comments

Comments
 (0)