diff mbox series

s390: Implement TARGET_ATOMIC_ALIGN_FOR_MODE

Message ID 20230516064322.2584953-1-stefansf@linux.ibm.com
State New
Headers show
Series s390: Implement TARGET_ATOMIC_ALIGN_FOR_MODE | expand

Commit Message

Stefan Schulze Frielinghaus May 16, 2023, 6:43 a.m. UTC
So far atomic objects are aligned according to their default alignment.
For 128 bit scalar types like int128 or long double this results in an
8 byte alignment which is wrong and must be 16 byte.

libstdc++ already computes a correct alignment, though, still adding a
test case in order to make sure that both implementations are
compatible.

Bootstrapped and regtested.  Ok for mainline?  Since this is an ABI
break, is a backport to GCC 13 reasonable?

gcc/ChangeLog:

	* config/s390/s390.cc (TARGET_ATOMIC_ALIGN_FOR_MODE):
	New.
	(s390_atomic_align_for_mode): New.

gcc/testsuite/ChangeLog:

	* g++.target/s390/atomic-align-1.C: New test.
	* gcc.target/s390/atomic-align-1.c: New test.
	* gcc.target/s390/atomic-align-2.c: New test.
---
 gcc/config/s390/s390.cc                       |  8 ++++++
 .../g++.target/s390/atomic-align-1.C          | 25 +++++++++++++++++++
 .../gcc.target/s390/atomic-align-1.c          | 23 +++++++++++++++++
 .../gcc.target/s390/atomic-align-2.c          | 18 +++++++++++++
 4 files changed, 74 insertions(+)
 create mode 100644 gcc/testsuite/g++.target/s390/atomic-align-1.C
 create mode 100644 gcc/testsuite/gcc.target/s390/atomic-align-1.c
 create mode 100644 gcc/testsuite/gcc.target/s390/atomic-align-2.c

Comments

Andreas Krebbel May 16, 2023, 8:05 p.m. UTC | #1
On 5/16/23 08:43, Stefan Schulze Frielinghaus wrote:
> So far atomic objects are aligned according to their default alignment.
> For 128 bit scalar types like int128 or long double this results in an
> 8 byte alignment which is wrong and must be 16 byte.
> 
> libstdc++ already computes a correct alignment, though, still adding a
> test case in order to make sure that both implementations are
> compatible.
> 
> Bootstrapped and regtested.  Ok for mainline?  Since this is an ABI
> break, is a backport to GCC 13 reasonable?

Ok for mainline.

I would also like to have it in GCC 13. It is an ABI breakage but on the other hand it also fixes an
ABI inconsistency between C and C++ which we should fix asap I think.

Andreas


