arm: Implement arm Function target attribute 'branch-protection'

Message ID 20230127144311.2188730-1-andrea.corallo@arm.com
State New
Headers
Series arm: Implement arm Function target attribute 'branch-protection' |

Commit Message

Andrea Corallo Jan. 27, 2023, 2:43 p.m. UTC
  gcc/

	* config/arm/arm.cc (arm_valid_target_attribute_rec): Add ARM function
	attribute 'branch-protection' and parse its options.
	* doc/extend.texi: Document ARM Function attribute 'branch-protection'.

gcc/testsuite/

	* gcc.target/arm/acle/pacbti-m-predef-13.c: New test.

Co-Authored-By: Tejas Belagod  <tbelagod@arm.com>
---
 gcc/config/arm/arm.cc                         | 16 ++++++++
 gcc/doc/extend.texi                           |  7 ++++
 .../gcc.target/arm/acle/pacbti-m-predef-13.c  | 41 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
  

Comments

Andrea Corallo Feb. 8, 2023, 2:58 p.m. UTC | #1
Andrea Corallo <andrea.corallo@arm.com> writes:

> gcc/
>
> 	* config/arm/arm.cc (arm_valid_target_attribute_rec): Add ARM function
> 	attribute 'branch-protection' and parse its options.
> 	* doc/extend.texi: Document ARM Function attribute 'branch-protection'.
>
> gcc/testsuite/
>
> 	* gcc.target/arm/acle/pacbti-m-predef-13.c: New test.
>
> Co-Authored-By: Tejas Belagod  <tbelagod@arm.com>
> ---
>  gcc/config/arm/arm.cc                         | 16 ++++++++
>  gcc/doc/extend.texi                           |  7 ++++
>  .../gcc.target/arm/acle/pacbti-m-predef-13.c  | 41 +++++++++++++++++++
>  3 files changed, 64 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
>
> diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc
> index efc48349dd3..add33090f18 100644
> --- a/gcc/config/arm/arm.cc
> +++ b/gcc/config/arm/arm.cc
> @@ -33568,6 +33568,22 @@ arm_valid_target_attribute_rec (tree args, struct gcc_options *opts)
>  
>  	  opts->x_arm_arch_string = xstrndup (arch, strlen (arch));
>  	}
> +      else if (startswith (q, "branch-protection="))
> +	{
> +	  char *bp_str = q + strlen ("branch-protection=");
> +
> +	  opts->x_arm_branch_protection_string
> +	    = xstrndup (bp_str, strlen (bp_str));
> +
> +	  /* Capture values from target attribute.  */
> +	  aarch_validate_mbranch_protection
> +	    (opts->x_arm_branch_protection_string);
> +
> +	  /* Init function target attr values.  */
> +	  opts->x_aarch_ra_sign_scope = aarch_ra_sign_scope;
> +	  opts->x_aarch_enable_bti = aarch_enable_bti;
> +
> +	}
>        else if (q[0] == '+')
>  	{
>  	  opts->x_arm_arch_string
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 4a89a3eae7c..23ee43919dd 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -4492,6 +4492,13 @@ Enable or disable calls to out-of-line helpers to implement atomic operations.
>  This corresponds to the behavior of the command line options
>  @option{-moutline-atomics} and @option{-mno-outline-atomics}.
>  
> +@item branch-protection=
> +@cindex @code{branch-protection=} function attribute, arm
> +Select the function scope on which branch protection will be applied.
> +The behavior and permissible arguments are the same as for the
> +command-line option @option{-mbranch-protection=}.  The default value
> +is @code{none}.
> +
>  @end table
>  
>  The above target attributes can be specified as follows:
> diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
> new file mode 100644
> index 00000000000..b6d2df53072
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
> @@ -0,0 +1,41 @@
> +/* { dg-do compile } */
> +/* { dg-require-effective-target mbranch_protection_ok } */
> +/* { dg-options "-march=armv8.1-m.main+fp -mbranch-protection=pac-ret+leaf -mfloat-abi=hard --save-temps" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#if defined (__ARM_FEATURE_BTI_DEFAULT)
> +#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be undefined."
> +#endif
> +
> +#if !defined (__ARM_FEATURE_PAC_DEFAULT)
> +#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
> +#endif
> +
> +/*
> +**foo:
> +**	bti
> +**	...
> +*/
> +__attribute__((target("branch-protection=pac-ret+bti"), noinline))
> +int foo ()
> +{
> +  return 3;
> +}
> +
> +/*
> +**main:
> +**	pac	ip, lr, sp
> +**	...
> +**	aut	ip, lr, sp
> +**	bx	lr
> +*/
> +int
> +main()
> +{
> +  return 1 + foo ();
> +}
> +
> +/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
> +/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
> +/* { dg-final { scan-assembler-not "\.eabi_attribute 74" } } */
> +/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */

Ping

  Andrea
  
Ramana Radhakrishnan April 22, 2023, 9:14 p.m. UTC | #2
On Fri, Jan 27, 2023 at 2:44 PM Andrea Corallo via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> gcc/
>
>         * config/arm/arm.cc (arm_valid_target_attribute_rec): Add ARM function
>         attribute 'branch-protection' and parse its options.
>         * doc/extend.texi: Document ARM Function attribute 'branch-protection'.


Nit: s/ARM/arm or s/ARM/AArch32.

>
> gcc/testsuite/
>
>         * gcc.target/arm/acle/pacbti-m-predef-13.c: New test.
>
> Co-Authored-By: Tejas Belagod  <tbelagod@arm.com>
> ---
>  gcc/config/arm/arm.cc                         | 16 ++++++++
>  gcc/doc/extend.texi                           |  7 ++++
>  .../gcc.target/arm/acle/pacbti-m-predef-13.c  | 41 +++++++++++++++++++
>  3 files changed, 64 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
>
> diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc
> index efc48349dd3..add33090f18 100644
> --- a/gcc/config/arm/arm.cc
> +++ b/gcc/config/arm/arm.cc
> @@ -33568,6 +33568,22 @@ arm_valid_target_attribute_rec (tree args, struct gcc_options *opts)
>
>           opts->x_arm_arch_string = xstrndup (arch, strlen (arch));
>         }
> +      else if (startswith (q, "branch-protection="))
> +       {
> +         char *bp_str = q + strlen ("branch-protection=");
> +
> +         opts->x_arm_branch_protection_string
> +           = xstrndup (bp_str, strlen (bp_str));
> +
> +         /* Capture values from target attribute.  */
> +         aarch_validate_mbranch_protection
> +           (opts->x_arm_branch_protection_string);
> +
> +         /* Init function target attr values.  */
> +         opts->x_aarch_ra_sign_scope = aarch_ra_sign_scope;
> +         opts->x_aarch_enable_bti = aarch_enable_bti;
> +
> +       }
>        else if (q[0] == '+')
>         {
>           opts->x_arm_arch_string
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 4a89a3eae7c..23ee43919dd 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -4492,6 +4492,13 @@ Enable or disable calls to out-of-line helpers to implement atomic operations.
>  This corresponds to the behavior of the command line options
>  @option{-moutline-atomics} and @option{-mno-outline-atomics}.
>
> +@item branch-protection=
> +@cindex @code{branch-protection=} function attribute, arm
> +Select the function scope on which branch protection will be applied.
> +The behavior and permissible arguments are the same as for the
> +command-line option @option{-mbranch-protection=}.  The default value
> +is @code{none}.
> +
>  @end table
>
>  The above target attributes can be specified as follows:
> diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
> new file mode 100644
> index 00000000000..b6d2df53072
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
> @@ -0,0 +1,41 @@
> +/* { dg-do compile } */
> +/* { dg-require-effective-target mbranch_protection_ok } */
> +/* { dg-options "-march=armv8.1-m.main+fp -mbranch-protection=pac-ret+leaf -mfloat-abi=hard --save-temps" } */
> +/* { dg-final { check-function-bodies "**" "" } } */
> +
> +#if defined (__ARM_FEATURE_BTI_DEFAULT)
> +#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be undefined."
> +#endif
> +
> +#if !defined (__ARM_FEATURE_PAC_DEFAULT)
> +#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
> +#endif
> +
> +/*
> +**foo:
> +**     bti
> +**     ...
> +*/
> +__attribute__((target("branch-protection=pac-ret+bti"), noinline))
> +int foo ()
> +{
> +  return 3;
> +}
> +
> +/*
> +**main:
> +**     pac     ip, lr, sp
> +**     ...
> +**     aut     ip, lr, sp
> +**     bx      lr
> +*/
> +int
> +main()
> +{
> +  return 1 + foo ();
> +}
> +
> +/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
> +/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
> +/* { dg-final { scan-assembler-not "\.eabi_attribute 74" } } */
> +/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */
> --
> 2.25.1
>


If the attribute covers all the options that are in the command line
documentation, surely there need to be tests for
__attribute__((target("branch-protection=standard")) ?


regards
Ramana
  

Patch

diff --git a/gcc/config/arm/arm.cc b/gcc/config/arm/arm.cc
index efc48349dd3..add33090f18 100644
--- a/gcc/config/arm/arm.cc
+++ b/gcc/config/arm/arm.cc
@@ -33568,6 +33568,22 @@  arm_valid_target_attribute_rec (tree args, struct gcc_options *opts)
 
 	  opts->x_arm_arch_string = xstrndup (arch, strlen (arch));
 	}
+      else if (startswith (q, "branch-protection="))
+	{
+	  char *bp_str = q + strlen ("branch-protection=");
+
+	  opts->x_arm_branch_protection_string
+	    = xstrndup (bp_str, strlen (bp_str));
+
+	  /* Capture values from target attribute.  */
+	  aarch_validate_mbranch_protection
+	    (opts->x_arm_branch_protection_string);
+
+	  /* Init function target attr values.  */
+	  opts->x_aarch_ra_sign_scope = aarch_ra_sign_scope;
+	  opts->x_aarch_enable_bti = aarch_enable_bti;
+
+	}
       else if (q[0] == '+')
 	{
 	  opts->x_arm_arch_string
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 4a89a3eae7c..23ee43919dd 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -4492,6 +4492,13 @@  Enable or disable calls to out-of-line helpers to implement atomic operations.
 This corresponds to the behavior of the command line options
 @option{-moutline-atomics} and @option{-mno-outline-atomics}.
 
+@item branch-protection=
+@cindex @code{branch-protection=} function attribute, arm
+Select the function scope on which branch protection will be applied.
+The behavior and permissible arguments are the same as for the
+command-line option @option{-mbranch-protection=}.  The default value
+is @code{none}.
+
 @end table
 
 The above target attributes can be specified as follows:
diff --git a/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
new file mode 100644
index 00000000000..b6d2df53072
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/acle/pacbti-m-predef-13.c
@@ -0,0 +1,41 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target mbranch_protection_ok } */
+/* { dg-options "-march=armv8.1-m.main+fp -mbranch-protection=pac-ret+leaf -mfloat-abi=hard --save-temps" } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#if defined (__ARM_FEATURE_BTI_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_BTI_DEFAULT should be undefined."
+#endif
+
+#if !defined (__ARM_FEATURE_PAC_DEFAULT)
+#error "Feature test macro __ARM_FEATURE_PAC_DEFAULT should be defined."
+#endif
+
+/*
+**foo:
+**	bti
+**	...
+*/
+__attribute__((target("branch-protection=pac-ret+bti"), noinline))
+int foo ()
+{
+  return 3;
+}
+
+/*
+**main:
+**	pac	ip, lr, sp
+**	...
+**	aut	ip, lr, sp
+**	bx	lr
+*/
+int
+main()
+{
+  return 1 + foo ();
+}
+
+/* { dg-final { scan-assembler "\.eabi_attribute 50, 1" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 52, 1" } } */
+/* { dg-final { scan-assembler-not "\.eabi_attribute 74" } } */
+/* { dg-final { scan-assembler "\.eabi_attribute 76, 1" } } */