https://gcc.gnu.org/g:20aa69b90a61c9d5546ea7c0de81feed138b6e94

commit r17-5889-g20aa69b90a61c9d5546ea7c0de81feed138b6e94
Author: Karl Meakin <[email protected]>
Date:   Wed Sep 9 15:16:54 2026 +0000

    aarch64: Prevent FP intrinsics from being DCE'd if -ftrapping-math is 
enabled.
    
    Previously, a function like
    ```c
    void foo(float32x2_t a, float32x2_t b) {
      vadd_f32(a, b);
    }
    ```
    
    would be optimised to a no-op, since the result of `vadd_f32` is unused.
    This is undesirable if `-ftrapping-math` is enabled, because the `FADD`
    instruction could produce a floating-point exception if the result is a
    signalling-nan.
    
    Fix by inserting an `asm volatile` statement (which creates a dependency
    on the result) during expansion if `-ftrapping-math` is enabled.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64-neon-builtins-base.cc
            (build_optimization_barrier): New function.
            (gimple_arith::fold): Insert an optimization barrier when
            necessary.
            (gimple_function_base::maybe_optimization_barrier): New
            function.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/neon/no_trapping_math.c: New test.
            * gcc.target/aarch64/neon/discarded_result.c: Update expected
            assembly for `foo`.
            * gcc.target/aarch64/neon/trapping_math.c: New test.
            * gcc.target/aarch64/simd/pr99195_1.c: Add `-fno-trapping-math`
            to `dg-options` to prevent unnecessary `fmov`s being generated.
            * gcc.target/aarch64/simd/pr99195_2.c: Likewise.

Diff:
---
 gcc/config/aarch64/aarch64-neon-builtins-base.cc   | 35 +++++++++-
 .../gcc.target/aarch64/neon/discarded_result.c     |  1 +
 .../gcc.target/aarch64/neon/no_trapping_math.c     | 68 ++++++++++++++++++++
 .../gcc.target/aarch64/neon/trapping_math.c        | 74 ++++++++++++++++++++++
 gcc/testsuite/gcc.target/aarch64/simd/pr99195_1.c  |  2 +-
 gcc/testsuite/gcc.target/aarch64/simd/pr99195_2.c  |  2 +-
 6 files changed, 179 insertions(+), 3 deletions(-)

diff --git a/gcc/config/aarch64/aarch64-neon-builtins-base.cc 
b/gcc/config/aarch64/aarch64-neon-builtins-base.cc
index 0bdef25c8668..ba5aa68748fd 100644
--- a/gcc/config/aarch64/aarch64-neon-builtins-base.cc
+++ b/gcc/config/aarch64/aarch64-neon-builtins-base.cc
@@ -45,6 +45,23 @@
 #include "gimple-fold.h"
 
 namespace aarch64_acle {
+/* Build an asm volatile statement that prevents VALUE from being optimized
+   away, even if it is not used.
+   Equivalent to the following C code:
+   asm volatile ("" : : "w" (VALUE));  */
+gimple *
+build_optimization_barrier (tree value)
+{
+  vec<tree, va_gc> *inputs = nullptr;
+  vec_safe_push (inputs,
+                build_tree_list (build_tree_list (NULL_TREE,
+                                                  build_string (1, "w")),
+                                 value));
+  auto stmt = gimple_build_asm_vec ("", inputs, nullptr, nullptr, nullptr);
+  gimple_asm_set_volatile (stmt, true);
+  return stmt;
+}
+
 /* Build a cast expression, `(TYPE)EXPR`, if necessary to make an expression
    with type TYPE.  */
 tree
@@ -162,8 +179,24 @@ one_element_vector_type_p (tree type)
    At least one of `expand` or `fold` must be overriden by derived classes.  */
 class gimple_function_base : public function_base
 {
+public:
   rtx expand (function_expander &) const override { gcc_unreachable (); }
   gimple *fold (gimple_folder &) const override { gcc_unreachable (); }
+
+  /* If the expression can raise an FP exception, use an optimization barrier
+     to prevent it being removed even if the resulting value is not used.
+     FIXME (Karl Meakin): This shouldn't be necessary, but not all
+     optimizations obey `-ftrapping-math` at the moment.  */
+  gimple *maybe_optimization_barrier (gimple_folder &f, tree ret) const
+  {
+    if (f.call_properties () & CP_RAISE_FP_EXCEPTIONS)
+      {
+       f.assign (f.lhs, ret);
+       return build_optimization_barrier (f.lhs);
+      }
+    else
+      return gimple_build_assign (f.lhs, ret);
+  }
 };
 
 template <insn_code insn> struct gimple_exact_insn : gimple_function_base
@@ -281,7 +314,7 @@ public:
        gcc_unreachable ();
       }
 
-    return gimple_build_assign (f.lhs, ret);
+    return this->maybe_optimization_barrier (f, ret);
   }
 };
 
diff --git a/gcc/testsuite/gcc.target/aarch64/neon/discarded_result.c 
b/gcc/testsuite/gcc.target/aarch64/neon/discarded_result.c
index 1adec7fe8d3b..c5033a6429e6 100644
--- a/gcc/testsuite/gcc.target/aarch64/neon/discarded_result.c
+++ b/gcc/testsuite/gcc.target/aarch64/neon/discarded_result.c
@@ -8,6 +8,7 @@
 
 /*
 ** foo:
+** fadd        v[0-9]+\.2s, (v0\.2s, v1\.2s|v1\.2s, v0\.2s)
 ** ret
 */
 void foo(float32x2_t a, float32x2_t b)
