public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94288] New: co_await in while loop crashes g++
@ 2020-03-23 16:59 chbugzilla at outlook dot com
  2020-03-23 17:08 ` [Bug c++/94288] " marxin at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: chbugzilla at outlook dot com @ 2020-03-23 16:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94288
           Summary: co_await in while loop crashes g++
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chbugzilla at outlook dot com
  Target Milestone: ---

Created attachment 48095
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48095&action=edit
Pre-processed sample code

I am using a recent development build of g++-10 to test some coroutine code
that I am able to run successfully using MSVC but that leads to a compiler
crash with g++.

I attached a small sample to reproduce the issue. I am not sure what's the root
cause but I am able to workaround the problem by pulling the co_await out of
the while loop condition. Replacing std::vector<int> with e.g. int and testing
for zero in the while loop also crashes the compiler, albeit with a different
error.

Build command:
--------------

g++-10 -fcoroutines -std=c++20 -pthread sample.cpp


Output of build:
----------------

sample.cpp: In function ‘void _Z3barv.actor(bar()::_Z3barv.frame*)’:
sample.cpp:72:5: internal compiler error: in fold_convert_loc, at
fold-const.c:2435
   72 |     while ((co_await foo()).empty())
      |     ^~~~~
0x5d60f2 fold_convert_loc(unsigned int, tree_node*, tree_node*)
        ../../src/gcc/fold-const.c:2435
0x9d6a1a gimplify_cond_expr
        ../../src/gcc/gimplify.c:4149
0x9ce9e2 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:13559
0x9cf0fd gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823
0x9cf0fd gimplify_statement_list
        ../../src/gcc/gimplify.c:1869
0x9cf0fd gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:14046
0x9d1f59 gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823
0x9d1f59 gimplify_and_add(tree_node*, gimple**)
        ../../src/gcc/gimplify.c:486
0x9d1f59 gimplify_loop_expr
        ../../src/gcc/gimplify.c:1843
0x9cf216 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:13807
0x9cf0fd gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823
0x9cf0fd gimplify_statement_list
        ../../src/gcc/gimplify.c:1869
0x9cf0fd gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:14046
0x9cf0fd gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823
0x9cf0fd gimplify_statement_list
        ../../src/gcc/gimplify.c:1869
0x9cf0fd gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:14046
0x9ce267 gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823
0x9ce267 gimplify_and_add(tree_node*, gimple**)
        ../../src/gcc/gimplify.c:486
0x9ce267 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../src/gcc/gimplify.c:13953
0x9cf0fd gimplify_stmt(tree_node**, gimple**)
        ../../src/gcc/gimplify.c:6823


Output of g++-10 -v:
--------------------

Using built-in specs.
COLLECT_GCC=g++-10
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
10-20200321-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs
--enable-languages=c,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-10
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --with-target-system-zlib=auto
--enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-offload-targets=nvptx-none,amdgcn-amdhsa,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.0.1 20200321 (experimental) [master revision
15711e837b2:ab6586cb024:497498c878d48754318e486428e2aa30854020b9] (Ubuntu
10-20200321-1ubuntu1)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
@ 2020-03-23 17:08 ` marxin at gcc dot gnu.org
  2020-03-23 17:21 ` marxin at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-03-23 17:08 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |iains at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
           Priority|P3                          |P1
   Last reconfirmed|                            |2020-03-23
   Target Milestone|---                         |10.0
     Ever confirmed|0                           |1

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Started with introduction of coroutines r10-6063-g49789fd08378e3ff. I'm going
to reduce a test-case.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
  2020-03-23 17:08 ` [Bug c++/94288] " marxin at gcc dot gnu.org
@ 2020-03-23 17:21 ` marxin at gcc dot gnu.org
  2020-03-24 16:33 ` iains at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-03-23 17:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
Reduced test-case:

namespace std {
template <typename _Result> struct coroutine_traits {
  using promise_type = _Result::promise_type;
};
template <typename = void> struct coroutine_handle;
template <> struct coroutine_handle<> {};
template <typename> struct coroutine_handle : coroutine_handle<> {};
struct A {
  void await_ready();
  void await_suspend(coroutine_handle<>);
  void await_resume();
};
} // namespace std
struct B {
  struct C {
    void await_ready();
    template <typename Promise>
    void await_suspend(std::coroutine_handle<Promise>);
    void await_resume();
  };
  std::coroutine_handle<> __trans_tmp_1;
  auto get_return_object() { return __trans_tmp_1; }
  auto initial_suspend() { return std::A(); }
  auto final_suspend() { return C(); }
  void unhandled_exception();
};
struct D {
  using promise_type = B;
  std::coroutine_handle<> _handle;
  void await_ready();
  void await_suspend(std::coroutine_handle<>);
  void await_resume();
};
D foo() { co_await foo(); }

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
  2020-03-23 17:08 ` [Bug c++/94288] " marxin at gcc dot gnu.org
  2020-03-23 17:21 ` marxin at gcc dot gnu.org
