public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MIPS: add speculation_barrier support
@ 2023-04-28 12:33 YunQiang Su
  2023-04-28 12:36 ` Jiaxun Yang
  2023-04-28 13:12 ` [PATCH v2] " YunQiang Su
  0 siblings, 2 replies; 18+ messages in thread
From: YunQiang Su @ 2023-04-28 12:33 UTC (permalink / raw)
  To: gcc-patches; +Cc: macro, jiaxun.yang, syq, richard.sandiford, YunQiang Su

speculation_barrier for MIPS needs sync+jr.hb (r2+),
so we implement __speculation_barrier in libgcc, like arm32 does.

gcc/ChangeLog:
	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
        prototype.
	* config/mips/mips.cc (speculation_barrier_libfunc): New static
        variable.
	(mips_init_libfuncs): Initialize it.
	(mips_emit_speculation_barrier): New function.
	* config/arm/arm.md (speculation_barrier): Call
        mips_emit_speculation_barrier.

libgcc/ChangeLog:
	* config/mips/lib1funcs.S: New file.
	define __speculation_barrier and include mips16.S.
	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
	define LIB1ASMFUNCS as _speculation_barrier.
	set version info for __speculation_barrier.
	* config/mips/libgcc-mips.ver: New file.
	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
	included in lib1funcs.S now.
---
 gcc/config/mips/mips-protos.h      |  2 +
 gcc/config/mips/mips.cc            | 13 +++++++
 gcc/config/mips/mips.md            | 12 ++++++
 libgcc/config/mips/lib1funcs.S     | 60 ++++++++++++++++++++++++++++++
 libgcc/config/mips/libgcc-mips.ver | 21 +++++++++++
 libgcc/config/mips/t-mips          |  7 ++++
 libgcc/config/mips/t-mips16        |  3 +-
 7 files changed, 116 insertions(+), 2 deletions(-)
 create mode 100644 libgcc/config/mips/lib1funcs.S
 create mode 100644 libgcc/config/mips/libgcc-mips.ver

diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index 20483469105..da7902c235b 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
 extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
 extern void mips_expand_vec_cmp_expr (rtx *);
 
+extern void mips_emit_speculation_barrier_function (void);
+
 #endif /* ! GCC_MIPS_PROTOS_H */
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index ca822758b41..139707fda34 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
   return 0;
 }
 
+
+static GTY(()) rtx speculation_barrier_libfunc;
+
 /* Implement TARGET_INIT_LIBFUNCS.  */
 
 static void
@@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
       init_sync_libfuncs (UNITS_PER_WORD);
     }
+  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
 }
 
 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
@@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
       }
 }
 
+/* Emit a speculation barrier.
+   JR.HB is needed, so we need to put
+   speculation_barrier_libfunc in libgcc */
+void
+mips_emit_speculation_barrier_function ()
+{
+  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
+}
+
 /* A SEQUENCE is breakable iff the branch inside it has a compact form
    and the target has compact branches.  */
 
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index ac1d77afc7d..5d04ac566dd 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -160,6 +160,8 @@
   ;; The `.insn' pseudo-op.
   UNSPEC_INSN_PSEUDO
   UNSPEC_JRHB
+
+  VUNSPEC_SPECULATION_BARRIER
 ])
 
 (define_constants
@@ -7455,6 +7457,16 @@
   mips_expand_conditional_move (operands);
   DONE;
 })
+
+(define_expand "speculation_barrier"
+  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
+  ""
+  "
+  mips_emit_speculation_barrier_function ();
+  DONE;
+  "
+)
+
 \f
 ;;
 ;;  ....................
diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
new file mode 100644
index 00000000000..45d74e2e762
--- /dev/null
+++ b/libgcc/config/mips/lib1funcs.S
@@ -0,0 +1,60 @@
+/* Copyright (C) 1995-2023 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
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+#include "mips16.S"
+
+#ifdef L_speculation_barrier
+
+/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
+   So, we use normal MIPS code here, just like what we do for __sync_* */
+	.set nomips16
+
+	.set noreorder
+	.globl	__speculation_barrier
+	.ent	__speculation_barrier
+
+__speculation_barrier:
+	.set	push
+/* MIPS1 has no sync, and in fact it doesn't need it at all.
+   We wish that all newer CPUs should run software with MIPS2+ */
+#if __mips >= 2
+	sync /* complementation barrier for memory */
+#endif
+#if __mips_isa_rev >= 2
+/* MIPSr2+: sync+jr.hb is enough */
+	jr.hb	$ra /* Jump with instruction hazard barrier */
+#else
+/* Make ssnop available, ssnop only recognized by GAS since mips32,
+   however it's actually available since R5500,
+   and it will be decoded as nop on earlier processors */
+	.set mips32
+/* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
+   so 8 ssnop is sufficient to block all speculation on all CPUs */
+	.rept 8
+	ssnop
+	.endr
+	jr	$ra
+#endif
+	.set	pop
+	.end	__speculation_barrier
+
+	.set reorder
+#endif
diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
new file mode 100644
index 00000000000..f0e9fc54965
--- /dev/null
+++ b/libgcc/config/mips/libgcc-mips.ver
@@ -0,0 +1,21 @@
+# Copyright (C) 2008-2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+GCC_14.0 {
+  __speculation_barrier
+}
diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
index 4fb8e136217..d05ef7cbf74 100644
--- a/libgcc/config/mips/t-mips
+++ b/libgcc/config/mips/t-mips
@@ -7,3 +7,10 @@ softfp_truncations :=
 softfp_exclude_libgcc2 := n
 
 LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
+
+
+LIB1ASMSRC = mips/lib1funcs.S
+LIB1ASMFUNCS = _speculation_barrier
+
+# Version these symbols if building libgcc.so.
+SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
index 2bad5119d51..5fd9d60d7a3 100644
--- a/libgcc/config/mips/t-mips16
+++ b/libgcc/config/mips/t-mips16
@@ -16,8 +16,7 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-LIB1ASMSRC = mips/mips16.S
-LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
+LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
 	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
 	_m16unordsf2 \
 	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] MIPS: add speculation_barrier support
  2023-04-28 12:33 [PATCH] MIPS: add speculation_barrier support YunQiang Su
@ 2023-04-28 12:36 ` Jiaxun Yang
  2023-04-28 13:07   ` YunQiang Su
  2023-04-28 13:12 ` [PATCH v2] " YunQiang Su
  1 sibling, 1 reply; 18+ messages in thread
From: Jiaxun Yang @ 2023-04-28 12:36 UTC (permalink / raw)
  To: YunQiang Su
  Cc: gcc-patches, Maciej W. Rozycki, YunQiang Su, richard.sandiford



> 2023年4月28日 13:33,YunQiang Su <yunqiang.su@cipunited.com> 写道:
> 
> speculation_barrier for MIPS needs sync+jr.hb (r2+),
> so we implement __speculation_barrier in libgcc, like arm32 does.
> 
> gcc/ChangeLog:
> * config/mips/mips-protos.h (mips_emit_speculation_barrier): New
>        prototype.
> * config/mips/mips.cc (speculation_barrier_libfunc): New static
>        variable.
> (mips_init_libfuncs): Initialize it.
> (mips_emit_speculation_barrier): New function.
> * config/arm/arm.md (speculation_barrier): Call
>        mips_emit_speculation_barrier.

^ arm? Typo.

Thanks
Jiaxun

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] MIPS: add speculation_barrier support
  2023-04-28 12:36 ` Jiaxun Yang
@ 2023-04-28 13:07   ` YunQiang Su
  0 siblings, 0 replies; 18+ messages in thread
From: YunQiang Su @ 2023-04-28 13:07 UTC (permalink / raw)
  To: Jiaxun Yang
  Cc: YunQiang Su, gcc-patches, Maciej W. Rozycki, richard.sandiford

Jiaxun Yang <jiaxun.yang@flygoat.com> 于2023年4月28日周五 20:36写道:
>
>
>
> > 2023年4月28日 13:33,YunQiang Su <yunqiang.su@cipunited.com> 写道:
> >
> > speculation_barrier for MIPS needs sync+jr.hb (r2+),
> > so we implement __speculation_barrier in libgcc, like arm32 does.
> >
> > gcc/ChangeLog:
> > * config/mips/mips-protos.h (mips_emit_speculation_barrier): New
> >        prototype.
> > * config/mips/mips.cc (speculation_barrier_libfunc): New static
> >        variable.
> > (mips_init_libfuncs): Initialize it.
> > (mips_emit_speculation_barrier): New function.
> > * config/arm/arm.md (speculation_barrier): Call
> >        mips_emit_speculation_barrier.
>
> ^ arm? Typo.
>

ohhh. You are right.
I copied the commit message from:
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=ebdb6f237772df251378d2c08350d345135bcb9e

New patch will be send.

> Thanks
> Jiaxun

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v2] MIPS: add speculation_barrier support
  2023-04-28 12:33 [PATCH] MIPS: add speculation_barrier support YunQiang Su
  2023-04-28 12:36 ` Jiaxun Yang
@ 2023-04-28 13:12 ` YunQiang Su
  2023-05-03 18:29   ` Richard Sandiford
  2023-05-12 10:03   ` [PATCH v3] " YunQiang Su
  1 sibling, 2 replies; 18+ messages in thread
From: YunQiang Su @ 2023-04-28 13:12 UTC (permalink / raw)
  To: gcc-patches; +Cc: macro, jiaxun.yang, syq, richard.sandiford, YunQiang Su

speculation_barrier for MIPS needs sync+jr.hb (r2+),
so we implement __speculation_barrier in libgcc, like arm32 does.

gcc/ChangeLog:
	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
        prototype.
	* config/mips/mips.cc (speculation_barrier_libfunc): New static
        variable.
	(mips_init_libfuncs): Initialize it.
	(mips_emit_speculation_barrier): New function.
	* config/mips/mips.md (speculation_barrier): Call
        mips_emit_speculation_barrier.

libgcc/ChangeLog:
	* config/mips/lib1funcs.S: New file.
	define __speculation_barrier and include mips16.S.
	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
	define LIB1ASMFUNCS as _speculation_barrier.
	set version info for __speculation_barrier.
	* config/mips/libgcc-mips.ver: New file.
	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
	included in lib1funcs.S now.
---
 gcc/config/mips/mips-protos.h      |  2 +
 gcc/config/mips/mips.cc            | 13 +++++++
 gcc/config/mips/mips.md            | 12 ++++++
 libgcc/config/mips/lib1funcs.S     | 60 ++++++++++++++++++++++++++++++
 libgcc/config/mips/libgcc-mips.ver | 21 +++++++++++
 libgcc/config/mips/t-mips          |  7 ++++
 libgcc/config/mips/t-mips16        |  3 +-
 7 files changed, 116 insertions(+), 2 deletions(-)
 create mode 100644 libgcc/config/mips/lib1funcs.S
 create mode 100644 libgcc/config/mips/libgcc-mips.ver

diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index 20483469105..da7902c235b 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
 extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
 extern void mips_expand_vec_cmp_expr (rtx *);
 
+extern void mips_emit_speculation_barrier_function (void);
+
 #endif /* ! GCC_MIPS_PROTOS_H */
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index ca822758b41..139707fda34 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
   return 0;
 }
 
+
+static GTY(()) rtx speculation_barrier_libfunc;
+
 /* Implement TARGET_INIT_LIBFUNCS.  */
 
 static void
@@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
       init_sync_libfuncs (UNITS_PER_WORD);
     }
+  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
 }
 
 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
@@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
       }
 }
 
