Skip to content

Commit b7e2014

Browse files
authored
[libclc] Move smoothstep to CLC and optimize its codegen (#123183)
This commit moves the implementation of the smoothstep function to the CLC library, whilst optimizing the codegen. This commit also adds support for 'half' versions of smoothstep, which were previously missing. The CLC smoothstep implementation now keeps everything in vectors, rather than recursively splitting vectors by half down to the scalar base form. This should result in more optimal codegen across the board. This commit also removes some non-standard overloads of smoothstep with mixed types, such as 'double smoothstep(float, float, float)'. There aren't any mixed-(element )type versions of smoothstep as far as I can see: gentype smoothstep(gentype edge0, gentype edge1, gentype x) gentypef smoothstep(float edge0, float edge1, gentypef x) gentyped smoothstep(double edge0, double edge1, gentyped x) gentypeh smoothstep(half edge0, half edge1, gentypeh x) The CLC library only defines the first type, for simplicity; the OpenCL layer is responsible for handling the scalar/scalar/vector forms. Note that the scalar/scalar/vector forms now splat the scalars to the vector type, rather than recursively split vectors as before. The macro that used to 'vectorize' smoothstep in this way has been moved out of the shared clcmacro.h header as it was only used for the smoothstep builtin. Note that the CLC clamp function is now built for both SPIR-V targets. This is to help build the CLC smoothstep function for the Mesa SPIR-V target.
1 parent 628976c commit b7e2014

File tree

10 files changed

+118
-50
lines changed

10 files changed

+118
-50
lines changed

libclc/clc/include/clc/clcmacro.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,6 @@
102102
FUNCTION(x.hi, y.hi, z.hi)); \
103103
}
104104

105-
#define _CLC_V_S_S_V_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE, \
106-
ARG2_TYPE, ARG3_TYPE) \
107-
DECLSPEC RET_TYPE##2 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##2 z) { \
108-
return (RET_TYPE##2)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
109-
} \
110-
\
111-
DECLSPEC RET_TYPE##3 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##3 z) { \
112-
return (RET_TYPE##3)(FUNCTION(x, y, z.x), FUNCTION(x, y, z.y), \
113-
FUNCTION(x, y, z.z)); \
114-
} \
115-
\
116-
DECLSPEC RET_TYPE##4 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##4 z) { \
117-
return (RET_TYPE##4)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
118-
} \
119-
\
120-
DECLSPEC RET_TYPE##8 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##8 z) { \
121-
return (RET_TYPE##8)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
122-
} \
123-
\
124-
DECLSPEC RET_TYPE##16 FUNCTION(ARG1_TYPE x, ARG2_TYPE y, ARG3_TYPE##16 z) { \
125-
return (RET_TYPE##16)(FUNCTION(x, y, z.lo), FUNCTION(x, y, z.hi)); \
126-
}
127-
128105
#define _CLC_V_V_VP_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE, \
129106
ADDR_SPACE, ARG2_TYPE) \
130107
DECLSPEC __CLC_XCONCAT(RET_TYPE, 2) \
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __CLC_COMMON_CLC_SMOOTHSTEP_H__
2+
#define __CLC_COMMON_CLC_SMOOTHSTEP_H__
3+
4+
// note: Unlike OpenCL __clc_smoothstep is only defined for three matching
5+
// argument types.
6+
7+
#define __CLC_BODY <clc/common/clc_smoothstep.inc>
8+
#include <clc/math/gentype.inc>
9+
#undef __CLC_BODY
10+
11+
#endif // __CLC_COMMON_CLC_SMOOTHSTEP_H__
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __clc_smoothstep(__CLC_GENTYPE edge0,
2+
__CLC_GENTYPE edge1,
3+
__CLC_GENTYPE x);
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
#ifndef __CLC_SHARED_CLC_CLAMP_H__
22
#define __CLC_SHARED_CLC_CLAMP_H__
33

4-
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5-
// clspv and spir-v targets provide their own OpenCL-compatible clamp
6-
#define __clc_clamp clamp
7-
#else
8-
94
#define __CLC_BODY <clc/shared/clc_clamp.inc>
105
#include <clc/integer/gentype.inc>
116

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

15-
#endif
16-
1710
#endif // __CLC_SHARED_CLC_CLAMP_H__

