From d32e71d7c732c37fd28241b85f501d7a192aa22c Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Thu, 27 Mar 2025 14:53:19 +0000 Subject: [PATCH] [libclc] Move fmod, remainder & remquo to the CLC library (#132054) These functions were already nominally in the CLC namespace; this commit just formally moves them over. Note that 'half' versions of these CLC functions are now provided. Previously the corresponding OpenCL builtins would forward directly to the 'float' versions of the CLC builtins. Now the OpenCL builtins call the 'half' CLC builtins, which themselves call the 'float' CLC versions. This keeps the interface between the OpenCL and CLC libraries neater and keeps the CLC library self-contained. No changes to the generated code for non-SPIR-V targets is observed. --- .../include/clc/math/binary_def_via_fp32.inc | 13 +++++++++++ .../include/clc}/math/clc_fmod.h | 10 +++++++- .../include/clc}/math/clc_remainder.h | 10 +++++++- .../include/clc}/math/clc_remquo.h | 10 ++++++-- libclc/clc/include/clc/math/gentype.inc | 7 ++++-- .../include/clc/math/remquo_decl.inc} | 3 ++- libclc/clc/lib/generic/SOURCES | 3 +++ .../lib => clc/lib/generic}/math/clc_fmod.cl | 22 ++++++++++++++---- .../lib/generic}/math/clc_remainder.cl | 23 +++++++++++++++---- .../lib/generic}/math/clc_remquo.cl | 9 +++++--- libclc/clspv/lib/SOURCES | 3 --- libclc/generic/include/clc/math/remquo.h | 6 ++--- libclc/generic/lib/SOURCES | 3 --- libclc/generic/lib/math/fmod.cl | 6 ++--- libclc/generic/lib/math/remainder.cl | 6 ++--- libclc/generic/lib/math/remquo.cl | 2 +- libclc/generic/lib/math/remquo.inc | 3 ++- libclc/spirv/lib/SOURCES | 3 --- 18 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 libclc/clc/include/clc/math/binary_def_via_fp32.inc rename libclc/{generic/include => clc/include/clc}/math/clc_fmod.h (73%) rename libclc/{generic/include => clc/include/clc}/math/clc_remainder.h (71%) rename libclc/{generic/include => clc/include/clc}/math/clc_remquo.h (75%) rename libclc/{generic/include/clc/math/remquo.inc => clc/include/clc/math/remquo_decl.inc} (74%) rename libclc/{generic/lib => clc/lib/generic}/math/clc_fmod.cl (91%) rename libclc/{generic/lib => clc/lib/generic}/math/clc_remainder.cl (92%) rename libclc/{generic/lib => clc/lib/generic}/math/clc_remquo.cl (98%) diff --git a/libclc/clc/include/clc/math/binary_def_via_fp32.inc b/libclc/clc/include/clc/math/binary_def_via_fp32.inc new file mode 100644 index 000000000000..0b4108adb061 --- /dev/null +++ b/libclc/clc/include/clc/math/binary_def_via_fp32.inc @@ -0,0 +1,13 @@ +//===----------------------------------------------------------------------===// +// +// 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_FUNCTION(__CLC_GENTYPE x, + __CLC_GENTYPE y) { + return __CLC_CONVERT_GENTYPE( + __CLC_FUNCTION(__CLC_CONVERT_FLOATN(x), __CLC_CONVERT_FLOATN(y))); +} diff --git a/libclc/generic/include/math/clc_fmod.h b/libclc/clc/include/clc/math/clc_fmod.h similarity index 73% rename from libclc/generic/include/math/clc_fmod.h rename to libclc/clc/include/clc/math/clc_fmod.h index c94428cf159b..59778fd0f612 100644 --- a/libclc/generic/include/math/clc_fmod.h +++ b/libclc/clc/include/clc/math/clc_fmod.h @@ -6,7 +6,15 @@ // //===----------------------------------------------------------------------===// +#ifndef __CLC_MATH_CLC_FMOD_H__ +#define __CLC_MATH_CLC_FMOD_H__ + #define __CLC_FUNCTION __clc_fmod -#define __CLC_BODY +#define __CLC_BODY + #include + +#undef __CLC_BODY #undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_FMOD_H__ diff --git a/libclc/generic/include/math/clc_remainder.h b/libclc/clc/include/clc/math/clc_remainder.h similarity index 71% rename from libclc/generic/include/math/clc_remainder.h rename to libclc/clc/include/clc/math/clc_remainder.h index e6c2c84a6dc9..2a1c88297d26 100644 --- a/libclc/generic/include/math/clc_remainder.h +++ b/libclc/clc/include/clc/math/clc_remainder.h @@ -6,7 +6,15 @@ // //===----------------------------------------------------------------------===// +#ifndef __CLC_MATH_CLC_REMAINDER_H__ +#define __CLC_MATH_CLC_REMAINDER_H__ + #define __CLC_FUNCTION __clc_remainder -#define __CLC_BODY +#define __CLC_BODY + #include + +#undef __CLC_BODY #undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_REMAINDER_H__ diff --git a/libclc/generic/include/math/clc_remquo.h b/libclc/clc/include/clc/math/clc_remquo.h similarity index 75% rename from libclc/generic/include/math/clc_remquo.h rename to libclc/clc/include/clc/math/clc_remquo.h index b64441fff1d8..6588342cf81b 100644 --- a/libclc/generic/include/math/clc_remquo.h +++ b/libclc/clc/include/clc/math/clc_remquo.h @@ -6,11 +6,17 @@ // //===----------------------------------------------------------------------===// +#ifndef __CLC_MATH_CLC_REMQUO_H__ +#define __CLC_MATH_CLC_REMQUO_H__ + #define __CLC_FUNCTION __clc_remquo -#define __CLC_BODY +#define __CLC_BODY #define __CLC_ADDRESS_SPACE private #include -#undef __CLC_ADDRESS_SPACE +#undef __CLC_ADDRESS_SPACE +#undef __CLC_BODY #undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_REMQUO_H__ diff --git a/libclc/clc/include/clc/math/gentype.inc b/libclc/clc/include/clc/math/gentype.inc index 31aa16401e64..f65ccc2d42f7 100644 --- a/libclc/clc/include/clc/math/gentype.inc +++ b/libclc/clc/include/clc/math/gentype.inc @@ -68,6 +68,7 @@ #define __CLC_CONVERT_S_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_S_GENTYPE) #define __CLC_CONVERT_U_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_U_GENTYPE) +#if (!defined(__HALF_ONLY) && !defined(__DOUBLE_ONLY)) #define __CLC_SCALAR_GENTYPE float #define __CLC_FPSIZE 32 #define __CLC_FP_LIT(x) x##F @@ -133,7 +134,9 @@ #undef __CLC_FPSIZE #undef __CLC_SCALAR_GENTYPE -#ifndef __FLOAT_ONLY +#endif + +#if (!defined(__HALF_ONLY) && !defined(__FLOAT_ONLY)) #ifdef cl_khr_fp64 #pragma OPENCL EXTENSION cl_khr_fp64 : enable @@ -204,7 +207,7 @@ #endif #endif -#ifndef __FLOAT_ONLY +#if (!defined(__FLOAT_ONLY) && !defined(__DOUBLE_ONLY)) #ifdef cl_khr_fp16 #pragma OPENCL EXTENSION cl_khr_fp16 : enable diff --git a/libclc/generic/include/clc/math/remquo.inc b/libclc/clc/include/clc/math/remquo_decl.inc similarity index 74% rename from libclc/generic/include/clc/math/remquo.inc rename to libclc/clc/include/clc/math/remquo_decl.inc index 15a67b92ddde..ecd703042a96 100644 --- a/libclc/generic/include/clc/math/remquo.inc +++ b/libclc/clc/include/clc/math/remquo_decl.inc @@ -6,4 +6,5 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q); +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION( + __CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q); diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index d851065bb2e2..39db96d758fc 100644 --- a/libclc/clc/lib/generic/SOURCES +++ b/libclc/clc/lib/generic/SOURCES @@ -33,6 +33,7 @@ math/clc_copysign.cl math/clc_ep_log.cl math/clc_fabs.cl math/clc_fma.cl +math/clc_fmod.cl math/clc_floor.cl math/clc_frexp.cl math/clc_hypot.cl @@ -45,6 +46,8 @@ math/clc_mad.cl math/clc_modf.cl math/clc_nan.cl math/clc_nextafter.cl +math/clc_remainder.cl +math/clc_remquo.cl math/clc_rint.cl math/clc_round.cl math/clc_rsqrt.cl diff --git a/libclc/generic/lib/math/clc_fmod.cl b/libclc/clc/lib/generic/math/clc_fmod.cl similarity index 91% rename from libclc/generic/lib/math/clc_fmod.cl rename to libclc/clc/lib/generic/math/clc_fmod.cl index f8cb09d1ee11..6af84a14f3d1 100644 --- a/libclc/generic/lib/math/clc_fmod.cl +++ b/libclc/clc/lib/generic/math/clc_fmod.cl @@ -6,17 +6,16 @@ // //===----------------------------------------------------------------------===// -#include #include #include #include +#include #include #include -#include +#include #include #include #include -#include _CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) { int ux = __clc_as_int(x); @@ -67,6 +66,9 @@ _CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) { _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_fmod, float, float); #ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) { ulong ux = __clc_as_ulong(x); ulong ax = ux & ~SIGNBIT_DP64; @@ -92,7 +94,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) { // but it doesn't matter - it just means that we'll go round // the loop below one extra time. int ntimes = __clc_max(0, (xexp1 - yexp1) / 53); - double w = ldexp(dy, ntimes * 53); + double w = __clc_ldexp(dy, ntimes * 53); w = ntimes == 0 ? dy : w; double scale = ntimes == 0 ? 1.0 : 0x1.0p-53; @@ -171,3 +173,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) { _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_fmod, double, double); #endif + +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +// Forward the half version of this builtin onto the float one +#define __HALF_ONLY +#define __CLC_FUNCTION __clc_fmod +#define __CLC_BODY +#include + +#endif diff --git a/libclc/generic/lib/math/clc_remainder.cl b/libclc/clc/lib/generic/math/clc_remainder.cl similarity index 92% rename from libclc/generic/lib/math/clc_remainder.cl rename to libclc/clc/lib/generic/math/clc_remainder.cl index e7ab4c653fd2..c79d271c624e 100644 --- a/libclc/generic/lib/math/clc_remainder.cl +++ b/libclc/clc/lib/generic/math/clc_remainder.cl @@ -6,17 +6,17 @@ // //===----------------------------------------------------------------------===// -#include #include #include #include +#include #include #include -#include +#include +#include #include #include #include -#include _CLC_DEF _CLC_OVERLOAD float __clc_remainder(float x, float y) { int ux = __clc_as_int(x); @@ -77,6 +77,9 @@ _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_remainder, float, float); #ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) { ulong ux = __clc_as_ulong(x); ulong ax = ux & ~SIGNBIT_DP64; @@ -104,7 +107,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) { // but it doesn't matter - it just means that we'll go round // the loop below one extra time. int ntimes = __clc_max(0, (xexp1 - yexp1) / 53); - double w = ldexp(dy, ntimes * 53); + double w = __clc_ldexp(dy, ntimes * 53); w = ntimes == 0 ? dy : w; double scale = ntimes == 0 ? 1.0 : 0x1.0p-53; @@ -207,3 +210,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) { _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_remainder, double, double); #endif + +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +// Forward the half version of this builtin onto the float one +#define __HALF_ONLY +#define __CLC_FUNCTION __clc_remainder +#define __CLC_BODY +#include + +#endif diff --git a/libclc/generic/lib/math/clc_remquo.cl b/libclc/clc/lib/generic/math/clc_remquo.cl similarity index 98% rename from libclc/generic/lib/math/clc_remquo.cl rename to libclc/clc/lib/generic/math/clc_remquo.cl index 61c7fae29bc0..9fa94c1c290b 100644 --- a/libclc/generic/lib/math/clc_remquo.cl +++ b/libclc/clc/lib/generic/math/clc_remquo.cl @@ -6,17 +6,17 @@ // //===----------------------------------------------------------------------===// -#include #include #include #include +#include #include #include +#include #include #include #include #include -#include _CLC_DEF _CLC_OVERLOAD float __clc_remquo(float x, float y, __private int *quo) { @@ -116,6 +116,9 @@ __VEC_REMQUO(float, 8, 4) __VEC_REMQUO(float, 16, 8) #ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, __private int *pquo) { ulong ux = __clc_as_ulong(x); @@ -144,7 +147,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, // but it doesn't matter - it just means that we'll go round // the loop below one extra time. int ntimes = __clc_max(0, (xexp1 - yexp1) / 53); - double w = ldexp(dy, ntimes * 53); + double w = __clc_ldexp(dy, ntimes * 53); w = ntimes == 0 ? dy : w; double scale = ntimes == 0 ? 1.0 : 0x1.0p-53; diff --git a/libclc/clspv/lib/SOURCES b/libclc/clspv/lib/SOURCES index fa9e68f6985e..4354cb6f4b32 100644 --- a/libclc/clspv/lib/SOURCES +++ b/libclc/clspv/lib/SOURCES @@ -17,12 +17,9 @@ subnormal_config.cl ../../generic/lib/math/atanpi.cl ../../generic/lib/math/cbrt.cl ../../generic/lib/math/clc_exp10.cl -../../generic/lib/math/clc_fmod.cl ../../generic/lib/math/clc_pow.cl ../../generic/lib/math/clc_pown.cl ../../generic/lib/math/clc_powr.cl -../../generic/lib/math/clc_remainder.cl -../../generic/lib/math/clc_remquo.cl ../../generic/lib/math/clc_rootn.cl ../../generic/lib/math/clc_tan.cl ../../generic/lib/math/clc_tanpi.cl diff --git a/libclc/generic/include/clc/math/remquo.h b/libclc/generic/include/clc/math/remquo.h index 69c95e59c8d7..e332b32886c2 100644 --- a/libclc/generic/include/clc/math/remquo.h +++ b/libclc/generic/include/clc/math/remquo.h @@ -8,17 +8,17 @@ #define __CLC_FUNCTION remquo -#define __CLC_BODY +#define __CLC_BODY #define __CLC_ADDRESS_SPACE global #include #undef __CLC_ADDRESS_SPACE -#define __CLC_BODY +#define __CLC_BODY #define __CLC_ADDRESS_SPACE local #include #undef __CLC_ADDRESS_SPACE -#define __CLC_BODY +#define __CLC_BODY #define __CLC_ADDRESS_SPACE private #include #undef __CLC_ADDRESS_SPACE diff --git a/libclc/generic/lib/SOURCES b/libclc/generic/lib/SOURCES index b17ac97af0c4..896c25b43078 100644 --- a/libclc/generic/lib/SOURCES +++ b/libclc/generic/lib/SOURCES @@ -108,7 +108,6 @@ math/floor.cl math/fma.cl math/fmax.cl math/fmin.cl -math/clc_fmod.cl math/fmod.cl math/fract.cl math/frexp.cl @@ -163,9 +162,7 @@ math/clc_pown.cl math/pown.cl math/clc_powr.cl math/powr.cl -math/clc_remainder.cl math/remainder.cl -math/clc_remquo.cl math/remquo.cl math/rint.cl math/clc_rootn.cl diff --git a/libclc/generic/lib/math/fmod.cl b/libclc/generic/lib/math/fmod.cl index 5bce88de407c..07f880486c0f 100644 --- a/libclc/generic/lib/math/fmod.cl +++ b/libclc/generic/lib/math/fmod.cl @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include -#include +#include -#define __CLC_FUNC fmod -#define __CLC_BODY +#define FUNCTION fmod +#define __CLC_BODY #include diff --git a/libclc/generic/lib/math/remainder.cl b/libclc/generic/lib/math/remainder.cl index 92a3f08caa21..b5f96c2cd603 100644 --- a/libclc/generic/lib/math/remainder.cl +++ b/libclc/generic/lib/math/remainder.cl @@ -7,8 +7,8 @@ //===----------------------------------------------------------------------===// #include -#include +#include -#define __CLC_FUNC remainder -#define __CLC_BODY +#define FUNCTION remainder +#define __CLC_BODY #include diff --git a/libclc/generic/lib/math/remquo.cl b/libclc/generic/lib/math/remquo.cl index 0ab9327f5a9a..d0c4b16ed7c3 100644 --- a/libclc/generic/lib/math/remquo.cl +++ b/libclc/generic/lib/math/remquo.cl @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// #include -#include +#include #define __CLC_BODY #define __CLC_ADDRESS_SPACE global diff --git a/libclc/generic/lib/math/remquo.inc b/libclc/generic/lib/math/remquo.inc index 498c94e98e71..0c71aa0be020 100644 --- a/libclc/generic/lib/math/remquo.inc +++ b/libclc/generic/lib/math/remquo.inc @@ -6,7 +6,8 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q) { +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y, + __CLC_ADDRESS_SPACE __CLC_INTN *q) { __CLC_INTN local_q; __CLC_GENTYPE ret = __clc_remquo(x, y, &local_q); *q = local_q; diff --git a/libclc/spirv/lib/SOURCES b/libclc/spirv/lib/SOURCES index e39c95a985ff..6be5107c5e73 100644 --- a/libclc/spirv/lib/SOURCES +++ b/libclc/spirv/lib/SOURCES @@ -41,7 +41,6 @@ subnormal_config.cl ../../generic/lib/math/clc_exp10.cl ../../generic/lib/math/exp10.cl math/fma.cl -../../generic/lib/math/clc_fmod.cl ../../generic/lib/math/fmod.cl ../../generic/lib/math/fract.cl ../../generic/lib/math/frexp.cl @@ -65,9 +64,7 @@ math/fma.cl ../../generic/lib/math/pown.cl ../../generic/lib/math/clc_powr.cl ../../generic/lib/math/powr.cl -../../generic/lib/math/clc_remainder.cl ../../generic/lib/math/remainder.cl -../../generic/lib/math/clc_remquo.cl ../../generic/lib/math/remquo.cl ../../generic/lib/math/clc_rootn.cl ../../generic/lib/math/rootn.cl