[llvm-branch-commits] [clang] [llvm] [openmp] [OpenMP][AMDGPU] Implement the device lock routines (PR #242479)

Julian Brown via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 13 12:48:52 PST 2026


https://github.com/jtb20 created https://github.com/llvm/llvm-project/pull/242479

This patch adds implementations of omp_init_lock, omp_set_lock, omp_unset_lock and omp_destroy_lock for AMDGPU.

A spin lock has a problem on targets that group execution threads into wavefronts or warps: the lanes of a wavefront share one instruction stream.  When several active lanes spin for the same lock, the one that takes it is predicated off until all the others have left the loop, so it never reaches the omp_unset_lock that would let those others proceed. OpenMP 6.0 permits this (threads in one progress unit that synchronize while divergent may deadlock), but a hung kernel is hard to diagnose, so omp_set_lock checks whether another active lane of its wavefront wants the same lock, says so, and traps.  The check cannot see lanes of one wavefront that reach omp_set_lock for the same lock on divergent paths, one after the other; those still hang.

The message goes to stderr through the GPU libc when it is available, by way of a new ompx::reportError: this allows errors to be emitted more reliably when output is not a terminal.

The omp_test_lock test is now un-XFAILed on AMDGPU.

Assisted-By: Claude

>From 258af8d2107a830f08d0b926b076d85fe617869b Mon Sep 17 00:00:00 2001
From: Julian Brown <julian.brown at amd.com>
Date: Thu, 12 Nov 2026 17:02:52 -0600
Subject: [PATCH] [OpenMP][AMDGPU] Implement the device lock routines

This patch adds implementations of omp_init_lock, omp_set_lock,
omp_unset_lock and omp_destroy_lock for AMDGPU.

A spin lock has a problem on targets that group execution threads into
wavefronts or warps: the lanes of a wavefront share one instruction
stream.  When several active lanes spin for the same lock, the one that
takes it is predicated off until all the others have left the loop, so
it never reaches the omp_unset_lock that would let those others proceed.
OpenMP 6.0 permits this (threads in one progress unit that synchronize
while divergent may deadlock), but a hung kernel is hard to diagnose, so
omp_set_lock checks whether another active lane of its wavefront wants
the same lock, says so, and traps.  The check cannot see lanes of one
wavefront that reach omp_set_lock for the same lock on divergent paths,
one after the other; those still hang.

The message goes to stderr through the GPU libc when it is available,
by way of a new ompx::reportError: this allows errors to be emitted more
reliably when output is not a terminal.

The omp_test_lock test is now un-XFAILed on AMDGPU.

Assisted-By: Claude
---
 clang/docs/ReleaseNotes.md                  |  7 +++
 offload/test/api/omp_lock.c                 | 69 +++++++++++++++++++++
 offload/test/api/omp_lock_wave_contention.c | 40 ++++++++++++
 offload/test/api/omp_test_lock.c            |  3 -
 openmp/device/include/LibC.h                |  4 ++
 openmp/device/src/LibC.cpp                  | 18 ++++++
 openmp/device/src/Synchronization.cpp       | 40 ++++++++++--
 7 files changed, 172 insertions(+), 9 deletions(-)
 create mode 100644 offload/test/api/omp_lock.c
 create mode 100644 offload/test/api/omp_lock_wave_contention.c

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index fc9026c575d05..219e9925b3f00 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1683,6 +1683,13 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
   a time with a loop over the warp's active lanes, instead of a loop over every
   thread of the team.
 
+- The OpenMP simple lock routines `omp_init_lock`, `omp_set_lock`,
+  `omp_unset_lock` and `omp_destroy_lock` are now implemented in AMDGPU
+  device code, where they previously trapped. The lanes of a wavefront share
+  one instruction stream, so a lane holding a lock cannot release it while
+  another lane of the same wavefront spins for it; `omp_set_lock` traps in
+  that case rather than hang.
+
 ### SYCL Support
 
 - On a `spirv32-unknown-unknown` or `spirv64-unknown-unknown` offload target,
diff --git a/offload/test/api/omp_lock.c b/offload/test/api/omp_lock.c
new file mode 100644
index 0000000000000..f33dbda2402d0
--- /dev/null
+++ b/offload/test/api/omp_lock.c
@@ -0,0 +1,69 @@
+// RUN: %libomptarget-compile-run-and-check-generic
+
+// REQUIRES: amdgpu
+
+// Device-side omp_set_lock and omp_unset_lock. omp_test_lock is covered by
+// omp_test_lock.c.
+
+#include <omp.h>
+#include <stdio.h>
+
+#pragma omp declare target
+omp_lock_t SeqLock;
+omp_lock_t CountLock;
+#pragma omp end declare target
+
+#define THREADS 256
+#define ITERS 200
+
+int main() {
+  int Freed = 0;
+  int Count = 0;
+  int Contenders = 0;
+
+  // Locks may only be shared within a contention group, so each lock is
+  // created, used and destroyed by the threads of a single team.
+#pragma omp target parallel num_threads(THREADS)                               \
+    map(tofrom : Freed, Count, Contenders)
+  {
+#pragma omp single
+    {
+      omp_init_lock(&SeqLock);
+      omp_init_lock(&CountLock);
+
+      // A lock that is set and then unset is free again.
+      omp_set_lock(&SeqLock);
+      omp_unset_lock(&SeqLock);
+      Freed = omp_test_lock(&SeqLock);
+      omp_unset_lock(&SeqLock);
+    }
+
+    // Mutual exclusion between threads in different warps. Only one thread of
+    // each warp takes part, so no two contenders share a wavefront.
+    if (omp_get_thread_num() % 64 == 0) {
+#pragma omp atomic
+      Contenders += 1;
+      for (int I = 0; I < ITERS; ++I) {
+        omp_set_lock(&CountLock);
+        volatile int *P = &Count;
+        int V = *P;
+        *P = V + 1;
+        omp_unset_lock(&CountLock);
+      }
+    }
+
+#pragma omp barrier
+#pragma omp single
+    {
+      omp_destroy_lock(&SeqLock);
+      omp_destroy_lock(&CountLock);
+    }
+  }
+
+  // CHECK: Freed = 1
+  printf("Freed = %d\n", Freed != 0);
+  // CHECK: Count OK
+  printf("Count %s\n",
+         Count == Contenders * ITERS && Contenders > 1 ? "OK" : "BAD");
+  return 0;
+}
diff --git a/offload/test/api/omp_lock_wave_contention.c b/offload/test/api/omp_lock_wave_contention.c
new file mode 100644
index 0000000000000..7383558fba66c
--- /dev/null
+++ b/offload/test/api/omp_lock_wave_contention.c
@@ -0,0 +1,40 @@
+// RUN: %libomptarget-compile-generic
+// RUN: %not --crash %libomptarget-run-generic 2>&1 | %fcheck-generic
+// RUN: %if libc %{ %not --crash %libomptarget-run-generic 2>&1 | %fcheck-generic --check-prefix=LIBC %}
+
+// REQUIRES: amdgpu
+
+// Lanes of one wavefront execute a single instruction stream, so if two of
+// them contend for the same lock the one that takes it can never run on to
+// release it. The runtime traps rather than hanging, after saying why on stderr
+// if the GPU libc is available. The lock calls are kept in
+// separate functions so that the compiler cannot see the region they enclose
+// and serialize the lanes around it.
+
+#include <omp.h>
+#include <stdio.h>
+
+#pragma omp declare target
+omp_lock_t Lock;
+int X;
+[[gnu::noinline]] void acquire(void) { omp_set_lock(&Lock); }
+[[gnu::noinline]] void release(void) { omp_unset_lock(&Lock); }
+#pragma omp end declare target
+
+int main() {
+  // LIBC: omp_set_lock: threads of one wavefront are contending for the same lock, which can never be released; aborting
+  // CHECK: execution interrupted by hardware trap instruction
+#pragma omp target parallel num_threads(64)
+  {
+#pragma omp single
+    omp_init_lock(&Lock);
+
+    acquire();
+    X++;
+    release();
+  }
+
+  // CHECK-NOT: not reached
+  printf("not reached\n");
+  return 0;
+}
diff --git a/offload/test/api/omp_test_lock.c b/offload/test/api/omp_test_lock.c
index f8d082bc2b3d5..80aca238de6d7 100644
--- a/offload/test/api/omp_test_lock.c
+++ b/offload/test/api/omp_test_lock.c
@@ -3,9 +3,6 @@
 // omp_test_lock in device code tries to take the lock and reports whether it
 // succeeded.
 
-// The other AMDGPU device lock routines are not implemented yet.
-// XFAIL: amdgpu
-
 #include <omp.h>
 #include <stdio.h>
 
diff --git a/openmp/device/include/LibC.h b/openmp/device/include/LibC.h
index 94b5e65196067..99664193b9898 100644
--- a/openmp/device/include/LibC.h
+++ b/openmp/device/include/LibC.h
@@ -18,6 +18,10 @@ namespace ompx {
 
 int printf(const char *Format, ...);
 
+/// Writes \p Message where the user will see it even if the kernel then
+/// aborts: to stderr when the GPU libc is available, otherwise with printf.
+void reportError(const char *Message);
+
 } // namespace ompx
 
 #endif
diff --git a/openmp/device/src/LibC.cpp b/openmp/device/src/LibC.cpp
index 905f341ee88a2..2aa6a204f128c 100644
--- a/openmp/device/src/LibC.cpp
+++ b/openmp/device/src/LibC.cpp
@@ -14,6 +14,16 @@ extern "C" int vprintf(const char *format, __builtin_va_list) { return -1; }
 extern "C" int vprintf(const char *format, __builtin_va_list);
 #endif
 
+#ifdef OMPTARGET_HAS_LIBC
+// Provided by the GPU libc. The host writes stderr unbuffered, so a message
+// sent there survives a kernel abort, whereas printf output to stdout may still
+// be buffered when the host process is terminated. The libc's public headers
+// are generated when it is built and are not available here, so declare what
+// is used; FILE is only ever handled through a pointer.
+extern "C" void *stderr;
+extern "C" int fputs(const char *, void *);
+#endif
+
 extern "C" {
 [[gnu::weak]] int memcmp(const void *lhs, const void *rhs, size_t count) {
   auto *L = reinterpret_cast<const unsigned char *>(lhs);
@@ -45,4 +55,12 @@ namespace ompx {
   __builtin_va_start(vlist, Format);
   return ::vprintf(Format, vlist);
 }
+
+void reportError(const char *Message) {
+#ifdef OMPTARGET_HAS_LIBC
+  ::fputs(Message, ::stderr);
+#else
+  printf("%s", Message);
+#endif
+}
 } // namespace ompx
diff --git a/openmp/device/src/Synchronization.cpp b/openmp/device/src/Synchronization.cpp
index 241ece8a11b9c..dab218f406048 100644
--- a/openmp/device/src/Synchronization.cpp
+++ b/openmp/device/src/Synchronization.cpp
@@ -91,15 +91,43 @@ void syncThreadsAligned(atomic::OrderingTy Ordering) {
   synchronize::threads(Ordering);
 }
 
-// TODO: Don't have wavefront lane locks. Possibly can't have them.
-void unsetLock(omp_lock_t *) { __builtin_trap(); }
-void initLock(omp_lock_t *) { __builtin_trap(); }
-void destroyLock(omp_lock_t *) { __builtin_trap(); }
-void setLock(omp_lock_t *) { __builtin_trap(); }
-
 constexpr uint32_t UNSET = 0;
 constexpr uint32_t SET = 1;
 
+void unsetLock(omp_lock_t *Lock) {
+  [[maybe_unused]] uint32_t Before = atomicExchange(
+      reinterpret_cast<uint32_t *>(Lock), UNSET, atomic::seq_cst);
+}
+
+void initLock(omp_lock_t *Lock) { unsetLock(Lock); }
+
+void destroyLock(omp_lock_t *Lock) { unsetLock(Lock); }
+
+// The lanes of a wavefront share one instruction stream. A lane that wins the
+// lock is masked off until every other active lane has left the spin loop, so
+// it cannot reach its omp_unset_lock while another lane of the same wavefront
+// is still waiting for the lock: two such lanes never make progress. OpenMP 6.0
+// permits this (divergent threads that synchronize within one progress unit may
+// deadlock), but a hung kernel is far harder to diagnose than a trap, so report
+// it. Lanes that want different locks, and lanes arriving one at a time (for
+// instance because the compiler has serialized them), wait only for other
+// wavefronts, which do make progress.
+void setLock(omp_lock_t *Lock) {
+  LaneMaskTy Active = mapping::activemask();
+  if (utils::popc(Active) > 1 &&
+      utils::popc(
+          __gpu_match_any_u64(Active, reinterpret_cast<uint64_t>(Lock))) > 1) {
+    if (mapping::isLeaderInWarp())
+      reportError("omp_set_lock: threads of one wavefront are contending for "
+                  "the same lock, which can never be released; aborting\n");
+    __builtin_trap();
+  }
+  // Back off as setCriticalLock does.
+  while (!atomic::cas(reinterpret_cast<uint32_t *>(Lock), UNSET, SET,
+                      atomic::seq_cst, atomic::seq_cst))
+    __builtin_amdgcn_s_sleep(32);
+}
+
 void unsetCriticalLock(omp_lock_t *Lock) {
   [[maybe_unused]] uint32_t before =
       atomicExchange((uint32_t *)Lock, UNSET, atomic::acq_rel);



More information about the llvm-branch-commits mailing list