public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "michal.jankovic59 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105989] Coroutine frame space for temporaries in a co_await expression is not reused
Date: Wed, 15 Jun 2022 17:20:23 +0000	[thread overview]
Message-ID: <bug-105989-4-KbLqxl7WM1@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105989-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105989

--- Comment #2 from Michal Jankovič <michal.jankovic59 at gmail dot com> ---
Reading through gcc/cp/coroutines.cc, it seems like the coroutine frame is
indeed composed as a flat struct with fields for all the local variables,
temps, and special stuff needed by the actor. 

Relevant code:
coro_make_frame_entry - adds a field to the coro_frame_type structure.
register_local_var_uses - here coro_make_frame_entry is called for each local
variable.
morph_fn_to_coro - here the coro_frame_type structure definition is created
from the collected fields.

Comments in the code state that the mid-end should be able to optimize out
unused fields from this struct, which currently does not happen, but even if it
did, this does not solve the overlapping of variables.

I think that instead of a flat struct, the frontend should generate a structure
recursively composed of unions of structures, having a union for each disjunct
scope (including an implicit scope for expression temporaries).

Example:

task task_1() {
  {
    char a[128];
    co_await task_2();
  }
  {
    char b[128];
    co_await task_3(get_temporary());

    static awaiter awaiter_not_stored_in_frame;
    co_await awaiter_not_stored_in_frame;
  }
}

Currently, this generates a coro frame struct that looks something like this:
struct coro_frame {
  // builtin state ...
  char a[128];
  awaiter task_2_awaiter;
  char b[128];
  int get_temporary_result;
  awaiter task_3_awaiter;  
  awaiter* awaiter_not_stored_in_frame;
};

Instead, the frontend could generate something like this:
struct coro_frame {
  // builtin state ...
  union {
    struct {
      char a[128];
      awaiter task_2_awaiter;
    };
    struct {
      char b[128];
      union {
        struct { 
          int get_temporary_result;
          awaiter task_3_awaiter; 
        };
        struct { awaiter* awaiter_not_stored_in_frame; };
      };
    };
  };
};

  parent reply	other threads:[~2022-06-15 17:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15  9:03 [Bug c++/105989] New: " michal.jankovic59 at gmail dot com
2022-06-15  9:49 ` [Bug c++/105989] " michal.jankovic59 at gmail dot com
2022-06-15 17:20 ` michal.jankovic59 at gmail dot com [this message]
2022-06-21  1:25 ` pinskia at gcc dot gnu.org
2022-07-07 15:36 ` michal.jankovic59 at gmail dot com
2022-07-12 13:39 ` michal.jankovic59 at gmail dot com
2022-07-12 13:41 ` michal.jankovic59 at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105989-4-KbLqxl7WM1@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).