+/* Emit a speculation barrier.
+   JR.HB is needed, so we need to put
+   speculation_barrier_libfunc in libgcc */
+void
+mips_emit_speculation_barrier_function ()
+{
+  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
+}
+
 /* A SEQUENCE is breakable iff the branch inside it has a compact form
    and the target has compact branches.  */
 
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index ac1d77afc7d..5d04ac566dd 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -160,6 +160,8 @@
   ;; The `.insn' pseudo-op.
   UNSPEC_INSN_PSEUDO
   UNSPEC_JRHB
+
+  VUNSPEC_SPECULATION_BARRIER
 ])
 
 (define_constants
@@ -7455,6 +7457,16 @@
   mips_expand_conditional_move (operands);
   DONE;
 })
+
+(define_expand "speculation_barrier"
+  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
+  ""
+  "
+  mips_emit_speculation_barrier_function ();
+  DONE;
+  "
+)
+
 \f
 ;;
 ;;  ....................
diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
new file mode 100644
index 00000000000..45d74e2e762
--- /dev/null
+++ b/libgcc/config/mips/lib1funcs.S
@@ -0,0 +1,60 @@
+/* Copyright (C) 1995-2023 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
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+#include "mips16.S"
+
+#ifdef L_speculation_barrier
+
+/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
+   So, we use normal MIPS code here, just like what we do for __sync_* */
+	.set nomips16
+
+	.set noreorder
+	.globl	__speculation_barrier
+	.ent	__speculation_barrier
+
+__speculation_barrier:
+	.set	push
+/* MIPS1 has no sync, and in fact it doesn't need it at all.
+   We wish that all newer CPUs should run software with MIPS2+ */
+#if __mips >= 2
+	sync /* complementation barrier for memory */
+#endif
+#if __mips_isa_rev >= 2
+/* MIPSr2+: sync+jr.hb is enough */
+	jr.hb	$ra /* Jump with instruction hazard barrier */
+#else
+/* Make ssnop available, ssnop only recognized by GAS since mips32,
+   however it's actually available since R5500,
+   and it will be decoded as nop on earlier processors */
+	.set mips32
+/* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
+   so 8 ssnop is sufficient to block all speculation on all CPUs */
+	.rept 8
+	ssnop
+	.endr
+	jr	$ra
+#endif
+	.set	pop
+	.end	__speculation_barrier
+
+	.set reorder
+#endif
diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
new file mode 100644
index 00000000000..f0e9fc54965
--- /dev/null
+++ b/libgcc/config/mips/libgcc-mips.ver
@@ -0,0 +1,21 @@
+# Copyright (C) 2008-2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+GCC_14.0 {
+  __speculation_barrier
+}
diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
index 4fb8e136217..d05ef7cbf74 100644
--- a/libgcc/config/mips/t-mips
+++ b/libgcc/config/mips/t-mips
@@ -7,3 +7,10 @@ softfp_truncations :=
 softfp_exclude_libgcc2 := n
 
 LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
+
+
+LIB1ASMSRC = mips/lib1funcs.S
+LIB1ASMFUNCS = _speculation_barrier
+
+# Version these symbols if building libgcc.so.
+SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
index 2bad5119d51..5fd9d60d7a3 100644
--- a/libgcc/config/mips/t-mips16
+++ b/libgcc/config/mips/t-mips16
@@ -16,8 +16,7 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-LIB1ASMSRC = mips/mips16.S
-LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
+LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
 	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
 	_m16unordsf2 \
 	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-04-28 13:12 ` [PATCH v2] " YunQiang Su
@ 2023-05-03 18:29   ` Richard Sandiford
  2023-05-03 21:04     ` Maciej W. Rozycki
  2023-05-12 10:03   ` [PATCH v3] " YunQiang Su
  1 sibling, 1 reply; 18+ messages in thread
From: Richard Sandiford @ 2023-05-03 18:29 UTC (permalink / raw)
  To: YunQiang Su; +Cc: gcc-patches, macro, jiaxun.yang, syq

YunQiang Su <yunqiang.su@cipunited.com> writes:
> speculation_barrier for MIPS needs sync+jr.hb (r2+),
> so we implement __speculation_barrier in libgcc, like arm32 does.

Looks reasonable, but do you have a source for the fallback
pre-r2 handling?  (Thanks for adding that btw, since I realise
it's not your focus here.)

Nit: the copyright for the new files should start with this year,
unless you're copying something significant from an existing file.

Thanks,
Richard

>
> gcc/ChangeLog:
> 	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
>         prototype.
> 	* config/mips/mips.cc (speculation_barrier_libfunc): New static
>         variable.
> 	(mips_init_libfuncs): Initialize it.
> 	(mips_emit_speculation_barrier): New function.
> 	* config/mips/mips.md (speculation_barrier): Call
>         mips_emit_speculation_barrier.
>
> libgcc/ChangeLog:
> 	* config/mips/lib1funcs.S: New file.
> 	define __speculation_barrier and include mips16.S.
> 	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
> 	define LIB1ASMFUNCS as _speculation_barrier.
> 	set version info for __speculation_barrier.
> 	* config/mips/libgcc-mips.ver: New file.
> 	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
> 	included in lib1funcs.S now.
> ---
>  gcc/config/mips/mips-protos.h      |  2 +
>  gcc/config/mips/mips.cc            | 13 +++++++
>  gcc/config/mips/mips.md            | 12 ++++++
>  libgcc/config/mips/lib1funcs.S     | 60 ++++++++++++++++++++++++++++++
>  libgcc/config/mips/libgcc-mips.ver | 21 +++++++++++
>  libgcc/config/mips/t-mips          |  7 ++++
>  libgcc/config/mips/t-mips16        |  3 +-
>  7 files changed, 116 insertions(+), 2 deletions(-)
>  create mode 100644 libgcc/config/mips/lib1funcs.S
>  create mode 100644 libgcc/config/mips/libgcc-mips.ver
>
> diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
> index 20483469105..da7902c235b 100644
> --- a/gcc/config/mips/mips-protos.h
> +++ b/gcc/config/mips/mips-protos.h
> @@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
>  extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
>  extern void mips_expand_vec_cmp_expr (rtx *);
>  
> +extern void mips_emit_speculation_barrier_function (void);
> +
>  #endif /* ! GCC_MIPS_PROTOS_H */
> diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
> index ca822758b41..139707fda34 100644
> --- a/gcc/config/mips/mips.cc
> +++ b/gcc/config/mips/mips.cc
> @@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
>    return 0;
>  }
>  
> +
> +static GTY(()) rtx speculation_barrier_libfunc;
> +
>  /* Implement TARGET_INIT_LIBFUNCS.  */
>  
>  static void
> @@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
>        synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
>        init_sync_libfuncs (UNITS_PER_WORD);
>      }
> +  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
>  }
>  
>  /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
> @@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
>        }
>  }
>  
> +/* Emit a speculation barrier.
> +   JR.HB is needed, so we need to put
> +   speculation_barrier_libfunc in libgcc */
> +void
> +mips_emit_speculation_barrier_function ()
> +{
> +  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
> +}
> +
>  /* A SEQUENCE is breakable iff the branch inside it has a compact form
>     and the target has compact branches.  */
>  
> diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
> index ac1d77afc7d..5d04ac566dd 100644
> --- a/gcc/config/mips/mips.md
> +++ b/gcc/config/mips/mips.md
> @@ -160,6 +160,8 @@
>    ;; The `.insn' pseudo-op.
>    UNSPEC_INSN_PSEUDO
>    UNSPEC_JRHB
> +
> +  VUNSPEC_SPECULATION_BARRIER
>  ])
>  
>  (define_constants
> @@ -7455,6 +7457,16 @@
>    mips_expand_conditional_move (operands);
>    DONE;
>  })
> +
> +(define_expand "speculation_barrier"
> +  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
> +  ""
> +  "
> +  mips_emit_speculation_barrier_function ();
> +  DONE;
> +  "
> +)
> +
>  \f
>  ;;
>  ;;  ....................
> diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
> new file mode 100644
> index 00000000000..45d74e2e762
> --- /dev/null
> +++ b/libgcc/config/mips/lib1funcs.S
> @@ -0,0 +1,60 @@
> +/* Copyright (C) 1995-2023 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
> +Free Software Foundation; either version 3, or (at your option) any
> +later version.
> +
> +This file is distributed in the hope that it will be useful, but
> +WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +General Public License for more details.
> +
> +Under Section 7 of GPL version 3, you are granted additional
> +permissions described in the GCC Runtime Library Exception, version
> +3.1, as published by the Free Software Foundation.
> +
> +You should have received a copy of the GNU General Public License and
> +a copy of the GCC Runtime Library Exception along with this program;
> +see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#include "mips16.S"
> +
> +#ifdef L_speculation_barrier
> +
> +/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
> +   So, we use normal MIPS code here, just like what we do for __sync_* */
> +	.set nomips16
> +
> +	.set noreorder
> +	.globl	__speculation_barrier
> +	.ent	__speculation_barrier
> +
> +__speculation_barrier:
> +	.set	push
> +/* MIPS1 has no sync, and in fact it doesn't need it at all.
> +   We wish that all newer CPUs should run software with MIPS2+ */
> +#if __mips >= 2
> +	sync /* complementation barrier for memory */
> +#endif
> +#if __mips_isa_rev >= 2
> +/* MIPSr2+: sync+jr.hb is enough */
> +	jr.hb	$ra /* Jump with instruction hazard barrier */
> +#else
> +/* Make ssnop available, ssnop only recognized by GAS since mips32,
> +   however it's actually available since R5500,
> +   and it will be decoded as nop on earlier processors */
> +	.set mips32
> +/* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
> +   so 8 ssnop is sufficient to block all speculation on all CPUs */
> +	.rept 8
> +	ssnop
> +	.endr
> +	jr	$ra
> +#endif
> +	.set	pop
> +	.end	__speculation_barrier
> +
> +	.set reorder
> +#endif
> diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
> new file mode 100644
> index 00000000000..f0e9fc54965
> --- /dev/null
> +++ b/libgcc/config/mips/libgcc-mips.ver
> @@ -0,0 +1,21 @@
> +# Copyright (C) 2008-2023 Free Software Foundation, Inc.
> +#
> +# This file is part of GCC.
> +#
> +# GCC is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3, or (at your option)
> +# any later version.
> +#
> +# GCC is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with GCC; see the file COPYING3.  If not see
> +# <http://www.gnu.org/licenses/>.
> +
> +GCC_14.0 {
> +  __speculation_barrier
> +}
> diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
> index 4fb8e136217..d05ef7cbf74 100644
> --- a/libgcc/config/mips/t-mips
> +++ b/libgcc/config/mips/t-mips
> @@ -7,3 +7,10 @@ softfp_truncations :=
>  softfp_exclude_libgcc2 := n
>  
>  LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
> +
> +
> +LIB1ASMSRC = mips/lib1funcs.S
> +LIB1ASMFUNCS = _speculation_barrier
> +
> +# Version these symbols if building libgcc.so.
> +SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
> diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
> index 2bad5119d51..5fd9d60d7a3 100644
> --- a/libgcc/config/mips/t-mips16
> +++ b/libgcc/config/mips/t-mips16
> @@ -16,8 +16,7 @@
>  # along with GCC; see the file COPYING3.  If not see
>  # <http://www.gnu.org/licenses/>.
>  
> -LIB1ASMSRC = mips/mips16.S
> -LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
> +LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
>  	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
>  	_m16unordsf2 \
>  	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-05-03 18:29   ` Richard Sandiford
@ 2023-05-03 21:04     ` Maciej W. Rozycki
  2023-05-03 22:12       ` Jiaxun Yang
  0 siblings, 1 reply; 18+ messages in thread
From: Maciej W. Rozycki @ 2023-05-03 21:04 UTC (permalink / raw)
  To: Richard Sandiford; +Cc: YunQiang Su, gcc-patches, jiaxun.yang, syq

On Wed, 3 May 2023, Richard Sandiford wrote:

> > speculation_barrier for MIPS needs sync+jr.hb (r2+),
> > so we implement __speculation_barrier in libgcc, like arm32 does.
> 
> Looks reasonable, but do you have a source for the fallback
> pre-r2 handling?  (Thanks for adding that btw, since I realise
> it's not your focus here.)

 Seconded WRT legacy MIPS support, really appreciated.

 I think there may be no authoritative source of information here, this is 
a grey area.  The longest SSNOP sequences I have seen were for the various 
Broadcom implementations and counted 7 instructions.  Both the Linux 
kernel and the CFE firmware has them.

 Also we may not be able to fully enforce ordering for the oldest devices 
that do not implement SYNC, as this is system-specific, e.g. involving 
branching on the CP0 condition with the BC0F instruction, and inventing an 
OS interface for that seems unreasonable at this point of history.

 OTOH Linux emulates any traps on SYNC and even though the handler does 
nothing beyond decoding the instruction the exception handling overhead 
ought to make all the effects from before the exception invocation fully 
visible after its completion.  So for Linux targets I think we ought to 
just emit SYNC unconditionally; glibc has already relied on it for years 
now.

 Overall, oh well, I guess we'll have to live with the limitations.

> > diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
> > new file mode 100644
> > index 00000000000..45d74e2e762
> > --- /dev/null
> > +++ b/libgcc/config/mips/lib1funcs.S
[...]
> > +/* Make ssnop available, ssnop only recognized by GAS since mips32,
> > +   however it's actually available since R5500,
> > +   and it will be decoded as nop on earlier processors */
> > +	.set mips32

 I'm fairly sure `.set mips32' will break with NewABI compilations and may 
