[libclc] 9fa81a4 - [libclc] Move step to the CLC library; add missing half variants (#140936)

via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 01:54:30 PDT 2025


Author: Fraser Cormack
Date: 2025-05-22T09:54:27+01:00
New Revision: 9fa81a486e317c7201318d710559093b3a5233bb

URL: https://github.com/llvm/llvm-project/commit/9fa81a486e317c7201318d710559093b3a5233bb
DIFF: https://github.com/llvm/llvm-project/commit/9fa81a486e317c7201318d710559093b3a5233bb.diff

LOG: [libclc] Move step to the CLC library; add missing half variants (#140936)

The half variants were missing but are trivial to implement. There were
some incorrect mixed type overloads (step(float, double)) which aren't
in the OpenCL specification and so have been removed.

Like certain other builtins the CLC step function only deals with
identical types. The OpenCL layer is responsible for casting the scalar
argument to a vector.

This commit also trivially vectorizes the CLC function, generating
better bytecode.

Added: 
    libclc/clc/include/clc/common/clc_step.h
    libclc/clc/lib/generic/common/clc_step.cl
    libclc/clc/lib/generic/common/clc_step.inc
    libclc/opencl/lib/generic/common/step.inc

Modified: 
    libclc/clc/include/clc/clcmacro.h
    libclc/clc/lib/generic/SOURCES
    libclc/opencl/include/clc/opencl/common/step.inc
    libclc/opencl/lib/generic/common/step.cl

Removed: 
    


################################################################################
diff  --git a/libclc/clc/include/clc/clcmacro.h b/libclc/clc/include/clc/clcmacro.h
index c9f70d2998d37..de7b977021f8b 100644
--- a/libclc/clc/include/clc/clcmacro.h
+++ b/libclc/clc/include/clc/clcmacro.h
@@ -73,39 +73,6 @@
         FUNCTION(x.sf, y.sf));                                                 \
   }
 
-#define _CLC_V_S_V_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE,          \
-                             ARG2_TYPE)                                        \
-  DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE x, ARG2_TYPE##2 y) {                 \
-    return (RET_TYPE##2)(FUNCTION(x, y.s0), FUNCTION(x, y.s1));                \
-  }                                                                            \
-                                                                               \
-  DECLSPEC RET_TYPE##3 FUNCTION(ARG1_TYPE x, ARG2_TYPE##3 y) {                 \
-    return (RET_TYPE##3)(FUNCTION(x, y.s0), FUNCTION(x, y.s1),                 \
-                         FUNCTION(x, y.s2));                                   \
-  }                                                                            \
-                                                                               \
-  DECLSPEC RET_TYPE##4 FUNCTION(ARG1_TYPE x, ARG2_TYPE##4 y) {                 \
-    return (RET_TYPE##4)(FUNCTION(x, y.s0), FUNCTION(x, y.s1),                 \
-                         FUNCTION(x, y.s2), FUNCTION(x, y.s3));                \
-  }                                                                            \
-                                                                               \
-  DECLSPEC RET_TYPE##8 FUNCTION(ARG1_TYPE x, ARG2_TYPE##8 y) {                 \
-    return (RET_TYPE##8)(FUNCTION(x, y.s0), FUNCTION(x, y.s1),                 \
-                         FUNCTION(x, y.s2), FUNCTION(x, y.s3),                 \
-                         FUNCTION(x, y.s4), FUNCTION(x, y.s5),                 \
-                         FUNCTION(x, y.s6), FUNCTION(x, y.s7));                \
-  }                                                                            \
-                                                                               \
-  DECLSPEC RET_TYPE##16 FUNCTION(ARG1_TYPE x, ARG2_TYPE##16 y) {               \
-    return (RET_TYPE##16)(                                                     \
-        FUNCTION(x, y.s0), FUNCTION(x, y.s1), FUNCTION(x, y.s2),               \
-        FUNCTION(x, y.s3), FUNCTION(x, y.s4), FUNCTION(x, y.s5),               \
-        FUNCTION(x, y.s6), FUNCTION(x, y.s7), FUNCTION(x, y.s8),               \
-        FUNCTION(x, y.s9), FUNCTION(x, y.sa), FUNCTION(x, y.sb),               \
-        FUNCTION(x, y.sc), FUNCTION(x, y.sd), FUNCTION(x, y.se),               \
-        FUNCTION(x, y.sf));                                                    \
-  }
-
 #define _CLC_TERNARY_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE,        \
                                ARG2_TYPE, ARG3_TYPE)                           \
   DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE##2 x, ARG2_TYPE##2 y,                \

