On Wed, Nov 11, 2026 at 8:39 AM Sun, Qin <[email protected]> wrote:

"i386: Implement TARGET_UNSPEC_MAY_TRAP_P for trapping SSE/AVX/x87
UNSPECs" would be more clear.

>
> i386 did not define TARGET_UNSPEC_MAY_TRAP_P, so default_unspec_may_trap_p
> was used, which braught false negative on UNSPEC_FIX_NOTRUNC (integer

brought

> result) and UNSPEC_IEEE_MIN/MAX (vector float mode).  Under -ftrapping-math
> these UNSPECs could then be hoisted above the guard that keeps them from
> executing on a NaN, raising a spurious FE_INVALID.
> Implement this hook and explicitly return 1 for some predefined labels.

The above sentence is quite vague...

Opt-in list is fragile. Every new AVX10.x or APX UNSPEC that can raise
an exception will silently reintroduce this bug class unless someone
remembers to add it. An opt-out structure would fail safe: return 1
under flag_trapping_math when any operand or the result is
FLOAT_MODE_P, and carve out the known non-trapping UNSPECs (BF16
converts, fpclass, rcp14/rsqrt14, and so on).

flag_trapping_math is on by default, so this affects plain -O2 code
using intrinsics such as _mm_min_ps, _mm_cvtps_epi32 and _mm_dp_ps,
not just exotic flag combinations. Hoisting out of loops,
if-conversion and scheduler speculation are all lost for them. That is
probably the correct trade-off for correctness, but the commit message
should say so.

You also didn't say how the patch was tested.

