Skip to content

Commit 72edbd9

Browse files
Naghasandavidtwco
andauthored
[SYCL-PTX] Add intermediate layer to libclc to ease type management (#1712)
This patch introduce a "core" layer, with function starting by `__clc_`, to implement builtin functions common to opencl and SPIR-V. Some SPIR-V builtins are prefixed by a `s` or `u` to describe the type of the input. From the implementation point of view this serves no purpose (as it is carried by the type) and leads to a more complex usage of the builtins. SPIR-V conversion function are expressed using various names, conversion implementation are now implemented as `__clc_convert_<desc>` and the opencl and SPIR-V convert function are implemented in terms of `__clc_convert_*` functions Note: this patch introduce a generated header (`spirv_builtins.h`), the generator will be upstreamed separately. Signed-off-by: Victor Lomuller <victor@codeplay.com> Co-authored-by: David Wood <david.wood@codeplay.com>
1 parent 98b6ee4 commit 72edbd9

File tree

148 files changed

+14920
-1454
lines changed

Some content is hidden

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

148 files changed

+14920
-1454
lines changed

libclc/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,15 @@ endif()
160160

161161
find_program( PYTHON python )
162162
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/lib/gen_convert.py clc_script_loc )
163+
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/libspirv/gen_core_convert.py core_script_loc )
163164
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/libspirv/gen_convert.py spirv_script_loc )
164165

166+
add_custom_command(
167+
OUTPUT convert-core.cl
168+
COMMAND ${PYTHON} ${core_script_loc} > convert-core.cl
169+
DEPENDS ${core_script_loc} )
170+
add_custom_target( "generate_convert_core.cl" DEPENDS convert-core.cl )
171+
165172
add_custom_command(
166173
OUTPUT convert-spirv.cl
167174
COMMAND ${PYTHON} ${spirv_script_loc} > convert-spirv.cl
@@ -211,7 +218,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
211218
libclc_configure_lib_source(libspirv_files
212219
LIB_DIR libspirv
213220
DIRS ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS}
214-
DEPS convert-spirv.cl )
221+
DEPS convert-spirv.cl convert-core.cl)
215222

216223
foreach( d ${${t}_devices} )
217224
# Some targets don't have a specific GPU to target
@@ -230,7 +237,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
230237
COMPILE_OPT ${mcpu}
231238
FILES ${libspirv_files}
232239
ALIASES ${${d}_aliases}
233-
GENERATE_TARGET "generate_convert_clc.cl"
240+
GENERATE_TARGET "generate_convert_spirv.cl" "generate_convert_core.cl"
234241
PARENT_TARGET libspirv-builtins)
235242

236243
add_libclc_builtin_set(clc-${arch_suffix}
@@ -240,7 +247,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
240247
FILES ${lib_files}
241248
LIB_DEP libspirv-${arch_suffix}
242249
ALIASES ${${d}_aliases}
243-
GENERATE_TARGET "generate_convert_spirv.cl"
250+
GENERATE_TARGET "generate_convert_clc.cl"
244251
PARENT_TARGET libclc-builtins)
245252
endforeach( d )
246253
endforeach( t )

libclc/cmake/modules/AddLibclc.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ endfunction(add_libclc_alias alias target)
4242
macro(add_libclc_builtin_set arch_suffix)
4343
cmake_parse_arguments(ARG
4444
""
45-
"TRIPLE;TARGET_ENV;LIB_DEP;GENERATE_TARGET;PARENT_TARGET"
46-
"FILES;ALIASES;COMPILE_OPT"
45+
"TRIPLE;TARGET_ENV;LIB_DEP;PARENT_TARGET"
46+
"FILES;ALIASES;GENERATE_TARGET;COMPILE_OPT"
4747
${ARGN})
4848

4949
if (DEFINED ${ARG_LIB_DEP})

libclc/generic/gen_convert_common.py

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
# This file contains common variables and helper functions used by the
22
# `gen_convert.py` in both the libclc and libspirv libraries.
33

4-
types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'float', 'double']
5-
int_types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
4+
types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'half', 'float', 'double']
5+
int_types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
66
unsigned_types = ['uchar', 'ushort', 'uint', 'ulong']
7-
signed_types = ['char', 'short', 'int', 'long']
8-
float_types = ['float', 'double']
7+
signed_types = ['char', 'schar', 'short', 'int', 'long']
8+
float_types = ['half', 'float', 'double']
99
int64_types = ['long', 'ulong']
1010
float64_types = ['double']
11+
float16_types = ['half']
1112
vector_sizes = ['', '2', '3', '4', '8', '16']
12-
half_sizes = {'2': '', '4': '2', '8': '4', '16': '8'}
13+
half_sizes = [('2', ''), ('4', '2'), ('8', '4'), ('16', '8')]
1314

