Skip to content

[libspirv] Reuse clc function in generic math/common built-ins #19231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libclc/clc/include/clc/math/unary_builtin_scalarize.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
#define __CLC_BUILTIN_H __CLC_BUILTIN_F
#endif

#if (!defined(__HALF_ONLY) && !defined(__DOUBLE_ONLY))
_CLC_DEFINE_UNARY_BUILTIN_SCALARIZE(float, __CLC_FUNCTION, __CLC_BUILTIN_F, float)
#endif

#ifndef __FLOAT_ONLY

#if (!defined(__HALF_ONLY) && !defined(__FLOAT_ONLY))
#ifdef cl_khr_fp64

#pragma OPENCL EXTENSION cl_khr_fp64 : enable

_CLC_DEFINE_UNARY_BUILTIN_SCALARIZE(double, __CLC_FUNCTION, __CLC_BUILTIN_D, double)

#endif
#endif // (!defined(__HALF_ONLY) && !defined(__FLOAT_ONLY))

#if (!defined(__FLOAT_ONLY) && !defined(__DOUBLE_ONLY))
#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable

_CLC_DEFINE_UNARY_BUILTIN_SCALARIZE(half, __CLC_FUNCTION, __CLC_BUILTIN_H, half)

#endif

#endif // !__FLOAT_ONLY
#endif // (!defined(__FLOAT_ONLY) && !defined(__DOUBLE_ONLY))
6 changes: 6 additions & 0 deletions libclc/clc/lib/amdgcn/SOURCES
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
math/clc_exp.cl
math/clc_fmax.cl
math/clc_fmin.cl
math/clc_ldexp_override.cl
math/clc_lgamma.cl
math/clc_log.cl
math/clc_sinpi.cl
math/clc_sqrt.cl
math/clc_sqrt_fp64.cl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_exp.h>

#define __CLC_FUNCTION __spirv_ocl_exp
#define __CLC_FUNCTION __clc_exp
#define __CLC_BUILTIN __ocml_exp

float __ocml_exp_f32(float);
Expand Down
45 changes: 13 additions & 32 deletions libclc/clc/lib/amdgcn/math/clc_fmax.cl
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,25 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <clc/internal/clc.h>
#include <clc/relational/clc_isnan.h>
#include <clc/math/clc_fmax.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)
#define __CLC_FUNCTION __clc_fmax
#define __CLC_BUILTIN __ocml_fmax

#ifdef cl_khr_fp64
float __ocml_fmax_f32(float, float);
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, _f32)

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
double __ocml_fmax_f64(double, double);
#define __CLC_BUILTIN_D __CLC_XCONCAT(__CLC_BUILTIN, _f64)
#endif // cl_khr_fp64

_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
half __ocml_fmax_f16(half, half);
#define __CLC_BUILTIN_H __CLC_XCONCAT(__CLC_BUILTIN, _f16)
#endif // cl_khr_fp16

_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
#include <clc/math/binary_builtin.inc>
46 changes: 13 additions & 33 deletions libclc/clc/lib/amdgcn/math/clc_fmin.cl
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,25 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <clc/internal/clc.h>
#include <clc/relational/clc_isnan.h>
#include <clc/math/clc_fmin.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)
#define __CLC_FUNCTION __clc_fmin
#define __CLC_BUILTIN __ocml_fmin

#ifdef cl_khr_fp64
float __ocml_fmin_f32(float, float);
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, _f32)

#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
double __ocml_fmin_f64(double, double);
#define __CLC_BUILTIN_D __CLC_XCONCAT(__CLC_BUILTIN, _f64)
#endif // cl_khr_fp64

#ifdef cl_khr_fp16

#pragma OPENCL EXTENSION cl_khr_fp16 : enable
half __ocml_fmin_f16(half, half);
#define __CLC_BUILTIN_H __CLC_XCONCAT(__CLC_BUILTIN, _f16)
#endif // cl_khr_fp16

_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
#include <clc/math/binary_builtin.inc>
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_lgamma.h>

#define __CLC_FUNCTION __spirv_ocl_lgamma
#define __CLC_FUNCTION __clc_lgamma
#define __CLC_BUILTIN __ocml_lgamma

float __ocml_lgamma_f32(float);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_log.h>

#define __CLC_FUNCTION __spirv_ocl_log
#define __CLC_FUNCTION __clc_log
#define __CLC_BUILTIN __ocml_log

float __ocml_log_f32(float);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_sinpi.h>

