[gcc r12-4651] Simplify (_Float16) sqrtf((float) a) to .SQRT(a) when a is a _Float16 value.

hongtao Liu liuhongt@gcc.gnu.org
Mon Oct 25 08:54:06 GMT 2021


https://gcc.gnu.org/g:1a07bc9cda77b1211e95ae295b30e46c0d9ee222

commit r12-4651-g1a07bc9cda77b1211e95ae295b30e46c0d9ee222
Author: liuhongt <hongtao.liu@intel.com>
Date:   Mon Oct 25 10:51:33 2021 +0800

    Simplify (_Float16) sqrtf((float) a) to .SQRT(a) when a is a _Float16 value.
    
    Similar for sqrt/sqrtl.
    
    gcc/ChangeLog:
    
            PR target/102464
            * match.pd: Simplify (_Float16) sqrtf((float) a) to .SQRT(a)
            when direct_internal_fn_supported_p, similar for sqrt/sqrtl.
    
    gcc/testsuite/ChangeLog:
    
            PR target/102464
            * gcc.target/i386/pr102464-sqrtph.c: New test.
            * gcc.target/i386/pr102464-sqrtsh.c: New test.

Diff:
---
 gcc/match.pd                                    |  6 ++++--
 gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c | 27 +++++++++++++++++++++++++
 gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c | 23 +++++++++++++++++++++
 3 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 5bed2e12715..43d1c1bc0bd 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6228,14 +6228,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 	    BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
 	    BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
 	    BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
-	    BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF)
+	    BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
+	    BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
 	  IFN_FLOOR IFN_FLOOR IFN_FLOOR
 	  IFN_CEIL IFN_CEIL IFN_CEIL
 	  IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
 	  IFN_ROUND IFN_ROUND IFN_ROUND
 	  IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
-	  IFN_RINT IFN_RINT IFN_RINT)
+	  IFN_RINT IFN_RINT IFN_RINT
+	  IFN_SQRT IFN_SQRT IFN_SQRT)
  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
     if x is a _Float16.  */
  (simplify
diff --git a/gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c b/gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c
new file mode 100644
index 00000000000..8bd19c6e65e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c
@@ -0,0 +1,27 @@
+/* PR target/102464.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512fp16 -mavx512vl -ffast-math -ftree-vectorize" } */
+
+#include<math.h>
+void foo1 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrtf (b[i]);
+}
+
+void foo2 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrt (b[i]);
+}
+
+void foo3 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrtl (b[i]);
+}
+
+/* { dg-final { scan-assembler-not "vcvtsh2s\[sd\]" } } */
+/* { dg-final { scan-assembler-not "vcvtph2p\[sd\]" } } */
+/* { dg-final { scan-assembler-not "extendhfxf" } } */
+/* { dg-final { scan-assembler-times "vsqrtph\[^\n\r\]*xmm\[0-9\]" 3 } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c b/gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c
new file mode 100644
index 00000000000..4cf0089a67f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c
@@ -0,0 +1,23 @@
+/* PR target/102464.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512fp16 -ffast-math" } */
+
+#include<math.h>
+_Float16 foo1 (_Float16 a)
+{
+  return sqrtf (a);
+}
+
+_Float16 foo2 (_Float16 a)
+{
+  return sqrt (a);
+}
+
+_Float16 foo3 (_Float16 a)
+{
+  return sqrtl (a);
+}
+
+/* { dg-final { scan-assembler-not "vcvtsh2s\[sd\]" } } */
+/* { dg-final { scan-assembler-not "extendhfxf" } } */
+/* { dg-final { scan-assembler-times "vsqrtsh\[^\n\r\]*xmm\[0-9\]" 3 } } */


More information about the Gcc-cvs mailing list