Skip to contentSkip to conversation

hexagon: fix HVX scatter gather, BE host

PATCH Superseded Brian Cain 3 participants 9 messages Aug 19 – Aug 20

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

cover hexagon: fix HVX scatter gather, BE host

Anton pointed out this endianness issue with HVX,
and found this region length issue along the way.

Brian Cain (3):
target/hexagon: fix HVX big-endian byte access target/hexagon: fix HVX scatter/gather region-length check tests/tcg/hexagon: add vgather/vscatter region-length tests

 target/hexagon/mmvec/macros.h           |  42 ++++----
 target/hexagon/mmvec/mmvec.h            |  18 ++++
 target/hexagon/cpu.c                    |  17 ++--
 target/hexagon/genptr.c                 |   3 +-
 target/hexagon/mmvec/system_ext_mmvec.c |   2 +-
 target/hexagon/op_helper.c              |  17 +++-
 tests/tcg/hexagon/scatter_gather.c      | 125 +++++++++++++++++++++++-
 7 files changed, 191 insertions(+), 33 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 target/hexagon: fix HVX big-endian byte access

Keep HVX lanes host-native while converting byte indexes at memory and helper boundaries.

Also place vector comparison predicate bytes according to host word order.

---
 target/hexagon/mmvec/macros.h           | 22 ++++++++++++++--------
 target/hexagon/mmvec/mmvec.h            | 18 ++++++++++++++++++
 target/hexagon/cpu.c                    | 17 ++++++++++-------
 target/hexagon/genptr.c                 |  3 ++-
 target/hexagon/mmvec/system_ext_mmvec.c |  2 +-
 target/hexagon/op_helper.c              | 17 ++++++++++++-----
 6 files changed, 57 insertions(+), 22 deletions(-)
target/hexagon/mmvec/macros.h +14 -8
index b36f833b1de..0ed35720eea 100644
@@ -53,7 +53,7 @@
 
 #define LOG_VTCM_BYTE(VA, MASK, VAL, IDX) \
     do { \
-        env->vtcm_log.data.ub[IDX] = (VAL); \
+        mmvec_set_byte(&env->vtcm_log.data, IDX, VAL); \
         if (MASK) { \
             set_bit((IDX), env->vtcm_log.mask); \
         } else { \
@@ -133,7 +133,8 @@
         target_ulong va_high = EA + LEN; \
         for (int i0 = 0; i0 < 4; i0++) { \
             log_byte = (va + i0) <= va_high; \
-            LOG_VTCM_BYTE(va + i0, log_byte, INC. ub[4 * IDX + i0], \
+            LOG_VTCM_BYTE(va + i0, log_byte, \
+                           hexagon_mmvec_get_byte(&(INC), 4 * IDX + i0), \
                           4 * IDX + i0); \
         } \
     } while (0)
@@ -144,7 +145,8 @@
         target_ulong va_high = EA + LEN; \
         for (int i0 = 0; i0 < 2; i0++) { \
             log_byte = (va + i0) <= va_high; \
-            LOG_VTCM_BYTE(va + i0, log_byte, INC.ub[2 * IDX + i0], \
+            LOG_VTCM_BYTE(va + i0, log_byte, \
+                           hexagon_mmvec_get_byte(&(INC), 2 * IDX + i0), \
                           2 * IDX + i0); \
         } \
     } while (0)
@@ -157,7 +159,8 @@
         target_ulong va_high = EA + LEN; \
         for (int i0 = 0; i0 < 2; i0++) { \
             log_byte = (va + i0) <= va_high; \
-            LOG_VTCM_BYTE(va + i0, log_byte, INC.ub[2 * IDX + i0], \
+            LOG_VTCM_BYTE(va + i0, log_byte, \
+                           hexagon_mmvec_get_byte(&(INC), 2 * IDX + i0), \
                           2 * IDX + i0); \
         } \
     } while (0)
@@ -174,7 +177,7 @@
             log_byte = ((va + i0) <= va_high) && QVAL; \
             uint8_t B; \
             B = cpu_ldub_data_ra(env, EA + i0, ra); \
-            env->tmp_VRegs[0].ub[ELEMENT_SIZE * IDX + i0] = B; \
+            mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \
             LOG_VTCM_BYTE(va + i0, log_byte, B, ELEMENT_SIZE * IDX + i0); \
         } \
     } while (0)