@ 2020-03-24 16:33 ` iains at gcc dot gnu.org
  2020-03-24 21:04 ` marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2020-03-24 16:33 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |iains at gcc dot gnu.org

--- Comment #3 from Iain Sandoe <iains at gcc dot gnu.org> ---
thanks for the report.  The reduced testcase at c#2 doesn't fire for me once
pending updates are applied. However, the attached case preprocessed code does;
I think we will be able to make suitable test-cases, once the analysis is done.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
                   ` (2 preceding siblings ...)
  2020-03-24 16:33 ` iains at gcc dot gnu.org
@ 2020-03-24 21:04 ` marxin at gcc dot gnu.org
  2020-03-24 21:08 ` iains at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-03-24 21:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Iain Sandoe from comment #3)
> thanks for the report.  The reduced testcase at c#2 doesn't fire for me once
> pending updates are applied. However, the attached case preprocessed code
> does; I think we will be able to make suitable test-cases, once the analysis
> is done.

If you point me to the pending patches, I can make reduction based on that?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
                   ` (3 preceding siblings ...)
  2020-03-24 21:04 ` marxin at gcc dot gnu.org
@ 2020-03-24 21:08 ` iains at gcc dot gnu.org
  2020-03-25  9:17 ` marxin at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2020-03-24 21:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Martin Liška from comment #4)
