[libclc] [libclc] Move sign to the CLC builtins library (PR #115699)

Fraser Cormack via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 04:11:14 PST 2024


https://github.com/frasercrmck updated https://github.com/llvm/llvm-project/pull/115699

>From 0a9bcdc5fb9da8ada6d2580026f498f282ca7cf7 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fraser at codeplay.com>
Date: Thu, 7 Nov 2024 12:40:56 +0000
Subject: [PATCH] [libclc] Move sign to the CLC builtins library

This patch necessitates some changes to how CLSPV and SPIR-V targets are
built. This is the first patch in this series in which an OpenCL
function declaration has been called from the CLC library for these
targets. Since libclc's OpenCL headers aren't being included at this
stage, the OpenCL sign function isn't available. To fix this, these two
libclc targets now have clang declare OpenCL builtins when building the
internal CLC library.

The __CLC_INTERNAL preprocessor definition has been repurposed (without
the leading underscores) to be passed when building the internal CLC
library. It was only used in one other place to guard an extra maths
preprocessor definition, which we can do unconditionally.

There are no changes (with llvm-diff) to any libclc target other than
SPIR-V, which now has OpenCL sign call __clc_sign.
---
 libclc/CMakeLists.txt                         | 11 ++-
 libclc/clc/include/clc/common/clc_sign.h      | 17 ++++
 libclc/clc/include/clc/common/floatn.inc      | 85 +++++++++++++++++++
 libclc/clc/include/clc/common/unary_def.inc   |  7 ++
 libclc/clc/lib/generic/SOURCES                |  1 +
 libclc/clc/lib/generic/common/clc_sign.cl     | 38 +++++++++
 libclc/clc/lib/spirv/SOURCES                  |  1 +
 libclc/clc/lib/spirv64/SOURCES                |  1 +
 .../generic/include/clc/float/definitions.h   |  4 +-
 libclc/generic/lib/common/sign.cl             | 37 +-------
 10 files changed, 164 insertions(+), 38 deletions(-)
 create mode 100644 libclc/clc/include/clc/common/clc_sign.h
 create mode 100644 libclc/clc/include/clc/common/floatn.inc
 create mode 100644 libclc/clc/include/clc/common/unary_def.inc
 create mode 100644 libclc/clc/lib/generic/common/clc_sign.cl

diff --git a/libclc/CMakeLists.txt b/libclc/CMakeLists.txt
index 2c2c7f16e29442..68ce43605b8dd6 100644
--- a/libclc/CMakeLists.txt
+++ b/libclc/CMakeLists.txt
@@ -347,7 +347,6 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
     string( TOUPPER "CLC_${MACRO_ARCH}" CLC_TARGET_DEFINE )
 
     list( APPEND build_flags
-      -D__CLC_INTERNAL
       -D${CLC_TARGET_DEFINE}
       # All libclc builtin libraries see CLC headers
       -I${CMAKE_CURRENT_SOURCE_DIR}/clc/include
@@ -359,12 +358,20 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
       list( APPEND build_flags -mcpu=${cpu} )
     endif()
 
+    set( clc_build_flags ${build_flags} -DCLC_INTERNAL )
+
+    # clspv and spir-v targets remap some CLC functions to OpenCL builtins.
+    # Automatically provide those declarations to the compiler for CLC builtins.
+    if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 OR ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
+      list( APPEND clc_build_flags -Xclang -fdeclare-opencl-builtins )
+    endif()
+
     add_libclc_builtin_set(
       CLC_INTERNAL
       ARCH ${ARCH}
       ARCH_SUFFIX clc-${arch_suffix}
       TRIPLE ${clang_triple}
-      COMPILE_FLAGS ${build_flags}
+      COMPILE_FLAGS ${clc_build_flags}
       OPT_FLAGS ${opt_flags}
       LIB_FILES ${clc_lib_files}
     )
diff --git a/libclc/clc/include/clc/common/clc_sign.h b/libclc/clc/include/clc/common/clc_sign.h
new file mode 100644
index 00000000000000..bda4fa65dfa890
--- /dev/null
+++ b/libclc/clc/include/clc/common/clc_sign.h
@@ -0,0 +1,17 @@
+#ifndef __CLC_COMMON_CLC_SIGN_H__
+#define __CLC_COMMON_CLC_SIGN_H__
+
+#if defined(CLC_CLSPV)
+// clspv targets provide their own OpenCL-compatible sign
+#define __clc_sign sign
+#else
+
+#define __CLC_FUNCTION __clc_sign
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#include <clc/math/gentype.inc>
+#undef __CLC_FUNCTION
+#undef __CLC_BODY
+
+#endif
+
+#endif // __CLC_COMMON_CLC_SIGN_H__
diff --git a/libclc/clc/include/clc/common/floatn.inc b/libclc/clc/include/clc/common/floatn.inc
new file mode 100644
index 00000000000000..2c4285d5bcbc58
--- /dev/null
+++ b/libclc/clc/include/clc/common/floatn.inc
@@ -0,0 +1,85 @@
+#define __CLC_FLOATN float
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN float2
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN float3
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN float4
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN float8
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN float16
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#undef __CLC_FLOAT
+#undef __CLC_INT
+
+#ifdef cl_khr_fp64
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+#define __CLC_FLOATN double
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN double2
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN double3
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN double4
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN double8
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN double16
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#endif
+#ifdef cl_khr_fp16
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+#define __CLC_FLOATN half
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN half2
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN half3
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN half4
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN half8
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#define __CLC_FLOATN half16
+#include __CLC_BODY
+#undef __CLC_FLOATN
+
+#endif
+
+#undef __CLC_BODY
diff --git a/libclc/clc/include/clc/common/unary_def.inc b/libclc/clc/include/clc/common/unary_def.inc
new file mode 100644
index 00000000000000..d4a3bf80775f0e
--- /dev/null
+++ b/libclc/clc/include/clc/common/unary_def.inc
@@ -0,0 +1,7 @@
+#include <clc/utils.h>
+
+#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
+
+_CLC_OVERLOAD _CLC_DEF __CLC_FLOATN FUNCTION(__CLC_FLOATN a) {
+  return __CLC_FUNCTION(FUNCTION)(a);
+}
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index d7ffaaf6dc3f42..133705f9c1c96d 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -1,3 +1,4 @@
+common/clc_sign.cl
 geometric/clc_dot.cl
 integer/clc_abs.cl
 integer/clc_abs_diff.cl
diff --git a/libclc/clc/lib/generic/common/clc_sign.cl b/libclc/clc/lib/generic/common/clc_sign.cl
new file mode 100644
index 00000000000000..6c4db4af3f4c57
--- /dev/null
+++ b/libclc/clc/lib/generic/common/clc_sign.cl
@@ -0,0 +1,38 @@
+#include <clc/clcmacro.h>
+#include <clc/internal/clc.h>
+#include <clc/relational/clc_isnan.h>
+
+#define CLC_SIGN(TYPE, F)                                                      \
+  _CLC_DEF _CLC_OVERLOAD TYPE __clc_sign(TYPE x) {                             \
+    if (__clc_isnan(x)) {                                                      \
+      return 0.0F;                                                             \
+    }                                                                          \
+    if (x > 0.0F) {                                                            \
+      return 1.0F;                                                             \
+    }                                                                          \
+    if (x < 0.0F) {                                                            \
+      return -1.0F;                                                            \
+    }                                                                          \
+    return x; /* -0.0 or +0.0 */                                               \
+  }
+
+CLC_SIGN(float, f)
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_sign, float)
+
+#ifdef cl_khr_fp64
+
+#pragma OPENCL EXTENSION cl_khr_fp64 : enable
+
+CLC_SIGN(double, )
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_sign, double)
+
+#endif
+
+#ifdef cl_khr_fp16
+
+#pragma OPENCL EXTENSION cl_khr_fp16 : enable
+
+CLC_SIGN(half, )
+_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_sign, half)
+
+#endif
diff --git a/libclc/clc/lib/spirv/SOURCES b/libclc/clc/lib/spirv/SOURCES
index d8effd19613c8b..20e869863e00a2 100644
--- a/libclc/clc/lib/spirv/SOURCES
+++ b/libclc/clc/lib/spirv/SOURCES
@@ -1,2 +1,3 @@
+../generic/common/clc_sign.cl
 ../generic/geometric/clc_dot.cl
 
