[llvm-branch-commits] [llvm] [openmp] [OpenMP] Make device omp_test_lock try to set the lock (PR #242477)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 13 12:49:02 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Julian Brown (jtb20)

<details>
<summary>Changes</summary>

This patch fixes the semantics of omp_test_lock for NVPTX and SPIR-V, and uses the same implementation for AMDGPU too.  The function is intended to take the lock without blocking, and return whether it succeeded, whereas the previous implementations just tested whether the lock was already taken or not.

Other AMDGPU lock routines are added by subsequent patches, so the newly-added test is XFAILed for that target until those land.

Assisted-By: Claude

---
Full diff: https://github.com/llvm/llvm-project/pull/242477.diff


2 Files Affected:

- (added) offload/test/api/omp_test_lock.c (+62) 
- (modified) openmp/device/src/Synchronization.cpp (+7-8) 


``````````diff
diff --git a/offload/test/api/omp_test_lock.c b/offload/test/api/omp_test_lock.c
new file mode 100644
index 0000000000000..f8d082bc2b3d5
--- /dev/null
+++ b/offload/test/api/omp_test_lock.c
@@ -0,0 +1,62 @@
+// RUN: %libomptarget-compile-run-and-check-generic
+
+// 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>
+
+#pragma omp declare target
+omp_lock_t RaceLock;
+omp_lock_t SeqLock;
+#pragma omp end declare target
+
+int main() {
+  int Winners = 0;
+  int Seq[2] = {0, 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(256) map(tofrom : Winners, Seq)
+  {
+#pragma omp single
+    {
+      omp_init_lock(&RaceLock);
+      omp_init_lock(&SeqLock);
+    }
+
+    // Of many threads trying to take a free lock, exactly one succeeds.
+    int Won = omp_test_lock(&RaceLock);
+    if (Won) {
+#pragma omp atomic
+      Winners += 1;
+    }
+#pragma omp barrier
+    if (Won)
+      omp_unset_lock(&RaceLock);
+
+    // A free lock is taken, and is free again once released.
+#pragma omp single
+    {
+      Seq[0] = omp_test_lock(&SeqLock);
+      omp_unset_lock(&SeqLock);
+      Seq[1] = omp_test_lock(&SeqLock);
+      omp_unset_lock(&SeqLock);
+    }
+
+#pragma omp single
+    {
+      omp_destroy_lock(&RaceLock);
+      omp_destroy_lock(&SeqLock);
+    }
+  }
+
+  // CHECK: Winners = 1
+  printf("Winners = %d\n", Winners);
+  // CHECK: Seq = 1 1
+  printf("Seq = %d %d\n", Seq[0] != 0, Seq[1] != 0);
+  return 0;
+}
diff --git a/openmp/device/src/Synchronization.cpp b/openmp/device/src/Synchronization.cpp
index d1b772becab41..241ece8a11b9c 100644
--- a/openmp/device/src/Synchronization.cpp
+++ b/openmp/device/src/Synchronization.cpp
@@ -93,7 +93,6 @@ void syncThreadsAligned(atomic::OrderingTy Ordering) {
 
 // TODO: Don't have wavefront lane locks. Possibly can't have them.
 void unsetLock(omp_lock_t *) { __builtin_trap(); }
-int testLock(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(); }
@@ -149,10 +148,6 @@ void unsetLock(omp_lock_t *Lock) {
       reinterpret_cast<uint32_t *>(Lock), UNSET, atomic::seq_cst);
 }
 
-int testLock(omp_lock_t *Lock) {
-  return atomic::add(reinterpret_cast<uint32_t *>(Lock), 0u, atomic::seq_cst);
-}
-
 void initLock(omp_lock_t *Lock) { unsetLock(Lock); }
 
 void destroyLock(omp_lock_t *Lock) { unsetLock(Lock); }
@@ -206,9 +201,6 @@ void namedBarrier() {
 void unsetLock(omp_lock_t *Lock) {
   atomic::store((int32_t *)Lock, 0, atomic::seq_cst);
 }
-int testLock(omp_lock_t *Lock) {
-  return atomic::add((int32_t *)Lock, 0, atomic::seq_cst);
-}
 void initLock(omp_lock_t *Lock) { unsetLock(Lock); }
 void destroyLock(omp_lock_t *Lock) { unsetLock(Lock); }
 void setLock(omp_lock_t *Lock) {
@@ -224,6 +216,13 @@ void syncThreadsAligned(atomic::OrderingTy Ordering) {
 }
 #endif
 
+// A lock is a 32-bit word that is 1 while the lock is held and 0 otherwise.
+// omp_test_lock tries once to take the lock, and reports whether it did.
+int testLock(omp_lock_t *Lock) {
+  return atomic::cas(reinterpret_cast<uint32_t *>(Lock), 0u, 1u,
+                     atomic::seq_cst, atomic::seq_cst);
+}
+
 } // namespace impl
 
 void synchronize::init(bool IsSPMD) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/242477


More information about the llvm-branch-commits mailing list