public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Uros Bizjak <ubizjak@gmail.com>
Cc: gcc-patches@gcc.gnu.org, Florian Weimer <fweimer@redhat.com>,
	"H.J. Lu" <hjl.tools@gmail.com>
Subject: [PATCH] i386: Emit 16b atomics inline with -m64 -mcx16 -mavx [PR104688]
Date: Sun, 13 Nov 2022 19:52:49 -1200	[thread overview]
Message-ID: <Y3Hz0QswGCGclWGu@tucnak> (raw)

Hi!

Working virtually out of Baker Island.

Given
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104688#c10
the following patch implements atomic load/store (and therefore also
enabling compare and exchange) for -m64 -mcx16 -mavx.

Ok for trunk if it passes bootstrap/regtest?

2022-11-13  Jakub Jelinek  <jakub@redhat.com>

	PR target/104688
	* config/i386/sync.md (atomic_loadti, atomic_storeti): New
	define_expand patterns.
	(atomic_loadti_1, atomic_storeti_1): New define_insn patterns.

	* gcc.target/i386/pr104688-1.c: New test.
	* gcc.target/i386/pr104688-2.c: New test.
	* gcc.target/i386/pr104688-3.c: New test.

--- gcc/config/i386/sync.md.jj	2022-11-07 20:54:37.259400942 -1200
+++ gcc/config/i386/sync.md	2022-11-13 19:27:22.977987355 -1200
@@ -225,6 +225,31 @@ (define_insn_and_split "atomic_loaddi_fp
   DONE;
 })
 
