Skip to content

[SYCL-PTX] Add intermediate layer to libclc to ease type management #1712

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 6 commits into from
May 28, 2020
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
13 changes: 10 additions & 3 deletions libclc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,15 @@ endif()

find_program( PYTHON python )
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/lib/gen_convert.py clc_script_loc )
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/libspirv/gen_core_convert.py core_script_loc )
file( TO_CMAKE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/generic/libspirv/gen_convert.py spirv_script_loc )

add_custom_command(
OUTPUT convert-core.cl
COMMAND ${PYTHON} ${core_script_loc} > convert-core.cl
DEPENDS ${core_script_loc} )
add_custom_target( "generate_convert_core.cl" DEPENDS convert-core.cl )

add_custom_command(
OUTPUT convert-spirv.cl
COMMAND ${PYTHON} ${spirv_script_loc} > convert-spirv.cl
Expand Down Expand Up @@ -211,7 +218,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
libclc_configure_lib_source(libspirv_files
LIB_DIR libspirv
DIRS ${dirs} ${DARCH} ${DARCH}-${OS} ${DARCH}-${VENDOR}-${OS}
DEPS convert-spirv.cl )
DEPS convert-spirv.cl convert-core.cl)

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

add_libclc_builtin_set(clc-${arch_suffix}
Expand All @@ -240,7 +247,7 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
FILES ${lib_files}
LIB_DEP libspirv-${arch_suffix}
ALIASES ${${d}_aliases}
GENERATE_TARGET "generate_convert_spirv.cl"
GENERATE_TARGET "generate_convert_clc.cl"
PARENT_TARGET libclc-builtins)
endforeach( d )
endforeach( t )
Expand Down
4 changes: 2 additions & 2 deletions libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ endfunction(add_libclc_alias alias target)
macro(add_libclc_builtin_set arch_suffix)
cmake_parse_arguments(ARG
""
"TRIPLE;TARGET_ENV;LIB_DEP;GENERATE_TARGET;PARENT_TARGET"
"FILES;ALIASES;COMPILE_OPT"
"TRIPLE;TARGET_ENV;LIB_DEP;PARENT_TARGET"
"FILES;ALIASES;GENERATE_TARGET;COMPILE_OPT"
${ARGN})

if (DEFINED ${ARG_LIB_DEP})
Expand Down
103 changes: 45 additions & 58 deletions libclc/generic/gen_convert_common.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
# This file contains common variables and helper functions used by the
# `gen_convert.py` in both the libclc and libspirv libraries.

types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'float', 'double']
int_types = ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'half', 'float', 'double']
int_types = ['char', 'schar', 'uchar', 'short', 'ushort', 'int', 'uint', 'long', 'ulong']
unsigned_types = ['uchar', 'ushort', 'uint', 'ulong']
signed_types = ['char', 'short', 'int', 'long']
float_types = ['float', 'double']
signed_types = ['char', 'schar', 'short', 'int', 'long']
float_types = ['half', 'float', 'double']
int64_types = ['long', 'ulong']
float64_types = ['double']
float16_types = ['half']
vector_sizes = ['', '2', '3', '4', '8', '16']
half_sizes = {'2': '', '4': '2', '8': '4', '16': '8'}
half_sizes = [('2', ''), ('4', '2'), ('8', '4'), ('16', '8')]

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

