Bug 105426 - [12/13 Regression] [wrong-code][regression][coroutines] range-for temporaries are not persisted in coroutines
Summary: [12/13 Regression] [wrong-code][regression][coroutines] range-for temporaries...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: 12.0
Assignee: Iain Sandoe
URL:
Keywords: c++-coroutines, wrong-code
Depends on:
Blocks:
 
Reported: 2022-04-28 15:56 UTC by Avi Kivity
Modified: 2022-10-31 20:09 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 11.3.0
Known to fail: 12.0
Last reconfirmed: 2022-04-28 00:00:00


Attachments
reproducer (491 bytes, text/x-csrc)
2022-04-28 15:56 UTC, Avi Kivity
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Avi Kivity 2022-04-28 15:56:14 UTC
The attached reproducer, compiled with:


    g++ --std=c++20 coroutine-unpersisted-range-for-temporary.cc -g -O3


generates correct results for gcc before 15a176a833f and for clang, but incorrect results in 15a176a833f and later.
Comment 1 Avi Kivity 2022-04-28 15:56:55 UTC
Created attachment 52898 [details]
reproducer
Comment 2 Iain Sandoe 2022-04-28 18:55:15 UTC
So the fix for PR105287 was too ambitious - and reveals that we are missing to name some variables that should be promoted.

Rather than delay for a second fix, the proposal would be a partial reversion of r12-8308-g15a176a833f23e, like so (which would be more suitable for the branch).  This does fix the new issue (and still fixes the analyser one) in my local testing.


diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index 551ddc9cc41..2e393b2cddc 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -3973,6 +3973,9 @@ register_local_var_uses (tree *stmt, int *do_subtree, void *d)
          else if (lvname != NULL_TREE)
            buf = xasprintf ("%s_%u_%u", IDENTIFIER_POINTER (lvname),
                             lvd->nest_depth, lvd->bind_indx);
+         else
+           buf = xasprintf ("_D%u_%u_%u", DECL_UID (lvar), lvd->nest_depth,
+                            lvd->bind_indx);
          /* TODO: Figure out if we should build a local type that has any
             excess alignment or size from the original decl.  */
          if (buf)
Comment 3 Richard Biener 2022-04-29 06:36:18 UTC
possibly combining IDENTIFIER_POINTER with DECL_UID might be easier for debugging things later
Comment 4 GCC Commits 2022-04-29 06:36:55 UTC
The master branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:3d8d093e820b10a4b4b2af8949a368377c0888cb

commit r13-28-g3d8d093e820b10a4b4b2af8949a368377c0888cb
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Thu Apr 28 20:06:29 2022 +0100

    c++, coroutines: Partial reversion of r12-8308-g15a176a833f23e [PR105426].
    
    The changes to fix PR 105287 included a tightening of the constraints on which
    variables are promoted to frame copies.  This has exposed that we are failing
    to name some variables that should be promoted.
    
    We avoid the use of DECL_UID to build anonymous symbols since that might not
    be stable for -fcompare-debug.
    
    The long-term fix is to address the cases where the naming has been missed,
    but for the short-term (and for the GCC-12 branch) backing out the additional
    constraint is proposed.
    
    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
    
            PR c++/105426
    
    gcc/cp/ChangeLog:
    
            * coroutines.cc (register_local_var_uses): Allow promotion of unnamed
            temporaries to coroutine frame copies.
Comment 5 Iain Sandoe 2022-04-29 06:40:08 UTC
(In reply to Richard Biener from comment #3)
> possibly combining IDENTIFIER_POINTER with DECL_UID might be easier for
> debugging things later

it does make debugging easier (one reason I was using decl_uid) but Jakub points out that this might not be stable for -fcompare-debug, so we've used a sequence number.
Comment 6 GCC Commits 2022-04-29 08:29:54 UTC
The releases/gcc-12 branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:7cc5a20ba3f05a783fb75762cfb77ccb571285ab

commit r12-8319-g7cc5a20ba3f05a783fb75762cfb77ccb571285ab
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Thu Apr 28 20:06:29 2022 +0100

    c++, coroutines: Partial reversion of r12-8308-g15a176a833f23e [PR105426].
    
    The changes to fix PR 105287 included a tightening of the constraints on which
    variables are promoted to frame copies.  This has exposed that we are failing
    to name some variables that should be promoted.
    
    We avoid the use of DECL_UID to build anonymous symbols since that might not
    be stable for -fcompare-debug.
    
    The long-term fix is to address the cases where the naming has been missed,
    but for the short-term (and for the GCC-12 branch) backing out the additional
    constraint is proposed.
    
    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>
    
            PR c++/105426
    
    gcc/cp/ChangeLog:
    
            * coroutines.cc (register_local_var_uses): Allow promotion of unnamed
            temporaries to coroutine frame copies.
    
    (cherry picked from commit 3d8d093e820b10a4b4b2af8949a368377c0888cb)
Comment 7 Iain Sandoe 2022-04-29 08:30:35 UTC
so fixed on gcc-12 and 13