public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>,
	Andrew Stubbs <andrew_stubbs@mentor.com>
Subject: [Patch] GCN: Implement __atomic_compare_exchange_{1,2} in libgcc [PR102215]
Date: Wed, 9 Mar 2022 17:29:44 +0100	[thread overview]
Message-ID: <f36f7738-dba4-0934-56b8-63775dce63ad@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1121 bytes --]

This shows up with with OpenMP offloading as libgomp since a couple
of months uses __atomic_compare_exchange (see PR for details),
causing link errors when the gcn libgomp.a is linked.
It also shows up with sollve_vv.

The implementation does a bit copy'n'paste from the current
implementation + calls the existing word/uint32_t-wide version
of the atomic intrinsic. The semantic is described at
https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

In terms of the args, the _4 has:
DEF_SYNC_BUILTIN (BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4,
                   "__atomic_compare_exchange_4",
                   BT_FN_BOOL_VPTR_PTR_I4_BOOL_INT_INT,
                   ATTR_NOTHROWCALL_LEAF_LIST)
and the arg names try to match the GCC manual.


OK for mainline?
Tested with libgomp + sollve_vv and -march=gfx908.

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

[-- Attachment #2: gcn-atomic.diff --]
[-- Type: text/x-patch, Size: 3505 bytes --]

GCN: Implement __atomic_compare_exchange_{1,2} in libgcc [PR102215]

libgcc/ChangeLog:

	PR target/102215
	* config/gcn/atomic.c (__sync_val_compare_and_swap_##SIZE): Move
	a line up to non-arg-dependent value first.
	(__ATOMIC_COMPARE_EXCHANGE): Define + call to generate
	__atomic_compare_exchange_{1,2}.

diff --git a/libgcc/config/gcn/atomic.c b/libgcc/config/gcn/atomic.c
index 8784f90dfe2..cf29fa82aba 100644
--- a/libgcc/config/gcn/atomic.c
+++ b/libgcc/config/gcn/atomic.c
@@ -18,42 +18,69 @@
 
    You should have received a copy of the GNU General Public License and
    a copy of the GCC Runtime Library Exception along with this program;
    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    <http://www.gnu.org/licenses/>.  */
 
 #include <stdbool.h>
 
 #define __SYNC_SUBWORD_COMPARE_AND_SWAP(TYPE, SIZE)			     \
 									     \
 TYPE									     \
 __sync_val_compare_and_swap_##SIZE (TYPE *ptr, TYPE oldval, TYPE newval)     \
 {									     \
+  unsigned int valmask = (1 << (SIZE * 8)) - 1;				     \
   unsigned int *wordptr = (unsigned int *)((__UINTPTR_TYPE__ ) ptr & ~3UL);  \
   int shift = ((__UINTPTR_TYPE__ ) ptr & 3UL) * 8;			     \
-  unsigned int valmask = (1 << (SIZE * 8)) - 1;				     \
   unsigned int wordmask = ~(valmask << shift);				     \
   unsigned int oldword = *wordptr;					     \
   for (;;)								     \
     {									     \
       TYPE prevval = (oldword >> shift) & valmask;			     \
       if (__builtin_expect (prevval != oldval, 0))			     \
 	return prevval;							     \
       unsigned int newword = oldword & wordmask;			     \
       newword |= ((unsigned int) newval) << shift;			     \
       unsigned int prevword						     \
 	  = __sync_val_compare_and_swap_4 (wordptr, oldword, newword);	     \
       if (__builtin_expect (prevword == oldword, 1))			     \
 	return oldval;							     \
       oldword = prevword;						     \
     }									     \
 }									     \
 									     \
 bool									     \
 __sync_bool_compare_and_swap_##SIZE (TYPE *ptr, TYPE oldval, TYPE newval)    \
 {									     \
   return __sync_val_compare_and_swap_##SIZE (ptr, oldval, newval) == oldval; \
 }
 
 __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned char, 1)
 __SYNC_SUBWORD_COMPARE_AND_SWAP (unsigned short, 2)
 
+
+#define __ATOMIC_COMPARE_EXCHANGE(TYPE,SIZE)				      \
+bool									      \
+__atomic_compare_exchange_##SIZE (TYPE *ptr, TYPE *expected,		      \
+				  TYPE desired, bool weak,		      \
+				  int success_memorder, int failure_memorder) \
+{									      \
+  unsigned int valmask = (1 << (SIZE * 8)) - 1;				      \
+									      \
+  unsigned int *wordptr = (unsigned int *)((__UINTPTR_TYPE__ ) ptr & ~3UL);   \
+  int ptrshift = ((__UINTPTR_TYPE__ ) ptr & 3UL) * 8;			      \
+  unsigned int wordmask = ~(valmask << ptrshift);			      \
+									      \
+  unsigned int ptrword = *wordptr;					      \
+  unsigned int exptword = ptrword & wordmask;				      \
+  unsigned int newword = ptrword & wordmask;				      \
+  exptword |= ((unsigned int) *expected) << ptrshift;			      \
+  newword |= ((unsigned int) desired) << ptrshift;			      \
+  if (__atomic_compare_exchange_4 (wordptr, &exptword, newword, weak,	      \
+				   success_memorder, failure_memorder))	      \
+    return true;							      \
+  *expected = (TYPE) ((exptword >> ptrshift) & valmask);		      \
+  return false;								      \
+}
+
+__ATOMIC_COMPARE_EXCHANGE (unsigned char, 1)
+__ATOMIC_COMPARE_EXCHANGE (unsigned short, 2)

             reply	other threads:[~2022-03-09 16:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-09 16:29 Tobias Burnus [this message]
2022-03-09 16:36 ` [Patch] GCN: Implement __atomic_compare_exchange_{1, 2} " Andrew Stubbs

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=f36f7738-dba4-0934-56b8-63775dce63ad@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=andrew_stubbs@mentor.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).