public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] jit: fix missing types for builtins [PR 95306]
@ 2020-05-26 22:09 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2020-05-26 22:09 UTC (permalink / raw)
  To: gcc-patches, jit

PR jit/95306 reports that attempts to use builtins
__builtin_sadd_overflow" and "__builtin_memcpy" via
gcc_jit_context_get_builtin_function lead to inscrutable error
messages of the form:
  unimplemented primitive type for builtin: 42
and:
  unimplemented primitive type for builtin: 38

The root cause is that jit-builtins.c only implements a subset
of the types defined via DEF_PRIMITIVE_TYPE in builtin-types.def.

This patch:
- implements enough types to enable the above two builtins to be
  referenced
- documents gcc_jit_context_get_builtin_function, and notes the
  limitation that not all types are supported (supporting
  some of them would take a lot of extra work)
- improves the error message for the unsupported cases
- adds a testcase for __builtin_memcpy.  This required
  jit_langhook_global_bindings_p to be implemented (otherwise
  the assertion there failed deep inside "expand" on the builtin)
- adds test coverage for the above

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to master as r11-647-gbf40f0ba95037f235b007a55a7682646a0578b26.

gcc/jit/ChangeLog:
	PR jit/95306
	* docs/topics/functions.rst
	(gcc_jit_context_get_builtin_function): Document.
	* docs/_build/texinfo/libgccjit.texi: Regenerate.
	* dummy-frontend.c (jit_langhook_global_bindings_p): Remove
	gcc_unreachable.
	* jit-builtins.c (type_names): New array.
	(get_string_for_type_id): New function.
	(gcc::jit::builtins_manager::make_primitive_type): Show name of
	type in error messages.  Update cases to reflect the order in
	builtin-types.def.  Implement cases for BT_INT8, BT_INT16,
	BT_UINT8, BT_CONST_PTR, BT_VOLATILE_PTR, BT_INT_PTR, BT_FLOAT_PTR,
	BT_CONST_DOUBLE_PTR, BT_SIZE, BT_CONST_SIZE.

gcc/testsuite/ChangeLog:
	PR jit/95306
	* jit.dg/all-non-failing-tests.h: Add test-builtin-memcpy.c and
	test-pr95306-builtin-types.c.
	* jit.dg/test-builtin-memcpy.c: New test.
	* jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c:
	New test.
	* jit.dg/test-pr95306-builtin-types.c: New test.
---
 gcc/jit/docs/topics/functions.rst             |  15 +++
 gcc/jit/dummy-frontend.c                      |   1 -
 gcc/jit/jit-builtins.c                        | 122 ++++++++++++++++--
 gcc/testsuite/jit.dg/all-non-failing-tests.h  |  20 +++
 gcc/testsuite/jit.dg/test-builtin-memcpy.c    |  69 ++++++++++
 ..._get_builtin_function-unimplemented-type.c |  20 +++
 .../jit.dg/test-pr95306-builtin-types.c       |  22 ++++
 7 files changed, 259 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-builtin-memcpy.c
 create mode 100644 gcc/testsuite/jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c
 create mode 100644 gcc/testsuite/jit.dg/test-pr95306-builtin-types.c

diff --git a/gcc/jit/docs/topics/functions.rst b/gcc/jit/docs/topics/functions.rst
index 29ce96c1e56..eb40d64010e 100644
--- a/gcc/jit/docs/topics/functions.rst
+++ b/gcc/jit/docs/topics/functions.rst
@@ -125,6 +125,21 @@ Functions
                gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,\
                                                      const char *name)
 
+   Get the :type:`gcc_jit_function` for the built-in function with the
+   given name.  For example:
+
+   .. code-block:: c
+
+      gcc_jit_function *fn
+        = gcc_jit_context_get_builtin_function (ctxt, "__builtin_memcpy");
+
+   .. note:: Due to technical limitations with how libgccjit interacts with
+      the insides of GCC, not all built-in functions are supported.  More
+      precisely, not all types are supported for parameters of built-in
+      functions from libgccjit.  Attempts to get a built-in function that
+      uses such a parameter will lead to an error being emitted within
+      the context.
+
 .. function::  gcc_jit_object *\
                gcc_jit_function_as_object (gcc_jit_function *func)
 
