[PATCH] tcg/riscv64: Fix vector constant matching for logical ops

Aurelien Jarno posted 1 patch 1 month, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261008181647.3015949-1-aurelien@aurel32.net
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <Alistair.Francis@wdc.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Richard Henderson <richard.henderson@linaro.org>
There is a newer version of this series
tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
[PATCH] tcg/riscv64: Fix vector constant matching for logical ops
Posted by Aurelien Jarno 1 month, 2 weeks ago
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")
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
---
 tcg/riscv64/tcg-target.c.inc | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tcg/riscv64/tcg-target.c.inc b/tcg/riscv64/tcg-target.c.inc
index 14a397117a..382c4b6a28 100644
--- a/tcg/riscv64/tcg-target.c.inc
+++ b/tcg/riscv64/tcg-target.c.inc
@@ -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 == (uint8_t)val * 0x0101010101010101ull)) {
+            /* Val is replicated by VECE; extract the highest element. */
+            val >>= (-8 << vece) & 63;
+        }
     }
     /*
      * Sign extended from 12 bits: [-0x800, 0x7ff].
-- 
2.53.0
Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
Posted by Richard Henderson 1 month, 2 weeks ago
On 10/8/26 20:16, Aurelien Jarno wrote:
> +        if ((vece != MO_8) || (val == (uint8_t)val * 0x0101010101010101ull)) {

Do you really want

     val = dup_const(vece, val)

?

r~
Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
Posted by Aurelien Jarno 1 month, 2 weeks ago
Hi,

On 2026-10-09 07:05, Richard Henderson wrote:
> On 10/8/26 20:16, Aurelien Jarno wrote:
> > +        if ((vece != MO_8) || (val == (uint8_t)val * 0x0101010101010101ull)) {
> 
> Do you really want
> 
>     val = dup_const(vece, val)
> 
> ?

No, I do not want that, as vece is not defined (in practice is 0 aka 
MO_8) for logical ops.

What I want is really extracting the element only if:

- vece != MO_8
 
=> This means VECE is defined (not 0), so it is possible to blindly 
extract a single element.

OR

- val == dup_const(vece, val)

=> When VECE is possibly undefined, this checks that the constant can be 
properly represented as an element replicated using VECE.

Or maybe you just mean to replace the check by

+        if ((vece != MO_8) || (val == dup_const(vece, val))) {

?

Regards
Aurelien

-- 
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
aurelien@aurel32.net                     http://aurel32.net
Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
Posted by Richard Henderson 1 month, 1 week ago
On 10/9/26 20:02, Aurelien Jarno wrote:
> Or maybe you just mean to replace the check by
> 
> +        if ((vece != MO_8) || (val == dup_const(vece, val))) {
> 
> ?
Yep.


r~
Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
Posted by Philippe Mathieu-Daudé 1 month, 2 weeks ago
On 9/10/26 20:02, Aurelien Jarno wrote:
> Hi,
> 
> On 2026-10-09 07:05, Richard Henderson wrote:
>> On 10/8/26 20:16, Aurelien Jarno wrote:
>>> +        if ((vece != MO_8) || (val == (uint8_t)val * 0x0101010101010101ull)) {
>>
>> Do you really want
>>
>>      val = dup_const(vece, val)


> Or maybe you just mean to replace the check by
> 
> +        if ((vece != MO_8) || (val == dup_const(vece, val))) {
Likely ;)