Bug 60228 - ICE using lambda in #pragma omp declare reduction
Summary: ICE using lambda in #pragma omp declare reduction
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 8.4
Assignee: Jakub Jelinek
URL:
Keywords: c++-lambda, ice-on-valid-code, openmp
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2014-02-16 18:46 UTC by Volker Reichelt
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.0
Last reconfirmed: 2017-10-03 00:00:00


Attachments
gcc10-pr60228.patch (2.47 KB, patch)
2019-11-28 13:13 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2014-02-16 18:46:56 UTC
The following valid code snippet (compiled with "-std=c++11 -fopenmp")
triggers an ICE on trunk:

=======================================================
template<typename T> struct A
{
  #pragma omp declare reduction (x : T : omp_out += omp_in + [](){ return 0; }()) initializer (omp_priv = 0)
};
=======================================================

bug.cc:3:108: internal compiler error: in cp_check_omp_declare_reduction, at cp/semantics.c:4944
   #pragma omp declare reduction (x : T : omp_out += omp_in + [](){ return 0; }()) initializer (omp_priv = 0)
                                                                                                            ^
0x7328eb cp_check_omp_declare_reduction(tree_node*)
        ../../gcc/gcc/cp/semantics.c:4944
0x6cfd7c cp_parser_late_parsing_for_member
        ../../gcc/gcc/cp/parser.c:23459
0x6a9edc cp_parser_class_specifier_1
        ../../gcc/gcc/cp/parser.c:19406
0x6a9edc cp_parser_class_specifier
        ../../gcc/gcc/cp/parser.c:19437
0x6a9edc cp_parser_type_specifier
        ../../gcc/gcc/cp/parser.c:14302
0x6c2ba0 cp_parser_decl_specifier_seq
        ../../gcc/gcc/cp/parser.c:11547
0x6c8883 cp_parser_single_declaration
        ../../gcc/gcc/cp/parser.c:23039
0x6c8d64 cp_parser_template_declaration_after_export
        ../../gcc/gcc/cp/parser.c:22915
0x6d4119 cp_parser_declaration
        ../../gcc/gcc/cp/parser.c:10947
0x6d2c08 cp_parser_declaration_seq_opt
        ../../gcc/gcc/cp/parser.c:10869
0x6d44aa cp_parser_translation_unit
        ../../gcc/gcc/cp/parser.c:4014
0x6d44aa c_parse_file()
        ../../gcc/gcc/cp/parser.c:31536
0x7f3813 c_common_parse_file()
        ../../gcc/gcc/c-family/c-opts.c:1060
