On Sun, Jul 6, 2025 at 2:23 PM Andrew Pinski <quic_apin...@quicinc.com> wrote:
>
> These builtins requires a constant integer for the third argument but 
> currently
> there is assert rather than error. This fixes that and updates the 
> documentation too.
> Uses the same terms as was being used for the __builtin_prefetch arguments.
>
> Bootstrapped and tested on x86_64-linux-gnu.

Pushed to GCC 15 branch too.

>
>         PR middle-end/120709
>
> gcc/ChangeLog:
>
>         * builtins.cc (expand_builtin_crc_table_based): Error out
>         instead of asserting the 3rd argument is an integer constant.
>         * internal-fn.cc (expand_crc_optab_fn): Likewise.
>         * doc/extend.texi (crc): Document requirement of the poly argument
>         being a constant.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/crc-non-cst-poly-1.c: New test.
>
> Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
> ---
>  gcc/builtins.cc                           | 12 +++++++++---
>  gcc/doc/extend.texi                       |  4 ++--
>  gcc/internal-fn.cc                        | 11 ++++++++---
>  gcc/testsuite/gcc.dg/crc-non-cst-poly-1.c | 11 +++++++++++
>  4 files changed, 30 insertions(+), 8 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/crc-non-cst-poly-1.c
>
> diff --git a/gcc/builtins.cc b/gcc/builtins.cc
> index a2ce3726810..7f580a3145f 100644
> --- a/gcc/builtins.cc
> +++ b/gcc/builtins.cc
> @@ -7799,11 +7799,17 @@ expand_builtin_crc_table_based (internal_fn fn, 
> scalar_mode crc_mode,
>
>    rtx op1 = expand_normal (rhs1);
>    rtx op2 = expand_normal (rhs2);
> -  gcc_assert (TREE_CODE (rhs3) == INTEGER_CST);
> -  rtx op3 = gen_int_mode (TREE_INT_CST_LOW (rhs3), crc_mode);
> +  rtx op3;
> +  if (TREE_CODE (rhs3) != INTEGER_CST)
> +    {
> +      error ("third argument to %<crc%> builtins must be a constant");
> +      op3 = const0_rtx;
> +    }
> +  else
> +    op3 = convert_to_mode (crc_mode, expand_normal (rhs3), 0);
>
>    if (CONST_INT_P (op2))
> -    op2 = gen_int_mode (INTVAL (op2), crc_mode);
> +    op2 = convert_to_mode (crc_mode, op2, 0);
>
>    if (fn == IFN_CRC)
>      expand_crc_table_based (target, op1, op2, op3, data_mode);
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 70adf2dab0a..a119ad31ea2 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -15553,7 +15553,7 @@ are 128-bit.  Only supported on targets when 128-bit 
> types are supported.
>  Returns the calculated 8-bit bit-reversed CRC using the initial CRC (8-bit),
>  data (8-bit) and the polynomial (8-bit).
>  @var{crc} is the initial CRC, @var{data} is the data and
> -@var{poly} is the polynomial without leading 1.
> +@var{poly} is the polynomial without leading 1. @var{poly} is required to be 
> a compile-time constant.
>  Table-based or clmul-based CRC may be used for the
>  calculation, depending on the target architecture.
>  @enddefbuiltin
> @@ -15608,7 +15608,7 @@ is 32-bit.
>  Returns the calculated 8-bit bit-forward CRC using the initial CRC (8-bit),
>  data (8-bit) and the polynomial (8-bit).
>  @var{crc} is the initial CRC, @var{data} is the data and
> -@var{poly} is the polynomial without leading 1.
> +@var{poly} is the polynomial without leading 1. @var{poly} is required to be 
> a compile-time constant.
>  Table-based or clmul-based CRC may be used for the
>  calculation, depending on the target architecture.
>  @enddefbuiltin
> diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
> index 3f4ac937367..39048d77d23 100644
> --- a/gcc/internal-fn.cc
> +++ b/gcc/internal-fn.cc
> @@ -4031,9 +4031,14 @@ expand_crc_optab_fn (internal_fn fn, gcall *stmt, 
> convert_optab optab)
>    rtx dest = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
>    rtx crc = expand_normal (rhs1);
>    rtx data = expand_normal (rhs2);
> -  gcc_assert (TREE_CODE (rhs3) == INTEGER_CST);
> -  rtx polynomial = gen_rtx_CONST_INT (TYPE_MODE (result_type),
> -                                     TREE_INT_CST_LOW (rhs3));
> +  rtx polynomial;
> +  if (TREE_CODE (rhs3) != INTEGER_CST)
> +    {
> +      error ("third argument to %<crc%> builtins must be a constant");
> +      polynomial = const0_rtx;
> +    }
> +  else
> +    polynomial = convert_to_mode (TYPE_MODE (result_type), expand_normal 
> (rhs3), 0);
>
>    /* Use target specific expansion if it exists.
>       Otherwise, generate table-based CRC.  */
> diff --git a/gcc/testsuite/gcc.dg/crc-non-cst-poly-1.c 
> b/gcc/testsuite/gcc.dg/crc-non-cst-poly-1.c
> new file mode 100644
> index 00000000000..0c3d9054017
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/crc-non-cst-poly-1.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +/* PR middle-end/120709 */
> +/* Make sure we don't ICE on a non-constant poly argument. */
> +
> +
> +typedef unsigned char uint8_t;
> +uint8_t crc8_data8(uint8_t crc, uint8_t data, uint8_t polynomial) {
> +  return __builtin_rev_crc32_data8 (crc, data, polynomial); /* { dg-error 
> "must be a constant" } */
> +}
> --
> 2.43.0
>

Reply via email to