Skip to content

Commit 1f4c7b2

Browse files
frasercrmcktstellar
authored andcommitted
[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 (cherry picked from commit 3e678cb)
1 parent e19be41 commit 1f4c7b2

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
@@ -2117,7 +2117,8 @@ static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
21172117
// a single addi instruction.
21182118
if (((StepOpcode == ISD::MUL && isInt<12>(SplatStepVal)) ||
21192119
(StepOpcode == ISD::SHL && isUInt<5>(SplatStepVal))) &&
2120-
isPowerOf2_32(StepDenominator) && isInt<5>(Addend)) {
2120+
isPowerOf2_32(StepDenominator) &&
2121+
(SplatStepVal >= 0 || StepDenominator == 1) && isInt<5>(Addend)) {
21212122
SDValue VID = DAG.getNode(RISCVISD::VID_VL, DL, ContainerVT, Mask, VL);
21222123
// Convert right out of the scalable type so we can use standard ISD
21232124
// 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
@@ -727,17 +727,17 @@ define <4 x i8> @buildvec_not_vid_v4i8_2() {
727727
ret <4 x i8> <i8 3, i8 3, i8 1, i8 0>
728728
}
729729

730-
; FIXME: This is not a valid way to emit this vid sequence: shift-right for
731-
; division only works for non-negative numbers!
730+
; We match this as a VID sequence (-3 / 8) + 5 but choose not to introduce
731+
; division to compute it.
732732
define <16 x i8> @buildvec_not_vid_v16i8() {
733733
; CHECK-LABEL: buildvec_not_vid_v16i8:
734734
; CHECK: # %bb.0:
735+
; CHECK-NEXT: li a0, 3
735736
; CHECK-NEXT: vsetivli zero, 16, e8, m1, ta, mu
736-
; CHECK-NEXT: vid.v v8
737-
; CHECK-NEXT: li a0, -3
738-
; CHECK-NEXT: vmul.vx v8, v8, a0
739-
; CHECK-NEXT: vsrl.vi v8, v8, 3
740-
; CHECK-NEXT: vadd.vi v8, v8, 5
737+
; CHECK-NEXT: vmv.s.x v9, a0
738+
; CHECK-NEXT: vmv.v.i v8, 0
739+
; CHECK-NEXT: vsetivli zero, 7, e8, m1, tu, mu
740+
; CHECK-NEXT: vslideup.vi v8, v9, 6
741741
; CHECK-NEXT: ret
742742
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>
743743
}

0 commit comments

Comments
 (0)