Please submit a full bug report, [etc.]
Comment 1 Jakub Jelinek 2014-08-11 10:16:19 UTC
I would not call this valid, because OpenMP 4.0 doesn't support C++11, thus using C++11 constructs inside of OpenMP constructs is invalid.
Comment 2 Paolo Carlini 2017-10-02 09:50:25 UTC
Would it make sense to reject the testcase a little more gracefully? Or even accept is as an extension?
Comment 3 Paolo Carlini 2018-03-03 22:08:09 UTC
Something I didn't really pay attention to before: the ICE only happens in a template, otherwise the lambda is already accepted as an extension. There must be a way to fix the problem without explicitly handling C++11 constructs...
Comment 4 Steffen Seckler 2019-11-25 12:50:32 UTC
I would also like to see this supported, as the intel compiler is already able to support this.
Comment 5 Steffen Seckler 2019-11-27 10:35:02 UTC
(In reply to Steffen Seckler from comment #4)
> I would also like to see this supported, as the intel compiler is already
> able to support this.

fyi: the clang compiler will support lambdas inside of
#pragma omp declare reduction
as of a913e872d6e7044ae77e55c45ab3ea5304eb7262, which will most likely be in the next release or in 9.0.1
see:
https://bugs.llvm.org/show_bug.cgi?id=44133
https://bugs.llvm.org/show_bug.cgi?id=44134
Comment 6 Jakub Jelinek 2019-11-28 13:13:15 UTC
Created attachment 47389 [details]
gcc10-pr60228.patch

Untested fix.
Comment 7 Jakub Jelinek 2019-11-29 09:09:22 UTC
Author: jakub
Date: Fri Nov 29 09:08:46 2019
New Revision: 278831

URL: https://gcc.gnu.org/viewcvs?rev=278831&root=gcc&view=rev
Log:
	PR c++/60228
	* parser.c (cp_parser_omp_declare_reduction_exprs): If
	processing_template_decl, wrap the combiner or initializer
	into EXPR_STMT.
	* decl.c (start_preparsed_function): Don't start a lambda scope
	for DECL_OMP_DECLARE_REDUCTION_P functions.
	(finish_function): Don't finish a lambda scope for
	DECL_OMP_DECLARE_REDUCTION_P functions, nor cp_fold_function
	them nor cp_genericize them.
	* mangle.c (decl_mangling_context): Look through
	DECL_OMP_DECLARE_REDUCTION_P functions.
	* semantics.c (expand_or_defer_fn_1): For DECL_OMP_DECLARE_REDUCTION_P
	functions, use tentative linkage, don't keep their bodies with
	-fkeep-inline-functions and return false at the end.

	* g++.dg/gomp/openmp-simd-2.C: Don't expect bodies for
	DECL_OMP_DECLARE_REDUCTION_P functions.

	* testsuite/libgomp.c++/udr-20.C: New test.
	* testsuite/libgomp.c++/udr-21.C: New test.

Added:
    trunk/libgomp/testsuite/libgomp.c++/udr-20.C
    trunk/libgomp/testsuite/libgomp.c++/udr-21.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/mangle.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/gomp/openmp-simd-2.C
    trunk/libgomp/ChangeLog
Comment 8 Jakub Jelinek 2019-11-29 09:11:15 UTC
Author: jakub
Date: Fri Nov 29 09:10:44 2019
New Revision: 278832

URL: https://gcc.gnu.org/viewcvs?rev=278832&root=gcc&view=rev
Log:
	PR c++/60228
	* parser.c (cp_parser_omp_declare_reduction_exprs): If
	processing_template_decl, wrap the combiner or initializer
	into EXPR_STMT.
	* decl.c (start_preparsed_function): Don't start a lambda scope
	for DECL_OMP_DECLARE_REDUCTION_P functions.
	(finish_function): Don't finish a lambda scope for
	DECL_OMP_DECLARE_REDUCTION_P functions, nor cp_fold_function
	them nor cp_genericize them.
	* mangle.c (decl_mangling_context): Look through
	DECL_OMP_DECLARE_REDUCTION_P functions.
	* semantics.c (expand_or_defer_fn_1): For DECL_OMP_DECLARE_REDUCTION_P
	functions, use tentative linkage, don't keep their bodies with
	-fkeep-inline-functions and return false at the end.

	* g++.dg/gomp/openmp-simd-2.C: Don't expect bodies for
	DECL_OMP_DECLARE_REDUCTION_P functions.

	* testsuite/libgomp.c++/udr-20.C: New test.
	* testsuite/libgomp.c++/udr-21.C: New test.

Modified:
    trunk/libgomp/testsuite/libgomp.c++/udr-20.C
    trunk/libgomp/testsuite/libgomp.c++/udr-21.C
Comment 9 Jakub Jelinek 2019-12-20 17:31:10 UTC
Author: jakub
Date: Fri Dec 20 17:30:38 2019
New Revision: 279662

URL: https://gcc.gnu.org/viewcvs?rev=279662&root=gcc&view=rev
Log:
	Backported from mainline
	2019-11-29  Jakub Jelinek  <jakub@redhat.com>

	PR c++/60228
	* parser.c (cp_parser_omp_declare_reduction_exprs): If
	processing_template_decl, wrap the combiner or initializer
	into EXPR_STMT.
	* decl.c (start_preparsed_function): Don't start a lambda scope
	for DECL_OMP_DECLARE_REDUCTION_P functions.
	(finish_function): Don't finish a lambda scope for
	DECL_OMP_DECLARE_REDUCTION_P functions, nor cp_fold_function
	them nor cp_genericize them.
	* mangle.c (decl_mangling_context): Look through
	DECL_OMP_DECLARE_REDUCTION_P functions.
	* semantics.c (expand_or_defer_fn_1): For DECL_OMP_DECLARE_REDUCTION_P
	functions, use tentative linkage, don't keep their bodies with
	-fkeep-inline-functions and return false at the end.

	* g++.dg/gomp/openmp-simd-2.C: Don't expect bodies for
	DECL_OMP_DECLARE_REDUCTION_P functions.

	* testsuite/libgomp.c++/udr-20.C: New test.
	* testsuite/libgomp.c++/udr-21.C: New test.

Added:
    branches/gcc-9-branch/libgomp/testsuite/libgomp.c++/udr-20.C
    branches/gcc-9-branch/libgomp/testsuite/libgomp.c++/udr-21.C
Modified:
    branches/gcc-9-branch/gcc/cp/ChangeLog
    branches/gcc-9-branch/gcc/cp/decl.c
    branches/gcc-9-branch/gcc/cp/mangle.c
    branches/gcc-9-branch/gcc/cp/parser.c
    branches/gcc-9-branch/gcc/cp/semantics.c
    branches/gcc-9-branch/gcc/testsuite/ChangeLog
    branches/gcc-9-branch/gcc/testsuite/g++.dg/gomp/openmp-simd-2.C
    branches/gcc-9-branch/libgomp/ChangeLog
Comment 10 GCC Commits 2020-02-14 16:35:14 UTC
The releases/gcc-8 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:1aafdd7c93226cb092b560462028394a82f404a2

commit r8-9990-g1aafdd7c93226cb092b560462028394a82f404a2
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Feb 14 12:56:42 2020 +0100

    backport: re PR c++/60228 (ICE using lambda in #pragma omp declare reduction)
    
    	Backported from mainline
    	2019-11-29  Jakub Jelinek  <jakub@redhat.com>
    
    	PR c++/60228
    	* parser.c (cp_parser_omp_declare_reduction_exprs): If
    	processing_template_decl, wrap the combiner or initializer
    	into EXPR_STMT.
    	* decl.c (start_preparsed_function): Don't start a lambda scope
    	for DECL_OMP_DECLARE_REDUCTION_P functions.
    	(finish_function): Don't finish a lambda scope for
    	DECL_OMP_DECLARE_REDUCTION_P functions, nor cp_fold_function
    	them nor cp_genericize them.
    	* mangle.c (decl_mangling_context): Look through
    	DECL_OMP_DECLARE_REDUCTION_P functions.
    	* semantics.c (expand_or_defer_fn_1): For DECL_OMP_DECLARE_REDUCTION_P
    	functions, use tentative linkage, don't keep their bodies with
    	-fkeep-inline-functions and return false at the end.
    
    	* g++.dg/gomp/openmp-simd-2.C: Don't expect bodies for
    	DECL_OMP_DECLARE_REDUCTION_P functions.
    
    	* testsuite/libgomp.c++/udr-20.C: New test.
    	* testsuite/libgomp.c++/udr-21.C: New test.
Comment 11 Jakub Jelinek 2020-02-14 17:25:04 UTC
Fixed for 8.4+ and 9.3+.