Skip to content

[libclc] Move min/max/clamp into the CLC builtins library #114386

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

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
15 changes: 12 additions & 3 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,30 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
message( STATUS " device: ${d} ( ${${d}_aliases} )" )

if ( ARCH STREQUAL spirv OR ARCH STREQUAL spirv64 )
set( build_flags -O0 -finline-hint-functions )
set( build_flags -O0 -finline-hint-functions -DCLC_SPIRV )
set( opt_flags )
set( spvflags --spirv-max-version=1.1 )
set( MACRO_ARCH SPIRV32 )
if( ARCH STREQUAL spirv64 )
set( MACRO_ARCH SPIRV64 )
endif()
elseif( ARCH STREQUAL clspv OR ARCH STREQUAL clspv64 )
set( build_flags "-Wno-unknown-assumption")
set( build_flags "-Wno-unknown-assumption" -DCLC_CLSPV )
set( opt_flags -O3 )
set( MACRO_ARCH CLSPV32 )
if( ARCH STREQUAL clspv64 )
set( MACRO_ARCH CLSPV64 )
endif()
else()
set( build_flags )
set( opt_flags -O3 )
set( MACRO_ARCH ${ARCH} )
endif()

set( LIBCLC_ARCH_OBJFILE_DIR "${LIBCLC_OBJFILE_DIR}/${arch_suffix}" )
file( MAKE_DIRECTORY ${LIBCLC_ARCH_OBJFILE_DIR} )

string( TOUPPER "CLC_${ARCH}" CLC_TARGET_DEFINE )
string( TOUPPER "CLC_${MACRO_ARCH}" CLC_TARGET_DEFINE )

list( APPEND build_flags
-D__CLC_INTERNAL
Expand Down
4 changes: 2 additions & 2 deletions libclc/clc/include/clc/clcfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

// avoid inlines for SPIR-V related targets since we'll optimise later in the
// chain
#if defined(CLC_SPIRV) || defined(CLC_SPIRV64)
#if defined(CLC_SPIRV)
#define _CLC_DEF
#elif defined(CLC_CLSPV) || defined(CLC_CLSPV64)
#elif defined(CLC_CLSPV)
#define _CLC_DEF __attribute__((noinline)) __attribute__((clspv_libclc_builtin))
#else
#define _CLC_DEF __attribute__((always_inline))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//These 2 defines only change when switching between data sizes or base types to
//keep this file manageable.
// These 2 defines only change when switching between data sizes or base types
// to keep this file manageable.
#define __CLC_GENSIZE 8
#define __CLC_SCALAR_GENTYPE char

Expand Down
15 changes: 15 additions & 0 deletions libclc/clc/include/clc/shared/clc_clamp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible clamp
#define __clc_clamp clamp
#else

#include <clc/clcfunc.h>
#include <clc/clctypes.h>

#define __CLC_BODY <clc/shared/clc_clamp.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc/shared/clc_clamp.inc>
#include <clc/math/gentype.inc>

#endif
9 changes: 9 additions & 0 deletions libclc/clc/include/clc/shared/clc_clamp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
__CLC_GENTYPE y,
__CLC_GENTYPE z);

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
__CLC_SCALAR_GENTYPE y,
__CLC_SCALAR_GENTYPE z);
#endif
12 changes: 12 additions & 0 deletions libclc/clc/include/clc/shared/clc_max.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible max
#define __clc_max max
#else

#define __CLC_BODY <clc/shared/clc_max.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc/shared/clc_max.inc>
#include <clc/math/gentype.inc>

#endif
7 changes: 7 additions & 0 deletions libclc/clc/include/clc/shared/clc_max.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
__CLC_GENTYPE b);

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b);
#endif
12 changes: 12 additions & 0 deletions libclc/clc/include/clc/shared/clc_min.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
// clspv and spir-v targets provide their own OpenCL-compatible min
#define __clc_min min
#else

#define __CLC_BODY <clc/shared/clc_min.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc/shared/clc_min.inc>
#include <clc/math/gentype.inc>