cause issues if a higher ISA level with some ASEs enabled has been chosen 
by the user building GCC.  But it's not needed either, as contrary to the 
comment SSNOP is universally accepted by GAS, as it should:

{"ssnop",		"",		0x00000040, 0xffffffff, 0,              	INSN2_ALIAS,	I1,		0,	0 }, /* sll */

Very old versions of GAS indeed required MIPS32+ for SSNOP, but the bug 
was fixed back in 2010, landing in binutils 2.21.  I wouldn't bother 
striving to support such old versions of binutils, i.e. either upgrade or 
patch locally (it might then be worth mentioning in `gcc/doc/install.texi' 
though).

 As to producing SYNC for MIPS I, glibc has this:

#if _MIPS_SIM == _ABIO32 && __mips < 2
#define MIPS_PUSH_MIPS2 ".set	mips2\n\t"
#else
#define MIPS_PUSH_MIPS2
#endif

for the ISA override, so I guess we could take a similar approach.  It 
should be safe, as it doesn't change the ISA unnecessarily.

  Maciej

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-05-03 21:04     ` Maciej W. Rozycki
@ 2023-05-03 22:12       ` Jiaxun Yang
  2023-05-07 17:34         ` Maciej W. Rozycki
  0 siblings, 1 reply; 18+ messages in thread
From: Jiaxun Yang @ 2023-05-03 22:12 UTC (permalink / raw)
  To: Maciej W. Rozycki, YunQiang Su
  Cc: Richard Sandiford, gcc-patches, YunQiang Su



> 2023年5月3日 22:04,Maciej W. Rozycki <macro@orcam.me.uk> 写道:
> 
> On Wed, 3 May 2023, Richard Sandiford wrote:
> 
>>> speculation_barrier for MIPS needs sync+jr.hb (r2+),
>>> so we implement __speculation_barrier in libgcc, like arm32 does.
>> 
>> Looks reasonable, but do you have a source for the fallback
>> pre-r2 handling?  (Thanks for adding that btw, since I realise
>> it's not your focus here.)
> 
> Seconded WRT legacy MIPS support, really appreciated.

Hi all,

Just my two cents on legacy MIPS support.

Since it’s possible to run R2- binary on R2+ processor, we’d better find a
semantic that do eliminate speculation on all processors. While SSNOPs
on R2+ processors is pretty much undefined, there is no guarantee that
SSNOP sequence can eliminate speculation.

My proposal is for R2- CPUs we can do a dummy syscall to act as instruction
hazard barrier, since exception must clear the pipeline this should be true
for all known implementations.

The most lightweight syscall I know is to do a MIPS_ATOMIC_SET with
sysmips. A dummy variable on stack should do the track. Do let me know if there
is a better option.

I have a vague memory about a discussion finding that exception does not indicate
a memory barrier, so perhaps we still need a sync preceding to that syscall.


> 
> I think there may be no authoritative source of information here, this is 
> a grey area.  The longest SSNOP sequences I have seen were for the various 
> Broadcom implementations and counted 7 instructions.  Both the Linux 
> kernel and the CFE firmware has them.

Was it for SiByte or BMIPS?

> 
> Also we may not be able to fully enforce ordering for the oldest devices 
> that do not implement SYNC, as this is system-specific, e.g. involving 
> branching on the CP0 condition with the BC0F instruction, and inventing an 
> OS interface for that seems unreasonable at this point of history.

I guess this is not a valid concern for user space applications?
As per R4000 manual BC0F will issue “Coprocessor unusable exception”
exception and it’s certain that we have Staus.CU0 = 0 in user space.

BC0F is not mentioned in MIPS-IV and later MIPS specs.

Thanks
Jiaxun

> 
> OTOH Linux emulates any traps on SYNC and even though the handler does 
> nothing beyond decoding the instruction the exception handling overhead 
> ought to make all the effects from before the exception invocation fully 
> visible after its completion.  So for Linux targets I think we ought to 
> just emit SYNC unconditionally; glibc has already relied on it for years 
> now.
> 
> Overall, oh well, I guess we'll have to live with the limitations.
> 
>  Maciej


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-05-03 22:12       ` Jiaxun Yang
@ 2023-05-07 17:34         ` Maciej W. Rozycki
  2023-05-07 18:47           ` Jiaxun Yang
  0 siblings, 1 reply; 18+ messages in thread
From: Maciej W. Rozycki @ 2023-05-07 17:34 UTC (permalink / raw)
  To: Jiaxun Yang; +Cc: YunQiang Su, Richard Sandiford, gcc-patches, YunQiang Su

On Wed, 3 May 2023, Jiaxun Yang wrote:

> Since it’s possible to run R2- binary on R2+ processor, we’d better find a
> semantic that do eliminate speculation on all processors. While SSNOPs
> on R2+ processors is pretty much undefined, there is no guarantee that
> SSNOP sequence can eliminate speculation.

 Not exactly undefined on R2+, SSNOP is still required to single-issue, so 
it does act as an execution barrier.  Good point otherwise.

 Both EHB and J[AL]R.HB are backwards compatible however (except for an 
obscure 4Kc J[AL]R.HB erratum I came across once and which may be no 
longer relevant), so I think the legacy sequence ought to just return via 
JR.HB as well, therefore providing the required semantics with newer 
hardware.  If it does trap for 4Kc, then the OS can emulate it (and we can 
ignore it for bare metal, deferring to whoever might be interested for a 
workaround).

> My proposal is for R2- CPUs we can do a dummy syscall to act as instruction
> hazard barrier, since exception must clear the pipeline this should be true
> for all known implementations.

 I think the SSNOP approach should be sufficient.

> The most lightweight syscall I know is to do a MIPS_ATOMIC_SET with
> sysmips. A dummy variable on stack should do the track. Do let me know if there
> is a better option.

 That would have to be gettimeofday(2) then, the most performance-critical 
one, and also one that does not have side effects.  The real syscall and 
not VSDO emulation of course (there's a reason it's emulated via VSDO, 
which is exactly our reason too).

> I have a vague memory about a discussion finding that exception does not indicate
> a memory barrier, so perhaps we still need a sync preceding to that syscall.

 There is no claim that I could find in the architecture specification 
saying that taking an exception implies a memory barrier and therefore we 
must conclude it does not.  Likewise executing ERET.

 As I say I think the SSNOP approach should be sufficient, along with 
relying on SYNC emulation.

> > I think there may be no authoritative source of information here, this is 
> > a grey area.  The longest SSNOP sequences I have seen were for the various 
> > Broadcom implementations and counted 7 instructions.  Both the Linux 
> > kernel and the CFE firmware has them.
> 
> Was it for SiByte or BMIPS?

 Both AFAICT.

> > Also we may not be able to fully enforce ordering for the oldest devices 
> > that do not implement SYNC, as this is system-specific, e.g. involving 
> > branching on the CP0 condition with the BC0F instruction, and inventing an 
> > OS interface for that seems unreasonable at this point of history.
> 
> I guess this is not a valid concern for user space applications?
> As per R4000 manual BC0F will issue “Coprocessor unusable exception”
> exception and it’s certain that we have Staus.CU0 = 0 in user space.

 Exactly, which is why an OS service would have to provide the required 
semantics to the userland, and none might be available.  And we probably 
do not care anyway, because I gather this is a security feature to prevent 
certain types of data leaks via a side channel.  I wouldn't expect anyone 
doing any serious security-sensitive processing with legacy MIPS hardware.  
And then there's no speculative execution with all these pieces of legacy 
hardware (R3000, eh?) that have no suitable barriers provided, mostly 
because they are not relevant for them anyway.

 For bare metal we probably do not care about such legacy hardware either 
way.

 Overall I'd say let's do the best we can without bending backwards and 
then rely on people's common sense.

  Maciej

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-05-07 17:34         ` Maciej W. Rozycki
@ 2023-05-07 18:47           ` Jiaxun Yang
  2023-05-07 19:16             ` Maciej W. Rozycki
  0 siblings, 1 reply; 18+ messages in thread
From: Jiaxun Yang @ 2023-05-07 18:47 UTC (permalink / raw)
  To: Maciej W. Rozycki
  Cc: YunQiang Su, Richard Sandiford, gcc-patches, YunQiang Su



> 2023年5月7日 18:34,Maciej W. Rozycki <macro@orcam.me.uk> 写道:
> 
> On Wed, 3 May 2023, Jiaxun Yang wrote:
> 
>> Since it’s possible to run R2- binary on R2+ processor, we’d better find a
>> semantic that do eliminate speculation on all processors. While SSNOPs
>> on R2+ processors is pretty much undefined, there is no guarantee that
>> SSNOP sequence can eliminate speculation.
> 
> Not exactly undefined on R2+, SSNOP is still required to single-issue, so 
> it does act as an execution barrier.  Good point otherwise.
> 
> Both EHB and J[AL]R.HB are backwards compatible however (except for an 
> obscure 4Kc J[AL]R.HB erratum I came across once and which may be no 
> longer relevant), so I think the legacy sequence ought to just return via 
> JR.HB as well, therefore providing the required semantics with newer 
> hardware.  If it does trap for 4Kc, then the OS can emulate it (and we can 
> ignore it for bare metal, deferring to whoever might be interested for a 
> workaround).

Hmm, I just checked MIPS-IV manual, it seems like HB bit (bit 10) is defined as
zero for both JR and JALR.

Is it actually omitted in implementation?

Thanks
Jiaxun



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] MIPS: add speculation_barrier support
  2023-05-07 18:47           ` Jiaxun Yang
@ 2023-05-07 19:16             ` Maciej W. Rozycki
  0 siblings, 0 replies; 18+ messages in thread
From: Maciej W. Rozycki @ 2023-05-07 19:16 UTC (permalink / raw)
  To: Jiaxun Yang; +Cc: YunQiang Su, Richard Sandiford, gcc-patches, YunQiang Su

On Sun, 7 May 2023, Jiaxun Yang wrote:

> > Both EHB and J[AL]R.HB are backwards compatible however (except for an 
> > obscure 4Kc J[AL]R.HB erratum I came across once and which may be no 
> > longer relevant), so I think the legacy sequence ought to just return via 
> > JR.HB as well, therefore providing the required semantics with newer 
> > hardware.  If it does trap for 4Kc, then the OS can emulate it (and we can 
> > ignore it for bare metal, deferring to whoever might be interested for a 
> > workaround).
> 
> Hmm, I just checked MIPS-IV manual, it seems like HB bit (bit 10) is defined as
> zero for both JR and JALR.
> 
> Is it actually omitted in implementation?

 What has become the hint field was supposed not to be decoded in earlier 
silicon, which is why such encodings were chosen for JR.HB and JALR.HB.  
I was told it was actually verified across existing silicon at the time 
the instructions were being defined with MIPSr1 (i.e. when the hint field 
was added to the ISA).  The issue with the 4Kc was an unfortunate mistake.

 Many instructions are not fully decoded causing encoding aliases, MFHI, 
etc. comes to mind.

  Maciej

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v3] MIPS: add speculation_barrier support
  2023-04-28 13:12 ` [PATCH v2] " YunQiang Su
  2023-05-03 18:29   ` Richard Sandiford