#define __CLC_FUNCTION __spirv_ocl_sinpi
#define __CLC_FUNCTION __clc_sinpi
#define __CLC_BUILTIN __ocml_sinpi

float __ocml_sinpi_f32(float);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
//
//===----------------------------------------------------------------------===//

#include <clc/clcmacro.h>
#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_sqrt.h>

#define __CLC_FUNCTION __spirv_ocl_sqrt
#define __CLC_FUNCTION __clc_sqrt
#define __CLC_BUILTIN __ocml_sqrt

float __ocml_sqrt_f32(float);
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, _f32)

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
double __ocml_sqrt_f64(double);
#define __CLC_BUILTIN_D __CLC_XCONCAT(__CLC_BUILTIN, _f64)
#endif // cl_khr_fp64
#define __FLOAT_ONLY
#include <clc/math/unary_builtin_scalarize.inc>

#undef __FLOAT_ONLY
#undef __CLC_BUILTIN_H

#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
half __ocml_sqrt_f16(half);
#define __CLC_BUILTIN_H __CLC_XCONCAT(__CLC_BUILTIN, _f16)
#endif // cl_khr_fp16

#define __HALF_ONLY
#include <clc/math/unary_builtin_scalarize.inc>
22 changes: 22 additions & 0 deletions libclc/clc/lib/amdgcn/math/clc_sqrt_fp64.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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/internal/clc.h>
#include <clc/math/clc_sqrt.h>

#define __CLC_FUNCTION __clc_sqrt
#define __CLC_BUILTIN __ocml_sqrt

#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
double __ocml_sqrt_f64(double);
#define __CLC_BUILTIN_D __CLC_XCONCAT(__CLC_BUILTIN, _f64)
#endif // cl_khr_fp64

#define __DOUBLE_ONLY
#include <clc/math/unary_builtin_scalarize.inc>
3 changes: 3 additions & 0 deletions libclc/clc/lib/ptx-nvidiacl/SOURCES
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
math/clc_log.cl
math/clc_sinpi.cl
math/clc_sqrt.cl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_log.h>

#include <libspirv/ptx-nvidiacl/libdevice.h>
#include <clc/clcmacro.h>
float __nv_logf(float);
double __nv_log(double);

#define __CLC_FUNCTION __spirv_ocl_log
#define __CLC_FUNCTION __clc_log
#define __CLC_BUILTIN __nv_log
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, f)
#include <clc/math/unary_builtin_scalarize.inc>
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_sinpi.h>

#include <libspirv/ptx-nvidiacl/libdevice.h>
#include <clc/clcmacro.h>
float __nv_sinpif(float);
double __nv_sinpi(double);

#define __CLC_FUNCTION __spirv_ocl_sinpi
#define __CLC_FUNCTION __clc_sinpi
#define __CLC_BUILTIN __nv_sinpi
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, f)
#include <clc/math/unary_builtin_scalarize.inc>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//
//===----------------------------------------------------------------------===//

#include <libspirv/spirv.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_sqrt.h>

#include <libspirv/ptx-nvidiacl/libdevice.h>
float __nv_sqrtf(float);
double __nv_sqrt(double);

#define __CLC_FUNCTION __spirv_ocl_sqrt
#define __CLC_FUNCTION __clc_sqrt
#define __CLC_BUILTIN __nv_sqrt
#define __CLC_BUILTIN_F __CLC_XCONCAT(__CLC_BUILTIN, f)
#include <clc/math/unary_builtin_scalarize.inc>
7 changes: 0 additions & 7 deletions libclc/libspirv/lib/amdgcn-amdhsa/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,15 @@ math/cosh.cl
math/cospi.cl
math/erf.cl
math/erfc.cl
math/exp.cl
math/exp10.cl
math/exp2.cl
math/expm1.cl
math/fdim.cl
math/fmax.cl
math/fmin.cl
math/fmod.cl
math/frexp.cl
math/hypot.cl
math/ilogb.cl
math/ldexp.cl
math/lgamma.cl
math/log.cl
math/log2.cl
math/log10.cl
math/log1p.cl
Expand All @@ -60,8 +55,6 @@ math/rsqrt.cl
math/sin.cl
math/sincos.cl
math/sinh.cl
math/sinpi.cl
math/sqrt.cl
math/tan.cl
math/tanh.cl
workitem/get_global_size.cl
Expand Down
30 changes: 0 additions & 30 deletions libclc/libspirv/lib/amdgcn-amdhsa/math/fmax.cl

This file was deleted.

Loading