From patchwork Wed Apr 10 13:06:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1921996 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=h+I5Kjgb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4VF35D1rsjz1yYS for ; Wed, 10 Apr 2024 23:07:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5D2C3858288 for ; Wed, 10 Apr 2024 13:07:08 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id B30093858D20 for ; Wed, 10 Apr 2024 13:06:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B30093858D20 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 B30093858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712754408; cv=none; b=X70K8DlrwAdKHfhSYZzO+s/T6XNfD7rEbVInOT2179l1gHrF2ZxXFRjawP+AHmRL2VyVFi/EgVpeYir/Ij/XEcf591/X18R+R2mG1HZRvoHZBpkh0U/V2BdoYkB2o/D5yQoq+RsLb2H01br9XYQoSO1lmmOhZWAqhQiJagYcTDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712754408; c=relaxed/simple; bh=VpQVDJb0TrW33at1HqJjcMt+fO3ktaiSJoGsF/2a7PA=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=tOw7Kp3vWg6IVgLd+EbP/J/7PUe+6WpckHrrnxVRanXVanZUKD09x5GmWOi9kv4F8QfkS8R1Uv/rXPUpJz53mi/+bAiARdLuAZ91bUMfwPDLQbeperToZvfW+iRyRW1WTTX9fNHDnEVvTEqx61TPoSE7S8rqA9dy/mxBuk5aedw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712754406; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=bz/sLUpqRYExQLSnRRtC0yZKgt81Is/DasZemj1NDvI=; b=h+I5KjgbrytONv1kT+3+rmPC25P0OHcSwGQPdtVczhxlCIff3fZr9DAV9gcZPvTiYGBOWq e1ZMDstUZHKSgrF6Bq9msR0z56trEXMUtyozqnkteChcxZgJnW49eXXKUJO3zspsfRANbr xPGTc4m6C3dmX0Mo0akuhohPLAHnt7o= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-681-A9sXJ9iYNiGPxKh6ymlu3g-1; Wed, 10 Apr 2024 09:06:44 -0400 X-MC-Unique: A9sXJ9iYNiGPxKh6ymlu3g-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03FA61049B80 for ; Wed, 10 Apr 2024 13:06:44 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BD164492BC6; Wed, 10 Apr 2024 13:06:43 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 43AD6cBi111739 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 10 Apr 2024 15:06:38 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 43AD6b7Q111738; Wed, 10 Apr 2024 15:06:37 +0200 Date: Wed, 10 Apr 2024 15:06:37 +0200 From: Jakub Jelinek To: Jason Merrill , Patrick Palka Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix ANNOTATE_EXPR instantiation [PR114409] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following testcase ICEs starting with the r14-4229 PR111529 change which moved ANNOTATE_EXPR handling from tsubst_expr to tsubst_copy_and_build. ANNOTATE_EXPR is only allowed in the IL to wrap a loop condition, and the loop condition of while/for loops can be a COMPOUND_EXPR with DECL_EXPR in the first operand and the corresponding VAR_DECL in the second, as created by finish_cond else if (!empty_expr_stmt_p (cond)) expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr); Since then Patrick reworked the instantiation, so that we have now tsubst_stmt and tsubst_expr and ANNOTATE_EXPR ended up in the latter, while only tsubst_stmt can handle DECL_EXPR. Now, the reason why the while/for loops with variable declaration in the condition works in templates without the pragmas (i.e. without ANNOTATE_EXPR) is that both the FOR_STMT and WHILE_STMT handling uses RECUR aka tsubst_stmt in handling of the *_COND operand: case FOR_STMT: stmt = begin_for_stmt (NULL_TREE, NULL_TREE); RECUR (FOR_INIT_STMT (t)); finish_init_stmt (stmt); tmp = RECUR (FOR_COND (t)); finish_for_cond (tmp, stmt, false, 0, false); and case WHILE_STMT: stmt = begin_while_stmt (); tmp = RECUR (WHILE_COND (t)); finish_while_stmt_cond (tmp, stmt, false, 0, false); Therefore, it will handle DECL_EXPR embedded in COMPOUND_EXPR of the {WHILE,FOR}_COND just fine. But if that COMPOUND_EXPR with DECL_EXPR is wrapped with one or more ANNOTATE_EXPRs, because ANNOTATE_EXPR is now done solely in tsubst_expr and uses RECUR there (i.e. tsubst_expr), it will ICE on DECL_EXPR in there. Here are 2 possible fixes for this. The first one keeps ANNOTATE_EXPR handling in tsubst_expr but uses tsubst_stmt for the first operand. The second one moves ANNOTATE_EXPR handling to tsubst_stmt (and uses tsubst_expr for the second/third operand (it could just RECUR too if you prefer that)). Yet another possibility could be to duplicate the ANNOTATE_EXPR handling from tsubst_expr to tsubst_stmt, where both would just RECUR on its operands, so if one arrives to ANNOTATE_EXPR from tsubst_stmt, it will tsubst_stmt recursively, if from tsubst_expr (when?) then it would handle it using tsubst_expr. So far just lightly tested (but g++.dg/ext/unroll-4.C and the new test both pass with both versions of the patch), what do you prefer? I'd like to avoid testing too many variants... 2024-04-10 Jakub Jelinek PR c++/114409 * pt.cc (tsubst_expr) : Use tsubst_stmt rather than tsubst_expr aka RECUR on op1. * g++.dg/ext/pr114409-2.C: New test. Jakub 2024-04-10 Jakub Jelinek PR c++/114409 * pt.cc (tsubst_expr) : Move to ... (tsubst_stmt) : ... here. Use tsubst_expr instead of RECUR for the last 2 arguments. * g++.dg/ext/pr114409-2.C: New test. --- gcc/cp/pt.cc.jj 2024-04-09 09:29:04.721521726 +0200 +++ gcc/cp/pt.cc 2024-04-10 14:45:25.527142692 +0200 @@ -19433,6 +19433,23 @@ tsubst_stmt (tree t, tree args, tsubst_f case PREDICT_EXPR: RETURN (add_stmt (copy_node (t))); + case ANNOTATE_EXPR: + { + /* Although ANNOTATE_EXPR is an expression, it can only appear in + WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted + using tsubst_stmt rather than tsubst_expr and can contain + DECL_EXPRs. */ + tree op1 = RECUR (TREE_OPERAND (t, 0)); + tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); + tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl); + if (TREE_CODE (op2) == INTEGER_CST + && wi::to_widest (op2) == (int) annot_expr_unroll_kind) + op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)), + op3); + RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR, + TREE_TYPE (op1), op1, op2, op3)); + } + default: gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); @@ -21772,19 +21789,6 @@ tsubst_expr (tree t, tree args, tsubst_f RETURN (op); } - case ANNOTATE_EXPR: - { - op1 = RECUR (TREE_OPERAND (t, 0)); - tree op2 = RECUR (TREE_OPERAND (t, 1)); - tree op3 = RECUR (TREE_OPERAND (t, 2)); - if (TREE_CODE (op2) == INTEGER_CST - && wi::to_widest (op2) == (int) annot_expr_unroll_kind) - op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)), - op3); - RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR, - TREE_TYPE (op1), op1, op2, op3)); - } - default: /* Handle Objective-C++ constructs, if appropriate. */ if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl)) --- gcc/testsuite/g++.dg/ext/pr114409-2.C.jj 2024-04-10 14:35:19.693300552 +0200 +++ gcc/testsuite/g++.dg/ext/pr114409-2.C 2024-04-10 14:35:13.513383766 +0200 @@ -0,0 +1,36 @@ +// PR c++/114409 +// { dg-do compile } +// { dg-options "-O2" } + +template +T +foo (T) +{ + static T t; + return 42 - ++t; +} + +template +void +bar (T x) +{ + #pragma GCC novector + while (T y = foo (x)) + ++y; +} + +template +void +baz (T x) +{ + #pragma GCC novector + for (; T y = foo (x); ) + ++y; +} + +void +qux () +{ + bar (0); + baz (0); +} --- gcc/cp/pt.cc.jj 2024-04-09 09:29:04.721521726 +0200 +++ gcc/cp/pt.cc 2024-04-10 14:38:43.591554947 +0200 @@ -21774,7 +21774,10 @@ tsubst_expr (tree t, tree args, tsubst_f case ANNOTATE_EXPR: { - op1 = RECUR (TREE_OPERAND (t, 0)); + /* ANNOTATE_EXPR should only appear in WHILE_COND, DO_COND or + FOR_COND expressions, which are tsubsted using tsubst_stmt + rather than tsubst_expr and can contain DECL_EXPRs. */ + op1 = tsubst_stmt (TREE_OPERAND (t, 0), args, complain, in_decl); tree op2 = RECUR (TREE_OPERAND (t, 1)); tree op3 = RECUR (TREE_OPERAND (t, 2)); if (TREE_CODE (op2) == INTEGER_CST --- gcc/testsuite/g++.dg/ext/pr114409-2.C.jj 2024-04-10 14:35:19.693300552 +0200 +++ gcc/testsuite/g++.dg/ext/pr114409-2.C 2024-04-10 14:35:13.513383766 +0200 @@ -0,0 +1,36 @@ +// PR c++/114409 +// { dg-do compile } +// { dg-options "-O2" } + +template +T +foo (T) +{ + static T t; + return 42 - ++t; +} + +template +void +bar (T x) +{ + #pragma GCC novector + while (T y = foo (x)) + ++y; +} + +template +void +baz (T x) +{ + #pragma GCC novector + for (; T y = foo (x); ) + ++y; +} + +void +qux () +{ + bar (0); + baz (0); +}