public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] GCN: Implement __atomic_compare_exchange_{1,2} in libgcc [PR102215]
@ 2022-03-09 16:29 Tobias Burnus
  2022-03-09 16:36 ` [Patch] GCN: Implement __atomic_compare_exchange_{1, 2} " Andrew Stubbs
  0 siblings, 1 reply; 2+ messages in thread
From: Tobias Burnus @ 2022-03-09 16:29 UTC (permalink / raw)
  To: gcc-patches, Andrew Stubbs

[-- 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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Patch] GCN: Implement __atomic_compare_exchange_{1, 2} in libgcc [PR102215]
  2022-03-09 16:29 [Patch] GCN: Implement __atomic_compare_exchange_{1,2} in libgcc [PR102215] Tobias Burnus
@ 2022-03-09 16:36 ` Andrew Stubbs
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Stubbs @ 2022-03-09 16:36 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches

On 09/03/2022 16:29, Tobias Burnus wrote:
> 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.

OK.

Andrew

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-09 16:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-09 16:29 [Patch] GCN: Implement __atomic_compare_exchange_{1,2} in libgcc [PR102215] Tobias Burnus
2022-03-09 16:36 ` [Patch] GCN: Implement __atomic_compare_exchange_{1, 2} " Andrew Stubbs

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).