@@ -216,9 +219,10 @@
                     uint8_t val; \
                     val = cpu_ldub_data_ra(env, env->vtcm_log.va[i + j], ra); \
                     dst |= val << (8 * j); \
-                    inc |= env->vtcm_log.data.ub[j + i] << (8 * j); \
+                    inc |= hexagon_mmvec_get_byte(&env->vtcm_log.data, j + i) \
+                           << (8 * j); \
                     clear_bit(j + i, env->vtcm_log.mask); \
-                    env->vtcm_log.data.ub[j + i] = 0; \
+                    mmvec_set_byte(&env->vtcm_log.data, j + i, 0); \
                 } \
                 dst += inc; \
                 for (int j = 0; j < sizeof(TYPE); j++) { \
@@ -249,7 +253,9 @@
         int log_byte = 0; \
         for (i0 = 0; i0 < ELEM_SIZE; i0++) { \
             log_byte = ((va + i0) <= va_high) && QVAL; \
-            LOG_VTCM_BYTE(va + i0, log_byte, IN.ub[ELEM_SIZE * IDX + i0], \
+            LOG_VTCM_BYTE(va + i0, log_byte, \
+                           hexagon_mmvec_get_byte(&(IN), \
+                                                    ELEM_SIZE * IDX + i0), \
                           ELEM_SIZE * IDX + i0); \
         } \
     } while (0)
target/hexagon/mmvec/mmvec.h +18 -0
index 8e72f2f6ae7..a8a777812b9 100644
@@ -20,6 +20,7 @@
 
 #include "exec/target_long.h"
 #include "qemu/bitmap.h"
+#include "qemu/bitops.h"
 
 #define MAX_VEC_SIZE_LOGBYTES 7
 #define MAX_VEC_SIZE_BYTES  (1 << MAX_VEC_SIZE_LOGBYTES)
@@ -69,6 +70,23 @@ typedef union {
     int8_t    b[MAX_VEC_SIZE_BYTES / 1 / 8];
 } MMQReg;
 
+/* Map architectural byte indexes over host-native HVX lanes. */
+static inline uint8_t hexagon_mmvec_get_byte(const MMVector *v, size_t index)
+{
+    return extract64(v->ud[index / 8], (index % 8) * 8, 8);
+}
+
+static inline void mmvec_set_byte(MMVector *v, size_t index, uint8_t value)
+{
+    v->ud[index / 8] = deposit64(v->ud[index / 8], (index % 8) * 8, 8,
+                                 value);
+}
+
+static inline uint8_t mmqreg_get_byte(const MMQReg *q, size_t index)
+{
+    return extract64(q->ud[index / 8], (index % 8) * 8, 8);
+}
+
 typedef struct {
     MMVector data;
     DECLARE_BITMAP(mask, MAX_VEC_SIZE_BYTES);
target/hexagon/cpu.c +10 -7
index 7067e5b70f7..8d83700db24 100644
@@ -182,7 +182,7 @@ static void print_vreg(FILE *f, CPUHexagonState *env, int regnum,
     if (skip_if_zero) {
         bool nonzero_found = false;
         for (int i = 0; i < MAX_VEC_SIZE_BYTES; i++) {
-            if (env->VRegs[regnum].ub[i] != 0) {
+            if (hexagon_mmvec_get_byte(&env->VRegs[regnum], i) != 0) {
                 nonzero_found = true;
                 break;
             }
@@ -193,9 +193,12 @@ static void print_vreg(FILE *f, CPUHexagonState *env, int regnum,
     }
 
     qemu_fprintf(f, "  v%d = ( ", regnum);
-    qemu_fprintf(f, "0x%02x", env->VRegs[regnum].ub[MAX_VEC_SIZE_BYTES - 1]);
+    qemu_fprintf(f, "0x%02x",
+                 hexagon_mmvec_get_byte(&env->VRegs[regnum],
+                                        MAX_VEC_SIZE_BYTES - 1));
     for (int i = MAX_VEC_SIZE_BYTES - 2; i >= 0; i--) {
-        qemu_fprintf(f, ", 0x%02x", env->VRegs[regnum].ub[i]);
+        qemu_fprintf(f, ", 0x%02x",
+                     hexagon_mmvec_get_byte(&env->VRegs[regnum], i));
     }
     qemu_fprintf(f, " )\n");
 }
@@ -211,7 +214,7 @@ static void print_qreg(FILE *f, CPUHexagonState *env, int regnum,
     if (skip_if_zero) {
         bool nonzero_found = false;
         for (int i = 0; i < MAX_VEC_SIZE_BYTES / 8; i++) {
-            if (env->QRegs[regnum].ub[i] != 0) {
+            if (mmqreg_get_byte(&env->QRegs[regnum], i) != 0) {
                 nonzero_found = true;
                 break;
             }
@@ -222,10 +225,10 @@ static void print_qreg(FILE *f, CPUHexagonState *env, int regnum,
     }
 
     qemu_fprintf(f, "  q%d = ( ", regnum);
-    qemu_fprintf(f, "0x%02x",
-                 env->QRegs[regnum].ub[MAX_VEC_SIZE_BYTES / 8 - 1]);
+    qemu_fprintf(f, "0x%02x", mmqreg_get_byte(&env->QRegs[regnum],
+                 MAX_VEC_SIZE_BYTES / 8 - 1));
     for (int i = MAX_VEC_SIZE_BYTES / 8 - 2; i >= 0; i--) {
-        qemu_fprintf(f, ", 0x%02x", env->QRegs[regnum].ub[i]);
+        qemu_fprintf(f, ", 0x%02x", mmqreg_get_byte(&env->QRegs[regnum], i));
     }
     qemu_fprintf(f, " )\n");
 }
target/hexagon/genptr.c +2 -1
index 2a98b13b714..5de8fe43f0b 100644
@@ -1528,7 +1528,8 @@ static void vec_to_qvec(size_t size, intptr_t dstoff, intptr_t srcoff)
             tcg_gen_deposit_i64(mask, mask, bits, j, size);
         }
 
-        tcg_gen_st8_i64(mask, tcg_env, dstoff + i);
+        tcg_gen_st8_i64(mask, tcg_env,
+                        dstoff + (i ^ (HOST_BIG_ENDIAN ? 3 : 0)));
     }
 }
 
target/hexagon/mmvec/system_ext_mmvec.c +1 -1
index 8351f2cc01b..081cadd814a 100644
@@ -26,7 +26,7 @@ void mem_gather_store(CPUHexagonState *env, target_ulong vaddr, int slot)
     env->vstore_pending[slot] = 1;
     env->vstore[slot].va   = vaddr;
     env->vstore[slot].size = size;
-    memcpy(&env->vstore[slot].data.ub[0], &env->tmp_VRegs[0], size);
+    memcpy(&env->vstore[slot].data, &env->tmp_VRegs[0], size);
 
     /* On a gather store, overwrite the store mask to emulate dropped gathers */
     bitmap_copy(env->vstore[slot].mask, env->vtcm_log.mask, size);
target/hexagon/op_helper.c +12 -5
index 23894ff3d28..33b0efd9e43 100644
@@ -146,7 +146,10 @@ void HELPER(commit_hvx_stores)(CPUHexagonState *env)
             int size = env->vstore[i].size;
             for (int j = 0; j < size; j++) {
                 if (test_bit(j, env->vstore[i].mask)) {
-                    cpu_stb_data_ra(env, va + j, env->vstore[i].data.ub[j], ra);
+                    cpu_stb_data_ra(env, va + j,
+                                    hexagon_mmvec_get_byte(&env->vstore[i].data,
+                                                           j),
+                                    ra);
                 }
             }
         }
@@ -169,9 +172,11 @@ void HELPER(commit_hvx_stores)(CPUHexagonState *env)
             for (int i = 0; i < sizeof(MMVector); i++) {
                 if (test_bit(i, env->vtcm_log.mask)) {
                     cpu_stb_data_ra(env, env->vtcm_log.va[i],
-                                    env->vtcm_log.data.ub[i], ra);
+                                     hexagon_mmvec_get_byte(&env->vtcm_log.data,
+                                                            i),
+                                     ra);
                     clear_bit(i, env->vtcm_log.mask);
-                    env->vtcm_log.data.ub[i] = 0;
+                    mmvec_set_byte(&env->vtcm_log.data, i, 0);
                 }
 
             }
@@ -1370,7 +1375,8 @@ void HELPER(vhist)(CPUHexagonState *env)
 
     for (int lane = 0; lane < 8; lane++) {
         for (int i = 0; i < sizeof(MMVector) / 8; ++i) {
-            unsigned char value = input->ub[(sizeof(MMVector) / 8) * lane + i];
+            unsigned char value = hexagon_mmvec_get_byte(input,
+                (sizeof(MMVector) / 8) * lane + i);
             unsigned char regno = value >> 3;
             unsigned char element = value & 7;
 
@@ -1385,7 +1391,8 @@ void HELPER(vhistq)(CPUHexagonState *env)
 
     for (int lane = 0; lane < 8; lane++) {
         for (int i = 0; i < sizeof(MMVector) / 8; ++i) {
-            unsigned char value = input->ub[(sizeof(MMVector) / 8) * lane + i];
+            unsigned char value = hexagon_mmvec_get_byte(input,
+                (sizeof(MMVector) / 8) * lane + i);
             unsigned char regno = value >> 3;
             unsigned char element = value & 7;
 
Signature · 1 line
2.34.1
Reviewed-by Anton Johansson on v1 1/3
#2 lore raw

Re: [PATCH 1/3] target/hexagon: fix HVX big-endian byte access

Quoted 7 lines · Brian Cain

Keep HVX lanes host-native while converting byte indexes at memory and helper boundaries.

Also place vector comparison predicate bytes according to host word order.

Quoted 8 lines
---
 target/hexagon/mmvec/macros.h           | 22 ++++++++++++++--------
 target/hexagon/mmvec/mmvec.h            | 18 ++++++++++++++++++
 target/hexagon/cpu.c                    | 17 ++++++++++-------
 target/hexagon/genptr.c                 |  3 ++-
 target/hexagon/mmvec/system_ext_mmvec.c |  2 +-
 target/hexagon/op_helper.c              | 17 ++++++++++++-----
 6 files changed, 57 insertions(+), 22 deletions(-)

Wouldn't have thought of using extract, nice!

#3 lore raw

Re: [PATCH 1/3] target/hexagon: fix HVX big-endian byte access

In reply to Brian Cain
Quoted 4 lines · On Wed, 19 Aug 2026 16:09:37 -0700 Brian Cain <[email protected]>…
target/hexagon/mmvec/mmvec.h +0 -0
index 8e72f2f6ae7..a8a777812b9 100644

target/hexagon/mmvec/mmvec.h typedef union { · line 79

Quoted 10 lines
target/hexagon/mmvec/mmvec.h +7 -0
@@ -69,6 +70,23 @@ typedef union {
     int8_t    b[MAX_VEC_SIZE_BYTES / 1 / 8];
 } MMQReg;
 
+/* Map architectural byte indexes over host-native HVX lanes. */
+static inline uint8_t hexagon_mmvec_get_byte(const MMVector *v, size_t index)
+{
+    return extract64(v->ud[index / 8], (index % 8) * 8, 8);
+}
+
+static inline void mmvec_set_byte(MMVector *v, size_t index, uint8_t value)

Should this be named hexagon_mmvec_set_byte to match the getter?

target/hexagon/mmvec/mmvec.h typedef union { · line 88

Quoted 9 lines
target/hexagon/mmvec/mmvec.h +9 -0
+{
+    v->ud[index / 8] = deposit64(v->ud[index / 8], (index % 8) * 8, 8,
+                                 value);
+}
+
+static inline uint8_t mmqreg_get_byte(const MMQReg *q, size_t index)
+{
+    return extract64(q->ud[index / 8], (index % 8) * 8, 8);
+}

For QRegs, I think this should be:

extract32(q->uw[index / 4], (index % 4) * 8, 8)

author
#4 lore raw

Re: [PATCH 1/3] target/hexagon: fix HVX big-endian byte access

Quoted 27 lines · Matheus Tavares Bernardino

target/hexagon/mmvec/mmvec.h typedef union { · line 79

Quoted 13 lines · On Wed, 19 Aug 2026 16:09:37 -0700 Brian Cain <[email protected]>…
target/hexagon/mmvec/mmvec.h +7 -0
index 8e72f2f6ae7..a8a777812b9 100644
@@ -69,6 +70,23 @@ typedef union {
      int8_t    b[MAX_VEC_SIZE_BYTES / 1 / 8];
  } MMQReg;
 
+/* Map architectural byte indexes over host-native HVX lanes. */
+static inline uint8_t hexagon_mmvec_get_byte(const MMVector *v, size_t index)
+{
+    return extract64(v->ud[index / 8], (index % 8) * 8, 8);
+}
+
+static inline void mmvec_set_byte(MMVector *v, size_t index, uint8_t value)

Should this be named hexagon_mmvec_set_byte to match the getter?

target/hexagon/mmvec/mmvec.h typedef union { · line 88

Quoted 9 lines
target/hexagon/mmvec/mmvec.h +9 -0
+{
+    v->ud[index / 8] = deposit64(v->ud[index / 8], (index % 8) * 8, 8,
+                                 value);
+}
+
+static inline uint8_t mmqreg_get_byte(const MMQReg *q, size_t index)
+{
+    return extract64(q->ud[index / 8], (index % 8) * 8, 8);
+}

For QRegs, I think this should be:

Quoted 1 line

extract32(q->uw[index / 4], (index % 4) * 8, 8)

You're right about both of the above, I'll fix these.

author
#5 lore raw

PATCH 2/3 target/hexagon: fix HVX scatter/gather region-length check

In reply to Brian Cain

The region-length bounds check compared (EA + i0) <= (EA + LEN), which reduces to i0 <= LEN and never actually depends on the region base. This let vgather/vscatter keep elements whose offset was beyond the declared region instead of dropping them. Compare the offset to the region length directly instead.

---
 target/hexagon/mmvec/macros.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
target/hexagon/mmvec/macros.h +10 -10
index 0ed35720eea..7ee6f5e62fb 100644
@@ -130,9 +130,9 @@
     do { \
         int log_byte = 0; \
         target_ulong va = EA; \
-        target_ulong va_high = EA + LEN; \
+        int in_region = (OFFSET) <= (LEN); \
         for (int i0 = 0; i0 < 4; i0++) { \
-            log_byte = (va + i0) <= va_high; \
+            log_byte = in_region; \
             LOG_VTCM_BYTE(va + i0, log_byte, \
                            hexagon_mmvec_get_byte(&(INC), 4 * IDX + i0), \
                           4 * IDX + i0); \
@@ -142,9 +142,9 @@
     do { \
         int log_byte = 0; \
         target_ulong va = EA; \
-        target_ulong va_high = EA + LEN; \
+        int in_region = (OFFSET) <= (LEN); \
         for (int i0 = 0; i0 < 2; i0++) { \
-            log_byte = (va + i0) <= va_high; \
+            log_byte = in_region; \
             LOG_VTCM_BYTE(va + i0, log_byte, \
                            hexagon_mmvec_get_byte(&(INC), 2 * IDX + i0), \
                           2 * IDX + i0); \
@@ -156,9 +156,9 @@
     do { \
         int log_byte = 0; \
         target_ulong va = EA; \
-        target_ulong va_high = EA + LEN; \
+        int in_region = (OFFSET) <= (LEN); \
         for (int i0 = 0; i0 < 2; i0++) { \
-            log_byte = (va + i0) <= va_high; \
+            log_byte = in_region; \
             LOG_VTCM_BYTE(va + i0, log_byte, \
                            hexagon_mmvec_get_byte(&(INC), 2 * IDX + i0), \
                           2 * IDX + i0); \
@@ -170,11 +170,11 @@
     do { \
         int i0; \
         target_ulong va = EA; \
-        target_ulong va_high = EA + LEN; \
         uintptr_t ra = GETPC(); \
         int log_byte = 0; \
+        int in_region = (OFFSET) <= (LEN); \
         for (i0 = 0; i0 < ELEMENT_SIZE; i0++) { \
-            log_byte = ((va + i0) <= va_high) && QVAL; \
+            log_byte = in_region && QVAL; \
             uint8_t B; \
             B = cpu_ldub_data_ra(env, EA + i0, ra); \
             mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \
@@ -249,10 +249,10 @@
     do { \
         int i0; \
         target_ulong va = EA; \
-        target_ulong va_high = EA + LEN; \
         int log_byte = 0; \
+        int in_region = (OFFSET) <= (LEN); \
         for (i0 = 0; i0 < ELEM_SIZE; i0++) { \
-            log_byte = ((va + i0) <= va_high) && QVAL; \
+            log_byte = in_region && QVAL; \
             LOG_VTCM_BYTE(va + i0, log_byte, \
                            hexagon_mmvec_get_byte(&(IN), \
                                                     ELEM_SIZE * IDX + i0), \
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 2/3] target/hexagon: fix HVX scatter/gather region-length check

Quoted 20 lines · On Wed, 19 Aug 2026 16:09:38 -0700 Brian Cain <[email protected]>…

The region-length bounds check compared (EA + i0) <= (EA + LEN), which reduces to i0 <= LEN and never actually depends on the region base. This let vgather/vscatter keep elements whose offset was beyond the declared region instead of dropping them. Compare the offset to the region length directly instead.

---
 target/hexagon/mmvec/macros.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
target/hexagon/mmvec/macros.h +0 -0
index 0ed35720eea..7ee6f5e62fb 100644
@@ -170,11 +170,11 @@
     do { \
         int i0; \
         target_ulong va = EA; \

target/hexagon/mmvec/macros.h line 180

Quoted 10 lines
target/hexagon/mmvec/macros.h +2 -2
-        target_ulong va_high = EA + LEN; \
         uintptr_t ra = GETPC(); \
         int log_byte = 0; \
+        int in_region = (OFFSET) <= (LEN); \
         for (i0 = 0; i0 < ELEMENT_SIZE; i0++) { \
-            log_byte = ((va + i0) <= va_high) && QVAL; \
+            log_byte = in_region && QVAL; \
             uint8_t B; \
             B = cpu_ldub_data_ra(env, EA + i0, ra); \
             mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \

Should we also protect the memory load on what could be an out-of-bounds access?

            uint8_t B = 0; \
            if (log_byte) { \
                B = cpu_ldub_data_ra(env, va + i0, ra); \
                mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \
            } \
author
#7 lore raw

Re: [PATCH 2/3] target/hexagon: fix HVX scatter/gather region-length check

Quoted 33 lines · Matheus Tavares Bernardino
Quoted 19 lines · On Wed, 19 Aug 2026 16:09:38 -0700 Brian Cain <[email protected]>…

The region-length bounds check compared (EA + i0) <= (EA + LEN), which reduces to i0 <= LEN and never actually depends on the region base. This let vgather/vscatter keep elements whose offset was beyond the declared region instead of dropping them. Compare the offset to the region length directly instead.

---
  target/hexagon/mmvec/macros.h | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
target/hexagon/mmvec/macros.h +0 -0
index 0ed35720eea..7ee6f5e62fb 100644
@@ -170,11 +170,11 @@
      do { \
          int i0; \
          target_ulong va = EA; \

target/hexagon/mmvec/macros.h line 180

Quoted 10 lines
target/hexagon/mmvec/macros.h +2 -2
-        target_ulong va_high = EA + LEN; \
          uintptr_t ra = GETPC(); \
          int log_byte = 0; \
+        int in_region = (OFFSET) <= (LEN); \
          for (i0 = 0; i0 < ELEMENT_SIZE; i0++) { \
-            log_byte = ((va + i0) <= va_high) && QVAL; \
+            log_byte = in_region && QVAL; \
              uint8_t B; \
              B = cpu_ldub_data_ra(env, EA + i0, ra); \
              mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \

Should we also protect the memory load on what could be an out-of-bounds access?

Quoted 5 lines
             uint8_t B = 0; \
             if (log_byte) { \
                 B = cpu_ldub_data_ra(env, va + i0, ra); \
                 mmvec_set_byte(&env->tmp_VRegs[0], ELEMENT_SIZE * IDX + i0, B); \
             } \

I will fix this.

author
#8 lore raw

PATCH 3/3 tests/tcg/hexagon: add vgather/vscatter region-length tests

In reply to Brian Cain

Cover the case where an offset lies beyond the region length: it must be dropped rather than read/written.

---
 tests/tcg/hexagon/scatter_gather.c | 125 ++++++++++++++++++++++++++++-
 1 file changed, 124 insertions(+), 1 deletion(-)
tests/tcg/hexagon/scatter_gather.c +124 -1
index bf8b5e03172..e1c4df0e64a 100644
@@ -32,6 +32,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <inttypes.h>
+#include <hexagon_types.h>
+#include <hvx_hexagon_protos.h>
 
 typedef long HVX_Vector       __attribute__((__vector_size__(128)))
                               __attribute__((aligned(128)));
@@ -85,6 +87,23 @@ unsigned int   word_predicates[MATRIX_SIZE] __attribute__((aligned(128)));
 /* make this big enough for all the operations */
 const size_t region_len = sizeof(vtcm);
 
+/* Mu (region length - 1); offset is kept iff offset <= Mu */
+#define GATHER_LEN_TEST_MU 127
+static unsigned char gather_len_test_src[256] __attribute__((aligned(128)));
+static unsigned char gather_len_test_dst[128] __attribute__((aligned(128)));
+static unsigned char gather_len_test_dst_ref[128];
+static unsigned short gather_len_test_offsets[MATRIX_SIZE]
+    __attribute__((aligned(128)));
+
+/* offsets are unique so a dropped write can't be masked by another lane */
+#define SCATTER_LEN_TEST_MU 127
+static unsigned short scatter_len_test_dst[128] __attribute__((aligned(128)));
+static unsigned short scatter_len_test_dst_ref[128];
+static unsigned short scatter_len_test_offsets[MATRIX_SIZE]
+    __attribute__((aligned(128)));
+static unsigned short scatter_len_test_values[MATRIX_SIZE]
+    __attribute__((aligned(128)));
+
 /* optionally add sync instructions */
 #define SYNC_VECTOR 1
 
@@ -96,7 +115,7 @@ static void sync_scatter(void *addr)
      * synchronization.  Normally the dummy load would be deferred as
      * long as possible to minimize stalls.
      */
-    asm volatile("vmem(%0 + #0):scatter_release\n" : : "r"(addr));
+    asm volatile("vmem(%[addr] + #0):scatter_release\n" : : [addr] "r"(addr));
     /* use volatile to force the load */
     volatile HVX_Vector vDummy = *(HVX_Vector *)addr; vDummy = vDummy;
 #endif
@@ -855,6 +874,102 @@ void check_gather_16_32_masked(void)
                  MATRIX_SIZE * sizeof(unsigned short));
 }
 
+/* vgather must drop elements whose offset is beyond the region */
+void create_gather_region_len_test(void)
+{
+    for (int i = 0; i < 256; i++) {
+        gather_len_test_src[i] = (unsigned char)(13 + 7 * i);
+    }
+    memset(gather_len_test_offsets, 0, sizeof(gather_len_test_offsets));
+    gather_len_test_offsets[0] = 0;
+    gather_len_test_offsets[1] = GATHER_LEN_TEST_MU - 1;
+    gather_len_test_offsets[2] = GATHER_LEN_TEST_MU;
+    gather_len_test_offsets[3] = GATHER_LEN_TEST_MU + 1;
+    gather_len_test_offsets[4] = GATHER_LEN_TEST_MU + 73;
+    memset(gather_len_test_dst, FILL_CHAR, sizeof(gather_len_test_dst));
+}
+
+/* gather with a region shorter than the source buffer, using HVX */
+void vector_gather_region_len(void)
+{
+    HVX_Vector voff = *(HVX_Vector *)gather_len_test_offsets;
+
+    Q6_vgather_ARMVh((HVX_Vector *)gather_len_test_dst,
+                      (int)(uintptr_t)gather_len_test_src,
+                      GATHER_LEN_TEST_MU, voff);
+
+    sync_gather(gather_len_test_dst);
+}
+
+/* gather with a region shorter than the source buffer, using C */
+void scalar_gather_region_len(unsigned char *dst)
+{
+    for (int i = 0; i < MATRIX_SIZE; i++) {
+        unsigned short off = gather_len_test_offsets[i];
+        if (off <= GATHER_LEN_TEST_MU) {
+            dst[2 * i] = gather_len_test_src[off];
+            dst[2 * i + 1] = gather_len_test_src[off + 1];
+        }
+    }
+}
+
+void check_gather_region_len(void)
+{
+    memset(gather_len_test_dst_ref, FILL_CHAR,
+           sizeof(gather_len_test_dst_ref));
+    scalar_gather_region_len(gather_len_test_dst_ref);
+    check_buffer(__func__, gather_len_test_dst, gather_len_test_dst_ref,
+                 sizeof(gather_len_test_dst_ref));
+}
+
+/* vscatter must drop elements whose offset is beyond the region */
+void create_scatter_region_len_test(void)
+{
+    memset(scatter_len_test_offsets, 0, sizeof(scatter_len_test_offsets));
+    scatter_len_test_offsets[0] = 0;
+    scatter_len_test_offsets[1] = SCATTER_LEN_TEST_MU - 3;
+    scatter_len_test_offsets[2] = SCATTER_LEN_TEST_MU - 1;
+    scatter_len_test_offsets[3] = SCATTER_LEN_TEST_MU + 1;
+    scatter_len_test_offsets[4] = SCATTER_LEN_TEST_MU + 73;
+
+    for (int i = 0; i < MATRIX_SIZE; i++) {
+        scatter_len_test_values[i] = 0x4100 + i;
+    }
+    memset(scatter_len_test_dst, FILL_CHAR, sizeof(scatter_len_test_dst));
+}
+
+/* scatter with a region shorter than the destination buffer, using HVX */
+void vector_scatter_region_len(void)
+{
+    HVX_Vector voff = *(HVX_Vector *)scatter_len_test_offsets;
+    HVX_Vector vval = *(HVX_Vector *)scatter_len_test_values;
+
+    Q6_vscatter_RMVhV((int)(uintptr_t)scatter_len_test_dst,
+                       SCATTER_LEN_TEST_MU, voff, vval);
+
+    sync_scatter(scatter_len_test_dst);
+}
+
+/* scatter with a region shorter than the destination buffer, using C */
+void scalar_scatter_region_len(unsigned short *dst)
+{
+    for (int i = 0; i < MATRIX_SIZE; i++) {
+        unsigned short off = scatter_len_test_offsets[i];
+        if (off <= SCATTER_LEN_TEST_MU) {
+            dst[off / 2] = scatter_len_test_values[i];
+        }
+    }
+}
+
+void check_scatter_region_len(void)
+{
+    memset(scatter_len_test_dst_ref, FILL_CHAR,
+           sizeof(scatter_len_test_dst_ref));
+    scalar_scatter_region_len(scatter_len_test_dst_ref);
+    check_buffer(__func__, scatter_len_test_dst, scatter_len_test_dst_ref,
+                 sizeof(scatter_len_test_dst_ref));
+}
+
 /* print scatter16 buffer */
 void print_scatter16_buffer(void)
 {
@@ -1035,6 +1150,14 @@ int main()
     print_scatter16_32_buffer();
     check_scatter_16_32_masked();
 
+    create_gather_region_len_test();
+    vector_gather_region_len();
+    check_gather_region_len();
+
+    create_scatter_region_len_test();
+    vector_scatter_region_len();
+    check_scatter_region_len();
+
     puts(err ? "FAIL" : "PASS");
     return err;
 }
Signature · 1 line
2.34.1