#endif
7 changes: 7 additions & 0 deletions libclc/clc/include/clc/shared/clc_min.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
__CLC_GENTYPE b);

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b);
#endif
3 changes: 3 additions & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
geometric/clc_dot.cl
shared/clc_clamp.cl
shared/clc_max.cl
shared/clc_min.cl
7 changes: 7 additions & 0 deletions libclc/clc/lib/generic/shared/clc_clamp.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <clc/internal/clc.h>

#define __CLC_BODY <clc_clamp.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc_clamp.inc>
#include <clc/math/gentype.inc>
14 changes: 14 additions & 0 deletions libclc/clc/lib/generic/shared/clc_clamp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
__CLC_GENTYPE y,
__CLC_GENTYPE z) {
return (x > z ? z : (x < y ? y : x));
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_clamp(__CLC_GENTYPE x,
__CLC_SCALAR_GENTYPE y,
__CLC_SCALAR_GENTYPE z) {
return (x > (__CLC_GENTYPE)z ? (__CLC_GENTYPE)z
: (x < (__CLC_GENTYPE)y ? (__CLC_GENTYPE)y : x));
}
#endif
7 changes: 7 additions & 0 deletions libclc/clc/lib/generic/shared/clc_max.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <clc/internal/clc.h>

#define __CLC_BODY <clc_max.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc_max.inc>
#include <clc/math/gentype.inc>
11 changes: 11 additions & 0 deletions libclc/clc/lib/generic/shared/clc_max.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
__CLC_GENTYPE b) {
return (a > b ? a : b);
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_max(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b) {
return (a > (__CLC_GENTYPE)b ? a : (__CLC_GENTYPE)b);
}
#endif
Comment on lines +1 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use __builtin_elementwise_max? It's simplest to just not have all this boilerplate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, seems like a sensible refactor while we're at it. I'm generally just copy/pasting code over to try and affect as little as possible.

7 changes: 7 additions & 0 deletions libclc/clc/lib/generic/shared/clc_min.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <clc/internal/clc.h>

#define __CLC_BODY <clc_min.inc>
#include <clc/integer/gentype.inc>

#define __CLC_BODY <clc_min.inc>
#include <clc/math/gentype.inc>
11 changes: 11 additions & 0 deletions libclc/clc/lib/generic/shared/clc_min.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
__CLC_GENTYPE b) {
return (b < a ? b : a);
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_min(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b) {
return (b < (__CLC_GENTYPE)a ? (__CLC_GENTYPE)b : a);
}
#endif
2 changes: 2 additions & 0 deletions libclc/generic/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* THE SOFTWARE.
*/

#include <clc/clcfunc.h>

_CLC_DECL bool __clc_subnormals_disabled();
_CLC_DECL bool __clc_fp16_subnormals_supported();
_CLC_DECL bool __clc_fp32_subnormals_supported();
Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/common/smoothstep.cl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SMOOTH_STEP_DEF(double, double, SMOOTH_STEP_IMPL_D);

_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, double, double, double);

#if !defined(CLC_SPIRV) && !defined(CLC_SPIRV64)
#if !defined(CLC_SPIRV)
SMOOTH_STEP_DEF(float, double, SMOOTH_STEP_IMPL_D);
SMOOTH_STEP_DEF(double, float, SMOOTH_STEP_IMPL_D);

Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/common/step.cl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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) && !defined(CLC_SPIRV64)
#if !defined(CLC_SPIRV)
STEP_DEF(float, double);
STEP_DEF(double, float);

Expand Down
4 changes: 3 additions & 1 deletion libclc/generic/lib/math/clc_hypot.cl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#include <clc/clc.h>
#include <clc/shared/clc_clamp.h>
#include <math/clc_hypot.h>