@ 2023-05-12 10:03   ` YunQiang Su
  2023-05-12 10:30     ` [PATCH v4] " YunQiang Su
  1 sibling, 1 reply; 18+ messages in thread
From: YunQiang Su @ 2023-05-12 10:03 UTC (permalink / raw)
  To: gcc-patches
  Cc: syq, macro, xry111, richard.sandiford, jiaxun.yang, amodra, YunQiang Su

speculation_barrier for MIPS needs sync+jr.hb (r2+),
so we implement __speculation_barrier in libgcc, like arm32 does.

gcc/ChangeLog:
	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
        prototype.
	* config/mips/mips.cc (speculation_barrier_libfunc): New static
        variable.
	(mips_init_libfuncs): Initialize it.
	(mips_emit_speculation_barrier): New function.
	* config/mips/mips.md (speculation_barrier): Call
        mips_emit_speculation_barrier.
	* configure.ac: error if gas doesn't accept ssnop for mips1.
	* configure: regenerated.
	* doc/install.texi: documents mips requires binutils 2.21+.

libgcc/ChangeLog:
	* config/mips/lib1funcs.S: New file.
	define __speculation_barrier and include mips16.S.
	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
	define LIB1ASMFUNCS as _speculation_barrier.
	set version info for __speculation_barrier.
	* config/mips/libgcc-mips.ver: New file.
	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
	included in lib1funcs.S now.
---
 gcc/config/mips/mips-protos.h      |  2 +
 gcc/config/mips/mips.cc            | 13 ++++++
 gcc/config/mips/mips.md            | 12 ++++++
 gcc/configure                      | 32 +++++++++++++++
 gcc/configure.ac                   |  7 ++++
 gcc/doc/install.texi               |  2 +
 libgcc/config/mips/lib1funcs.S     | 63 ++++++++++++++++++++++++++++++
 libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
 libgcc/config/mips/t-mips          |  7 ++++
 libgcc/config/mips/t-mips16        |  3 +-
 10 files changed, 160 insertions(+), 2 deletions(-)
 create mode 100644 libgcc/config/mips/lib1funcs.S
 create mode 100644 libgcc/config/mips/libgcc-mips.ver

diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index 20483469105..da7902c235b 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
 extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
 extern void mips_expand_vec_cmp_expr (rtx *);
 
+extern void mips_emit_speculation_barrier_function (void);
+
 #endif /* ! GCC_MIPS_PROTOS_H */
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index ca822758b41..139707fda34 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
   return 0;
 }
 
+
+static GTY(()) rtx speculation_barrier_libfunc;
+
 /* Implement TARGET_INIT_LIBFUNCS.  */
 
 static void
@@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
       init_sync_libfuncs (UNITS_PER_WORD);
     }
+  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
 }
 
 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
@@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
       }
 }
 
+/* Emit a speculation barrier.
+   JR.HB is needed, so we need to put
+   speculation_barrier_libfunc in libgcc */
+void
+mips_emit_speculation_barrier_function ()
+{
+  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
+}
+
 /* A SEQUENCE is breakable iff the branch inside it has a compact form
    and the target has compact branches.  */
 
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index ac1d77afc7d..5d04ac566dd 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -160,6 +160,8 @@
   ;; The `.insn' pseudo-op.
   UNSPEC_INSN_PSEUDO
   UNSPEC_JRHB
+
+  VUNSPEC_SPECULATION_BARRIER
 ])
 
 (define_constants
@@ -7455,6 +7457,16 @@
   mips_expand_conditional_move (operands);
   DONE;
 })
+
+(define_expand "speculation_barrier"
+  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
+  ""
+  "
+  mips_emit_speculation_barrier_function ();
+  DONE;
+  "
+)
+
 \f
 ;;
 ;;  ....................
diff --git a/gcc/configure b/gcc/configure
index 191f68581b3..e55b40d7f4c 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -28699,6 +28699,38 @@ $as_echo "#define HAVE_LD_PERSONALITY_RELAXATION 1" >>confdefs.h
 
     fi
 
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for mips1 recognize ssnop" >&5
+$as_echo_n "checking assembler for mips1 recognize ssnop... " >&6; }
+if ${gcc_cv_as_mips1_ssnop+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_mips1_ssnop=no
+  if test x$gcc_cv_as != x; then
+    $as_echo 'ssnop' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags -mabi=32 -mips1 -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+	  echo $ac_try
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_mips1_ssnop=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_mips1_ssnop" >&5
+$as_echo "$gcc_cv_as_mips1_ssnop" >&6; }
+
+
+    if test x$gcc_cv_as_mips1_ssnop = xno; then
+      as_fn_error $? "New binutils is required to support ssnop even for MIPS I: 2.21+" "$LINENO" 5
+    fi
+
     { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for -mnan= support" >&5
 $as_echo_n "checking assembler for -mnan= support... " >&6; }
 if ${gcc_cv_as_mips_nan+:} false; then :
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 075424669c9..6d2cb4c51a0 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5270,6 +5270,13 @@ EOF
 pointers into PC-relative form.])
     fi
 
+    gcc_GAS_CHECK_FEATURE([mips1 recognize ssnop], gcc_cv_as_mips1_ssnop,
+                          [-mabi=32 -mips1], [ssnop],,,)
+    if test x$gcc_cv_as_mips1_ssnop = xno; then
+      AC_MSG_ERROR(
+	[New binutils is required to support ssnop even for MIPS I: 2.21+])
+    fi
+
     gcc_GAS_CHECK_FEATURE([-mnan= support],
       gcc_cv_as_mips_nan,
       [-mnan=2008],,,
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index fa91ce1953d..fae6ec5f7a9 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -4610,6 +4610,8 @@ the use of break, use the @option{--with-divide=breaks}
 @command{configure} option when configuring GCC@.  The default is to
 use traps on systems that support them.
 
+GNU Binutils 2.21 or newer is required.
+
 @html
 <hr />
 @end html
diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
new file mode 100644
index 00000000000..b6e7c1362dd
--- /dev/null
+++ b/libgcc/config/mips/lib1funcs.S
@@ -0,0 +1,63 @@
+/* Copyright (C) 2023 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
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+#include "mips16.S"
+
+#ifdef L_speculation_barrier
+
+/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
+   So, we use normal MIPS code here, just like what we do for __sync_* */
+	.set nomips16
+
+	.set noreorder
+	.globl	__speculation_barrier
+	.ent	__speculation_barrier
+
+__speculation_barrier:
+	.set	push
+#if __mips >= 2 || defined(__linux)
+	/* MIPS1 has no sync. while Linux can trap&emu sync */
+	/* FIXME: Will somebody use linux/gcc for MIPS1/baremetal? */
+	sync /* complementation barrier for memory */
+#else
+	/* ssnop is actually available since R5500,
+	   and it will be decoded as nop on earlier processors.
+	   gas can recognize it even with -march=mips1 since 2.21 */
+	/* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
+	   so 8 ssnop is sufficient to block all speculation on all CPUs */
+	.rept 8
+	ssnop
+	.endr
+#endif
+
+
+#if __mips_isa_rev >= 1
+	/* yes, the below one is the encoding, don't remove this arm,
+	   since r6 changes the encoding */
+	jr.hb	$ra /* Jump with instruction hazard barrier */
+#else
+	.word	0x03e00408 /* The encoding of jr.hb $ra */
+#endif
+	.set	pop
+	.end	__speculation_barrier
+
+	.set reorder
+#endif
diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
new file mode 100644
index 00000000000..68f8d2bbd51
--- /dev/null
+++ b/libgcc/config/mips/libgcc-mips.ver
@@ -0,0 +1,21 @@
+# Copyright (C) 2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+GCC_14.0 {
+  __speculation_barrier
+}
diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
index 4fb8e136217..d05ef7cbf74 100644
--- a/libgcc/config/mips/t-mips
+++ b/libgcc/config/mips/t-mips
@@ -7,3 +7,10 @@ softfp_truncations :=
 softfp_exclude_libgcc2 := n
 
 LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
+
+
+LIB1ASMSRC = mips/lib1funcs.S
+LIB1ASMFUNCS = _speculation_barrier
+
+# Version these symbols if building libgcc.so.
+SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
index 2bad5119d51..5fd9d60d7a3 100644
--- a/libgcc/config/mips/t-mips16
+++ b/libgcc/config/mips/t-mips16
@@ -16,8 +16,7 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-LIB1ASMSRC = mips/mips16.S
-LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
+LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
 	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
 	_m16unordsf2 \
 	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v4] MIPS: add speculation_barrier support
  2023-05-12 10:03   ` [PATCH v3] " YunQiang Su
@ 2023-05-12 10:30     ` YunQiang Su
  2023-05-31  9:43       ` YunQiang Su
  0 siblings, 1 reply; 18+ messages in thread
From: YunQiang Su @ 2023-05-12 10:30 UTC (permalink / raw)
  To: gcc-patches
  Cc: syq, macro, xry111, richard.sandiford, jiaxun.yang, amodra, YunQiang Su

speculation_barrier for MIPS needs sync+jr.hb (r2+),
so we implement __speculation_barrier in libgcc, like arm32 does.

gcc/ChangeLog:
	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
        prototype.
	* config/mips/mips.cc (speculation_barrier_libfunc): New static
        variable.
	(mips_init_libfuncs): Initialize it.
	(mips_emit_speculation_barrier): New function.
	* config/mips/mips.md (speculation_barrier): Call
        mips_emit_speculation_barrier.
	* configure.ac: error if gas doesn't accept ssnop for mips1.
	* configure: regenerated.
	* doc/install.texi: documents mips requires binutils 2.21+.

libgcc/ChangeLog:
	* config/mips/lib1funcs.S: New file.
	define __speculation_barrier and include mips16.S.
	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
	define LIB1ASMFUNCS as _speculation_barrier.
	set version info for __speculation_barrier.
	* config/mips/libgcc-mips.ver: New file.
	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
	included in lib1funcs.S now.
---
 gcc/config/mips/mips-protos.h      |  2 +
 gcc/config/mips/mips.cc            | 13 +++++++
 gcc/config/mips/mips.md            | 12 ++++++
 gcc/configure                      | 32 +++++++++++++++
 gcc/configure.ac                   |  7 ++++
 gcc/doc/install.texi               |  2 +
 libgcc/config/mips/lib1funcs.S     | 62 ++++++++++++++++++++++++++++++
 libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
 libgcc/config/mips/t-mips          |  7 ++++
 libgcc/config/mips/t-mips16        |  3 +-
 10 files changed, 159 insertions(+), 2 deletions(-)
 create mode 100644 libgcc/config/mips/lib1funcs.S
 create mode 100644 libgcc/config/mips/libgcc-mips.ver

diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index 20483469105..da7902c235b 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
 extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
 extern void mips_expand_vec_cmp_expr (rtx *);
 
