From patchwork Tue Dec 7 02:15:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kewen.Lin" X-Patchwork-Id: 1564467 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=iX2W81xy; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J7P6j3NDXz9s1l for ; Tue, 7 Dec 2021 13:16:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A5CD33858425 for ; Tue, 7 Dec 2021 02:16:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A5CD33858425 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638843369; bh=9KT1soLhOcPQmCEIy6P/nZ1SWA0fQtGOkUbDnLhrcGU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=iX2W81xyPXneKSb088YZHCxd4jXOjcza8rxdvZB300OLvTsjeocCSxMEAySVPVAJG RPQ6jgjo8Eue3Mr5fVRWMF60KbrEzJtIKD8HsULqXrQc7M8P8GoYgy2EsD4nQB/8EI SCjy8Tz8GKwZBPD2/4wtwF3liqMDgnzE9LuLux7s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 010403858D28 for ; Tue, 7 Dec 2021 02:15:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 010403858D28 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1B70mUiJ024909; Tue, 7 Dec 2021 02:15:26 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3csw0ghsrk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 07 Dec 2021 02:15:26 +0000 Received: from m0098404.ppops.net (m0098404.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 1B720QRq013334; Tue, 7 Dec 2021 02:15:26 GMT Received: from ppma01fra.de.ibm.com (46.49.7a9f.ip4.static.sl-reverse.com [159.122.73.70]) by mx0a-001b2d01.pphosted.com with ESMTP id 3csw0ghsqy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 07 Dec 2021 02:15:25 +0000 Received: from pps.filterd (ppma01fra.de.ibm.com [127.0.0.1]) by ppma01fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 1B72CTJU032317; Tue, 7 Dec 2021 02:15:23 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma01fra.de.ibm.com with ESMTP id 3cqyy98n8v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 07 Dec 2021 02:15:23 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 1B72FJ3c19726660 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 7 Dec 2021 02:15:19 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C9835A4067; Tue, 7 Dec 2021 02:15:19 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AED34A405B; Tue, 7 Dec 2021 02:15:16 +0000 (GMT) Received: from KewenLins-MacBook-Pro.local (unknown [9.197.229.3]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 7 Dec 2021 02:15:16 +0000 (GMT) To: GCC Patches Subject: [PATCH] pragma: Update target option node when optimization changes [PR103515] Message-ID: <6e9eaac5-3154-6460-1327-5c6afc14e33a@linux.ibm.com> Date: Tue, 7 Dec 2021 10:15:14 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: SHs1Z5ufgDCfa8bZ4h_ix_ZuR798wO_z X-Proofpoint-GUID: 1aDJLpbj69C6KzvWocddu0sW6QACGhzE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2021-12-06_08,2021-12-06_02,2021-12-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 adultscore=0 bulkscore=0 priorityscore=1501 phishscore=0 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 clxscore=1015 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112070013 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "Kewen.Lin via Gcc-patches" From: "Kewen.Lin" Reply-To: "Kewen.Lin" Cc: Bill Schmidt , bergner@linux.ibm.com, Segher Boessenkool Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, For a function with optimize pragma, it's possible that the target options change as optimization options change. Now we create one optimization option node when parsing pragma optimize, but don't create target option node for possible target option changes. It makes later processing not detect the target options have actually changed and doesn't update the target options accordingly. This patch is to check whether target options have changed when creating one optimization option node for pragma optimize, and make one target option node if needed. The associated test case shows the difference. Without this patch, the function foo1 will perform unrolling which is unexpected. The reason is that flag unroll_only_small_loops isn't correctly set for it. The value is updated after parsing function foo2, but doesn't get restored later since both decls don't have DECL_FUNCTION_SPECIFIC_TARGET set and the hook think we don't need to switch. With this patch, there is no unrolling for foo1, which is also consistent with the behavior by replacing pragma by attribute whether w/ and w/o this patch. Bootstrapped and regtested on x86_64-redhat-linux, aarch64-linux-gnu and powerpc64{,le}-linux-gnu. Is it ok for trunk? BR, Kewen --- gcc/ChangeLog: PR target/103515 * attribs.c (decl_attributes): Check if target options change and create one node if so. gcc/testsuite/ChangeLog: PR target/103515 * gcc.target/powerpc/pr103515.c: New test. ----- --- gcc/attribs.c | 13 ++++++++- gcc/testsuite/gcc.target/powerpc/pr103515.c | 30 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/powerpc/pr103515.c diff --git a/gcc/attribs.c b/gcc/attribs.c index c252f5af07b..761d9760769 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -607,7 +607,18 @@ decl_attributes (tree *node, tree attributes, int flags, if (TREE_CODE (*node) == FUNCTION_DECL && optimization_current_node != optimization_default_node && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)) - DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node; + { + DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) = optimization_current_node; + tree cur_tree + = build_target_option_node (&global_options, &global_options_set); + tree old_tree = DECL_FUNCTION_SPECIFIC_TARGET (*node); + if (!old_tree) + old_tree = target_option_default_node; + /* The changes on optimization options can cause the changes in + target options, update it accordingly if it's changed. */ + if (old_tree != cur_tree) + DECL_FUNCTION_SPECIFIC_TARGET (*node) = cur_tree; + } /* If this is a function and the user used #pragma GCC target, add the options to the attribute((target(...))) list. */ diff --git a/gcc/testsuite/gcc.target/powerpc/pr103515.c b/gcc/testsuite/gcc.target/powerpc/pr103515.c new file mode 100644 index 00000000000..698b9a93037 --- /dev/null +++ b/gcc/testsuite/gcc.target/powerpc/pr103515.c @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-rtl-loop2_unroll-optimized" } */ + +/* The pragma specified for foo2 should not affect foo1. + Verify compiler won't perform unrolling for foo1. */ + +#define N 1024 +extern int a1[N], b1[N], c1[N]; +extern int a2[N], b2[N], c2[N]; +extern int n; + +void +foo1 () +{ + int i; + for (i = 0; i < n; i++) + c1[i] += a1[i] + b1[i]; +} + +#pragma GCC optimize("O3,unroll-loops") +void +foo2 () +{ + int i; + for (i = 0; i < n; i++) + c2[i] += a2[i] + b2[i]; +} + +/* { dg-final { scan-rtl-dump-times "optimized: loop unrolled" 1 "loop2_unroll" } } */ +