Skip to contentSkip to conversation

target/riscv: rvv: Fix unexpected behavior of vector reduction instructions when vl is 0

PATCH Applied Max Chou 4 participants 5 messages Jan 24, 2025 – Mar 6, 2025 RISC-V

lore
Share & tools
lore

Max Chou

author
#0 lore raw

PATCH 1/1 target/riscv: rvv: Fix unexpected behavior of vector reduction instructions when vl is 0

According to the Vector Reduction Operations section in the RISC-V "V" Vector Extension spec,
"If vl=0, no operation is performed and the destination register is not updated."

The vd should be updated when vl is larger than 0.

---
 target/riscv/vector_helper.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/vector_helper.c +6 -2
index 5386e3b97c5..7773df6a7c7 100644
@@ -4659,7 +4659,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
         }                                                 \
         s1 = OP(s1, (TD)s2);                              \
     }                                                     \
-    *((TD *)vd + HD(0)) = s1;                             \
+    if (vl > 0) {                                         \
+        *((TD *)vd + HD(0)) = s1;                         \
+    }                                                     \
     env->vstart = 0;                                      \
     /* set tail elements to 1s */                         \
     vext_set_elems_1s(vd, vta, esz, vlenb);               \
@@ -4745,7 +4747,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
         }                                                  \
         s1 = OP(s1, (TD)s2, &env->fp_status);              \
     }                                                      \
-    *((TD *)vd + HD(0)) = s1;                              \
+    if (vl > 0) {                                          \
+        *((TD *)vd + HD(0)) = s1;                          \
+    }                                                      \
     env->vstart = 0;                                       \
     /* set tail elements to 1s */                          \
     vext_set_elems_1s(vd, vta, esz, vlenb);                \
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Max Chou

According to the Vector Reduction Operations section in the RISC-V "V" Vector Extension spec,
"If vl=0, no operation is performed and the destination register is not updated."

The vd should be updated when vl is larger than 0.

Fixes fe5c9ab1fc ("target/riscv: vector single-width integer reduction instructions")
Fixes f714361ed7 ("target/riscv: rvv-1.0: implement vstart CSR")
Quoted 1 line
Quoted 30 lines
---
  target/riscv/vector_helper.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/vector_helper.c +6 -2
index 5386e3b97c5..7773df6a7c7 100644
@@ -4659,7 +4659,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
          }                                                 \
          s1 = OP(s1, (TD)s2);                              \
      }                                                     \
-    *((TD *)vd + HD(0)) = s1;                             \
+    if (vl > 0) {                                         \
+        *((TD *)vd + HD(0)) = s1;                         \
+    }                                                     \
      env->vstart = 0;                                      \
      /* set tail elements to 1s */                         \
      vext_set_elems_1s(vd, vta, esz, vlenb);               \
@@ -4745,7 +4747,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
          }                                                  \
          s1 = OP(s1, (TD)s2, &env->fp_status);              \
      }                                                      \
-    *((TD *)vd + HD(0)) = s1;                              \
+    if (vl > 0) {                                          \
+        *((TD *)vd + HD(0)) = s1;                          \
+    }                                                      \
      env->vstart = 0;                                       \
      /* set tail elements to 1s */                          \
      vext_set_elems_1s(vd, vta, esz, vlenb);                \
#2 lore raw
In reply to Max Chou
Quoted 9 lines · Max Chou

According to the Vector Reduction Operations section in the RISC-V "V" Vector Extension spec,
"If vl=0, no operation is performed and the destination register is not updated."

The vd should be updated when vl is larger than 0.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 34 lines
---
 target/riscv/vector_helper.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
target/riscv/vector_helper.c +6 -3
index 5386e3b97c5..7773df6a7c7 100644
@@ -4659,7 +4659,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,          \
         }                                                 \
         s1 = OP(s1, (TD)s2);                              \
     }                                                     \
-    *((TD *)vd + HD(0)) = s1;                             \
+    if (vl > 0) {                                         \
+        *((TD *)vd + HD(0)) = s1;                         \
+    }                                                     \
     env->vstart = 0;                                      \
     /* set tail elements to 1s */                         \
     vext_set_elems_1s(vd, vta, esz, vlenb);               \
@@ -4745,7 +4747,9 @@ void HELPER(NAME)(void *vd, void *v0, void *vs1,           \
         }                                                  \
         s1 = OP(s1, (TD)s2, &env->fp_status);              \
     }                                                      \
-    *((TD *)vd + HD(0)) = s1;                              \
+    if (vl > 0) {                                          \
+        *((TD *)vd + HD(0)) = s1;                          \
+    }                                                      \
     env->vstart = 0;                                       \
     /* set tail elements to 1s */                          \
     vext_set_elems_1s(vd, vta, esz, vlenb);                \
--

2.34.1

#3 lore raw
In reply to Max Chou
Quoted 6 lines · Max Chou

According to the Vector Reduction Operations section in the RISC-V "V" Vector Extension spec,
"If vl=0, no operation is performed and the destination register is not updated."

The vd should be updated when vl is larger than 0.

Is this a qemu-stable material?

If yes, how far to previous releases it is worth to pick? (Current older stable series are 7.2 and 8.2).

I currently picked this up for 7.2, 8.2 and 9.2.

Thanks,

/mjt

#4 lore raw
Quoted 10 lines · Michael Tokarev
Quoted 6 lines · Max Chou

According to the Vector Reduction Operations section in the RISC-V "V" Vector Extension spec,
"If vl=0, no operation is performed and the destination register is not updated."

The vd should be updated when vl is larger than 0.

Is this a qemu-stable material?

Yes

Quoted 5 lines

If yes, how far to previous releases it is worth to pick? (Current older stable series are 7.2 and 8.2).

I currently picked this up for 7.2, 8.2 and 9.2.

That's perfect

Alistair

Quoted 5 lines

Thanks,

/mjt