diff --git a/gcc/jit/dummy-frontend.c b/gcc/jit/dummy-frontend.c
index 956ba625164..27fe9d3db96 100644
--- a/gcc/jit/dummy-frontend.c
+++ b/gcc/jit/dummy-frontend.c
@@ -230,7 +230,6 @@ jit_langhook_builtin_function (tree decl)
 static bool
 jit_langhook_global_bindings_p (void)
 {
-  gcc_unreachable ();
   return true;
 }
 
diff --git a/gcc/jit/jit-builtins.c b/gcc/jit/jit-builtins.c
index 2ea298b4415..4842ff3ea96 100644
--- a/gcc/jit/jit-builtins.c
+++ b/gcc/jit/jit-builtins.c
@@ -245,6 +245,85 @@ builtins_manager::make_builtin_function (enum built_in_function builtin_id)
   return result;
 }
 
+/* Build an array of type names for use by get_string_for_type_id.  */
+
+static const char * const type_names[] = {
+#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) #ENUM,
+#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) #ENUM,
+#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) #ENUM,
+#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
+#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
+#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
+#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) #ENUM,
+#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			    ARG6)					\
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			    ARG6, ARG7)					\
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			    ARG6, ARG7, ARG8)				\
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			    ARG6, ARG7, ARG8, ARG9)			\
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			     ARG6, ARG7, ARG8, ARG9, ARG10)		 \
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+			     ARG6, ARG7, ARG8, ARG9, ARG10, ARG11)	 \
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+				ARG6)					\
+					  #ENUM,
+#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
+				ARG6, ARG7)				\
+					  #ENUM,
+#define DEF_POINTER_TYPE(ENUM, TYPE) #ENUM,
+
+#include "builtin-types.def"
+
+#undef DEF_PRIMITIVE_TYPE
+#undef DEF_FUNCTION_TYPE_0
+#undef DEF_FUNCTION_TYPE_1
+#undef DEF_FUNCTION_TYPE_2
+#undef DEF_FUNCTION_TYPE_3
+#undef DEF_FUNCTION_TYPE_4
+#undef DEF_FUNCTION_TYPE_5
+#undef DEF_FUNCTION_TYPE_6
+#undef DEF_FUNCTION_TYPE_7
+#undef DEF_FUNCTION_TYPE_8
+#undef DEF_FUNCTION_TYPE_9
+#undef DEF_FUNCTION_TYPE_10
+#undef DEF_FUNCTION_TYPE_11
+#undef DEF_FUNCTION_TYPE_VAR_0
+#undef DEF_FUNCTION_TYPE_VAR_1
+#undef DEF_FUNCTION_TYPE_VAR_2
+#undef DEF_FUNCTION_TYPE_VAR_3
+#undef DEF_FUNCTION_TYPE_VAR_4
+#undef DEF_FUNCTION_TYPE_VAR_5
+#undef DEF_FUNCTION_TYPE_VAR_6
+#undef DEF_FUNCTION_TYPE_VAR_7
+#undef DEF_POINTER_TYPE
+};
+
+/* Get a string for TYPE_ID suitable for use in logs and error messages
+   (e.g. "BT_PID").  */
+
+static const char *
+get_string_for_type_id (enum jit_builtin_type type_id)
+{
+  gcc_assert (type_id < sizeof (type_names)/sizeof(type_names[0]));
+  return type_names[type_id];
+}
+
 /* Get the recording::type for a given type of builtin function,
    by ID, creating it if it doesn't already exist.  */
 
@@ -383,7 +462,8 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
     default:
       // only some of these types are implemented so far:
       m_ctxt->add_error (NULL,
-			 "unimplemented primitive type for builtin: %d", type_id);
+			 "unimplemented primitive type for builtin (type: %s)",
+			 get_string_for_type_id (type_id));
       return NULL;
 
     case BT_VOID: return m_ctxt->get_type (GCC_JIT_TYPE_VOID);
@@ -395,10 +475,11 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
     case BT_LONGLONG: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_LONG);
     case BT_ULONGLONG:
       return m_ctxt->get_type (GCC_JIT_TYPE_UNSIGNED_LONG_LONG);
-    // case BT_INT128:
-    // case BT_UINT128:
     // case BT_INTMAX:
     // case BT_UINTMAX:
+    case BT_INT8: return m_ctxt->get_int_type (1, true);
+    case BT_INT16: return m_ctxt->get_int_type (2, true);
+    case BT_UINT8: return m_ctxt->get_int_type (1, false);
     case BT_UINT16: return m_ctxt->get_int_type (2, false);
     case BT_UINT32: return m_ctxt->get_int_type (4, false);
     case BT_UINT64: return m_ctxt->get_int_type (8, false);
