]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: Enable switchable target
authorYang Yujie <yangyujie@loongson.cn>
Mon, 8 Apr 2024 08:45:13 +0000 (16:45 +0800)
committerLulu Cheng <chenglulu@loongson.cn>
Tue, 9 Apr 2024 09:03:15 +0000 (17:03 +0800)
This patch fixes the back-end context switching in cases where functions
should be built with their own target contexts instead of the
global one, such as LTO linking and functions with target attributes (TBD).

PR target/113233

gcc/ChangeLog:

* config/loongarch/loongarch.cc (loongarch_reg_init):
Reinitialize the loongarch_regno_mode_ok cache.
(loongarch_option_override): Same.
(loongarch_save_restore_target_globals): Restore target globals.
(loongarch_set_current_function): Restore the target contexts
for functions.
(TARGET_SET_CURRENT_FUNCTION): Define.
* config/loongarch/loongarch.h (SWITCHABLE_TARGET): Enable
switchable target context.
* config/loongarch/loongarch-builtins.cc (loongarch_init_builtins):
Initialize all builtin functions at startup.
(loongarch_expand_builtin): Turn assertion of builtin availability
into a test.

gcc/testsuite/ChangeLog:

* lib/target-supports.exp: Define condition loongarch_sx_as.
* gcc.dg/lto/pr113233_0.c: New test.

gcc/config/loongarch/loongarch-builtins.cc
gcc/config/loongarch/loongarch.cc
gcc/config/loongarch/loongarch.h
gcc/testsuite/gcc.dg/lto/pr113233_0.c [new file with mode: 0644]
gcc/testsuite/lib/target-supports.exp

index efe7e5e5ebc93c8ae6df3ca6567777956d3e9cf7..fbe46833c9b7d1b56c62cd3c69e28fb472173d45 100644 (file)
@@ -2512,14 +2512,11 @@ loongarch_init_builtins (void)
   for (i = 0; i < ARRAY_SIZE (loongarch_builtins); i++)
     {
       d = &loongarch_builtins[i];
-      if (d->avail ())
-       {
-         type = loongarch_build_function_type (d->function_type);
-         loongarch_builtin_decls[i]
-           = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL,
-                                   NULL);
-         loongarch_get_builtin_decl_index[d->icode] = i;
-       }
+      type = loongarch_build_function_type (d->function_type);
+      loongarch_builtin_decls[i]
+       = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL,
+                         NULL);
+      loongarch_get_builtin_decl_index[d->icode] = i;
     }
 }
 
@@ -3105,15 +3102,21 @@ loongarch_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
                          int ignore ATTRIBUTE_UNUSED)
 {
   tree fndecl;
-  unsigned int fcode, avail;
+  unsigned int fcode;
   const struct loongarch_builtin_description *d;
 
   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
   fcode = DECL_MD_FUNCTION_CODE (fndecl);
   gcc_assert (fcode < ARRAY_SIZE (loongarch_builtins));
   d = &loongarch_builtins[fcode];
-  avail = d->avail ();
-  gcc_assert (avail != 0);
+
+  if (!d->avail ())
+    {
+      error_at (EXPR_LOCATION (exp),
+               "built-in function %qD is not enabled", fndecl);
+      return target;
+    }
+
   switch (d->builtin_type)
     {
     case LARCH_BUILTIN_DIRECT:
index c90b701a5336d2c8f3d86e618c78177ea9ebd2e7..6b92e7034c56895796e0e7d442ec958945b02661 100644 (file)
@@ -7570,15 +7570,19 @@ loongarch_global_init (void)
        loongarch_dwarf_regno[i] = INVALID_REGNUM;
     }
 
+  /* Function to allocate machine-dependent function status.  */
+  init_machine_status = &loongarch_init_machine_status;
+};
+
+static void
+loongarch_reg_init (void)
+{
   /* Set up loongarch_hard_regno_mode_ok.  */
   for (int mode = 0; mode < MAX_MACHINE_MODE; mode++)
     for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
       loongarch_hard_regno_mode_ok_p[mode][regno]
        = loongarch_hard_regno_mode_ok_uncached (regno, (machine_mode) mode);
-
-  /* Function to allocate machine-dependent function status.  */
-  init_machine_status = &loongarch_init_machine_status;
-};
+}
 
 static void
 loongarch_option_override_internal (struct loongarch_target *target,
@@ -7605,20 +7609,92 @@ loongarch_option_override_internal (struct loongarch_target *target,
 
   /* Override some options according to the resolved target.  */
   loongarch_target_option_override (target, opts, opts_set);
+
+  target_option_default_node = target_option_current_node
+    = build_target_option_node (opts, opts_set);
+
+  loongarch_reg_init ();
+}
+
+/* Remember the last target of loongarch_set_current_function.  */
+
+static GTY(()) tree loongarch_previous_fndecl;
+
+/* Restore or save the TREE_TARGET_GLOBALS from or to new_tree.
+   Used by loongarch_set_current_function to
+   make sure optab availability predicates are recomputed when necessary.  */
+
+static void
+loongarch_save_restore_target_globals (tree new_tree)
+{
+  if (TREE_TARGET_GLOBALS (new_tree))
+    restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
+  else if (new_tree == target_option_default_node)
+    restore_target_globals (&default_target_globals);
+  else
+    TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
+}
+
+/* Implement TARGET_SET_CURRENT_FUNCTION.  */
+
+static void
+loongarch_set_current_function (tree fndecl)
+{
+  if (fndecl == loongarch_previous_fndecl)
+    return;
+
+  tree old_tree;
+  if (loongarch_previous_fndecl == NULL_TREE)
+    old_tree = target_option_current_node;
+  else if (DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl))
+    old_tree = DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl);
+  else
+    old_tree = target_option_default_node;
+
+  if (fndecl == NULL_TREE)
+    {
+      if (old_tree != target_option_current_node)
+       {
+         loongarch_previous_fndecl = NULL_TREE;
+         cl_target_option_restore (&global_options, &global_options_set,
+                                   TREE_TARGET_OPTION
+                                   (target_option_current_node));
+       }
+      return;
+    }
+
+  tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
+  if (new_tree == NULL_TREE)
+    new_tree = target_option_default_node;
+
+  loongarch_previous_fndecl = fndecl;
+
+  if (new_tree == old_tree)
+    return;
+
+  cl_target_option_restore (&global_options, &global_options_set,
+                           TREE_TARGET_OPTION (new_tree));
+
+  loongarch_reg_init ();
+
+  loongarch_save_restore_target_globals (new_tree);
 }
 
