[libclc] 68bb8a0 - libclc: Add work_group_any/work_group_all implementation (#185260)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 8 00:41:24 PST 2026


Author: Matt Arsenault
Date: 2026-03-08T09:41:20+01:00
New Revision: 68bb8a0ded61c11b7bc7f91f8d4c7a9bbb04b7d0

URL: https://github.com/llvm/llvm-project/commit/68bb8a0ded61c11b7bc7f91f8d4c7a9bbb04b7d0
DIFF: https://github.com/llvm/llvm-project/commit/68bb8a0ded61c11b7bc7f91f8d4c7a9bbb04b7d0.diff

LOG: libclc: Add work_group_any/work_group_all implementation (#185260)

Added: 
    libclc/clc/include/clc/collective/clc_work_group_any_all.h
    libclc/clc/lib/generic/collective/clc_work_group_any_all.cl
    libclc/opencl/lib/generic/collective/work_group_any_all.cl

Modified: 
    libclc/clc/lib/generic/SOURCES
    libclc/opencl/lib/generic/SOURCES

Removed: 
    


################################################################################
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 cfe-commits mailing list