Skip to content

Commit 758ac2a

Browse files
alexey-bataevtstellar
authored andcommitted
[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 (cherry picked from commit 23b6a05)
1 parent 19662e3 commit 758ac2a

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