[PATCH RFC] tcg: Support maxsz=4 in GVEC descriptors

WX Chen posted 1 patch 4 days, 21 hours ago
include/tcg/tcg-gvec-desc.h | 12 +++++++++---
tcg/tcg-op-gvec.c           | 31 ++++++++++++++++++-------------
2 files changed, 27 insertions(+), 16 deletions(-)
[PATCH RFC] tcg: Support maxsz=4 in GVEC descriptors
Posted by WX Chen 4 days, 21 hours ago
This patch adds support for oprsz = maxsz = 4 in GVEC descriptors.

The MAXSZ field currently encodes and decodes sizes as follows:

    encoded_maxsz = maxsz / 8 - 1
    maxsz = (encoded_maxsz + 1) * 8

Field value 2 would represent 24 bytes. However, maxsz values of
16 bytes or more must be multiples of 16, so this field value is
unused by valid descriptors.

In this patch, we reuse MAXSZ field value 2 to represent 4 bytes.
As for oprsz, OPRSZ field value 2 already means "use MAXSZ", allowing
oprsz to follow maxsz without changing the OPRSZ decoding logic.

All existing valid descriptor encodings are preserved. The size
and alignment requirements for generic GVEC operation expansion
remain unchanged.

Suggested-by: Chao Liu <chao.liu@processmission.com>
Signed-off-by: WX Chen <wxchen0913@gmail.com>
---
This patch adds support for oprsz = maxsz = 4 to the shared GVEC descriptor 
format. Feedback on extending the common format for this RISC-V use case 
would be appreciated.

Currently, the GVEC descriptor only supports the following `maxsz` values:
if maxsz < 16
     maxsz should be 8-byte aligned
else
     maxsz should be 16-byte aligned
     
maxsz = 8, 16, 32, 48, 64, ..., 2048

That is, maxsz = 4 is currently not supported. This will
cause some issues when VLEN = 32.
Here is an example issue. First, enable the `Zve32x` extension, which
allows a minimum VLEN of 32. Build qemu-system-riscv64 with
--enable-debug-tcg on. Then, we set:

-cpu rv64,v=false,zve32x=true,vlen=32,elen=32

Then, we run a program through TCG that contains the following
code segment:
vsetivli zero, 1, e32, m1, tu, mu   # SEW = 32, LMUL = 1, vl = 1
vredsum.vs v3, v2, v1