#include "config.h"
Expand All @@ -39,7 +40,8 @@ _CLC_DEF _CLC_OVERLOAD float __clc_hypot(float x, float y) {
ux = c ? aux : auy;
uy = c ? auy : aux;

int xexp = clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
int xexp =
__clc_clamp((int)(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
float fx_exp = as_float((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fi_exp = as_float((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
float fx = as_float(ux) * fi_exp;
Expand Down
9 changes: 5 additions & 4 deletions libclc/generic/lib/math/clc_ldexp.cl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
* THE SOFTWARE.
*/

#include <clc/clc.h>
#include "config.h"
#include "../clcmacro.h"
#include "config.h"
#include "math.h"
#include <clc/clc.h>
#include <clc/shared/clc_clamp.h>

_CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {

Expand All @@ -35,7 +36,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_ldexp(float x, int n) {
int m = i & 0x007fffff;
int s = i & 0x80000000;
int v = add_sat(e, n);
v = clamp(v, 0, 0xff);
v = __clc_clamp(v, 0, 0xff);
int mr = e == 0 | v == 0 | v == 0xff ? 0 : m;
int c = e == 0xff;
mr = c ? m : mr;
Expand Down Expand Up @@ -110,7 +111,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_ldexp(double x, int n) {
ux = c ? ux : l;

int v = e + n;
v = clamp(v, -0x7ff, 0x7ff);
v = __clc_clamp(v, -0x7ff, 0x7ff);

ux &= ~EXPBITS_DP64;

Expand Down
2 changes: 1 addition & 1 deletion libclc/generic/lib/math/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#if (defined __AMDGCN__ || defined __R600__) && !defined __HAS_FMAF__
#define HAVE_HW_FMA32() (0)
#elif defined CLC_SPIRV || defined CLC_SPIRV64
#elif defined(CLC_SPIRV)
bool __attribute__((noinline)) __clc_runtime_has_hw_fma32(void);
#define HAVE_HW_FMA32() __clc_runtime_has_hw_fma32()
#else
Expand Down
1 change: 1 addition & 0 deletions libclc/generic/lib/shared/clamp.cl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <clc/clc.h>
#include <clc/shared/clc_clamp.h>

#define __CLC_BODY <clamp.inc>
#include <clc/integer/gentype.inc>
Expand Down
4 changes: 2 additions & 2 deletions libclc/generic/lib/shared/clamp.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE clamp(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_GENTYPE z) {
return (x > z ? z : (x < y ? y : x));
return __clc_clamp(x, y, z);
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE clamp(__CLC_GENTYPE x, __CLC_SCALAR_GENTYPE y, __CLC_SCALAR_GENTYPE z) {
return (x > (__CLC_GENTYPE)z ? (__CLC_GENTYPE)z : (x < (__CLC_GENTYPE)y ? (__CLC_GENTYPE)y : x));
return __clc_clamp(x, y, z);
}
#endif
1 change: 1 addition & 0 deletions libclc/generic/lib/shared/max.cl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <clc/clc.h>
#include <clc/shared/clc_max.h>

#define __CLC_BODY <max.inc>
#include <clc/integer/gentype.inc>
Expand Down
7 changes: 4 additions & 3 deletions libclc/generic/lib/shared/max.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a, __CLC_GENTYPE b) {
return (a > b ? a : b);
return __clc_max(a, b);
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a, __CLC_SCALAR_GENTYPE b) {
return (a > (__CLC_GENTYPE)b ? a : (__CLC_GENTYPE)b);
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE max(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b) {
return __clc_max(a, b);
}
#endif
1 change: 1 addition & 0 deletions libclc/generic/lib/shared/min.cl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <clc/clc.h>
#include <clc/shared/clc_min.h>

#define __CLC_BODY <min.inc>
#include <clc/integer/gentype.inc>
Expand Down
7 changes: 4 additions & 3 deletions libclc/generic/lib/shared/min.inc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a, __CLC_GENTYPE b) {
return (b < a ? b : a);
return __clc_min(a, b);
}

#ifndef __CLC_SCALAR
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a, __CLC_SCALAR_GENTYPE b) {
return (b < (__CLC_GENTYPE)a ? (__CLC_GENTYPE)b : a);
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE min(__CLC_GENTYPE a,
__CLC_SCALAR_GENTYPE b) {
return __clc_min(a, b);
}
#endif
Loading