jit: fix missing types for builtins [PR 95306]
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 26 May 2020 13:28:16 +0000 (09:28 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 26 May 2020 22:00:46 +0000 (18:00 -0400)
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

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/_build/texinfo/libgccjit.texi
gcc/jit/docs/topics/functions.rst
gcc/jit/dummy-frontend.c
gcc/jit/jit-builtins.c
gcc/testsuite/jit.dg/all-non-failing-tests.h
gcc/testsuite/jit.dg/test-builtin-memcpy.c [new file with mode: 0644]
gcc/testsuite/jit.dg/test-error-gcc_jit_context_get_builtin_function-unimplemented-type.c [new file with mode: 0644]
gcc/testsuite/jit.dg/test-pr95306-builtin-types.c [new file with mode: 0644]

index 8eed7d27b3ddc617d075e52b4ab8d95c0ca9446c..6114f8194a5f32d17e780c68b1c2287ed4cf9a78 100644 (file)
@@ -7037,7 +7037,26 @@ buffer.
 
 @geindex gcc_jit_context_get_builtin_function (C function)
 @anchor{topics/functions c gcc_jit_context_get_builtin_function}@anchor{df}
-@deffn {C Function} gcc_jit_function *gcc_jit_context_get_builtin_function (gcc_jit_context@w{ }*ctxt, const char@w{ }*name)
+@deffn {C Function} gcc_jit_function *            gcc_jit_context_get_builtin_function (gcc_jit_context@w{ }*ctxt, const char@w{ }*name)
+
+Get the @ref{29,,gcc_jit_function} for the built-in function with the
+given name.  For example:
+
+@example
+gcc_jit_function *fn
+  = gcc_jit_context_get_builtin_function (ctxt, "__builtin_memcpy");
+@end example
+
+@cartouche
+@quotation 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.
+@end quotation
+@end cartouche
 @end deffn
 
 @geindex gcc_jit_function_as_object (C function)
index 29ce96c1e56f15cef44da38927a7c9ce4f19f5b4..eb40d64010e22b411d698659936d27c0164da32f 100644 (file)
@@ -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)
 
index 956ba625164c3457845b131854bb3cc664c904ad..27fe9d3db9699568f20a44288efb913db1dcd630 100644 (file)
@@ -230,7 +230,6 @@ jit_langhook_builtin_function (tree decl)
 static bool
 jit_langhook_global_bindings_p (void)
 {
-  gcc_unreachable ();
   return true;
 }
 
index 2ea298b4415844101d4ed073247e0e3d6e3ead6d..4842ff3ea968b7239b465c4875aab1d2f9a269a8 100644 (file)
@@ -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:
     }
 }
 
index ad469dad6994177358f11089d39aada7b0e8a425..babcd3979b751752bca6ddff9cfb15ef5b5b64f3 100644 (file)
 #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
 #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 (file)
index 0000000..ba4fa59
--- /dev/null
@@ -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 (file)
index 0000000..89b59d4
--- /dev/null
@@ -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 (file)
index 0000000..6c3b087
--- /dev/null
@@ -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);
+}