Skip to content

Commit 3ee52d9

Browse files
authored
[libspirv] Align with upstream; pull in optimizations (#16881)
Recent changes to upstream libclc have provided more optimized builtins in the CLC library. Harnessing those in libspirv brings in performance improvements to the smoothstep, radians, and degrees builtins through vector codegen. It also reduces duplicated code so should marginally improve build times.
1 parent d3568af commit 3ee52d9

File tree

13 files changed

+52
-158
lines changed

13 files changed

+52
-158
lines changed

libclc/generic/lib/common/radians.cl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <clc/clc.h>
2424
#include <clc/clcmacro.h>
2525
#include <clc/common/clc_radians.h>
26-
#include <libspirv/spirv.h>
27-
2826

2927
_CLC_DEFINE_UNARY_BUILTIN(float, radians, __clc_radians, float)
3028

libclc/generic/lib/common/smoothstep.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <clc/clc.h>
2424
#include <clc/clcmacro.h>
25-
#include <libspirv/spirv.h>
2625
#include <clc/common/clc_smoothstep.h>
2726

2827
#define SMOOTHSTEP_SINGLE_DEF(X_TYPE) \

libclc/generic/lib/math/mad.inc

Lines changed: 0 additions & 3 deletions
This file was deleted.

libclc/libspirv/lib/generic/common/mix.cl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/math/clc_mad.h>
910
#include <libspirv/spirv.h>
1011

1112
#define __CLC_BODY <mix.inc>

libclc/libspirv/lib/generic/common/mix.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __spirv_ocl_mix(__CLC_GENTYPE x,
1010
__CLC_GENTYPE y,
1111
__CLC_GENTYPE a) {
12-
return __spirv_ocl_mad(y - x, a, x);
12+
return __clc_mad(y - x, a, x);
1313
}
1414

1515
#ifndef __CLC_SCALAR

libclc/libspirv/lib/generic/common/radians.cl

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <libspirv/spirv.h>
10-
119
#include <clc/clcmacro.h>
10+
#include <clc/common/clc_radians.h>
11+
#include <libspirv/spirv.h>
1212

13-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_radians(float degrees) {
14-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
15-
return 0x1.1df46ap-6F * degrees;
16-
}
17-
18-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __spirv_ocl_radians, float);
19-
20-
21-
#ifdef cl_khr_fp64
22-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
23-
24-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_radians(double degrees) {
25-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
26-
return 0x1.1df46a2529d39p-6 * degrees;
27-
}
28-
29-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __spirv_ocl_radians,
30-
double);
31-
32-
#endif
33-
34-
#ifdef cl_khr_fp16
35-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
36-
37-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_radians(half degrees) {
38-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
39-
return M_PI_OVER_180_H * degrees;
40-
}
41-
42-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __spirv_ocl_radians, half);
43-
44-
#endif
13+
#define FUNCTION __spirv_ocl_radians
14+
#define __CLC_FUNCTION(x) __clc_radians
15+
#define __CLC_BODY <clc/common/unary_def.inc>
16+
#include <clc/math/gentype.inc>

libclc/libspirv/lib/generic/common/smoothstep.cl

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,36 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/clcmacro.h>
10+
#include <clc/common/clc_smoothstep.h>
911
#include <libspirv/spirv.h>
1012

11-
#include <clc/clcmacro.h>
13+
#define SMOOTHSTEP_SINGLE_DEF(X_TYPE) \
14+
_CLC_OVERLOAD _CLC_DEF X_TYPE __spirv_ocl_smoothstep( \
15+
X_TYPE edge0, X_TYPE edge1, X_TYPE x) { \
16+
return __clc_smoothstep(edge0, edge1, x); \
17+
}
1218

