public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Issue a warning for conversion between short and __bf16 under TARGET_AVX512BF16.
@ 2023-06-26  8:54 liuhongt
  2023-06-27  5:13 ` Hongtao Liu
  0 siblings, 1 reply; 2+ messages in thread
From: liuhongt @ 2023-06-26  8:54 UTC (permalink / raw)
  To: gcc-patches; +Cc: crazylht, hjl.tools

__bfloat16 is redefined from typedef short to real __bf16 since GCC
V13. The patch issues an warning for potential silent implicit
conversion between __bf16 and short where users may only expect a
data movement.

To avoid too many false positive, warning is only under
TARGET_AVX512BF16.

Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}
Ok for trunk?

gcc/ChangeLog:

	* config/i386/i386.cc (ix86_invalid_conversion): New function.
	(TARGET_INVALID_CONVERSION): Define as
	ix86_invalid_conversion.

gcc/testsuite/ChangeLog:

	* gcc.target/i386/bf16_short_warn.c: New test.
---
 gcc/config/i386/i386.cc                       | 32 +++++++++++++++++++
 .../gcc.target/i386/bf16_short_warn.c         | 17 ++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/i386/bf16_short_warn.c

diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index 0761965344b..dc02eac6203 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -22718,6 +22718,35 @@ x86_emit_floatuns (rtx operands[2])
 
   emit_label (donelab);
 }
+
+/* Return the diagnostic message string if conversion from FROMTYPE to
+   TOTYPE is not allowed, NULL otherwise.
+   Currently it's used to warn for silent implicit conversion between __bf16
+   and short, since __bfloat16 is refined as real __bf16 instead of short
+   since GCC13.  */
+
+static const char *
+ix86_invalid_conversion (const_tree fromtype, const_tree totype)
+{
+  if (element_mode (fromtype) != element_mode (totype)
+      && (TARGET_AVX512BF16 || TARGET_AVXNECONVERT))
+    {
+      /* Warn for silent implicit conversion where user may expect
+	 a bitcast.  */
+      if ((TYPE_MODE (fromtype) == BFmode
+	   && TYPE_MODE (totype) == HImode)
+	  || (TYPE_MODE (totype) == BFmode
+	      && TYPE_MODE (fromtype) == HImode))
+	warning (0, "%<__bfloat16%> is redefined from typedef %<short%> "
+		"to real %<__bf16%> since GCC V13, be careful of "
+		 "implicit conversion between %<__bf16%> and %<short%>; "
+		 "a explicit bitcast may be needed here");
+    }
+
+  /* Conversion allowed.  */
+  return NULL;
+}
+
 \f
 /* Target hook for scalar_mode_supported_p.  */
 static bool
@@ -25009,6 +25038,9 @@ ix86_run_selftests (void)
 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
 #endif
 
+#undef TARGET_INVALID_CONVERSION
+#define TARGET_INVALID_CONVERSION ix86_invalid_conversion
+
 #undef TARGET_COMP_TYPE_ATTRIBUTES
 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
 
diff --git a/gcc/testsuite/gcc.target/i386/bf16_short_warn.c b/gcc/testsuite/gcc.target/i386/bf16_short_warn.c
new file mode 100644
index 00000000000..3e47a815200
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/bf16_short_warn.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include<immintrin.h>
+typedef struct {
+short payload;
+} BFloat16;
+
+__attribute__((target("avx512vl,avx512bf16")))
+BFloat16 tobf16_avx512(float f)
+{
+    BFloat16 r;
+    __m128bh m = _mm_cvtneps_pbh(_mm_set_ss(f));
+    r.payload = m[0]; /* { dg-warning " be careful of implicit conversion between '__bf16' and 'short'" } */
+    return r;
+}
+
-- 
2.39.1.388.g2fc9e9ca3c


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Issue a warning for conversion between short and __bf16 under TARGET_AVX512BF16.
  2023-06-26  8:54 [PATCH] Issue a warning for conversion between short and __bf16 under TARGET_AVX512BF16 liuhongt
@ 2023-06-27  5:13 ` Hongtao Liu
  0 siblings, 0 replies; 2+ messages in thread
