|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # SPIR-V built-in library: type conversion functions |
3 | 3 | # |
4 | | -#===----------------------------------------------------------------------===// |
| 4 | +# ===----------------------------------------------------------------------===// |
5 | 5 | # |
6 | 6 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
7 | 7 | # See https://llvm.org/LICENSE.txt for license information. |
8 | 8 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
9 | 9 | # |
10 | | -#===----------------------------------------------------------------------===// |
| 10 | +# ===----------------------------------------------------------------------===// |
11 | 11 | # |
12 | 12 | # This script generates the file convert-spirv.cl, which contains all of the |
13 | 13 | # SPIR-V conversion functions. |
|
17 | 17 | import sys |
18 | 18 |
|
19 | 19 | from os.path import dirname, join, abspath |
20 | | -sys.path.insert(0, abspath(join(dirname(__file__), '..', '..', '..', 'generic'))) |
21 | 20 |
|
22 | | -from gen_convert_common import ( |
23 | | - types, int_types, signed_types, unsigned_types, float_types, int64_types, float64_types, |
24 | | - vector_sizes, half_sizes, saturation, rounding_modes, float_prefix, float_suffix, bool_type, |
25 | | - unsigned_type, sizeof_type, limit_max, limit_min, conditional_guard, close_conditional_guard, |
26 | | - clc_core_fn_name) |
27 | | - |
28 | | -types.remove('char') |
29 | | -int_types.remove('char') |
30 | | -signed_types.remove('char') |
31 | | -rounding_modes = [''] + rounding_modes |
| 21 | +sys.path.insert(0, abspath(join(dirname(__file__), "..", "..", "..", "generic"))) |
32 | 22 |
|
33 | | -print("""/* !!!! AUTOGENERATED FILE generated by convert_type.py !!!!! |
| 23 | +from gen_convert_common import ( |
| 24 | + types, |
| 25 | + int_types, |
| 26 | + signed_types, |
| 27 | + unsigned_types, |
| 28 | + float_types, |
| 29 | + int64_types, |
| 30 | + float64_types, |
| 31 | + vector_sizes, |
| 32 | + half_sizes, |
| 33 | + saturation, |
| 34 | + rounding_modes, |
| 35 | + float_prefix, |
| 36 | + float_suffix, |
| 37 | + bool_type, |
| 38 | + unsigned_type, |
| 39 | + sizeof_type, |
| 40 | + limit_max, |
| 41 | + limit_min, |
| 42 | + conditional_guard, |
| 43 | + close_conditional_guard, |
| 44 | + clc_core_fn_name, |
| 45 | +) |
| 46 | + |
| 47 | +types.remove("char") |
| 48 | +int_types.remove("char") |
| 49 | +signed_types.remove("char") |
| 50 | +rounding_modes = [""] + rounding_modes |
| 51 | + |
| 52 | +print( |
| 53 | + """/* !!!! AUTOGENERATED FILE generated by convert_type.py !!!!! |
34 | 54 |
|
35 | 55 | DON'T CHANGE THIS FILE. MAKE YOUR CHANGES TO convert_type.py AND RUN: |
36 | 56 | $ ./generate-conversion-type-cl.sh |
|
66 | 86 | #pragma OPENCL EXTENSION cles_khr_int64 : enable |
67 | 87 | #endif |
68 | 88 |
|
69 | | -""") |
70 | | - |
71 | | -def spirv_fn_name(src, dst, size='', mode='', sat='', force_sat_decoration=False): |
72 | | - """ |
73 | | - This helper function returns the correct SPIR-V function name for a given source and destination |
74 | | - type, with optional size, mode and saturation arguments. |
75 | | - For saturated, 2 form can co-exist: SatConvertUToS/SatConvertSToU and SConvert/UConvert + _sat. |
76 | | - By default, SatConvert* is emitted, force_decoration will emit the *Convert + _sat. |
77 | | - """ |
78 | | - is_src_float = src in float_types |
79 | | - is_src_unsigned = src in unsigned_types |
80 | | - is_src_signed = src in signed_types |
81 | | - is_dst_float = dst in float_types |
82 | | - is_dst_unsigned = dst in unsigned_types |
83 | | - is_dst_signed = dst in signed_types |
84 | | - use_sat_insn = sat != '' and not force_sat_decoration |
85 | | - |
86 | | - if dst == "schar": |
87 | | - dst = "char" |
88 | | - |
89 | | - if is_src_unsigned and is_dst_signed and use_sat_insn: |
90 | | - return '__spirv_SatConvertUToS_R{DST}{N}'.format(DST=dst, N=size) |
91 | | - elif is_src_signed and is_dst_unsigned and use_sat_insn: |
92 | | - return '__spirv_SatConvertSToU_R{DST}{N}'.format(DST=dst, N=size) |
93 | | - elif is_src_float and is_dst_signed: |
94 | | - return '__spirv_ConvertFToS_R{DST}{N}{SAT}{MODE}'.format(DST=dst, N=size, SAT=sat, MODE=mode) |
95 | | - elif is_src_float and is_dst_unsigned: |
96 | | - return '__spirv_ConvertFToU_R{DST}{N}{SAT}{MODE}'.format(DST=dst, N=size, SAT=sat, MODE=mode) |
97 | | - elif is_src_signed and is_dst_float: |
98 | | - return '__spirv_ConvertSToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode) |
99 | | - elif is_src_unsigned and is_dst_float: |
100 | | - return '__spirv_ConvertUToF_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode) |
101 | | - elif is_src_float and is_dst_float: |
102 | | - return '__spirv_FConvert_R{DST}{N}{MODE}'.format(DST=dst, N=size, MODE=mode) |
103 | | - elif is_dst_unsigned: |
104 | | - return '__spirv_UConvert_R{DST}{N}{SAT}'.format(DST=dst, N=size, SAT=sat) |
105 | | - elif is_dst_signed: |
106 | | - return '__spirv_SConvert_R{DST}{N}{SAT}'.format(DST=dst, N=size, SAT=sat) |
107 | | - sys.stderr.write("Unhandled param set: {}, {}, {}, {}, {}\n".format(src, dst, size, mode, sat)) |
108 | | - assert(False) |
| 89 | +""" |
| 90 | +) |
| 91 | + |
| 92 | + |
| 93 | +def spirv_fn_name(src, dst, size="", mode="", sat="", force_sat_decoration=False): |
| 94 | + """ |
| 95 | + This helper function returns the correct SPIR-V function name for a given source and destination |
| 96 | + type, with optional size, mode and saturation arguments. |
| 97 | + For saturated, 2 form can co-exist: SatConvertUToS/SatConvertSToU and SConvert/UConvert + _sat. |
| 98 | + By default, SatConvert* is emitted, force_decoration will emit the *Convert + _sat. |
| 99 | + """ |
| 100 | + is_src_float = src in float_types |
| 101 | + is_src_unsigned = src in unsigned_types |
| 102 | + is_src_signed = src in signed_types |
| 103 | + is_dst_float = dst in float_types |
| 104 | + is_dst_unsigned = dst in unsigned_types |
| 105 | + is_dst_signed = dst in signed_types |
| 106 | + use_sat_insn = sat != "" and not force_sat_decoration |
| 107 | + |
| 108 | + if dst == "schar": |
| 109 | + dst = "char" |
| 110 | + |
| 111 | + if is_src_unsigned and is_dst_signed and use_sat_insn: |
| 112 | + return "__spirv_SatConvertUToS_R{DST}{N}".format(DST=dst, N=size) |
| 113 | + elif is_src_signed and is_dst_unsigned and use_sat_insn: |
| 114 | + return "__spirv_SatConvertSToU_R{DST}{N}".format(DST=dst, N=size) |
| 115 | + elif is_src_float and is_dst_signed: |
| 116 | + return "__spirv_ConvertFToS_R{DST}{N}{SAT}{MODE}".format( |
| 117 | + DST=dst, N=size, SAT=sat, MODE=mode |
| 118 | + ) |
| 119 | + elif is_src_float and is_dst_unsigned: |
| 120 | + return "__spirv_ConvertFToU_R{DST}{N}{SAT}{MODE}".format( |
| 121 | + DST=dst, N=size, SAT=sat, MODE=mode |
| 122 | + ) |
| 123 | + elif is_src_signed and is_dst_float: |
| 124 | + return "__spirv_ConvertSToF_R{DST}{N}{MODE}".format(DST=dst, N=size, MODE=mode) |
| 125 | + elif is_src_unsigned and is_dst_float: |
| 126 | + return "__spirv_ConvertUToF_R{DST}{N}{MODE}".format(DST=dst, N=size, MODE=mode) |
| 127 | + elif is_src_float and is_dst_float: |
| 128 | + return "__spirv_FConvert_R{DST}{N}{MODE}".format(DST=dst, N=size, MODE=mode) |
| 129 | + elif is_dst_unsigned: |
| 130 | + return "__spirv_UConvert_R{DST}{N}{SAT}".format(DST=dst, N=size, SAT=sat) |
| 131 | + elif is_dst_signed: |
| 132 | + return "__spirv_SConvert_R{DST}{N}{SAT}".format(DST=dst, N=size, SAT=sat) |
| 133 | + sys.stderr.write( |
| 134 | + "Unhandled param set: {}, {}, {}, {}, {}\n".format(src, dst, size, mode, sat) |
| 135 | + ) |
| 136 | + assert False |
| 137 | + |
109 | 138 |
|
110 | 139 | def is_same_size(src, dst): |
111 | | - return sizeof_type[src] == sizeof_type[dst] |
| 140 | + return sizeof_type[src] == sizeof_type[dst] |
| 141 | + |
112 | 142 |
|
113 | 143 | def is_signed_unsigned_conversion(src, dst): |
114 | | - return (src in unsigned_types and dst in signed_types) or (src in signed_types and dst in unsigned_types) |
| 144 | + return (src in unsigned_types and dst in signed_types) or ( |
| 145 | + src in signed_types and dst in unsigned_types |
| 146 | + ) |
| 147 | + |
115 | 148 |
|
116 | | -def generate_spirv_fn_impl(src, dst, size='', mode='', sat='', force_decoration=False): |
117 | | - close_conditional = conditional_guard(src, dst) |
| 149 | +def generate_spirv_fn_impl(src, dst, size="", mode="", sat="", force_decoration=False): |
| 150 | + close_conditional = conditional_guard(src, dst) |
118 | 151 |
|
119 | | - print("""_CLC_DEF _CLC_OVERLOAD _CLC_CONSTFN |
| 152 | + print( |
| 153 | + """_CLC_DEF _CLC_OVERLOAD _CLC_CONSTFN |
120 | 154 | {DST}{N} {FN}({SRC}{N} x) |
121 | 155 | {{ |
122 | 156 | return {CORE_FN}(x); |
123 | 157 | }} |
124 | | -""".format(FN=spirv_fn_name(src, dst, size=size, sat=sat, mode=mode, force_sat_decoration=force_decoration), |
125 | | - CORE_FN=clc_core_fn_name(dst, size=size, sat=sat, mode=mode), |
126 | | - SRC=src, DST=dst, N=size)) |
127 | | - |
128 | | - close_conditional_guard(close_conditional) |
| 158 | +""".format( |
| 159 | + FN=spirv_fn_name( |
| 160 | + src, |
| 161 | + dst, |
| 162 | + size=size, |
| 163 | + sat=sat, |
| 164 | + mode=mode, |
| 165 | + force_sat_decoration=force_decoration, |
| 166 | + ), |
| 167 | + CORE_FN=clc_core_fn_name(dst, size=size, sat=sat, mode=mode), |
| 168 | + SRC=src, |
| 169 | + DST=dst, |
| 170 | + N=size, |
| 171 | + ) |
| 172 | + ) |
| 173 | + |
| 174 | + close_conditional_guard(close_conditional) |
| 175 | + |
| 176 | + |
| 177 | +def generate_spirv_fn(src, dst, size="", mode="", sat=""): |
| 178 | + generate_spirv_fn_impl( |
| 179 | + src, dst, size=size, mode=mode, sat=sat, force_decoration=False |
| 180 | + ) |
| 181 | + # There is an alias for saturated conversion |
| 182 | + # if signed to unsigned or unsigned to signed conversion |
| 183 | + # and if the componant types are not equals |
| 184 | + if ( |
| 185 | + sat != "" |
| 186 | + and is_signed_unsigned_conversion(src, dst) |
| 187 | + and not is_same_size(src, dst) |
| 188 | + ): |
| 189 | + generate_spirv_fn_impl( |
| 190 | + src, dst, size=size, mode=mode, sat=sat, force_decoration=True |
| 191 | + ) |
129 | 192 |
|
130 | | -def generate_spirv_fn(src, dst, size='', mode='', sat=''): |
131 | | - generate_spirv_fn_impl(src, dst, size=size, mode=mode, sat=sat, force_decoration=False) |
132 | | - # There is an alias for saturated conversion |
133 | | - # if signed to unsigned or unsigned to signed conversion |
134 | | - # and if the componant types are not equals |
135 | | - if sat != '' and is_signed_unsigned_conversion(src, dst) and not is_same_size(src, dst): |
136 | | - generate_spirv_fn_impl(src, dst, size=size, mode=mode, sat=sat, force_decoration=True) |
137 | 193 |
|
138 | 194 | # __spirv_ConvertFToU /__spirv_ConvertFToS + sat + mode |
139 | 195 | for src in float_types: |
140 | | - for dst in int_types: |
141 | | - for size in vector_sizes: |
142 | | - for mode in rounding_modes: |
143 | | - for sat in saturation: |
144 | | - generate_spirv_fn(src, dst, size, mode, sat) |
| 196 | + for dst in int_types: |
| 197 | + for size in vector_sizes: |
| 198 | + for mode in rounding_modes: |
| 199 | + for sat in saturation: |
| 200 | + generate_spirv_fn(src, dst, size, mode, sat) |
145 | 201 |
|
146 | 202 | # __spirv_ConvertUToF / __spirv_ConvertSToF + mode |
147 | 203 | for src in int_types: |
148 | | - for dst in float_types: |
149 | | - for size in vector_sizes: |
150 | | - for mode in rounding_modes: |
151 | | - generate_spirv_fn(src, dst, size, mode) |
| 204 | + for dst in float_types: |
| 205 | + for size in vector_sizes: |
| 206 | + for mode in rounding_modes: |
| 207 | + generate_spirv_fn(src, dst, size, mode) |
152 | 208 |
|
153 | 209 | # __spirv_FConvert + mode |
154 | 210 | for src in float_types: |
155 | | - for dst in float_types: |
156 | | - for size in vector_sizes: |
157 | | - for mode in rounding_modes: |
158 | | - generate_spirv_fn(src, dst, size, mode) |
| 211 | + for dst in float_types: |
| 212 | + for size in vector_sizes: |
| 213 | + for mode in rounding_modes: |
| 214 | + generate_spirv_fn(src, dst, size, mode) |
159 | 215 |
|
160 | 216 | # __spirv_UConvert + sat |
161 | 217 | for src in int_types: |
162 | | - for dst in unsigned_types: |
163 | | - for size in vector_sizes: |
164 | | - for sat in saturation: |
165 | | - generate_spirv_fn(src, dst, size, sat=sat) |
| 218 | + for dst in unsigned_types: |
| 219 | + for size in vector_sizes: |
| 220 | + for sat in saturation: |
| 221 | + generate_spirv_fn(src, dst, size, sat=sat) |
166 | 222 |
|
167 | 223 | # __spirv_SConvert + sat |
168 | 224 | for src in int_types: |
169 | | - for dst in signed_types: |
170 | | - for size in vector_sizes: |
171 | | - for sat in saturation: |
172 | | - generate_spirv_fn(src, dst, size, sat=sat) |
| 225 | + for dst in signed_types: |
| 226 | + for size in vector_sizes: |
| 227 | + for sat in saturation: |
| 228 | + generate_spirv_fn(src, dst, size, sat=sat) |
0 commit comments