+;; Intel SDM guarantees that 16-byte VMOVDQA on 16-byte aligned address
+;; is atomic.  AMD will give a similar guarantee.
+(define_expand "atomic_loadti"
+  [(set (match_operand:TI 0 "register_operand" "=x,Yv")
+	(unspec:TI [(match_operand:TI 1 "memory_operand" "m,m")
+		    (match_operand:SI 2 "const_int_operand")]
+		   UNSPEC_LDA))]
+  "TARGET_64BIT && TARGET_CMPXCHG16B && TARGET_AVX"
+{
+  emit_insn (gen_atomic_loadti_1 (operands[0], operands[1]));
+  DONE;
+})
+
+(define_insn "atomic_loadti_1"
+  [(set (match_operand:TI 0 "register_operand" "=x,Yv")
+	(unspec:TI [(match_operand:TI 1 "memory_operand" "m,m")]
+		   UNSPEC_LDA))]
+  "TARGET_64BIT && TARGET_CMPXCHG16B && TARGET_AVX"
+  "@
+   vmovdqa\t{%1, %0|%0, %1}
+   vmovdqa64\t{%1, %0|%0, %1}"
+  [(set_attr "type" "ssemov")
+   (set_attr "prefix" "vex,evex")
+   (set_attr "mode" "TI")])
+
 (define_expand "atomic_store<mode>"
   [(set (match_operand:ATOMIC 0 "memory_operand")
 	(unspec:ATOMIC [(match_operand:ATOMIC 1 "nonimmediate_operand")
@@ -276,6 +301,36 @@ (define_insn "atomic_store<mode>_1"
   ""
   "%K2mov{<imodesuffix>}\t{%1, %0|%0, %1}")
 
+(define_expand "atomic_storeti"
+  [(set (match_operand:TI 0 "memory_operand")
+	(unspec:TI [(match_operand:TI 1 "register_operand")
+		    (match_operand:SI 2 "const_int_operand")]
+		   UNSPEC_STA))]
+  "TARGET_64BIT && TARGET_CMPXCHG16B && TARGET_AVX"
+{
+  enum memmodel model = memmodel_from_int (INTVAL (operands[2]));
+
+  emit_insn (gen_atomic_storeti_1 (operands[0], operands[1], operands[2]));
+
+  /* ... followed by an MFENCE, if required.  */
+  if (is_mm_seq_cst (model))
+    emit_insn (gen_mem_thread_fence (operands[2]));
+  DONE;
+})
+
+(define_insn "atomic_storeti_1"
+  [(set (match_operand:TI 0 "memory_operand" "=m,m")
+	(unspec:TI [(match_operand:TI 1 "register_operand" "x,Yv")
+		     (match_operand:SI 2 "const_int_operand")]
+		    UNSPEC_STA))]
+  ""
+  "@
+   %K2vmovdqa\t{%1, %0|%0, %1}
+   %K2vmovdqa64\t{%1, %0|%0, %1}"
+  [(set_attr "type" "ssemov")
+   (set_attr "prefix" "vex,evex")
+   (set_attr "mode" "TI")])
+
 (define_insn_and_split "atomic_storedi_fpu"
   [(set (match_operand:DI 0 "memory_operand" "=m,m,m")
 	(unspec:DI [(match_operand:DI 1 "nonimmediate_operand" "x,m,?r")]
--- gcc/testsuite/gcc.target/i386/pr104688-1.c.jj	2022-11-13 19:36:43.251332612 -1200
+++ gcc/testsuite/gcc.target/i386/pr104688-1.c	2022-11-13 19:40:22.649334650 -1200
@@ -0,0 +1,34 @@
+/* PR target/104688 */
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-O2 -mno-cx16" } */
+/* { dg-final { scan-assembler "\t__sync_val_compare_and_swap_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_load_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_store_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_compare_exchange_16" } } */
+
+__int128 v;
+
+__int128
+f1 (void)
+{
+  return __sync_val_compare_and_swap (&v, 42, 0);
+}
+
+__int128
+f2 (void)
+{
+  return __atomic_load_n (&v, __ATOMIC_SEQ_CST);
+}
+
+void
+f3 (__int128 x)
+{
+  __atomic_store_n (&v, 42, __ATOMIC_SEQ_CST);
+}
+
+__int128
+f4 (void)
+{
+  __int128 y = 42;
+  __atomic_compare_exchange_n (&v, &y, 0, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}
--- gcc/testsuite/gcc.target/i386/pr104688-2.c.jj	2022-11-13 19:36:46.513288025 -1200
+++ gcc/testsuite/gcc.target/i386/pr104688-2.c	2022-11-13 19:40:34.676170305 -1200
@@ -0,0 +1,34 @@
+/* PR target/104688 */
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-O2 -mno-avx" } */
+/* { dg-final { scan-assembler "\t__sync_val_compare_and_swap_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_load_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_store_16" } } */
+/* { dg-final { scan-assembler "\t__atomic_compare_exchange_16" } } */
+
+__int128 v;
+
+__int128
+f1 (void)
+{
+  return __sync_val_compare_and_swap (&v, 42, 0);
+}
+
+__int128
+f2 (void)
+{
+  return __atomic_load_n (&v, __ATOMIC_SEQ_CST);
+}
+
+void
+f3 (__int128 x)
+{
+  __atomic_store_n (&v, 42, __ATOMIC_SEQ_CST);
+}
+
+__int128
+f4 (void)
+{
+  __int128 y = 42;
+  __atomic_compare_exchange_n (&v, &y, 0, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}
--- gcc/testsuite/gcc.target/i386/pr104688-3.c.jj	2022-11-13 19:37:00.899091450 -1200
+++ gcc/testsuite/gcc.target/i386/pr104688-3.c	2022-11-13 19:40:41.984070460 -1200
@@ -0,0 +1,34 @@
+/* PR target/104688 */
+/* { dg-do compile { target int128 } } */
+/* { dg-options "-O2 -mcx16 -mavx" } */
+/* { dg-final { scan-assembler-not "\t__sync_val_compare_and_swap_16" } } */
+/* { dg-final { scan-assembler-not "\t__atomic_load_16" } } */
+/* { dg-final { scan-assembler-not "\t__atomic_store_16" } } */
+/* { dg-final { scan-assembler-not "\t__atomic_compare_exchange_16" } } */
+
+__int128 v;
+
+__int128
+f1 (void)
+{
+  return __sync_val_compare_and_swap (&v, 42, 0);
+}
+
+__int128
+f2 (void)
+{
+  return __atomic_load_n (&v, __ATOMIC_SEQ_CST);
+}
+
+void
+f3 (__int128 x)
+{
+  __atomic_store_n (&v, 42, __ATOMIC_SEQ_CST);
+}
+
+__int128
+f4 (void)
+{
+  __int128 y = 42;
+  __atomic_compare_exchange_n (&v, &y, 0, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}


	Jakub


             reply	other threads:[~2022-11-14  7:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-14  7:52 Jakub Jelinek [this message]
2022-11-14  7:57 ` Uros Bizjak
2022-11-14  9:04   ` Hongtao Liu
2022-11-14  9:08     ` Hongtao Liu
2022-11-14  9:13     ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y3Hz0QswGCGclWGu@tucnak \
    --to=jakub@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hjl.tools@gmail.com \
    --cc=ubizjak@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).