From e91dad93ff50b429d5717c81fbd5fe20ff2defd9 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Mon, 11 Mar 2024 16:17:14 +0000
Subject: [PATCH] rts: fix errors when compiling with TSAN

This commit fixes rts compilation errors when compiling with TSAN:

- xxx_FENCE macros are redefined and trigger CPP warnings.
- Use SIZEOF_W. WORD_SIZE_IN_BITS is provided by MachDeps.h which
  Cmm.h doesn't include by default.
---
 rts/include/Cmm.h     | 4 ++--
 rts/include/stg/SMP.h | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/rts/include/Cmm.h b/rts/include/Cmm.h
index 0265a4b2042a..519f7275fd91 100644
--- a/rts/include/Cmm.h
+++ b/rts/include/Cmm.h
@@ -700,9 +700,9 @@
 
 #if defined(TSAN_ENABLED)
 // This is may be efficient than a fence but TSAN can reason about it.
-#if WORD_SIZE_IN_BITS == 64
+#if SIZEOF_W == 8
 #define ACQUIRE_FENCE_ON(x) if (1) { W_ tmp; (tmp) = prim %load_acquire64(x); }
-#elif WORD_SIZE_IN_BITS == 32
+#elif SIZEOF_W == 4
 #define ACQUIRE_FENCE_ON(x) if (1) { W_ tmp; (tmp) = prim %load_acquire32(x); }
 #endif
 #else
diff --git a/rts/include/stg/SMP.h b/rts/include/stg/SMP.h
index c9111fde7cda..22edad0e167e 100644
--- a/rts/include/stg/SMP.h
+++ b/rts/include/stg/SMP.h
@@ -549,6 +549,9 @@ busy_wait_nop(void)
 #define SEQ_CST_FENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST)
 
 #if defined(TSAN_ENABLED)
+#undef ACQUIRE_FENCE
+#undef RELEASE_FENCE
+#undef SEQ_CST_FENCE
 #define ACQUIRE_FENCE() NO_WARN(-Wtsan, __atomic_thread_fence(__ATOMIC_ACQUIRE);)
 #define RELEASE_FENCE() NO_WARN(-Wtsan, __atomic_thread_fence(__ATOMIC_RELEASE);)
 #define SEQ_CST_FENCE() NO_WARN(-Wtsan, __atomic_thread_fence(__ATOMIC_SEQ_CST);)
-- 
GitLab