https://gcc.gnu.org/g:94816c640adf33bb25c79b9a0d5a74d35724b650
commit r15-9051-g94816c640adf33bb25c79b9a0d5a74d35724b650 Author: Jakub Jelinek <ja...@redhat.com> Date: Mon Mar 31 07:51:04 2025 +0200 c++: Honor noipa attribute for FE nothrow discovery [PR119518] The following testcase has different code generation in bar depending on whether foo is defined or just declared. That is undesirable when it has noipa attribute, that attribute is documented to be a black box between caller and callee, so the caller shouldn't know about any implicitly determined properties of the callee and callee shouldn't know about its callers. E.g. the ipa-pure-const passes including nothrow discovery in there all honor noipa attribute, but the FE did not. 2025-03-31 Jakub Jelinek <ja...@redhat.com> PR c++/119518 * decl.cc (finish_function): Don't set TREE_NOTHROW for functions with "noipa" attribute even when we can prove they can't throw. * g++.dg/opt/pr119518.C: New test. Diff: --- gcc/cp/decl.cc | 3 ++- gcc/testsuite/g++.dg/opt/pr119518.C | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 7d10b228ec62..2ed94fd786ce 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -19454,7 +19454,8 @@ finish_function (bool inline_p) && !cp_function_chain->can_throw && !flag_non_call_exceptions && !decl_replaceable_p (fndecl, - opt_for_fn (fndecl, flag_semantic_interposition))) + opt_for_fn (fndecl, flag_semantic_interposition)) + && !lookup_attribute ("noipa", DECL_ATTRIBUTES (fndecl))) TREE_NOTHROW (fndecl) = 1; cleanup: diff --git a/gcc/testsuite/g++.dg/opt/pr119518.C b/gcc/testsuite/g++.dg/opt/pr119518.C new file mode 100644 index 000000000000..152b8806fdc3 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr119518.C @@ -0,0 +1,20 @@ +// PR c++/119518 +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-optimized" } +// { dg-final { scan-tree-dump "S::~S \\\(&s\\\)" "optimized" } } + +[[gnu::noipa, noreturn]] void +foo () +{ + for (;;) + ; +} + +struct S { ~S (); }; + +void +bar () +{ + S s; + foo (); +}