[llvm-branch-commits] [libclc] libclc: Add work_group_any/work_group_all implementation (PR #185260)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Mar 8 00:14:35 PST 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/185260

None

>From 1a39e70775401605388713a56926c41ec83fed28 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sat, 7 Mar 2026 18:57:55 +0100
Subject: [PATCH] libclc: Add work_group_any/work_group_all implementation

---
 .../clc/collective/clc_work_group_any_all.h   | 17 ++++++
 libclc/clc/lib/generic/SOURCES                |  1 +
 .../collective/clc_work_group_any_all.cl      | 58 +++++++++++++++++++
 libclc/opencl/lib/generic/SOURCES             |  1 +
 .../generic/collective/work_group_any_all.cl  | 17 ++++++
 5 files changed, 94 insertions(+)
 create mode 100644 libclc/clc/include/clc/collective/clc_work_group_any_all.h
 create mode 100644 libclc/clc/lib/generic/collective/clc_work_group_any_all.cl
 create mode 100644 libclc/opencl/lib/generic/collective/work_group_any_all.cl

diff --git a/libclc/clc/include/clc/collective/clc_work_group_any_all.h b/libclc/clc/include/clc/collective/clc_work_group_any_all.h
new file mode 100644
index 0000000000000..40c8c515f5998
--- /dev/null
+++ b/libclc/clc/include/clc/collective/clc_work_group_any_all.h
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __CLC_COLLECTIVE_CLC_WORK_GROUP_ANY_ALL_H__
+#define __CLC_COLLECTIVE_CLC_WORK_GROUP_ANY_ALL_H__
+
+#include "clc/internal/clc.h"
+
+_CLC_OVERLOAD _CLC_DECL int __clc_work_group_any(int predicate);
+_CLC_OVERLOAD _CLC_DECL int __clc_work_group_all(int predicate);
+
+#endif // __CLC_COLLECTIVE_CLC_WORK_GROUP_ANY_ALL_H__
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index b7ca616299556..0e9166a2b249e 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -14,6 +14,7 @@ atomic/clc_atomic_flag_test_and_set.cl
 atomic/clc_atomic_inc.cl
 atomic/clc_atomic_load.cl
 atomic/clc_atomic_store.cl
+collective/clc_work_group_any_all.cl
 common/clc_degrees.cl
 common/clc_radians.cl
 common/clc_sign.cl
diff --git a/libclc/clc/lib/generic/collective/clc_work_group_any_all.cl b/libclc/clc/lib/generic/collective/clc_work_group_any_all.cl
new file mode 100644
index 0000000000000..4c79ef1f73eba
--- /dev/null
+++ b/libclc/clc/lib/generic/collective/clc_work_group_any_all.cl
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clc/atomic/clc_atomic_fetch_and.h"
+#include "clc/atomic/clc_atomic_fetch_or.h"
+#include "clc/atomic/clc_atomic_load.h"
+#include "clc/atomic/clc_atomic_store.h"
+#include "clc/collective/clc_work_group_any_all.h"
+#include "clc/subgroup/clc_subgroup.h"
+#include "clc/synchronization/clc_work_group_barrier.h"
+
+#pragma OPENCL EXTENSION __cl_clang_function_scope_local_variables : enable
+
+static int work_group_any_all_impl(int predicate, bool is_all) {
+  __local uint scratch;
+
+  uint n = __clc_get_num_sub_groups();
+  int a =
+      is_all ? __clc_sub_group_all(predicate) : __clc_sub_group_any(predicate);
+  if (n == 1)
+    return a;
+
+  uint l = __clc_get_sub_group_local_id();
+  uint i = __clc_get_sub_group_id();
+
+  if ((i == 0) & (l == 0))
+    __clc_atomic_store(&scratch, a, __ATOMIC_RELAXED, __MEMORY_SCOPE_WRKGRP);
+
+  __clc_work_group_barrier(__MEMORY_SCOPE_WRKGRP, __CLC_MEMORY_LOCAL);
+
+  if ((i != 0) & (l == 0)) {
+    if (is_all)
+      __clc_atomic_fetch_and(&scratch, a, __ATOMIC_RELAXED,
+                             __MEMORY_SCOPE_WRKGRP);
+    else
+      __clc_atomic_fetch_or(&scratch, a, __ATOMIC_RELAXED,
+                            __MEMORY_SCOPE_WRKGRP);
+  }
+
+  __clc_work_group_barrier(__MEMORY_SCOPE_WRKGRP, __CLC_MEMORY_LOCAL);
+  a = __clc_atomic_load(&scratch, __ATOMIC_RELAXED, __MEMORY_SCOPE_WRKGRP);
+  __clc_work_group_barrier(__MEMORY_SCOPE_WRKGRP, __CLC_MEMORY_LOCAL);
+
+  return a;
+}
+
+_CLC_OVERLOAD _CLC_DEF int __clc_work_group_all(int predicate) {
+  return work_group_any_all_impl(predicate, true);
+}
+
+_CLC_OVERLOAD _CLC_DEF int __clc_work_group_any(int predicate) {
+  return work_group_any_all_impl(predicate, false);
+}
diff --git a/libclc/opencl/lib/generic/SOURCES b/libclc/opencl/lib/generic/SOURCES
index f735c66548c30..5dc92c27f9d74 100644
--- a/libclc/opencl/lib/generic/SOURCES
+++ b/libclc/opencl/lib/generic/SOURCES
@@ -42,6 +42,7 @@ atomic/atom_sub.cl
 atomic/atom_xchg.cl
 atomic/atom_xor.cl
 atomic/atomic_work_item_fence.cl
+collective/work_group_any_all.cl
 common/degrees.cl
 common/mix.cl
 common/radians.cl
diff --git a/libclc/opencl/lib/generic/collective/work_group_any_all.cl b/libclc/opencl/lib/generic/collective/work_group_any_all.cl
new file mode 100644
index 0000000000000..7d1391deaa2e3
--- /dev/null
+++ b/libclc/opencl/lib/generic/collective/work_group_any_all.cl
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "clc/collective/clc_work_group_any_all.h"
+
+_CLC_OVERLOAD _CLC_DEF int work_group_all(int predicate) {
+  return __clc_work_group_all(predicate);
+}
+
+_CLC_OVERLOAD _CLC_DEF int work_group_any(int predicate) {
+  return __clc_work_group_any(predicate);
+}



More information about the llvm-branch-commits mailing list