Skip to content

Commit c9b9b56

Browse files
authored
[libclc] Reland 7e6a739 (#16606)
This upstream libclc commit was dropped during a pulldown. This relands it, adapting it to our downstream libclc framework. Most of the upstream commit was to the OpenCL builtins, which we don't directly make use of for DPC++. ---- libclc: increase fp16 support (#98149) Increase fp16 support to allow clspv to continue to be OpenCL compliant following the update of the OpenCL-CTS adding more testing on math functions and conversions with half. Math functions are implemented by upscaling to fp32 and using the fp32 implementation. It garantees the accuracy required for half-precision float-point by the CTS.
1 parent 44c58bb commit c9b9b56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+445
-45
lines changed

libclc/clc/include/clc/clcmacro.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@
247247
} \
248248
_CLC_BINARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, FUNCTION, half, half)
249249

250+
#pragma OPENCL EXTENSION cl_khr_fp16 : disable
251+
250252
#else
251253

252254
#define _CLC_DEFINE_UNARY_BUILTIN_FP16(FUNCTION)

libclc/clspv/lib/math/fma.cl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,14 @@ _CLC_DEF _CLC_OVERLOAD float fma(float a, float b, float c) {
269269
((uint)st_fma.mantissa.lo & 0x7fffff));
270270
}
271271
_CLC_TERNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, fma, float, float, float)
272+
273+
#ifdef cl_khr_fp16
274+
275+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
276+
277+
_CLC_DEF _CLC_OVERLOAD half fma(half a, half b, half c) {
278+
return (half)mad((float)a, (float)b, (float)c);
279+
}
280+
_CLC_TERNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, half, fma, half, half, half)
281+
282+
#endif

libclc/generic/gen_convert_common.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,31 @@
4747
'half' : 2, 'float' : 4,
4848
'double': 8}
4949

50-
limit_max = {'char' : 'CHAR_MAX',
51-
'schar' : 'CHAR_MAX',
52-
'uchar' : 'UCHAR_MAX',
53-
'short' : 'SHRT_MAX',
54-
'ushort': 'USHRT_MAX',
55-
'int' : 'INT_MAX',
56-
'uint' : 'UINT_MAX',
57-
'long' : 'LONG_MAX',
58-
'ulong' : 'ULONG_MAX'}
50+
limit_max = {
51+
"char": "CHAR_MAX",
52+
"schar": "CHAR_MAX",
53+
"uchar": "UCHAR_MAX",
54+
"short": "SHRT_MAX",
55+
"ushort": "USHRT_MAX",
56+
"int": "INT_MAX",
57+
"uint": "UINT_MAX",
58+
"long": "LONG_MAX",
59+
"ulong": "ULONG_MAX",
60+
"half": "0x1.ffcp+15",
61+
}
5962

60-
limit_min = {'char' : 'CHAR_MIN',
61-
'schar' : 'CHAR_MIN',
62-
'uchar' : '0',
63-
'short' : 'SHRT_MIN',
64-
'ushort': '0',
65-
'int' : 'INT_MIN',
66-
'uint' : '0',
67-
'long' : 'LONG_MIN',
68-
'ulong' : '0'}
63+
limit_min = {
64+
"char": "CHAR_MIN",
65+
"schar": "CHAR_MIN",
66+
"uchar": "0",
67+
"short": "SHRT_MIN",
68+
"ushort": "0",
69+
"int": "INT_MIN",
70+
"uint": "0",
71+
"long": "LONG_MIN",
72+
"ulong": "0",
73+
"half": "-0x1.ffcp+15",
74+
}
6975

7076

7177
def conditional_guard(src, dst):

libclc/generic/include/clc/convert.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@
2323
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, ulong, SUFFIX) \
2424
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, float, SUFFIX)
2525