>
> gcc/ChangeLog:
>
>         PR target/128229
>         * config/i386/i386.cc (ix86_unspec_may_trap_p): New.
>         (TARGET_UNSPEC_MAY_TRAP_P): Define.
>
> gcc/testsuite/ChangeLog:
>
>         PR target/128229
>         * gcc.target/i386/pr128229-1.c: New test.
>         * gcc.target/i386/pr128229-2.c: Ditto.
>         * gcc.target/i386/pr128229-3.c: Ditto.
>         * gcc.target/i386/pr128229-4.c: Ditto.
>         * gcc.target/i386/pr128229-5.c: Ditto.
> ---
>  gcc/config/i386/i386.cc                    | 70 ++++++++++++++++++++++
>  gcc/testsuite/gcc.target/i386/pr128229-1.c | 34 +++++++++++
>  gcc/testsuite/gcc.target/i386/pr128229-2.c | 39 ++++++++++++
>  gcc/testsuite/gcc.target/i386/pr128229-3.c | 30 ++++++++++
>  gcc/testsuite/gcc.target/i386/pr128229-4.c | 25 ++++++++
>  gcc/testsuite/gcc.target/i386/pr128229-5.c | 25 ++++++++
>  6 files changed, 223 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr128229-1.c
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr128229-2.c
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr128229-3.c
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr128229-4.c
>  create mode 100644 gcc/testsuite/gcc.target/i386/pr128229-5.c
>
> diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
> index d1e16e6ecaf..6f0a1f441ac 100644
> --- a/gcc/config/i386/i386.cc
> +++ b/gcc/config/i386/i386.cc
> @@ -29840,6 +29840,76 @@ ix86_libm_function_max_error (unsigned cfn, 
> machine_mode mode,
>  #undef TARGET_LIBM_FUNCTION_MAX_ERROR
>  #define TARGET_LIBM_FUNCTION_MAX_ERROR ix86_libm_function_max_error
>
> +/* Implement TARGET_UNSPEC_MAY_TRAP_P.  */
> +
> +static int
> +ix86_unspec_may_trap_p (const_rtx x, unsigned flags)
> +{
> +  if (flag_trapping_math)
> +    switch (XINT (x, 1))
> +      {
> +      case UNSPEC_PCMP:
> +      case UNSPEC_GETEXP:
> +       {
> +         /* UNSPEC_PCMP is shared by vcmp{ps,pd,ph}, the integer
> +            vpcmp[u]{b,w,d,q} and vcmpbf16; UNSPEC_GETEXP byvgetexp{ps,pd,ph}

byvgetexp should be by vgetexp

> +            and vgetexpbf16.  Only the SF/DF/HF forms can raise a floating
> +            point exception; the integer and BF16 forms never update MXCSR.
> +            Use the first source operand as the discriminator.  */
> +         machine_mode mode = GET_MODE (XVECEXP (x, 0, 0));
> +         if (FLOAT_MODE_P (mode) && GET_MODE_INNER (mode) != BFmode)
> +           return 1;
> +       }
> +       break;
> +      case UNSPEC_FIX_NOTRUNC:
> +      case UNSPEC_UNSIGNED_FIX_NOTRUNC:
> +      case UNSPEC_VCVTT:
> +      case UNSPEC_VCVTTU:
> +      case UNSPEC_SFIX_SATURATION:
> +      case UNSPEC_UFIX_SATURATION:
> +      case UNSPEC_VCVTPS2IBS:
> +      case UNSPEC_VCVTPS2IUBS:
> +      case UNSPEC_VCVTTPS2IBS:
> +      case UNSPEC_VCVTTPS2IUBS:
> +      case UNSPEC_VCVTPH2IBS:
> +      case UNSPEC_VCVTPH2IUBS:
> +      case UNSPEC_VCVTTPH2IBS:
> +      case UNSPEC_VCVTTPH2IUBS:
> +      case UNSPEC_VCVTPS2PH:
> +      case UNSPEC_VCVTPH2PS:
> +      case UNSPEC_COMX:
> +      case UNSPEC_OPTCOMX:
> +      case UNSPEC_IEEE_MIN:
> +      case UNSPEC_IEEE_MAX:
> +      case UNSPEC_MINMAX:
> +      case UNSPEC_RANGE:
> +      case UNSPEC_ROUND:
> +      case UNSPEC_REDUCE:
> +      case UNSPEC_GETMANT:
> +      case UNSPEC_FIXUPIMM:
> +      case UNSPEC_SCALEF:
> +      case UNSPEC_DP:
> +      case UNSPEC_FMADDSUB:
> +      case UNSPEC_COMPLEX_FMA:
> +      case UNSPEC_COMPLEX_FCMA:
> +      case UNSPEC_COMPLEX_FMUL:
> +      case UNSPEC_COMPLEX_FCMUL:
> +      case UNSPEC_COMPLEX_FMA_PAIR:
> +      case UNSPEC_COMPLEX_FCMA_PAIR:
> +      case UNSPEC_FIST:
> +      case UNSPEC_FIST_FLOOR:
> +      case UNSPEC_FIST_CEIL:
> +       return 1;
> +      default:
> +       break;
> +      }

The list probably isn’t complete. Please audit the AVX10.2 FP8/BF8 and
bias conversions (the UNSPEC_VCVT*BF8, *HF8 and BIAS families) against
the SDM exception tables.

> +
> +  return default_unspec_may_trap_p (x, flags);
> +}
> +
> +#undef TARGET_UNSPEC_MAY_TRAP_P
> +#define TARGET_UNSPEC_MAY_TRAP_P ix86_unspec_may_trap_p
> +
>  #if TARGET_MACHO
>  static bool
>  ix86_cannot_copy_insn_p (rtx_insn *insn)
> diff --git a/gcc/testsuite/gcc.target/i386/pr128229-1.c 
> b/gcc/testsuite/gcc.target/i386/pr128229-1.c
> new file mode 100644
> index 00000000000..0168e90d1e0
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr128229-1.c
> @@ -0,0 +1,34 @@
> +/* PR target/128229 */
> +/* { dg-do run } */
> +/* { dg-options "-O2 -msse2 -mfpmath=sse -fno-math-errno -ftrapping-math" } 
> */
> +/* { dg-require-effective-target fenv_exceptions } */
> +/* { dg-require-effective-target sse2_runtime } */
> +
> +#include <fenv.h>
> +
> +/* lrint expands to cvtsd2si (UNSPEC_FIX_NOTRUNC).  It is loop invariant
> +   here, but it must not be hoisted above the guard: executing it on a NaN
> +   raises FE_INVALID.  */
> +
> +__attribute__((noipa)) long
> +foo (double x, int *p, int n)
> +{
> +  long s = 0;
> +  for (int i = 0; i < n; i++)
> +    if (p[i])
> +      s += __builtin_lrint (x);
> +  return s;
> +}
> +
> +int p[64];
> +
> +int
> +main (void)
> +{
> +  feclearexcept (FE_INVALID);
> +  if (foo (__builtin_nan (""), p, 64) != 0)
> +    __builtin_abort ();
> +  if (fetestexcept (FE_INVALID))
> +    __builtin_abort ();
> +  return 0;
> +}
> diff --git a/gcc/testsuite/gcc.target/i386/pr128229-2.c 
> b/gcc/testsuite/gcc.target/i386/pr128229-2.c
> new file mode 100644
> index 00000000000..8c5b8b63ad9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr128229-2.c
> @@ -0,0 +1,39 @@
> +/* PR target/128229 */
> +/* { dg-do run } */
> +/* { dg-options "-O2 -msse2 -mfpmath=sse -ftrapping-math" } */
> +/* { dg-require-effective-target fenv_exceptions } */
> +/* { dg-require-effective-target sse2_runtime } */
> +
> +#include <emmintrin.h>
> +#include <fenv.h>
> +
> +/* minpd (UNSPEC_IEEE_MIN) raises FE_INVALID on a NaN operand.  It is loop
> +   invariant here, but it must not be hoisted above the guard.  */
> +
> +__attribute__((noipa)) __m128d
> +foo (__m128d a, __m128d b, int *p, int n)
> +{
> +  __m128d r = _mm_setzero_pd ();
> +  for (int i = 0; i < n; i++)
> +    if (p[i])
> +      r = _mm_add_pd (r, _mm_min_pd (a, b));
> +  return r;
> +}
> +
> +int p[64];
> +
> +int
> +main (void)
> +{
> +  __m128d a = _mm_set1_pd (__builtin_nan (""));
> +  __m128d b = _mm_set1_pd (1.0);
> +  double r[2];
> +
> +  feclearexcept (FE_INVALID);
> +  _mm_storeu_pd (r, foo (a, b, p, 64));
> +  if (r[0] != 0.0 || r[1] != 0.0)
> +    __builtin_abort ();
> +  if (fetestexcept (FE_INVALID))
> +    __builtin_abort ();
> +  return 0;
> +}
> diff --git a/gcc/testsuite/gcc.target/i386/pr128229-3.c 
> b/gcc/testsuite/gcc.target/i386/pr128229-3.c
> new file mode 100644
> index 00000000000..17c176cfc98
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr128229-3.c
> @@ -0,0 +1,30 @@
> +/* PR target/128229 */
> +/* { dg-do run } */
> +/* { dg-options "-O2 -msse2 -mfpmath=sse -fno-math-errno -ftrapping-math" } 
> */
> +/* { dg-require-effective-target fenv_exceptions } */
> +/* { dg-require-effective-target sse2_runtime } */
> +
> +#include <fenv.h>
> +
> +/* cvtsd2si (UNSPEC_FIX_NOTRUNC) raises FE_INVALID on a NaN.  The
> +   conversion is guarded; if-conversion must not speculate it.  */
> +
> +__attribute__((noipa)) int
> +foo (double x, int c)
> +{
> +  int r = 0;
> +  if (c)
> +    r = __builtin_irint (x);
> +  return r;
> +}
> +
> +int
> +main (void)
> +{
> +  feclearexcept (FE_INVALID);
> +  if (foo (__builtin_nan (""), 0) != 0)
> +    __builtin_abort ();
> +  if (fetestexcept (FE_INVALID))
> +    __builtin_abort ();
> +  return 0;
> +}

pr128229-1.c to -3.c: Please confirm these fail on an unpatched
compiler. The run tests depend on RTL hoisting or ifcvt actually
happening, and on GIMPLE not doing it first.

> diff --git a/gcc/testsuite/gcc.target/i386/pr128229-4.c 
> b/gcc/testsuite/gcc.target/i386/pr128229-4.c
> new file mode 100644
> index 00000000000..fc85c04f2e0
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr128229-4.c
> @@ -0,0 +1,25 @@

Please add /* PR target/xxxx */ ...

> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mavx10.2 -ftrapping-math -fdump-rtl-loop2_invariant" } 
> */
> +/* UNSPEC_GETEXP and UNSPEC_PCMP are shared with vgetexpbf16 and vcmpbf16,
> +   which never raise floating point exceptions.  Make sure the mode check
> +   in ix86_unspec_may_trap_p still lets them be hoisted out of the loop.  */
> +
> +#include <immintrin.h>
> +
> +void
> +getexp_bf16 (__m256bh a, int *p, int *e, __m256bh *out)
> +{
> +  for (; p != e; p++, out++)
> +    if (*p)
> +      *out = _mm256_getexp_pbh (a);
> +}
> +
> +void
> +cmp_bf16 (__m256bh a, __m256bh b, int *p, int *e, __mmask16 *out)
> +{
> +  for (; p != e; p++, out++)
> +    if (*p)
> +      *out = _mm256_cmp_pbh_mask (a, b, _CMP_LT_OS);
> +}
> +
> +/* { dg-final { scan-rtl-dump-times "Decided to move invariant" 2 
> "loop2_invariant" } } */
> diff --git a/gcc/testsuite/gcc.target/i386/pr128229-5.c 
> b/gcc/testsuite/gcc.target/i386/pr128229-5.c
> new file mode 100644
> index 00000000000..1da9160107b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/pr128229-5.c
> @@ -0,0 +1,25 @@

... also here.

> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mavx10.2 -ftrapping-math -fdump-rtl-loop2_invariant" } 
> */
> +/* vgetexpps and vcmpps may raise floating point exceptions, so they must
> +   not be hoisted out of the guarded loop, unlike their BF16 forms in
> +   pr128229-4.c which share the same UNSPECs.  */
> +
> +#include <immintrin.h>
> +
> +void
> +getexp_f32 (__m256 a, int *p, int *e, __m256 *out)
> +{
> +  for (; p != e; p++, out++)
> +    if (*p)
> +      *out = _mm256_getexp_ps (a);
> +}
> +
> +void
> +cmp_f32 (__m256 a, __m256 b, int *p, int *e, __mmask8 *out)
> +{
> +  for (; p != e; p++, out++)
> +    if (*p)
> +      *out = _mm256_cmp_ps_mask (a, b, _CMP_LT_OS);
> +}
> +
> +/* { dg-final { scan-rtl-dump-not "Decided to move invariant" 
> "loop2_invariant" } } */

scan-rtl-dump-not "Decided to move invariant" is function-wide, so any
unrelated invariant (an address, a constant) makes it fail. Narrow it,
or check the specific insn. It is also only meaningful if it fails
without the patch, so please confirm that.

Uros.

> --
> 2.25.1
>

Reply via email to