https://github.com/frasercrmck created https://github.com/llvm/llvm-project/pull/134218
This is an alternative to #128506 which doesn't attempt to change the codegen for fmin and fmax on their way to the CLC library. The amdgcn and r600 custom definitions of fmin/fmax are now converted to custom definitions of __clc_fmin and __clc_fmax. The only codegen change is that non-standard vector/scalar overloads of fmin/fmax have been removed. We were currently (accidentally, presumably) providing overloads with mixed elment types such as fmin(double2, float), fmax(half4, double), etc. The only vector/scalar overloads in the OpenCL spec are those with scalars of the same element type as the vector in the first argument. >From 583261a0d1fc57ddb8ae964cbaf4f044c39d9bf2 Mon Sep 17 00:00:00 2001 From: Fraser Cormack <fra...@codeplay.com> Date: Thu, 3 Apr 2025 09:29:28 +0100 Subject: [PATCH] [libclc] Move fmin & fmax to CLC library This is an alternative to #128506 which doesn't attempt to change the codegen for fmin and fmax on their way to the CLC library. The amdgcn and r600 custom definitions of fmin/fmax are now converted to custom definitions of __clc_fmin and __clc_fmax. The only codegen change is that non-standard vector/scalar overloads of fmin/fmax have been removed. We were currently (accidentally, presumably) providing overloads with mixed elment types such as fmin(double2, float), fmax(half4, double), etc. The only vector/scalar overloads in the OpenCL spec are those with scalars of the same element type as the vector in the first argument. --- libclc/amdgcn/lib/SOURCES | 2 - libclc/amdgcn/lib/math/fmax.cl | 53 ------------------- libclc/amdgcn/lib/math/fmin.cl | 53 ------------------- libclc/clc/include/clc/math/clc_fmax.h | 20 +++++++ libclc/clc/include/clc/math/clc_fmin.h | 20 +++++++ .../binary_decl_with_scalar_second_arg.inc | 15 ++++++ .../binary_def_with_scalar_second_arg.inc | 25 +++++++++ libclc/clc/lib/amdgcn/SOURCES | 2 + libclc/clc/lib/amdgcn/math/clc_fmax.cl | 49 +++++++++++++++++ libclc/clc/lib/amdgcn/math/clc_fmin.cl | 50 +++++++++++++++++ libclc/clc/lib/generic/SOURCES | 4 +- libclc/clc/lib/generic/math/clc_fmax.cl | 36 +++++++++++++ libclc/clc/lib/generic/math/clc_fmin.cl | 36 +++++++++++++ libclc/clc/lib/r600/SOURCES | 2 + libclc/clc/lib/r600/math/clc_fmax.cl | 32 +++++++++++ libclc/clc/lib/r600/math/clc_fmin.cl | 33 ++++++++++++ libclc/generic/lib/math/fmax.cl | 31 ++--------- libclc/generic/lib/math/fmin.cl | 30 ++--------- libclc/r600/lib/SOURCES | 2 - libclc/r600/lib/math/fmax.cl | 36 ------------- libclc/r600/lib/math/fmin.cl | 37 ------------- 21 files changed, 329 insertions(+), 239 deletions(-) delete mode 100644 libclc/amdgcn/lib/math/fmax.cl delete mode 100644 libclc/amdgcn/lib/math/fmin.cl create mode 100644 libclc/clc/include/clc/math/clc_fmax.h create mode 100644 libclc/clc/include/clc/math/clc_fmin.h create mode 100644 libclc/clc/include/clc/shared/binary_decl_with_scalar_second_arg.inc create mode 100644 libclc/clc/include/clc/shared/binary_def_with_scalar_second_arg.inc create mode 100644 libclc/clc/lib/amdgcn/math/clc_fmax.cl create mode 100644 libclc/clc/lib/amdgcn/math/clc_fmin.cl create mode 100644 libclc/clc/lib/generic/math/clc_fmax.cl create mode 100644 libclc/clc/lib/generic/math/clc_fmin.cl create mode 100644 libclc/clc/lib/r600/math/clc_fmax.cl create mode 100644 libclc/clc/lib/r600/math/clc_fmin.cl delete mode 100644 libclc/r600/lib/math/fmax.cl delete mode 100644 libclc/r600/lib/math/fmin.cl diff --git a/libclc/amdgcn/lib/SOURCES b/libclc/amdgcn/lib/SOURCES index 6c6e77db0d84b..213f62cc73a74 100644 --- a/libclc/amdgcn/lib/SOURCES +++ b/libclc/amdgcn/lib/SOURCES @@ -1,6 +1,4 @@ cl_khr_int64_extended_atomics/minmax_helpers.ll -math/fmax.cl -math/fmin.cl mem_fence/fence.cl synchronization/barrier.cl workitem/get_global_offset.cl diff --git a/libclc/amdgcn/lib/math/fmax.cl b/libclc/amdgcn/lib/math/fmax.cl deleted file mode 100644 index 8d3bf0495390f..0000000000000 --- a/libclc/amdgcn/lib/math/fmax.cl +++ /dev/null @@ -1,53 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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/clc.h> -#include <clc/clcmacro.h> - -_CLC_DEF _CLC_OVERLOAD float fmax(float x, float y) -{ - /* fcanonicalize removes sNaNs and flushes denormals if not enabled. - * Otherwise fmax instruction flushes the values for comparison, - * but outputs original denormal */ - x = __builtin_canonicalizef(x); - y = __builtin_canonicalizef(y); - return __builtin_fmaxf(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, fmax, float, float) - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEF _CLC_OVERLOAD double fmax(double x, double y) -{ - x = __builtin_canonicalize(x); - y = __builtin_canonicalize(y); - return __builtin_fmax(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, fmax, double, double) - -#endif -#ifdef cl_khr_fp16 - -#pragma OPENCL EXTENSION cl_khr_fp16 : enable - -_CLC_DEF _CLC_OVERLOAD half fmax(half x, half y) -{ - if (isnan(x)) - return y; - if (isnan(y)) - return x; - return (y < x) ? x : y; -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, fmax, half, half) - -#endif - -#define __CLC_BODY <../../../generic/lib/math/fmax.inc> -#include <clc/math/gentype.inc> diff --git a/libclc/amdgcn/lib/math/fmin.cl b/libclc/amdgcn/lib/math/fmin.cl deleted file mode 100644 index 689401323105d..0000000000000 --- a/libclc/amdgcn/lib/math/fmin.cl +++ /dev/null @@ -1,53 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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/clc.h> -#include <clc/clcmacro.h> - -_CLC_DEF _CLC_OVERLOAD float fmin(float x, float y) -{ - /* fcanonicalize removes sNaNs and flushes denormals if not enabled. - * Otherwise fmin instruction flushes the values for comparison, - * but outputs original denormal */ - x = __builtin_canonicalizef(x); - y = __builtin_canonicalizef(y); - return __builtin_fminf(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, fmin, float, float) - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEF _CLC_OVERLOAD double fmin(double x, double y) -{ - x = __builtin_canonicalize(x); - y = __builtin_canonicalize(y); - return __builtin_fmin(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, fmin, double, double) - -#endif -#ifdef cl_khr_fp16 - -#pragma OPENCL EXTENSION cl_khr_fp16 : enable - -_CLC_DEF _CLC_OVERLOAD half fmin(half x, half y) -{ - if (isnan(x)) - return y; - if (isnan(y)) - return x; - return (y < x) ? y : x; -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, fmin, half, half) - -#endif - -#define __CLC_BODY <../../../generic/lib/math/fmin.inc> -#include <clc/math/gentype.inc> diff --git a/libclc/clc/include/clc/math/clc_fmax.h b/libclc/clc/include/clc/math/clc_fmax.h new file mode 100644 index 0000000000000..2f9e6ea1c7e56 --- /dev/null +++ b/libclc/clc/include/clc/math/clc_fmax.h @@ -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 +// +//===----------------------------------------------------------------------===// + +#ifndef __CLC_MATH_CLC_FMAX_H__ +#define __CLC_MATH_CLC_FMAX_H__ + +#define __CLC_FUNCTION __clc_fmax +#define __CLC_BODY <clc/shared/binary_decl_with_scalar_second_arg.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_BODY +#undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_FMAX_H__ diff --git a/libclc/clc/include/clc/math/clc_fmin.h b/libclc/clc/include/clc/math/clc_fmin.h new file mode 100644 index 0000000000000..19add2938a9b6 --- /dev/null +++ b/libclc/clc/include/clc/math/clc_fmin.h @@ -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 +// +//===----------------------------------------------------------------------===// + +#ifndef __CLC_MATH_CLC_FMIN_H__ +#define __CLC_MATH_CLC_FMIN_H__ + +#define __CLC_FUNCTION __clc_fmin +#define __CLC_BODY <clc/shared/binary_decl_with_scalar_second_arg.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_BODY +#undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_FMIN_H__ diff --git a/libclc/clc/include/clc/shared/binary_decl_with_scalar_second_arg.inc b/libclc/clc/include/clc/shared/binary_decl_with_scalar_second_arg.inc new file mode 100644 index 0000000000000..6044902a5be7c --- /dev/null +++ b/libclc/clc/include/clc/shared/binary_decl_with_scalar_second_arg.inc @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// 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_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, + __CLC_GENTYPE y); + +#ifndef __CLC_SCALAR +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, + __CLC_SCALAR_GENTYPE y); +#endif diff --git a/libclc/clc/include/clc/shared/binary_def_with_scalar_second_arg.inc b/libclc/clc/include/clc/shared/binary_def_with_scalar_second_arg.inc new file mode 100644 index 0000000000000..49fa7d2ce36e9 --- /dev/null +++ b/libclc/clc/include/clc/shared/binary_def_with_scalar_second_arg.inc @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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/utils.h> + +#ifndef __CLC_FUNCTION +#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x) +#endif + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE a, + __CLC_GENTYPE b) { + return __CLC_FUNCTION(FUNCTION)(a, b); +} + +#ifndef __CLC_SCALAR +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE a, + __CLC_SCALAR_GENTYPE b) { + return __CLC_FUNCTION(FUNCTION)(a, (__CLC_GENTYPE)b); +} +#endif diff --git a/libclc/clc/lib/amdgcn/SOURCES b/libclc/clc/lib/amdgcn/SOURCES index b85f80d3b29bb..3a48049271aff 100644 --- a/libclc/clc/lib/amdgcn/SOURCES +++ b/libclc/clc/lib/amdgcn/SOURCES @@ -1 +1,3 @@ +math/clc_fmax.cl +math/clc_fmin.cl math/clc_ldexp_override.cl diff --git a/libclc/clc/lib/amdgcn/math/clc_fmax.cl b/libclc/clc/lib/amdgcn/math/clc_fmax.cl new file mode 100644 index 0000000000000..b9d7bcef32b1b --- /dev/null +++ b/libclc/clc/lib/amdgcn/math/clc_fmax.cl @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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> +#include <clc/internal/clc.h> +#include <clc/relational/clc_isnan.h> + +_CLC_DEF _CLC_OVERLOAD float __clc_fmax(float x, float y) { + /* fcanonicalize removes sNaNs and flushes denormals if not enabled. + * Otherwise fmax instruction flushes the values for comparison, + * but outputs original denormal */ + x = __builtin_canonicalizef(x); + y = __builtin_canonicalizef(y); + return __builtin_fmaxf(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmax, float, float) + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEF _CLC_OVERLOAD double __clc_fmax(double x, double y) { + x = __builtin_canonicalize(x); + y = __builtin_canonicalize(y); + return __builtin_fmax(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmax, double, + double) + +#endif +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +_CLC_DEF _CLC_OVERLOAD half __clc_fmax(half x, half y) { + if (__clc_isnan(x)) + return y; + if (__clc_isnan(y)) + return x; + return (y < x) ? x : y; +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmax, half, half) + +#endif diff --git a/libclc/clc/lib/amdgcn/math/clc_fmin.cl b/libclc/clc/lib/amdgcn/math/clc_fmin.cl new file mode 100644 index 0000000000000..8b77fc213b494 --- /dev/null +++ b/libclc/clc/lib/amdgcn/math/clc_fmin.cl @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// 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> +#include <clc/internal/clc.h> +#include <clc/relational/clc_isnan.h> + +_CLC_DEF _CLC_OVERLOAD float __clc_fmin(float x, float y) { + /* fcanonicalize removes sNaNs and flushes denormals if not enabled. + * Otherwise fmin instruction flushes the values for comparison, + * but outputs original denormal */ + x = __builtin_canonicalizef(x); + y = __builtin_canonicalizef(y); + return __builtin_fminf(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmin, float, float) + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEF _CLC_OVERLOAD double __clc_fmin(double x, double y) { + x = __builtin_canonicalize(x); + y = __builtin_canonicalize(y); + return __builtin_fmin(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmin, double, + double) + +#endif + +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +_CLC_DEF _CLC_OVERLOAD half __clc_fmin(half x, half y) { + if (__clc_isnan(x)) + return y; + if (__clc_isnan(y)) + return x; + return (y < x) ? y : x; +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmin, half, half) + +#endif diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index 2e9878e6eaa4e..0c45ccb590eff 100644 --- a/libclc/clc/lib/generic/SOURCES +++ b/libclc/clc/lib/generic/SOURCES @@ -41,8 +41,10 @@ math/clc_expm1.cl math/clc_exp_helper.cl math/clc_fabs.cl math/clc_fma.cl -math/clc_fmod.cl +math/clc_fmax.cl +math/clc_fmin.cl math/clc_floor.cl +math/clc_fmod.cl math/clc_frexp.cl math/clc_hypot.cl math/clc_ldexp.cl diff --git a/libclc/clc/lib/generic/math/clc_fmax.cl b/libclc/clc/lib/generic/math/clc_fmax.cl new file mode 100644 index 0000000000000..8ee369f57d38b --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_fmax.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 <clc/clcmacro.h> +#include <clc/internal/clc.h> +#include <clc/relational/clc_isnan.h> + +_CLC_DEFINE_BINARY_BUILTIN(float, __clc_fmax, __builtin_fmaxf, float, float); + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEFINE_BINARY_BUILTIN(double, __clc_fmax, __builtin_fmax, double, double); + +#endif + +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +_CLC_DEF _CLC_OVERLOAD half __clc_fmax(half x, half y) { + if (__clc_isnan(x)) + return y; + if (__clc_isnan(y)) + return x; + return (x < y) ? y : x; +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmax, half, half) + +#endif diff --git a/libclc/clc/lib/generic/math/clc_fmin.cl b/libclc/clc/lib/generic/math/clc_fmin.cl new file mode 100644 index 0000000000000..2f307274b9be5 --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_fmin.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 <clc/clcmacro.h> +#include <clc/internal/clc.h> +#include <clc/relational/clc_isnan.h> + +_CLC_DEFINE_BINARY_BUILTIN(float, __clc_fmin, __builtin_fminf, float, float); + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEFINE_BINARY_BUILTIN(double, __clc_fmin, __builtin_fmin, double, double); + +#endif + +#ifdef cl_khr_fp16 + +#pragma OPENCL EXTENSION cl_khr_fp16 : enable + +_CLC_DEF _CLC_OVERLOAD half __clc_fmin(half x, half y) { + if (__clc_isnan(x)) + return y; + if (__clc_isnan(y)) + return x; + return (y < x) ? y : x; +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_fmin, half, half) + +#endif diff --git a/libclc/clc/lib/r600/SOURCES b/libclc/clc/lib/r600/SOURCES index 8d5caf167aa4e..75d32f4f535bc 100644 --- a/libclc/clc/lib/r600/SOURCES +++ b/libclc/clc/lib/r600/SOURCES @@ -1,2 +1,4 @@ +math/clc_fmax.cl +math/clc_fmin.cl math/clc_native_rsqrt.cl math/clc_rsqrt_override.cl diff --git a/libclc/clc/lib/r600/math/clc_fmax.cl b/libclc/clc/lib/r600/math/clc_fmax.cl new file mode 100644 index 0000000000000..7e9c6df789eda --- /dev/null +++ b/libclc/clc/lib/r600/math/clc_fmax.cl @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// 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> +#include <clc/internal/clc.h> +#include <clc/math/math.h> + +_CLC_DEF _CLC_OVERLOAD float __clc_fmax(float x, float y) { + // Flush denormals if not enabled. Otherwise fmax instruction flushes the + // values for comparison, but outputs original denormal + x = __clc_flush_denormal_if_not_supported(x); + y = __clc_flush_denormal_if_not_supported(y); + return __builtin_fmaxf(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmax, float, float) + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEF _CLC_OVERLOAD double __clc_fmax(double x, double y) { + return __builtin_fmax(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmax, double, + double) + +#endif diff --git a/libclc/clc/lib/r600/math/clc_fmin.cl b/libclc/clc/lib/r600/math/clc_fmin.cl new file mode 100644 index 0000000000000..cbcf849f7b7e8 --- /dev/null +++ b/libclc/clc/lib/r600/math/clc_fmin.cl @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// 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> +#include <clc/internal/clc.h> +#include <clc/math/math.h> + +_CLC_DEF _CLC_OVERLOAD float __clc_fmin(float x, float y) { + // fcanonicalize removes sNaNs and flushes denormals if not enabled. Otherwise + // fmin instruction flushes the values for comparison, but outputs original + // denormal + x = __clc_flush_denormal_if_not_supported(x); + y = __clc_flush_denormal_if_not_supported(y); + return __builtin_fminf(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_fmin, float, float) + +#ifdef cl_khr_fp64 + +#pragma OPENCL EXTENSION cl_khr_fp64 : enable + +_CLC_DEF _CLC_OVERLOAD double __clc_fmin(double x, double y) { + return __builtin_fmin(x, y); +} +_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_fmin, double, + double) + +#endif diff --git a/libclc/generic/lib/math/fmax.cl b/libclc/generic/lib/math/fmax.cl index 1a4afcb1c98cb..6ad5901eab739 100644 --- a/libclc/generic/lib/math/fmax.cl +++ b/libclc/generic/lib/math/fmax.cl @@ -7,33 +7,8 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> -#include <clc/clcmacro.h> +#include <clc/math/clc_fmax.h> -_CLC_DEFINE_BINARY_BUILTIN(float, fmax, __builtin_fmaxf, float, float); - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEFINE_BINARY_BUILTIN(double, fmax, __builtin_fmax, double, double); - -#endif - -#ifdef cl_khr_fp16 - -#pragma OPENCL EXTENSION cl_khr_fp16 : enable - -_CLC_DEF _CLC_OVERLOAD half fmax(half x, half y) -{ - if (isnan(x)) - return y; - if (isnan(y)) - return x; - return (x < y) ? y : x; -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, fmax, half, half) - -#endif - -#define __CLC_BODY <fmax.inc> +#define FUNCTION fmax +#define __CLC_BODY <clc/shared/binary_def_with_scalar_second_arg.inc> #include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/math/fmin.cl b/libclc/generic/lib/math/fmin.cl index c0d3dbf820dcd..bb42a2a6d647d 100644 --- a/libclc/generic/lib/math/fmin.cl +++ b/libclc/generic/lib/math/fmin.cl @@ -7,32 +7,8 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> -#include <clc/clcmacro.h> +#include <clc/math/clc_fmin.h> -_CLC_DEFINE_BINARY_BUILTIN(float, fmin, __builtin_fminf, float, float); - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEFINE_BINARY_BUILTIN(double, fmin, __builtin_fmin, double, double); - -#endif -#ifdef cl_khr_fp16 - -#pragma OPENCL EXTENSION cl_khr_fp16 : enable - -_CLC_DEF _CLC_OVERLOAD half fmin(half x, half y) -{ - if (isnan(x)) - return y; - if (isnan(y)) - return x; - return (y < x) ? y : x; -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, fmin, half, half) - -#endif - -#define __CLC_BODY <fmin.inc> +#define FUNCTION fmin +#define __CLC_BODY <clc/shared/binary_def_with_scalar_second_arg.inc> #include <clc/math/gentype.inc> diff --git a/libclc/r600/lib/SOURCES b/libclc/r600/lib/SOURCES index 4342ac38201c1..c4561274d8b2d 100644 --- a/libclc/r600/lib/SOURCES +++ b/libclc/r600/lib/SOURCES @@ -1,5 +1,3 @@ -math/fmax.cl -math/fmin.cl synchronization/barrier.cl workitem/get_global_offset.cl workitem/get_group_id.cl diff --git a/libclc/r600/lib/math/fmax.cl b/libclc/r600/lib/math/fmax.cl deleted file mode 100644 index 68c0925900e8a..0000000000000 --- a/libclc/r600/lib/math/fmax.cl +++ /dev/null @@ -1,36 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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/clc.h> -#include <clc/clcmacro.h> -#include <clc/math/math.h> - -_CLC_DEF _CLC_OVERLOAD float fmax(float x, float y) -{ - /* Flush denormals if not enabled. Otherwise fmax instruction flushes - * the values for comparison, but outputs original denormal */ - x = __clc_flush_denormal_if_not_supported(x); - y = __clc_flush_denormal_if_not_supported(y); - return __builtin_fmaxf(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, fmax, float, float) - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEF _CLC_OVERLOAD double fmax(double x, double y) -{ - return __builtin_fmax(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, fmax, double, double) - -#endif - -#define __CLC_BODY <../../../generic/lib/math/fmax.inc> -#include <clc/math/gentype.inc> diff --git a/libclc/r600/lib/math/fmin.cl b/libclc/r600/lib/math/fmin.cl deleted file mode 100644 index 343bce143380f..0000000000000 --- a/libclc/r600/lib/math/fmin.cl +++ /dev/null @@ -1,37 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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/clc.h> -#include <clc/clcmacro.h> -#include <clc/math/math.h> - -_CLC_DEF _CLC_OVERLOAD float fmin(float x, float y) -{ - /* fcanonicalize removes sNaNs and flushes denormals if not enabled. - * Otherwise fmin instruction flushes the values for comparison, - * but outputs original denormal */ - x = __clc_flush_denormal_if_not_supported(x); - y = __clc_flush_denormal_if_not_supported(y); - return __builtin_fminf(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, fmin, float, float) - -#ifdef cl_khr_fp64 - -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_DEF _CLC_OVERLOAD double fmin(double x, double y) -{ - return __builtin_fmin(x, y); -} -_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, fmin, double, double) - -#endif - -#define __CLC_BODY <../../../generic/lib/math/fmin.inc> -#include <clc/math/gentype.inc> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits