[libclc] libclc: Add uintptr_t overloads of atomic_fetch_add and sub (PR #185263)

Matt Arsenault via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 8 00:40:52 PST 2026


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

This is a special case because the pointee type is unsigned, but the
input value is signed. Directly use the opencl builtins, because these
work correctly without any ugly casting required, and it's not worth
putting a wrapper in clc.

>From 81a9f1e17450e2aa6a8ebbaec8188a7dd659b2bc Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Sun, 8 Mar 2026 09:33:49 +0100
Subject: [PATCH] libclc: Add uintptr_t overloads of atomic_fetch_add and sub

This is a special case because the pointee type is unsigned, but the
input value is signed. Directly use the opencl builtins, because these
work correctly without any ugly casting required, and it's not worth
putting a wrapper in clc.
---
 libclc/opencl/lib/generic/atomic/atomic_fetch_add.cl | 6 ++++++
 libclc/opencl/lib/generic/atomic/atomic_fetch_sub.cl | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/libclc/opencl/lib/generic/atomic/atomic_fetch_add.cl b/libclc/opencl/lib/generic/atomic/atomic_fetch_add.cl
index 428ac19830f7f..fc9a61828c662 100644
--- a/libclc/opencl/lib/generic/atomic/atomic_fetch_add.cl
+++ b/libclc/opencl/lib/generic/atomic/atomic_fetch_add.cl
@@ -17,3 +17,9 @@
 
 #define __CLC_BODY <atomic_def.inc>
 #include <clc/math/gentype.inc>
+
+_CLC_OVERLOAD _CLC_DEF uintptr_t atomic_fetch_add(volatile atomic_uintptr_t *p,
+                                                  ptrdiff_t v) {
+  return __opencl_atomic_fetch_add(p, v, memory_order_seq_cst,
+                                   memory_scope_device);
+}
diff --git a/libclc/opencl/lib/generic/atomic/atomic_fetch_sub.cl b/libclc/opencl/lib/generic/atomic/atomic_fetch_sub.cl
index 474aca6658014..e7e78033dd103 100644
--- a/libclc/opencl/lib/generic/atomic/atomic_fetch_sub.cl
+++ b/libclc/opencl/lib/generic/atomic/atomic_fetch_sub.cl
@@ -17,3 +17,9 @@
 
 #define __CLC_BODY <atomic_def.inc>
 #include <clc/math/gentype.inc>
+
+_CLC_OVERLOAD _CLC_DEF uintptr_t atomic_fetch_sub(volatile atomic_uintptr_t *p,
+                                                  ptrdiff_t v) {
+  return __opencl_atomic_fetch_sub(p, v, memory_order_seq_cst,
+                                   memory_scope_device);
+}



More information about the cfe-commits mailing list