Rather than running the program successfully, QEMU triggers the following
assertion:
qemu-system-riscv64: ../tcg/tcg-op-gvec.c:56: check_size_align: 
Assertion `(maxsz & max_align) == 0' failed.
The call chain leading to this assertion is:
trans_vredsum_vs
     → opivv_trans
     → tcg_gen_gvec_4_ptr
     → simd_desc(oprsz=4, maxsz=4, data)
     → check_size_align
     → assertion failure
     
This is because the current GVEC descriptor does not support maxsz = 4.
In addition, a GVEC descriptor is often constructed before a
gen_helper_* call. For RVV instruction helper call, the maxsz will often
be treated as the length of physical vector register, which is 4 in our
issue. Since maxsz = 4 is not supported for descriptor construction, the
assertion is triggered. This can happen on most RVV helpers, not only
vredsum.vs. Floating-point instructions provided by Zve32f can also trigger
similar assertions.

By adding 4-byte size support to the descriptor, we can not only fix the
assertion discussed above, but also unblock many RVV helper call paths
when VLEN = 32, which they should be accessible. Most RVV helpers already 
handle variable vector register sizes. Adding 4-byte descriptor support 
enables these existing implementations to run with VLEN=32 without requiring 
changes to their computation logic.

Here is a detailed analysis about the cause of the issue: 

English version
https://zhenbaii.github.io/%E8%99%9A%E6%8B%9F%E5%8C%96/2026/11/17/riscv-zve32x-gvec-en/

This initial patch adds 4-byte descriptor support, but does not
address all issues related to VLEN=32. For example:
- alignment assumptions in mask helpers
- GDB reading the vector registers

All previously valid descriptor encodings are preserved, and
encoding and decoding behavior is unchanged for existing valid
sizes of 8 bytes and above. Runtime testing so far has been
limited to RISC-V.
---
 include/tcg/tcg-gvec-desc.h | 12 +++++++++---
 tcg/tcg-op-gvec.c           | 31 ++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/include/tcg/tcg-gvec-desc.h b/include/tcg/tcg-gvec-desc.h
index 94c7d14b2643e960d58cf66c6355c769f39c2905..5e1cd1e5c4e314240a460e0c8ac76ebf1f2117d6 100644
--- a/include/tcg/tcg-gvec-desc.h
+++ b/include/tcg/tcg-gvec-desc.h
@@ -23,8 +23,11 @@
 #include "qemu/bitops.h"
 
 /*
- * This configuration allows MAXSZ to represent 2048 bytes, and
- * OPRSZ to match MAXSZ, or represent the smaller values 8, 16, or 32.
+ * MAXSZ represents 4 bytes with field value 2, or otherwise represents
+ * (field value + 1) * 8 bytes, up to 2048 bytes.  Field value 2 would
+ * otherwise represent 24 bytes, which is not a supported maximum size.
+ *
+ * OPRSZ can match MAXSZ, or represent the smaller values 8, 16, or 32.
  *
  * Encode this with:
  *   0, 1, 3 -> 8, 16, 32
@@ -34,6 +37,7 @@
  */
 #define SIMD_MAXSZ_SHIFT   0
 #define SIMD_MAXSZ_BITS    8
+#define SIMD_MAXSZ_4_CODE  2
 
 #define SIMD_OPRSZ_SHIFT   (SIMD_MAXSZ_SHIFT + SIMD_MAXSZ_BITS)
 #define SIMD_OPRSZ_BITS    2
@@ -47,7 +51,9 @@ uint32_t simd_desc(uint32_t oprsz, uint32_t maxsz, int32_t data);
 /* Extract the max vector size from a descriptor.  */
 static inline intptr_t simd_maxsz(uint32_t desc)
 {
-    return extract32(desc, SIMD_MAXSZ_SHIFT, SIMD_MAXSZ_BITS) * 8 + 8;
+    uint32_t f = extract32(desc, SIMD_MAXSZ_SHIFT, SIMD_MAXSZ_BITS);
+
+    return f == SIMD_MAXSZ_4_CODE ? 4 : f * 8 + 8;
 }
 
 /* Extract the operation size from a descriptor.  */
diff --git a/tcg/tcg-op-gvec.c b/tcg/tcg-op-gvec.c
index bc323e250091984ea8307c6d39cd796e96da1a35..99b976ca1635eb2d93f036a2b45b331c36b2778c 100644
--- a/tcg/tcg-op-gvec.c
+++ b/tcg/tcg-op-gvec.c
@@ -97,7 +97,24 @@ uint32_t simd_desc(uint32_t oprsz, uint32_t maxsz, int32_t data)
 {
     uint32_t desc = 0;
 
-    check_size_align(oprsz, maxsz, 0);
+    if (maxsz == 4) {
+        tcg_debug_assert(oprsz == 4);
+        oprsz = 2;
+        maxsz = SIMD_MAXSZ_4_CODE;
+    } else {
+        check_size_align(oprsz, maxsz, 0);
+
+        oprsz = (oprsz / 8) - 1;
+        maxsz = (maxsz / 8) - 1;
+
+        /*
+         * OPRSZ is {8,16,32} or matches MAXSZ.  Encode the final case
+         * with '2', as that would otherwise map to 24.
+         */
+        if (oprsz == maxsz) {
+            oprsz = 2;
+        }
+    }
 
     /*
      * We want to check that 'data' will fit into SIMD_DATA_BITS.
@@ -113,18 +130,6 @@ uint32_t simd_desc(uint32_t oprsz, uint32_t maxsz, int32_t data)
     tcg_debug_assert(data == sextract32(data, 0, SIMD_DATA_BITS) ||
                      data == extract32(data, 0, SIMD_DATA_BITS));
 
-    oprsz = (oprsz / 8) - 1;
-    maxsz = (maxsz / 8) - 1;
-
-    /*
-     * We have just asserted in check_size_align that either
-     * oprsz is {8,16,32} or matches maxsz.  Encode the final
-     * case with '2', as that would otherwise map to 24.
-     */
-    if (oprsz == maxsz) {
-        oprsz = 2;
-    }
-
     desc = deposit32(desc, SIMD_OPRSZ_SHIFT, SIMD_OPRSZ_BITS, oprsz);
     desc = deposit32(desc, SIMD_MAXSZ_SHIFT, SIMD_MAXSZ_BITS, maxsz);
     desc = deposit32(desc, SIMD_DATA_SHIFT, SIMD_DATA_BITS, data);

---
base-commit: 4437f0c7ef333aa97ac5deb7b95aedf377a471a6
change-id: 20261118-bug-issue-rvv-vlen32-gvec-04839878e5f0

Best regards,
-- 
WX Chen <wxchen0913@gmail.com>


Re: [PATCH RFC] tcg: Support maxsz=4 in GVEC descriptors
Posted by Richard Henderson 4 days, 7 hours ago
On 11/17/26 23:09, WX Chen wrote:
> This patch adds support for oprsz = maxsz = 4 in GVEC descriptors.
> 
> The MAXSZ field currently encodes and decodes sizes as follows:
> 
>      encoded_maxsz = maxsz / 8 - 1
>      maxsz = (encoded_maxsz + 1) * 8
> 
> Field value 2 would represent 24 bytes. However, maxsz values of
> 16 bytes or more must be multiples of 16, so this field value is
> unused by valid descriptors.
> 
> In this patch, we reuse MAXSZ field value 2 to represent 4 bytes.
> As for oprsz, OPRSZ field value 2 already means "use MAXSZ", allowing
> oprsz to follow maxsz without changing the OPRSZ decoding logic.
> 
> All existing valid descriptor encodings are preserved. The size
> and alignment requirements for generic GVEC operation expansion
> remain unchanged.

The obvious follow-up question is: what are you going to do with it?
Is this intended only for use within riscv tcg helper functions?

At minimum you need to adjust assertions elsewhere in tcg/ which 
currently rely on the assertions that you're removing/moving.


r~