+extern void mips_emit_speculation_barrier_function (void);
+
 #endif /* ! GCC_MIPS_PROTOS_H */
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index ca822758b41..139707fda34 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
   return 0;
 }
 
+
+static GTY(()) rtx speculation_barrier_libfunc;
+
 /* Implement TARGET_INIT_LIBFUNCS.  */
 
 static void
@@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
       init_sync_libfuncs (UNITS_PER_WORD);
     }
+  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
 }
 
 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
@@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
       }
 }
 
+/* Emit a speculation barrier.
+   JR.HB is needed, so we need to put
+   speculation_barrier_libfunc in libgcc */
+void
+mips_emit_speculation_barrier_function ()
+{
+  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
+}
+
 /* A SEQUENCE is breakable iff the branch inside it has a compact form
    and the target has compact branches.  */
 
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index ac1d77afc7d..5d04ac566dd 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -160,6 +160,8 @@
   ;; The `.insn' pseudo-op.
   UNSPEC_INSN_PSEUDO
   UNSPEC_JRHB
+
+  VUNSPEC_SPECULATION_BARRIER
 ])
 
 (define_constants
@@ -7455,6 +7457,16 @@
   mips_expand_conditional_move (operands);
   DONE;
 })
+
+(define_expand "speculation_barrier"
+  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
+  ""
+  "
+  mips_emit_speculation_barrier_function ();
+  DONE;
+  "
+)
+
 \f
 ;;
 ;;  ....................
diff --git a/gcc/configure b/gcc/configure
index 191f68581b3..e55b40d7f4c 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -28699,6 +28699,38 @@ $as_echo "#define HAVE_LD_PERSONALITY_RELAXATION 1" >>confdefs.h
 
     fi
 
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for mips1 recognize ssnop" >&5
+$as_echo_n "checking assembler for mips1 recognize ssnop... " >&6; }
+if ${gcc_cv_as_mips1_ssnop+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  gcc_cv_as_mips1_ssnop=no
+  if test x$gcc_cv_as != x; then
+    $as_echo 'ssnop' > conftest.s
+    if { ac_try='$gcc_cv_as $gcc_cv_as_flags -mabi=32 -mips1 -o conftest.o conftest.s >&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+	  echo $ac_try
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+    then
+	gcc_cv_as_mips1_ssnop=yes
+    else
+      echo "configure: failed program was" >&5
+      cat conftest.s >&5
+    fi
+    rm -f conftest.o conftest.s
+  fi
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_mips1_ssnop" >&5
+$as_echo "$gcc_cv_as_mips1_ssnop" >&6; }
+
+
+    if test x$gcc_cv_as_mips1_ssnop = xno; then
+      as_fn_error $? "New binutils is required to support ssnop even for MIPS I: 2.21+" "$LINENO" 5
+    fi
+
     { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for -mnan= support" >&5
 $as_echo_n "checking assembler for -mnan= support... " >&6; }
 if ${gcc_cv_as_mips_nan+:} false; then :
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 075424669c9..6d2cb4c51a0 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5270,6 +5270,13 @@ EOF
 pointers into PC-relative form.])
     fi
 
+    gcc_GAS_CHECK_FEATURE([mips1 recognize ssnop], gcc_cv_as_mips1_ssnop,
+                          [-mabi=32 -mips1], [ssnop],,,)
+    if test x$gcc_cv_as_mips1_ssnop = xno; then
+      AC_MSG_ERROR(
+	[New binutils is required to support ssnop even for MIPS I: 2.21+])
+    fi
+
     gcc_GAS_CHECK_FEATURE([-mnan= support],
       gcc_cv_as_mips_nan,
       [-mnan=2008],,,
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index fa91ce1953d..fae6ec5f7a9 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -4610,6 +4610,8 @@ the use of break, use the @option{--with-divide=breaks}
 @command{configure} option when configuring GCC@.  The default is to
 use traps on systems that support them.
 
+GNU Binutils 2.21 or newer is required.
+
 @html
 <hr />
 @end html
diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
new file mode 100644
index 00000000000..12314953a7f
--- /dev/null
+++ b/libgcc/config/mips/lib1funcs.S
@@ -0,0 +1,62 @@
+/* Copyright (C) 2023 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
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+//#include "mips16.S"
+
+#ifdef L_speculation_barrier
+
+/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
+   So, we use normal MIPS code here, just like what we do for __sync_* */
+	.set nomips16
+
+	.set noreorder
+	.globl	__speculation_barrier
+	.ent	__speculation_barrier
+
+__speculation_barrier:
+	.set	push
+#if __mips >= 2 || defined(__linux)
+	/* MIPS1 has no sync. while Linux can trap&emu sync */
+	/* FIXME: Will somebody use linux/gcc for MIPS1/baremetal? */
+	sync /* complementation barrier for memory */
+#endif
+
+
+#if __mips_isa_rev >= 1
+	/* Binutils claims that JR in R1 can do same as jr.hb.
+	   R6 changes the encoding of jr.hb */
+	jr.hb	$ra /* Jump with instruction hazard barrier */
+#else
+	/* ssnop is actually available since R5500,
+	   and it will be decoded as nop on earlier processors.
+	   gas can recognize it even with -march=mips1 since 2.21 */
+	/* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
+	   so 8 ssnop is sufficient to block all speculation on all CPUs */
+	.rept 8
+	ssnop
+	.endr
+	.word	0x03e00408 /* The encoding of jr.hb $ra */
+#endif
+	.set	pop
+	.end	__speculation_barrier
+
+	.set reorder
+#endif
diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
new file mode 100644
index 00000000000..68f8d2bbd51
--- /dev/null
+++ b/libgcc/config/mips/libgcc-mips.ver
@@ -0,0 +1,21 @@
+# Copyright (C) 2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+GCC_14.0 {
+  __speculation_barrier
+}
diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
index 4fb8e136217..d05ef7cbf74 100644
--- a/libgcc/config/mips/t-mips
+++ b/libgcc/config/mips/t-mips
@@ -7,3 +7,10 @@ softfp_truncations :=
 softfp_exclude_libgcc2 := n
 
 LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
+
+
+LIB1ASMSRC = mips/lib1funcs.S
+LIB1ASMFUNCS = _speculation_barrier
+
+# Version these symbols if building libgcc.so.
+SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
index 2bad5119d51..5fd9d60d7a3 100644
--- a/libgcc/config/mips/t-mips16
+++ b/libgcc/config/mips/t-mips16
@@ -16,8 +16,7 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-LIB1ASMSRC = mips/mips16.S
-LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
+LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
 	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
 	_m16unordsf2 \
 	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] MIPS: add speculation_barrier support
  2023-05-12 10:30     ` [PATCH v4] " YunQiang Su
@ 2023-05-31  9:43       ` YunQiang Su
  2023-05-31 10:35         ` Maciej W. Rozycki
  0 siblings, 1 reply; 18+ messages in thread
From: YunQiang Su @ 2023-05-31  9:43 UTC (permalink / raw)
  To: YunQiang Su
  Cc: apinski--- via Gcc-patches, Maciej W. Rozycki, Xi Ruoyao,
	Richard Sandiford, Jiaxun Yang, Alan Modra

YunQiang Su <yunqiang.su@cipunited.com> 于2023年5月12日周五 18:30写道:
>
> speculation_barrier for MIPS needs sync+jr.hb (r2+),
> so we implement __speculation_barrier in libgcc, like arm32 does.
>
> gcc/ChangeLog:
>         * config/mips/mips-protos.h (mips_emit_speculation_barrier): New
>         prototype.
>         * config/mips/mips.cc (speculation_barrier_libfunc): New static
>         variable.
>         (mips_init_libfuncs): Initialize it.
>         (mips_emit_speculation_barrier): New function.
>         * config/mips/mips.md (speculation_barrier): Call
>         mips_emit_speculation_barrier.
>         * configure.ac: error if gas doesn't accept ssnop for mips1.
>         * configure: regenerated.
>         * doc/install.texi: documents mips requires binutils 2.21+.
>
> libgcc/ChangeLog:
>         * config/mips/lib1funcs.S: New file.
>         define __speculation_barrier and include mips16.S.
>         * config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
>         define LIB1ASMFUNCS as _speculation_barrier.
>         set version info for __speculation_barrier.
>         * config/mips/libgcc-mips.ver: New file.
>         * config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S is
>         included in lib1funcs.S now.

If no objection, I will commit this V4 patch.