From: Hongtao Liu @ 2023-06-27  5:13 UTC (permalink / raw)
  To: liuhongt; +Cc: gcc-patches, hjl.tools

On Mon, Jun 26, 2023 at 4:54 PM liuhongt <hongtao.liu@intel.com> wrote:
>
> __bfloat16 is redefined from typedef short to real __bf16 since GCC
> V13. The patch issues an warning for potential silent implicit
> conversion between __bf16 and short where users may only expect a
> data movement.
>
> To avoid too many false positive, warning is only under
> TARGET_AVX512BF16.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}
> Ok for trunk?
Ready to install.
>
> gcc/ChangeLog:
>
>         * config/i386/i386.cc (ix86_invalid_conversion): New function.
>         (TARGET_INVALID_CONVERSION): Define as
>         ix86_invalid_conversion.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.target/i386/bf16_short_warn.c: New test.
> ---
>  gcc/config/i386/i386.cc                       | 32 +++++++++++++++++++
>  .../gcc.target/i386/bf16_short_warn.c         | 17 ++++++++++
>  2 files changed, 49 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/i386/bf16_short_warn.c
>
> diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
> index 0761965344b..dc02eac6203 100644
> --- a/gcc/config/i386/i386.cc
> +++ b/gcc/config/i386/i386.cc
> @@ -22718,6 +22718,35 @@ x86_emit_floatuns (rtx operands[2])
>
>    emit_label (donelab);
>  }
> +
> +/* Return the diagnostic message string if conversion from FROMTYPE to
> +   TOTYPE is not allowed, NULL otherwise.
> +   Currently it's used to warn for silent implicit conversion between __bf16
> +   and short, since __bfloat16 is refined as real __bf16 instead of short
> +   since GCC13.  */
> +
> +static const char *
> +ix86_invalid_conversion (const_tree fromtype, const_tree totype)
> +{
> +  if (element_mode (fromtype) != element_mode (totype)
> +      && (TARGET_AVX512BF16 || TARGET_AVXNECONVERT))
> +    {
> +      /* Warn for silent implicit conversion where user may expect
> +        a bitcast.  */
> +      if ((TYPE_MODE (fromtype) == BFmode
> +          && TYPE_MODE (totype) == HImode)
> +         || (TYPE_MODE (totype) == BFmode
> +             && TYPE_MODE (fromtype) == HImode))
> +       warning (0, "%<__bfloat16%> is redefined from typedef %<short%> "
> +               "to real %<__bf16%> since GCC V13, be careful of "
> +                "implicit conversion between %<__bf16%> and %<short%>; "
> +                "a explicit bitcast may be needed here");
> +    }
> +
> +  /* Conversion allowed.  */
> +  return NULL;
> +}
> +
>
>  /* Target hook for scalar_mode_supported_p.  */
>  static bool
> @@ -25009,6 +25038,9 @@ ix86_run_selftests (void)
>  #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
>  #endif
>
> +#undef TARGET_INVALID_CONVERSION
> +#define TARGET_INVALID_CONVERSION ix86_invalid_conversion
> +
>  #undef TARGET_COMP_TYPE_ATTRIBUTES
>  #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
>
> diff --git a/gcc/testsuite/gcc.target/i386/bf16_short_warn.c b/gcc/testsuite/gcc.target/i386/bf16_short_warn.c
> new file mode 100644
> index 00000000000..3e47a815200
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/bf16_short_warn.c
> @@ -0,0 +1,17 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2" } */
> +
> +#include<immintrin.h>
> +typedef struct {
> +short payload;
> +} BFloat16;
> +
> +__attribute__((target("avx512vl,avx512bf16")))
> +BFloat16 tobf16_avx512(float f)
> +{
> +    BFloat16 r;
> +    __m128bh m = _mm_cvtneps_pbh(_mm_set_ss(f));
> +    r.payload = m[0]; /* { dg-warning " be careful of implicit conversion between '__bf16' and 'short'" } */
> +    return r;
> +}
> +
> --
> 2.39.1.388.g2fc9e9ca3c
>


-- 
BR,
Hongtao

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-27  5:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-26  8:54 [PATCH] Issue a warning for conversion between short and __bf16 under TARGET_AVX512BF16 liuhongt
2023-06-27  5:13 ` Hongtao Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).