diff --git a/libclc/clc/lib/spirv64/SOURCES b/libclc/clc/lib/spirv64/SOURCES
index 9200810ace38e7..befbc7052982a1 100644
--- a/libclc/clc/lib/spirv64/SOURCES
+++ b/libclc/clc/lib/spirv64/SOURCES
@@ -1 +1,2 @@
+../generic/common/clc_sign.cl
 ../generic/geometric/clc_dot.cl
diff --git a/libclc/generic/include/clc/float/definitions.h b/libclc/generic/include/clc/float/definitions.h
index 62501230c92de2..be3d0130f3e614 100644
--- a/libclc/generic/include/clc/float/definitions.h
+++ b/libclc/generic/include/clc/float/definitions.h
@@ -31,9 +31,7 @@
 #define M_SQRT2_F       0x1.6a09e6p+0f
 #define M_SQRT1_2_F     0x1.6a09e6p-1f
 
-#ifdef __CLC_INTERNAL
-#define M_LOG210_F      0x1.a934f0p+1f
-#endif
+#define M_LOG210_F 0x1.a934f0p+1f
 
 #ifdef cl_khr_fp64
 
diff --git a/libclc/generic/lib/common/sign.cl b/libclc/generic/lib/common/sign.cl
index ad8f7405e0cb30..5e70ef73f3f45c 100644
--- a/libclc/generic/lib/common/sign.cl
+++ b/libclc/generic/lib/common/sign.cl
@@ -1,37 +1,8 @@
 #include <clc/clc.h>
 #include <clc/clcmacro.h>
+#include <clc/common/clc_sign.h>
 
-#define SIGN(TYPE, F) \
-_CLC_DEF _CLC_OVERLOAD TYPE sign(TYPE x) { \
-  if (isnan(x)) { \
-    return 0.0F;   \
-  }               \
-  if (x > 0.0F) { \
-    return 1.0F;  \
-  }               \
-  if (x < 0.0F) { \
-    return -1.0F; \
-  }               \
-  return x; /* -0.0 or +0.0 */  \
-}
+#define FUNCTION sign
+#define __CLC_BODY <clc/common/unary_def.inc>
 
-SIGN(float, f)
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, sign, float)
-
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-SIGN(double, )
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, sign, double)
-
-#endif
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-SIGN(half,)
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, sign, half)
-
-#endif
+#include <clc/common/floatn.inc>



More information about the cfe-commits mailing list