@@ -407,6 +488,13 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
     case BT_FLOAT: return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT);
     case BT_DOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE);
     case BT_LONGDOUBLE: return m_ctxt->get_type (GCC_JIT_TYPE_LONG_DOUBLE);
+    // case BT_FLOAT16:
+    // case BT_FLOAT32:
+    // case BT_FLOAT64:
+    // case BT_FLOAT128:
+    // case BT_FLOAT32X:
+    // case BT_FLOAT64X:
+    // case BT_FLOAT128X:
     case BT_COMPLEX_FLOAT:
       return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_FLOAT);
     case BT_COMPLEX_DOUBLE:
@@ -415,18 +503,33 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
       return m_ctxt->get_type (GCC_JIT_TYPE_COMPLEX_LONG_DOUBLE);
     case BT_PTR: return m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
     case BT_FILEPTR: return m_ctxt->get_type (GCC_JIT_TYPE_FILE_PTR);
-    // case BT_CONST:
-    // case BT_VOLATILE_PTR:
+    // case BT_CONST_TM_PTR:
+    // case BT_FENV_T_PTR:
+    // case BT_CONST_FENV_T_PTR:
+    // case BT_FEXCEPT_T_PTR:
+    // case BT_CONST_FEXCEPT_T_PTR:
+    case BT_CONST_PTR:
+      return m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_const ()->get_pointer ();
+    case BT_VOLATILE_PTR:
+      return (m_ctxt->get_type (GCC_JIT_TYPE_VOID)->get_volatile ()
+	      ->get_pointer ());
     // case BT_CONST_VOLATILE_PTR:
     // case BT_PTRMODE:
-    // case BT_INT_PTR:
-    // case BT_FLOAT_PTR:
+    case BT_INT_PTR:
+      return m_ctxt->get_type (GCC_JIT_TYPE_INT)->get_pointer ();
+    case BT_FLOAT_PTR:
+      return m_ctxt->get_type (GCC_JIT_TYPE_FLOAT)->get_pointer ();
     case BT_DOUBLE_PTR:
       return m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_pointer ();
-    // case BT_CONST_DOUBLE_PTR:
+    case BT_CONST_DOUBLE_PTR:
+      return (m_ctxt->get_type (GCC_JIT_TYPE_DOUBLE)->get_const ()
+	      ->get_pointer ());
     // case BT_LONGDOUBLE_PTR:
     // case BT_PID:
-    // case BT_SIZE:
+    case BT_SIZE:
+      return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
+    case BT_CONST_SIZE:
+      return m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T)->get_const ();
     // case BT_SSIZE:
     // case BT_WINT:
     // case BT_STRING:
@@ -441,6 +544,7 @@ builtins_manager::make_primitive_type (enum jit_builtin_type type_id)
     // case BT_I4:
     // case BT_I8:
     // case BT_I16:
+    // case BT_PTR_CONST_STRING:
     }
 }
 
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index ad469dad699..babcd3979b7 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -67,6 +67,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-builtin-memcpy.c */
+#define create_code create_code_builtin_memcpy
+#define verify_code verify_code_builtin_memcpy
+#include "test-builtin-memcpy.c"
+#undef create_code
+#undef verify_code
+
 /* test-calling-external-function.c */
 #define create_code create_code_calling_external_function
 #define verify_code verify_code_calling_external_function
@@ -220,6 +227,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-pr95306-builtin-types.c.  */
+#define create_code create_code_pr95306_builtin_types
+#define verify_code verify_code_pr95306_builtin_types
+#include "test-pr95306-builtin-types.c"
+#undef create_code
+#undef verify_code
+
 /* test-reading-struct.c */
 #define create_code create_code_reading_struct
 #define verify_code verify_code_reading_struct
