[C++,coroutines,2/7,v2] Define builtins and internal functions.

Message ID 83DC8C08-4504-4D44-9206-3BE5683E0328@sandoe.co.uk
State New
Headers show
Series
  • [C++,coroutines,1/7] Common code and base definitions.
Related show

Commit Message

Iain Sandoe Jan. 9, 2020, 12:36 p.m.
Jeff Law <law@redhat.com> wrote:

> On 11/17/19 3:24 AM, Iain Sandoe wrote:

>> This part of the patch series provides the builtin functions

>> used by the standard library code and the internal functions

>> used to implement lowering of the coroutine state machine.

>> 

>> gcc/ChangeLog:

>> 

>> 2019-11-17  Iain Sandoe  <iain@sandoe.co.uk>

>> 

>> 	* builtin-types.def (BT_FN_BOOL_PTR): New.

>> 	(BT_FN_PTR_PTR_SIZE_BOOL): New.

>> 	* builtins.def (DEF_COROUTINE_BUILTIN): New.

>> 	* coroutine-builtins.def: New file.

>> 	* internal-fn.c (expand_CO_FRAME): New.

>> 	(expand_CO_YIELD): New.

>> 	(expand_CO_SUSPN): New.

>> 	(expand_CO_ACTOR): New.

>> 	* internal-fn.def (CO_ACTOR): New.

>> 	(CO_YIELD): New.

>> 	(CO_SUSPN): New.

>> 	(CO_FRAME): New.

> This is OK as would be any minor adjustments you may ultimately need due

> to other feedback on the kit.


Said minor adjustments included in the patch below (the actual SVN commit IDs noted)
Iain

-- 
2.14.3

Patch

========

This part of the patch series provides the builtin functions
used by the standard library code and the internal functions
used to implement lowering of the coroutine state machine.

* Squashed commits:

r279756 - Const-qualify the align argument to promise builtin.
r279817 - Update copyright year.

gcc/ChangeLog:

2020-01-09  Iain Sandoe  <iain@sandoe.co.uk>

	* builtin-types.def (BT_CONST_SIZE): New.
	(BT_FN_BOOL_PTR): New.
	(BT_FN_PTR_PTR_CONST_SIZE_BOOL): New.
	* builtins.def (DEF_COROUTINE_BUILTIN): New.
	* coroutine-builtins.def: New file.
	* internal-fn.c (expand_CO_FRAME): New.
	(expand_CO_YIELD): New.
	(expand_CO_SUSPN): New.
	(expand_CO_ACTOR): New.
	* internal-fn.def (CO_ACTOR): New.
	(CO_YIELD): New.
	(CO_SUSPN): New.
	(CO_FRAME): New.
---
 gcc/builtin-types.def      |  5 +++++
 gcc/builtins.def           |  9 ++++++++
 gcc/coroutine-builtins.def | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 gcc/internal-fn.c          | 26 +++++++++++++++++++++++
 gcc/internal-fn.def        |  6 ++++++
 5 files changed, 99 insertions(+)
 create mode 100644 gcc/coroutine-builtins.def