diff  --git a/libclc/clc/include/clc/common/clc_step.h b/libclc/clc/include/clc/common/clc_step.h
new file mode 100644
index 0000000000000..6b093d06896c0
--- /dev/null
+++ b/libclc/clc/include/clc/common/clc_step.h
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_COMMON_CLC_STEP_H__
+#define __CLC_COMMON_CLC_STEP_H__
+
+#define __CLC_FUNCTION __clc_step
+#define __CLC_BODY <clc/shared/binary_decl.inc>
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_FUNCTION
+
+#endif // __CLC_COMMON_CLC_STEP_H__

diff  --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index 1cc8730d2ae8f..a8a906159e286 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -2,6 +2,7 @@ common/clc_degrees.cl
 common/clc_radians.cl
 common/clc_sign.cl
 common/clc_smoothstep.cl
+common/clc_step.cl
 geometric/clc_cross.cl
 geometric/clc_distance.cl
 geometric/clc_dot.cl

diff  --git a/libclc/clc/lib/generic/common/clc_step.cl b/libclc/clc/lib/generic/common/clc_step.cl
new file mode 100644
index 0000000000000..c21c27a3e9404
--- /dev/null
+++ b/libclc/clc/lib/generic/common/clc_step.cl
@@ -0,0 +1,12 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/clcmacro.h>
+
+#define __CLC_BODY <clc_step.inc>
+#include <clc/math/gentype.inc>

diff  --git a/libclc/clc/lib/generic/common/clc_step.inc b/libclc/clc/lib/generic/common/clc_step.inc
new file mode 100644
index 0000000000000..627f13e82c164
--- /dev/null
+++ b/libclc/clc/lib/generic/common/clc_step.inc
@@ -0,0 +1,12 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_step(__CLC_GENTYPE edge,
+                                                __CLC_GENTYPE x) {
+  return x < edge ? __CLC_FP_LIT(0.0) : __CLC_FP_LIT(1.0);
+}

diff  --git a/libclc/opencl/include/clc/opencl/common/step.inc b/libclc/opencl/include/clc/opencl/common/step.inc
index bb95c62b1c2ea..86d4ed03dbcab 100644
--- a/libclc/opencl/include/clc/opencl/common/step.inc
+++ b/libclc/opencl/include/clc/opencl/common/step.inc
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 _CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(__CLC_GENTYPE edge, __CLC_GENTYPE x);
-_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(float edge, __CLC_GENTYPE x);
 
-#ifdef cl_khr_fp64
-_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(double edge, __CLC_GENTYPE x);
+#ifndef __CLC_SCALAR
+_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE step(__CLC_SCALAR_GENTYPE edge,
+                                           __CLC_GENTYPE x);
 #endif

diff  --git a/libclc/opencl/lib/generic/common/step.cl b/libclc/opencl/lib/generic/common/step.cl
index 99e69963c37ba..99a7af08c9264 100644
--- a/libclc/opencl/lib/generic/common/step.cl
+++ b/libclc/opencl/lib/generic/common/step.cl
@@ -6,36 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <clc/clcmacro.h>
+#include <clc/common/clc_step.h>
 #include <clc/opencl/clc.h>
 
-_CLC_OVERLOAD _CLC_DEF float step(float edge, float x) {
-  return x < edge ? 0.0f : 1.0f;
-}
-
-_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, float, float);
-
-_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, float, float);
-
-#ifdef cl_khr_fp64
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-#define STEP_DEF(edge_type, x_type)                                            \
-  _CLC_OVERLOAD _CLC_DEF x_type step(edge_type edge, x_type x) {               \
-    return x < edge ? 0.0 : 1.0;                                               \
-  }
-
-STEP_DEF(double, double);
-
-_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
-_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, double, double);
-
-#if !defined(CLC_SPIRV)
-STEP_DEF(float, double);
-STEP_DEF(double, float);
-
-_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, step, float, double);
-_CLC_V_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, step, double, float);
-#endif
-
-#endif
+#define __CLC_BODY <step.inc>
+#include <clc/math/gentype.inc>

diff  --git a/libclc/opencl/lib/generic/common/step.inc b/libclc/opencl/lib/generic/common/step.inc
new file mode 100644
index 0000000000000..b1e9411792157
--- /dev/null
+++ b/libclc/opencl/lib/generic/common/step.inc
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE step(__CLC_GENTYPE edge, __CLC_GENTYPE x) {
+  return __clc_step(edge, x);
+}
+
+#if !defined(__CLC_SCALAR)
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE step(__CLC_SCALAR_GENTYPE edge,
+                                          __CLC_GENTYPE x) {
+  return __clc_step((__CLC_GENTYPE)edge, x);
+}
+
+#endif


        


More information about the cfe-commits mailing list