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

commit r15-1863-g1c314247aab43aaa278ecc51d666f8c5896d8bbb
Author: Antoni Boucher <boua...@zoho.com>
Date:   Thu Nov 16 10:59:22 2023 -0500

    libgccjit: Add support for the type bfloat16
    
    gcc/jit/ChangeLog:
    
            PR jit/112574
            * docs/topics/types.rst: Document GCC_JIT_TYPE_BFLOAT16.
            * jit-common.h: Update NUM_GCC_JIT_TYPES.
            * jit-playback.cc (get_tree_node_for_type): Support bfloat16.
            * jit-recording.cc (recording::memento_of_get_type::get_size,
            recording::memento_of_get_type::dereference,
            recording::memento_of_get_type::is_int,
            recording::memento_of_get_type::is_signed,
            recording::memento_of_get_type::is_float,
            recording::memento_of_get_type::is_bool): Support bfloat16.
            * libgccjit.h (enum gcc_jit_types): Add GCC_JIT_TYPE_BFLOAT16.
    
    gcc/testsuite/ChangeLog:
    
            PR jit/112574
            * jit.dg/all-non-failing-tests.h: New test test-bfloat16.c.
            * jit.dg/test-types.c: Test GCC_JIT_TYPE_BFLOAT16.
            * jit.dg/test-bfloat16.c: New test.

Diff:
---
 gcc/jit/docs/topics/types.rst                |  2 ++
 gcc/jit/jit-common.h                         |  2 +-
 gcc/jit/jit-playback.cc                      |  6 +++++
 gcc/jit/jit-recording.cc                     | 11 +++++++++
 gcc/jit/libgccjit.h                          |  4 ++-
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  3 +++
 gcc/testsuite/jit.dg/test-bfloat16.c         | 37 ++++++++++++++++++++++++++++
 gcc/testsuite/jit.dg/test-types.c            |  4 +++
 8 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/gcc/jit/docs/topics/types.rst b/gcc/jit/docs/topics/types.rst
index bb51f037b7e..6a7a35280ed 100644
--- a/gcc/jit/docs/topics/types.rst
+++ b/gcc/jit/docs/topics/types.rst
@@ -113,6 +113,8 @@ Standard types
        - C99's ``__int128_t``
      * - :c:data:`GCC_JIT_TYPE_FLOAT`
        -
+     * - :c:data:`GCC_JIT_TYPE_BFLOAT16`
+       - C's ``__bfloat16``
      * - :c:data:`GCC_JIT_TYPE_DOUBLE`
        -
      * - :c:data:`GCC_JIT_TYPE_LONG_DOUBLE`
diff --git a/gcc/jit/jit-common.h b/gcc/jit/jit-common.h
index 1e335878b56..afb41763e46 100644
--- a/gcc/jit/jit-common.h
+++ b/gcc/jit/jit-common.h
@@ -36,7 +36,7 @@ along with GCC; see the file COPYING3.  If not see
 #endif
 #endif
 
-const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_INT128_T + 1;
+const int NUM_GCC_JIT_TYPES = GCC_JIT_TYPE_BFLOAT16 + 1;
 
 /* This comment is included by the docs.
 
diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index b3f54da24ab..1b5445d6101 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -281,6 +281,12 @@ get_tree_node_for_type (enum gcc_jit_types type_)
 
     case GCC_JIT_TYPE_FLOAT:
       return float_type_node;
+    case GCC_JIT_TYPE_BFLOAT16:
+#ifndef HAVE_BFmode
+      add_error (NULL, "gcc_jit_types value unsupported on this target: %i",
+                type_);
+#endif
+      return bfloat16_type_node;
     case GCC_JIT_TYPE_DOUBLE:
       return double_type_node;
     case GCC_JIT_TYPE_LONG_DOUBLE:
diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc
index 43a6795f8f3..cc7f529c9e8 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -2418,6 +2418,10 @@ recording::memento_of_get_type::get_size ()
       m = targetm.c.mode_for_floating_type (TI_FLOAT_TYPE);
       size = GET_MODE_PRECISION (m).to_constant ();
       break;
+#ifdef HAVE_BFmode
+    case GCC_JIT_TYPE_BFLOAT16:
+      return GET_MODE_UNIT_SIZE (BFmode);
+#endif
     case GCC_JIT_TYPE_DOUBLE:
       m = targetm.c.mode_for_floating_type (TI_DOUBLE_TYPE);
       size = GET_MODE_PRECISION (m).to_constant ();
@@ -2479,6 +2483,7 @@ recording::memento_of_get_type::dereference ()
     case GCC_JIT_TYPE_INT64_T:
     case GCC_JIT_TYPE_INT128_T:
     case GCC_JIT_TYPE_FLOAT:
+    case GCC_JIT_TYPE_BFLOAT16:
     case GCC_JIT_TYPE_DOUBLE:
     case GCC_JIT_TYPE_LONG_DOUBLE:
     case GCC_JIT_TYPE_COMPLEX_FLOAT:
@@ -2543,6 +2548,7 @@ recording::memento_of_get_type::is_int () const
       return true;
 
     case GCC_JIT_TYPE_FLOAT:
+    case GCC_JIT_TYPE_BFLOAT16:
     case GCC_JIT_TYPE_DOUBLE:
     case GCC_JIT_TYPE_LONG_DOUBLE:
       return false;
@@ -2601,6 +2607,7 @@ recording::memento_of_get_type::is_signed () const
     case GCC_JIT_TYPE_UINT128_T:
 
     case GCC_JIT_TYPE_FLOAT:
+    case GCC_JIT_TYPE_BFLOAT16:
     case GCC_JIT_TYPE_DOUBLE:
     case GCC_JIT_TYPE_LONG_DOUBLE:
 
@@ -2660,6 +2667,7 @@ recording::memento_of_get_type::is_float () const
       return false;
 
     case GCC_JIT_TYPE_FLOAT:
+    case GCC_JIT_TYPE_BFLOAT16:
     case GCC_JIT_TYPE_DOUBLE:
     case GCC_JIT_TYPE_LONG_DOUBLE:
       return true;
@@ -2723,6 +2731,7 @@ recording::memento_of_get_type::is_bool () const
       return false;
 
     case GCC_JIT_TYPE_FLOAT:
+    case GCC_JIT_TYPE_BFLOAT16:
     case GCC_JIT_TYPE_DOUBLE:
     case GCC_JIT_TYPE_LONG_DOUBLE:
       return false;
@@ -2803,6 +2812,7 @@ static const char * const get_type_strings[] = {
   "__int64_t",    /* GCC_JIT_TYPE_INT64_T */
   "__int128_t",   /* GCC_JIT_TYPE_INT128_T */
 
