* Use i386/crtfastmath.c on Solaris 2/x86
@ 2011-05-31 14:01 Rainer Orth
2011-05-31 14:23 ` Uros Bizjak
0 siblings, 1 reply; 7+ messages in thread
From: Rainer Orth @ 2011-05-31 14:01 UTC (permalink / raw)
To: gcc-patches; +Cc: Uros Bizjak
I had long meant to support -fast-math on Solaris 2/x86. While working
on the Solaris toplevel libgcc move, I've done it with the following
patch.
The only complication is that I need to make sure that SSE insns are only
used if the host supports them.
Bootstrapped without regressions on i386-pc-solaris2.8,
i386-pc-solaris2.9, i386-pc-solaris2.11, and sparc-sun-solaris2.11.
The libgcc part depends on the toplevel libgcc patch, so actually
applying this patch will have to wait until that one is in.
Ok for mainline?
Rainer
2011-05-28 Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
gcc:
* config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]:
Include <signal.h>, <ucontext.h>.
(sigill_caught): Define.
(sigill_hdlr): New function.
(set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Check if SSE
insns can be executed.
* config/sol2.h (ENDFILE_SPEC): Use crtfastmath.o if -ffast-math
etc.
* config/sparc/sol2.h (ENDFILE_SPEC): Remove.
libgcc:
* config.host (i[34567]86-*-solaris2*): Add i386/t-crtfm to
tmake_file.
Add crtfastmath.o to extra_parts.
diff --git a/gcc/config/i386/crtfastmath.c b/gcc/config/i386/crtfastmath.c
--- a/gcc/config/i386/crtfastmath.c
+++ b/gcc/config/i386/crtfastmath.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2007, 2009, 2011 Free Software Foundation, Inc.
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -30,6 +30,26 @@
#include "cpuid.h"
#endif
+#if !defined __x86_64 && defined __sun__ && defined __svr4__
+#include <signal.h>
+#include <ucontext.h>
+
+static volatile sig_atomic_t sigill_caught;
+
+static void
+sigill_hdlr (int sig __attribute((unused)),
+ siginfo_t *sip __attribute__((unused)),
+ ucontext_t *ucp)
+{
+ sigill_caught = 1;
+ /* Set PC to the instruction after the faulting one to skip over it,
+ otherwise we enter an infinite loop. 4 is the size of the stmxcsr
+ instruction. */
+ ucp->uc_mcontext.gregs[EIP] += 4;
+ setcontext (ucp);
+}
+#endif
+
static void __attribute__((constructor))
#ifndef __x86_64__
/* The i386 ABI only requires 4-byte stack alignment, so this is necessary
@@ -45,6 +65,32 @@ set_fast_math (void)
if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
return;
+#if defined __sun__ && defined __svr4__
+ /* Solaris 2 before Solaris 9 4/04 cannot execute SSE instructions even
+ if the CPU supports them. Programs receive SIGILL instead, so check
+ for that at runtime. */
+
+ if (edx & bit_SSE)
+ {
+ struct sigaction act, oact;
+
+ act.sa_handler = sigill_hdlr;
+ sigemptyset (&act.sa_mask);
+ /* Need to set SA_SIGINFO so a ucontext_t * is passed to the handler. */
+ act.sa_flags = SA_SIGINFO;
+ sigaction (SIGILL, &act, &oact);
+
+ /* We need a single SSE instruction here so the handler can safely skip
+ over it. */
+ __asm__ volatile ("movss %xmm2,%xmm1");
+
+ sigaction (SIGILL, &oact, NULL);
+
+ if (sigill_caught)
+ return;
+ }
+#endif /* __sun__ && __svr4__ */
+
if (edx & bit_SSE)
{
unsigned int mxcsr = __builtin_ia32_stmxcsr ();
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -141,7 +141,9 @@ along with GCC; see the file COPYING3.
%{p|pg:-ldl} -lc}"
#undef ENDFILE_SPEC
-#define ENDFILE_SPEC "crtend.o%s crtn.o%s"
+#define ENDFILE_SPEC \
+ "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
+ crtend.o%s crtn.o%s"
/* We don't use the standard svr4 STARTFILE_SPEC because it's wrong for us. */
#undef STARTFILE_SPEC
diff --git a/gcc/config/sparc/sol2.h b/gcc/config/sparc/sol2.h
--- a/gcc/config/sparc/sol2.h
+++ b/gcc/config/sparc/sol2.h
@@ -117,11 +117,6 @@ along with GCC; see the file COPYING3.
#define NO_DBX_BNSYM_ENSYM 1
\f
-#undef ENDFILE_SPEC
-#define ENDFILE_SPEC \
- "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
- crtend.o%s crtn.o%s"
-
/* Select a format to encode pointers in exception handling data. CODE
is 0 for data, 1 for code labels, 2 for function pointers. GLOBAL is
true if the symbol may be affected by dynamic relocations.
diff --git a/libgcc/config.host b/libgcc/config.host
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -338,6 +338,8 @@ i[34567]86-*-rtems*)
tmake_file="${tmake_file} t-crtin i386/t-softfp i386/t-crtstuff t-rtems"
;;
i[34567]86-*-solaris2*)
+ tmake_file="$tmake_file i386/t-crtfm"
+ extra_parts="$extra_parts crtfastmath.o"
;;
i[4567]86-wrs-vxworks|i[4567]86-wrs-vxworksae)
;;
--
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-05-31 14:01 Use i386/crtfastmath.c on Solaris 2/x86 Rainer Orth
@ 2011-05-31 14:23 ` Uros Bizjak
2011-06-01 14:51 ` Rainer Orth
0 siblings, 1 reply; 7+ messages in thread
From: Uros Bizjak @ 2011-05-31 14:23 UTC (permalink / raw)
To: Rainer Orth; +Cc: gcc-patches
On Tue, May 31, 2011 at 2:25 PM, Rainer Orth
<ro@cebitec.uni-bielefeld.de> wrote:
> The only complication is that I need to make sure that SSE insns are only
> used if the host supports them.
>
> Bootstrapped without regressions on i386-pc-solaris2.8,
> i386-pc-solaris2.9, i386-pc-solaris2.11, and sparc-sun-solaris2.11.
>
> The libgcc part depends on the toplevel libgcc patch, so actually
> applying this patch will have to wait until that one is in.
>
> Ok for mainline?
>
> Rainer
>
>
> 2011-05-28 Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
>
> gcc:
> * config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]:
> Include <signal.h>, <ucontext.h>.
> (sigill_caught): Define.
> (sigill_hdlr): New function.
> (set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Check if SSE
> insns can be executed.
> * config/sol2.h (ENDFILE_SPEC): Use crtfastmath.o if -ffast-math
> etc.
> * config/sparc/sol2.h (ENDFILE_SPEC): Remove.
>
> libgcc:
> * config.host (i[34567]86-*-solaris2*): Add i386/t-crtfm to
> tmake_file.
> Add crtfastmath.o to extra_parts.
Please just put "if (edx & bit_SSE)" part inside existing check. You
will need to split assignment of mxcsr from the declaration, though.
OK with this change.
Thanks,
Uros.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-05-31 14:23 ` Uros Bizjak
@ 2011-06-01 14:51 ` Rainer Orth
2011-06-01 17:24 ` Richard Henderson
0 siblings, 1 reply; 7+ messages in thread
From: Rainer Orth @ 2011-06-01 14:51 UTC (permalink / raw)
To: Uros Bizjak; +Cc: gcc-patches
Uros Bizjak <ubizjak@gmail.com> writes:
> Please just put "if (edx & bit_SSE)" part inside existing check. You
> will need to split assignment of mxcsr from the declaration, though.
>
> OK with this change.
Here's the patch I've actually comitted after a quick bootstrap on
i386-pc-solaris2.10.
Thanks.
Rainer
2011-05-28 Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
gcc:
* config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]:
Include <signal.h>, <ucontext.h>.
(sigill_caught): Define.
(sigill_hdlr): New function.
(set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Check if SSE
insns can be executed.
* config/sol2.h (ENDFILE_SPEC): Use crtfastmath.o if -ffast-math
etc.
* config/sparc/sol2.h (ENDFILE_SPEC): Remove.
libgcc:
* config.host (i[34567]86-*-solaris2*): Add i386/t-crtfm to
tmake_file.
Add crtfastmath.o to extra_parts.
diff --git a/gcc/config/i386/crtfastmath.c b/gcc/config/i386/crtfastmath.c
--- a/gcc/config/i386/crtfastmath.c
+++ b/gcc/config/i386/crtfastmath.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
+ * Copyright (C) 2005, 2007, 2009, 2011 Free Software Foundation, Inc.
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@@ -30,6 +30,26 @@
#include "cpuid.h"
#endif
+#if !defined __x86_64 && defined __sun__ && defined __svr4__
+#include <signal.h>
+#include <ucontext.h>
+
+static volatile sig_atomic_t sigill_caught;
+
+static void
+sigill_hdlr (int sig __attribute((unused)),
+ siginfo_t *sip __attribute__((unused)),
+ ucontext_t *ucp)
+{
+ sigill_caught = 1;
+ /* Set PC to the instruction after the faulting one to skip over it,
+ otherwise we enter an infinite loop. 4 is the size of the stmxcsr
+ instruction. */
+ ucp->uc_mcontext.gregs[EIP] += 4;
+ setcontext (ucp);
+}
+#endif
+
static void __attribute__((constructor))
#ifndef __x86_64__
/* The i386 ABI only requires 4-byte stack alignment, so this is necessary
@@ -47,9 +67,31 @@ set_fast_math (void)
if (edx & bit_SSE)
{
- unsigned int mxcsr = __builtin_ia32_stmxcsr ();
+ unsigned int mxcsr;
- mxcsr |= MXCSR_FTZ;
+#if defined __sun__ && defined __svr4__
+ /* Solaris 2 before Solaris 9 4/04 cannot execute SSE instructions even
+ if the CPU supports them. Programs receive SIGILL instead, so check
+ for that at runtime. */
+ struct sigaction act, oact;
+
+ act.sa_handler = sigill_hdlr;
+ sigemptyset (&act.sa_mask);
+ /* Need to set SA_SIGINFO so a ucontext_t * is passed to the handler. */
+ act.sa_flags = SA_SIGINFO;
+ sigaction (SIGILL, &act, &oact);
+
+ /* We need a single SSE instruction here so the handler can safely skip
+ over it. */
+ __asm__ volatile ("movss %xmm2,%xmm1");
+
+ sigaction (SIGILL, &oact, NULL);
+
+ if (sigill_caught)
+ return;
+#endif /* __sun__ && __svr4__ */
+
+ mxcsr = __builtin_ia32_stmxcsr () | MXCSR_FTZ;
if (edx & bit_FXSAVE)
{
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -141,7 +141,9 @@ along with GCC; see the file COPYING3.
%{p|pg:-ldl} -lc}"
#undef ENDFILE_SPEC
-#define ENDFILE_SPEC "crtend.o%s crtn.o%s"
+#define ENDFILE_SPEC \
+ "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
+ crtend.o%s crtn.o%s"
/* We don't use the standard svr4 STARTFILE_SPEC because it's wrong for us. */
#undef STARTFILE_SPEC
diff --git a/gcc/config/sparc/sol2.h b/gcc/config/sparc/sol2.h
--- a/gcc/config/sparc/sol2.h
+++ b/gcc/config/sparc/sol2.h
@@ -117,11 +117,6 @@ along with GCC; see the file COPYING3.
#define NO_DBX_BNSYM_ENSYM 1
\f
-#undef ENDFILE_SPEC
-#define ENDFILE_SPEC \
- "%{Ofast|ffast-math|funsafe-math-optimizations:crtfastmath.o%s} \
- crtend.o%s crtn.o%s"
-
/* Select a format to encode pointers in exception handling data. CODE
is 0 for data, 1 for code labels, 2 for function pointers. GLOBAL is
true if the symbol may be affected by dynamic relocations.
diff --git a/libgcc/config.host b/libgcc/config.host
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -338,6 +338,8 @@ i[34567]86-*-rtems*)
tmake_file="${tmake_file} t-crtin i386/t-softfp i386/t-crtstuff t-rtems"
;;
i[34567]86-*-solaris2*)
+ tmake_file="$tmake_file i386/t-crtfm"
+ extra_parts="$extra_parts crtfastmath.o"
;;
i[4567]86-wrs-vxworks|i[4567]86-wrs-vxworksae)
;;
--
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-06-01 14:51 ` Rainer Orth
@ 2011-06-01 17:24 ` Richard Henderson
2011-06-01 17:29 ` Rainer Orth
2011-06-03 17:39 ` Rainer Orth
0 siblings, 2 replies; 7+ messages in thread
From: Richard Henderson @ 2011-06-01 17:24 UTC (permalink / raw)
To: Rainer Orth; +Cc: Uros Bizjak, gcc-patches
On 06/01/2011 07:51 AM, Rainer Orth wrote:
> + /* Set PC to the instruction after the faulting one to skip over it,
> + otherwise we enter an infinite loop. 4 is the size of the stmxcsr
> + instruction. */
...
> + /* We need a single SSE instruction here so the handler can safely skip
> + over it. */
> + __asm__ volatile ("movss %xmm2,%xmm1");
The comment referencing stmxcsr doesn't match the movss code.
It's still a 4 byte opcode, so the code still works.
I do wonder if using "movaps %xmm0,%xmm0" might be cleaner,
to avoid clobbering a register, even if that register is
surely dead anyway. That's a 3 byte opcode though, so the
handler would need updating.
r~
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-06-01 17:24 ` Richard Henderson
@ 2011-06-01 17:29 ` Rainer Orth
2011-06-01 18:06 ` Richard Henderson
2011-06-03 17:39 ` Rainer Orth
1 sibling, 1 reply; 7+ messages in thread
From: Rainer Orth @ 2011-06-01 17:29 UTC (permalink / raw)
To: Richard Henderson; +Cc: Uros Bizjak, gcc-patches
Richard Henderson <rth@redhat.com> writes:
> On 06/01/2011 07:51 AM, Rainer Orth wrote:
>> + /* Set PC to the instruction after the faulting one to skip over it,
>> + otherwise we enter an infinite loop. 4 is the size of the stmxcsr
>> + instruction. */
> ...
>> + /* We need a single SSE instruction here so the handler can safely skip
>> + over it. */
>> + __asm__ volatile ("movss %xmm2,%xmm1");
>
> The comment referencing stmxcsr doesn't match the movss code.
> It's still a 4 byte opcode, so the code still works.
Copy-and-paste error ;-( We already have the same code in
libgfortran/config/fpu-387.h and (without the comment) in
gcc/testsuite/lib/target-supports.exp. I still mean to fix
driver-i386.c to correcly handle -march=native on Solaris 8 and 9 which
cannot in general execute SSE insns. I wonder if there's a better place
to share this code?
> I do wonder if using "movaps %xmm0,%xmm0" might be cleaner,
> to avoid clobbering a register, even if that register is
> surely dead anyway. That's a 3 byte opcode though, so the
> handler would need updating.
I'll give it a try.
Thanks.
Rainer
--
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-06-01 17:29 ` Rainer Orth
@ 2011-06-01 18:06 ` Richard Henderson
0 siblings, 0 replies; 7+ messages in thread
From: Richard Henderson @ 2011-06-01 18:06 UTC (permalink / raw)
To: Rainer Orth; +Cc: Uros Bizjak, gcc-patches
On 06/01/2011 10:29 AM, Rainer Orth wrote:
> I still mean to fix
> driver-i386.c to correcly handle -march=native on Solaris 8 and 9 which
> cannot in general execute SSE insns. I wonder if there's a better place
> to share this code?
I can't think of a good place. :-(
r~
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Use i386/crtfastmath.c on Solaris 2/x86
2011-06-01 17:24 ` Richard Henderson
2011-06-01 17:29 ` Rainer Orth
@ 2011-06-03 17:39 ` Rainer Orth
1 sibling, 0 replies; 7+ messages in thread
From: Rainer Orth @ 2011-06-03 17:39 UTC (permalink / raw)
To: Richard Henderson; +Cc: Uros Bizjak, gcc-patches, fortran
Richard Henderson <rth@redhat.com> writes:
> On 06/01/2011 07:51 AM, Rainer Orth wrote:
>> + /* Set PC to the instruction after the faulting one to skip over it,
>> + otherwise we enter an infinite loop. 4 is the size of the stmxcsr
>> + instruction. */
> ...
>> + /* We need a single SSE instruction here so the handler can safely skip
>> + over it. */
>> + __asm__ volatile ("movss %xmm2,%xmm1");
>
> The comment referencing stmxcsr doesn't match the movss code.
> It's still a 4 byte opcode, so the code still works.
>
> I do wonder if using "movaps %xmm0,%xmm0" might be cleaner,
> to avoid clobbering a register, even if that register is
> surely dead anyway. That's a 3 byte opcode though, so the
> handler would need updating.
Here's what I installed after regtesting completed on
i386-pc-solaris2.{8, 11}.
Thanks.
Rainer
2011-06-02 Richard Henderson <rth@redhat.com>
Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>
gcc:
* config/i386/crtfastmath.c [!__x86_64__ && __sun__ && __svr4__]
(sigill_hdlr): Correct insn, insn size.
(set_fast_math) [!__x86_64__ && __sun__ && __svr4__]: Use movaps.
gcc/testsuite:
* lib/target-supports.exp (check_sse_os_support_available): Use
movaps.
libgfortran.
* config/fpu-387.h [__sun__ && __svr4__] (sigill_hdlr): Correct
insn, insn size.
(has_sse) [!__x86_64__ && __sun__ && __svr4__]: Use movaps.
diff --git a/gcc/config/i386/crtfastmath.c b/gcc/config/i386/crtfastmath.c
--- a/gcc/config/i386/crtfastmath.c
+++ b/gcc/config/i386/crtfastmath.c
@@ -30,7 +30,7 @@
#include "cpuid.h"
#endif
-#if !defined __x86_64 && defined __sun__ && defined __svr4__
+#if !defined __x86_64__ && defined __sun__ && defined __svr4__
#include <signal.h>
#include <ucontext.h>
@@ -43,9 +43,9 @@ sigill_hdlr (int sig __attribute((unused
{
sigill_caught = 1;
/* Set PC to the instruction after the faulting one to skip over it,
- otherwise we enter an infinite loop. 4 is the size of the stmxcsr
+ otherwise we enter an infinite loop. 3 is the size of the movaps
instruction. */
- ucp->uc_mcontext.gregs[EIP] += 4;
+ ucp->uc_mcontext.gregs[EIP] += 3;
setcontext (ucp);
}
#endif
@@ -83,7 +83,7 @@ set_fast_math (void)
/* We need a single SSE instruction here so the handler can safely skip
over it. */
- __asm__ volatile ("movss %xmm2,%xmm1");
+ __asm__ volatile ("movaps %xmm0,%xmm0");
sigaction (SIGILL, &oact, NULL);
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -1059,7 +1059,7 @@ proc check_sse_os_support_available { }
check_runtime_nocache sse_os_support_available {
int main ()
{
- __asm__ volatile ("movss %xmm2,%xmm1");
+ __asm__ volatile ("movaps %xmm0,%xmm0");
return 0;
}
} "-msse"
diff --git a/libgfortran/config/fpu-387.h b/libgfortran/config/fpu-387.h
--- a/libgfortran/config/fpu-387.h
+++ b/libgfortran/config/fpu-387.h
@@ -1,5 +1,5 @@
/* FPU-related code for x86 and x86_64 processors.
- Copyright 2005, 2007, 2009, 2010 Free Software Foundation, Inc.
+ Copyright 2005, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
Contributed by Francois-Xavier Coudert <coudert@clipper.ens.fr>
This file is part of the GNU Fortran 95 runtime library (libgfortran).
@@ -40,9 +40,9 @@ sigill_hdlr (int sig __attribute((unused
{
sigill_caught = 1;
/* Set PC to the instruction after the faulting one to skip over it,
- otherwise we enter an infinite loop. 4 is the size of the stmxcsr
+ otherwise we enter an infinite loop. 3 is the size of the movaps
instruction. */
- ucp->uc_mcontext.gregs[EIP] += 4;
+ ucp->uc_mcontext.gregs[EIP] += 3;
setcontext (ucp);
}
#endif
@@ -73,7 +73,7 @@ has_sse (void)
/* We need a single SSE instruction here so the handler can safely skip
over it. */
- __asm__ volatile ("movss %xmm2,%xmm1");
+ __asm__ volatile ("movaps %xmm0,%xmm0");
sigaction (SIGILL, &oact, NULL);
--
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2011-06-03 17:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-31 14:01 Use i386/crtfastmath.c on Solaris 2/x86 Rainer Orth
2011-05-31 14:23 ` Uros Bizjak
2011-06-01 14:51 ` Rainer Orth
2011-06-01 17:24 ` Richard Henderson
2011-06-01 17:29 ` Rainer Orth
2011-06-01 18:06 ` Richard Henderson
2011-06-03 17:39 ` Rainer Orth
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).