> 
> gcc/ChangeLog:
> 
> 	* config/s390/s390.cc (TARGET_ATOMIC_ALIGN_FOR_MODE):
> 	New.
> 	(s390_atomic_align_for_mode): New.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.target/s390/atomic-align-1.C: New test.
> 	* gcc.target/s390/atomic-align-1.c: New test.
> 	* gcc.target/s390/atomic-align-2.c: New test.
> ---
>  gcc/config/s390/s390.cc                       |  8 ++++++
>  .../g++.target/s390/atomic-align-1.C          | 25 +++++++++++++++++++
>  .../gcc.target/s390/atomic-align-1.c          | 23 +++++++++++++++++
>  .../gcc.target/s390/atomic-align-2.c          | 18 +++++++++++++
>  4 files changed, 74 insertions(+)
>  create mode 100644 gcc/testsuite/g++.target/s390/atomic-align-1.C
>  create mode 100644 gcc/testsuite/gcc.target/s390/atomic-align-1.c
>  create mode 100644 gcc/testsuite/gcc.target/s390/atomic-align-2.c
> 
> diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
> index 505de995da8..4813bf91dc4 100644
> --- a/gcc/config/s390/s390.cc
> +++ b/gcc/config/s390/s390.cc
> @@ -450,6 +450,14 @@ s390_preserve_fpr_arg_p (int regno)
>  	  && regno >= FPR0_REGNUM);
>  }
>  
> +#undef TARGET_ATOMIC_ALIGN_FOR_MODE
> +#define TARGET_ATOMIC_ALIGN_FOR_MODE s390_atomic_align_for_mode
> +static unsigned int
> +s390_atomic_align_for_mode (machine_mode mode)
> +{
> +  return GET_MODE_BITSIZE (mode);
> +}
> +
>  /* A couple of shortcuts.  */
>  #define CONST_OK_FOR_J(x) \
>  	CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
> diff --git a/gcc/testsuite/g++.target/s390/atomic-align-1.C b/gcc/testsuite/g++.target/s390/atomic-align-1.C
> new file mode 100644
> index 00000000000..43aa0bc39ed
> --- /dev/null
> +++ b/gcc/testsuite/g++.target/s390/atomic-align-1.C
> @@ -0,0 +1,25 @@
> +/* { dg-do compile { target int128 } } */
> +/* { dg-options "-std=c++11" } */
> +/* { dg-final { scan-assembler-times {\.align\t2} 2 } } */
> +/* { dg-final { scan-assembler-times {\.align\t4} 2 } } */
> +/* { dg-final { scan-assembler-times {\.align\t8} 3 } } */
> +/* { dg-final { scan-assembler-times {\.align\t16} 2 } } */
> +
> +#include <atomic>
> +
> +// 2
> +std::atomic<char> var_char;
> +std::atomic<short> var_short;
> +// 4
> +std::atomic<int> var_int;
> +// 8
> +std::atomic<long> var_long;
> +std::atomic<long long> var_long_long;
> +// 16
> +std::atomic<__int128> var_int128;
> +// 4
> +std::atomic<float> var_float;
> +// 8
> +std::atomic<double> var_double;
> +// 16
> +std::atomic<long double> var_long_double;
> diff --git a/gcc/testsuite/gcc.target/s390/atomic-align-1.c b/gcc/testsuite/gcc.target/s390/atomic-align-1.c
> new file mode 100644
> index 00000000000..b2e1233e3ee
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/s390/atomic-align-1.c
> @@ -0,0 +1,23 @@
> +/* { dg-do compile { target int128 } } */
> +/* { dg-options "-std=c11" } */
> +/* { dg-final { scan-assembler-times {\.align\t2} 2 } } */
> +/* { dg-final { scan-assembler-times {\.align\t4} 2 } } */
> +/* { dg-final { scan-assembler-times {\.align\t8} 3 } } */
> +/* { dg-final { scan-assembler-times {\.align\t16} 2 } } */
> +
> +// 2
> +_Atomic char var_char;
> +_Atomic short var_short;
> +// 4
> +_Atomic int var_int;
> +// 8
> +_Atomic long var_long;
> +_Atomic long long var_long_long;
> +// 16
> +_Atomic __int128 var_int128;
> +// 4
> +_Atomic float var_float;
> +// 8
> +_Atomic double var_double;
> +// 16
> +_Atomic long double var_long_double;
> diff --git a/gcc/testsuite/gcc.target/s390/atomic-align-2.c b/gcc/testsuite/gcc.target/s390/atomic-align-2.c
> new file mode 100644
> index 00000000000..0bf17341bf8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/s390/atomic-align-2.c
> @@ -0,0 +1,18 @@
> +/* { dg-do compile { target int128 } } */
> +/* { dg-options "-O -std=c11" } */
> +/* { dg-final { scan-assembler-not {abort} } } */
> +
> +/* The stack is 8 byte aligned which means GCC has to manually align a 16 byte
> +   aligned object.  This is done by allocating not 16 but rather 24 bytes for
> +   variable X and then manually aligning a pointer inside the memory block.
> +   Validate this by ensuring that the if-statement is optimized out.  */
> +
> +void bar (_Atomic unsigned __int128 *ptr);
> +
> +void foo (void) {
> +  _Atomic unsigned __int128 x;
> +  unsigned long n = (unsigned long)&x;
> +  if (n % 16 != 0)
> +    __builtin_abort ();
> +  bar (&x);
> +}
diff mbox series

