On 9/15/21 14:32, Iain Sandoe wrote:
Hi Jason,

On 15 Sep 2021, at 18:32, Jason Merrill <ja...@redhat.com> wrote:

On 9/14/21 11:36, Iain Sandoe wrote:
Hi
Some small code cleanups that allow us to have just one place that
we handle a statement with await expression(s) embedded.  Also we
can reduce the work done to figure out whether a statement contains
any such expressions.
tested on x86_64,powerpc64le-linux x86_64-darwin
OK for master?
thanks
Iain
-----
There is no need to make a MODIFY_EXPR for any of the condition
vars that we synthesize.
Expansion of co_return can be carried out independently of any
co_awaits that might be contained which simplifies this.
Where we are rewriting statements to handle await expression
logic, there is no need to carry out any analysis - we just need
to detect the presence of any co_await.
Signed-off-by: Iain Sandoe <i...@sandoe.co.uk>
gcc/cp/ChangeLog:
        * coroutines.cc (await_statement_walker): Code cleanups.
---
  gcc/cp/coroutines.cc | 121 ++++++++++++++++++++-----------------------
  1 file changed, 56 insertions(+), 65 deletions(-)
diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index d2cc2e73c89..27556723b71 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -3412,16 +3412,11 @@ await_statement_walker (tree *stmt, int *do_subtree, 
void *d)
        return NULL_TREE;
      }
  -  /* We have something to be handled as a single statement.  */
-  bool has_cleanup_wrapper = TREE_CODE (*stmt) == CLEANUP_POINT_EXPR;
-  hash_set<tree> visited;
-  awpts->saw_awaits = 0;
-  hash_set<tree> truth_aoif_to_expand;
-  awpts->truth_aoif_to_expand = &truth_aoif_to_expand;
-  awpts->needs_truth_if_exp = false;
-  awpts->has_awaiter_init = false;
+  /* We have something to be handled as a single statement.  We have to handle
+     a few statements specially where await statements have to be moved out of
+     constructs.  */
    tree expr = *stmt;
-  if (has_cleanup_wrapper)
+  if (TREE_CODE (*stmt) == CLEANUP_POINT_EXPR)
      expr = TREE_OPERAND (expr, 0);
    STRIP_NOPS (expr);
  @@ -3437,6 +3432,8 @@ await_statement_walker (tree *stmt, int *do_subtree, 
void *d)
           transforms can be implemented.  */
        case IF_STMT:
          {
+           tree *await_ptr;
+           hash_set<tree> visited;
            /* Transform 'if (cond with awaits) then stmt1 else stmt2' into
               bool cond = cond with awaits.
               if (cond) then stmt1 else stmt2.  */
@@ -3444,10 +3441,8 @@ await_statement_walker (tree *stmt, int *do_subtree, 
void *d)
            /* We treat the condition as if it was a stand-alone statement,
               to see if there are any await expressions which will be analyzed
               and registered.  */
-           if ((res = cp_walk_tree (&IF_COND (if_stmt),
-               analyze_expression_awaits, d, &visited)))
-             return res;
-           if (!awpts->saw_awaits)
+           if (!(cp_walk_tree (&IF_COND (if_stmt),
+                 find_any_await, &await_ptr, &visited)))
              return NULL_TREE; /* Nothing special to do here.  */
            gcc_checking_assert (!awpts->bind_stack->is_empty());
@@ -3463,7 +3458,7 @@ await_statement_walker (tree *stmt, int *do_subtree, void 
*d)
            /* We want to initialize the new variable with the expression
               that contains the await(s) and potentially also needs to
               have truth_if expressions expanded.  */
-           tree new_s = build2_loc (sloc, MODIFY_EXPR, boolean_type_node,
+           tree new_s = build2_loc (sloc, INIT_EXPR, boolean_type_node,
                                     newvar, cond_inner);
            finish_expr_stmt (new_s);
            IF_COND (if_stmt) = newvar;
@@ -3477,25 +3472,25 @@ await_statement_walker (tree *stmt, int *do_subtree, 
void *d)
          break;
        case FOR_STMT:
          {
+           tree *await_ptr;
+           hash_set<tree> visited;
            /* for loops only need special treatment if the condition or the
               iteration expression contain a co_await.  */
            tree for_stmt = *stmt;
            /* Sanity check.  */
-           if ((res = cp_walk_tree (&FOR_INIT_STMT (for_stmt),
-               analyze_expression_awaits, d, &visited)))
-             return res;
-           gcc_checking_assert (!awpts->saw_awaits);
-
-           if ((res = cp_walk_tree (&FOR_COND (for_stmt),
-               analyze_expression_awaits, d, &visited)))
-             return res;
-           bool for_cond_await = awpts->saw_awaits != 0;
-           unsigned save_awaits = awpts->saw_awaits;
-
-           if ((res = cp_walk_tree (&FOR_EXPR (for_stmt),
-               analyze_expression_awaits, d, &visited)))
-             return res;
-           bool for_expr_await = awpts->saw_awaits > save_awaits;
+           gcc_checking_assert
+             (!(cp_walk_tree (&FOR_INIT_STMT (for_stmt), find_any_await,
+                              &await_ptr, &visited)));

What's the rationale for this assert?  [expr.await] seems to say explicitly 
that an await can appear in the initializer of an init-statement.

Indeed (and we would not expect otherwise)
  - but currently GCC appears to generate code for:

     for (loop_ind_var = init; … ; …) {}

   that looks like:

   loop_ind_var = init;
   for (; … ; …) {}

If that changes (and the init contains an await expr) then we’d need to apply 
that transform manually, so the assert is in place to check that the assumption 
about existing behaviour is met.

Then the patch is OK with that rationale in a comment.

Jason

Reply via email to