26-
#ifdef cl_khr_fp64
26+
#if defined(cl_khr_fp64) && defined(cl_khr_fp16)
27+
#define _CLC_VECTOR_CONVERT_FROM(FROM_TYPE, SUFFIX) \
28+
_CLC_VECTOR_CONVERT_FROM1(FROM_TYPE, SUFFIX) \
29+
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, double, SUFFIX) \
30+
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, half, SUFFIX)
31+
#elif defined(cl_khr_fp64)
2732
#define _CLC_VECTOR_CONVERT_FROM(FROM_TYPE, SUFFIX) \
2833
_CLC_VECTOR_CONVERT_FROM1(FROM_TYPE, SUFFIX) \
2934
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, double, SUFFIX)
35+
#elif defined(cl_khr_fp16)
36+
#define _CLC_VECTOR_CONVERT_FROM(FROM_TYPE, SUFFIX) \
37+
_CLC_VECTOR_CONVERT_FROM1(FROM_TYPE, SUFFIX) \
38+
_CLC_VECTOR_CONVERT_DECL(FROM_TYPE, half, SUFFIX)
3039
#else
3140
#define _CLC_VECTOR_CONVERT_FROM(FROM_TYPE, SUFFIX) \
3241
_CLC_VECTOR_CONVERT_FROM1(FROM_TYPE, SUFFIX)
@@ -43,11 +52,19 @@
4352
_CLC_VECTOR_CONVERT_FROM(ulong, SUFFIX) \
4453
_CLC_VECTOR_CONVERT_FROM(float, SUFFIX)
4554

46-
#ifdef cl_khr_fp64
55+
#if defined(cl_khr_fp64) && defined(cl_khr_fp16)
56+
#define _CLC_VECTOR_CONVERT_TO(SUFFIX) \
57+
_CLC_VECTOR_CONVERT_TO1(SUFFIX) \
58+
_CLC_VECTOR_CONVERT_FROM(double, SUFFIX) \
59+
_CLC_VECTOR_CONVERT_FROM(half, SUFFIX)
60+
#elif defined(cl_khr_fp64)
4761
#define _CLC_VECTOR_CONVERT_TO(SUFFIX) \
4862
_CLC_VECTOR_CONVERT_TO1(SUFFIX) \
4963
_CLC_VECTOR_CONVERT_FROM(double, SUFFIX)
50-
#else
64+
#elif defined(cl_khr_fp16)
65+
#define _CLC_VECTOR_CONVERT_TO(SUFFIX) \
66+
_CLC_VECTOR_CONVERT_TO1(SUFFIX) \
67+
_CLC_VECTOR_CONVERT_FROM(half, SUFFIX)
5168
#define _CLC_VECTOR_CONVERT_TO(SUFFIX) \
5269
_CLC_VECTOR_CONVERT_TO1(SUFFIX)
5370
#endif

libclc/generic/lib/gen_convert.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@
6565
"uint",
6666
"long",
6767
"ulong",
68+
"half",
6869
"float",
6970
"double",
7071
]
7172
int_types = ["char", "uchar", "short", "ushort", "int", "uint", "long", "ulong"]
7273
unsigned_types = ["uchar", "ushort", "uint", "ulong"]
73-
float_types = ["float", "double"]
74+
float_types = ["half", "float", "double"]
7475
int64_types = ["long", "ulong"]
7576
float64_types = ["double"]
77+
float16_types = ["half"]
7678
vector_sizes = ["", "2", "3", "4", "8", "16"]
7779
half_sizes = [("2", ""), ("4", "2"), ("8", "4"), ("16", "8")]
7880

7981
saturation = ["", "_sat"]
8082
rounding_modes = ["_rtz", "_rte", "_rtp", "_rtn"]
81-
float_prefix = {"float": "FLT_", "double": "DBL_"}
82-
float_suffix = {"float": "f", "double": ""}
8383

