Skip to content

Commit 7b6e983

Browse files
threads: fix TSAN detection with GCC
Also restore barrier exit comments to clarify.
1 parent 32f945d commit 7b6e983

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

ggml/src/ggml.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ int ggml_sve_cnt_b = 0;
7676
#if __has_feature(thread_sanitizer)
7777
#define GGML_TSAN_ENABLED 1
7878
#endif
79+
#else // __has_feature
80+
#if defined(__SANITIZE_THREAD__)
81+
#define GGML_TSAN_ENABLED 1
7982
#endif
83+
#endif // __has_feature
8084

8185
#if defined(_WIN32)
8286

@@ -3216,20 +3220,24 @@ static void ggml_barrier(struct ggml_threadpool * tp) {
32163220
if (n_barrier == (n_threads - 1)) {
32173221
// last thread
32183222
atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
3223+
3224+
// exit barrier (fill seq-cst fence)
32193225
atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
3220-
} else {
3221-
// wait for other threads
3222-
while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
3223-
ggml_thread_cpu_relax();
3224-
}
3226+
return;
3227+
}
32253228

3226-
#ifdef GGML_TSAN_ENABLED
3227-
// TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
3228-
atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
3229-
#else
3230-
atomic_thread_fence(memory_order_seq_cst);
3231-
#endif
3229+
// wait for other threads
3230+
while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
3231+
ggml_thread_cpu_relax();
32323232
}
3233+
3234+
// exit barrier (full seq-cst fence)
3235+
// TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
3236+
#ifdef GGML_TSAN_ENABLED
3237+
atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
3238+
#else
3239+
atomic_thread_fence(memory_order_seq_cst);
3240+
#endif
32333241
#endif
32343242
}
32353243

@@ -20260,8 +20268,8 @@ static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * s
2026020268

2026120269
// sync thread state after polling
2026220270
static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
20263-
#ifdef GGML_TSAN_ENABLED
2026420271
// TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
20272+
#ifdef GGML_TSAN_ENABLED
2026520273
atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
2026620274
#else
2026720275
atomic_thread_fence(memory_order_seq_cst);

0 commit comments

Comments
 (0)