diff --git a/gcc/testsuite/gcc.target/aarch64/neon/no_trapping_math.c 
b/gcc/testsuite/gcc.target/aarch64/neon/no_trapping_math.c
new file mode 100644
index 000000000000..e4cd471d0e52
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/neon/no_trapping_math.c
@@ -0,0 +1,68 @@
+/* { dg-do compile } */
+/* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-additional-options "-fno-trapping-math" } */
+
+/* These tests assert that floating point instructions are removed if the 
result
+   is unused and `-ftrapping-math` is disabled. */
+
+#include "arm_neon_test.h"
+
+/*
+** test_vadd_f16:
+** ret
+*/
+void
+test_vadd_f16 (float16x4_t a, float16x4_t b)
+{
+  vadd_f16 (a, b);
+}
+
+/*
+** test_vadd_f32:
+** ret
+*/
+void
+test_vadd_f32 (float32x2_t a, float32x2_t b)
+{
+  vadd_f32 (a, b);
+}
+
+/*
+** test_vadd_f64:
+** ret
+*/
+void
+test_vadd_f64 (float64x1_t a, float64x1_t b)
+{
+  vadd_f64 (a, b);
+}
+
+/*
+** test_vaddq_f16:
+** ret
+*/
+void
+test_vaddq_f16 (float16x8_t a, float16x8_t b)
+{
+  vaddq_f16 (a, b);
+}
+
+/*
+** test_vaddq_f32:
+** ret
+*/
+void
+test_vaddq_f32 (float32x4_t a, float32x4_t b)
+{
+  vaddq_f32 (a, b);
+}
+
+/*
+** test_vaddq_f64:
+** ret
+*/
+void
+test_vaddq_f64 (float64x2_t a, float64x2_t b)
+{
+  vaddq_f64 (a, b);
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/neon/trapping_math.c 
b/gcc/testsuite/gcc.target/aarch64/neon/trapping_math.c
new file mode 100644
index 000000000000..0f56da59c12e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/neon/trapping_math.c
@@ -0,0 +1,74 @@
+/* { dg-do compile } */
+/* { dg-final { check-function-bodies "**" "" } } */
+/* { dg-additional-options "-ftrapping-math" } */
+
+/* These tests assert that floating point expressions are not removed, even if
+   the result is unused, when `-ftrapping-math` is enabled. */
+
+#include "arm_neon_test.h"
+
+/*
+** test_vadd_f16:
+** fadd        v[0-9]+\.4h, (v0\.4h, v1\.4h|v1\.4h, v0\.4h)
+** ret
+*/
+void
+test_vadd_f16 (float16x4_t a, float16x4_t b)
+{
+  vadd_f16 (a, b);
+}
+
+/*
+** test_vadd_f32:
+** fadd        v[0-9]+\.2s, (v0\.2s, v1\.2s|v1\.2s, v0\.2s)
+** ret
+*/
+void
+test_vadd_f32 (float32x2_t a, float32x2_t b)
+{
+  vadd_f32 (a, b);
+}
+
+/*
+** test_vadd_f64:
+** fadd        d[0-9]+, (d0, d1|d1, d0)
+** ret
+*/
+void
+test_vadd_f64 (float64x1_t a, float64x1_t b)
+{
+  vadd_f64 (a, b);
+}
+
+/*
+** test_vaddq_f16:
+** fadd        v[0-9]+\.8h, (v0\.8h, v1\.8h|v1\.8h, v0\.8h)
+** ret
+*/
+void
+test_vaddq_f16 (float16x8_t a, float16x8_t b)
+{
+  vaddq_f16 (a, b);
+}
+
+/*
+** test_vaddq_f32:
+** fadd        v[0-9]+\.4s, (v0\.4s, v1\.4s|v1\.4s, v0\.4s)
+** ret
+*/
+void
+test_vaddq_f32 (float32x4_t a, float32x4_t b)
+{
+  vaddq_f32 (a, b);
+}
+
+/*
+** test_vaddq_f64:
+** fadd        v[0-9]+\.2d, (v0\.2d, v1\.2d|v1\.2d, v0\.2d)
+** ret
+*/
+void
+test_vaddq_f64 (float64x2_t a, float64x2_t b)
+{
+  vaddq_f64 (a, b);
+}
diff --git a/gcc/testsuite/gcc.target/aarch64/simd/pr99195_1.c 
b/gcc/testsuite/gcc.target/aarch64/simd/pr99195_1.c
index 765cb270b4ca..6600fdbb704b 100644
--- a/gcc/testsuite/gcc.target/aarch64/simd/pr99195_1.c
+++ b/gcc/testsuite/gcc.target/aarch64/simd/pr99195_1.c
@@ -3,7 +3,7 @@
     the top half of the vector register and no explicit zeroing instructions
     are emitted.  */
 /* { dg-do compile } */
-/* { dg-options "-O" } */
+/* { dg-options "-O -fno-trapping-math" } */
 
 #include <arm_neon.h>
 
diff --git a/gcc/testsuite/gcc.target/aarch64/simd/pr99195_2.c 
b/gcc/testsuite/gcc.target/aarch64/simd/pr99195_2.c
index f11f49e00871..6a96a617d788 100644
--- a/gcc/testsuite/gcc.target/aarch64/simd/pr99195_2.c
+++ b/gcc/testsuite/gcc.target/aarch64/simd/pr99195_2.c
@@ -3,7 +3,7 @@
     the top half of the vector register and no explicit zeroing instructions
     are emitted.  */
 /* { dg-do compile } */
-/* { dg-options "-O -march=armv8.2-a+fp16" } */
+/* { dg-options "-O -march=armv8.2-a+fp16 -fno-trapping-math" } */
 
 #include <arm_neon.h>

Reply via email to