diff --git a/gcc/builtin-types.def b/gcc/builtin-types.def
index b2a5a8be6c..c7aa691b24 100644
--- a/gcc/builtin-types.def
+++ b/gcc/builtin-types.def
@@ -131,6 +131,8 @@  DEF_PRIMITIVE_TYPE (BT_CONST_DOUBLE_PTR,
 DEF_PRIMITIVE_TYPE (BT_LONGDOUBLE_PTR, long_double_ptr_type_node)
 DEF_PRIMITIVE_TYPE (BT_PID, pid_type_node)
 DEF_PRIMITIVE_TYPE (BT_SIZE, size_type_node)
+DEF_PRIMITIVE_TYPE (BT_CONST_SIZE,
+		    build_qualified_type (size_type_node, TYPE_QUAL_CONST))
 DEF_PRIMITIVE_TYPE (BT_SSIZE, signed_size_type_node)
 DEF_PRIMITIVE_TYPE (BT_WINT, wint_type_node)
 DEF_PRIMITIVE_TYPE (BT_STRING, string_type_node)
@@ -300,6 +302,7 @@  DEF_FUNCTION_TYPE_1 (BT_FN_UINT32_UINT32, BT_UINT32, BT_UINT32)
 DEF_FUNCTION_TYPE_1 (BT_FN_UINT64_UINT64, BT_UINT64, BT_UINT64)
 DEF_FUNCTION_TYPE_1 (BT_FN_UINT64_FLOAT, BT_UINT64, BT_FLOAT)
 DEF_FUNCTION_TYPE_1 (BT_FN_BOOL_INT, BT_BOOL, BT_INT)
+DEF_FUNCTION_TYPE_1 (BT_FN_BOOL_PTR, BT_BOOL, BT_PTR)
 DEF_FUNCTION_TYPE_1 (BT_FN_PTR_CONST_PTR, BT_PTR, BT_CONST_PTR)
 DEF_FUNCTION_TYPE_1 (BT_FN_CONST_PTR_CONST_PTR, BT_CONST_PTR, BT_CONST_PTR)
 DEF_FUNCTION_TYPE_1 (BT_FN_UINT16_UINT32, BT_UINT16, BT_UINT32)
@@ -628,6 +631,8 @@  DEF_FUNCTION_TYPE_3 (BT_FN_VOID_UINT32_UINT32_PTR,
 DEF_FUNCTION_TYPE_3 (BT_FN_VOID_SIZE_SIZE_PTR, BT_VOID, BT_SIZE, BT_SIZE,
 		     BT_PTR)
 DEF_FUNCTION_TYPE_3 (BT_FN_UINT_UINT_PTR_PTR, BT_UINT, BT_UINT, BT_PTR, BT_PTR)
+DEF_FUNCTION_TYPE_3 (BT_FN_PTR_PTR_CONST_SIZE_BOOL,
+		     BT_PTR, BT_PTR, BT_CONST_SIZE, BT_BOOL)
 
 DEF_FUNCTION_TYPE_4 (BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR,
 		     BT_SIZE, BT_CONST_PTR, BT_SIZE, BT_SIZE, BT_FILEPTR)
diff --git a/gcc/builtins.def b/gcc/builtins.def
index 674ca239ec..5ab842c34c 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -189,6 +189,12 @@  along with GCC; see the file COPYING3.  If not see
   DEF_BUILTIN (ENUM, NAME, BUILT_IN_NORMAL, BT_LAST, BT_LAST, false, false, \
 	       false, ATTR_LAST, false, false)
 
+/* Builtins used in implementing coroutine support. */
+#undef DEF_COROUTINE_BUILTIN
+#define DEF_COROUTINE_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
+  DEF_BUILTIN (ENUM, "__builtin_coro_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
+	       true, true, true, ATTRS, true, flag_coroutines)
+
 /* Builtin used by the implementation of OpenACC and OpenMP.  Few of these are
    actually implemented in the compiler; most are in libgomp.  */
 /* These builtins also need to be enabled in offloading compilers invoked from
@@ -1064,6 +1070,9 @@  DEF_GCC_BUILTIN (BUILT_IN_LINE, "LINE", BT_FN_INT, ATTR_NOTHROW_LEAF_LIST)
 /* Sanitizer builtins. */
 #include "sanitizer.def"
 
+/* Coroutine builtins.  */
+#include "coroutine-builtins.def"
+
 /* Do not expose the BRIG builtins by default gcc-wide, but only privately in
    the BRIG FE as long as there are no references for them in the middle end
    or any of the upstream backends.  */
diff --git a/gcc/coroutine-builtins.def b/gcc/coroutine-builtins.def
new file mode 100644
index 0000000000..3839cb54fa
--- /dev/null
+++ b/gcc/coroutine-builtins.def
@@ -0,0 +1,53 @@ 
+/* This file contains the definitions and documentation for the
+   coroutines builtins used in GCC.
+
+   Copyright (C) 2018-2020 Free Software Foundation, Inc.
+
+ Contributed by Iain Sandoe <iain@sandoe.co.uk> under contract to Facebook.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+/* Before including this file, you should define a macro:
+
+     DEF_BUILTIN_STUB(ENUM, NAME)
+     DEF_COROUTINE_BUILTIN (ENUM, NAME, TYPE, ATTRS)
+
+   See builtins.def for details.
+   The builtins are created used by library implementations of C++
+   coroutines.  */
+
+/* This has to come before all the coroutine builtins.  */
+DEF_BUILTIN_STUB (BEGIN_COROUTINE_BUILTINS, (const char *) 0)
+
+/* These are the builtins that are externally-visible and used by the
+   standard library implementation of the coroutine header.  */
+
+DEF_COROUTINE_BUILTIN (BUILT_IN_CORO_PROMISE, "promise",
+		       BT_FN_PTR_PTR_CONST_SIZE_BOOL,
+		       ATTR_CONST_NOTHROW_LEAF_LIST)
+
+DEF_COROUTINE_BUILTIN (BUILT_IN_CORO_RESUME, "resume", BT_FN_VOID_PTR,
+		       ATTR_NULL)
+
+DEF_COROUTINE_BUILTIN (BUILT_IN_CORO_DESTROY, "destroy", BT_FN_VOID_PTR,
+		       ATTR_NULL)
+
+DEF_COROUTINE_BUILTIN (BUILT_IN_CORO_DONE, "done", BT_FN_BOOL_PTR,
+		       ATTR_NOTHROW_LEAF_LIST)
+
+/* This has to come after all the coroutine builtins.  */
+DEF_BUILTIN_STUB (END_COROUTINE_BUILTINS, (const char *) 0)
diff --git a/gcc/internal-fn.c b/gcc/internal-fn.c
index ceac2df9ce..52d1638917 100644
--- a/gcc/internal-fn.c
+++ b/gcc/internal-fn.c
@@ -2884,6 +2884,32 @@  expand_NOP (internal_fn, gcall *)
   /* Nothing.  But it shouldn't really prevail.  */
 }
 
+/* Coroutines, all should have been processed at this stage.  */
+
+static void
+expand_CO_FRAME (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_YIELD (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_SUSPN (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
+static void
+expand_CO_ACTOR (internal_fn, gcall *)
+{
+  gcc_unreachable ();
+}
+
 /* Expand a call to FN using the operands in STMT.  FN has a single
    output operand and NARGS input operands.  */
 
diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def
index ed11b93b67..1d190d492f 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -366,6 +366,12 @@  DEF_INTERNAL_FN (LAUNDER, ECF_LEAF | ECF_NOTHROW | ECF_NOVOPS, NULL)
 /* Divmod function.  */
 DEF_INTERNAL_FN (DIVMOD, ECF_CONST | ECF_LEAF, NULL)
 
+/* For coroutines.  */
+DEF_INTERNAL_FN (CO_ACTOR, ECF_NOTHROW | ECF_LEAF, NULL)
+DEF_INTERNAL_FN (CO_YIELD, ECF_NOTHROW, NULL)
+DEF_INTERNAL_FN (CO_SUSPN, ECF_NOTHROW, NULL)
+DEF_INTERNAL_FN (CO_FRAME, ECF_PURE | ECF_NOTHROW | ECF_LEAF, NULL)
+
 /* A NOP function with arbitrary arguments and return value.  */
 DEF_INTERNAL_FN (NOP, ECF_CONST | ECF_LEAF | ECF_NOTHROW, NULL)