+
+
 /* Implement TARGET_OPTION_OVERRIDE.  */
 
 static void
 loongarch_option_override (void)
 {
+  /* Global initializations.  */
+  loongarch_global_init ();
+
   /* Setting up the target configuration.  */
   loongarch_option_override_internal (&la_target,
                                      &global_options,
                                      &global_options_set);
 
-  /* Global initializations.  */
-  loongarch_global_init ();
 }
 
 /* Implement TARGET_OPTION_SAVE.  */
@@ -10935,6 +11011,9 @@ loongarch_asm_code_end (void)
 #undef TARGET_OPTION_RESTORE
 #define TARGET_OPTION_RESTORE loongarch_option_restore
 
+#undef TARGET_SET_CURRENT_FUNCTION
+#define TARGET_SET_CURRENT_FUNCTION loongarch_set_current_function
+
 #undef TARGET_LEGITIMIZE_ADDRESS
 #define TARGET_LEGITIMIZE_ADDRESS loongarch_legitimize_address
 
index 23bb32548831cfbf47546418de76f2325b6ad25e..db8a9eb9516632a473b05a4b539a0413e47b5c0f 100644 (file)
@@ -23,6 +23,8 @@ along with GCC; see the file COPYING3.  If not see
 
 #include "config/loongarch/loongarch-opts.h"
 
+#define SWITCHABLE_TARGET 1
+
 #define TARGET_SUPPORTS_WIDE_INT 1
 
 /* Macros to silence warnings about numbers being signed in traditional
diff --git a/gcc/testsuite/gcc.dg/lto/pr113233_0.c b/gcc/testsuite/gcc.dg/lto/pr113233_0.c
new file mode 100644 (file)
index 0000000..0a045c5
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-require-effective-target loongarch_sx_as } */
+/* { dg-lto-do link } */
+/* { dg-skip-if "" { ! { loongarch*-linux-* } } } */
+/* { dg-lto-options { {-mlsx } } } */
+/* { dg-suppress-ld-options { -mlsx } } */
+
+#include <lsxintrin.h>
+
+int main (void)
+{
+  __m128i a, b, c;
+  c = __lsx_vand_v (a, b);
+  return 0;
+} 
index 49f2482686a2a22216b2d48d3ae3906dc6c21532..edce672c0e21a0e184c8265a4cd1ae3e94397c45 100644 (file)
@@ -13413,6 +13413,18 @@ proc check_effective_target_loongarch_sx { } {
     } "-mlsx"]
 }
 
+proc check_effective_target_loongarch_sx_as { } {
+    return [check_no_compiler_messages loongarch_sx_as object {
+        #include <lsxintrin.h>
+        int main (void)
+        {
+          __m128i a, b, c;
+          c = __lsx_vand_v (a, b);
+          return 0;
+        }
+    } "-mlsx"]
+}
+
 proc check_effective_target_loongarch_sx_hw { } {
     return [check_runtime loongarch_sx_hw {
        #include <lsxintrin.h>