Message ID | 75d4b8f9-5ed2-de4c-d606-510a66714c9a@linux.ibm.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 763FB386EC02 for <patchwork@sourceware.org>; Mon, 6 Jun 2022 06:21:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 763FB386EC02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654496476; bh=3KG0EeLRVmpKvgNSvsgtq802gRoT6Z2w1ooaSexX2bc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=OtHhb5cP8zwIPn84vK4QCq+mgen9YuiZyfG9uY8ayB28TbKiQv1QBXFKzvIyzeopc d4zJpThzSp0zU3gAbBIH8NwOncFRzFcpKR9BCYR5HlaWAID+Pq/MILlFh79Yt9Yz+k +L5kj8qA39Z85eP21ODJhXb+0mPerQgm/+98FKWo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id A59283857823 for <gcc-patches@gcc.gnu.org>; Mon, 6 Jun 2022 06:20:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A59283857823 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2565Y1iU032654; Mon, 6 Jun 2022 06:20:47 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3ggh151bpm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Jun 2022 06:20:46 +0000 Received: from m0098419.ppops.net (m0098419.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 2566KkO4016566; Mon, 6 Jun 2022 06:20:46 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3ggh151bp8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Jun 2022 06:20:46 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 25666G5Y025828; Mon, 6 Jun 2022 06:20:44 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04ams.nl.ibm.com with ESMTP id 3gfy199wvw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Jun 2022 06:20:44 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2566KULh20447736 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 6 Jun 2022 06:20:30 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3F160A405B; Mon, 6 Jun 2022 06:20:42 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 77A35A4054; Mon, 6 Jun 2022 06:20:40 +0000 (GMT) Received: from [9.197.252.204] (unknown [9.197.252.204]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Mon, 6 Jun 2022 06:20:40 +0000 (GMT) Message-ID: <75d4b8f9-5ed2-de4c-d606-510a66714c9a@linux.ibm.com> Date: Mon, 6 Jun 2022 14:20:38 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Content-Language: en-US To: GCC Patches <gcc-patches@gcc.gnu.org> Subject: [PATCH] inline: Rebuild target option node for caller [PR105459] Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: CcrdLnws6QpDiQ88xPgJRwm4ShG-UfA- X-Proofpoint-ORIG-GUID: zbpRD84t8HU6clm89sc7yEfpGJp-GD29 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-06_01,2022-06-03_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 mlxlogscore=999 impostorscore=0 malwarescore=0 bulkscore=0 mlxscore=0 adultscore=0 suspectscore=0 clxscore=1011 spamscore=0 phishscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2206060029 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: "Kewen.Lin via Gcc-patches" <gcc-patches@gcc.gnu.org> Reply-To: "Kewen.Lin" <linkw@linux.ibm.com> Cc: Jakub Jelinek <jakub@redhat.com>, Jan Hubicka <hubicka@ucw.cz> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
inline: Rebuild target option node for caller [PR105459]
|
|
Commit Message
Kewen.Lin
June 6, 2022, 6:20 a.m. UTC
Hi, PR105459 exposes one issue in inline_call handling that when it decides to copy FP flags from callee to caller and rebuild the optimization node for caller fndecl, it's possible that the target option node is also necessary to be rebuilt. Without updating target option node early, it can make nodes share the same target option node wrongly, later when we want to unshare it somewhere (like in target hook) it can get unexpected results, like ICE on uninitialized secondary member of target globals exposed in this PR. Commit r12-3721 makes it get exact fp_expression info and causes more optimization chances then exposes this issue. Commit r11-5855 introduces two target options to shadow flag_excess_precision and flag_unsafe_math_optimizations and shows the need to rebuild target node in inline_call when optimization node changes. As commented in PR105459, I tried to postpone init_function_start in cgraph_node::expand, but abandoned it since I thought it just concealed the issue. And I also tried to adjust the target node when current function switching, but failed since we get the NULL cfun and fndecl in WPA phase. Bootstrapped and regtested on x86_64-redhat-linux, powerpc64-linux-gnu P8 and powerpc64le-linux-gnu P9. Any thoughts? Is it OK for trunk? BR, Kewen ----- PR tree-optimization/105459 gcc/ChangeLog: * ipa-inline-transform.cc (inline_call): Rebuild target option node once optimization node gets rebuilt. gcc/testsuite/ChangeLog: * gcc.dg/lto/pr105459_0.c: New test. --- gcc/ipa-inline-transform.cc | 50 +++++++++++++++++++++++++-- gcc/testsuite/gcc.dg/lto/pr105459_0.c | 35 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/lto/pr105459_0.c -- 2.27.0
Comments
Hi, Gentle ping https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596212.html BR, Kewen on 2022/6/6 14:20, Kewen.Lin via Gcc-patches wrote: > Hi, > > PR105459 exposes one issue in inline_call handling that when it > decides to copy FP flags from callee to caller and rebuild the > optimization node for caller fndecl, it's possible that the target > option node is also necessary to be rebuilt. Without updating > target option node early, it can make nodes share the same target > option node wrongly, later when we want to unshare it somewhere > (like in target hook) it can get unexpected results, like ICE on > uninitialized secondary member of target globals exposed in this PR. > > Commit r12-3721 makes it get exact fp_expression info and causes > more optimization chances then exposes this issue. Commit r11-5855 > introduces two target options to shadow flag_excess_precision and > flag_unsafe_math_optimizations and shows the need to rebuild target > node in inline_call when optimization node changes. > > As commented in PR105459, I tried to postpone init_function_start > in cgraph_node::expand, but abandoned it since I thought it just > concealed the issue. And I also tried to adjust the target node > when current function switching, but failed since we get the NULL > cfun and fndecl in WPA phase. > > Bootstrapped and regtested on x86_64-redhat-linux, powerpc64-linux-gnu > P8 and powerpc64le-linux-gnu P9. > > Any thoughts? Is it OK for trunk? > > BR, > Kewen > ----- > > PR tree-optimization/105459 > > gcc/ChangeLog: > > * ipa-inline-transform.cc (inline_call): Rebuild target option node > once optimization node gets rebuilt. > > gcc/testsuite/ChangeLog: > > * gcc.dg/lto/pr105459_0.c: New test. > --- > gcc/ipa-inline-transform.cc | 50 +++++++++++++++++++++++++-- > gcc/testsuite/gcc.dg/lto/pr105459_0.c | 35 +++++++++++++++++++ > 2 files changed, 83 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/lto/pr105459_0.c > > diff --git a/gcc/ipa-inline-transform.cc b/gcc/ipa-inline-transform.cc > index 07288e57c73..edba58377f4 100644 > --- a/gcc/ipa-inline-transform.cc > +++ b/gcc/ipa-inline-transform.cc > @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see > #include "ipa-modref.h" > #include "symtab-thunks.h" > #include "symtab-clones.h" > +#include "target.h" > > int ncalls_inlined; > int nfunctions_inlined; > @@ -469,8 +470,53 @@ inline_call (struct cgraph_edge *e, bool update_original, > } > > /* Reload global optimization flags. */ > - if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > - set_cfun (cfun, true); > + if (reload_optimization_node) > + { > + /* Only need to check and update target option node > + when target_option_default_node is not NULL. */ > + if (target_option_default_node) > + { > + /* Save the current context for optimization and target option > + node. */ > + tree old_optimize > + = build_optimization_node (&global_options, &global_options_set); > + tree old_target_opt > + = build_target_option_node (&global_options, &global_options_set); > + > + /* Restore optimization with new optimizatin node. */ > + tree new_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl); > + if (old_optimize != new_optimize) > + cl_optimization_restore (&global_options, &global_options_set, > + TREE_OPTIMIZATION (new_optimize)); > + > + /* Restore target option with the one from caller fndecl. */ > + tree cur_target_opt = DECL_FUNCTION_SPECIFIC_TARGET (to->decl); > + if (!cur_target_opt) > + cur_target_opt = target_option_default_node; > + cl_target_option_restore (&global_options, &global_options_set, > + TREE_TARGET_OPTION (cur_target_opt)); > + > + /* Update target option as optimization changes. */ > + targetm.target_option.override (); > + > + /* Rebuild target option node for caller fndecl and replace > + with it if the node changes. */ > + tree new_target_opt > + = build_target_option_node (&global_options, &global_options_set); > + if (cur_target_opt != new_target_opt) > + DECL_FUNCTION_SPECIFIC_TARGET (to->decl) = new_target_opt; > + > + /* Restore the context with previous saved nodes. */ > + if (old_optimize != new_optimize) > + cl_optimization_restore (&global_options, &global_options_set, > + TREE_OPTIMIZATION (old_optimize)); > + cl_target_option_restore (&global_options, &global_options_set, > + TREE_TARGET_OPTION (old_target_opt)); > + } > + > + if (DECL_STRUCT_FUNCTION (to->decl) == cfun) > + set_cfun (cfun, true); > + } > > /* If aliases are involved, redirect edge to the actual destination and > possibly remove the aliases. */ > diff --git a/gcc/testsuite/gcc.dg/lto/pr105459_0.c b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > new file mode 100644 > index 00000000000..c799e6ef23d > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > @@ -0,0 +1,35 @@ > +/* { dg-lto-do link } */ > +/* { dg-lto-options { { -flto -O1 } } } */ > + > +double m; > +int n; > + > +__attribute__ ((optimize ("-funsafe-math-optimizations"))) > +void > +bar (int x) > +{ > + n = x; > + m = n; > +} > + > +__attribute__ ((flatten)) > +void > +foo (int x) > +{ > + bar (x); > +} > + > +void > +quux (void) > +{ > + ++n; > +} > + > +int > +main (void) > +{ > + foo (0); > + quux (); > + > + return 0; > +} > -- > 2.27.0
On Thu, Jun 23, 2022 at 4:03 AM Kewen.Lin <linkw@linux.ibm.com> wrote: > > Hi, > > Gentle ping https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596212.html > > BR, > Kewen > > on 2022/6/6 14:20, Kewen.Lin via Gcc-patches wrote: > > Hi, > > > > PR105459 exposes one issue in inline_call handling that when it > > decides to copy FP flags from callee to caller and rebuild the > > optimization node for caller fndecl, it's possible that the target > > option node is also necessary to be rebuilt. Without updating > > target option node early, it can make nodes share the same target > > option node wrongly, later when we want to unshare it somewhere > > (like in target hook) it can get unexpected results, like ICE on > > uninitialized secondary member of target globals exposed in this PR. I think that /* Reload global optimization flags. */ if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) set_cfun (cfun, true); is supposed to do that via ix86_set_current_function which will eventually re-build the target optimization node exactly for this reason. But with LTO we arrive here during WPA time only and there cfun is NULL (and so is DECL_STRUCT_FUNCTION (to->decl)), so the target doesn't get the chance to fix things up here. Now, it should be fine to delay this fixup until we set the cfun at LTRANS time but there we run into if (old_tree != new_tree) { cl_target_option_restore (&global_options, &global_options_set, TREE_TARGET_OPTION (new_tree)); ... } else if (flag_unsafe_math_optimizations != TREE_TARGET_OPTION (new_tree)->x_ix86_unsafe_math_optimizations || (flag_excess_precision != TREE_TARGET_OPTION (new_tree)->x_ix86_excess_precision)) { ... FIXUP! ... and old_tree != new_tree disables the fixup. When we refactor the above to always consider the FP flag change (so apply it lazily), then this fixes the testcase in the PR as well. Thus something like the attached. Ideally this stuff would be refactored to a target hook that can work without the set_cfun, also working towards merging the target and optimization node since they have to be kept in sync ... I think your proposed patch makes another variant through the maze to do something at WPA time but that makes it all even more complicated :/ Sorry for the delay btw. Folks - any other opinions? Thanks, Richard. > > Commit r12-3721 makes it get exact fp_expression info and causes > > more optimization chances then exposes this issue. Commit r11-5855 > > introduces two target options to shadow flag_excess_precision and > > flag_unsafe_math_optimizations and shows the need to rebuild target > > node in inline_call when optimization node changes. > > > > As commented in PR105459, I tried to postpone init_function_start > > in cgraph_node::expand, but abandoned it since I thought it just > > concealed the issue. And I also tried to adjust the target node > > when current function switching, but failed since we get the NULL > > cfun and fndecl in WPA phase. > > > > Bootstrapped and regtested on x86_64-redhat-linux, powerpc64-linux-gnu > > P8 and powerpc64le-linux-gnu P9. > > > > Any thoughts? Is it OK for trunk? > > > > BR, > > Kewen > > ----- > > > > PR tree-optimization/105459 > > > > gcc/ChangeLog: > > > > * ipa-inline-transform.cc (inline_call): Rebuild target option node > > once optimization node gets rebuilt. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/lto/pr105459_0.c: New test. > > --- > > gcc/ipa-inline-transform.cc | 50 +++++++++++++++++++++++++-- > > gcc/testsuite/gcc.dg/lto/pr105459_0.c | 35 +++++++++++++++++++ > > 2 files changed, 83 insertions(+), 2 deletions(-) > > create mode 100644 gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > > diff --git a/gcc/ipa-inline-transform.cc b/gcc/ipa-inline-transform.cc > > index 07288e57c73..edba58377f4 100644 > > --- a/gcc/ipa-inline-transform.cc > > +++ b/gcc/ipa-inline-transform.cc > > @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see > > #include "ipa-modref.h" > > #include "symtab-thunks.h" > > #include "symtab-clones.h" > > +#include "target.h" > > > > int ncalls_inlined; > > int nfunctions_inlined; > > @@ -469,8 +470,53 @@ inline_call (struct cgraph_edge *e, bool update_original, > > } > > > > /* Reload global optimization flags. */ > > - if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > > - set_cfun (cfun, true); > > + if (reload_optimization_node) > > + { > > + /* Only need to check and update target option node > > + when target_option_default_node is not NULL. */ > > + if (target_option_default_node) > > + { > > + /* Save the current context for optimization and target option > > + node. */ > > + tree old_optimize > > + = build_optimization_node (&global_options, &global_options_set); > > + tree old_target_opt > > + = build_target_option_node (&global_options, &global_options_set); > > + > > + /* Restore optimization with new optimizatin node. */ > > + tree new_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl); > > + if (old_optimize != new_optimize) > > + cl_optimization_restore (&global_options, &global_options_set, > > + TREE_OPTIMIZATION (new_optimize)); > > + > > + /* Restore target option with the one from caller fndecl. */ > > + tree cur_target_opt = DECL_FUNCTION_SPECIFIC_TARGET (to->decl); > > + if (!cur_target_opt) > > + cur_target_opt = target_option_default_node; > > + cl_target_option_restore (&global_options, &global_options_set, > > + TREE_TARGET_OPTION (cur_target_opt)); > > + > > + /* Update target option as optimization changes. */ > > + targetm.target_option.override (); > > + > > + /* Rebuild target option node for caller fndecl and replace > > + with it if the node changes. */ > > + tree new_target_opt > > + = build_target_option_node (&global_options, &global_options_set); > > + if (cur_target_opt != new_target_opt) > > + DECL_FUNCTION_SPECIFIC_TARGET (to->decl) = new_target_opt; > > + > > + /* Restore the context with previous saved nodes. */ > > + if (old_optimize != new_optimize) > > + cl_optimization_restore (&global_options, &global_options_set, > > + TREE_OPTIMIZATION (old_optimize)); > > + cl_target_option_restore (&global_options, &global_options_set, > > + TREE_TARGET_OPTION (old_target_opt)); > > + } > > + > > + if (DECL_STRUCT_FUNCTION (to->decl) == cfun) > > + set_cfun (cfun, true); > > + } > > > > /* If aliases are involved, redirect edge to the actual destination and > > possibly remove the aliases. */ > > diff --git a/gcc/testsuite/gcc.dg/lto/pr105459_0.c b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > new file mode 100644 > > index 00000000000..c799e6ef23d > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > @@ -0,0 +1,35 @@ > > +/* { dg-lto-do link } */ > > +/* { dg-lto-options { { -flto -O1 } } } */ > > + > > +double m; > > +int n; > > + > > +__attribute__ ((optimize ("-funsafe-math-optimizations"))) > > +void > > +bar (int x) > > +{ > > + n = x; > > + m = n; > > +} > > + > > +__attribute__ ((flatten)) > > +void > > +foo (int x) > > +{ > > + bar (x); > > +} > > + > > +void > > +quux (void) > > +{ > > + ++n; > > +} > > + > > +int > > +main (void) > > +{ > > + foo (0); > > + quux (); > > + > > + return 0; > > +} > > -- > > 2.27.0
> On Thu, Jun 23, 2022 at 4:03 AM Kewen.Lin <linkw@linux.ibm.com> wrote: > > > > Hi, > > > > Gentle ping https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596212.html > > > > BR, > > Kewen > > > > on 2022/6/6 14:20, Kewen.Lin via Gcc-patches wrote: > > > Hi, > > > > > > PR105459 exposes one issue in inline_call handling that when it > > > decides to copy FP flags from callee to caller and rebuild the > > > optimization node for caller fndecl, it's possible that the target > > > option node is also necessary to be rebuilt. Without updating > > > target option node early, it can make nodes share the same target > > > option node wrongly, later when we want to unshare it somewhere > > > (like in target hook) it can get unexpected results, like ICE on > > > uninitialized secondary member of target globals exposed in this PR. > > I think that > > /* Reload global optimization flags. */ > if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > set_cfun (cfun, true); > > is supposed to do that via ix86_set_current_function which will eventually > re-build the target optimization node exactly for this reason. > > But with LTO we arrive here during WPA time only and there cfun is NULL > (and so is DECL_STRUCT_FUNCTION (to->decl)), so the target doesn't > get the chance to fix things up here. I see this logic was added by Martin in 2017 and it indeed looks bit odd, but I suppose it is intended primarily for early inliner where cfun is non-NULL and we really need to update global state. > > Now, it should be fine to delay this fixup until we set the cfun at LTRANS > time but there we run into > > if (old_tree != new_tree) > { > cl_target_option_restore (&global_options, &global_options_set, > TREE_TARGET_OPTION (new_tree)); > ... > } > else if (flag_unsafe_math_optimizations > != TREE_TARGET_OPTION (new_tree)->x_ix86_unsafe_math_optimizations > || (flag_excess_precision > != TREE_TARGET_OPTION (new_tree)->x_ix86_excess_precision)) > { > ... FIXUP! ... > > and old_tree != new_tree disables the fixup. > > When we refactor the above to always consider the FP flag change (so apply it > lazily), then this fixes the testcase in the PR as well. Thus something like > the attached. > > Ideally this stuff would be refactored to a target hook that can work without > the set_cfun, also working towards merging the target and optimization node > since they have to be kept in sync ... > > I think your proposed patch makes another variant through the maze to > do something at WPA time but that makes it all even more complicated :/ > > Sorry for the delay btw. > > Folks - any other opinions? Your patch looks reasonable to me... Indeed working on nodes directly would be nicer, but that means bigger surgery in the optimization handling right? Thanks, Honza > > Thanks, > Richard. > > > > Commit r12-3721 makes it get exact fp_expression info and causes > > > more optimization chances then exposes this issue. Commit r11-5855 > > > introduces two target options to shadow flag_excess_precision and > > > flag_unsafe_math_optimizations and shows the need to rebuild target > > > node in inline_call when optimization node changes. > > > > > > As commented in PR105459, I tried to postpone init_function_start > > > in cgraph_node::expand, but abandoned it since I thought it just > > > concealed the issue. And I also tried to adjust the target node > > > when current function switching, but failed since we get the NULL > > > cfun and fndecl in WPA phase. > > > > > > Bootstrapped and regtested on x86_64-redhat-linux, powerpc64-linux-gnu > > > P8 and powerpc64le-linux-gnu P9. > > > > > > Any thoughts? Is it OK for trunk? > > > > > > BR, > > > Kewen > > > ----- > > > > > > PR tree-optimization/105459 > > > > > > gcc/ChangeLog: > > > > > > * ipa-inline-transform.cc (inline_call): Rebuild target option node > > > once optimization node gets rebuilt. > > > > > > gcc/testsuite/ChangeLog: > > > > > > * gcc.dg/lto/pr105459_0.c: New test. > > > --- > > > gcc/ipa-inline-transform.cc | 50 +++++++++++++++++++++++++-- > > > gcc/testsuite/gcc.dg/lto/pr105459_0.c | 35 +++++++++++++++++++ > > > 2 files changed, 83 insertions(+), 2 deletions(-) > > > create mode 100644 gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > > > > diff --git a/gcc/ipa-inline-transform.cc b/gcc/ipa-inline-transform.cc > > > index 07288e57c73..edba58377f4 100644 > > > --- a/gcc/ipa-inline-transform.cc > > > +++ b/gcc/ipa-inline-transform.cc > > > @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see > > > #include "ipa-modref.h" > > > #include "symtab-thunks.h" > > > #include "symtab-clones.h" > > > +#include "target.h" > > > > > > int ncalls_inlined; > > > int nfunctions_inlined; > > > @@ -469,8 +470,53 @@ inline_call (struct cgraph_edge *e, bool update_original, > > > } > > > > > > /* Reload global optimization flags. */ > > > - if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > > > - set_cfun (cfun, true); > > > + if (reload_optimization_node) > > > + { > > > + /* Only need to check and update target option node > > > + when target_option_default_node is not NULL. */ > > > + if (target_option_default_node) > > > + { > > > + /* Save the current context for optimization and target option > > > + node. */ > > > + tree old_optimize > > > + = build_optimization_node (&global_options, &global_options_set); > > > + tree old_target_opt > > > + = build_target_option_node (&global_options, &global_options_set); > > > + > > > + /* Restore optimization with new optimizatin node. */ > > > + tree new_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl); > > > + if (old_optimize != new_optimize) > > > + cl_optimization_restore (&global_options, &global_options_set, > > > + TREE_OPTIMIZATION (new_optimize)); > > > + > > > + /* Restore target option with the one from caller fndecl. */ > > > + tree cur_target_opt = DECL_FUNCTION_SPECIFIC_TARGET (to->decl); > > > + if (!cur_target_opt) > > > + cur_target_opt = target_option_default_node; > > > + cl_target_option_restore (&global_options, &global_options_set, > > > + TREE_TARGET_OPTION (cur_target_opt)); > > > + > > > + /* Update target option as optimization changes. */ > > > + targetm.target_option.override (); > > > + > > > + /* Rebuild target option node for caller fndecl and replace > > > + with it if the node changes. */ > > > + tree new_target_opt > > > + = build_target_option_node (&global_options, &global_options_set); > > > + if (cur_target_opt != new_target_opt) > > > + DECL_FUNCTION_SPECIFIC_TARGET (to->decl) = new_target_opt; > > > + > > > + /* Restore the context with previous saved nodes. */ > > > + if (old_optimize != new_optimize) > > > + cl_optimization_restore (&global_options, &global_options_set, > > > + TREE_OPTIMIZATION (old_optimize)); > > > + cl_target_option_restore (&global_options, &global_options_set, > > > + TREE_TARGET_OPTION (old_target_opt)); > > > + } > > > + > > > + if (DECL_STRUCT_FUNCTION (to->decl) == cfun) > > > + set_cfun (cfun, true); > > > + } > > > > > > /* If aliases are involved, redirect edge to the actual destination and > > > possibly remove the aliases. */ > > > diff --git a/gcc/testsuite/gcc.dg/lto/pr105459_0.c b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > new file mode 100644 > > > index 00000000000..c799e6ef23d > > > --- /dev/null > > > +++ b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > @@ -0,0 +1,35 @@ > > > +/* { dg-lto-do link } */ > > > +/* { dg-lto-options { { -flto -O1 } } } */ > > > + > > > +double m; > > > +int n; > > > + > > > +__attribute__ ((optimize ("-funsafe-math-optimizations"))) > > > +void > > > +bar (int x) > > > +{ > > > + n = x; > > > + m = n; > > > +} > > > + > > > +__attribute__ ((flatten)) > > > +void > > > +foo (int x) > > > +{ > > > + bar (x); > > > +} > > > + > > > +void > > > +quux (void) > > > +{ > > > + ++n; > > > +} > > > + > > > +int > > > +main (void) > > > +{ > > > + foo (0); > > > + quux (); > > > + > > > + return 0; > > > +} > > > -- > > > 2.27.0
Hi Richi, Thanks for the insightful comments! on 2022/7/1 16:40, Richard Biener wrote: > On Thu, Jun 23, 2022 at 4:03 AM Kewen.Lin <linkw@linux.ibm.com> wrote: >> >> Hi, >> >> Gentle ping https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596212.html >> >> BR, >> Kewen >> >> on 2022/6/6 14:20, Kewen.Lin via Gcc-patches wrote: >>> Hi, >>> >>> PR105459 exposes one issue in inline_call handling that when it >>> decides to copy FP flags from callee to caller and rebuild the >>> optimization node for caller fndecl, it's possible that the target >>> option node is also necessary to be rebuilt. Without updating >>> target option node early, it can make nodes share the same target >>> option node wrongly, later when we want to unshare it somewhere >>> (like in target hook) it can get unexpected results, like ICE on >>> uninitialized secondary member of target globals exposed in this PR. > > I think that > > /* Reload global optimization flags. */ > if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > set_cfun (cfun, true); > > is supposed to do that via ix86_set_current_function which will eventually > re-build the target optimization node exactly for this reason. > > But with LTO we arrive here during WPA time only and there cfun is NULL > (and so is DECL_STRUCT_FUNCTION (to->decl)), so the target doesn't > get the chance to fix things up here. > Yes, when I read this code, I was thinking if we can do some similar to get the hook to update target node, but as you pointed out, the cfun is NULL here. :( > Now, it should be fine to delay this fixup until we set the cfun at LTRANS > time but there we run into > > if (old_tree != new_tree) > { > cl_target_option_restore (&global_options, &global_options_set, > TREE_TARGET_OPTION (new_tree)); > ... > } > else if (flag_unsafe_math_optimizations > != TREE_TARGET_OPTION (new_tree)->x_ix86_unsafe_math_optimizations > || (flag_excess_precision > != TREE_TARGET_OPTION (new_tree)->x_ix86_excess_precision)) > { > ... FIXUP! ... > > and old_tree != new_tree disables the fixup. > > When we refactor the above to always consider the FP flag change (so apply it > lazily), then this fixes the testcase in the PR as well. Thus something like > the attached. Good idea! Previously following the code for reload_optimization_node, I thought it's good to update the target node information up to date at the same time, but your proposal with delaying fixup till LTRANS looks better, IIUC WPA passes won't care about this information out of date or not. > > Ideally this stuff would be refactored to a target hook that can work without > the set_cfun, also working towards merging the target and optimization node > since they have to be kept in sync ... > > I think your proposed patch makes another variant through the maze to > do something at WPA time but that makes it all even more complicated :/ > > Sorry for the delay btw. > No problem! Thanks again!! BR, Kewen
On Fri, Jul 1, 2022 at 11:20 AM Jan Hubicka <hubicka@kam.mff.cuni.cz> wrote: > > > On Thu, Jun 23, 2022 at 4:03 AM Kewen.Lin <linkw@linux.ibm.com> wrote: > > > > > > Hi, > > > > > > Gentle ping https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596212.html > > > > > > BR, > > > Kewen > > > > > > on 2022/6/6 14:20, Kewen.Lin via Gcc-patches wrote: > > > > Hi, > > > > > > > > PR105459 exposes one issue in inline_call handling that when it > > > > decides to copy FP flags from callee to caller and rebuild the > > > > optimization node for caller fndecl, it's possible that the target > > > > option node is also necessary to be rebuilt. Without updating > > > > target option node early, it can make nodes share the same target > > > > option node wrongly, later when we want to unshare it somewhere > > > > (like in target hook) it can get unexpected results, like ICE on > > > > uninitialized secondary member of target globals exposed in this PR. > > > > I think that > > > > /* Reload global optimization flags. */ > > if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > > set_cfun (cfun, true); > > > > is supposed to do that via ix86_set_current_function which will eventually > > re-build the target optimization node exactly for this reason. > > > > But with LTO we arrive here during WPA time only and there cfun is NULL > > (and so is DECL_STRUCT_FUNCTION (to->decl)), so the target doesn't > > get the chance to fix things up here. > > I see this logic was added by Martin in 2017 and it indeed looks bit > odd, but I suppose it is intended primarily for early inliner where cfun > is non-NULL and we really need to update global state. > > > > Now, it should be fine to delay this fixup until we set the cfun at LTRANS > > time but there we run into > > > > if (old_tree != new_tree) > > { > > cl_target_option_restore (&global_options, &global_options_set, > > TREE_TARGET_OPTION (new_tree)); > > ... > > } > > else if (flag_unsafe_math_optimizations > > != TREE_TARGET_OPTION (new_tree)->x_ix86_unsafe_math_optimizations > > || (flag_excess_precision > > != TREE_TARGET_OPTION (new_tree)->x_ix86_excess_precision)) > > { > > ... FIXUP! ... > > > > and old_tree != new_tree disables the fixup. > > > > When we refactor the above to always consider the FP flag change (so apply it > > lazily), then this fixes the testcase in the PR as well. Thus something like > > the attached. > > > > Ideally this stuff would be refactored to a target hook that can work without > > the set_cfun, also working towards merging the target and optimization node > > since they have to be kept in sync ... > > > > I think your proposed patch makes another variant through the maze to > > do something at WPA time but that makes it all even more complicated :/ > > > > Sorry for the delay btw. > > > > Folks - any other opinions? > > Your patch looks reasonable to me... Indeed working on nodes directly > would be nicer, but that means bigger surgery in the optimization > handling right? Yeah, nothing like I feel doing right now ... I'm going to give the patch full testing now. Richard. > > Thanks, > Honza > > > > Thanks, > > Richard. > > > > > > Commit r12-3721 makes it get exact fp_expression info and causes > > > > more optimization chances then exposes this issue. Commit r11-5855 > > > > introduces two target options to shadow flag_excess_precision and > > > > flag_unsafe_math_optimizations and shows the need to rebuild target > > > > node in inline_call when optimization node changes. > > > > > > > > As commented in PR105459, I tried to postpone init_function_start > > > > in cgraph_node::expand, but abandoned it since I thought it just > > > > concealed the issue. And I also tried to adjust the target node > > > > when current function switching, but failed since we get the NULL > > > > cfun and fndecl in WPA phase. > > > > > > > > Bootstrapped and regtested on x86_64-redhat-linux, powerpc64-linux-gnu > > > > P8 and powerpc64le-linux-gnu P9. > > > > > > > > Any thoughts? Is it OK for trunk? > > > > > > > > BR, > > > > Kewen > > > > ----- > > > > > > > > PR tree-optimization/105459 > > > > > > > > gcc/ChangeLog: > > > > > > > > * ipa-inline-transform.cc (inline_call): Rebuild target option node > > > > once optimization node gets rebuilt. > > > > > > > > gcc/testsuite/ChangeLog: > > > > > > > > * gcc.dg/lto/pr105459_0.c: New test. > > > > --- > > > > gcc/ipa-inline-transform.cc | 50 +++++++++++++++++++++++++-- > > > > gcc/testsuite/gcc.dg/lto/pr105459_0.c | 35 +++++++++++++++++++ > > > > 2 files changed, 83 insertions(+), 2 deletions(-) > > > > create mode 100644 gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > > > > > > diff --git a/gcc/ipa-inline-transform.cc b/gcc/ipa-inline-transform.cc > > > > index 07288e57c73..edba58377f4 100644 > > > > --- a/gcc/ipa-inline-transform.cc > > > > +++ b/gcc/ipa-inline-transform.cc > > > > @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see > > > > #include "ipa-modref.h" > > > > #include "symtab-thunks.h" > > > > #include "symtab-clones.h" > > > > +#include "target.h" > > > > > > > > int ncalls_inlined; > > > > int nfunctions_inlined; > > > > @@ -469,8 +470,53 @@ inline_call (struct cgraph_edge *e, bool update_original, > > > > } > > > > > > > > /* Reload global optimization flags. */ > > > > - if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) > > > > - set_cfun (cfun, true); > > > > + if (reload_optimization_node) > > > > + { > > > > + /* Only need to check and update target option node > > > > + when target_option_default_node is not NULL. */ > > > > + if (target_option_default_node) > > > > + { > > > > + /* Save the current context for optimization and target option > > > > + node. */ > > > > + tree old_optimize > > > > + = build_optimization_node (&global_options, &global_options_set); > > > > + tree old_target_opt > > > > + = build_target_option_node (&global_options, &global_options_set); > > > > + > > > > + /* Restore optimization with new optimizatin node. */ > > > > + tree new_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl); > > > > + if (old_optimize != new_optimize) > > > > + cl_optimization_restore (&global_options, &global_options_set, > > > > + TREE_OPTIMIZATION (new_optimize)); > > > > + > > > > + /* Restore target option with the one from caller fndecl. */ > > > > + tree cur_target_opt = DECL_FUNCTION_SPECIFIC_TARGET (to->decl); > > > > + if (!cur_target_opt) > > > > + cur_target_opt = target_option_default_node; > > > > + cl_target_option_restore (&global_options, &global_options_set, > > > > + TREE_TARGET_OPTION (cur_target_opt)); > > > > + > > > > + /* Update target option as optimization changes. */ > > > > + targetm.target_option.override (); > > > > + > > > > + /* Rebuild target option node for caller fndecl and replace > > > > + with it if the node changes. */ > > > > + tree new_target_opt > > > > + = build_target_option_node (&global_options, &global_options_set); > > > > + if (cur_target_opt != new_target_opt) > > > > + DECL_FUNCTION_SPECIFIC_TARGET (to->decl) = new_target_opt; > > > > + > > > > + /* Restore the context with previous saved nodes. */ > > > > + if (old_optimize != new_optimize) > > > > + cl_optimization_restore (&global_options, &global_options_set, > > > > + TREE_OPTIMIZATION (old_optimize)); > > > > + cl_target_option_restore (&global_options, &global_options_set, > > > > + TREE_TARGET_OPTION (old_target_opt)); > > > > + } > > > > + > > > > + if (DECL_STRUCT_FUNCTION (to->decl) == cfun) > > > > + set_cfun (cfun, true); > > > > + } > > > > > > > > /* If aliases are involved, redirect edge to the actual destination and > > > > possibly remove the aliases. */ > > > > diff --git a/gcc/testsuite/gcc.dg/lto/pr105459_0.c b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > > new file mode 100644 > > > > index 00000000000..c799e6ef23d > > > > --- /dev/null > > > > +++ b/gcc/testsuite/gcc.dg/lto/pr105459_0.c > > > > @@ -0,0 +1,35 @@ > > > > +/* { dg-lto-do link } */ > > > > +/* { dg-lto-options { { -flto -O1 } } } */ > > > > + > > > > +double m; > > > > +int n; > > > > + > > > > +__attribute__ ((optimize ("-funsafe-math-optimizations"))) > > > > +void > > > > +bar (int x) > > > > +{ > > > > + n = x; > > > > + m = n; > > > > +} > > > > + > > > > +__attribute__ ((flatten)) > > > > +void > > > > +foo (int x) > > > > +{ > > > > + bar (x); > > > > +} > > > > + > > > > +void > > > > +quux (void) > > > > +{ > > > > + ++n; > > > > +} > > > > + > > > > +int > > > > +main (void) > > > > +{ > > > > + foo (0); > > > > + quux (); > > > > + > > > > + return 0; > > > > +} > > > > -- > > > > 2.27.0 > >
On 6/6/22 08:20, Kewen.Lin wrote:
> |Hi, PR105459 exposes one issue in inline_call handling that when it decides to copy FP flags from callee to caller and rebuild the optimization node for caller fndecl, it's possible that the target option node is also necessary to be rebuilt. Without updating target option node early, it can make nodes share the same target option node wrongly, later when we want to unshare it somewhere (like in target hook) it can get unexpected results, like ICE on uninitialized secondary member of target globals exposed in this PR.|
Hello.
I think your patch seems reasonable. As you mentioned we need to keep
pair of target and optimization nodes together and the only correct way
is by using build_target_option_node for the DECL_FUNCTION_SPECIFIC_TARGET.
Thanks for the fix.
Martin
on 2022/7/8 19:37, Martin Liška wrote: > On 6/6/22 08:20, Kewen.Lin wrote: >> |Hi, PR105459 exposes one issue in inline_call handling that when it decides to copy FP flags from callee to caller and rebuild the optimization node for caller fndecl, it's possible that the target option node is also necessary to be rebuilt. Without updating target option node early, it can make nodes share the same target option node wrongly, later when we want to unshare it somewhere (like in target hook) it can get unexpected results, like ICE on uninitialized secondary member of target globals exposed in this PR.| > > > Hello. > > I think your patch seems reasonable. As you mentioned we need to keep > pair of target and optimization nodes together and the only correct way > is by using build_target_option_node for the DECL_FUNCTION_SPECIFIC_TARGET. Hi Martin, Thanks for your comments!! Yeah, the idea of this patch is to make target option node up to date right away, but Richi thought it's too complicated and posted another patch to leave it to be fixed up later in LTRANS, Honza already approved it. I guessed that thread escaped from your mail radar somehow, it started from [1]. [1] https://gcc.gnu.org/pipermail/gcc-patches/2022-July/597595.html BR, Kewen
diff --git a/gcc/ipa-inline-transform.cc b/gcc/ipa-inline-transform.cc index 07288e57c73..edba58377f4 100644 --- a/gcc/ipa-inline-transform.cc +++ b/gcc/ipa-inline-transform.cc @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see #include "ipa-modref.h" #include "symtab-thunks.h" #include "symtab-clones.h" +#include "target.h" int ncalls_inlined; int nfunctions_inlined; @@ -469,8 +470,53 @@ inline_call (struct cgraph_edge *e, bool update_original, } /* Reload global optimization flags. */ - if (reload_optimization_node && DECL_STRUCT_FUNCTION (to->decl) == cfun) - set_cfun (cfun, true); + if (reload_optimization_node) + { + /* Only need to check and update target option node + when target_option_default_node is not NULL. */ + if (target_option_default_node) + { + /* Save the current context for optimization and target option + node. */ + tree old_optimize + = build_optimization_node (&global_options, &global_options_set); + tree old_target_opt + = build_target_option_node (&global_options, &global_options_set); + + /* Restore optimization with new optimizatin node. */ + tree new_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (to->decl); + if (old_optimize != new_optimize) + cl_optimization_restore (&global_options, &global_options_set, + TREE_OPTIMIZATION (new_optimize)); + + /* Restore target option with the one from caller fndecl. */ + tree cur_target_opt = DECL_FUNCTION_SPECIFIC_TARGET (to->decl); + if (!cur_target_opt) + cur_target_opt = target_option_default_node; + cl_target_option_restore (&global_options, &global_options_set, + TREE_TARGET_OPTION (cur_target_opt)); + + /* Update target option as optimization changes. */ + targetm.target_option.override (); + + /* Rebuild target option node for caller fndecl and replace + with it if the node changes. */ + tree new_target_opt + = build_target_option_node (&global_options, &global_options_set); + if (cur_target_opt != new_target_opt) + DECL_FUNCTION_SPECIFIC_TARGET (to->decl) = new_target_opt; + + /* Restore the context with previous saved nodes. */ + if (old_optimize != new_optimize) + cl_optimization_restore (&global_options, &global_options_set, + TREE_OPTIMIZATION (old_optimize)); + cl_target_option_restore (&global_options, &global_options_set, + TREE_TARGET_OPTION (old_target_opt)); + } + + if (DECL_STRUCT_FUNCTION (to->decl) == cfun) + set_cfun (cfun, true); + } /* If aliases are involved, redirect edge to the actual destination and possibly remove the aliases. */ diff --git a/gcc/testsuite/gcc.dg/lto/pr105459_0.c b/gcc/testsuite/gcc.dg/lto/pr105459_0.c new file mode 100644 index 00000000000..c799e6ef23d --- /dev/null +++ b/gcc/testsuite/gcc.dg/lto/pr105459_0.c @@ -0,0 +1,35 @@ +/* { dg-lto-do link } */ +/* { dg-lto-options { { -flto -O1 } } } */ + +double m; +int n; + +__attribute__ ((optimize ("-funsafe-math-optimizations"))) +void +bar (int x) +{ + n = x; + m = n; +} + +__attribute__ ((flatten)) +void +foo (int x) +{ + bar (x); +} + +void +quux (void) +{ + ++n; +} + +int +main (void) +{ + foo (0); + quux (); + + return 0; +}