[PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling

Philippe Mathieu-Daudé posted 6 patches 3 weeks, 4 days ago
Failed in applying to current master (apply log)
tcg/riscv64/tcg-target-con-set.h |   1 +
tcg/riscv64/tcg-target-con-str.h |   1 +
tests/tcg/aarch64/sve-vec-imm.c  | 233 +++++++++++++++++++++++++++++++
tcg/riscv64/tcg-target.c.inc     | 116 ++++++++++++---
tests/tcg/aarch64/meson.build    |   1 +
5 files changed, 330 insertions(+), 22 deletions(-)
create mode 100644 tests/tcg/aarch64/sve-vec-imm.c
[PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling
Posted by Philippe Mathieu-Daudé 3 weeks, 4 days ago
The riscv64 backend mishandles constant operands of RVV .vi forms:

- logical ops apply the immediate at a stale SEW (wrong results,
  since v9.2);
- arithmetic, compare and select ops encode the whole replicated
  64-bit constant instead of one element;
- shifts take uimm5, but the encoder checks for simm5.

1-2: add a uimm5 encoder for shifts,
3-4: extract one element (vec_imm()) for arithmetic and compare/select,
5: follow rth suggestion of adding a new constraint to pick the smallest
   element size at which the constant replicates as a simm5, and the
   emitter switches SEW to it,
6: AI-generated regression test.

Tested with qemu-aarch64 built for a riscv64 host (debug-TCG), run
under qemu-riscv64 -cpu max: the new test fails or aborts before the
series, and passes with it at every SVE vector length.

[1] https://lore.kernel.org/qemu-devel/20261010174742.3552498-1-aurelien@aurel32.net/
Supersedes: <20261010174742.3552498-1-aurelien@aurel32.net>

Philippe Mathieu-Daudé (6):
  tcg/riscv64: Factor encode_vi_raw() out of encode_vi()
  tcg/riscv64: Use a uimm5 encoder for vector shift immediates
  tcg/riscv64: Extract elements for vector arithmetic immediates
  tcg/riscv64: Fix constant handling in vector comparisons and selects
  tcg/riscv64: Fix logical vector immediate matching and emission
  tests/tcg/aarch64: Add vector immediate regression tests

 tcg/riscv64/tcg-target-con-set.h |   1 +
 tcg/riscv64/tcg-target-con-str.h |   1 +
 tests/tcg/aarch64/sve-vec-imm.c  | 233 +++++++++++++++++++++++++++++++
 tcg/riscv64/tcg-target.c.inc     | 116 ++++++++++++---
 tests/tcg/aarch64/meson.build    |   1 +
 5 files changed, 330 insertions(+), 22 deletions(-)
 create mode 100644 tests/tcg/aarch64/sve-vec-imm.c

-- 
2.55.0


Re: [PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling
Posted by Michael Tokarev 4 days, 13 hours ago
On 10/28/26 17:33, Philippe Mathieu-Daudé wrote:
> The riscv64 backend mishandles constant operands of RVV .vi forms:
> 
> - logical ops apply the immediate at a stale SEW (wrong results,
>    since v9.2);
> - arithmetic, compare and select ops encode the whole replicated
>    64-bit constant instead of one element;
> - shifts take uimm5, but the encoder checks for simm5.
> 
> 1-2: add a uimm5 encoder for shifts,
> 3-4: extract one element (vec_imm()) for arithmetic and compare/select,
> 5: follow rth suggestion of adding a new constraint to pick the smallest
>     element size at which the constant replicates as a simm5, and the
>     emitter switches SEW to it,
> 6: AI-generated regression test.
> 
> Tested with qemu-aarch64 built for a riscv64 host (debug-TCG), run
> under qemu-riscv64 -cpu max: the new test fails or aborts before the
> series, and passes with it at every SVE vector length.
> 
> [1] https://lore.kernel.org/qemu-devel/20261010174742.3552498-1-aurelien@aurel32.net/
> Supersedes: <20261010174742.3552498-1-aurelien@aurel32.net>
> 
> Philippe Mathieu-Daudé (6):
>    tcg/riscv64: Factor encode_vi_raw() out of encode_vi()
>    tcg/riscv64: Use a uimm5 encoder for vector shift immediates
>    tcg/riscv64: Extract elements for vector arithmetic immediates
>    tcg/riscv64: Fix constant handling in vector comparisons and selects
>    tcg/riscv64: Fix logical vector immediate matching and emission
>    tests/tcg/aarch64: Add vector immediate regression tests

Hi!

 From this patchset, 5/6 "Fix logical vector immediate matching and emission" is
marked for-stable.  However, it uses a function (vec_imm) which is introduced in
3/6  "Fix logical vector immediate matching and emission".  Obviously, just 5/6
alone wont work.

However, it feels like whole patchset should be picked up for the stable series,
not just a single patch - because the whole thing fixes an assortment of problems
in this area.

Alternatively I can pick just this function from 3/6, which does not feel right.

What do you think?

Thanks,

/mjt

>   tcg/riscv64/tcg-target-con-set.h |   1 +
>   tcg/riscv64/tcg-target-con-str.h |   1 +
>   tests/tcg/aarch64/sve-vec-imm.c  | 233 +++++++++++++++++++++++++++++++
>   tcg/riscv64/tcg-target.c.inc     | 116 ++++++++++++---
>   tests/tcg/aarch64/meson.build    |   1 +
>   5 files changed, 330 insertions(+), 22 deletions(-)
>   create mode 100644 tests/tcg/aarch64/sve-vec-imm.c
> 


Re: [PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling
Posted by Michael Tokarev 4 days, 13 hours ago
On 11/18/26 17:32, Michael Tokarev wrote:
> On 10/28/26 17:33, Philippe Mathieu-Daudé wrote:
>> The riscv64 backend mishandles constant operands of RVV .vi forms:
>>
>> - logical ops apply the immediate at a stale SEW (wrong results,
>>    since v9.2);
>> - arithmetic, compare and select ops encode the whole replicated
>>    64-bit constant instead of one element;
>> - shifts take uimm5, but the encoder checks for simm5.
>>
>> 1-2: add a uimm5 encoder for shifts,
>> 3-4: extract one element (vec_imm()) for arithmetic and compare/select,
>> 5: follow rth suggestion of adding a new constraint to pick the smallest
>>     element size at which the constant replicates as a simm5, and the
>>     emitter switches SEW to it,
>> 6: AI-generated regression test.
>>
>> Tested with qemu-aarch64 built for a riscv64 host (debug-TCG), run
>> under qemu-riscv64 -cpu max: the new test fails or aborts before the
>> series, and passes with it at every SVE vector length.
>>
>> [1] https://lore.kernel.org/qemu-devel/20261010174742.3552498-1-aurelien@aurel32.net/
>> Supersedes: <20261010174742.3552498-1-aurelien@aurel32.net>
>>
>> Philippe Mathieu-Daudé (6):
>>    tcg/riscv64: Factor encode_vi_raw() out of encode_vi()
>>    tcg/riscv64: Use a uimm5 encoder for vector shift immediates
>>    tcg/riscv64: Extract elements for vector arithmetic immediates
>>    tcg/riscv64: Fix constant handling in vector comparisons and selects
>>    tcg/riscv64: Fix logical vector immediate matching and emission
>>    tests/tcg/aarch64: Add vector immediate regression tests
> 
> Hi!
> 
>  From this patchset, 5/6 "Fix logical vector immediate matching and emission" is
> marked for-stable.  However, it uses a function (vec_imm) which is introduced in
> 3/6  "Fix logical vector immediate matching and emission".  Obviously, just 5/6
> alone wont work.
> 
> However, it feels like whole patchset should be picked up for the stable series,
> not just a single patch - because the whole thing fixes an assortment of problems
> in this area.

Aha, so the other patches fixes ussues introduced after the last qemu releases.
So..

> Alternatively I can pick just this function from 3/6, which does not feel right.

This seems to be the right way to go.

Thanks,

/mjt

Re: [PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling
Posted by Philippe Mathieu-Daudé 4 days, 12 hours ago
On 18/11/26 15:34, Michael Tokarev wrote:
> On 11/18/26 17:32, Michael Tokarev wrote:
>> On 10/28/26 17:33, Philippe Mathieu-Daudé wrote:
>>> The riscv64 backend mishandles constant operands of RVV .vi forms:
>>>
>>> - logical ops apply the immediate at a stale SEW (wrong results,
>>>    since v9.2);
>>> - arithmetic, compare and select ops encode the whole replicated
>>>    64-bit constant instead of one element;
>>> - shifts take uimm5, but the encoder checks for simm5.
>>>
>>> 1-2: add a uimm5 encoder for shifts,
>>> 3-4: extract one element (vec_imm()) for arithmetic and compare/select,
>>> 5: follow rth suggestion of adding a new constraint to pick the smallest
>>>     element size at which the constant replicates as a simm5, and the
>>>     emitter switches SEW to it,
>>> 6: AI-generated regression test.
>>>
>>> Tested with qemu-aarch64 built for a riscv64 host (debug-TCG), run
>>> under qemu-riscv64 -cpu max: the new test fails or aborts before the
>>> series, and passes with it at every SVE vector length.
>>>
>>> [1] https://lore.kernel.org/qemu-devel/20261010174742.3552498-1- 
>>> aurelien@aurel32.net/
>>> Supersedes: <20261010174742.3552498-1-aurelien@aurel32.net>
>>>
>>> Philippe Mathieu-Daudé (6):
>>>    tcg/riscv64: Factor encode_vi_raw() out of encode_vi()
>>>    tcg/riscv64: Use a uimm5 encoder for vector shift immediates
>>>    tcg/riscv64: Extract elements for vector arithmetic immediates
>>>    tcg/riscv64: Fix constant handling in vector comparisons and selects
>>>    tcg/riscv64: Fix logical vector immediate matching and emission
>>>    tests/tcg/aarch64: Add vector immediate regression tests
>>
>> Hi!
>>
>>  From this patchset, 5/6 "Fix logical vector immediate matching and 
>> emission" is
>> marked for-stable.  However, it uses a function (vec_imm) which is 
>> introduced in
>> 3/6  "Fix logical vector immediate matching and emission".  Obviously, 
>> just 5/6
>> alone wont work.
>>
>> However, it feels like whole patchset should be picked up for the 
>> stable series,
>> not just a single patch - because the whole thing fixes an assortment 
>> of problems
>> in this area.
> 
> Aha, so the other patches fixes ussues introduced after the last qemu 
> releases.
> So..
> 
>> Alternatively I can pick just this function from 3/6, which does not 
>> feel right.
> 
> This seems to be the right way to go.

If this isn't too much burden, sure. Otherwise I'll defer to Aurelien.


Re: [PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling
Posted by Michael Tokarev 4 days, 12 hours ago
On 11/18/26 18:32, Philippe Mathieu-Daudé wrote:
> On 18/11/26 15:34, Michael Tokarev wrote:

>> Aha, so the other patches fixes ussues introduced after the last qemu releases.
>> So..
>>
>>> Alternatively I can pick just this function from 3/6, which does not feel right.
>>
>> This seems to be the right way to go.
> 
> If this isn't too much burden, sure. Otherwise I'll defer to Aurelien.

https://gitlab.com/mjt0k/qemu/-/commit/9ee013bd83c0c817c2eedc9efc5e510095970775
fwiw (it also contains another change for TCG_CT_CONST_LOGICAL_VI definition for 10.0.x).

Thanks,

/mjt