All of lore.kernel.org
 help / color / mirror / Atom feed
From: Aurelien Jarno <aurelien@aurel32.net>
To: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>
Cc: qemu-devel@nongnu.org, qemu-riscv@nongnu.org,
	Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>,
	Richard Henderson <richard.henderson@linaro.org>,
	Alistair Francis <Alistair.Francis@wdc.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	qemu-arm@nongnu.org
Subject: Re: [PATCH v3 3/6] tcg/riscv64: Extract elements for vector arithmetic immediates
Date: Thu, 29 Oct 2026 19:18:56 +0100	[thread overview]
Message-ID: <auOOEFFecNzz9mCG@aurel32.net> (raw)
In-Reply-To: <20261028143330.6518-4-philmd@oss.qualcomm.com>

On 2026-10-28 15:33, Philippe Mathieu-Daudé wrote:
> The .vi emitters for add, reverse sub and saturating ops pass the
> whole replicated constant (e.g. 0x0505050505050505) to encode_vi()
> instead of a single element. This aborts debug builds on common
> vectorized guest code and is undefined behaviour in release builds.
> 
> Add vec_imm() to extract the VECE element, and use it.
> 
> Fixes: 006352d69f14 ("tcg/riscv64: Assert immediate in range ...")
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
>  tcg/riscv64/tcg-target.c.inc | 23 +++++++++++++++++------
>  1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/tcg/riscv64/tcg-target.c.inc b/tcg/riscv64/tcg-target.c.inc
> index 0e749ae296b8..a3d995e290b7 100644
> --- a/tcg/riscv64/tcg-target.c.inc
> +++ b/tcg/riscv64/tcg-target.c.inc
> @@ -735,6 +735,12 @@ static void tcg_out_opc_vi_u(TCGContext *s, RISCVInsn opc,
>      tcg_out32(s, encode_vi_u(opc, vd, imm, vs2, true));
>  }
>  
> +/* Constants are replicated by VECE; extract one element for .vi forms. */
> +static int64_t vec_imm(TCGArg arg, unsigned vece)
> +{
> +    return sextract64(arg, 0, 8 << vece);
> +}
> +
>  static void tcg_out_opc_vv_vi(TCGContext *s, RISCVInsn o_vv, RISCVInsn o_vi,
>                                TCGReg vd, TCGReg vs2, TCGArg vi1, int c_vi1)
>  {
> @@ -2788,12 +2794,13 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
>          break;
>      case INDEX_op_add_vec:
>          set_vtype_len_sew(s, type, vece);
> -        tcg_out_opc_vv_vi(s, OPC_VADD_VV, OPC_VADD_VI, a0, a1, a2, c2);
> +        tcg_out_opc_vv_vi(s, OPC_VADD_VV, OPC_VADD_VI, a0, a1,
> +                          c2 ? vec_imm(a2, vece) : a2, c2);
>          break;
>      case INDEX_op_sub_vec:
>          set_vtype_len_sew(s, type, vece);
>          if (const_args[1]) {
> -            tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a2, a1);
> +            tcg_out_opc_vi(s, OPC_VRSUB_VI, a0, a2, vec_imm(a1, vece));
>          } else {
>              tcg_out_opc_vv(s, OPC_VSUB_VV, a0, a1, a2);
>          }
> @@ -2824,19 +2831,23 @@ static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
>          break;
>      case INDEX_op_ssadd_vec:
>          set_vtype_len_sew(s, type, vece);
> -        tcg_out_opc_vv_vi(s, OPC_VSADD_VV, OPC_VSADD_VI, a0, a1, a2, c2);
> +        tcg_out_opc_vv_vi(s, OPC_VSADD_VV, OPC_VSADD_VI, a0, a1,
> +                          c2 ? vec_imm(a2, vece) : a2, c2);
>          break;
>      case INDEX_op_sssub_vec:
>          set_vtype_len_sew(s, type, vece);
> -        tcg_out_opc_vv_vi(s, OPC_VSSUB_VV, OPC_VSSUB_VI, a0, a1, a2, c2);
> +        tcg_out_opc_vv_vi(s, OPC_VSSUB_VV, OPC_VSSUB_VI, a0, a1,
> +                          c2 ? vec_imm(a2, vece) : a2, c2);
>          break;
>      case INDEX_op_usadd_vec:
>          set_vtype_len_sew(s, type, vece);
> -        tcg_out_opc_vv_vi(s, OPC_VSADDU_VV, OPC_VSADDU_VI, a0, a1, a2, c2);
> +        tcg_out_opc_vv_vi(s, OPC_VSADDU_VV, OPC_VSADDU_VI, a0, a1,
> +                          c2 ? vec_imm(a2, vece) : a2, c2);
>          break;
>      case INDEX_op_ussub_vec:
>          set_vtype_len_sew(s, type, vece);
> -        tcg_out_opc_vv_vi(s, OPC_VSSUBU_VV, OPC_VSSUBU_VI, a0, a1, a2, c2);
> +        tcg_out_opc_vv_vi(s, OPC_VSSUBU_VV, OPC_VSSUBU_VI, a0, a1,
> +                          c2 ? vec_imm(a2, vece) : a2, c2);
>          break;
>      case INDEX_op_smax_vec:
>          set_vtype_len_sew(s, type, vece);

