From patchwork Tue Dec 5 06:55:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 173742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3251252vqy; Mon, 4 Dec 2023 22:56:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEeIK1FOPw/1FymojVwDPhMbhS5AXrTP6Y+geo66dfUXHzK9yBPnzP3zJlO4XYU4DMl2qm+ X-Received: by 2002:ae9:e10f:0:b0:77d:7e09:548 with SMTP id g15-20020ae9e10f000000b0077d7e090548mr996261qkm.22.1701759368551; Mon, 04 Dec 2023 22:56:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701759368; cv=pass; d=google.com; s=arc-20160816; b=mNYqFsjWBZnt8kPBeA5Nj+3RkzD2FAjAsHohgHCcqS0JREhV7rHc7i7g7kE/MpwKpK wHj2PpWXRiwGoPIt54umUqQTrCrGdD75H/3eL2/SSlmLLAGh3YyZ+1/pBXJ3UONSDSrD DkNsKC1NWvNOURTGtnW8atXIsSAl2+Vhai9lfvbBwJsdxio7hzpCu+Xec6d/wJjdFOho F2nSTCg8d/3FnY+QRYLo0a9qhFaCjWowQFqpPbk5Q2bcIzMoZpnPcbSsFo7j2rHdvIs9 5WCNFoxNjO9Q3LTA/ccr0TB3cg6PnboITPXWoNpd5W+jAu29z+JdglmRl0mMPJo/ayLc Z2gw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=+/EDz+BhTYrohS4afSMaseWtfwLP1tZ23mAMV63wyHM=; fh=0xZT+NBKSeH8qOu04/61f1ZGePpF4jF/gxp331YE14k=; b=pbdj4Qhdr9pgP1Ye2UMn7wp/Zu4+w2wYCD8wjHN7OCfANQoJigKCuopYhxSHft4Sh5 rGbWd63U07wFwJuJJCj8o/InBv1DkZPW9MixtB17pCoRoeDyiXaabtRvl3XAOg8lS20i LGdhabcPMYwByocMH/xJTKD3nn54nrDOHm1QfpTZfT0qMtkC2ktQaClQSN9iruSxM48P 1SW+WSwVjrWo+sl6c3FjtuF6k4ARjD2it4Qmgq5g4RjChhkPHB18aMMutvxh8d+rij0y zpVly5uEcnYJjV59x0J6I4dtHJRK8pMH2GOmvmp+lIovmndXBGsbTFt38PqnhnyHX1rp XTVw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ai5xE9gD; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h15-20020a05620a400f00b0077d9473d5d1si12264212qko.255.2023.12.04.22.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 22:56:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ai5xE9gD; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1553338449E9 for ; Tue, 5 Dec 2023 06:56: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.133.124]) by sourceware.org (Postfix) with ESMTPS id AB43A385841C for ; Tue, 5 Dec 2023 06:55:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB43A385841C 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 AB43A385841C 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=1701759345; cv=none; b=UHVgCHnJhcSb4sgg2cVs6X/Ar3yH56M17hL2OX5T6tpQVNH4Kf4QZfCl7HDn/CxJ35NjXyA2sTws1M7DX/0WNnnWFs32+GgwtUmiU8qNGpVYnOM5SbiQvaXB9/mc7EnZ8fFtT32E4uv91wHvJ7IHI+LLiO+R5/w2a1zwyfTEra8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701759345; c=relaxed/simple; bh=Ebhe7pF/G9GbLuEDXFCEqT5SC/lfdgKtE29Wm8ZaJH4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=e/Gz5LWZhBMoNQPTwA6/dAxV6Vhf1+GH1c6E3LZjrk64pjOi80gIlTWpxHiuhGC6BjQhDLQuLKehN4PKMr2nA3ynEMuw3sYDBva8mkGqhcqLOuInWsdsL071zzG/4fS106K7PhOt08hQX01hY1/PWCDWgoHw3H7uxtH8YIo+VLQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701759343; 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=+/EDz+BhTYrohS4afSMaseWtfwLP1tZ23mAMV63wyHM=; b=ai5xE9gD+nw3iwUEMcH5WimT98rdrFJrxScf6LgHPmohyPX4xZzDk7xLQDlplt6pJgetAw ZkbBzDN4pvkJKueh8qci0XY++tdSh3d8OxhAapCp8Y8F08NJ54X3eqOw88ByZf1bzbYC0a 5qUUxkih2Okv+TJzl9qnaBK16pSTetk= 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-56-ttShQkjkMI2MOQfXQDxo0A-1; Tue, 05 Dec 2023 01:55:40 -0500 X-MC-Unique: ttShQkjkMI2MOQfXQDxo0A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 A8EFC836F20 for ; Tue, 5 Dec 2023 06:55:40 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.157]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C6493C25; Tue, 5 Dec 2023 06:55:40 +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 3B56tcio693774 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 5 Dec 2023 07:55:38 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3B56tb17693773; Tue, 5 Dec 2023 07:55:37 +0100 Date: Tue, 5 Dec 2023 07:55:37 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Further #pragma GCC unroll C++ fix [PR112795] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.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_H3, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784424031349482132 X-GMAIL-MSGID: 1784424031349482132 Hi! When committing the #pragma GCC unroll patch, I found I forgot one spot for diagnosting the invalid unrolls - if #pragma GCC unroll argument is dependent and the pragma is before a range for loop, the unroll tree (now, before one converted form ushort) is saved into RANGE_FOR_UNROLL and tsubst_stmt was RECURing on it, but didn't diagnose if it was invalid and so we ICEd later in the middle-end when ANNOTATE_EXPR had unexpected argument. The following patch fixes that. Or should I create some helper function (how to call it) and call it from all of cp_parser_pragma_unroll, tsubst_stmt (here) and tsubst_expr (ANNOTATE_EXPR)? Another option is diagnose it instead when we create the ANNOTATE_EXPRs, but unfortunately that is in 3 different places. And at least for the non-template case we'd have worse location_t. Bootstrapped/regtested on x86_64-linux and i686-linux. 2023-12-04 Jakub Jelinek PR c++/112795 * pt.cc (tsubst_stmt) : Perform RANGE_FOR_UNROLL value checking here as well. * g++.dg/ext/unroll-2.C: Use { target c++11 } instead of dg-skip-if for -std=gnu++98. * g++.dg/ext/unroll-3.C: Likewise. * g++.dg/ext/unroll-7.C: New test. * g++.dg/ext/unroll-8.C: New test. Jakub --- gcc/cp/pt.cc.jj 2023-12-04 08:59:06.000000000 +0100 +++ gcc/cp/pt.cc 2023-12-04 10:49:38.149254907 +0100 @@ -18407,22 +18407,46 @@ tsubst_stmt (tree t, tree args, tsubst_f complain, in_decl, decomp); } + tree unroll = RECUR (RANGE_FOR_UNROLL (t)); + if (unroll) + { + HOST_WIDE_INT lunroll; + if (type_dependent_expression_p (unroll)) + ; + else if (!INTEGRAL_TYPE_P (TREE_TYPE (unroll)) + || (!value_dependent_expression_p (unroll) + && (!tree_fits_shwi_p (unroll) + || (lunroll = tree_to_shwi (unroll)) < 0 + || lunroll >= USHRT_MAX))) + { + error_at (EXPR_LOCATION (RANGE_FOR_UNROLL (t)), + "%<#pragma GCC unroll%> requires an " + "assignment-expression that evaluates to a " + "non-negative integral constant less than %u", + USHRT_MAX); + unroll = integer_one_node; + } + else if (TREE_CODE (unroll) == INTEGER_CST) + { + unroll = fold_convert (integer_type_node, unroll); + if (integer_zerop (unroll)) + unroll = integer_one_node; + } + } + if (processing_template_decl) { RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t); - RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t); + RANGE_FOR_UNROLL (stmt) = unroll; RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t); finish_range_for_decl (stmt, decl, expr); if (decomp && decl != error_mark_node) cp_finish_decomp (decl, decomp); } else - { - tree unroll = RECUR (RANGE_FOR_UNROLL (t)); - stmt = cp_convert_range_for (stmt, decl, expr, decomp, - RANGE_FOR_IVDEP (t), unroll, - RANGE_FOR_NOVECTOR (t)); - } + stmt = cp_convert_range_for (stmt, decl, expr, decomp, + RANGE_FOR_IVDEP (t), unroll, + RANGE_FOR_NOVECTOR (t)); bool prev = note_iteration_stmt_body_start (); RECUR (RANGE_FOR_BODY (t)); --- gcc/testsuite/g++.dg/ext/unroll-2.C.jj 2020-01-12 11:54:37.172401958 +0100 +++ gcc/testsuite/g++.dg/ext/unroll-2.C 2023-12-04 10:17:00.390997063 +0100 @@ -1,6 +1,5 @@ -// { dg-do compile } +// { dg-do compile { target c++11 } } // { dg-options "-O2 -fdump-tree-cunrolli-details" } -// { dg-skip-if "range for" { *-*-* } { "-std=gnu++98" } { "" } } void foo (int (&a)[8], int *b, int *c) --- gcc/testsuite/g++.dg/ext/unroll-3.C.jj 2020-01-12 11:54:37.172401958 +0100 +++ gcc/testsuite/g++.dg/ext/unroll-3.C 2023-12-04 10:17:13.526813516 +0100 @@ -1,6 +1,5 @@ -// { dg-do compile } +// { dg-do compile { target c++11 } } // { dg-options "-O2 -fdump-tree-cunrolli-details" } -// { dg-skip-if "range for" { *-*-* } { "-std=gnu++98" } { "" } } template void --- gcc/testsuite/g++.dg/ext/unroll-7.C.jj 2023-12-04 10:17:53.481255222 +0100 +++ gcc/testsuite/g++.dg/ext/unroll-7.C 2023-12-04 10:39:23.258115349 +0100 @@ -0,0 +1,45 @@ +// PR c++/112795 +// { dg-do compile { target c++11 } } +// { dg-options "-O2 -fdump-tree-cunrolli-details" } + +void baz (int); +constexpr int n = 3; +constexpr int m = 7; + +template +void +foo (int (&a)[3], T b) +{ +#pragma GCC unroll(n) + for (auto i : a) + baz (i); +#pragma GCC unroll(m) + for (auto i : b) + baz (i); +} + +template +void +bar (int (&a)[N]) +{ +#pragma GCC unroll(N) + for (auto i : a) + baz (i); +} + +void +qux () +{ + int a[3] = { 1, 2, 3 }; + int b[7] = { 4, 5, 6, 7, 8, 9, 10 }; + int c[6] = { 11, 12, 13, 14, 15, 16 }; + int d[10] = { 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 }; + foo (a, b); + bar <6> (c); + bar <10> (d); +} + +// { dg-final { scan-tree-dump "loop with 3 iterations completely unrolled" "cunrolli" } } +// { dg-final { scan-tree-dump "loop with 6 iterations completely unrolled" "cunrolli" } } +// { dg-final { scan-tree-dump "loop with 7 iterations completely unrolled" "cunrolli" } } +// { dg-final { scan-tree-dump "loop with 10 iterations completely unrolled" "cunrolli" } } --- gcc/testsuite/g++.dg/ext/unroll-8.C.jj 2023-12-04 10:17:56.763209358 +0100 +++ gcc/testsuite/g++.dg/ext/unroll-8.C 2023-12-04 10:44:58.531217053 +0100 @@ -0,0 +1,86 @@ +// PR c++/112795 +// { dg-do compile { target c++11 } } + +void +foo (int (&a)[3]) +{ + #pragma GCC unroll 1.0f // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll 0xffffffffffffffffULL // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll -42 // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; +} + +template +void +bar (U a) +{ + #pragma GCC unroll 1.0f // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll 0xffffffffffffffffULL // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll -42 // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; +} + +template +void +baz (U a) +{ + #pragma GCC unroll (N + 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll (N + 0xffffffffffffffffULL) + for (auto i : a) + ; + #pragma GCC unroll (N - 42) + for (auto i : a) + ; + #pragma GCC unroll ((T) 1.0f) + for (auto i : a) + ; + #pragma GCC unroll ((T) 0xffffffffffffffffULL) + for (auto i : a) + ; + #pragma GCC unroll ((T) -42) + for (auto i : a) + ; +} + +template +void +qux (U a) +{ + #pragma GCC unroll (N + 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll (N + 0xffffffffffffffffULL)// { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll (N - 42) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll ((T) 1.0f) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll ((T) 0xffffffffffffffffULL)// { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; + #pragma GCC unroll ((T) -42) // { dg-error "'#pragma GCC unroll' requires an assignment-expression that evaluates to a non-negative integral constant less than" } + for (auto i : a) + ; +} + +void +corge () +{ + int a[3] = { 1, 2, 3 }; + qux (a); +}