+  "bfloat16", /* GCC_JIT_TYPE_BFLOAT16 */
 };
 
 /* Implementation of recording::memento::make_debug_string for
@@ -2848,6 +2858,7 @@ static const char * const get_type_enum_strings[] = {
   "GCC_JIT_TYPE_INT32_T",
   "GCC_JIT_TYPE_INT64_T",
   "GCC_JIT_TYPE_INT128_T",
+  "GCC_JIT_TYPE_BFLOAT16",
 };
 
 void
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index c82a4be792e..1d5be27374e 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -604,7 +604,9 @@ enum gcc_jit_types
   GCC_JIT_TYPE_INT16_T,
   GCC_JIT_TYPE_INT32_T,
   GCC_JIT_TYPE_INT64_T,
-  GCC_JIT_TYPE_INT128_T
+  GCC_JIT_TYPE_INT128_T,
+
+  GCC_JIT_TYPE_BFLOAT16,
 };
 
 extern gcc_jit_type *
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 63b4e78c8ce..75721329ab5 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -80,6 +80,9 @@
 #undef create_code
 #undef verify_code
 
+/* test-bfloat16.c: This can't be in the testcases array as it
+   is target-specific.  */
+
 /* test-builtin-memcpy.c */
 #define create_code create_code_builtin_memcpy
 #define verify_code verify_code_builtin_memcpy
diff --git a/gcc/testsuite/jit.dg/test-bfloat16.c 
b/gcc/testsuite/jit.dg/test-bfloat16.c
new file mode 100644
index 00000000000..6aed3920351
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-bfloat16.c
@@ -0,0 +1,37 @@
+/* { dg-do compile { target x86_64-*-* } } */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "libgccjit.h"
+
+/* We don't want set_options() in harness.h to set -O3 so our little local
+   is optimized away. */
+#define TEST_ESCHEWS_SET_OPTIONS
+static void set_options (gcc_jit_context *ctxt, const char *argv0)
+{
+}
+
+#define TEST_COMPILING_TO_FILE
+#define OUTPUT_KIND      GCC_JIT_OUTPUT_KIND_ASSEMBLER
+#define OUTPUT_FILENAME  "output-of-test-bfloat16.c.s"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  gcc_jit_type *bf16_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BFLOAT16);
+
+  gcc_jit_lvalue *foo =
+    gcc_jit_context_new_global (
+      ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, bf16_type, "foo");
+
+  gcc_jit_rvalue *value =
+    gcc_jit_context_new_rvalue_from_double (ctxt, bf16_type, 3.1415);
+  gcc_jit_global_set_initializer_rvalue (foo, value);
+}
+
+/* { dg-final { jit-verify-output-file-was-created "" } } */
+/* { dg-final { jit-verify-assembler-output ".value    16457" } } */
+/* { dg-final { jit-verify-assembler-output ".size     foo, 2" } } */
diff --git a/gcc/testsuite/jit.dg/test-types.c 
b/gcc/testsuite/jit.dg/test-types.c
index a01944e35fa..f51252e0da0 100644
--- a/gcc/testsuite/jit.dg/test-types.c
+++ b/gcc/testsuite/jit.dg/test-types.c
@@ -1,3 +1,4 @@
+#include <immintrin.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -492,4 +493,7 @@ verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
 
   CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, 
GCC_JIT_TYPE_FLOAT)), sizeof (float));
   CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, 
GCC_JIT_TYPE_DOUBLE)), sizeof (double));
+#ifdef HAVE_BFmode
+  CHECK_VALUE (gcc_jit_type_get_size (gcc_jit_context_get_type (ctxt, 
GCC_JIT_TYPE_BFLOAT16)), sizeof (__bfloat16));
+#endif
 }

Reply via email to