1415
saturation = ['','_sat']
1516
rounding_modes = ['_rtz','_rte','_rtp','_rtn']
1617
float_prefix = {'float':'FLT_', 'double':'DBL_'}
1718
float_suffix = {'float':'f', 'double':''}
1819

19-
bool_type = {'char' : 'char',
20-
'uchar' : 'char',
21-
'short' : 'short',
22-
'ushort': 'short',
23-
'int' : 'int',
24-
'uint' : 'int',
25-
'long' : 'long',
26-
'ulong' : 'long',
20+
bool_type = {'char' : 'char',
21+
'schar' : 'schar',
22+
'uchar' : 'schar',
23+
'short' : 'short',
24+
'ushort' : 'short',
25+
'int' : 'int',
26+
'uint' : 'int',
27+
'long' : 'long',
28+
'ulong' : 'long',
29+
'half' : 'short',
2730
'float' : 'int',
2831
'double' : 'long'}
2932

3033
unsigned_type = {'char' : 'uchar',
34+
'schar' : 'uchar',
3135
'uchar' : 'uchar',
3236
'short' : 'ushort',
3337
'ushort': 'ushort',
@@ -36,13 +40,15 @@
3640
'long' : 'ulong',
3741
'ulong' : 'ulong'}
3842

39-
sizeof_type = {'char' : 1, 'uchar' : 1,
43+
sizeof_type = {'char' : 1, 'schar' : 1, 'uchar' : 1,
4044
'short' : 2, 'ushort' : 2,
4145
'int' : 4, 'uint' : 4,
4246
'long' : 8, 'ulong' : 8,
43-
'float' : 4, 'double' : 8}
47+
'half' : 2, 'float' : 4,
48+
'double': 8}
4449

4550
limit_max = {'char' : 'CHAR_MAX',
51+
'schar' : 'CHAR_MAX',
4652
'uchar' : 'UCHAR_MAX',
4753
'short' : 'SHRT_MAX',
4854
'ushort': 'USHRT_MAX',
@@ -52,6 +58,7 @@
5258
'ulong' : 'ULONG_MAX'}
5359

5460
limit_min = {'char' : 'CHAR_MIN',
61+
'schar' : 'CHAR_MIN',
5562
'uchar' : '0',
5663
'short' : 'SHRT_MIN',
5764
'ushort': '0',
@@ -70,55 +77,35 @@ def conditional_guard(src, dst):
7077
"""
7178
int64_count = 0
7279
float64_count = 0
73-
if src in int64_types:
74-
int64_count = int64_count +1
75-
elif src in float64_types:
76-
float64_count = float64_count + 1
77-
if dst in int64_types:
78-
int64_count = int64_count +1
79-
elif dst in float64_types:
80-
float64_count = float64_count + 1
80+
float16_count = 0
81+
if src in int64_types or dst in int64_types:
82+
int64_count = 1
83+
if src in float64_types or dst in float64_types:
84+
float64_count = 1
85+
if src in float16_types or dst in float16_types:
86+
float16_count = 1
87+
if float16_count > 0:
88+
print("#ifdef cl_khr_fp16")
8189
if float64_count > 0:
8290
#In embedded profile, if cl_khr_fp64 is supported cles_khr_int64 has to be
8391
print("#ifdef cl_khr_fp64")
84-
return True
92+
return 1 + float16_count
8593
elif int64_count > 0:
8694
print("#if defined cles_khr_int64 || !defined(__EMBEDDED_PROFILE__)")
87-
return True
88-
return False
95+
return 1 + float16_count
96+
return float16_count
8997

90-
91-
92-
def spirv_fn_name(src, dst, size='', mode='', sat=''):
98+
def close_conditional_guard(close_conditional):
9399
"""
94-
This helper function returns the correct SPIR-V function name for a given source and destination
95-
type, with optional size, mode and saturation arguments.
100+
This function will close conditional guard opened by conditional_guard.
96101
"""
97-
is_src_float = src in float_types
98-
is_src_unsigned = src in unsigned_types
99-
is_src_signed = src in signed_types
100-
is_dst_float = dst in float_types
101-
is_dst_unsigned = dst in unsigned_types
102-
is_dst_signed = dst in signed_types
103-
is_sat = sat != ''
102+
for _ in range(close_conditional):
103+
print("#endif")
104104

105-
if is_src_unsigned and is_dst_signed and is_sat:
106-
return '__spirv_SatConvertUToS_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
107-
elif is_src_signed and is_dst_unsigned and is_sat:
108-
return '__spirv_SatConvertSToU_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
109-
elif is_src_float and is_dst_signed:
110-
return '__spirv_ConvertFToS_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
111-
elif is_src_float and is_dst_unsigned:
112-
return '__spirv_ConvertFToU_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
113-
elif is_src_signed and is_dst_float:
114-
return '__spirv_ConvertSToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
115-
elif is_src_unsigned and is_dst_float:
116-
return '__spirv_ConvertUToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
117-
elif is_src_float and is_dst_float:
118-
return '__spirv_FConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
119-
elif is_src_unsigned and is_dst_unsigned:
120-
return '__spirv_UConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
121-
elif is_src_signed and is_dst_signed:
122-
return '__spirv_SConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
123-
else:
124-
return None
105+
def clc_core_fn_name(dst, size='', mode='', sat=''):
106+
"""
107+
This helper function returns the correct clc core conversion function name
108+
for a given source and destination type, with optional size, mode
109+
and saturation arguments.
110+
"""
111+
return "__clc_convert_{DST}{N}{SAT}{MODE}".format(DST=dst, N=size, SAT=sat, MODE=mode)

libclc/generic/include/as_type.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CLC_AS_TYPE
33

44
#define as_char(x) __builtin_astype(x, char)
5+
#define as_schar(x) __builtin_astype(x, schar)
56
#define as_uchar(x) __builtin_astype(x, uchar)
67
#define as_short(x) __builtin_astype(x, short)
78
#define as_ushort(x) __builtin_astype(x, ushort)
@@ -12,6 +13,7 @@
1213
#define as_float(x) __builtin_astype(x, float)
1314

1415
#define as_char2(x) __builtin_astype(x, char2)
16+
#define as_schar2(x) __builtin_astype(x, schar2)
1517
#define as_uchar2(x) __builtin_astype(x, uchar2)
1618
#define as_short2(x) __builtin_astype(x, short2)
1719
#define as_ushort2(x) __builtin_astype(x, ushort2)
@@ -22,6 +24,7 @@
2224
#define as_float2(x) __builtin_astype(x, float2)
2325

2426
#define as_char3(x) __builtin_astype(x, char3)
27+
#define as_schar3(x) __builtin_astype(x, schar3)
2528
#define as_uchar3(x) __builtin_astype(x, uchar3)
2629
#define as_short3(x) __builtin_astype(x, short3)
2730
#define as_ushort3(x) __builtin_astype(x, ushort3)
@@ -32,6 +35,7 @@
3235
#define as_float3(x) __builtin_astype(x, float3)
3336

3437
#define as_char4(x) __builtin_astype(x, char4)
38+
#define as_schar4(x) __builtin_astype(x, schar4)
3539
#define as_uchar4(x) __builtin_astype(x, uchar4)
3640
#define as_short4(x) __builtin_astype(x, short4)
3741
#define as_ushort4(x) __builtin_astype(x, ushort4)
@@ -42,6 +46,7 @@
4246
#define as_float4(x) __builtin_astype(x, float4)
4347

4448
#define as_char8(x) __builtin_astype(x, char8)
49+
#define as_schar8(x) __builtin_astype(x, schar8)
4550
#define as_uchar8(x) __builtin_astype(x, uchar8)
4651
#define as_short8(x) __builtin_astype(x, short8)
4752
#define as_ushort8(x) __builtin_astype(x, ushort8)
@@ -52,6 +57,7 @@
5257
#define as_float8(x) __builtin_astype(x, float8)
5358

5459
#define as_char16(x) __builtin_astype(x, char16)
60+
#define as_schar16(x) __builtin_astype(x, schar16)
5561
#define as_uchar16(x) __builtin_astype(x, uchar16)
5662
#define as_short16(x) __builtin_astype(x, short16)
5763
#define as_ushort16(x) __builtin_astype(x, ushort16)

libclc/generic/include/clc/clc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
1313
#endif
1414

15+
#define __CLC_NO_SCHAR
16+
1517
/* Function Attributes */
1618
#include <func.h>
1719

0 commit comments

Comments
 (0)