[libclc] 80ff736 - libclc: Add __printf_alloc implementation (#185231)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 7 23:42:30 PST 2026
Author: Matt Arsenault
Date: 2026-03-08T08:42:25+01:00
New Revision: 80ff7369c9ecdbef23a95b234eef3f05e7c37575
URL: https://github.com/llvm/llvm-project/commit/80ff7369c9ecdbef23a95b234eef3f05e7c37575
DIFF: https://github.com/llvm/llvm-project/commit/80ff7369c9ecdbef23a95b234eef3f05e7c37575.diff
LOG: libclc: Add __printf_alloc implementation (#185231)
AMDGPU OpenCL printf implementation emits a call to this helper
function.
Added:
libclc/opencl/lib/amdgcn/printf/__printf_alloc.cl
Modified:
libclc/opencl/lib/amdgcn/SOURCES
Removed:
################################################################################
diff --git a/libclc/opencl/lib/amdgcn/SOURCES b/libclc/opencl/lib/amdgcn/SOURCES
index 4153d964e118d..caf08495e8dae 100644
--- a/libclc/opencl/lib/amdgcn/SOURCES
+++ b/libclc/opencl/lib/amdgcn/SOURCES
@@ -1,4 +1,5 @@
async/wait_group_events.cl
mem_fence/fence.cl
+printf/__printf_alloc.cl
subgroup/subgroup.cl
synchronization/sub_group_barrier.cl
diff --git a/libclc/opencl/lib/amdgcn/printf/__printf_alloc.cl b/libclc/opencl/lib/amdgcn/printf/__printf_alloc.cl
new file mode 100644
index 0000000000000..f4a7f46cbbcac
--- /dev/null
+++ b/libclc/opencl/lib/amdgcn/printf/__printf_alloc.cl
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <amdhsa_abi.h>
+
+#define OFFSET 8
+
+// Atomically reserves space to the printf data buffer and returns a pointer to
+// it
+__global char *__printf_alloc(uint bytes) {
+ __constant amdhsa_implicit_kernarg_v5 *args =
+ (__constant amdhsa_implicit_kernarg_v5 *)
+ __builtin_amdgcn_implicitarg_ptr();
+ __global char *ptr = (__global char *)args->printf_buffer;
+
+ uint size = ((__global uint *)ptr)[1];
+ uint offset = __opencl_atomic_load((__global atomic_uint *)ptr,
+ memory_order_relaxed, memory_scope_device);
+
+ for (;;) {
+ if (OFFSET + offset + bytes > size)
+ return NULL;
+
+ if (__opencl_atomic_compare_exchange_strong(
+ (__global atomic_uint *)ptr, &offset, offset + bytes,
+ memory_order_relaxed, memory_order_relaxed, memory_scope_device))
+ break;
+ }
+
+ return ptr + OFFSET + offset;
+}
More information about the cfe-commits
mailing list