All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
@ 2026-10-08 18:16 Aurelien Jarno
  2026-10-09  5:05 ` Richard Henderson
  0 siblings, 1 reply; 4+ messages in thread
From: Aurelien Jarno @ 2026-10-08 18:16 UTC (permalink / raw)
  To: Palmer Dabbelt, Alistair Francis, Daniel Henrique Barboza,
	Richard Henderson, open list:RISC-V TCG target,
	open list:All patches CC here
  Cc: 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")
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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
  2026-10-08 18:16 [PATCH] tcg/riscv64: Fix vector constant matching for logical ops Aurelien Jarno
@ 2026-10-09  5:05 ` Richard Henderson
  2026-10-09 18:02   ` Aurelien Jarno
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Henderson @ 2026-10-09  5:05 UTC (permalink / raw)
  To: Aurelien Jarno, Palmer Dabbelt, Alistair Francis,
	Daniel Henrique Barboza, open list:RISC-V TCG target,
	open list:All patches CC here

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~


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
  2026-10-09  5:05 ` Richard Henderson
@ 2026-10-09 18:02   ` Aurelien Jarno
  2026-10-09 21:51     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 4+ messages in thread
From: Aurelien Jarno @ 2026-10-09 18:02 UTC (permalink / raw)
  To: Richard Henderson
  Cc: Palmer Dabbelt, Alistair Francis, Daniel Henrique Barboza,
	open list:RISC-V TCG target, open list:All patches CC here

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tcg/riscv64: Fix vector constant matching for logical ops
  2026-10-09 18:02   ` Aurelien Jarno
@ 2026-10-09 21:51     ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2026-10-09 21:51 UTC (permalink / raw)
  To: Aurelien Jarno, Richard Henderson
  Cc: Palmer Dabbelt, Alistair Francis, Daniel Henrique Barboza,
	open list:RISC-V TCG target, open list:All patches CC here

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 ;)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-09 21:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-08 18:16 [PATCH] tcg/riscv64: Fix vector constant matching for logical ops Aurelien Jarno
2026-10-09  5:05 ` Richard Henderson
2026-10-09 18:02   ` Aurelien Jarno
2026-10-09 21:51     ` Philippe Mathieu-Daudé

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.