]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
x86: Disallow -mtls-dialect=gnu with no_caller_saved_registers
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 24 Jul 2025 14:38:13 +0000 (07:38 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Mon, 28 Jul 2025 15:05:58 +0000 (08:05 -0700)
__tls_get_addr doesn't preserve vector registers.  When a function
with no_caller_saved_registers attribute calls __tls_get_addr, YMM
and ZMM registers will be clobbered.  Issue an error and suggest
-mtls-dialect=gnu2 in this case.

gcc/

PR target/121208
* config/i386/i386.cc (ix86_tls_get_addr): Issue an error for
-mtls-dialect=gnu with no_caller_saved_registers attribute and
suggest -mtls-dialect=gnu2.

gcc/testsuite/

PR target/121208
* gcc.target/i386/pr121208-1a.c: New test.
* gcc.target/i386/pr121208-1b.c: Likewise.
* gcc.target/i386/pr121208-2a.c: Likewise.
* gcc.target/i386/pr121208-2b.c: Likewise.
* gcc.target/i386/pr121208-3a.c: Likewise.
* gcc.target/i386/pr121208-3b.c: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
gcc/config/i386/i386.cc
gcc/testsuite/gcc.target/i386/pr121208-1a.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr121208-1b.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr121208-2a.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr121208-2b.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr121208-3a.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr121208-3b.c [new file with mode: 0644]

index 590cdf1c3605b0a6810380e64cbc012896ae08b7..0f0acae0e6478f2608a6cb19a9ed54ff593c18dd 100644 (file)
@@ -12442,6 +12442,28 @@ static GTY(()) rtx ix86_tls_symbol;
 static rtx
 ix86_tls_get_addr (void)
 {
+  if (cfun->machine->call_saved_registers
+      == TYPE_NO_CALLER_SAVED_REGISTERS)
+    {
+      /* __tls_get_addr doesn't preserve vector registers.  When a
+        function with no_caller_saved_registers attribute calls
+        __tls_get_addr, YMM and ZMM registers will be clobbered.
+        Issue an error and suggest -mtls-dialect=gnu2 in this case.  */
+      if (cfun->machine->func_type == TYPE_NORMAL)
+       error (G_("%<-mtls-dialect=gnu2%> must be used with a function"
+                 " with the %<no_caller_saved_registers%> attribute"));
+      else
+       error (cfun->machine->func_type == TYPE_EXCEPTION
+              ? G_("%<-mtls-dialect=gnu2%> must be used with an"
+                   " exception service routine")
+              : G_("%<-mtls-dialect=gnu2%> must be used with an"
+                   " interrupt service routine"));
+      /* Don't issue the same error twice.  */
+      cfun->machine->func_type = TYPE_NORMAL;
+      cfun->machine->call_saved_registers
+       = TYPE_DEFAULT_CALL_SAVED_REGISTERS;
+    }
+
   if (!ix86_tls_symbol)
     {
       const char *sym
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-1a.c b/gcc/testsuite/gcc.target/i386/pr121208-1a.c
new file mode 100644 (file)
index 0000000..ac851cb
--- /dev/null
@@ -0,0 +1,15 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu" } */
+
+extern __thread int bar;
+extern void func (void);
+
+__attribute__((no_caller_saved_registers))
+void
+foo (int error)
+{
+  bar = 1; /* { dg-error -mtls-dialect=gnu2 } */
+  if (error == 0)
+    func ();
+  bar = 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-1b.c b/gcc/testsuite/gcc.target/i386/pr121208-1b.c
new file mode 100644 (file)
index 0000000..b97ac71
--- /dev/null
@@ -0,0 +1,4 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu2" } */
+
+#include "pr121208-1a.c"
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-2a.c b/gcc/testsuite/gcc.target/i386/pr121208-2a.c
new file mode 100644 (file)
index 0000000..c1891ae
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu" } */
+
+typedef unsigned int uword_t __attribute__ ((mode (__word__)));
+extern __thread int bar;
+extern void func (void);
+
+__attribute__((target("general-regs-only")))
+__attribute__((interrupt))
+void
+foo (void *frame, uword_t error)
+{
+  bar = 1; /* { dg-error -mtls-dialect=gnu2 } */
+  if (error == 0)
+    func ();
+  bar = 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-2b.c b/gcc/testsuite/gcc.target/i386/pr121208-2b.c
new file mode 100644 (file)
index 0000000..269b120
--- /dev/null
@@ -0,0 +1,4 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu2" } */
+
+#include "pr121208-2a.c"
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-3a.c b/gcc/testsuite/gcc.target/i386/pr121208-3a.c
new file mode 100644 (file)
index 0000000..26fe687
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu" } */
+
+typedef unsigned int uword_t __attribute__ ((mode (__word__)));
+extern __thread int bar;
+extern void func (void);
+
+__attribute__((target("general-regs-only")))
+__attribute__((interrupt))
+void
+foo (void *frame)
+{
+  bar = 1; /* { dg-error -mtls-dialect=gnu2 } */
+  if (frame == 0)
+    func ();
+  bar = 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/pr121208-3b.c b/gcc/testsuite/gcc.target/i386/pr121208-3b.c
new file mode 100644 (file)
index 0000000..b672d75
--- /dev/null
@@ -0,0 +1,4 @@
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O2 -fPIC -mtls-dialect=gnu2" } */
+
+#include "pr121208-3a.c"