Skip to content

Commit 23b6a05

Browse files
[CG][RISCV]Fix shuffling of odd number of input vectors
If the input contains odd number of shuffled vectors, the 2 last shuffles are shuffled with the same first vector. Need to correctly process such situation: when the first vector is requested for the first time - extract it from the source vector, when it is requested the second time - reuse previous result. The second vector should be extracted in both cases. Fixes llvm#125269 Reviewers: topperc, preames Reviewed By: preames Pull Request: llvm#125693
1 parent e3abe94 commit 23b6a05

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -5214,17 +5214,21 @@ static SDValue lowerShuffleViaVRegSplitting(ShuffleVectorSDNode *SVN,
52145214
SmallDenseMap<unsigned, SDValue, 4> Values;
52155215
for (unsigned I : seq<unsigned>(Data.size())) {
52165216
const auto &[Idx1, Idx2, _] = Data[I];
5217-
if (Values.contains(Idx1)) {
5218-
assert(Idx2 != UINT_MAX && Values.contains(Idx2) &&
5219-
"Expected both indices to be extracted already.");
5220-
break;
5217+
// If the shuffle contains permutation of odd number of elements,
5218+
// Idx1 might be used already in the first iteration.
5219+
//
5220+
// Idx1 = shuffle Idx1, Idx2
5221+
// Idx1 = shuffle Idx1, Idx3
5222+
SDValue &V = Values.try_emplace(Idx1).first->getSecond();
5223+
if (!V)
5224+
V = ExtractValue(Idx1 >= NumOfSrcRegs ? V2 : V1,
5225+
(Idx1 % NumOfSrcRegs) * NumOpElts);
5226+
if (Idx2 != UINT_MAX) {
5227+
SDValue &V = Values.try_emplace(Idx2).first->getSecond();
5228+
if (!V)
5229+
V = ExtractValue(Idx2 >= NumOfSrcRegs ? V2 : V1,
5230+
(Idx2 % NumOfSrcRegs) * NumOpElts);
52215231
}
5222-
SDValue V = ExtractValue(Idx1 >= NumOfSrcRegs ? V2 : V1,
5223-
(Idx1 % NumOfSrcRegs) * NumOpElts);
5224-
Values[Idx1] = V;
5225-
if (Idx2 != UINT_MAX)
5226-
Values[Idx2] = ExtractValue(Idx2 >= NumOfSrcRegs ? V2 : V1,
5227-
(Idx2 % NumOfSrcRegs) * NumOpElts);
52285232
}
52295233
SDValue V;
52305234
for (const auto &[Idx1, Idx2, Mask] : Data) {

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-exact-vlen.ll

+28
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,31 @@ define void @shuffle_i256_ldst(ptr %p) vscale_range(2,2) {
431431
store <4 x i256> %res, ptr %p
432432
ret void
433433
}
434+
435+
define void @shuffle_3_input_vectors() vscale_range(4,4) {
436+
; CHECK-LABEL: shuffle_3_input_vectors:
437+
; CHECK: # %bb.0:
438+
; CHECK-NEXT: vsetvli a0, zero, e64, m8, ta, ma
439+
; CHECK-NEXT: vmv.v.i v8, 1
440+
; CHECK-NEXT: vsetivli zero, 1, e8, mf8, ta, ma
441+
; CHECK-NEXT: vmv.v.i v0, 6
442+
; CHECK-NEXT: vsetvli a0, zero, e64, m8, ta, ma
443+
; CHECK-NEXT: vmv.v.i v16, 0
444+
; CHECK-NEXT: vsetivli zero, 4, e64, m1, ta, mu
445+
; CHECK-NEXT: vslidedown.vi v20, v8, 1, v0.t
446+
; CHECK-NEXT: vslideup.vi v20, v9, 3
447+
; CHECK-NEXT: vslidedown.vi v21, v9, 1
448+
; CHECK-NEXT: vmv1r.v v22, v8
449+
; CHECK-NEXT: vsetvli a0, zero, e64, m8, ta, ma
450+
; CHECK-NEXT: vmsgt.vi v8, v16, 0
451+
; CHECK-NEXT: vsetvli zero, zero, e32, m4, ta, ma
452+
; CHECK-NEXT: vmv.x.s a0, v8
453+
; CHECK-NEXT: sb a0, 0(zero)
454+
; CHECK-NEXT: ret
455+
%1 = shufflevector <32 x i64> zeroinitializer, <32 x i64> splat (i64 1), <32 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 poison, i32 poison, i32 33, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
456+
%2 = icmp slt <32 x i64> zeroinitializer, %1
457+
%3 = bitcast <32 x i1> %2 to i32
458+
%4 = trunc i32 %3 to i8
459+
store i8 %4, ptr null, align 1
460+
ret void
461+
}

0 commit comments

Comments
 (0)