From 94816c640adf33bb25c79b9a0d5a74d35724b650 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 31 Mar 2025 07:51:04 +0200 Subject: [PATCH] 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 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. --- gcc/cp/decl.cc | 3 ++- gcc/testsuite/g++.dg/opt/pr119518.C | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr119518.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 7d10b228ec6..2ed94fd786c 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 00000000000..152b8806fdc --- /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 (); +} -- 2.47.2