Skip to content

Commit 3e678cb

Browse files
committed
[RISCV] Don't emit fractional VIDs with negative steps
We can't shift-right negative numbers to divide them, so avoid emitting such sequences. Use negative numerators as a proxy for this situation, since the indices are always non-negative. An alternative strategy could be to add a compiler flag to emit division instructions, which would at least allow us to test the VID sequence matching itself. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D123796
1 parent 627e210 commit 3e678cb

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,8 @@ static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
21862186
// a single addi instruction.
21872187
if (((StepOpcode == ISD::MUL && isInt<12>(SplatStepVal)) ||
21882188
(StepOpcode == ISD::SHL && isUInt<5>(SplatStepVal))) &&
2189-
isPowerOf2_32(StepDenominator) && isInt<5>(Addend)) {
2189+
isPowerOf2_32(StepDenominator) &&
2190+
(SplatStepVal >= 0 || StepDenominator == 1) && isInt<5>(Addend)) {
21902191
SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, ContainerVT, Mask, VL);
21912192
// Convert right out of the scalable type so we can use standard ISD
21922193
// nodes for the rest of the computation. If we used scalable types with

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-int-buildvec.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,17 +746,17 @@ define <4 x i8> @buildvec_not_vid_v4i8_2() {
746746
ret <4 x i8> <i8 3, i8 3, i8 1, i8 0>
747747
}
748748

749-
; FIXME: This is not a valid way to emit this vid sequence: shift-right for
750-
; division only works for non-negative numbers!
749+
; We match this as a VID sequence (-3 / 8) + 5 but choose not to introduce
750+
; division to compute it.
751751
define <16 x i8> @buildvec_not_vid_v16i8() {
752752
; CHECK-LABEL: buildvec_not_vid_v16i8:
753753
; CHECK: # %bb.0:
754+
; CHECK-NEXT: li a0, 3
754755
; CHECK-NEXT: vsetivli zero, 16, e8, m1, ta, mu
755-
; CHECK-NEXT: vid.v v8
756-
; CHECK-NEXT: li a0, -3
757-
; CHECK-NEXT: vmul.vx v8, v8, a0
758-
; CHECK-NEXT: vsrl.vi v8, v8, 3
759-
; CHECK-NEXT: vadd.vi v8, v8, 5
756+
; CHECK-NEXT: vmv.s.x v9, a0
757+
; CHECK-NEXT: vmv.v.i v8, 0
758+
; CHECK-NEXT: vsetivli zero, 7, e8, m1, tu, mu
759+
; CHECK-NEXT: vslideup.vi v8, v9, 6
760760
; CHECK-NEXT: ret
761761
ret <16 x i8> <i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 3, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 undef, i8 0, i8 0>
762762
}

0 commit comments

Comments
 (0)