> (In reply to Iain Sandoe from comment #3)
> > thanks for the report.  The reduced testcase at c#2 doesn't fire for me once
> > pending updates are applied. However, the attached case preprocessed code
> > does; I think we will be able to make suitable test-cases, once the analysis
> > is done.
> 
> If you point me to the pending patches, I can make reduction based on that?

(the reduction is not urgent, I've an idea about the problem, hopefully have a
few cycles on it tomorrow)

unreviewed:
https://gcc.gnu.org/pipermail/gcc-patches/2020-March/542407.html
review-in-progress:
https://gcc.gnu.org/pipermail/gcc-patches/2020-March/542411.html

(but thanks for the offer!)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
                   ` (4 preceding siblings ...)
  2020-03-24 21:08 ` iains at gcc dot gnu.org
@ 2020-03-25  9:17 ` marxin at gcc dot gnu.org
  2020-04-23 19:50 ` cvs-commit at gcc dot gnu.org
  2020-04-24 12:55 ` iains at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-03-25  9:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Iain Sandoe from comment #5)
> (In reply to Martin Liška from comment #4)
> > (In reply to Iain Sandoe from comment #3)
> > > thanks for the report.  The reduced testcase at c#2 doesn't fire for me once
> > > pending updates are applied. However, the attached case preprocessed code
> > > does; I think we will be able to make suitable test-cases, once the analysis
> > > is done.
> > 
> > If you point me to the pending patches, I can make reduction based on that?
> 
> (the reduction is not urgent, I've an idea about the problem, hopefully have
> a few cycles on it tomorrow)
> 
> unreviewed:
> https://gcc.gnu.org/pipermail/gcc-patches/2020-March/542407.html
> review-in-progress:
> https://gcc.gnu.org/pipermail/gcc-patches/2020-March/542411.html
> 
> (but thanks for the offer!)

I've just applied both patches and I can still confirm that I see the failure:

$ g++ -c pr94288-reduced.cc -fcoroutines  -std=c++20
pr94288-reduced.cc: In function ‘void _Z3foov.actor(foo()::_Z3foov.frame*)’:
pr94288-reduced.cc:34:3: internal compiler error: in fold_convert_loc, at
fold-const.c:2435
   34 | D foo() { co_await foo(); }
      |   ^~~
0x747db2 fold_convert_loc(unsigned int, tree_node*, tree_node*)
        ../../gcc/fold-const.c:2435
0xdecd37 gimple_boolify(tree_node*)
        ../../gcc/gimplify.c:3960
0xdf52c8 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:13632
0xdf2d07 gimplify_modify_expr
        ../../gcc/gimplify.c:5766
0xdf4eb8 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:13602
0xdf8996 gimplify_stmt(tree_node**, gimple**)
        ../../gcc/gimplify.c:6823
0xdf5f39 gimplify_cleanup_point_expr
        ../../gcc/gimplify.c:6565
0xdf5f39 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:13994
0xdfc41d gimplify_cond_expr
        ../../gcc/gimplify.c:4177
0xdf5260 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:13559
0xdf8996 gimplify_stmt(tree_node**, gimple**)
        ../../gcc/gimplify.c:6823
0xdf6ab3 gimplify_statement_list
        ../../gcc/gimplify.c:1869
0xdf6ab3 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:14046
0xdf8996 gimplify_stmt(tree_node**, gimple**)
        ../../gcc/gimplify.c:6823
0xdf6ab3 gimplify_statement_list
        ../../gcc/gimplify.c:1869
0xdf6ab3 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:14046
0xdf8996 gimplify_stmt(tree_node**, gimple**)
        ../../gcc/gimplify.c:6823
0xdf4f1c gimplify_and_add(tree_node*, gimple**)
        ../../gcc/gimplify.c:486
0xdf4f1c gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*),
int)
        ../../gcc/gimplify.c:13953
0xdf8996 gimplify_stmt(tree_node**, gimple**)
        ../../gcc/gimplify.c:6823
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
                   ` (5 preceding siblings ...)
  2020-03-25  9:17 ` marxin at gcc dot gnu.org
@ 2020-04-23 19:50 ` cvs-commit at gcc dot gnu.org
  2020-04-24 12:55 ` iains at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-04-23 19:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

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

commit r10-7923-g3dbc772128e944819b09e21021d4fcd5dc17f2d4
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Thu Apr 23 16:59:00 2020 +0100

    coroutines: Fix handling of conditional statements [PR94288]

    Normally, when we find a statement containing an await expression
    this will be expanded to a statement list implementing the control
    flow implied.  The expansion process successively replaces each
    await expression in a statement with the result of its await_resume().

    In the case of conditional statements (if, while, do, switch) the
    expansion of the condition (or expression in the case of do-while)
    cannot take place 'inline', leading to the PR.

    The solution is to evaluate the expression separately, and to
    transform while and do-while loops into endless loops with a break
    on the required condition.

    In fixing this, I realised that I'd also made a thinko in the case
    of expanding truth-and/or-if expressions, where one arm of the
    expression might need to be short-circuited.  The mechanism for
    expanding via the tree walk will not work correctly in this case and
    we need to pre-expand any truth-and/or-if with an await expression
    on its conditionally-taken arm.  This applies to any statement with
    truth-and/or-if expressions, so can be handled generically.

    gcc/cp/ChangeLog:

    2020-04-23  Iain Sandoe  <iain@sandoe.co.uk>

            PR c++/94288
            * coroutines.cc (await_statement_expander): Simplify cases.
            (struct susp_frame_data): Add fields for truth and/or if
            cases, rename one field.
            (analyze_expression_awaits): New.
            (expand_one_truth_if): New.
            (add_var_to_bind): New helper.
            (coro_build_add_if_not_cond_break): New helper.
            (await_statement_walker): Handle conditional expressions,
            handle expansion of truth-and/or-if cases.
            (bind_expr_find_in_subtree): New, checking-only.
            (coro_body_contains_bind_expr_p): New, checking-only.
            (morph_fn_to_coro): Ensure that we have a top level bind
            expression.

    gcc/testsuite/ChangeLog:

    2020-04-23  Iain Sandoe  <iain@sandoe.co.uk>

            PR c++/94288
            * g++.dg/coroutines/torture/co-await-18-if-cond.C: New test.
            * g++.dg/coroutines/torture/co-await-19-while-cond.C: New test.
            * g++.dg/coroutines/torture/co-await-20-do-while-cond.C: New test.
            * g++.dg/coroutines/torture/co-await-21-switch-value.C: New test.
            * g++.dg/coroutines/torture/co-await-22-truth-and-of-if.C: New
test.
            * g++.dg/coroutines/torture/co-ret-16-simple-control-flow.C: New
test.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/94288] co_await in while loop crashes g++
  2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
                   ` (6 preceding siblings ...)
  2020-04-23 19:50 ` cvs-commit at gcc dot gnu.org
@ 2020-04-24 12:55 ` iains at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2020-04-24 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Iain Sandoe <iains at gcc dot gnu.org> ---
A test case from the original post was committed as
r10-7944-g1d072f3eeac4 (I missed the PR number from the commit)
so fixed.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-24 12:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-23 16:59 [Bug c++/94288] New: co_await in while loop crashes g++ chbugzilla at outlook dot com
2020-03-23 17:08 ` [Bug c++/94288] " marxin at gcc dot gnu.org
2020-03-23 17:21 ` marxin at gcc dot gnu.org
2020-03-24 16:33 ` iains at gcc dot gnu.org
2020-03-24 21:04 ` marxin at gcc dot gnu.org
2020-03-24 21:08 ` iains at gcc dot gnu.org
2020-03-25  9:17 ` marxin at gcc dot gnu.org
2020-04-23 19:50 ` cvs-commit at gcc dot gnu.org
2020-04-24 12:55 ` iains at gcc dot gnu.org

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).