[rs6000] Add __builtin_speculation_barrier

Message ID 38902065-4936-16de-8057-0859eccc8c79@linux.vnet.ibm.com
State New
Headers show
Series
  • [rs6000] Add __builtin_speculation_barrier
Related show

Commit Message

Bill Schmidt Jan. 11, 2018, 4:25 p.m.
Hi,

This patch adds a pattern for "ori 31,31,0" as a speculation barrier, and
an associated built-in function.  This is currently restricted to Power7
and later.  Documentation and test included.

Bootstrapped and tested on powerpc64le-linux-gnu with no regressions.  Is
this okay for trunk?

Thanks,
Bill


[gcc]

2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	* config/rs6000/rs6000-builtin.def (BU_P7_MISC_X): New #define.
	(SPEC_BARRIER): New instantiation of BU_P7_MISC_X.
	* config/rs6000/rs6000.c (rs6000_expand_builtin): Handle
	MISC_BUILTIN_SPEC_BARRIER.
	(rs6000_init_builtins): Likewise.
	* config/rs6000/rs6000.md (UNSPECV_SPEC_BARRIER): New UNSPECV
	enum value.
	(speculation_barrier): New define_insn.
	* doc/extend.texi: Document __builtin_speculation_barrier.

[gcc/testsuite]

2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	* gcc.target/powerpc/spec-barr-1.c: New file.

Comments

Segher Boessenkool Jan. 11, 2018, 5:14 p.m. | #1
On Thu, Jan 11, 2018 at 10:25:25AM -0600, Bill Schmidt wrote:
> This patch adds a pattern for "ori 31,31,0" as a speculation barrier, and
> an associated built-in function.  This is currently restricted to Power7
> and later.  Documentation and test included.
> 
> Bootstrapped and tested on powerpc64le-linux-gnu with no regressions.  Is
> this okay for trunk?

Tiny thing:

> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
> 
> 	* config/rs6000/rs6000-builtin.def (BU_P7_MISC_X): New #define.
> 	(SPEC_BARRIER): New instantiation of BU_P7_MISC_X.
> 	* config/rs6000/rs6000.c (rs6000_expand_builtin): Handle
> 	MISC_BUILTIN_SPEC_BARRIER.
> 	(rs6000_init_builtins): Likewise.
> 	* config/rs6000/rs6000.md (UNSPECV_SPEC_BARRIER): New UNSPECV
> 	enum value.
> 	(speculation_barrier): New define_insn.
> 	* doc/extend.texi: Document __builtin_speculation_barrier.
> 
> [gcc/testsuite]
> 
> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
> 
> 	* gcc.target/powerpc/spec-barr-1.c: New file.

