i386 did not define TARGET_UNSPEC_MAY_TRAP_P, so default_unspec_may_trap_p
was used. It brought false negatives on UNSPEC_FIX_NOTRUNC (integer
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 the hook as an opt-out: under -ftrapping-math, any UNSPEC with a
non-BFmode floating-point operand or result is treated as trapping, except
those documented as never raising floating-point exceptions. New UNSPECs
are treated as trapping unless explicitly added to the list.
As -ftrapping-math is the default, conditionally executed intrinsics such
as _mm_min_ps and _mm_cvtps_epi32 are no longer hoisted, if-converted or
speculated at plain -O2. That is correct: these instructions do raise
exceptions on NaN inputs.
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.
---
Bootstrapped and regression tested on x86_64-pc-linux-gnu {,-m32}.
pr128229-1.c, pr128229-2.c and pr128229-3.c abort (FE_INVALID raised) with
an unpatched compiler and pass with the patch. pr128229-4.c and
pr128229-5.c are positive controls; they pass both unpatched and patched
by construction.
Ok for trunk?
gcc/config/i386/i386.cc | 81 ++++++++++++++++++++++
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 | 26 +++++++
gcc/testsuite/gcc.target/i386/pr128229-5.c | 47 +++++++++++++
6 files changed, 257 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..d22aa0bed42 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -29840,6 +29840,87 @@ 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)
+{
+ bool may_trap_mode_p = false;
+
+ if (flag_trapping_math)
+ {
+ subrtx_iterator::array_type array;
+ FOR_EACH_SUBRTX (iter, array, x, NONCONST)
+ {
+ machine_mode mode = GET_MODE (*iter);
+ if ((GET_MODE_CLASS (mode) == MODE_FLOAT
+ || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
+ && GET_MODE_INNER (mode) != BFmode)
+ {
+ may_trap_mode_p = true;
+ break;
+ }
+ }
+ }
+
+ if (may_trap_mode_p)
+ switch (XINT (x, 1))
+ {
+ /* Data movement and shuffles: SDM "SIMD Floating-Point Exceptions:
+ None". */
+ case UNSPEC_BLENDV:
+ case UNSPEC_INSERTPS:
+ case UNSPEC_VPERMIL:
+ case UNSPEC_VPERMIL2:
+ case UNSPEC_VPERMIL2F128:
+ case UNSPEC_VPERMT2:
+ case UNSPEC_VPERMVAR:
+ case UNSPEC_VTERNLOG:
+ case UNSPEC_CAST:
+ /* Masked, non-temporal, compress/expand and gather/scatter moves. */
+ case UNSPEC_MASKLOAD:
+ case UNSPEC_MASKMOV:
+ case UNSPEC_MOVNT:
+ case UNSPEC_MOVCC_MASK:
+ case UNSPEC_COMPRESS:
+ case UNSPEC_COMPRESS_STORE:
+ case UNSPEC_EXPAND:
+ case UNSPEC_GATHER:
+ case UNSPEC_SCATTER:
+ /* Tests and classification. */
+ case UNSPEC_MOVMSK:
+ case UNSPEC_PTEST:
+ case UNSPEC_VTESTP:
+ case UNSPEC_FPCLASS:
+ case UNSPEC_FXAM:
+ /* Approximations. */
+ case UNSPEC_RCP:
+ case UNSPEC_RSQRT:
+ case UNSPEC_RCP14:
+ case UNSPEC_RSQRT14:
+ /* Dot products and tile converts that do not consult or update
+ MXCSR. */
+ case UNSPEC_VDPBF16PS:
+ case UNSPEC_VDPPHPS:
+ case UNSPEC_TCVTROWD2PS:
+ case UNSPEC_TCVTROWPS2FP16H:
+ case UNSPEC_TCVTROWPS2FP16L:
+ /* These never raise floating-point exceptions themselves. Leave
+ the switch so that default_unspec_may_trap_p still analyzes
+ their operands. */
+ break;
+ default:
+ /* Any other UNSPEC operating on non-BF16 floating-point data is
+ assumed to update MXCSR. */
+ return 1;
+ }
+
+ 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;
+}
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..699ae58b497
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128229-4.c
@@ -0,0 +1,26 @@
+/* PR target/128229 */
+/* { 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..ccc34279d09
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr128229-5.c
@@ -0,0 +1,47 @@
+/* PR target/128229 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512vl -mavx512dq -ftrapping-math
-fdump-rtl-loop2_invariant" } */
+/* These UNSPECs operate on float vectors but never raise floating point
+ exceptions (SDM: "SIMD Floating-Point Exceptions: None"), so
+ ix86_unspec_may_trap_p must leave them alone and the loop invariant
+ pass must still be able to hoist them out of the guarded loop. */
+
+#include <immintrin.h>
+
+/* UNSPEC_VPERMIL. */
+void
+permil (__m256 a, __m256i idx, int *p, int *e, __m256 *out)
+{
+ for (; p != e; p++, out++)
+ if (*p)
+ *out = _mm256_permutevar_ps (a, idx);
+}
+
+/* UNSPEC_VPERMT2. */
+void
+permt2 (__m256 a, __m256i idx, __m256 b, int *p, int *e, __m256 *out)
+{
+ for (; p != e; p++, out++)
+ if (*p)
+ *out = _mm256_permutex2var_ps (a, idx, b);
+}
+
+/* UNSPEC_RCP14. */
+void
+rcp14 (__m256 a, int *p, int *e, __m256 *out)
+{
+ for (; p != e; p++, out++)
+ if (*p)
+ *out = _mm256_rcp14_ps (a);
+}
+
+/* UNSPEC_FPCLASS. */
+void
+fpclass (__m256 a, int *p, int *e, __mmask8 *out)
+{
+ for (; p != e; p++, out++)
+ if (*p)
+ *out = _mm256_fpclass_ps_mask (a, 0x81);
+}
+
+/* { dg-final { scan-rtl-dump-times "Decided to move invariant" 4
"loop2_invariant" } } */
--
2.25.1