[m-rev.] for review: Use clang compiler intrinsics for more atomic ops.

Peter Wang novalazy at gmail.com
Thu Aug 29 12:47:03 AEST 2024


Use clang compiler intrinsics for more atomic ops.

runtime/mercury_atomic_ops.h:
    Use compiler intrinsic for MR_ATOMIC_ADD_AND_FETCH_WORD_BODY
    when compiling with clang, instead of falling back to the
    compare and swap implementation.

    Use compiler intrinsic for MR_ATOMIC_SUB_INT_BODY
    when compiling with clang but not targeting x86/x86-64.
    In those cases, MR_atomic_sub_int() was not being defined,
    and causing a link error.

    Use compiler intrinsic for MR_ATOMIC_DEC_AND_IS_ZERO_WORD_BODY
    when compiling with clang but not targeting x86/x86-64.

[Note that I don't really have a way of testing that the atomic ops work,
as I would only be testing under emulation with QEMU.
And, asm_fast.par.gc doesn't work with clang anyway.]

diff --git a/runtime/mercury_atomic_ops.h b/runtime/mercury_atomic_ops.h
index 1ce95f3d8..a7128d674 100644
--- a/runtime/mercury_atomic_ops.h
+++ b/runtime/mercury_atomic_ops.h
@@ -1,7 +1,7 @@
 // vim: ts=4 sw=4 expandtab ft=c
 
 // Copyright (C) 2007, 2009-2011 The University of Melbourne.
-// Copyright (C) 2016, 2018 The Mercury team.
+// Copyright (C) 2016, 2018, 2021, 2024 The Mercury team.
 // This file is distributed under the terms specified in COPYING.LIB.
 
 // mercury_atomic.h - defines atomic operations and other primitives used by
@@ -189,7 +189,7 @@ MR_EXTERN_INLINE MR_bool    MR_atomic_dec_and_is_zero_uint(
 
 ////////////////////////////////////////////////////////////////////////////
 
-#if (MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)) &&           \
+#if (defined(MR_CLANG) || MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)) && \
     !defined(MR_AVOID_COMPILER_INTRINSICS)
 
     #define MR_ATOMIC_ADD_AND_FETCH_WORD_BODY                               \
@@ -334,7 +334,7 @@ MR_EXTERN_INLINE MR_bool    MR_atomic_dec_and_is_zero_uint(
                 );                                                          \
         } while (0)
 
-#elif MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)
+#elif defined(MR_CLANG) || MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)
 
     #define MR_ATOMIC_SUB_INT_BODY                                          \
         do {                                                                \
@@ -503,7 +503,7 @@ MR_EXTERN_INLINE MR_bool    MR_atomic_dec_and_is_zero_uint(
     #define MR_ATOMIC_DEC_AND_IS_ZERO_UINT_BODY                         \
         MR_ATOMIC_DEC_AND_IS_ZERO_WORD_BODY
 
-#elif MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)
+#elif defined(MR_CLANG) || MR_GNUC > 4 || (MR_GNUC == 4 && __GNUC_MINOR__ >= 1)
 
     #define MR_ATOMIC_DEC_AND_IS_ZERO_WORD_BODY                             \
         do {                                                                \
-- 
2.44.0



More information about the reviews mailing list