13-
_CLC_OVERLOAD _CLC_DEF float __spirv_ocl_smoothstep(float edge0, float edge1,
14-
float x) {
15-
float t = __spirv_ocl_fclamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
16-
return t * t * (3.0f - 2.0f * t);
17-
}
19+
#define SMOOTHSTEP_DEF(type) \
20+
SMOOTHSTEP_SINGLE_DEF(type) \
21+
SMOOTHSTEP_SINGLE_DEF(type##2) \
22+
SMOOTHSTEP_SINGLE_DEF(type##3) \
23+
SMOOTHSTEP_SINGLE_DEF(type##4) \
24+
SMOOTHSTEP_SINGLE_DEF(type##8) \
25+
SMOOTHSTEP_SINGLE_DEF(type##16)
1826

19-
_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __spirv_ocl_smoothstep,
20-
float, float, float)
27+
SMOOTHSTEP_DEF(float)
2128

2229
#ifdef cl_khr_fp64
2330
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
2431

25-
_CLC_OVERLOAD _CLC_DEF double __spirv_ocl_smoothstep(double edge0, double edge1,
26-
double x) {
27-
double t = __spirv_ocl_fclamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
28-
return t * t * (3.0 - 2.0 * t);
29-
}
30-
31-
_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __spirv_ocl_smoothstep,
32-
double, double, double)
32+
SMOOTHSTEP_DEF(double);
3333

3434
#endif
3535

3636
#ifdef cl_khr_fp16
3737
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
3838

39-
_CLC_OVERLOAD _CLC_DEF half __spirv_ocl_smoothstep(half edge0, half edge1,
40-
half x) {
41-
half t = __spirv_ocl_fclamp((x - edge0) / (edge1 - edge0), 0.0h, 1.0h);
42-
return t * t * (3.0h - 2.0h * t);
43-
}
44-
45-
_CLC_TERNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __spirv_ocl_smoothstep,
46-
half, half, half);
39+
SMOOTHSTEP_DEF(half);
4740

4841
#endif

libclc/libspirv/lib/generic/math/mad.cl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,22 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/clcmacro.h>
10+
#include <clc/math/clc_mad.h>
911
#include <libspirv/spirv.h>
1012

11-
#define __CLC_BODY <mad.inc>
12-
#include <clc/math/gentype.inc>
13+
_CLC_DEFINE_TERNARY_BUILTIN(float, __spirv_ocl_mad, __clc_mad, float, float, float)
14+
15+
#ifdef cl_khr_fp64
16+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
17+
18+
_CLC_DEFINE_TERNARY_BUILTIN(double, __spirv_ocl_mad, __clc_mad, double, double, double)
19+
20+
#endif
21+
22+
#ifdef cl_khr_fp16
23+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
24+
25+
_CLC_DEFINE_TERNARY_BUILTIN(half, __spirv_ocl_mad, __clc_mad, half, half, half)
26+
27+
#endif

libclc/libspirv/lib/generic/math/mad.inc

Lines changed: 0 additions & 13 deletions
This file was deleted.

libclc/libspirv/lib/generic/relational/bitselect.cl

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/relational/clc_bitselect.h>
910
#include <libspirv/spirv.h>
1011

11-
#include <clc/clcmacro.h>
12-
1312
#define __CLC_BODY <bitselect.inc>
1413
#include <clc/integer/gentype.inc>
15-
#undef __CLC_BODY
16-
17-
#define FLOAT_BITSELECT(f_type, i_type, width) \
18-
_CLC_OVERLOAD _CLC_DEF f_type##width __spirv_ocl_bitselect( \
19-
f_type##width x, f_type##width y, f_type##width z) { \
20-
return as_##f_type##width(__spirv_ocl_bitselect( \
21-
as_##i_type##width(x), as_##i_type##width(y), as_##i_type##width(z))); \
22-
}
23-
24-
FLOAT_BITSELECT(float, uint, )
25-
FLOAT_BITSELECT(float, uint, 2)
26-
FLOAT_BITSELECT(float, uint, 3)
27-
FLOAT_BITSELECT(float, uint, 4)
28-
FLOAT_BITSELECT(float, uint, 8)
29-
FLOAT_BITSELECT(float, uint, 16)
30-
31-
#ifdef cl_khr_fp64
32-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
33-
34-
FLOAT_BITSELECT(double, ulong, )
35-
FLOAT_BITSELECT(double, ulong, 2)
36-
FLOAT_BITSELECT(double, ulong, 3)
37-
FLOAT_BITSELECT(double, ulong, 4)
38-
FLOAT_BITSELECT(double, ulong, 8)
39-
FLOAT_BITSELECT(double, ulong, 16)
4014

41-
#endif
42-
43-
#ifdef cl_khr_fp16
44-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
45-
46-
FLOAT_BITSELECT(half, ushort, )
47-
FLOAT_BITSELECT(half, ushort, 2)
48-
FLOAT_BITSELECT(half, ushort, 3)
49-
FLOAT_BITSELECT(half, ushort, 4)
50-
FLOAT_BITSELECT(half, ushort, 8)
51-
FLOAT_BITSELECT(half, ushort, 16)
52-
53-
#endif
15+
#define __CLC_BODY <bitselect.inc>
16+
#include <clc/math/gentype.inc>

0 commit comments

Comments
 (0)