8484
bool_type = {
8585
"char": "char",
@@ -90,6 +90,7 @@
9090
"uint": "int",
9191
"long": "long",
9292
"ulong": "long",
93+
"half": "short",
9394
"float": "int",
9495
"double": "long",
9596
}
@@ -114,6 +115,7 @@
114115
"uint": 4,
115116
"long": 8,
116117
"ulong": 8,
118+
"half": 2,
117119
"float": 4,
118120
"double": 8,
119121
}
@@ -127,6 +129,7 @@
127129
"uint": "UINT_MAX",
128130
"long": "LONG_MAX",
129131
"ulong": "ULONG_MAX",
132+
"half": "0x1.ffcp+15",
130133
}
131134

132135
limit_min = {
@@ -138,24 +141,33 @@
138141
"uint": "0",
139142
"long": "LONG_MIN",
140143
"ulong": "0",
144+
"half": "-0x1.ffcp+15",
141145
}
142146

143147

144148
def conditional_guard(src, dst):
145149
int64_count = 0
146150
float64_count = 0
151+
float16_count = 0
147152
if src in int64_types:
148153
int64_count = int64_count + 1
149154
elif src in float64_types:
150155
float64_count = float64_count + 1
156+
elif src in float16_types:
157+
float16_count = float16_count + 1
151158
if dst in int64_types:
152159
int64_count = int64_count + 1
153160
elif dst in float64_types:
154161
float64_count = float64_count + 1
162+
elif dst in float16_types:
163+
float16_count = float16_count + 1
155164
if float64_count > 0:
156165
# In embedded profile, if cl_khr_fp64 is supported cles_khr_int64 has to be
157166
print("#ifdef cl_khr_fp64")
158167
return True
168+
elif float16_count > 0:
169+
print("#if defined cl_khr_fp16")
170+
return True
159171
elif int64_count > 0:
160172
print("#if defined cles_khr_int64 || !defined(__EMBEDDED_PROFILE__)")
161173
return True
@@ -198,6 +210,10 @@ def conditional_guard(src, dst):
198210
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
199211
#endif
200212
213+
#ifdef cl_khr_fp16
214+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
215+
#endif
216+
201217
#ifdef cl_khr_fp64
202218
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
203219

libclc/generic/lib/math/acos.cl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,11 @@ _CLC_OVERLOAD _CLC_DEF double acos(double x) {
171171
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acos, double);
172172

173173
#endif // cl_khr_fp64
174+
175+
#ifdef cl_khr_fp16
176+
177+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
178+
179+
_CLC_DEFINE_UNARY_BUILTIN_FP16(acos)
180+
181+
#endif

libclc/generic/lib/math/acosh.cl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ _CLC_OVERLOAD _CLC_DEF double acosh(double x) {
125125
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acosh, double)
126126

127127
#endif
128+
129+
#ifdef cl_khr_fp16
130+
131+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
132+
133+
_CLC_DEFINE_UNARY_BUILTIN_FP16(acosh)
134+
135+
#endif

libclc/generic/lib/math/acospi.cl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,11 @@ _CLC_OVERLOAD _CLC_DEF double acospi(double x) {
170170
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acospi, double)
171171

172172
#endif
173+
174+
#ifdef cl_khr_fp16
175+
176+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
177+
178+
_CLC_DEFINE_UNARY_BUILTIN_FP16(acospi)
179+
180+
#endif

libclc/generic/lib/math/asinh.cl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,11 @@ _CLC_OVERLOAD _CLC_DEF double asinh(double x) {
291291
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, asinh, double)
292292

293293
#endif
294+
295+
#ifdef cl_khr_fp16
296+
297+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
298+
299+
_CLC_DEFINE_UNARY_BUILTIN_FP16(asinh)
300+
301+
#endif

libclc/generic/lib/math/atan.cl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,12 @@ _CLC_OVERLOAD _CLC_DEF double atan(double x)
181181
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan, double);
182182

183183
#endif // cl_khr_fp64
184+
185+
186+
#ifdef cl_khr_fp16
187+
188+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
189+
190+
_CLC_DEFINE_UNARY_BUILTIN_FP16(atan)
191+
192+
#endif

0 commit comments

Comments
 (0)