https://gcc.gnu.org/g:f9edc5d5991e605314645c6c59401772dae1849e

commit r16-5152-gf9edc5d5991e605314645c6c59401772dae1849e
Author: Lulu Cheng <[email protected]>
Date:   Sat Sep 20 09:22:32 2025 +0800

    LoongArch: Implement TARGET_MANGLE_DECL_ASSEMBLER_NAME.
    
    Implements TARGET_MANGLE_DECL_ASSEMBLER_NAME for LoongArch.
    This is used to add function multiversioning suffixes to the assembler
    name.
    
    gcc/ChangeLog:
    
            * config/loongarch/loongarch.cc (INCLUDE_STRING): Include.
            (loongarch_mangle_decl_assembler_name): New function.
            (TARGET_MANGLE_DECL_ASSEMBLER_NAME): Define.

Diff:
---
 gcc/config/loongarch/loongarch.cc | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/gcc/config/loongarch/loongarch.cc 
b/gcc/config/loongarch/loongarch.cc
index d74537f91461..1e5ce54e83a0 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 
 #define IN_TARGET_CODE 1
 
+#define INCLUDE_STRING
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
@@ -11597,6 +11598,41 @@ loongarch_get_function_versions_dispatcher (void *decl)
   return dispatch_decl;
 }
 
+/* Implement TARGET_MANGLE_DECL_ASSEMBLER_NAME, to add function multiversioning
+   suffixes.  */
+
+tree
+loongarch_mangle_decl_assembler_name (tree decl, tree id)
+{
+  /* For function version, add the target suffix to the assembler name.  */
+  if (TREE_CODE (decl) == FUNCTION_DECL
+      && DECL_FUNCTION_VERSIONED (decl))
+    {
+      std::string name = IDENTIFIER_POINTER (id) + std::string (".");
+      tree target_attr = lookup_attribute ("target_version",
+                                          DECL_ATTRIBUTES (decl));
+
+      if (target_attr == NULL_TREE)
+       {
+         name += "default";
+         return get_identifier (name.c_str ());
+       }
+
+      const char *version_string
+       = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (target_attr)));
+
+      /* Replace non-alphanumeric characters with underscores as the suffix.  
*/
+      for (const char *c = version_string; *c; c++)
+       name += ISALNUM (*c) == 0 ? '_' : *c;
+
+      if (DECL_ASSEMBLER_NAME_SET_P (decl))
+       SET_DECL_RTL (decl, NULL);
+
+      id = get_identifier (name.c_str ());
+    }
+  return id;
+}
+
 /* Initialize the GCC target structure.  */
 #undef TARGET_ASM_ALIGNED_HI_OP
 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
@@ -11892,6 +11928,10 @@ loongarch_get_function_versions_dispatcher (void *decl)
 #define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER \
   loongarch_get_function_versions_dispatcher
 
+#undef TARGET_MANGLE_DECL_ASSEMBLER_NAME
+#define TARGET_MANGLE_DECL_ASSEMBLER_NAME \
+  loongarch_mangle_decl_assembler_name
+
 struct gcc_target targetm = TARGET_INITIALIZER;
 
 #include "gt-loongarch.h"

Reply via email to