> ---
>  gcc/config/mips/mips-protos.h      |  2 +
>  gcc/config/mips/mips.cc            | 13 +++++++
>  gcc/config/mips/mips.md            | 12 ++++++
>  gcc/configure                      | 32 +++++++++++++++
>  gcc/configure.ac                   |  7 ++++
>  gcc/doc/install.texi               |  2 +
>  libgcc/config/mips/lib1funcs.S     | 62 ++++++++++++++++++++++++++++++
>  libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
>  libgcc/config/mips/t-mips          |  7 ++++
>  libgcc/config/mips/t-mips16        |  3 +-
>  10 files changed, 159 insertions(+), 2 deletions(-)
>  create mode 100644 libgcc/config/mips/lib1funcs.S
>  create mode 100644 libgcc/config/mips/libgcc-mips.ver
>
> diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
> index 20483469105..da7902c235b 100644
> --- a/gcc/config/mips/mips-protos.h
> +++ b/gcc/config/mips/mips-protos.h
> @@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
>  extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
>  extern void mips_expand_vec_cmp_expr (rtx *);
>
> +extern void mips_emit_speculation_barrier_function (void);
> +
>  #endif /* ! GCC_MIPS_PROTOS_H */
> diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
> index ca822758b41..139707fda34 100644
> --- a/gcc/config/mips/mips.cc
> +++ b/gcc/config/mips/mips.cc
> @@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
>    return 0;
>  }
>
> +
> +static GTY(()) rtx speculation_barrier_libfunc;
> +
>  /* Implement TARGET_INIT_LIBFUNCS.  */
>
>  static void
> @@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
>        synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
>        init_sync_libfuncs (UNITS_PER_WORD);
>      }
> +  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
>  }
>
>  /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
> @@ -19092,6 +19096,15 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
>        }
>  }
>
> +/* Emit a speculation barrier.
> +   JR.HB is needed, so we need to put
> +   speculation_barrier_libfunc in libgcc */
> +void
> +mips_emit_speculation_barrier_function ()
> +{
> +  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
> +}
> +
>  /* A SEQUENCE is breakable iff the branch inside it has a compact form
>     and the target has compact branches.  */
>
> diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
> index ac1d77afc7d..5d04ac566dd 100644
> --- a/gcc/config/mips/mips.md
> +++ b/gcc/config/mips/mips.md
> @@ -160,6 +160,8 @@
>    ;; The `.insn' pseudo-op.
>    UNSPEC_INSN_PSEUDO
>    UNSPEC_JRHB
> +
> +  VUNSPEC_SPECULATION_BARRIER
>  ])
>
>  (define_constants
> @@ -7455,6 +7457,16 @@
>    mips_expand_conditional_move (operands);
>    DONE;
>  })
> +
> +(define_expand "speculation_barrier"
> +  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
> +  ""
> +  "
> +  mips_emit_speculation_barrier_function ();
> +  DONE;
> +  "
> +)
> +
>
>  ;;
>  ;;  ....................
> diff --git a/gcc/configure b/gcc/configure
> index 191f68581b3..e55b40d7f4c 100755
> --- a/gcc/configure
> +++ b/gcc/configure
> @@ -28699,6 +28699,38 @@ $as_echo "#define HAVE_LD_PERSONALITY_RELAXATION 1" >>confdefs.h
>
>      fi
>
> +    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for mips1 recognize ssnop" >&5
> +$as_echo_n "checking assembler for mips1 recognize ssnop... " >&6; }
> +if ${gcc_cv_as_mips1_ssnop+:} false; then :
> +  $as_echo_n "(cached) " >&6
> +else
> +  gcc_cv_as_mips1_ssnop=no
> +  if test x$gcc_cv_as != x; then
> +    $as_echo 'ssnop' > conftest.s
> +    if { ac_try='$gcc_cv_as $gcc_cv_as_flags -mabi=32 -mips1 -o conftest.o conftest.s >&5'
> +  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
> +         echo $ac_try
> +  (eval $ac_try) 2>&5
> +  ac_status=$?
> +  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
> +  test $ac_status = 0; }; }
> +    then
> +       gcc_cv_as_mips1_ssnop=yes
> +    else
> +      echo "configure: failed program was" >&5
> +      cat conftest.s >&5
> +    fi
> +    rm -f conftest.o conftest.s
> +  fi
> +fi
> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_mips1_ssnop" >&5
> +$as_echo "$gcc_cv_as_mips1_ssnop" >&6; }
> +
> +
> +    if test x$gcc_cv_as_mips1_ssnop = xno; then
> +      as_fn_error $? "New binutils is required to support ssnop even for MIPS I: 2.21+" "$LINENO" 5
> +    fi
> +
>      { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler for -mnan= support" >&5
>  $as_echo_n "checking assembler for -mnan= support... " >&6; }
>  if ${gcc_cv_as_mips_nan+:} false; then :
> diff --git a/gcc/configure.ac b/gcc/configure.ac
> index 075424669c9..6d2cb4c51a0 100644
> --- a/gcc/configure.ac
> +++ b/gcc/configure.ac
> @@ -5270,6 +5270,13 @@ EOF
>  pointers into PC-relative form.])
>      fi
>
> +    gcc_GAS_CHECK_FEATURE([mips1 recognize ssnop], gcc_cv_as_mips1_ssnop,
> +                          [-mabi=32 -mips1], [ssnop],,,)
> +    if test x$gcc_cv_as_mips1_ssnop = xno; then
> +      AC_MSG_ERROR(
> +       [New binutils is required to support ssnop even for MIPS I: 2.21+])
> +    fi
> +
>      gcc_GAS_CHECK_FEATURE([-mnan= support],
>        gcc_cv_as_mips_nan,
>        [-mnan=2008],,,
> diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
> index fa91ce1953d..fae6ec5f7a9 100644
> --- a/gcc/doc/install.texi
> +++ b/gcc/doc/install.texi
> @@ -4610,6 +4610,8 @@ the use of break, use the @option{--with-divide=breaks}
>  @command{configure} option when configuring GCC@.  The default is to
>  use traps on systems that support them.
>
> +GNU Binutils 2.21 or newer is required.
> +
>  @html
>  <hr />
>  @end html
> diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
> new file mode 100644
> index 00000000000..12314953a7f
> --- /dev/null
> +++ b/libgcc/config/mips/lib1funcs.S
> @@ -0,0 +1,62 @@
> +/* Copyright (C) 2023 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
> +Free Software Foundation; either version 3, or (at your option) any
> +later version.
> +
> +This file is distributed in the hope that it will be useful, but
> +WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +General Public License for more details.
> +
> +Under Section 7 of GPL version 3, you are granted additional
> +permissions described in the GCC Runtime Library Exception, version
> +3.1, as published by the Free Software Foundation.
> +
> +You should have received a copy of the GNU General Public License and
> +a copy of the GCC Runtime Library Exception along with this program;
> +see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
> +<http://www.gnu.org/licenses/>.  */
> +
> +//#include "mips16.S"
> +
> +#ifdef L_speculation_barrier
> +
> +/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
> +   So, we use normal MIPS code here, just like what we do for __sync_* */
> +       .set nomips16
> +
> +       .set noreorder
> +       .globl  __speculation_barrier
> +       .ent    __speculation_barrier
> +
> +__speculation_barrier:
> +       .set    push
> +#if __mips >= 2 || defined(__linux)
> +       /* MIPS1 has no sync. while Linux can trap&emu sync */
> +       /* FIXME: Will somebody use linux/gcc for MIPS1/baremetal? */
> +       sync /* complementation barrier for memory */
> +#endif
> +
> +
> +#if __mips_isa_rev >= 1
> +       /* Binutils claims that JR in R1 can do same as jr.hb.
> +          R6 changes the encoding of jr.hb */
> +       jr.hb   $ra /* Jump with instruction hazard barrier */
> +#else
> +       /* ssnop is actually available since R5500,
> +          and it will be decoded as nop on earlier processors.
> +          gas can recognize it even with -march=mips1 since 2.21 */
> +       /* MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
> +          so 8 ssnop is sufficient to block all speculation on all CPUs */
> +       .rept 8
> +       ssnop
> +       .endr
> +       .word   0x03e00408 /* The encoding of jr.hb $ra */
> +#endif
> +       .set    pop
> +       .end    __speculation_barrier
> +
> +       .set reorder
> +#endif
> diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
> new file mode 100644
> index 00000000000..68f8d2bbd51
> --- /dev/null
> +++ b/libgcc/config/mips/libgcc-mips.ver
> @@ -0,0 +1,21 @@
> +# Copyright (C) 2023 Free Software Foundation, Inc.
> +#
> +# This file is part of GCC.
> +#
> +# GCC is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3, or (at your option)
> +# any later version.
> +#
> +# GCC is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with GCC; see the file COPYING3.  If not see
> +# <http://www.gnu.org/licenses/>.
> +
> +GCC_14.0 {
> +  __speculation_barrier
> +}
> diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
> index 4fb8e136217..d05ef7cbf74 100644
> --- a/libgcc/config/mips/t-mips
> +++ b/libgcc/config/mips/t-mips
> @@ -7,3 +7,10 @@ softfp_truncations :=
>  softfp_exclude_libgcc2 := n
>
>  LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
> +
> +
> +LIB1ASMSRC = mips/lib1funcs.S
> +LIB1ASMFUNCS = _speculation_barrier
> +
> +# Version these symbols if building libgcc.so.
> +SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
> diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
> index 2bad5119d51..5fd9d60d7a3 100644
> --- a/libgcc/config/mips/t-mips16
> +++ b/libgcc/config/mips/t-mips16
> @@ -16,8 +16,7 @@
>  # along with GCC; see the file COPYING3.  If not see
>  # <http://www.gnu.org/licenses/>.
>
> -LIB1ASMSRC = mips/mips16.S
> -LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
> +LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
>         _m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
>         _m16unordsf2 \
>         _m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
> --
> 2.30.2
>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] MIPS: add speculation_barrier support
  2023-05-31  9:43       ` YunQiang Su
@ 2023-05-31 10:35         ` Maciej W. Rozycki
  2023-06-01  4:26           ` [PATCH v5] MIPS: Add " YunQiang Su
  0 siblings, 1 reply; 18+ messages in thread
From: Maciej W. Rozycki @ 2023-05-31 10:35 UTC (permalink / raw)
  To: YunQiang Su
  Cc: YunQiang Su, apinski--- via Gcc-patches, Xi Ruoyao,
	Richard Sandiford, Jiaxun Yang, Alan Modra

On Wed, 31 May 2023, YunQiang Su wrote:

> If no objection, I will commit this V4 patch.

 At first glance it has coding style issues.

  Maciej

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v5] MIPS: Add speculation_barrier support
  2023-05-31 10:35         ` Maciej W. Rozycki
@ 2023-06-01  4:26           ` YunQiang Su
  2023-06-08 12:35             ` Richard Earnshaw (lists)
  0 siblings, 1 reply; 18+ messages in thread
From: YunQiang Su @ 2023-06-01  4:26 UTC (permalink / raw)
  To: gcc-patches
  Cc: macro, jiaxun.yang, syq, richard.sandiford, jeffreyalaw, YunQiang Su

speculation_barrier for MIPS needs sync+jr.hb (r2+),
so we implement __speculation_barrier in libgcc, like arm32 does.

gcc/ChangeLog:
	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
        prototype.
	* config/mips/mips.cc (speculation_barrier_libfunc): New static
        variable.
	(mips_init_libfuncs): Initialize it.
	(mips_emit_speculation_barrier): New function.
	* config/mips/mips.md (speculation_barrier): Call
        mips_emit_speculation_barrier.

libgcc/ChangeLog:
	* config/mips/lib1funcs.S: New file.
	define __speculation_barrier and include mips16.S.
	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
	define LIB1ASMFUNCS as _speculation_barrier.
	set version info for __speculation_barrier.
	* config/mips/libgcc-mips.ver: New file.
	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S
	included in lib1funcs.S now.
---
 gcc/config/mips/mips-protos.h      |  2 +
 gcc/config/mips/mips.cc            | 12 ++++++
 gcc/config/mips/mips.md            | 12 ++++++
 libgcc/config/mips/lib1funcs.S     | 65 ++++++++++++++++++++++++++++++
 libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
 libgcc/config/mips/t-mips          |  7 ++++
 libgcc/config/mips/t-mips16        |  3 +-
 7 files changed, 120 insertions(+), 2 deletions(-)
 create mode 100644 libgcc/config/mips/lib1funcs.S
 create mode 100644 libgcc/config/mips/libgcc-mips.ver

diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index 20483469105..da7902c235b 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
 extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
 extern void mips_expand_vec_cmp_expr (rtx *);
 
+extern void mips_emit_speculation_barrier_function (void);
+
 #endif /* ! GCC_MIPS_PROTOS_H */
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index ca491b981a3..c1d1691306e 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
   return 0;
 }
 
+
+static GTY (()) rtx speculation_barrier_libfunc;
+
 /* Implement TARGET_INIT_LIBFUNCS.  */
 
 static void
@@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
       init_sync_libfuncs (UNITS_PER_WORD);
     }
+  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
 }
 
 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
@@ -19092,6 +19096,14 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
       }
 }
 
+/* Emit a speculation barrier.
+   JR.HB is needed, so we put speculation_barrier_libfunc in libgcc.  */
+void
+mips_emit_speculation_barrier_function ()
+{
+  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
+}
+
 /* A SEQUENCE is breakable iff the branch inside it has a compact form
    and the target has compact branches.  */
 
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index ac1d77afc7d..5d04ac566dd 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -160,6 +160,8 @@
   ;; The `.insn' pseudo-op.
   UNSPEC_INSN_PSEUDO
   UNSPEC_JRHB
+
+  VUNSPEC_SPECULATION_BARRIER
 ])
 
 (define_constants
@@ -7455,6 +7457,16 @@
   mips_expand_conditional_move (operands);
   DONE;
 })
+
+(define_expand "speculation_barrier"
+  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
+  ""
+  "
+  mips_emit_speculation_barrier_function ();
+  DONE;
+  "
+)
+
 \f
 ;;
 ;;  ....................
diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
new file mode 100644
index 00000000000..97a3655e8ab
--- /dev/null
+++ b/libgcc/config/mips/lib1funcs.S
@@ -0,0 +1,65 @@
+/* Copyright (C) 2023 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
+Free Software Foundation; either version 3, or (at your option) any
+later version.
+
+This file is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+//#include "mips16.S"
+
+#ifdef L_speculation_barrier
+
+/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
+   So, we use normal MIPS code here, just like what we do for __sync_*.  */
+	.set nomips16
+
+	.set noreorder
+	.globl	__speculation_barrier
+	.ent	__speculation_barrier
+
+__speculation_barrier:
+	.set	push
+#if __mips >= 2
+	sync /* complementation barrier for memory.  */
+#elif defined (__linux)
+	/* MIPS1 has no sync, while Linux can trap&emu sync.  */
+	/* FIXME: Will somebody use linux/gcc for MIPS1/baremetal?  */
+	.word 0x0000000f
+#endif
+
+
+#if __mips_isa_rev >= 1
+	/* Binutils claims that JR in R1 can do same as jr.hb.
+	   R6 changes the encoding of jr.hb.  */
+	jr.hb	$ra /* Jump with instruction hazard barrier.  */
+#else
+	/* ssnop is actually available since R5500,
+	   and it will be decoded as nop on earlier processors.
+	   gas can only recognize it with -march=mips1 since 2.21.
+	   MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
+	   so 8 ssnop is sufficient to block all speculation on all CPUs.  */
+	.rept 8
+	.word	0x00000040 /* The encoding of ssnop.  */
+	.endr
+	/* jr.hb will be decoded as jr on earlier processors.  */
+	.word	0x03e00408 /* The encoding of jr.hb $ra.  */
+#endif
+	.set	pop
+	.end	__speculation_barrier
+
+	.set reorder
+#endif
diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
new file mode 100644
index 00000000000..68f8d2bbd51
--- /dev/null
+++ b/libgcc/config/mips/libgcc-mips.ver
@@ -0,0 +1,21 @@
+# Copyright (C) 2023 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+GCC_14.0 {
+  __speculation_barrier
+}
diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
index 4fb8e136217..d05ef7cbf74 100644
--- a/libgcc/config/mips/t-mips
+++ b/libgcc/config/mips/t-mips
@@ -7,3 +7,10 @@ softfp_truncations :=
 softfp_exclude_libgcc2 := n
 
 LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
+
+
+LIB1ASMSRC = mips/lib1funcs.S
+LIB1ASMFUNCS = _speculation_barrier
+
+# Version these symbols if building libgcc.so.
+SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
index 2bad5119d51..5fd9d60d7a3 100644
--- a/libgcc/config/mips/t-mips16
+++ b/libgcc/config/mips/t-mips16
@@ -16,8 +16,7 @@
 # along with GCC; see the file COPYING3.  If not see
 # <http://www.gnu.org/licenses/>.
 
-LIB1ASMSRC = mips/mips16.S
-LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
+LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
 	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
 	_m16unordsf2 \
 	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
