Skip to contentSkip to conversation

tcg/riscv64: Fix vector constant matching for logical ops

PATCH In review Aurelien Jarno 5 participants 10 messages Oct 10 – Nov 13 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tcg/riscv64: Fix vector constant matching for logical ops

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
bic v4.8h, #0xf0
to be translated into the following op:
...
and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
...
which is then translated to the following RISC-V instruction:
vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
 tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg-target.c.inc +9 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
         return 1;
     }
     if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
     }
     /*
      * Sign extended from 12 bits: [-0x800, 0x7ff].
Signature · 1 line
2.53.0
#1 lore raw
Quoted 38 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
bic v4.8h, #0xf0
to be translated into the following op:
...
and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
...
which is then translated to the following RISC-V instruction:
vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
 tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg-target.c.inc +2 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
         return 1;
     }
     if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so

tcg/riscv64/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, int ct, · line 406

Quoted 10 lines
tcg/riscv64/tcg-target.c.inc +7 -0
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
     }
     /*
      * Sign extended from 12 bits: [-0x800, 0x7ff].

Gentle ping about this patch. Are there more things to fix?

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
#2 lore raw
In reply to Aurelien Jarno
Quoted 36 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
bic v4.8h, #0xf0
to be translated into the following op:

   ...
   and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
   ...
which is then translated to the following RISC-V instruction:
   vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
  tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg-target.c.inc +0 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
          return 1;
      }
      if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;

tcg/riscv64/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, int ct, · line 404

Quoted 10 lines
tcg/riscv64/tcg-target.c.inc +9 -0
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
      }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg-target.c.inc +6 -6
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
     case INDEX_op_and_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2);
         break;
     case INDEX_op_or_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2);
         break;
     case INDEX_op_xor_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2);
         break;
     case INDEX_op_not_vec:
         set_vtype_len(s, type);
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags)
     case INDEX_op_rotli_vec:
         return C_O1_I1(v, v);
     case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
     case INDEX_op_ssadd_vec:
     case INDEX_op_sssub_vec:
     case INDEX_op_usadd_vec:
     case INDEX_op_ussub_vec:
         return C_O1_I2(v, v, vK);
     case INDEX_op_sub_vec:
         return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:
     case INDEX_op_mul_vec:
     case INDEX_op_shlv_vec:
     case INDEX_op_shrv_vec:
     case INDEX_op_sarv_vec:
     case INDEX_op_rotlv_vec:
     case INDEX_op_rotrv_vec:
     case INDEX_op_smax_vec:
     case INDEX_op_smin_vec:
     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
         return C_O1_I2(v, v, v);

---

author Reviewed-by Tested-by
#3 lore raw

Hi Philippe,

Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 36 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
bic v4.8h, #0xf0
to be translated into the following op:

   ...
   and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
   ...
which is then translated to the following RISC-V instruction:
   vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
  tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg-target.c.inc +0 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
          return 1;
      }
      if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;

tcg/riscv64/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, int ct, · line 404

Quoted 10 lines
tcg/riscv64/tcg-target.c.inc +9 -0
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
      }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

Quoted 6 lines

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

This indeed doesn't work. The problem is that while the constant matching is being passed VECE = 0 (as it is unused for logical ops), the code generation is still being passed a 64-bit size. Therefore for the eor, it emits:

  vsetivli                zero,2,e64,m1,ta,ma
  lui                     t6,3
  addiw                   t6,t6,-448
  add                     t6,t6,s0
  vle64.v                 v1,(t6)
  vxor.vi                 v1,v1,15

It would make more sense to use 16 e8 elements like in the original ARM SVE code, but it seems that info is lost at this stage.

Quoted 49 lines

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg-target.c.inc +6 -6
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode
 opc,
     case INDEX_op_and_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2);
         break;
     case INDEX_op_or_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2);
         break;
     case INDEX_op_xor_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2);
         break;
     case INDEX_op_not_vec:
         set_vtype_len(s, type);
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType type,
 unsigned flags)
     case INDEX_op_rotli_vec:
         return C_O1_I1(v, v);
     case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
     case INDEX_op_ssadd_vec:
     case INDEX_op_sssub_vec:
     case INDEX_op_usadd_vec:
     case INDEX_op_ussub_vec:
         return C_O1_I2(v, v, vK);
     case INDEX_op_sub_vec:
         return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:
     case INDEX_op_mul_vec:
     case INDEX_op_shlv_vec:
     case INDEX_op_shrv_vec:
Quoted 8 lines
     case INDEX_op_sarv_vec:
     case INDEX_op_rotlv_vec:
     case INDEX_op_rotrv_vec:
     case INDEX_op_smax_vec:
     case INDEX_op_smin_vec:
     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
         return C_O1_I2(v, v, v);

I believe that until TCG is able to propagate VECE even for logical ops, this is the thing to do. It generates suboptimal code, but correct one. Therefore

Cheers
Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
#4 lore raw
Quoted 130 lines · Aurelien Jarno

Hi Philippe,

Quoted 53 lines · Philippe Mathieu-Daudé
Quoted 36 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction

    bic v4.8h, #0xf0
to be translated into the following op:
    ...
    and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
    ...
which is then translated to the following RISC-V instruction:
    vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
   tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
   1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg-target.c.inc +0 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
           return 1;
       }
       if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;

tcg/riscv64/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, int ct, · line 404

Quoted 10 lines
tcg/riscv64/tcg-target.c.inc +9 -0
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
       }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

Quoted 6 lines

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

This indeed doesn't work. The problem is that while the constant matching is being passed VECE = 0 (as it is unused for logical ops), the code generation is still being passed a 64-bit size. Therefore for the eor, it emits:

   vsetivli                zero,2,e64,m1,ta,ma
   lui                     t6,3
   addiw                   t6,t6,-448
   add                     t6,t6,s0
   vle64.v                 v1,(t6)
   vxor.vi                 v1,v1,15

It would make more sense to use 16 e8 elements like in the original ARM SVE code, but it seems that info is lost at this stage.

Quoted 53 lines

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg-target.c.inc +6 -6
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode
 opc,
      case INDEX_op_and_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2);
          break;
      case INDEX_op_or_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2);
          break;
      case INDEX_op_xor_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2);
          break;
      case INDEX_op_not_vec:
          set_vtype_len(s, type);
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType type,
 unsigned flags)
      case INDEX_op_rotli_vec:
          return C_O1_I1(v, v);
      case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
      case INDEX_op_ssadd_vec:
      case INDEX_op_sssub_vec:
      case INDEX_op_usadd_vec:
      case INDEX_op_ussub_vec:
          return C_O1_I2(v, v, vK);
      case INDEX_op_sub_vec:
          return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:
      case INDEX_op_mul_vec:

case INDEX_op_shlv_vec:
case INDEX_op_shrv_vec:
case INDEX_op_sarv_vec:
case INDEX_op_rotlv_vec:
case INDEX_op_rotrv_vec:
case INDEX_op_smax_vec:

Quoted 8 lines
Quoted 4 lines
      case INDEX_op_smin_vec:
      case INDEX_op_umax_vec:
      case INDEX_op_umin_vec:
          return C_O1_I2(v, v, v);

I believe that until TCG is able to propagate VECE even for logical ops, this is the thing to do. It generates suboptimal code, but correct one. Therefore

I think the right thing to do is create a function that extracts an appropriate element size for the constant, and a new constraint letter.

Something like

static int vec_logical_immediate_vece(uint64_t val)

{
    for (int e = MO_64; e >= MO_8; e--) {
        if (val == dup_const(e, val)) {
            return e;
        }
    }
    return -1;
}

...

    if ((ct & TCG_CT_CONST_LOGICAL_VI) &&
        vec_logical_immediate_vece(val) >= 0) {
        return true;
    }

...

    case INDEX_op_and_vec:
        if (c2) {
            vece = vec_logical_immediate_vece(a2);
            set_vtype_len_sew(s, type, vece);
            tcg_out_opc_vi(s, OPC_VAND_VI, a0, a1, a2);
        } else {
            set_vtype_len(s, type);
            tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2);
        }

etc. Very possibly wrapped up in another function akin to tcg_out_opc_vv_vi.

r~

#5 lore raw
In reply to Aurelien Jarno
Quoted 152 lines · Aurelien Jarno

Hi Philippe,

Quoted 65 lines · Philippe Mathieu-Daudé
Quoted 50 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that
corresponds to and_vec, or_vec and xor_vec), VECE is unused. However,
tcg_target_const_match() always treat it as an element size to extract
the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
   bic v4.8h, #0xf0
to be translated into the following op:
   ...
   and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
   ...
which is then translated to the following RISC-V instruction:
   vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the
it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector

opcodes")

---
  tcg/riscv64/tcg-target.c.inc | 11 +++++++++--

  1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc
index 14a397117a..1262b71a62 100644

tcg/riscv64/tcg-target.c.inc +0 -1
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t
 val, int ct,
           return 1;
       }
       if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest

element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after

Quoted 8 lines

+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) { +            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
      }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

Quoted 6 lines

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

This indeed doesn't work. The problem is that while the constant matching is being passed VECE = 0 (as it is unused for logical ops), the
code generation is still being passed a 64-bit size. Therefore for the
eor, it emits:
  vsetivli                zero,2,e64,m1,ta,ma
  lui                     t6,3
  addiw                   t6,t6,-448
  add                     t6,t6,s0
  vle64.v                 v1,(t6)
  vxor.vi                 v1,v1,15

It would make more sense to use 16 e8 elements like in the original ARM
SVE code, but it seems that info is lost at this stage.

Quoted 60 lines

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc

tcg/riscv64/tcg-target.c.inc +0 -0
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s,

TCGOpcode
opc,
     case INDEX_op_and_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2); break;
     case INDEX_op_or_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2); break;
     case INDEX_op_xor_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2); break;
     case INDEX_op_not_vec:
         set_vtype_len(s, type);

tcg/riscv64/tcg-target.c.inc +0 -0
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType
 type,
 unsigned flags)

     case INDEX_op_rotli_vec:
         return C_O1_I1(v, v);
     case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
     case INDEX_op_ssadd_vec:
     case INDEX_op_sssub_vec:
     case INDEX_op_usadd_vec:
     case INDEX_op_ussub_vec:
         return C_O1_I2(v, v, vK);
     case INDEX_op_sub_vec:
         return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:
     case INDEX_op_mul_vec:
     case INDEX_op_shlv_vec:
     case INDEX_op_shrv_vec:
     case INDEX_op_sarv_vec:
     case INDEX_op_rotlv_vec:
     case INDEX_op_rotrv_vec:
     case INDEX_op_smax_vec:
     case INDEX_op_smin_vec:

Quoted 8 lines
Quoted 3 lines

     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
         return C_O1_I2(v, v, v);

I believe that until TCG is able to propagate VECE even for logical ops,
this is the thing to do. It generates suboptimal code, but correct one.

Are you able to update this patch and send a v3 that include these fixes, or ideally what Richard said?

Alistair

Quoted 7 lines

Therefore

Cheers
Aurelien

#6 lore raw
Quoted 162 lines · Alistair Francis
Quoted 152 lines · Aurelien Jarno

Hi Philippe,

Quoted 65 lines · Philippe Mathieu-Daudé
Quoted 50 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that
corresponds to and_vec, or_vec and xor_vec), VECE is unused. However,
tcg_target_const_match() always treat it as an element size to extract
the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
   bic v4.8h, #0xf0
to be translated into the following op:
   ...
   and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
   ...
which is then translated to the following RISC-V instruction:
   vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the
it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector

opcodes")

---
   tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
   1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc
index 14a397117a..1262b71a62 100644

tcg/riscv64/tcg-target.c.inc +0 -1
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t
 val, int ct,
           return 1;
       }
       if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest

element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after

Quoted 8 lines

+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) { +            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
      }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

Quoted 6 lines

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

This indeed doesn't work. The problem is that while the constant matching is being passed VECE = 0 (as it is unused for logical ops), the
code generation is still being passed a 64-bit size. Therefore for the
eor, it emits:
  vsetivli                zero,2,e64,m1,ta,ma
  lui                     t6,3
  addiw                   t6,t6,-448
  add                     t6,t6,s0
  vle64.v                 v1,(t6)
  vxor.vi                 v1,v1,15

It would make more sense to use 16 e8 elements like in the original ARM
SVE code, but it seems that info is lost at this stage.

Quoted 60 lines

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc

tcg/riscv64/tcg-target.c.inc +0 -0
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s,

TCGOpcode
opc,
     case INDEX_op_and_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2);
         break;
     case INDEX_op_or_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2);
         break;
     case INDEX_op_xor_vec:
         set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2);
         break;
     case INDEX_op_not_vec:
         set_vtype_len(s, type);

tcg/riscv64/tcg-target.c.inc +3 -3
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType
 type,
 unsigned flags)
      case INDEX_op_rotli_vec:
          return C_O1_I1(v, v);
      case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
      case INDEX_op_ssadd_vec:
      case INDEX_op_sssub_vec:
      case INDEX_op_usadd_vec:
      case INDEX_op_ussub_vec:
          return C_O1_I2(v, v, vK);
      case INDEX_op_sub_vec:
          return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:

     case INDEX_op_mul_vec:
     case INDEX_op_shlv_vec:
     case INDEX_op_shrv_vec:
     case INDEX_op_sarv_vec:
     case INDEX_op_rotlv_vec:
     case INDEX_op_rotrv_vec:
     case INDEX_op_smax_vec:
     case INDEX_op_smin_vec:

Quoted 8 lines
Quoted 3 lines

     case INDEX_op_umax_vec:
     case INDEX_op_umin_vec:
         return C_O1_I2(v, v, v);

I believe that until TCG is able to propagate VECE even for logical ops,
this is the thing to do. It generates suboptimal code, but correct one.

Quoted 2 lines

Are you able to update this patch and send a v3 that include these fixes, or ideally what Richard said?

https://lore.kernel.org/qemu-devel/[email protected]/

I thought there would be some more review, but if there's no more comment I can merge it sometime this week.

r~

#7 lore raw
Quoted 163 lines · Richard Henderson
Quoted 162 lines · Alistair Francis
Quoted 160 lines · Aurelien Jarno

Hi Philippe,

Quoted 65 lines · Philippe Mathieu-Daudé
Quoted 50 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that
corresponds to and_vec, or_vec and xor_vec), VECE is unused. However,
tcg_target_const_match() always treat it as an element size to extract
the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
    bic v4.8h, #0xf0
to be translated into the following op:
    ...
    and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
    ...
which is then translated to the following RISC-V instruction:
    vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the
it can actually be represented as a duplicated constant.

Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector

opcodes")

---
   tcg/riscv64/tcg-target.c.inc | 11 +++++++++--

   1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc
index 14a397117a..1262b71a62 100644

tcg/riscv64/tcg-target.c.inc +0 -1
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t
 val, int ct,
            return 1;
        }
        if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest

element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after

Quoted 8 lines

+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) { +            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
       }

This fixes the BIC case, but I don't think it is sufficient.

I tested with qemu-aarch64 built for a riscv64 host, run under qemu-riscv64 -cpu max, guest -cpu max,sve-default-vector-length=16.

Quoted 6 lines

NEON bic v0.8h, #0xf0 // fail on master, now OK

add z1.b; and z0.d, #0xf // FAIL
add z1.d; and z0.b, #0xf // FAIL
add z1.b; orr z0.d, #-16 // FAIL
add z1.d; eor z0.b, #0xf // FAIL

This indeed doesn't work. The problem is that while the constant matching is being passed VECE = 0 (as it is unused for logical ops), the
code generation is still being passed a 64-bit size. Therefore for the
eor, it emits:
   vsetivli                zero,2,e64,m1,ta,ma
   lui                     t6,3
   addiw                   t6,t6,-448
   add                     t6,t6,s0
   vle64.v                 v1,(t6)
   vxor.vi                 v1,v1,15

It would make more sense to use 16 e8 elements like in the original ARM
SVE code, but it seems that info is lost at this stage.

Quoted 55 lines

Not accepting constants for and/or/xor_vec at all (using C_O1_I2(v, v, v) to get them loaded into registers) makes all five cases pass (I'm not sure we want to keep the .vi forms):

-- >8 --

tcg/riscv64/tcg- renamed from tcg/riscv64/tcg-target.c.inc +0 -0

target.c.inc

tcg/riscv64/tcg-target.c.inc +0 -0
@@ -2779,15 +2779,15 @@ static void tcg_out_vec_op(TCGContext *s,

TCGOpcode
opc,
      case INDEX_op_and_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VAND_VV, OPC_VAND_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VAND_VV, a0, a1, a2); break;
      case INDEX_op_or_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VOR_VV, OPC_VOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VOR_VV, a0, a1, a2); break;
      case INDEX_op_xor_vec:
          set_vtype_len(s, type);
-        tcg_out_opc_vv_vi(s, OPC_VXOR_VV, OPC_VXOR_VI, a0, a1, a2, c2);
+        tcg_out_opc_vv(s, OPC_VXOR_VV, a0, a1, a2); break;
      case INDEX_op_not_vec:
          set_vtype_len(s, type);

tcg/riscv64/tcg-target.c.inc +0 -0
@@ -2976,15 +2976,15 @@ tcg_target_op_def(TCGOpcode op, TCGType
 type,
 unsigned flags)

      case INDEX_op_rotli_vec:
          return C_O1_I1(v, v);
      case INDEX_op_add_vec:
-    case INDEX_op_and_vec:
-    case INDEX_op_or_vec:
-    case INDEX_op_xor_vec:
      case INDEX_op_ssadd_vec:
      case INDEX_op_sssub_vec:
      case INDEX_op_usadd_vec:
      case INDEX_op_ussub_vec:
          return C_O1_I2(v, v, vK);
      case INDEX_op_sub_vec:
          return C_O1_I2(v, vK, v);
+    case INDEX_op_and_vec:
+    case INDEX_op_or_vec:
+    case INDEX_op_xor_vec:
      case INDEX_op_mul_vec:
      case INDEX_op_shlv_vec:
      case INDEX_op_shrv_vec:

Quoted 8 lines

      case INDEX_op_sarv_vec:
      case INDEX_op_rotlv_vec:
      case INDEX_op_rotrv_vec:
      case INDEX_op_smax_vec:
      case INDEX_op_smin_vec:
      case INDEX_op_umax_vec:
      case INDEX_op_umin_vec:
          return C_O1_I2(v, v, v);

I believe that until TCG is able to propagate VECE even for logical ops,
this is the thing to do. It generates suboptimal code, but correct one.

Quoted 6 lines
Quoted 2 lines

Are you able to update this patch and send a v3 that include these fixes, or ideally what Richard said?

https://lore.kernel.org/qemu-devel/20261028143330.6518-1- [email protected]/

I thought there would be some more review, but if there's no more comment I can merge it sometime this week.

Indeed I wanted to respin updating commit descriptions per Aurelien suggestions but got overwhelmed with SD/MMC bugs.

#8 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Richard Henderson
Quoted 5 lines · Alistair Francis

On Tue, 2026-10-27 at 22:58 +0100, Aurelien Jarno wrote:

Are you able to update this patch and send a v3 that include these fixes, or ideally what Richard said?

Quoted 7 lines
Quoted 3 lines

https://lore.kernel.org/qemu-devel/20261028143330.6518-1- [email protected]/

I thought there would be some more review, but if there's no more comment I can merge it sometime this week.

Indeed I wanted to respin updating commit descriptions per Aurelien suggestions but got overwhelmed with SD/MMC bugs.

Will try to post that on Monday.

#9 lore raw
In reply to Aurelien Jarno
Quoted 17 lines · Aurelien Jarno

For logical vector operations (in the case of the RISC-V backend that corresponds to and_vec, or_vec and xor_vec), VECE is unused. However, tcg_target_const_match() always treat it as an element size to extract the corresponding constant element from the whole constant.

This causes the following aarch64 instruction
bic v4.8h, #0xf0
to be translated into the following op:

   ...
   and_vec v128,e8,tmp5,tmp4,v128$0xff0fff0fff0fff0f
   ...
which is then translated to the following RISC-V instruction:
   vand.vi v1,v1,15

Fix that by, when VECE is 0 aka MO_8, only extracting the element if the it can actually be represented as a duplicated constant.

Quoted 8 lines
Fixes 5a63f5998791 ("tcg/riscv: Add support for basic vector opcodes")
---
  tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Changes v1 -> v2:
- Use dup_const() instead of open coding the constant duplication.

Ping?

Can we get this one for 11.2?

Thanks,

/mjt

Quoted 22 lines
tcg/riscv64/tcg-target.c.inc +9 -2
index 14a397117a..1262b71a62 100644
@@ -392,8 +392,15 @@ static bool tcg_target_const_match(int64_t val, int ct,
          return 1;
      }
      if (type >= TCG_TYPE_V64) {
-        /* Val is replicated by VECE; extract the highest element. */
-        val >>= (-8 << vece) & 63;
+        /*
+         * For vector logical operations, VECE is unused and equals to 0, so
+         * we need to check that the constant can properly be represented after
+         * replication.
+         */
+        if ((vece != MO_8) || (val == dup_const(vece, val))) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
      }
      /*
       * Sign extended from 12 bits: [-0x800, 0x7ff].