This is technically correct. That said, the goal of all this extra code 
is to satisfy the assert added in commit 006352d69f14 ("tcg/riscv64: 
Assert immediate in range for {i,s,u,vi}-type insns") not to fix the 
code generation which is correct as it only keeps the lower 5 bits of 
the immediate.

At this stage we already know that the immediate can be represented as a 
duplicated simm5 constant, so just dropping the assert could be a 
simpler way to fix the issue.

-- 
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
aurelien@aurel32.net                     http://aurel32.net


  parent reply	other threads:[~2026-10-29 18:19 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 14:33 [PATCH v3 0/6] tcg/riscv64: Fix vector immediate handling Philippe Mathieu-Daudé
2026-10-28 14:33 ` [PATCH v3 1/6] tcg/riscv64: Factor encode_vi_raw() out of encode_vi() Philippe Mathieu-Daudé
2026-10-29 12:16   ` Richard Henderson
2026-10-29 18:18   ` Aurelien Jarno
2026-10-28 14:33 ` [PATCH v3 2/6] tcg/riscv64: Use a uimm5 encoder for vector shift immediates Philippe Mathieu-Daudé
2026-10-29 12:16   ` Richard Henderson
2026-10-29 18:18   ` Aurelien Jarno
2026-10-28 14:33 ` [PATCH v3 3/6] tcg/riscv64: Extract elements for vector arithmetic immediates Philippe Mathieu-Daudé
2026-10-29 12:17   ` Richard Henderson
2026-10-29 18:18   ` Aurelien Jarno [this message]
2026-10-28 14:33 ` [PATCH v3 4/6] tcg/riscv64: Fix constant handling in vector comparisons and selects Philippe Mathieu-Daudé
2026-10-29 12:17   ` Richard Henderson
2026-10-29 18:31   ` Aurelien Jarno
2026-10-28 14:33 ` [PATCH v3 5/6] tcg/riscv64: Fix logical vector immediate matching and emission Philippe Mathieu-Daudé
2026-10-29 12:20   ` Richard Henderson
2026-10-29 14:12     ` Richard Henderson
2026-10-29 18:33   ` Aurelien Jarno
2026-10-28 14:33 ` [PATCH v3 6/6] tests/tcg/aarch64: Add vector immediate regression tests Philippe Mathieu-Daudé
2026-10-29 12:22   ` Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auOOEFFecNzz9mCG@aurel32.net \
    --to=aurelien@aurel32.net \
    --cc=Alistair.Francis@wdc.com \
    --cc=daniel.barboza@oss.qualcomm.com \
    --cc=palmer@dabbelt.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@oss.qualcomm.com \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.