-- 
2.30.2


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5] MIPS: Add speculation_barrier support
  2023-06-01  4:26           ` [PATCH v5] MIPS: Add " YunQiang Su
@ 2023-06-08 12:35             ` Richard Earnshaw (lists)
  2023-06-16  7:53               ` YunQiang Su
  0 siblings, 1 reply; 18+ messages in thread
From: Richard Earnshaw (lists) @ 2023-06-08 12:35 UTC (permalink / raw)
  To: YunQiang Su, gcc-patches
  Cc: macro, jiaxun.yang, syq, richard.sandiford, jeffreyalaw


On 01/06/2023 05:26, YunQiang Su wrote:
> speculation_barrier for MIPS needs sync+jr.hb (r2+),
> so we implement __speculation_barrier in libgcc, like arm32 does.
> 
> gcc/ChangeLog:
> 	* config/mips/mips-protos.h (mips_emit_speculation_barrier): New
>          prototype.
> 	* config/mips/mips.cc (speculation_barrier_libfunc): New static
>          variable.
> 	(mips_init_libfuncs): Initialize it.
> 	(mips_emit_speculation_barrier): New function.
> 	* config/mips/mips.md (speculation_barrier): Call
>          mips_emit_speculation_barrier.
> 
> libgcc/ChangeLog:
> 	* config/mips/lib1funcs.S: New file.
> 	define __speculation_barrier and include mips16.S.
> 	* config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
> 	define LIB1ASMFUNCS as _speculation_barrier.
> 	set version info for __speculation_barrier.
> 	* config/mips/libgcc-mips.ver: New file.
> 	* config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S
> 	included in lib1funcs.S now.
> ---

Please remember to cite PR86793 when committing this fix.

R.

>   gcc/config/mips/mips-protos.h      |  2 +
>   gcc/config/mips/mips.cc            | 12 ++++++
>   gcc/config/mips/mips.md            | 12 ++++++
>   libgcc/config/mips/lib1funcs.S     | 65 ++++++++++++++++++++++++++++++
>   libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
>   libgcc/config/mips/t-mips          |  7 ++++
>   libgcc/config/mips/t-mips16        |  3 +-
>   7 files changed, 120 insertions(+), 2 deletions(-)
>   create mode 100644 libgcc/config/mips/lib1funcs.S
>   create mode 100644 libgcc/config/mips/libgcc-mips.ver
> 
> diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
> index 20483469105..da7902c235b 100644
> --- a/gcc/config/mips/mips-protos.h
> +++ b/gcc/config/mips/mips-protos.h
> @@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
>   extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
>   extern void mips_expand_vec_cmp_expr (rtx *);
>   
> +extern void mips_emit_speculation_barrier_function (void);
> +
>   #endif /* ! GCC_MIPS_PROTOS_H */
> diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
> index ca491b981a3..c1d1691306e 100644
> --- a/gcc/config/mips/mips.cc
> +++ b/gcc/config/mips/mips.cc
> @@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
>     return 0;
>   }
>   
> +
> +static GTY (()) rtx speculation_barrier_libfunc;
> +
>   /* Implement TARGET_INIT_LIBFUNCS.  */
>   
>   static void
> @@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
>         synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
>         init_sync_libfuncs (UNITS_PER_WORD);
>       }
> +  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
>   }
>   
>   /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
> @@ -19092,6 +19096,14 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
>         }
>   }
>   
> +/* Emit a speculation barrier.
> +   JR.HB is needed, so we put speculation_barrier_libfunc in libgcc.  */
> +void
> +mips_emit_speculation_barrier_function ()
> +{
> +  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
> +}
> +
>   /* A SEQUENCE is breakable iff the branch inside it has a compact form
>      and the target has compact branches.  */
>   
> diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
> index ac1d77afc7d..5d04ac566dd 100644
> --- a/gcc/config/mips/mips.md
> +++ b/gcc/config/mips/mips.md
> @@ -160,6 +160,8 @@
>     ;; The `.insn' pseudo-op.
>     UNSPEC_INSN_PSEUDO
>     UNSPEC_JRHB
> +
> +  VUNSPEC_SPECULATION_BARRIER
>   ])
>   
>   (define_constants
> @@ -7455,6 +7457,16 @@
>     mips_expand_conditional_move (operands);
>     DONE;
>   })
> +
> +(define_expand "speculation_barrier"
> +  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
> +  ""
> +  "
> +  mips_emit_speculation_barrier_function ();
> +  DONE;
> +  "
> +)
> +
>   \f
>   ;;
>   ;;  ....................
> diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
> new file mode 100644
> index 00000000000..97a3655e8ab
> --- /dev/null
> +++ b/libgcc/config/mips/lib1funcs.S
> @@ -0,0 +1,65 @@
> +/* Copyright (C) 2023 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
> +Free Software Foundation; either version 3, or (at your option) any
> +later version.
> +
> +This file is distributed in the hope that it will be useful, but
> +WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +General Public License for more details.
> +
> +Under Section 7 of GPL version 3, you are granted additional
> +permissions described in the GCC Runtime Library Exception, version
> +3.1, as published by the Free Software Foundation.
> +
> +You should have received a copy of the GNU General Public License and
> +a copy of the GCC Runtime Library Exception along with this program;
> +see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
> +<http://www.gnu.org/licenses/>.  */
> +
> +//#include "mips16.S"
> +
> +#ifdef L_speculation_barrier
> +
> +/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
> +   So, we use normal MIPS code here, just like what we do for __sync_*.  */
> +	.set nomips16
> +
> +	.set noreorder
> +	.globl	__speculation_barrier
> +	.ent	__speculation_barrier
> +
> +__speculation_barrier:
> +	.set	push
> +#if __mips >= 2
> +	sync /* complementation barrier for memory.  */
> +#elif defined (__linux)
> +	/* MIPS1 has no sync, while Linux can trap&emu sync.  */
> +	/* FIXME: Will somebody use linux/gcc for MIPS1/baremetal?  */
> +	.word 0x0000000f
> +#endif
> +
> +
> +#if __mips_isa_rev >= 1
> +	/* Binutils claims that JR in R1 can do same as jr.hb.
> +	   R6 changes the encoding of jr.hb.  */
> +	jr.hb	$ra /* Jump with instruction hazard barrier.  */
> +#else
> +	/* ssnop is actually available since R5500,
> +	   and it will be decoded as nop on earlier processors.
> +	   gas can only recognize it with -march=mips1 since 2.21.
> +	   MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
> +	   so 8 ssnop is sufficient to block all speculation on all CPUs.  */
> +	.rept 8
> +	.word	0x00000040 /* The encoding of ssnop.  */
> +	.endr
> +	/* jr.hb will be decoded as jr on earlier processors.  */
> +	.word	0x03e00408 /* The encoding of jr.hb $ra.  */
> +#endif
> +	.set	pop
> +	.end	__speculation_barrier
> +
> +	.set reorder
> +#endif
> diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
> new file mode 100644
> index 00000000000..68f8d2bbd51
> --- /dev/null
> +++ b/libgcc/config/mips/libgcc-mips.ver
> @@ -0,0 +1,21 @@
> +# Copyright (C) 2023 Free Software Foundation, Inc.
> +#
> +# This file is part of GCC.
> +#
> +# GCC is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3, or (at your option)
> +# any later version.
> +#
> +# GCC is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with GCC; see the file COPYING3.  If not see
> +# <http://www.gnu.org/licenses/>.
> +
> +GCC_14.0 {
> +  __speculation_barrier
> +}
> diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
> index 4fb8e136217..d05ef7cbf74 100644
> --- a/libgcc/config/mips/t-mips
> +++ b/libgcc/config/mips/t-mips
> @@ -7,3 +7,10 @@ softfp_truncations :=
>   softfp_exclude_libgcc2 := n
>   
>   LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
> +
> +
> +LIB1ASMSRC = mips/lib1funcs.S
> +LIB1ASMFUNCS = _speculation_barrier
> +
> +# Version these symbols if building libgcc.so.
> +SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
> diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
> index 2bad5119d51..5fd9d60d7a3 100644
> --- a/libgcc/config/mips/t-mips16
> +++ b/libgcc/config/mips/t-mips16
> @@ -16,8 +16,7 @@
>   # along with GCC; see the file COPYING3.  If not see
>   # <http://www.gnu.org/licenses/>.
>   
> -LIB1ASMSRC = mips/mips16.S
> -LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
> +LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
>   	_m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
>   	_m16unordsf2 \
>   	_m16fltsisf _m16fix_truncsfsi _m16fltunsisf \


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5] MIPS: Add speculation_barrier support
  2023-06-08 12:35             ` Richard Earnshaw (lists)
@ 2023-06-16  7:53               ` YunQiang Su
  2023-06-16  8:38                 ` Xi Ruoyao
  0 siblings, 1 reply; 18+ messages in thread
From: YunQiang Su @ 2023-06-16  7:53 UTC (permalink / raw)
  To: Richard Earnshaw (lists)
  Cc: YunQiang Su, gcc-patches, macro, jiaxun.yang, richard.sandiford,
	jeffreyalaw

Richard Earnshaw (lists) via Gcc-patches <gcc-patches@gcc.gnu.org>
于2023年6月8日周四 20:36写道:
>
>
> On 01/06/2023 05:26, YunQiang Su wrote:
> > speculation_barrier for MIPS needs sync+jr.hb (r2+),
> > so we implement __speculation_barrier in libgcc, like arm32 does.
> >
> > gcc/ChangeLog:
> >       * config/mips/mips-protos.h (mips_emit_speculation_barrier): New
> >          prototype.
> >       * config/mips/mips.cc (speculation_barrier_libfunc): New static
> >          variable.
> >       (mips_init_libfuncs): Initialize it.
> >       (mips_emit_speculation_barrier): New function.
> >       * config/mips/mips.md (speculation_barrier): Call
> >          mips_emit_speculation_barrier.
> >
> > libgcc/ChangeLog:
> >       * config/mips/lib1funcs.S: New file.
> >       define __speculation_barrier and include mips16.S.
> >       * config/mips/t-mips: define LIB1ASMSRC as mips/lib1funcs.S.
> >       define LIB1ASMFUNCS as _speculation_barrier.
> >       set version info for __speculation_barrier.
> >       * config/mips/libgcc-mips.ver: New file.
> >       * config/mips/t-mips16: don't define LIB1ASMSRC as mips16.S
> >       included in lib1funcs.S now.
> > ---
>
> Please remember to cite PR86793 when committing this fix.
>

Ohh, sorry. I forget it. I commented there.
I have no permission to close this bug report. Can you help to close it?

> R.
>
> >   gcc/config/mips/mips-protos.h      |  2 +
> >   gcc/config/mips/mips.cc            | 12 ++++++
> >   gcc/config/mips/mips.md            | 12 ++++++
> >   libgcc/config/mips/lib1funcs.S     | 65 ++++++++++++++++++++++++++++++
> >   libgcc/config/mips/libgcc-mips.ver | 21 ++++++++++
> >   libgcc/config/mips/t-mips          |  7 ++++
> >   libgcc/config/mips/t-mips16        |  3 +-
> >   7 files changed, 120 insertions(+), 2 deletions(-)
> >   create mode 100644 libgcc/config/mips/lib1funcs.S
> >   create mode 100644 libgcc/config/mips/libgcc-mips.ver
> >
> > diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
> > index 20483469105..da7902c235b 100644
> > --- a/gcc/config/mips/mips-protos.h
> > +++ b/gcc/config/mips/mips-protos.h
> > @@ -388,4 +388,6 @@ extern void mips_register_frame_header_opt (void);
> >   extern void mips_expand_vec_cond_expr (machine_mode, machine_mode, rtx *);
> >   extern void mips_expand_vec_cmp_expr (rtx *);
> >
> > +extern void mips_emit_speculation_barrier_function (void);
> > +
> >   #endif /* ! GCC_MIPS_PROTOS_H */
> > diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
> > index ca491b981a3..c1d1691306e 100644
> > --- a/gcc/config/mips/mips.cc
> > +++ b/gcc/config/mips/mips.cc
> > @@ -13611,6 +13611,9 @@ mips_autovectorize_vector_modes (vector_modes *modes, bool)
> >     return 0;
> >   }
> >
> > +
> > +static GTY (()) rtx speculation_barrier_libfunc;
> > +
> >   /* Implement TARGET_INIT_LIBFUNCS.  */
> >
> >   static void
> > @@ -13680,6 +13683,7 @@ mips_init_libfuncs (void)
> >         synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
> >         init_sync_libfuncs (UNITS_PER_WORD);
> >       }
> > +  speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
> >   }
> >
> >   /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
> > @@ -19092,6 +19096,14 @@ mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
> >         }
> >   }
> >
> > +/* Emit a speculation barrier.
> > +   JR.HB is needed, so we put speculation_barrier_libfunc in libgcc.  */
> > +void
> > +mips_emit_speculation_barrier_function ()
> > +{
> > +  emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
> > +}
> > +
> >   /* A SEQUENCE is breakable iff the branch inside it has a compact form
> >      and the target has compact branches.  */
> >
> > diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
> > index ac1d77afc7d..5d04ac566dd 100644
> > --- a/gcc/config/mips/mips.md
> > +++ b/gcc/config/mips/mips.md
> > @@ -160,6 +160,8 @@
> >     ;; The `.insn' pseudo-op.
> >     UNSPEC_INSN_PSEUDO
> >     UNSPEC_JRHB
> > +
> > +  VUNSPEC_SPECULATION_BARRIER
> >   ])
> >
> >   (define_constants
> > @@ -7455,6 +7457,16 @@
> >     mips_expand_conditional_move (operands);
> >     DONE;
> >   })
> > +
> > +(define_expand "speculation_barrier"
> > +  [(unspec_volatile [(const_int 0)] VUNSPEC_SPECULATION_BARRIER)]
> > +  ""
> > +  "
> > +  mips_emit_speculation_barrier_function ();
> > +  DONE;
> > +  "
> > +)
> > +
> >
> >   ;;
> >   ;;  ....................
> > diff --git a/libgcc/config/mips/lib1funcs.S b/libgcc/config/mips/lib1funcs.S
> > new file mode 100644
> > index 00000000000..97a3655e8ab
> > --- /dev/null
> > +++ b/libgcc/config/mips/lib1funcs.S
> > @@ -0,0 +1,65 @@
> > +/* Copyright (C) 2023 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
> > +Free Software Foundation; either version 3, or (at your option) any
> > +later version.
> > +
> > +This file is distributed in the hope that it will be useful, but
> > +WITHOUT ANY WARRANTY; without even the implied warranty of
> > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> > +General Public License for more details.
> > +
> > +Under Section 7 of GPL version 3, you are granted additional
> > +permissions described in the GCC Runtime Library Exception, version
> > +3.1, as published by the Free Software Foundation.
> > +
> > +You should have received a copy of the GNU General Public License and
> > +a copy of the GCC Runtime Library Exception along with this program;
> > +see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
> > +<http://www.gnu.org/licenses/>.  */
> > +
> > +//#include "mips16.S"
> > +
> > +#ifdef L_speculation_barrier
> > +
> > +/* MIPS16e1 has no sync/jr.hb instructions, and MIPS16e2 lacks of jr.hb.
> > +   So, we use normal MIPS code here, just like what we do for __sync_*.  */
> > +     .set nomips16
> > +
> > +     .set noreorder
> > +     .globl  __speculation_barrier
> > +     .ent    __speculation_barrier
> > +
> > +__speculation_barrier:
> > +     .set    push
> > +#if __mips >= 2
> > +     sync /* complementation barrier for memory.  */
> > +#elif defined (__linux)
> > +     /* MIPS1 has no sync, while Linux can trap&emu sync.  */
> > +     /* FIXME: Will somebody use linux/gcc for MIPS1/baremetal?  */
> > +     .word 0x0000000f
> > +#endif
> > +
> > +
> > +#if __mips_isa_rev >= 1
> > +     /* Binutils claims that JR in R1 can do same as jr.hb.
> > +        R6 changes the encoding of jr.hb.  */
> > +     jr.hb   $ra /* Jump with instruction hazard barrier.  */
> > +#else
> > +     /* ssnop is actually available since R5500,
> > +        and it will be decoded as nop on earlier processors.
> > +        gas can only recognize it with -march=mips1 since 2.21.
> > +        MIPS1 to MIPSr1: R10000 have 7 stage pipeline,
> > +        so 8 ssnop is sufficient to block all speculation on all CPUs.  */
> > +     .rept 8
> > +     .word   0x00000040 /* The encoding of ssnop.  */
> > +     .endr
> > +     /* jr.hb will be decoded as jr on earlier processors.  */
> > +     .word   0x03e00408 /* The encoding of jr.hb $ra.  */
> > +#endif
> > +     .set    pop
> > +     .end    __speculation_barrier
> > +
> > +     .set reorder
> > +#endif
> > diff --git a/libgcc/config/mips/libgcc-mips.ver b/libgcc/config/mips/libgcc-mips.ver
> > new file mode 100644
> > index 00000000000..68f8d2bbd51
> > --- /dev/null
> > +++ b/libgcc/config/mips/libgcc-mips.ver
> > @@ -0,0 +1,21 @@
> > +# Copyright (C) 2023 Free Software Foundation, Inc.
> > +#
> > +# This file is part of GCC.
> > +#
> > +# GCC is free software; you can redistribute it and/or modify
> > +# it under the terms of the GNU General Public License as published by
> > +# the Free Software Foundation; either version 3, or (at your option)
> > +# any later version.
> > +#
> > +# GCC is distributed in the hope that it will be useful,
> > +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +# GNU General Public License for more details.
> > +#
> > +# You should have received a copy of the GNU General Public License
> > +# along with GCC; see the file COPYING3.  If not see
> > +# <http://www.gnu.org/licenses/>.
> > +
> > +GCC_14.0 {
> > +  __speculation_barrier
> > +}
> > diff --git a/libgcc/config/mips/t-mips b/libgcc/config/mips/t-mips
> > index 4fb8e136217..d05ef7cbf74 100644
> > --- a/libgcc/config/mips/t-mips
> > +++ b/libgcc/config/mips/t-mips
> > @@ -7,3 +7,10 @@ softfp_truncations :=
> >   softfp_exclude_libgcc2 := n
> >
> >   LIB2ADD_ST += $(srcdir)/config/mips/lib2funcs.c
> > +
> > +
> > +LIB1ASMSRC = mips/lib1funcs.S
> > +LIB1ASMFUNCS = _speculation_barrier
> > +
> > +# Version these symbols if building libgcc.so.
> > +SHLIB_MAPFILES += $(srcdir)/config/mips/libgcc-mips.ver
> > diff --git a/libgcc/config/mips/t-mips16 b/libgcc/config/mips/t-mips16
> > index 2bad5119d51..5fd9d60d7a3 100644
> > --- a/libgcc/config/mips/t-mips16
> > +++ b/libgcc/config/mips/t-mips16
> > @@ -16,8 +16,7 @@
> >   # along with GCC; see the file COPYING3.  If not see
> >   # <http://www.gnu.org/licenses/>.
> >
> > -LIB1ASMSRC = mips/mips16.S
> > -LIB1ASMFUNCS = _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
> > +LIB1ASMFUNCS += _m16addsf3 _m16subsf3 _m16mulsf3 _m16divsf3 \
> >       _m16eqsf2 _m16nesf2 _m16gtsf2 _m16gesf2 _m16lesf2 _m16ltsf2 \
> >       _m16unordsf2 \
> >       _m16fltsisf _m16fix_truncsfsi _m16fltunsisf \
>

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v5] MIPS: Add speculation_barrier support
  2023-06-16  7:53               ` YunQiang Su
@ 2023-06-16  8:38                 ` Xi Ruoyao
  0 siblings, 0 replies; 18+ messages in thread
From: Xi Ruoyao @ 2023-06-16  8:38 UTC (permalink / raw)
  To: YunQiang Su, Richard Earnshaw (lists)
  Cc: YunQiang Su, gcc-patches, macro, jiaxun.yang, richard.sandiford,
	jeffreyalaw

On Fri, 2023-06-16 at 15:53 +0800, YunQiang Su wrote:
> Ohh, sorry. I forget it. I commented there.
> I have no permission to close this bug report. Can you help to close
> it?

Modify the email address of your Bugzilla account to your @gcc.gnu.org
address, then you should be able to close it.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2023-06-16  8:38 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-28 12:33 [PATCH] MIPS: add speculation_barrier support YunQiang Su
2023-04-28 12:36 ` Jiaxun Yang
2023-04-28 13:07   ` YunQiang Su
2023-04-28 13:12 ` [PATCH v2] " YunQiang Su
2023-05-03 18:29   ` Richard Sandiford
2023-05-03 21:04     ` Maciej W. Rozycki
2023-05-03 22:12       ` Jiaxun Yang
2023-05-07 17:34         ` Maciej W. Rozycki
2023-05-07 18:47           ` Jiaxun Yang
2023-05-07 19:16             ` Maciej W. Rozycki
2023-05-12 10:03   ` [PATCH v3] " YunQiang Su
2023-05-12 10:30     ` [PATCH v4] " YunQiang Su
2023-05-31  9:43       ` YunQiang Su
2023-05-31 10:35         ` Maciej W. Rozycki
2023-06-01  4:26           ` [PATCH v5] MIPS: Add " YunQiang Su
2023-06-08 12:35             ` Richard Earnshaw (lists)
2023-06-16  7:53               ` YunQiang Su
2023-06-16  8:38                 ` Xi Ruoyao

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).