@@ -318,6 +332,9 @@ const struct testcase testcases[] = {
   {"autovectorize",
    create_code_autovectorize,
    verify_code_autovectorize},
+  {"builtin-memcpy",
+   create_code_builtin_memcpy,
+   verify_code_builtin_memcpy},
   {"calling_external_function",
    create_code_calling_external_function,
    verify_code_calling_external_function},
@@ -381,6 +398,9 @@ const struct testcase testcases[] = {
   {"pr66779",
    create_code_pr66779,
    verify_code_pr66779},
+  {"pr95306_builtin_types",
+   create_code_pr95306_builtin_types,
+   verify_code_pr95306_builtin_types},
   {"reading_struct ",
    create_code_reading_struct ,
    verify_code_reading_struct },
diff --git a/gcc/testsuite/jit.dg/test-builtin-memcpy.c b/gcc/testsuite/jit.dg/test-builtin-memcpy.c
new file mode 100644
index 00000000000..ba4fa59fb1b
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-builtin-memcpy.c
@@ -0,0 +1,69 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+      void *
+      test_memcpy (void *dest, const void *src, size_t n)
+      {
+        return __builtin_memcpy (dest, src, n);
+      }
+   */
+  gcc_jit_type *t_void_ptr
+    = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID_PTR);
+  gcc_jit_type *t_const_void_ptr
+    = gcc_jit_type_get_pointer (gcc_jit_type_get_const
+				(gcc_jit_context_get_type
+				 (ctxt, GCC_JIT_TYPE_VOID)));
+  gcc_jit_type *t_size_t
+    = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_SIZE_T);
+
+  gcc_jit_param *dest
+    = gcc_jit_context_new_param (ctxt, NULL, t_void_ptr, "dest");
+  gcc_jit_param *src
+    = gcc_jit_context_new_param (ctxt, NULL, t_const_void_ptr, "src");
+  gcc_jit_param *n = gcc_jit_context_new_param (ctxt, NULL, t_size_t, "n");
+  gcc_jit_param *params[3] = {dest, src, n};
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt, NULL,
+				  GCC_JIT_FUNCTION_EXPORTED,
+				  t_void_ptr,
+				  "test_memcpy",
+				  3, params, 0);
+
+  gcc_jit_function *jit_memcpy
+    = gcc_jit_context_get_builtin_function (ctxt, "__builtin_memcpy");
+
+
+  gcc_jit_block *b_initial
+    = gcc_jit_function_new_block (func, "initial");
+  gcc_jit_rvalue *args[3] = {gcc_jit_param_as_rvalue (dest),
+			     gcc_jit_param_as_rvalue (src),
+			     gcc_jit_param_as_rvalue (n)};
+  gcc_jit_rvalue *call
+    = gcc_jit_context_new_call (ctxt, NULL, jit_memcpy, 3, args);
+  gcc_jit_block_end_with_return (b_initial, NULL, call);
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  typedef void *(*test_memcpy_type) (void *, const void *, size_t);
+  CHECK_NON_NULL (result);
+  test_memcpy_type test_memcpy =
+    (test_memcpy_type)gcc_jit_result_get_code (result, "test_memcpy");
+  CHECK_NON_NULL (test_memcpy);
+
+  int dst = 13;
+  int src = 42;
+  void *out = test_memcpy (&dst, &src, sizeof(int));
+  CHECK_VALUE(out, &dst);
+  CHECK_VALUE(dst, 42);
+}
diff --git a/gcc/testsuite/jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c
new file mode 100644
index 00000000000..89b59d4c7a8
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c
@@ -0,0 +1,20 @@
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  gcc_jit_context_get_builtin_function(ctxt, "__builtin_fork");
+}
+
+extern void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_VALUE (result, NULL);
+
+  /* Verify that the correct error message was emitted.  */
+  CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
+		      "unimplemented primitive type for builtin"
+		      " (type: BT_PID)");
+}
diff --git a/gcc/testsuite/jit.dg/test-pr95306-builtin-types.c b/gcc/testsuite/jit.dg/test-pr95306-builtin-types.c
new file mode 100644
index 00000000000..6c3b087ab98
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-pr95306-builtin-types.c
@@ -0,0 +1,22 @@
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+#define CHECK_BUILTIN(NAME) \
+  CHECK_NON_NULL (gcc_jit_context_get_builtin_function (ctxt, NAME));
+  
+  CHECK_BUILTIN ("__builtin_memcpy");
+  CHECK_BUILTIN ("__builtin_sadd_overflow");
+
+#undef CHECK_BUILTIN
+}
+
+extern void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  /* Verify that no errors were emitted.  */
+  CHECK_NON_NULL (result);
+}
-- 
2.21.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-05-26 22:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-26 22:09 [committed] jit: fix missing types for builtins [PR 95306] David Malcolm

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).