]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
s390: Implement TARGET_ATOMIC_ALIGN_FOR_MODE
authorStefan Schulze Frielinghaus <stefansf@linux.ibm.com>
Wed, 17 May 2023 08:21:39 +0000 (10:21 +0200)
committerStefan Schulze Frielinghaus <stefansf@linux.ibm.com>
Wed, 17 May 2023 08:21:39 +0000 (10:21 +0200)
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.

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
gcc/testsuite/g++.target/s390/atomic-align-1.C [new file with mode: 0644]
gcc/testsuite/gcc.target/s390/atomic-align-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/s390/atomic-align-2.c [new file with mode: 0644]

index b1cb54612b8136d87aabc31ce008006bd869dbe6..54ee5fc86b0ffc5a5267c46bd4a0228559b2d38b 100644 (file)
@@ -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 (file)
index 0000000..43aa0bc
--- /dev/null
@@ -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 (file)
index 0000000..b2e1233
--- /dev/null
@@ -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 (file)
index 0000000..0bf1734
--- /dev/null
@@ -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);
+}