Skip to content

[RISCV] Fix matching bug in VLA shuffle lowering #134750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5399,16 +5399,16 @@ static SDValue lowerDisjointIndicesShuffle(ShuffleVectorSDNode *SVN,
/// Is this mask local (i.e. elements only move within their local span), and
/// repeating (that is, the same rearrangement is being done within each span)?
static bool isLocalRepeatingShuffle(ArrayRef<int> Mask, int Span) {
SmallVector<int> LowSpan(Span, -1);
// Require a prefix from the original mask until the consumer code
// is adjusted to rewrite the mask instead of just taking a prefix.
SmallVector<int> LowSpan(Mask.take_front(Span));
for (auto [I, M] : enumerate(Mask)) {
if (M == -1)
continue;
if ((M / Span) != (int)(I / Span))
return false;
int SpanIdx = I % Span;
int Expected = M % Span;
if (LowSpan[SpanIdx] == -1)
LowSpan[SpanIdx] = Expected;
if (LowSpan[SpanIdx] != Expected)
return false;
}
Expand All @@ -5424,13 +5424,13 @@ static bool isLowSourceShuffle(ArrayRef<int> Mask, int Span) {
/// span, and then repeats that same result across all remaining spans. Note
/// that this doesn't check if all the inputs come from a single span!
static bool isSpanSplatShuffle(ArrayRef<int> Mask, int Span) {
SmallVector<int> LowSpan(Span, -1);
// Require a prefix from the original mask until the consumer code
// is adjusted to rewrite the mask instead of just taking a prefix.
SmallVector<int> LowSpan(Mask.take_front(Span));
for (auto [I, M] : enumerate(Mask)) {
if (M == -1)
continue;
int SpanIdx = I % Span;
if (LowSpan[SpanIdx] == -1)
LowSpan[SpanIdx] = M;
if (LowSpan[SpanIdx] != M)
return false;
}
Expand Down
31 changes: 20 additions & 11 deletions llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-int.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1372,36 +1372,45 @@ define <8 x i64> @shuffle_v8i164_span_splat(<8 x i64> %a) nounwind {
ret <8 x i64> %res
}

; FIXME: Doing this as a span spat requires rewriting the undef elements in
; the mask not just using a prefix of the mask.
; Doing this as a span splat requires rewriting the undef elements in the mask
; not just using a prefix of the mask.
define <8 x i64> @shuffle_v8i64_span_splat_neg(<8 x i64> %a) nounwind {
; CHECK-LABEL: shuffle_v8i64_span_splat_neg:
; CHECK: # %bb.0:
; CHECK-NEXT: csrr a0, vlenb
; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma
; CHECK-NEXT: vmv.v.i v9, 1
; CHECK-NEXT: vsetvli a0, zero, e64, m1, ta, ma
; CHECK-NEXT: srli a0, a0, 3
; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
; CHECK-NEXT: vslidedown.vx v10, v9, a0
; CHECK-NEXT: vsetvli a1, zero, e64, m1, ta, ma
; CHECK-NEXT: vrgatherei16.vv v13, v8, v10
; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
; CHECK-NEXT: vslidedown.vx v10, v10, a0
; CHECK-NEXT: vsetvli a1, zero, e64, m1, ta, ma
; CHECK-NEXT: vrgatherei16.vv v12, v8, v9
; CHECK-NEXT: vmv.v.v v13, v12
; CHECK-NEXT: vmv.v.v v14, v12
; CHECK-NEXT: vmv.v.v v15, v12
; CHECK-NEXT: vrgatherei16.vv v14, v8, v10
; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
; CHECK-NEXT: vslidedown.vx v9, v10, a0
; CHECK-NEXT: vsetvli a0, zero, e64, m1, ta, ma
; CHECK-NEXT: vrgatherei16.vv v15, v8, v9
; CHECK-NEXT: vmv4r.v v8, v12
; CHECK-NEXT: ret
%res = shufflevector <8 x i64> %a, <8 x i64> poison, <8 x i32> <i32 poison, i32 poison, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>
ret <8 x i64> %res
}

; FIXME: A locally repeating shuffle needs to use a mask prefix
; Doing this as a locally repeating shuffle requires rewriting the undef
; elements in the mask not just using a prefix of the mask.
define <8 x i32> @shuffle_v8i32_locally_repeating_neg(<8 x i32> %a) {
; CHECK-LABEL: shuffle_v8i32_locally_repeating_neg:
; CHECK: # %bb.0:
; CHECK-NEXT: lui a0, %hi(.LCPI87_0)
; CHECK-NEXT: addi a0, a0, %lo(.LCPI87_0)
; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma
; CHECK-NEXT: vle16.v v12, (a0)
; CHECK-NEXT: vsetvli a0, zero, e32, m1, ta, ma
; CHECK-NEXT: vrgatherei16.vv v11, v9, v12
; CHECK-NEXT: vrgatherei16.vv v10, v8, v12
; CHECK-NEXT: vmv2r.v v8, v10
; CHECK-NEXT: vmv.v.v v8, v10
; CHECK-NEXT: ret
%res = shufflevector <8 x i32> %a, <8 x i32> poison, <8 x i32> <i32 1, i32 0, i32 poison, i32 poison, i32 5, i32 4, i32 6, i32 6>
ret <8 x i32> %res
Expand Down