public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-8115] c++: local function versioning [PR104669]
Date: Tue, 12 Apr 2022 21:09:36 +0000 (GMT)	[thread overview]
Message-ID: <20220412210936.8DBE43858025@sourceware.org> (raw)

https://gcc.gnu.org/g:791a968630b3846b614a435b9a75a52f29147a08

commit r12-8115-g791a968630b3846b614a435b9a75a52f29147a08
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 12 16:40:14 2022 -0400

    c++: local function versioning [PR104669]
    
    There were two problems with this testcase: we weren't copying the target
    attribute from the second declaration to the global alias for the first
    one (duplicate_decls hunk), and then we were treating the third one as
    matching the earlier one even though both are versioned (decls_match hunk).
    The latter change required a fix to find_last_decl (used for attribute
    mismatch warnings) to give up if we see a versioned function, as in that
    case we can't determine whether the decls match, because we are still in the
    process of setting the attributes on the new decl.
    
            PR c++/104669
    
    gcc/cp/ChangeLog:
    
            * decl.cc (decls_match): Compare versions even if not recording.
            (duplicate_decls): Propagate attributes to alias.
            * decl2.cc (find_last_decl): Give up if versioned.
    
    gcc/testsuite/ChangeLog:
    
            * g++.target/i386/mv31.C: New test.

Diff:
---
 gcc/cp/decl.cc                       | 20 ++++++++++++++------
 gcc/cp/decl2.cc                      | 12 ++++++++++--
 gcc/testsuite/g++.target/i386/mv31.C | 10 ++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 31cae4d1d36..5f59612fb00 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -1069,11 +1069,14 @@ decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
       if (types_match
 	  && !DECL_EXTERN_C_P (newdecl)
 	  && !DECL_EXTERN_C_P (olddecl)
-	  && record_versions
-	  && maybe_version_functions (newdecl, olddecl,
-				      (!DECL_FUNCTION_VERSIONED (newdecl)
-				       || !DECL_FUNCTION_VERSIONED (olddecl))))
-	return 0;
+	  && targetm.target_option.function_versions (newdecl, olddecl))
+	{
+	  if (record_versions)
+	    maybe_version_functions (newdecl, olddecl,
+				     (!DECL_FUNCTION_VERSIONED (newdecl)
+				      || !DECL_FUNCTION_VERSIONED (olddecl)));
+	  return 0;
+	}
     }
   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
     {
@@ -2598,7 +2601,12 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
       else
 	{
 	  retrofit_lang_decl (newdecl);
-	  DECL_LOCAL_DECL_ALIAS (newdecl) = DECL_LOCAL_DECL_ALIAS (olddecl);
+	  tree alias = DECL_LOCAL_DECL_ALIAS (newdecl)
+	    = DECL_LOCAL_DECL_ALIAS (olddecl);
+	  DECL_ATTRIBUTES (alias)
+	    = (*targetm.merge_decl_attributes) (alias, newdecl);
+	  if (TREE_CODE (newdecl) == FUNCTION_DECL)
+	    merge_attribute_bits (newdecl, alias);
 	}
     }
 
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index c780702572d..e10331f8568 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -1637,8 +1637,16 @@ find_last_decl (tree decl)
 	  if (TREE_CODE (*iter) == OVERLOAD)
 	    continue;
 
-	  if (decls_match (decl, *iter, /*record_decls=*/false))
-	    return *iter;
+	  tree d = *iter;
+
+	  /* We can't compare versions in the middle of processing the
+	     attribute that has the version.  */
+	  if (TREE_CODE (d) == FUNCTION_DECL
+	      && DECL_FUNCTION_VERSIONED (d))
+	    return NULL_TREE;
+
+	  if (decls_match (decl, d, /*record_decls=*/false))
+	    return d;
 	}
       return NULL_TREE;
     }
diff --git a/gcc/testsuite/g++.target/i386/mv31.C b/gcc/testsuite/g++.target/i386/mv31.C
new file mode 100644
index 00000000000..5d8fd1ddf75
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/mv31.C
@@ -0,0 +1,10 @@
+// PR c++/104669
+
+void bar()
+{
+  int foo(void);
+  int foo(void) __attribute__((target("sse")));
+  int foo(void) __attribute__((target("default")));
+  int (*p)(void) = &foo;
+  return;
+}


                 reply	other threads:[~2022-04-12 21:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220412210936.8DBE43858025@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).