bool_type = {'char' : 'char',
'uchar' : 'char',
'short' : 'short',
'ushort': 'short',
'int' : 'int',
'uint' : 'int',
'long' : 'long',
'ulong' : 'long',
bool_type = {'char' : 'char',
'schar' : 'schar',
'uchar' : 'schar',
'short' : 'short',
'ushort' : 'short',
'int' : 'int',
'uint' : 'int',
'long' : 'long',
'ulong' : 'long',
'half' : 'short',
'float' : 'int',
'double' : 'long'}

unsigned_type = {'char' : 'uchar',
'schar' : 'uchar',
'uchar' : 'uchar',
'short' : 'ushort',
'ushort': 'ushort',
Expand All @@ -36,13 +40,15 @@
'long' : 'ulong',
'ulong' : 'ulong'}

sizeof_type = {'char' : 1, 'uchar' : 1,
sizeof_type = {'char' : 1, 'schar' : 1, 'uchar' : 1,
'short' : 2, 'ushort' : 2,
'int' : 4, 'uint' : 4,
'long' : 8, 'ulong' : 8,
'float' : 4, 'double' : 8}
'half' : 2, 'float' : 4,
'double': 8}

limit_max = {'char' : 'CHAR_MAX',
'schar' : 'CHAR_MAX',
'uchar' : 'UCHAR_MAX',
'short' : 'SHRT_MAX',
'ushort': 'USHRT_MAX',
Expand All @@ -52,6 +58,7 @@
'ulong' : 'ULONG_MAX'}

limit_min = {'char' : 'CHAR_MIN',
'schar' : 'CHAR_MIN',
'uchar' : '0',
'short' : 'SHRT_MIN',
'ushort': '0',
Expand All @@ -70,55 +77,35 @@ def conditional_guard(src, dst):
"""
int64_count = 0
float64_count = 0
if src in int64_types:
int64_count = int64_count +1
elif src in float64_types:
float64_count = float64_count + 1
if dst in int64_types:
int64_count = int64_count +1
elif dst in float64_types:
float64_count = float64_count + 1
float16_count = 0
if src in int64_types or dst in int64_types:
int64_count = 1
if src in float64_types or dst in float64_types:
float64_count = 1
if src in float16_types or dst in float16_types:
float16_count = 1
if float16_count > 0:
print("#ifdef cl_khr_fp16")
if float64_count > 0:
#In embedded profile, if cl_khr_fp64 is supported cles_khr_int64 has to be
print("#ifdef cl_khr_fp64")
return True
return 1 + float16_count
elif int64_count > 0:
print("#if defined cles_khr_int64 || !defined(__EMBEDDED_PROFILE__)")
return True
return False
return 1 + float16_count
return float16_count



def spirv_fn_name(src, dst, size='', mode='', sat=''):
def close_conditional_guard(close_conditional):
"""
This helper function returns the correct SPIR-V function name for a given source and destination
type, with optional size, mode and saturation arguments.
This function will close conditional guard opened by conditional_guard.
"""
is_src_float = src in float_types
is_src_unsigned = src in unsigned_types
is_src_signed = src in signed_types
is_dst_float = dst in float_types
is_dst_unsigned = dst in unsigned_types
is_dst_signed = dst in signed_types
is_sat = sat != ''
for _ in range(close_conditional):
print("#endif")

if is_src_unsigned and is_dst_signed and is_sat:
return '__spirv_SatConvertUToS_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_signed and is_dst_unsigned and is_sat:
return '__spirv_SatConvertSToU_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_float and is_dst_signed:
return '__spirv_ConvertFToS_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_float and is_dst_unsigned:
return '__spirv_ConvertFToU_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_signed and is_dst_float:
return '__spirv_ConvertSToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_unsigned and is_dst_float:
return '__spirv_ConvertUToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_float and is_dst_float:
return '__spirv_FConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_unsigned and is_dst_unsigned:
return '__spirv_UConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
elif is_src_signed and is_dst_signed:
return '__spirv_SConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode)
else:
return None
def clc_core_fn_name(dst, size='', mode='', sat=''):
"""
This helper function returns the correct clc core conversion function name
for a given source and destination type, with optional size, mode
and saturation arguments.
"""
return "__clc_convert_{DST}{N}{SAT}{MODE}".format(DST=dst, N=size, SAT=sat, MODE=mode)
6 changes: 6 additions & 0 deletions libclc/generic/include/as_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CLC_AS_TYPE

#define as_char(x) __builtin_astype(x, char)
#define as_schar(x) __builtin_astype(x, schar)
#define as_uchar(x) __builtin_astype(x, uchar)
#define as_short(x) __builtin_astype(x, short)
#define as_ushort(x) __builtin_astype(x, ushort)
Expand All @@ -12,6 +13,7 @@
#define as_float(x) __builtin_astype(x, float)

#define as_char2(x) __builtin_astype(x, char2)
#define as_schar2(x) __builtin_astype(x, schar2)
#define as_uchar2(x) __builtin_astype(x, uchar2)
#define as_short2(x) __builtin_astype(x, short2)
#define as_ushort2(x) __builtin_astype(x, ushort2)
Expand All @@ -22,6 +24,7 @@
#define as_float2(x) __builtin_astype(x, float2)

#define as_char3(x) __builtin_astype(x, char3)
#define as_schar3(x) __builtin_astype(x, schar3)
#define as_uchar3(x) __builtin_astype(x, uchar3)
#define as_short3(x) __builtin_astype(x, short3)
#define as_ushort3(x) __builtin_astype(x, ushort3)
Expand All @@ -32,6 +35,7 @@
#define as_float3(x) __builtin_astype(x, float3)

#define as_char4(x) __builtin_astype(x, char4)
#define as_schar4(x) __builtin_astype(x, schar4)
#define as_uchar4(x) __builtin_astype(x, uchar4)
#define as_short4(x) __builtin_astype(x, short4)
#define as_ushort4(x) __builtin_astype(x, ushort4)
Expand All @@ -42,6 +46,7 @@
#define as_float4(x) __builtin_astype(x, float4)

#define as_char8(x) __builtin_astype(x, char8)
#define as_schar8(x) __builtin_astype(x, schar8)
#define as_uchar8(x) __builtin_astype(x, uchar8)
#define as_short8(x) __builtin_astype(x, short8)
#define as_ushort8(x) __builtin_astype(x, ushort8)
Expand All @@ -52,6 +57,7 @@
#define as_float8(x) __builtin_astype(x, float8)

#define as_char16(x) __builtin_astype(x, char16)
#define as_schar16(x) __builtin_astype(x, schar16)
#define as_uchar16(x) __builtin_astype(x, uchar16)
#define as_short16(x) __builtin_astype(x, short16)
#define as_ushort16(x) __builtin_astype(x, ushort16)
Expand Down
2 changes: 2 additions & 0 deletions libclc/generic/include/clc/clc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
#endif

#define __CLC_NO_SCHAR

/* Function Attributes */
#include <func.h>

Expand Down
Loading