libclc/clc/lib/clspv/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
../generic/math/clc_floor.cl
44
../generic/math/clc_rint.cl
55
../generic/math/clc_trunc.cl
6+
../generic/shared/clc_clamp.cl

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
common/clc_smoothstep.cl
12
geometric/clc_dot.cl
23
integer/clc_abs.cl
34
integer/clc_abs_diff.cl
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
#include <clc/clcmacro.h>
23+
#include <clc/internal/clc.h>
24+
#include <clc/shared/clc_clamp.h>
25+
26+
#define SMOOTHSTEP_SINGLE_DEF(edge_type, x_type, lit_suff) \
27+
_CLC_OVERLOAD _CLC_DEF x_type __clc_smoothstep(edge_type edge0, \
28+
edge_type edge1, x_type x) { \
29+
x_type t = __clc_clamp((x - edge0) / (edge1 - edge0), 0.0##lit_suff, \
30+
1.0##lit_suff); \
31+
return t * t * (3.0##lit_suff - 2.0##lit_suff * t); \
32+
}
33+
34+
#define SMOOTHSTEP_DEF(type, lit_suffix) \
35+
SMOOTHSTEP_SINGLE_DEF(type, type, lit_suffix) \
36+
SMOOTHSTEP_SINGLE_DEF(type##2, type##2, lit_suffix) \
37+
SMOOTHSTEP_SINGLE_DEF(type##3, type##3, lit_suffix) \
38+
SMOOTHSTEP_SINGLE_DEF(type##4, type##4, lit_suffix) \
39+
SMOOTHSTEP_SINGLE_DEF(type##8, type##8, lit_suffix) \
40+
SMOOTHSTEP_SINGLE_DEF(type##16, type##16, lit_suffix)
41+
42+
SMOOTHSTEP_DEF(float, F)
43+
44+
#ifdef cl_khr_fp64
45+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
46+
SMOOTHSTEP_DEF(double, );
47+
#endif
48+
49+
#ifdef cl_khr_fp16
50+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
51+
SMOOTHSTEP_DEF(half, H);
52+
#endif

libclc/clc/lib/spirv/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
../generic/common/clc_smoothstep.cl
12
../generic/geometric/clc_dot.cl
23
../generic/math/clc_ceil.cl
34
../generic/math/clc_fabs.cl
45
../generic/math/clc_floor.cl
56
../generic/math/clc_rint.cl
67
../generic/math/clc_trunc.cl
8+
../generic/shared/clc_clamp.cl

libclc/clc/lib/spirv64/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
../generic/common/clc_smoothstep.cl
12
../generic/geometric/clc_dot.cl
23
../generic/math/clc_ceil.cl
34
../generic/math/clc_fabs.cl
45
../generic/math/clc_floor.cl
56
../generic/math/clc_rint.cl
67
../generic/math/clc_trunc.cl
8+
../generic/shared/clc_clamp.cl

libclc/generic/lib/common/smoothstep.cl

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,61 @@
2222

2323
#include <clc/clc.h>
2424
#include <clc/clcmacro.h>
25+
#include <clc/common/clc_smoothstep.h>
2526

26-
_CLC_OVERLOAD _CLC_DEF float smoothstep(float edge0, float edge1, float x) {
27-
float t = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
28-
return t * t * (3.0f - 2.0f * t);
29-
}
27+
#define SMOOTHSTEP_SINGLE_DEF(X_TYPE) \
28+
_CLC_OVERLOAD _CLC_DEF X_TYPE smoothstep(X_TYPE edge0, X_TYPE edge1, \
29+
X_TYPE x) { \
30+
return __clc_smoothstep(edge0, edge1, x); \
31+
}
3032

31-
_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, float, float, float);
33+
#define SMOOTHSTEP_S_S_V_DEFS(X_TYPE) \
34+
_CLC_OVERLOAD _CLC_DEF X_TYPE##2 smoothstep(X_TYPE x, X_TYPE y, \
35+
X_TYPE##2 z) { \
36+
return __clc_smoothstep((X_TYPE##2)x, (X_TYPE##2)y, z); \
37+
} \
38+
\
39+
_CLC_OVERLOAD _CLC_DEF X_TYPE##3 smoothstep(X_TYPE x, X_TYPE y, \
40+
X_TYPE##3 z) { \
41+
return __clc_smoothstep((X_TYPE##3)x, (X_TYPE##3)y, z); \
42+
} \
43+
\
44+
_CLC_OVERLOAD _CLC_DEF X_TYPE##4 smoothstep(X_TYPE x, X_TYPE y, \
45+
X_TYPE##4 z) { \
46+
return __clc_smoothstep((X_TYPE##4)x, (X_TYPE##4)y, z); \
47+
} \
48+
\
49+
_CLC_OVERLOAD _CLC_DEF X_TYPE##8 smoothstep(X_TYPE x, X_TYPE y, \
50+
X_TYPE##8 z) { \
51+
return __clc_smoothstep((X_TYPE##8)x, (X_TYPE##8)y, z); \
52+
} \
53+
\
54+
_CLC_OVERLOAD _CLC_DEF X_TYPE##16 smoothstep(X_TYPE x, X_TYPE y, \
55+
X_TYPE##16 z) { \
56+
return __clc_smoothstep((X_TYPE##16)x, (X_TYPE##16)y, z); \
57+
}
3258

33-
_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, float, float, float);
59+
#define SMOOTHSTEP_DEF(type) \
60+
SMOOTHSTEP_SINGLE_DEF(type) \
61+
SMOOTHSTEP_SINGLE_DEF(type##2) \
62+
SMOOTHSTEP_SINGLE_DEF(type##3) \
63+
SMOOTHSTEP_SINGLE_DEF(type##4) \
64+
SMOOTHSTEP_SINGLE_DEF(type##8) \
65+
SMOOTHSTEP_SINGLE_DEF(type##16) \
66+
SMOOTHSTEP_S_S_V_DEFS(type)
67+
68+
SMOOTHSTEP_DEF(float)
3469

3570
#ifdef cl_khr_fp64
3671
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
3772

38-
#define SMOOTH_STEP_DEF(edge_type, x_type, impl) \
39-
_CLC_OVERLOAD _CLC_DEF x_type smoothstep(edge_type edge0, edge_type edge1, x_type x) { \
40-
double t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); \
41-
return t * t * (3.0 - 2.0 * t); \
42-
}
43-
44-
SMOOTH_STEP_DEF(double, double, SMOOTH_STEP_IMPL_D);
73+
SMOOTHSTEP_DEF(double);
4574

46-
_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, double, double, double);
75+
#endif
4776

48-
#if !defined(CLC_SPIRV)
49-
SMOOTH_STEP_DEF(float, double, SMOOTH_STEP_IMPL_D);
50-
SMOOTH_STEP_DEF(double, float, SMOOTH_STEP_IMPL_D);
77+
#ifdef cl_khr_fp16
78+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
5179

52-
_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, smoothstep, float, float, double);
53-
_CLC_V_S_S_V_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, smoothstep, double, double, float);
54-
#endif
80+
SMOOTHSTEP_DEF(half);
5581

5682
#endif

0 commit comments

Comments
 (0)