Message ID | 2860356.0zOxi7LUvN@excalibur |
---|---|
State | New |
Headers | show |
Series | ping-2: [PATCH] c-family: Add more predefined macros for math flags | expand |
On Wed, Jul 14, 2021 at 12:32 AM Matthias Kretz <m.kretz@gsi.de> wrote: > > OK? > > On Wednesday, 30 June 2021 10:59:28 CEST Matthias Kretz wrote: > > Library code, especially in headers, sometimes needs to know how the > > compiler interprets / optimizes floating-point types and operations. > > This information can be used for additional optimizations or for > > ensuring correctness. This change makes -freciprocal-math, > > -fno-signed-zeros, -fno-trapping-math, -fassociative-math, and > > -frounding-math report their state via corresponding pre-defined macros. > > > > Signed-off-by: Matthias Kretz <m.kretz@gsi.de> > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/associative-math-1.c: New test. > > * gcc.dg/associative-math-2.c: New test. > > * gcc.dg/no-signed-zeros-1.c: New test. > > * gcc.dg/no-signed-zeros-2.c: New test. > > * gcc.dg/no-trapping-math-1.c: New test. > > * gcc.dg/no-trapping-math-2.c: New test. > > * gcc.dg/reciprocal-math-1.c: New test. > > * gcc.dg/reciprocal-math-2.c: New test. > > * gcc.dg/rounding-math-1.c: New test. > > * gcc.dg/rounding-math-2.c: New test. > > > > gcc/c-family/ChangeLog: > > > > * c-cppbuiltin.c (c_cpp_builtins_optimize_pragma): Define or > > undefine __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__, > > __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and > > __ROUNDING_MATH__ according to the new optimization flags. > > > > gcc/ChangeLog: > > > > * cppbuiltin.c (define_builtin_macros_for_compilation_flags): > > Define __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__, > > __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and > > __ROUNDING_MATH__ according to their corresponding flags. > > * doc/cpp.texi: Document __RECIPROCAL_MATH__, > > __NO_SIGNED_ZEROS__, __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, > > and __ROUNDING_MATH__. > > --- > > gcc/c-family/c-cppbuiltin.c | 25 +++++++++++++++++++++++ > > gcc/cppbuiltin.c | 10 +++++++++ > > gcc/doc/cpp.texi | 18 ++++++++++++++++ > > gcc/testsuite/gcc.dg/associative-math-1.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/associative-math-2.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/no-signed-zeros-1.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/no-signed-zeros-2.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/no-trapping-math-1.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/no-trapping-math-2.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/reciprocal-math-1.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/reciprocal-math-2.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/rounding-math-1.c | 17 +++++++++++++++ > > gcc/testsuite/gcc.dg/rounding-math-2.c | 17 +++++++++++++++ > > 13 files changed, 223 insertions(+) > > create mode 100644 gcc/testsuite/gcc.dg/associative-math-1.c > > create mode 100644 gcc/testsuite/gcc.dg/associative-math-2.c > > create mode 100644 gcc/testsuite/gcc.dg/no-signed-zeros-1.c > > create mode 100644 gcc/testsuite/gcc.dg/no-signed-zeros-2.c > > create mode 100644 gcc/testsuite/gcc.dg/no-trapping-math-1.c > > create mode 100644 gcc/testsuite/gcc.dg/no-trapping-math-2.c > > create mode 100644 gcc/testsuite/gcc.dg/reciprocal-math-1.c > > create mode 100644 gcc/testsuite/gcc.dg/reciprocal-math-2.c > > create mode 100644 gcc/testsuite/gcc.dg/rounding-math-1.c > > create mode 100644 gcc/testsuite/gcc.dg/rounding-math-2.c > > Hi Hongtao, Can this be used to address https://gcc.gnu.org/pipermail/gcc/2021-July/236778.html
On Wednesday, 14 July 2021 14:42:01 CEST H.J. Lu wrote: > On Wed, Jul 14, 2021 at 12:32 AM Matthias Kretz <m.kretz@gsi.de> wrote: > > OK? > > > > On Wednesday, 30 June 2021 10:59:28 CEST Matthias Kretz wrote: > > > Library code, especially in headers, sometimes needs to know how the > > > compiler interprets / optimizes floating-point types and operations. > > > This information can be used for additional optimizations or for > > > ensuring correctness. This change makes -freciprocal-math, > > > -fno-signed-zeros, -fno-trapping-math, -fassociative-math, and > > > -frounding-math report their state via corresponding pre-defined macros. > > > > > > Signed-off-by: Matthias Kretz <m.kretz@gsi.de> > > > > > > gcc/testsuite/ChangeLog: > > > * gcc.dg/associative-math-1.c: New test. > > > * gcc.dg/associative-math-2.c: New test. > > > * gcc.dg/no-signed-zeros-1.c: New test. > > > * gcc.dg/no-signed-zeros-2.c: New test. > > > * gcc.dg/no-trapping-math-1.c: New test. > > > * gcc.dg/no-trapping-math-2.c: New test. > > > * gcc.dg/reciprocal-math-1.c: New test. > > > * gcc.dg/reciprocal-math-2.c: New test. > > > * gcc.dg/rounding-math-1.c: New test. > > > * gcc.dg/rounding-math-2.c: New test. > > > > > > gcc/c-family/ChangeLog: > > > * c-cppbuiltin.c (c_cpp_builtins_optimize_pragma): Define or > > > undefine __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__, > > > __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and > > > __ROUNDING_MATH__ according to the new optimization flags. > > > > > > gcc/ChangeLog: > > > * cppbuiltin.c (define_builtin_macros_for_compilation_flags): > > > Define __RECIPROCAL_MATH__, __NO_SIGNED_ZEROS__, > > > __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, and > > > __ROUNDING_MATH__ according to their corresponding flags. > > > * doc/cpp.texi: Document __RECIPROCAL_MATH__, > > > __NO_SIGNED_ZEROS__, __NO_TRAPPING_MATH__, __ASSOCIATIVE_MATH__, > > > and __ROUNDING_MATH__. > > > > > Hi Hongtao, > > Can this be used to address > > https://gcc.gnu.org/pipermail/gcc/2021-July/236778.html It should help to determine when a workaround is necessary. I use inline asm to implement the workaround. Relevant libstdc++ code (not upstream yet and not making use of __ASSOCIATIVE_MATH__ yet): /* * Ensure the expressions leading up to the @p __x argument are evaluated at least once. * * Example: __force_evaluation(x + y) - y will not optimize to x with - fassociative-math. * _TV is expected to be __vector_type_t<floating-point type, N>. */ template <typename _TV> [[__gnu__::__flatten__, __gnu__::__const__]] _GLIBCXX_SIMD_INTRINSIC constexpr _TV __force_evaluation(_TV __x) noexcept { if (__builtin_is_constant_evaluated()) return __x; else return [&] { if constexpr(__have_sse) { if constexpr (sizeof(__x) >= 16) { asm("" :: "x"(__x)); asm("" : "+x"(__x)); } else if constexpr (is_same_v<__vector_type_t<float, 2>, _TV>) { asm("" :: "x"(__x[0]), "x"(__x[1])); asm("" : "+x"(__x[0]), "+x"(__x[1])); } else __assert_unreachable<_TV>(); } else if constexpr(__have_neon) { asm("" :: "w"(__x)); asm("" : "+w"(__x)); } else if constexpr (__have_power_vmx) { if constexpr (is_same_v<__vector_type_t<float, 2>, _TV>) { asm("" :: "fgr"(__x[0]), "fgr"(__x[1])); asm("" : "+fgr"(__x[0]), "+fgr"(__x[1])); } else { asm("" :: "v"(__x)); asm("" : "+v"(__x)); } } else { asm("" :: "g"(__x)); asm("" : "+g"(__x)); } return __x; }(); } // Returns __x + __y - __y without -fassociative-math optimizing to __x. // - _TV must be __vector_type_t<floating-point type, N>. // - _UV must be _TV or floating-point type. template <typename _TV, typename _UV> [[__gnu__::__const__]] _GLIBCXX_SIMD_INTRINSIC constexpr _TV __plus_minus(_TV __x, _UV __y) noexcept { #if defined __clang__ || __GCC_IEC_559 > 0 return (__x + __y) - __y; #else if (__builtin_is_constant_evaluated() || (__builtin_constant_p(__x) && __builtin_constant_p(__y))) return (__x + __y) - __y; #if defined __i386__ && !defined __SSE_MATH__ else if constexpr (sizeof(__x) == 8) { // operations on __x would use the FPU static_assert(is_same_v<_TV, __vector_type_t<float, 2>>); const auto __x4 = __vector_bitcast<float, 4>(__x); if constexpr (is_same_v<_TV, _UV>) return __vector_bitcast<float, 2>( __plus_minus(__x4, __vector_bitcast<float, 4>(__y))); else return __vector_bitcast<float, 2>(__plus_minus(__x4, __y)); } #endif else return __force_evaluation(__x + __y) - __y; #endif }
diff --git a/gcc/c-family/c-cppbuiltin.c b/gcc/c-family/c-cppbuiltin.c index f79f939bd10..671af04b1f8 100644 --- a/gcc/c-family/c-cppbuiltin.c +++ b/gcc/c-family/c-cppbuiltin.c @@ -628,6 +628,31 @@ c_cpp_builtins_optimize_pragma (cpp_reader *pfile, tree prev_tree, cpp_undef (pfile, "__FINITE_MATH_ONLY__"); cpp_define_unused (pfile, "__FINITE_MATH_ONLY__=0"); } + + if (!prev->x_flag_reciprocal_math && cur->x_flag_reciprocal_math) + cpp_define_unused (pfile, "__RECIPROCAL_MATH__"); + else if (prev->x_flag_reciprocal_math && !cur->x_flag_reciprocal_math) + cpp_undef (pfile, "__RECIPROCAL_MATH__"); + + if (!prev->x_flag_signed_zeros && cur->x_flag_signed_zeros) + cpp_undef (pfile, "__NO_SIGNED_ZEROS__"); + else if (prev->x_flag_signed_zeros && !cur->x_flag_signed_zeros) + cpp_define_unused (pfile, "__NO_SIGNED_ZEROS__"); + + if (!prev->x_flag_trapping_math && cur->x_flag_trapping_math) + cpp_undef (pfile, "__NO_TRAPPING_MATH__"); + else if (prev->x_flag_trapping_math && !cur->x_flag_trapping_math) + cpp_define_unused (pfile, "__NO_TRAPPING_MATH__"); + + if (!prev->x_flag_associative_math && cur->x_flag_associative_math) + cpp_define_unused (pfile, "__ASSOCIATIVE_MATH__"); + else if (prev->x_flag_associative_math && !cur->x_flag_associative_math) + cpp_undef (pfile, "__ASSOCIATIVE_MATH__"); + + if (!prev->x_flag_rounding_math && cur->x_flag_rounding_math) + cpp_define_unused (pfile, "__ROUNDING_MATH__"); + else if (prev->x_flag_rounding_math && !cur->x_flag_rounding_math) + cpp_undef (pfile, "__ROUNDING_MATH__"); } diff --git a/gcc/cppbuiltin.c b/gcc/cppbuiltin.c index 2c6bcfa92cf..e112ee83283 100644 --- a/gcc/cppbuiltin.c +++ b/gcc/cppbuiltin.c @@ -110,6 +110,16 @@ define_builtin_macros_for_compilation_flags (cpp_reader *pfile) cpp_define (pfile, "__SUPPORT_SNAN__"); if (!flag_errno_math) cpp_define (pfile, "__NO_MATH_ERRNO__"); + if (flag_reciprocal_math) + cpp_define (pfile, "__RECIPROCAL_MATH__"); + if (!flag_signed_zeros) + cpp_define (pfile, "__NO_SIGNED_ZEROS__"); + if (!flag_trapping_math) + cpp_define (pfile, "__NO_TRAPPING_MATH__"); + if (flag_associative_math) + cpp_define (pfile, "__ASSOCIATIVE_MATH__"); + if (flag_rounding_math) + cpp_define (pfile, "__ROUNDING_MATH__"); cpp_define_formatted (pfile, "__FINITE_MATH_ONLY__=%d", flag_finite_math_only); diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi index d4b3ff0a8b0..53f7204504c 100644 --- a/gcc/doc/cpp.texi +++ b/gcc/doc/cpp.texi @@ -2462,6 +2462,24 @@ features are supported by GCC. This macro is defined if @option{-fno-math-errno} is used, or enabled by another option such as @option{-ffast-math} or by default. +@item __RECIPROCAL_MATH__ +This macro is defined if @option{-freciprocal-math} is used, or enabled +by another option such as @option{-ffast-math} or by default. + +@item __NO_SIGNED_ZEROS__ +This macro is defined if @option{-fno-signed-zeros} is used, or enabled +by another option such as @option{-ffast-math} or by default. + +@item __NO_TRAPPING_MATH__ +This macro is defined if @option{-fno-trapping-math} is used. + +@item __ASSOCIATIVE_MATH__ +This macro is defined if @option{-fassociative-math} is used, or enabled +by another option such as @option{-ffast-math} or by default. + +@item __ROUNDING_MATH__ +This macro is defined if @option{-frounding-math} is used. + @item __GNUC_EXECUTION_CHARSET_NAME @itemx __GNUC_WIDE_EXECUTION_CHARSET_NAME These macros are defined to expand to a narrow string literal of diff --git a/gcc/testsuite/gcc.dg/associative-math-1.c b/gcc/testsuite/gcc.dg/associative-math-1.c new file mode 100644 index 00000000000..3f9ce5b4bf7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/associative-math-1.c @@ -0,0 +1,17 @@ +/* Test __ASSOCIATIVE_MATH__ is defined with -fassociative-math. */ +/* { dg-do compile } */ +/* { dg-options "-fassociative-math -fno-signed-zeros -fno-trapping-math" } */ + +#ifndef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-associative-math" +#ifdef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ defined" +#endif + +#pragma GCC optimize "-fassociative-math" +#ifndef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ not defined" +#endif diff --git a/gcc/testsuite/gcc.dg/associative-math-2.c b/gcc/testsuite/gcc.dg/associative-math-2.c new file mode 100644 index 00000000000..764d6f01bdb --- /dev/null +++ b/gcc/testsuite/gcc.dg/associative-math-2.c @@ -0,0 +1,17 @@ +/* Test __ASSOCIATIVE_MATH__ is not defined with -fno-associative-math. */ +/* { dg-do compile } */ +/* { dg-options "-fno-associative-math" } */ + +#ifdef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ defined" +#endif + +#pragma GCC optimize "-fassociative-math" +#ifndef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-associative-math" +#ifdef __ASSOCIATIVE_MATH__ +#error "__ASSOCIATIVE_MATH__ defined" +#endif diff --git a/gcc/testsuite/gcc.dg/no-signed-zeros-1.c b/gcc/testsuite/gcc.dg/no-signed-zeros-1.c new file mode 100644 index 00000000000..3b9cd71c84f --- /dev/null +++ b/gcc/testsuite/gcc.dg/no-signed-zeros-1.c @@ -0,0 +1,17 @@ +/* Test __NO_SIGNED_ZEROS__ is defined with -fno-signed-zeros. */ +/* { dg-do compile } */ +/* { dg-options "-fno-signed-zeros" } */ + +#ifndef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ not defined" +#endif + +#pragma GCC optimize "-fsigned-zeros" +#ifdef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ defined" +#endif + +#pragma GCC optimize "-fno-signed-zeros" +#ifndef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ not defined" +#endif diff --git a/gcc/testsuite/gcc.dg/no-signed-zeros-2.c b/gcc/testsuite/gcc.dg/no-signed-zeros-2.c new file mode 100644 index 00000000000..865a29f6863 --- /dev/null +++ b/gcc/testsuite/gcc.dg/no-signed-zeros-2.c @@ -0,0 +1,17 @@ +/* Test __NO_SIGNED_ZEROS__ is not defined with -fsigned-zeros. */ +/* { dg-do compile } */ +/* { dg-options "-fsigned-zeros" } */ + +#ifdef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ defined" +#endif + +#pragma GCC optimize "-fno-signed-zeros" +#ifndef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ not defined" +#endif + +#pragma GCC optimize "-fsigned-zeros" +#ifdef __NO_SIGNED_ZEROS__ +#error "__NO_SIGNED_ZEROS__ defined" +#endif diff --git a/gcc/testsuite/gcc.dg/no-trapping-math-1.c b/gcc/testsuite/gcc.dg/no-trapping-math-1.c new file mode 100644 index 00000000000..f4faec9a7a3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/no-trapping-math-1.c @@ -0,0 +1,17 @@ +/* Test __NO_TRAPPING_MATH__ is defined with -fno-trapping-math. */ +/* { dg-do compile } */ +/* { dg-options "-fno-trapping-math" } */ + +#ifndef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ not defined" +#endif + +#pragma GCC optimize "-ftrapping-math" +#ifdef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ defined" +#endif + +#pragma GCC optimize "-fno-trapping-math" +#ifndef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ not defined" +#endif diff --git a/gcc/testsuite/gcc.dg/no-trapping-math-2.c b/gcc/testsuite/gcc.dg/no-trapping-math-2.c new file mode 100644 index 00000000000..1904b5db453 --- /dev/null +++ b/gcc/testsuite/gcc.dg/no-trapping-math-2.c @@ -0,0 +1,17 @@ +/* Test __NO_TRAPPING_MATH__ is not defined with -ftrapping-math. */ +/* { dg-do compile } */ +/* { dg-options "-ftrapping-math" } */ + +#ifdef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ defined" +#endif + +#pragma GCC optimize "-fno-trapping-math" +#ifndef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ not defined" +#endif + +#pragma GCC optimize "-ftrapping-math" +#ifdef __NO_TRAPPING_MATH__ +#error "__NO_TRAPPING_MATH__ defined" +#endif diff --git a/gcc/testsuite/gcc.dg/reciprocal-math-1.c b/gcc/testsuite/gcc.dg/reciprocal-math-1.c new file mode 100644 index 00000000000..49173e23166 --- /dev/null +++ b/gcc/testsuite/gcc.dg/reciprocal-math-1.c @@ -0,0 +1,17 @@ +/* Test __RECIPROCAL_MATH__ is defined with -freciprocal-math. */ +/* { dg-do compile } */ +/* { dg-options "-freciprocal-math" } */ + +#ifndef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-reciprocal-math" +#ifdef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ defined" +#endif + +#pragma GCC optimize "-freciprocal-math" +#ifndef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ not defined" +#endif diff --git a/gcc/testsuite/gcc.dg/reciprocal-math-2.c b/gcc/testsuite/gcc.dg/reciprocal-math-2.c new file mode 100644 index 00000000000..959baa794b0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/reciprocal-math-2.c @@ -0,0 +1,17 @@ +/* Test __RECIPROCAL_MATH__ is not defined with -fno-reciprocal-math. */ +/* { dg-do compile } */ +/* { dg-options "-fno-reciprocal-math" } */ + +#ifdef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ defined" +#endif + +#pragma GCC optimize "-freciprocal-math" +#ifndef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-reciprocal-math" +#ifdef __RECIPROCAL_MATH__ +#error "__RECIPROCAL_MATH__ defined" +#endif diff --git a/gcc/testsuite/gcc.dg/rounding-math-1.c b/gcc/testsuite/gcc.dg/rounding-math-1.c new file mode 100644 index 00000000000..3b13549e48a --- /dev/null +++ b/gcc/testsuite/gcc.dg/rounding-math-1.c @@ -0,0 +1,17 @@ +/* Test __ROUNDING_MATH__ is defined with -frounding-math. */ +/* { dg-do compile } */ +/* { dg-options "-frounding-math" } */ + +#ifndef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-rounding-math" +#ifdef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ defined" +#endif + +#pragma GCC optimize "-frounding-math" +#ifndef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ not defined" +#endif diff --git a/gcc/testsuite/gcc.dg/rounding-math-2.c b/gcc/testsuite/gcc.dg/rounding-math-2.c new file mode 100644 index 00000000000..6468a84c4c7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/rounding-math-2.c @@ -0,0 +1,17 @@ +/* Test __ROUNDING_MATH__ is not defined with -fno-rounding-math. */ +/* { dg-do compile } */ +/* { dg-options "-fno-rounding-math" } */ + +#ifdef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ defined" +#endif + +#pragma GCC optimize "-frounding-math" +#ifndef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ not defined" +#endif + +#pragma GCC optimize "-fno-rounding-math" +#ifdef __ROUNDING_MATH__ +#error "__ROUNDING_MATH__ defined" +#endif