From 9fa81a486e317c7201318d710559093b3a5233bb Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Thu, 22 May 2025 09:54:27 +0100 Subject: [PATCH] [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. --- libclc/clc/include/clc/clcmacro.h | 33 ------------------ libclc/clc/include/clc/common/clc_step.h | 19 +++++++++++ libclc/clc/lib/generic/SOURCES | 1 + libclc/clc/lib/generic/common/clc_step.cl | 12 +++++++ libclc/clc/lib/generic/common/clc_step.inc | 12 +++++++ .../opencl/include/clc/opencl/common/step.inc | 6 ++-- libclc/opencl/lib/generic/common/step.cl | 34 ++----------------- libclc/opencl/lib/generic/common/step.inc | 20 +++++++++++ 8 files changed, 70 insertions(+), 67 deletions(-) create mode 100644 libclc/clc/include/clc/common/clc_step.h create mode 100644 libclc/clc/lib/generic/common/clc_step.cl create mode 100644 libclc/clc/lib/generic/common/clc_step.inc create mode 100644 libclc/opencl/lib/generic/common/step.inc diff --git a/libclc/clc/include/clc/clcmacro.h b/libclc/clc/include/clc/clcmacro.h index c9f70d2998d3..de7b977021f8 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 000000000000..6b093d06896c --- /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 + +#include + +#undef __CLC_FUNCTION + +#endif // __CLC_COMMON_CLC_STEP_H__ diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index 1cc8730d2ae8..a8a906159e28 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 000000000000..c21c27a3e940 --- /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 + +#define __CLC_BODY +#include 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 000000000000..627f13e82c16 --- /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 bb95c62b1c2e..86d4ed03dbca 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 99e69963c37b..99a7af08c926 100644 --- a/libclc/opencl/lib/generic/common/step.cl +++ b/libclc/opencl/lib/generic/common/step.cl @@ -6,36 +6,8 @@ // //===----------------------------------------------------------------------===// -#include +#include #include -_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 +#include diff --git a/libclc/opencl/lib/generic/common/step.inc b/libclc/opencl/lib/generic/common/step.inc new file mode 100644 index 000000000000..b1e941179215 --- /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