[Openmp-commits] [openmp] 40d3a0b - [openmp] Fix strict aliasing issue in cmpxchg routine

via Openmp-commits openmp-commits at lists.llvm.org
Thu May 12 07:17:02 PDT 2022


Author: serge-sans-paille
Date: 2022-05-12T16:14:48+02:00
New Revision: 40d3a0ba4d9e5452c0a68cfdaa8e88eb8ed5c63d

URL: https://github.com/llvm/llvm-project/commit/40d3a0ba4d9e5452c0a68cfdaa8e88eb8ed5c63d
DIFF: https://github.com/llvm/llvm-project/commit/40d3a0ba4d9e5452c0a68cfdaa8e88eb8ed5c63d.diff

LOG: [openmp] Fix strict aliasing issue in cmpxchg routine

Avoid warning under -fstrict-aliasing by using a call to memcpy to perform type
punning.

Differential Revision: https://reviews.llvm.org/D125467

Added: 
    

Modified: 
    openmp/runtime/src/kmp_os.h

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp_os.h b/openmp/runtime/src/kmp_os.h
index 0bc40c74fb1e3..580663560104e 100644
--- a/openmp/runtime/src/kmp_os.h
+++ b/openmp/runtime/src/kmp_os.h
@@ -17,6 +17,7 @@
 #include <atomic>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <string.h>
 
 #define KMP_FTN_PLAIN 1
 #define KMP_FTN_APPEND 2
@@ -864,15 +865,25 @@ static inline bool mips_sync_val_compare_and_swap(volatile kmp_uint64 *p,
   __sync_lock_test_and_set((volatile kmp_uint64 *)(p), (kmp_uint64)(v))
 
 inline kmp_real32 KMP_XCHG_REAL32(volatile kmp_real32 *p, kmp_real32 v) {
-  kmp_int32 tmp =
-      __sync_lock_test_and_set((volatile kmp_uint32 *)(p), *(kmp_uint32 *)&v);
-  return *(kmp_real32 *)&tmp;
+  volatile kmp_uint32 *up;
+  kmp_uint32 uv;
+  memcpy(&up, &p, sizeof(up));
+  memcpy(&uv, &v, sizeof(uv));
+  kmp_int32 tmp = __sync_lock_test_and_set(up, uv);
+  kmp_real32 ftmp;
+  memcpy(&ftmp, &tmp, sizeof(tmp));
+  return ftmp;
 }
 
 inline kmp_real64 KMP_XCHG_REAL64(volatile kmp_real64 *p, kmp_real64 v) {
-  kmp_int64 tmp =
-      __sync_lock_test_and_set((volatile kmp_uint64 *)(p), *(kmp_uint64 *)&v);
-  return *(kmp_real64 *)&tmp;
+  volatile kmp_uint64 *up;
+  kmp_uint64 uv;
+  memcpy(&up, &p, sizeof(up));
+  memcpy(&uv, &v, sizeof(uv));
+  kmp_int64 tmp = __sync_lock_test_and_set(up, uv);
+  kmp_real64 dtmp;
+  memcpy(&dtmp, &tmp, sizeof(tmp));
+  return dtmp;
 }
 
 #else


        


More information about the Openmp-commits mailing list