From patchwork Mon Aug 12 23:21:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1971710 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hGHUvjfm; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WjVs65l1bz1yXl for ; Tue, 13 Aug 2024 09:21:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9B0BF3858C60 for ; Mon, 12 Aug 2024 23:21:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 87C833858D20 for ; Mon, 12 Aug 2024 23:21:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 87C833858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 87C833858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723504879; cv=none; b=TSFe/QcxpP0j4NwHc5Dp1OGxDD3mT6AdhBqQbmAQHX0Sb9KzOi3vP/CxojDY89PI3LOGv2vQ7CnxoCZ51aiZaBwA7R/ituZ8KrSISpSvBDTxVri+IL4/RPJWJ3mnHVlqFfFoy1XZBAoknfWJQrwWFAgrnvo3gPLdyc9jhXMYM08= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723504879; c=relaxed/simple; bh=Q6ENXwFMfit936Ye2SoM3OZjwfvZVz42WmmgXyIX29Q=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=bSizqXjjeNlp/1hCYWRlJ+wAjC2BxJtATcqO3zuA/Wmexmz1Z8mVNHJ5Vmp2/0b3uMGYCew85ziyaPNR44LNvya26DJRzj8rU6PX2QbH6lhNtBKgl65/qia1rKmKC2wKd4ObXVI3YOR+eruas98L1k7YiGJRmrBBUYXJLCG8Ry4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723504876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RRDhk3KMRFY6uWyfgr55QfoiEl5hByJswF+NCZWjstw=; b=hGHUvjfmmnFq28Tx57JlRKQmbdzK7YCzAkVuvmX00C/Zoex21amL5N7GcKX+ClH3zduDtF LqdFIv19nm+z2OvJo9RTFtmNqhxX5aervbZUveChqTtbXmZi/AewJbSBF60tyvC/dBYxcy xTwyJIhobKy18PDo6F0ajRBf8OIJbDY= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-544-uI5IWIK4OniEHwZKor4Nug-1; Mon, 12 Aug 2024 19:21:14 -0400 X-MC-Unique: uI5IWIK4OniEHwZKor4Nug-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7a1d4335cceso902096685a.1 for ; Mon, 12 Aug 2024 16:21:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723504874; x=1724109674; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RRDhk3KMRFY6uWyfgr55QfoiEl5hByJswF+NCZWjstw=; b=Qv4rvHtYo6HbJvgsS/hmlWrYLSZOTnbOCkLgeH0iylX82UUP7+mLqEPfGfhdSBOMZw maDfyJnt51o8M20RXZqm3soCUoGXgNb0CR3cvuepmnCrF1dIkRypnE2MGLs77c0xLMGO bzZVfUWsg2i+DUeBpqQdROR5gj9h6t0x+Cd5wo46uu3+XjeGgFBh2y2zh8MNaJImZSbZ tR2+3thSAHhiP/m7SnHL2IR6tf2mGpeg5lmxh0j+bAnaNCYK1coBiY4dGuwlsgBlFt+7 Ms7rv4TwwUXHB3RL96g/8yn8jX8+NEGl+x4alrgJdU5GUEAyg4wyRWmwkdkDTgi+pHZT 9NDg== X-Forwarded-Encrypted: i=1; AJvYcCUPut0PRF33wv74r96v1v98NsjsjCWuraOJEmenN8QGpESh8tlCMcv6p51m/UJbJypqbX55MXhWe5SDu3fgkvseeyFC+jf7Dg== X-Gm-Message-State: AOJu0Yw9ww9cQRg7bolmfAOaaGE6wJDa21GkRNgwGrM00MkOV9UIQ0S7 zwXfH/IUWU2W9Ie6zOFdFH83lyp7g9x+TY0B9OLt/OKdqGzNIvpooRFPju047nELuLn9cmncfPA OgUItfAdqgOg4iDjx2AhnIG31osel54vsbzjsSCGqdQqdopw0IaG69Yk= X-Received: by 2002:a05:620a:d82:b0:7a1:da71:e73a with SMTP id af79cd13be357-7a4e37605b0mr220227485a.2.1723504873910; Mon, 12 Aug 2024 16:21:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVIY4tkqP3Spjy6FFtzuMIs1i0D1yXtkou133qYwMXX/UDL4KTN26aHvusNASQ6h0kNnwPcA== X-Received: by 2002:a05:620a:d82:b0:7a1:da71:e73a with SMTP id af79cd13be357-7a4e37605b0mr220223785a.2.1723504873314; Mon, 12 Aug 2024 16:21:13 -0700 (PDT) Received: from redhat.com ([2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a4c7e040e0sm286354785a.101.2024.08.12.16.21.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Aug 2024 16:21:12 -0700 (PDT) Date: Mon, 12 Aug 2024 19:21:11 -0400 From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH v2] c++: ICE with NSDMIs and fn arguments [PR116015] Message-ID: References: <20240808173750.450696-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org On Fri, Aug 09, 2024 at 05:15:05PM -0400, Jason Merrill wrote: > On 8/9/24 4:21 PM, Marek Polacek wrote: > > On Fri, Aug 09, 2024 at 12:58:34PM -0400, Jason Merrill wrote: > > > On 8/8/24 1:37 PM, Marek Polacek wrote: > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > > > > > -- >8 -- > > > > The problem in this PR is that we ended up with > > > > > > > > {.rows=(&)->n, > > > > .outer_stride=(&)->rows} > > > > > > > > that is, two PLACEHOLDER_EXPRs for different types on the same level > > > > in one { }. That should not happen; we may, for instance, neglect to > > > > replace a PLACEHOLDER_EXPR due to CONSTRUCTOR_PLACEHOLDER_BOUNDARY on > > > > the constructor. > > > > > > > > The same problem happened in PR100252, which I fixed by introducing > > > > replace_placeholders_for_class_temp_r. That didn't work here, though, > > > > because r_p_for_c_t_r only works for non-eliding TARGET_EXPRs: replacing > > > > a PLACEHOLDER_EXPR with a temporary that is going to be elided will > > > > result in a crash in gimplify_var_or_parm_decl when it encounters such > > > > a loose decl. > > > > > > > > But leaving the PLACEHOLDER_EXPRs in is also bad because then we end > > > > up with this PR. > > > > > > > > TARGET_EXPRs for function arguments are elided in gimplify_arg. The > > > > argument will get a real temporary only in get_formal_tmp_var. One > > > > idea was to use the temporary that is going to be elided anyway, and > > > > then replace_decl it with the real object once we get it. But that > > > > didn't work out: one problem is that we elide the TARGET_EXPR for an > > > > argument before we create the real temporary for the argument, and > > > > when we get it, the context that this was a TARGET_EXPR for an argument > > > > has been lost. We're also in the middle end territory now, even though > > > > this is a C++-specific problem. > > > > > > How complex! > > > > > > > I figured that since the to-be-elided temporary is going to stay around > > > > until gimplification, the front end is free to use it. Once we're done > > > > with things like store_init_value, which replaces PLACEHOLDER_EXPRs with > > > > the decl it is initializing, we can turn those to-be-elided temporaries > > > > into PLACEHOLDER_EXPRs again, so that cp_gimplify_init_expr can replace > > > > them with the real object once available. The context is not lost so we > > > > do not need an extra flag for these makeshift temporaries. > > > > > > Clever, that makes a lot of sense. But I wonder if we can avoid the problem > > > more simply than working around it? > > > > > > I see that the get_formal_tmp_var happens directly from gimplify_arg, so it > > > strips the TARGET_EXPR to avoid a temporary...and then immediately turns > > > around and creates a new temporary. > > > > > > Would it work to stop stripping the TARGET_EXPR in gimplify_arg and > > > therefore stop setting TARGET_EXPR_ELIDING_P in convert_for_arg_passing? > > > > Well, it does fix the ICE. But then a number of testcases fail :(. > > For instance, pr23372.C. .gimple diff w/ and w/o stripping the TARGET_EXPR: > > > > @@ -1,6 +1,9 @@ > > void g (struct A * a) > > { > > - f (MEM[(const struct A &)a]); > > + struct A D.2829; > > + > > + D.2829 = MEM[(const struct A &)a]; > > + f (D.2829); > > } > > > > The extra copy is there even in .optimized with -O2. > > > > > > It's always sad when we have to add complicated code just to work around > > a corner case, but the above pessimization looks pretty important :(. > > Ah, good point. In that case, the stripping avoids the copy because the > TARGET_EXPR_INITIAL is already (adjustable into) a suitable lvalue. The > current code already fails to avoid the redundant copy when _INITIAL is a > CONSTRUCTOR: > > void g (struct A * a) > { > struct A D.2805; > > D.2805 = {}; // boo > f (D.2805); > } > > I'm failing to find the PR about this issue. I also haven't found it (that doesn't mean it doesn't exist :)). I can file one if you'd like... Note that if we do fix that, we may be facing this problem again. > How about the change I mentioned only in the CONSTRUCTOR case? That works! Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- The problem in this PR is that we ended up with {.rows=(&)->n, .outer_stride=(&)->rows} that is, two PLACEHOLDER_EXPRs for different types on the same level in one { }. That should not happen; we may, for instance, neglect to replace a PLACEHOLDER_EXPR due to CONSTRUCTOR_PLACEHOLDER_BOUNDARY on the constructor. The same problem happened in PR100252, which I fixed by introducing replace_placeholders_for_class_temp_r. That didn't work here, though, because r_p_for_c_t_r only works for non-eliding TARGET_EXPRs: replacing a PLACEHOLDER_EXPR with a temporary that is going to be elided will result in a crash in gimplify_var_or_parm_decl when it encounters such a loose decl. But leaving the PLACEHOLDER_EXPRs in is also bad because then we end up with this PR. TARGET_EXPRs for function arguments are elided in gimplify_arg. The argument will get a real temporary only in get_formal_tmp_var. One idea was to use the temporary that is going to be elided anyway, and then replace_decl it with the real object once we get it. But that didn't work out: one problem is that we elide the TARGET_EXPR for an argument before we create the real temporary for the argument, and when we get it, the context that this was a TARGET_EXPR for an argument has been lost. We're also in the middle end territory now, even though this is a C++-specific problem. A solution is to simply stop eliding TARGET_EXPRs whose initializer is a CONSTRUCTOR. Such copies can't be (at the moment) elided anyway. But not eliding all TARGET_EXPRs would be a pessimization. PR c++/116015 gcc/cp/ChangeLog: * call.cc (convert_for_arg_passing): Don't set_target_expr_eliding when the TARGET_EXPR initializer is a CONSTRUCTOR. gcc/ChangeLog: * gimplify.cc (gimplify_arg): Do not strip a TARGET_EXPR whose initializer is a CONSTRUCTOR. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/nsdmi-aggr23.C: New test. --- gcc/cp/call.cc | 7 ++++-- gcc/gimplify.cc | 14 +++++++++++- gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C | 26 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C base-commit: 0451bc503da9c858e9f1ddfb8faec367c2e032c8 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 94015db4e65..0fe679aae9f 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9468,8 +9468,11 @@ convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain) if (complain & tf_warning) warn_for_address_of_packed_member (type, val); - /* gimplify_arg elides TARGET_EXPRs that initialize a function argument. */ - if (SIMPLE_TARGET_EXPR_P (val)) + /* gimplify_arg elides TARGET_EXPRs that initialize a function argument, + unless the initializer is a CONSTRUCTOR. In that case, we fail to + elide the copy anyway. See that function for more information. */ + if (SIMPLE_TARGET_EXPR_P (val) + && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR) set_target_expr_eliding (val); return val; diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 71cc6c38d80..37d0d3a59f5 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -3760,7 +3760,19 @@ gimplify_arg (tree *arg_p, gimple_seq *pre_p, location_t call_location, { tree init = TARGET_EXPR_INITIAL (*arg_p); if (init - && !VOID_TYPE_P (TREE_TYPE (init))) + && !VOID_TYPE_P (TREE_TYPE (init)) + /* Currently, due to c++/116015, it is not desirable to + strip a TARGET_EXPR whose initializer is a {}. The + problem is that if we do elide it, we also have to + replace all the occurrences of the slot temporary in the + initializer with the temporary created for the argument. + But we do not have that temporary yet so the replacement + would be quite awkward and it might be needed to resort + back to a PLACEHOLDER_EXPR. + + See convert_for_arg_passing for the C++ code that marks + the TARGET_EXPR as eliding or not. */ + && TREE_CODE (init) != CONSTRUCTOR) *arg_p = init; } } diff --git a/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C new file mode 100644 index 00000000000..2f5b8ca97bf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/nsdmi-aggr23.C @@ -0,0 +1,26 @@ +// PR c++/116015 +// { dg-do compile { target c++14 } } +// { dg-additional-options "-Wno-c++20-extensions" } + +struct MatrixLayout { + int rows = 0; + int outer_stride = rows; +}; +struct Matrix { + Matrix(MatrixLayout m) {} +}; +struct Widget { + int n = 5; + Matrix A0{{}}; + Matrix A1{{n}}; + Matrix A1_{{.rows = n}}; + Matrix A2{{n, n}}; +}; + +int +main () +{ + Widget w{}; + Widget w1; + Widget w2 = {}; +}