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

            Bug ID: 105989
           Summary: Coroutine frame space for temporaries in a co_await
                    expression is not reused
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: michal.jankovic59 at gmail dot com
  Target Milestone: ---

Created attachment 53142
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53142&action=edit
Minimal reproduction of the described issue.

Space in a coroutine frame that is allocated for temporaries in a co_await
expression (in particular the awaiter object) is not reused for subsequent
co_await-s. Writing the same co_await expression multiple times results in a
larger coroutine frame than using a for loop, where the space is only allocated
once. This is a bummer for me, as I would like to use coroutines for embedded
systems with limited memory, and every byte counts.

I would expect that the space in the coroutine frame for the temporaries of two
subsequent co_await expressions would overlap. This appears to work on Clang
(allthough it has a similar issue with HALO, where the space for the subroutine
frames do not overlap).

The attached minimal example shows the issue by logging allocations of the
coroutine frames; change the number of co_await expressions in coro_1() and the
size of the dummy array in the awaiter object to see that the space for the
awaiter is not reclaimed. Change it to 1 co_await in a for loop, and the
temporaries are only allocated once.

Compilation command (the issue is on all optimization levels, not just -O3):
g++ -std=c++20 -fcoroutines -O3 --save-temps report.cpp

gcc -v output:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-bootstrap
--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-linker-build-id --enable-lto
--enable-multilib --enable-plugin --enable-shared --enable-threads=posix
--disable-libssp --disable-libstdcxx-pch --disable-werror
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.0 (GCC)

Reply via email to