Patch

diff --git a/gcc/config/s390/s390.cc b/gcc/config/s390/s390.cc
index 505de995da8..4813bf91dc4 100644
--- a/gcc/config/s390/s390.cc
+++ b/gcc/config/s390/s390.cc
@@ -450,6 +450,14 @@  s390_preserve_fpr_arg_p (int regno)
 	  && regno >= FPR0_REGNUM);
 }
 
+#undef TARGET_ATOMIC_ALIGN_FOR_MODE
+#define TARGET_ATOMIC_ALIGN_FOR_MODE s390_atomic_align_for_mode
+static unsigned int
+s390_atomic_align_for_mode (machine_mode mode)
+{
+  return GET_MODE_BITSIZE (mode);
+}
+
 /* A couple of shortcuts.  */
 #define CONST_OK_FOR_J(x) \
 	CONST_OK_FOR_CONSTRAINT_P((x), 'J', "J")
diff --git a/gcc/testsuite/g++.target/s390/atomic-align-1.C b/gcc/testsuite/g++.target/s390/atomic-align-1.C
new file mode 100644
index 00000000000..43aa0bc39ed
--- /dev/null
+++ b/gcc/testsuite/g++.target/s390/atomic-align-1.C
@@ -0,0 +1,25 @@ 
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-std=c++11" } */
+/* { dg-final { scan-assembler-times {\.align\t2} 2 } } */
+/* { dg-final { scan-assembler-times {\.align\t4} 2 } } */
+/* { dg-final { scan-assembler-times {\.align\t8} 3 } } */
+/* { dg-final { scan-assembler-times {\.align\t16} 2 } } */
+
+#include <atomic>
+
+// 2
+std::atomic<char> var_char;
+std::atomic<short> var_short;
+// 4
+std::atomic<int> var_int;
+// 8
+std::atomic<long> var_long;
+std::atomic<long long> var_long_long;
+// 16
+std::atomic<__int128> var_int128;
+// 4
+std::atomic<float> var_float;
+// 8
+std::atomic<double> var_double;
+// 16
+std::atomic<long double> var_long_double;
diff --git a/gcc/testsuite/gcc.target/s390/atomic-align-1.c b/gcc/testsuite/gcc.target/s390/atomic-align-1.c
new file mode 100644
index 00000000000..b2e1233e3ee
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/atomic-align-1.c
@@ -0,0 +1,23 @@ 
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-std=c11" } */
+/* { dg-final { scan-assembler-times {\.align\t2} 2 } } */
+/* { dg-final { scan-assembler-times {\.align\t4} 2 } } */
+/* { dg-final { scan-assembler-times {\.align\t8} 3 } } */
+/* { dg-final { scan-assembler-times {\.align\t16} 2 } } */
+
+// 2
+_Atomic char var_char;
+_Atomic short var_short;
+// 4
+_Atomic int var_int;
+// 8
+_Atomic long var_long;
+_Atomic long long var_long_long;
+// 16
+_Atomic __int128 var_int128;
+// 4
+_Atomic float var_float;
+// 8
+_Atomic double var_double;
+// 16
+_Atomic long double var_long_double;
diff --git a/gcc/testsuite/gcc.target/s390/atomic-align-2.c b/gcc/testsuite/gcc.target/s390/atomic-align-2.c
new file mode 100644
index 00000000000..0bf17341bf8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/s390/atomic-align-2.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-O -std=c11" } */
+/* { dg-final { scan-assembler-not {abort} } } */
+
+/* The stack is 8 byte aligned which means GCC has to manually align a 16 byte
+   aligned object.  This is done by allocating not 16 but rather 24 bytes for
+   variable X and then manually aligning a pointer inside the memory block.
+   Validate this by ensuring that the if-statement is optimized out.  */
+
+void bar (_Atomic unsigned __int128 *ptr);
+
+void foo (void) {
+  _Atomic unsigned __int128 x;
+  unsigned long n = (unsigned long)&x;
+  if (n % 16 != 0)
+    __builtin_abort ();
+  bar (&x);
+}