> --- gcc/doc/extend.texi	(revision 256364)
> +++ gcc/doc/extend.texi	(working copy)
> @@ -15734,6 +15734,7 @@ unsigned long __builtin_divdeuo (unsigned long, un
>  unsigned int cdtbcd (unsigned int);
>  unsigned int cbcdtd (unsigned int);
>  unsigned int addg6s (unsigned int, unsigned int);
> +void __builtin_speculation_barrier ();

void __builtin_speculation_barrier (void);

Okay for trunk with that fixed.  Pre-approved for backports, too, if you
want some.


Segher
Richard Biener Jan. 11, 2018, 5:16 p.m. | #2
On January 11, 2018 6:14:34 PM GMT+01:00, Segher Boessenkool <segher@kernel.crashing.org> wrote:
>On Thu, Jan 11, 2018 at 10:25:25AM -0600, Bill Schmidt wrote:
>> This patch adds a pattern for "ori 31,31,0" as a speculation barrier,
>and
>> an associated built-in function.  This is currently restricted to
>Power7
>> and later.  Documentation and test included.
>> 
>> Bootstrapped and tested on powerpc64le-linux-gnu with no regressions.
> Is
>> this okay for trunk?

Can you name it __builtin_rs6000_speculation_barrier() or similar please? 

>Tiny thing:
>
>> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
>> 
>> 	* config/rs6000/rs6000-builtin.def (BU_P7_MISC_X): New #define.
>> 	(SPEC_BARRIER): New instantiation of BU_P7_MISC_X.
>> 	* config/rs6000/rs6000.c (rs6000_expand_builtin): Handle
>> 	MISC_BUILTIN_SPEC_BARRIER.
>> 	(rs6000_init_builtins): Likewise.
>> 	* config/rs6000/rs6000.md (UNSPECV_SPEC_BARRIER): New UNSPECV
>> 	enum value.
>> 	(speculation_barrier): New define_insn.
>> 	* doc/extend.texi: Document __builtin_speculation_barrier.
>> 
>> [gcc/testsuite]
>> 
>> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
>> 
>> 	* gcc.target/powerpc/spec-barr-1.c: New file.
>
>> --- gcc/doc/extend.texi	(revision 256364)
>> +++ gcc/doc/extend.texi	(working copy)
>> @@ -15734,6 +15734,7 @@ unsigned long __builtin_divdeuo (unsigned
>long, un
>>  unsigned int cdtbcd (unsigned int);
>>  unsigned int cbcdtd (unsigned int);
>>  unsigned int addg6s (unsigned int, unsigned int);
>> +void __builtin_speculation_barrier ();
>
>void __builtin_speculation_barrier (void);
>
>Okay for trunk with that fixed.  Pre-approved for backports, too, if
>you
>want some.
>
>
>Segher
Bill Schmidt Jan. 11, 2018, 5:17 p.m. | #3
On Jan 11, 2018, at 11:16 AM, Richard Biener <richard.guenther@gmail.com> wrote:
> 
> On January 11, 2018 6:14:34 PM GMT+01:00, Segher Boessenkool <segher@kernel.crashing.org> wrote:
>> On Thu, Jan 11, 2018 at 10:25:25AM -0600, Bill Schmidt wrote:
>>> This patch adds a pattern for "ori 31,31,0" as a speculation barrier,
>> and
>>> an associated built-in function.  This is currently restricted to
>> Power7
>>> and later.  Documentation and test included.
>>> 
>>> Bootstrapped and tested on powerpc64le-linux-gnu with no regressions.
>> Is
>>> this okay for trunk?
> 
> Can you name it __builtin_rs6000_speculation_barrier() or similar please? 

Sure, Richard.

Thanks, 
Bill
> 
>> Tiny thing:
>> 
>>> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
>>> 
>>> 	* config/rs6000/rs6000-builtin.def (BU_P7_MISC_X): New #define.
>>> 	(SPEC_BARRIER): New instantiation of BU_P7_MISC_X.
>>> 	* config/rs6000/rs6000.c (rs6000_expand_builtin): Handle
>>> 	MISC_BUILTIN_SPEC_BARRIER.
>>> 	(rs6000_init_builtins): Likewise.
>>> 	* config/rs6000/rs6000.md (UNSPECV_SPEC_BARRIER): New UNSPECV
>>> 	enum value.
>>> 	(speculation_barrier): New define_insn.
>>> 	* doc/extend.texi: Document __builtin_speculation_barrier.
>>> 
>>> [gcc/testsuite]
>>> 
>>> 2018-01-11  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>
>>> 
>>> 	* gcc.target/powerpc/spec-barr-1.c: New file.
>> 
>>> --- gcc/doc/extend.texi	(revision 256364)
>>> +++ gcc/doc/extend.texi	(working copy)
>>> @@ -15734,6 +15734,7 @@ unsigned long __builtin_divdeuo (unsigned
>> long, un
>>> unsigned int cdtbcd (unsigned int);
>>> unsigned int cbcdtd (unsigned int);
>>> unsigned int addg6s (unsigned int, unsigned int);
>>> +void __builtin_speculation_barrier ();
>> 
>> void __builtin_speculation_barrier (void);
>> 
>> Okay for trunk with that fixed.  Pre-approved for backports, too, if
>> you
>> want some.
>> 
>> 
>> Segher
>

Patch

Index: gcc/config/rs6000/rs6000-builtin.def
===================================================================
--- gcc/config/rs6000/rs6000-builtin.def	(revision 256364)
+++ gcc/config/rs6000/rs6000-builtin.def	(working copy)
@@ -638,7 +638,15 @@ 
 		     | RS6000_BTC_BINARY),				\
 		    CODE_FOR_ ## ICODE)			/* ICODE */
 
+#define BU_P7_MISC_X(ENUM, NAME, ATTR)					\
+  RS6000_BUILTIN_X (MISC_BUILTIN_ ## ENUM,		/* ENUM */	\
+		    "__builtin_" NAME,			/* NAME */	\
+		    RS6000_BTM_POPCNTD,			/* MASK */	\
+		    (RS6000_BTC_ ## ATTR		/* ATTR */	\
+		     | RS6000_BTC_SPECIAL),				\
+		    CODE_FOR_nothing)			/* ICODE */
 
+
 /* Miscellaneous builtins for instructions added in ISA 2.07.  These
    instructions do require the ISA 2.07 vector support, but they aren't vector
    instructions.  */
@@ -2317,6 +2325,11 @@  BU_DFP_MISC_2 (DSCLIQ,		"dscliq",	CONST,	dfp_dscli
 BU_DFP_MISC_2 (DSCRI,		"dscri",	CONST,	dfp_dscri_dd)
 BU_DFP_MISC_2 (DSCRIQ,		"dscriq",	CONST,	dfp_dscri_td)
 
+/* 0 argument void function that we pretend was added in ISA 2.06.
+   It's a special nop recognized by 2018+ firmware for P7 and up,
+   with speculation barrier semantics.  */
+BU_P7_MISC_X (SPEC_BARRIER,	"speculation_barrier",	MISC)
+
 /* 1 argument BCD functions added in ISA 2.06.  */
 BU_P7_MISC_1 (CDTBCD,		"cdtbcd",	CONST,	cdtbcd)
 BU_P7_MISC_1 (CBCDTD,		"cbcdtd",	CONST,	cbcdtd)
Index: gcc/config/rs6000/rs6000.c
===================================================================
--- gcc/config/rs6000/rs6000.c	(revision 256364)
+++ gcc/config/rs6000/rs6000.c	(working copy)
@@ -16792,6 +16792,12 @@  rs6000_expand_builtin (tree exp, rtx target, rtx s
     case RS6000_BUILTIN_CPU_SUPPORTS:
       return cpu_expand_builtin (fcode, exp, target);
 
+    case MISC_BUILTIN_SPEC_BARRIER:
+      {
+	emit_insn (gen_speculation_barrier ());
+	return NULL_RTX;
+      }
+
     case ALTIVEC_BUILTIN_MASK_FOR_LOAD:
     case ALTIVEC_BUILTIN_MASK_FOR_STORE:
       {
@@ -17164,6 +17170,8 @@  rs6000_init_builtins (void)
 
   ftype = build_function_type_list (void_type_node, NULL_TREE);
   def_builtin ("__builtin_cpu_init", ftype, RS6000_BUILTIN_CPU_INIT);
+  def_builtin ("__builtin_speculation_barrier", ftype,
+	       MISC_BUILTIN_SPEC_BARRIER);
 
   ftype = build_function_type_list (bool_int_type_node, const_ptr_type_node,
 				    NULL_TREE);
Index: gcc/config/rs6000/rs6000.md
===================================================================
--- gcc/config/rs6000/rs6000.md	(revision 256364)
+++ gcc/config/rs6000/rs6000.md	(working copy)
@@ -168,6 +168,7 @@ 
    UNSPECV_MFFS			; Move from FPSCR
    UNSPECV_MTFSF		; Move to FPSCR Fields
    UNSPECV_SPLIT_STACK_RETURN   ; A camouflaged return
+   UNSPECV_SPEC_BARRIER         ; Speculation barrier
   ])
 
 
@@ -12990,6 +12991,11 @@ 
     return "ori 1,1,0";
   return "ori 2,2,0";
 })
+
+(define_insn "speculation_barrier"
+  [(unspec_volatile:BLK [(const_int 0)] UNSPECV_SPEC_BARRIER)]
+  ""
+  "ori 31,31,0")
 
 ;; Define the subtract-one-and-jump insns, starting with the template
 ;; so loop.c knows what to generate.
Index: gcc/doc/extend.texi
===================================================================
--- gcc/doc/extend.texi	(revision 256364)
+++ gcc/doc/extend.texi	(working copy)
@@ -15734,6 +15734,7 @@  unsigned long __builtin_divdeuo (unsigned long, un
 unsigned int cdtbcd (unsigned int);
 unsigned int cbcdtd (unsigned int);
 unsigned int addg6s (unsigned int, unsigned int);
+void __builtin_speculation_barrier ();
 @end smallexample
 
 The @code{__builtin_divde}, @code{__builtin_divdeo},
Index: gcc/testsuite/gcc.target/powerpc/spec-barr-1.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/spec-barr-1.c	(nonexistent)
+++ gcc/testsuite/gcc.target/powerpc/spec-barr-1.c	(working copy)
@@ -0,0 +1,10 @@ 
+/* { dg-do compile { target { powerpc*-*-* } } } */
+/* { dg-skip-if "do not override -mcpu" { powerpc*-*-* } { "-mcpu=*" } { "-mcpu=power7" } } */
+/* { dg-options "-mcpu=power7" } */
+
+void foo ()
+{
+  __builtin_speculation_barrier ();
+}
+
+/* { dg-final { scan-assembler "ori 31,31,0" } } */