From: Xi Ruoyao <xry111@xry111.site>
To: Jinyang He <hejinyang@loongson.cn>,
Chenghua Xu <xuchenghua@loongson.cn>,
Lulu Cheng <chenglulu@loongson.cn>
Cc: Weining Lu <luweining@loongson.cn>, Xing Li <lixing@loongson.cn>,
yala <zhaojunchao@loongson.cn>, Peng Fan <fanpeng@loongson.cn>,
gcc-patches@gcc.gnu.org, Huang Pei <huangpei@loongson.cn>
Subject: Re: [PATCH] LoongArch: Fix atomic_exchange make comparison and may jump out
Date: Thu, 17 Nov 2022 13:56:31 +0800 [thread overview]
Message-ID: <c4c7b8b9d8f0fcb657e0ebbd2129f38e0002247b.camel@xry111.site> (raw)
In-Reply-To: <24e66a2a-6e0e-db69-7ecc-fd98ca3bb963@loongson.cn>
[-- Attachment #1: Type: text/plain, Size: 597 bytes --]
On Thu, 2022-11-17 at 11:46 +0800, Jinyang He wrote:
> > So we do need an additional dbar for compare-and-exchange, but do
> > not
> > need it for a bare atomic exchange?
> Yes.
Ok, I just noticed we also don't use dbar in atomic_add etc.
I've adjusted the patch a little (in attachment): rewritten the
ChangeLog and test, and renamed "value_exchange_and" to just
"value_exchange" because we are not doing anything beyond the exchange.
If it looks OK you can just send it as v2.
--
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University
[-- Attachment #2: pr107713.patch --]
[-- Type: text/x-patch, Size: 4552 bytes --]
From 8b64115dfb289777300c4e84e278ab3d8ec4fe86 Mon Sep 17 00:00:00 2001
From: Jinyang He <hejinyang@loongson.cn>
Date: Tue, 15 Nov 2022 21:03:28 +0800
Subject: [PATCH v2] LoongArch: Fix atomic_exchange expanding [PR107713]
We used to expand atomic_exchange_n(ptr, new, mem_order) for subword types
into something like:
{
__typeof__(*ptr) t = atomic_load_n(ptr, mem_order);
atomic_compare_exchange_n(ptr, &t, new, true, mem_order, mem_order);
return t;
}
It's incorrect because another thread may store a different value into *ptr
after atomic_load_n. Then atomic_compare_exchange_n will not store into
*ptr, but atomic_exchange_n should always perform the store.
gcc/ChangeLog:
PR target/107713
* config/loongarch/sync.md
(atomic_cas_value_exchange_7_<mode>): New define_insn.
(atomic_exchange): Use atomic_cas_value_exchange_7_si instead of
atomic_cas_value_cmp_and_7_si.
gcc/testsuite/ChangeLog:
PR target/107713
* gcc.target/loongarch/pr107713-1.c: New test.
* gcc.target/loongarch/pr107713-2.c: New test.
---
gcc/config/loongarch/sync.md | 27 +++++++++-
.../gcc.target/loongarch/pr107713-1.c | 50 +++++++++++++++++++
.../gcc.target/loongarch/pr107713-2.c | 9 ++++
3 files changed, 84 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/loongarch/pr107713-1.c
create mode 100644 gcc/testsuite/gcc.target/loongarch/pr107713-2.c
diff --git a/gcc/config/loongarch/sync.md b/gcc/config/loongarch/sync.md
index 0c4f1983e88..45be1442439 100644
--- a/gcc/config/loongarch/sync.md
+++ b/gcc/config/loongarch/sync.md
@@ -448,6 +448,29 @@
}
[(set (attr "length") (const_int 32))])
+(define_insn "atomic_cas_value_exchange_7_<mode>"
+ [(set (match_operand:GPR 0 "register_operand" "=&r")
+ (match_operand:GPR 1 "memory_operand" "+ZC"))
+ (set (match_dup 1)
+ (unspec_volatile:GPR [(match_operand:GPR 2 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 3 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 4 "reg_or_0_operand" "rJ")
+ (match_operand:GPR 5 "reg_or_0_operand" "rJ")
+ (match_operand:SI 6 "const_int_operand")] ;; model
+ UNSPEC_SYNC_EXCHANGE))
+ (clobber (match_scratch:GPR 7 "=&r"))]
+ ""
+{
+ return "%G6\\n\\t"
+ "1:\\n\\t"
+ "ll.<amo>\\t%0,%1\\n\\t"
+ "and\\t%7,%0,%z3\\n\\t"
+ "or%i5\\t%7,%7,%5\\n\\t"
+ "sc.<amo>\\t%7,%1\\n\\t"
+ "beqz\\t%7,1b\\n\\t";
+}
+ [(set (attr "length") (const_int 20))])
+
(define_expand "atomic_exchange<mode>"
[(set (match_operand:SHORT 0 "register_operand")
(unspec_volatile:SHORT
@@ -459,9 +482,9 @@
""
{
union loongarch_gen_fn_ptrs generator;
- generator.fn_7 = gen_atomic_cas_value_cmp_and_7_si;
+ generator.fn_7 = gen_atomic_cas_value_exchange_7_si;
loongarch_expand_atomic_qihi (generator, operands[0], operands[1],
- operands[1], operands[2], operands[3]);
+ const0_rtx, operands[2], operands[3]);
DONE;
})
diff --git a/gcc/testsuite/gcc.target/loongarch/pr107713-1.c b/gcc/testsuite/gcc.target/loongarch/pr107713-1.c
new file mode 100644
index 00000000000..d1536c95b27
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/pr107713-1.c
@@ -0,0 +1,50 @@
+/* { dg-do run } */
+/* { dg-require-effective-target pthread } */
+/* { dg-options "-pthread" } */
+
+#include <pthread.h>
+
+char x, x1, x2;
+
+void *
+work1 (void *)
+{
+ for (int i = 0; i < 100; i++)
+ x1 = __atomic_exchange_n (&x, x1, __ATOMIC_SEQ_CST);
+ return NULL;
+}
+
+void *
+work2 (void *)
+{
+ for (int i = 0; i < 100; i++)
+ x2 = __atomic_exchange_n (&x, x2, __ATOMIC_SEQ_CST);
+ return NULL;
+}
+
+void
+test (void)
+{
+ x = 0;
+ x1 = 1;
+ x2 = 2;
+ pthread_t w1, w2;
+ if (pthread_create (&w1, NULL, work1, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_create (&w2, NULL, work2, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_join (w1, NULL) != 0)
+ __builtin_abort ();
+ if (pthread_join (w2, NULL) != 0)
+ __builtin_abort ();
+ if ((x ^ x1 ^ x2) != 3)
+ __builtin_abort ();
+}
+
+int
+main ()
+{
+ int i;
+ for (i = 0; i < 10000; i++)
+ test ();
+}
diff --git a/gcc/testsuite/gcc.target/loongarch/pr107713-2.c b/gcc/testsuite/gcc.target/loongarch/pr107713-2.c
new file mode 100644
index 00000000000..82d44db3d51
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/pr107713-2.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-final { scan-assembler-times "beq|bne" 1 } } */
+
+char
+t (char *p, char x)
+{
+ return __atomic_exchange_n (p, x, __ATOMIC_RELAXED);
+}
--
2.37.3
prev parent reply other threads:[~2022-11-17 5:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-15 13:03 Jinyang He
2022-11-15 14:21 ` Xi Ruoyao
2022-11-16 2:11 ` Jinyang He
2022-11-16 11:46 ` Xi Ruoyao
2022-11-17 1:39 ` Jinyang He
2022-11-17 2:55 ` Jinyang He
2022-11-17 3:38 ` Xi Ruoyao
2022-11-17 3:46 ` Jinyang He
2022-11-17 5:56 ` Xi Ruoyao [this message]
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=c4c7b8b9d8f0fcb657e0ebbd2129f38e0002247b.camel@xry111.site \
--to=xry111@xry111.site \
--cc=chenglulu@loongson.cn \
--cc=fanpeng@loongson.cn \
--cc=gcc-patches@gcc.gnu.org \
--cc=hejinyang@loongson.cn \
--cc=huangpei@loongson.cn \
--cc=lixing@loongson.cn \
--cc=luweining@loongson.cn \
--cc=xuchenghua@loongson.cn \
--cc=zhaojunchao@loongson.cn \
/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).