[Openmp-commits] [PATCH] D125467: [openmp] Fix strict aliasing issue in cmpxchg routine

serge via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Thu May 12 06:39:23 PDT 2022

serge-sans-paille created this revision.
serge-sans-paille added reviewers: jlpeyton, jcownie.
Herald added subscribers: jeroen.dobbelaere, guansong, yaxunl.
Herald added a project: All.
serge-sans-paille requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: openmp-commits, sstefan1.
Herald added a project: OpenMP.

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

Compiler output seems unaffected, see https://godbolt.org/z/E4hhvox1x

  rG LLVM Github Monorepo



Index: openmp/runtime/src/kmp_os.h
--- openmp/runtime/src/kmp_os.h
+++ openmp/runtime/src/kmp_os.h
@@ -864,15 +864,25 @@
   __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;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125467.428927.patch
Type: text/x-patch
Size: 1163 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20220512/3bcaf692/attachment.bin>

More information about the Openmp-commits mailing list