| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "vulkan/vulkan_core.h" |
| 8 |
#include "msl_private.h" |
| 9 |
|
| 10 |
typedef enum ti_type { |
| 11 |
|
| 12 |
TYPE_NONE = 0, |
| 13 |
|
| 14 |
|
| 15 |
TYPE_GENERIC_DATA, |
| 16 |
|
| 17 |
TYPE_GENERIC_INT_OR_BOOL, |
| 18 |
|
| 19 |
TYPE_GENERIC_INT, |
| 20 |
|
| 21 |
TYPE_INT, |
| 22 |
TYPE_UINT, |
| 23 |
TYPE_BOOL, |
| 24 |
TYPE_FLOAT, |
| 25 |
TYPE_SAMPLER, |
| 26 |
} ti_type; |
| 27 |
|
| 28 |
static ti_type |
| 29 |
unify_types(ti_type t1, ti_type t2) |
| 30 |
{ |
| 31 |
ti_type generic = MIN2(t1, t2); |
| 32 |
ti_type specific = MAX2(t1, t2); |
| 33 |
if (t1 == t2) |
| 34 |
return TYPE_NONE; |
| 35 |
|
| 36 |
if (generic == TYPE_GENERIC_DATA || generic == TYPE_NONE) |
| 37 |
return specific; |
| 38 |
if ((generic == TYPE_GENERIC_INT_OR_BOOL) && |
| 39 |
((specific == TYPE_INT) || (specific == TYPE_UINT) || |
| 40 |
(specific == TYPE_BOOL))) |
| 41 |
return specific; |
| 42 |
if ((generic == TYPE_GENERIC_INT) && |
| 43 |
((specific == TYPE_INT) || (specific == TYPE_UINT))) |
| 44 |
return specific; |
| 45 |
return TYPE_NONE; |
| 46 |
} |
| 47 |
|
| 48 |
static ti_type |
| 49 |
ti_type_from_nir(nir_alu_type nir_type) |
| 50 |
{ |
| 51 |
switch (nir_alu_type_get_base_type(nir_type)) { |
| 52 |
case nir_type_int: |
| 53 |
return TYPE_INT; |
| 54 |
case nir_type_uint: |
| 55 |
return TYPE_UINT; |
| 56 |
case nir_type_float: |
| 57 |
return TYPE_FLOAT; |
| 58 |
case nir_type_bool: |
| 59 |
return TYPE_BOOL; |
| 60 |
default: |
| 61 |
assert(0); |
| 62 |
return TYPE_NONE; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static ti_type |
| 67 |
ti_type_from_pipe_format(enum pipe_format format) |
| 68 |
{ |
| 69 |
switch (format) { |
| 70 |
case PIPE_FORMAT_R16_FLOAT: |
| 71 |
case PIPE_FORMAT_R32_FLOAT: |
| 72 |
return TYPE_FLOAT; |
| 73 |
case PIPE_FORMAT_R8_UINT: |
| 74 |
case PIPE_FORMAT_R16_UINT: |
| 75 |
case PIPE_FORMAT_R32_UINT: |
| 76 |
case PIPE_FORMAT_R64_UINT: |
| 77 |
return TYPE_UINT; |
| 78 |
case PIPE_FORMAT_R8_SINT: |
| 79 |
case PIPE_FORMAT_R16_SINT: |
| 80 |
case PIPE_FORMAT_R32_SINT: |
| 81 |
case PIPE_FORMAT_R64_SINT: |
| 82 |
return TYPE_INT; |
| 83 |
default: |
| 84 |
assert(0); |
| 85 |
return 0u; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
set_type(struct hash_table *types, void *key, ti_type type) |
| 91 |
{ |
| 92 |
|
| 93 |
_mesa_hash_table_insert(types, key, (void *)type); |
| 94 |
} |
| 95 |
|
| 96 |
static ti_type |
| 97 |
get_type(struct hash_table *types, void *key) |
| 98 |
{ |
| 99 |
struct hash_entry *entry = _mesa_hash_table_search(types, key); |
| 100 |
if (!entry) |
| 101 |
return TYPE_NONE; |
| 102 |
return (ti_type)(intptr_t)(entry->data); |
| 103 |
} |
| 104 |
|
| 105 |
static bool |
| 106 |
update_instr_type(struct hash_table *types, nir_instr *instr, ti_type type) |
| 107 |
{ |
| 108 |
if (instr->type == nir_instr_type_alu) { |
| 109 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 110 |
switch (alu->op) { |
| 111 |
case nir_op_iadd: |
| 112 |
case nir_op_isub: |
| 113 |
case nir_op_ishl: |
| 114 |
case nir_op_iand: |
| 115 |
case nir_op_ior: |
| 116 |
case nir_op_ixor: |
| 117 |
set_type(types, &alu->def, type); |
| 118 |
set_type(types, &alu->src[0].src, type); |
| 119 |
set_type(types, &alu->src[1].src, type); |
| 120 |
return true; |
| 121 |
case nir_op_inot: |
| 122 |
set_type(types, &alu->def, type); |
| 123 |
set_type(types, &alu->src[0].src, type); |
| 124 |
return true; |
| 125 |
case nir_op_ieq: |
| 126 |
case nir_op_ine: |
| 127 |
set_type(types, &alu->src[0].src, type); |
| 128 |
set_type(types, &alu->src[1].src, type); |
| 129 |
return true; |
| 130 |
case nir_op_bcsel: |
| 131 |
set_type(types, &alu->def, type); |
| 132 |
set_type(types, &alu->src[1].src, type); |
| 133 |
set_type(types, &alu->src[2].src, type); |
| 134 |
return true; |
| 135 |
case nir_op_mov: |
| 136 |
case nir_op_vec2: |
| 137 |
case nir_op_vec3: |
| 138 |
case nir_op_vec4: |
| 139 |
set_type(types, &alu->def, type); |
| 140 |
for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) |
| 141 |
set_type(types, &alu->src[i].src, type); |
| 142 |
return true; |
| 143 |
default: |
| 144 |
return false; |
| 145 |
} |
| 146 |
} else if (instr->type == nir_instr_type_intrinsic) { |
| 147 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 148 |
nir_intrinsic_info info = nir_intrinsic_infos[intr->intrinsic]; |
| 149 |
switch (intr->intrinsic) { |
| 150 |
case nir_intrinsic_load_reg: |
| 151 |
set_type(types, &intr->def, type); |
| 152 |
set_type(types, &intr->src[0], type); |
| 153 |
return true; |
| 154 |
case nir_intrinsic_store_reg: |
| 155 |
set_type(types, &intr->src[0], type); |
| 156 |
set_type(types, &intr->src[1], type); |
| 157 |
return true; |
| 158 |
case nir_intrinsic_decl_reg: |
| 159 |
set_type(types, &intr->def, type); |
| 160 |
return true; |
| 161 |
case nir_intrinsic_load_global: |
| 162 |
case nir_intrinsic_load_global_constant: |
| 163 |
case nir_intrinsic_load_global_constant_bounded: |
| 164 |
case nir_intrinsic_load_global_constant_offset: |
| 165 |
case nir_intrinsic_load_push_constant: |
| 166 |
set_type(types, &intr->def, type); |
| 167 |
return true; |
| 168 |
|
| 169 |
case nir_intrinsic_load_scratch: |
| 170 |
case nir_intrinsic_store_scratch: |
| 171 |
case nir_intrinsic_load_shared: |
| 172 |
case nir_intrinsic_store_shared: |
| 173 |
case nir_intrinsic_load_frag_coord: |
| 174 |
return false; |
| 175 |
case nir_intrinsic_store_global: |
| 176 |
set_type(types, &intr->src[0], type); |
| 177 |
return true; |
| 178 |
case nir_intrinsic_read_first_invocation: |
| 179 |
case nir_intrinsic_read_invocation: |
| 180 |
case nir_intrinsic_quad_broadcast: |
| 181 |
case nir_intrinsic_quad_swap_horizontal: |
| 182 |
case nir_intrinsic_quad_swap_vertical: |
| 183 |
case nir_intrinsic_quad_swap_diagonal: |
| 184 |
case nir_intrinsic_shuffle: |
| 185 |
case nir_intrinsic_shuffle_down: |
| 186 |
case nir_intrinsic_shuffle_up: |
| 187 |
case nir_intrinsic_shuffle_xor: |
| 188 |
set_type(types, &intr->src[0], type); |
| 189 |
set_type(types, &intr->def, type); |
| 190 |
return true; |
| 191 |
default: |
| 192 |
if (info.has_dest && info.num_srcs == 0) { |
| 193 |
set_type(types, &intr->def, type); |
| 194 |
return true; |
| 195 |
} |
| 196 |
return false; |
| 197 |
} |
| 198 |
} else |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
infer_types_from_alu(struct hash_table *types, nir_alu_instr *alu) |
| 204 |
{ |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
switch (alu->op) { |
| 209 |
case nir_op_iadd: |
| 210 |
case nir_op_isub: |
| 211 |
case nir_op_ishl: |
| 212 |
|
| 213 |
set_type(types, &alu->def, TYPE_GENERIC_INT); |
| 214 |
set_type(types, &alu->src[0].src, TYPE_GENERIC_INT); |
| 215 |
set_type(types, &alu->src[1].src, TYPE_GENERIC_INT); |
| 216 |
break; |
| 217 |
case nir_op_iand: |
| 218 |
case nir_op_ior: |
| 219 |
case nir_op_ixor: |
| 220 |
set_type(types, &alu->def, TYPE_GENERIC_INT_OR_BOOL); |
| 221 |
set_type(types, &alu->src[0].src, TYPE_GENERIC_INT_OR_BOOL); |
| 222 |
set_type(types, &alu->src[1].src, TYPE_GENERIC_INT_OR_BOOL); |
| 223 |
break; |
| 224 |
case nir_op_inot: |
| 225 |
|
| 226 |
set_type(types, &alu->def, TYPE_GENERIC_INT_OR_BOOL); |
| 227 |
set_type(types, &alu->src[0].src, TYPE_GENERIC_INT_OR_BOOL); |
| 228 |
break; |
| 229 |
case nir_op_ieq: |
| 230 |
case nir_op_ine: |
| 231 |
|
| 232 |
set_type(types, &alu->def, TYPE_BOOL); |
| 233 |
set_type(types, &alu->src[0].src, TYPE_GENERIC_INT_OR_BOOL); |
| 234 |
set_type(types, &alu->src[1].src, TYPE_GENERIC_INT_OR_BOOL); |
| 235 |
break; |
| 236 |
case nir_op_bcsel: |
| 237 |
|
| 238 |
set_type(types, &alu->def, TYPE_GENERIC_DATA); |
| 239 |
set_type(types, &alu->src[0].src, TYPE_BOOL); |
| 240 |
set_type(types, &alu->src[1].src, TYPE_GENERIC_DATA); |
| 241 |
set_type(types, &alu->src[2].src, TYPE_GENERIC_DATA); |
| 242 |
break; |
| 243 |
|
| 244 |
|
| 245 |
case nir_op_mov: |
| 246 |
case nir_op_vec2: |
| 247 |
case nir_op_vec3: |
| 248 |
case nir_op_vec4: |
| 249 |
break; |
| 250 |
|
| 251 |
case nir_op_b2b32: |
| 252 |
set_type(types, &alu->def, TYPE_UINT); |
| 253 |
set_type(types, &alu->src[0].src, TYPE_UINT); |
| 254 |
break; |
| 255 |
|
| 256 |
default: { |
| 257 |
|
| 258 |
const nir_op_info *info = &nir_op_infos[alu->op]; |
| 259 |
set_type(types, &alu->def, ti_type_from_nir(info->output_type)); |
| 260 |
for (int i = 0; i < info->num_inputs; i++) { |
| 261 |
|
| 262 |
set_type(types, &alu->src[i].src, |
| 263 |
ti_type_from_nir(info->input_types[i])); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
infer_types_from_intrinsic(struct hash_table *types, nir_intrinsic_instr *instr) |
| 271 |
{ |
| 272 |
switch (instr->intrinsic) { |
| 273 |
case nir_intrinsic_load_input: |
| 274 |
case nir_intrinsic_load_interpolated_input: |
| 275 |
case nir_intrinsic_load_output: { |
| 276 |
ti_type ty = ti_type_from_nir(nir_intrinsic_dest_type(instr)); |
| 277 |
set_type(types, &instr->def, ty); |
| 278 |
break; |
| 279 |
} |
| 280 |
case nir_intrinsic_load_sample_pos_from_id: |
| 281 |
set_type(types, &instr->def, TYPE_FLOAT); |
| 282 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 283 |
break; |
| 284 |
case nir_intrinsic_load_global_constant: |
| 285 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 286 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 287 |
break; |
| 288 |
case nir_intrinsic_load_global_constant_bounded: |
| 289 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 290 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 291 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 292 |
set_type(types, &instr->src[2], TYPE_UINT); |
| 293 |
break; |
| 294 |
case nir_intrinsic_load_global_constant_offset: |
| 295 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 296 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 297 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 298 |
break; |
| 299 |
case nir_intrinsic_load_global: |
| 300 |
case nir_intrinsic_load_push_constant: |
| 301 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 302 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 303 |
break; |
| 304 |
|
| 305 |
case nir_intrinsic_global_atomic: |
| 306 |
case nir_intrinsic_global_atomic_swap: |
| 307 |
case nir_intrinsic_shared_atomic: |
| 308 |
case nir_intrinsic_shared_atomic_swap: { |
| 309 |
ti_type type = |
| 310 |
ti_type_from_nir(nir_atomic_op_type(nir_intrinsic_atomic_op(instr))); |
| 311 |
set_type(types, &instr->def, type); |
| 312 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 313 |
set_type(types, &instr->src[1], type); |
| 314 |
set_type(types, &instr->src[2], type); |
| 315 |
break; |
| 316 |
} |
| 317 |
case nir_intrinsic_store_global: |
| 318 |
set_type(types, &instr->src[0], TYPE_GENERIC_DATA); |
| 319 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 320 |
break; |
| 321 |
case nir_intrinsic_store_output: { |
| 322 |
ti_type ty = ti_type_from_nir(nir_intrinsic_src_type(instr)); |
| 323 |
set_type(types, &instr->src[0], ty); |
| 324 |
break; |
| 325 |
} |
| 326 |
case nir_intrinsic_decl_reg: |
| 327 |
if (nir_intrinsic_bit_size(instr) == 1) |
| 328 |
set_type(types, &instr->def, TYPE_BOOL); |
| 329 |
else |
| 330 |
set_type(types, &instr->def, TYPE_NONE); |
| 331 |
break; |
| 332 |
case nir_intrinsic_store_reg: |
| 333 |
set_type(types, &instr->src[0], TYPE_NONE); |
| 334 |
set_type(types, &instr->src[1], TYPE_NONE); |
| 335 |
break; |
| 336 |
case nir_intrinsic_load_reg: |
| 337 |
set_type(types, &instr->src[0], TYPE_NONE); |
| 338 |
set_type(types, &instr->def, TYPE_NONE); |
| 339 |
break; |
| 340 |
case nir_intrinsic_load_scratch: |
| 341 |
case nir_intrinsic_load_shared: |
| 342 |
set_type(types, &instr->def, TYPE_UINT); |
| 343 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 344 |
break; |
| 345 |
case nir_intrinsic_store_scratch: |
| 346 |
case nir_intrinsic_store_shared: |
| 347 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 348 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 349 |
break; |
| 350 |
case nir_intrinsic_load_workgroup_id: |
| 351 |
case nir_intrinsic_load_subgroup_id: |
| 352 |
case nir_intrinsic_load_local_invocation_id: |
| 353 |
case nir_intrinsic_load_global_invocation_id: |
| 354 |
case nir_intrinsic_load_num_workgroups: |
| 355 |
case nir_intrinsic_load_num_subgroups: |
| 356 |
case nir_intrinsic_load_subgroup_size: |
| 357 |
case nir_intrinsic_load_sample_id: |
| 358 |
case nir_intrinsic_load_sample_mask: |
| 359 |
case nir_intrinsic_load_subgroup_invocation: |
| 360 |
case nir_intrinsic_load_amplification_id_kk: |
| 361 |
set_type(types, &instr->def, TYPE_UINT); |
| 362 |
break; |
| 363 |
case nir_intrinsic_load_vulkan_descriptor: |
| 364 |
case nir_intrinsic_load_barycentric_at_sample: |
| 365 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 366 |
set_type(types, &instr->def, TYPE_UINT); |
| 367 |
break; |
| 368 |
case nir_intrinsic_load_buffer_ptr_kk: |
| 369 |
set_type(types, &instr->def, TYPE_UINT); |
| 370 |
break; |
| 371 |
|
| 372 |
|
| 373 |
case nir_intrinsic_load_texture_handle_kk: |
| 374 |
case nir_intrinsic_load_depth_texture_kk: |
| 375 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 376 |
break; |
| 377 |
case nir_intrinsic_load_sampler_handle_kk: |
| 378 |
set_type(types, &instr->def, TYPE_SAMPLER); |
| 379 |
break; |
| 380 |
case nir_intrinsic_store_clip_distance_kk: |
| 381 |
set_type(types, &instr->src[0], TYPE_FLOAT); |
| 382 |
break; |
| 383 |
case nir_intrinsic_ddx: |
| 384 |
case nir_intrinsic_ddy: |
| 385 |
case nir_intrinsic_ddx_coarse: |
| 386 |
case nir_intrinsic_ddy_coarse: |
| 387 |
case nir_intrinsic_ddx_fine: |
| 388 |
case nir_intrinsic_ddy_fine: |
| 389 |
case nir_intrinsic_load_barycentric_at_offset: |
| 390 |
set_type(types, &instr->src[0], TYPE_FLOAT); |
| 391 |
set_type(types, &instr->def, TYPE_FLOAT); |
| 392 |
break; |
| 393 |
case nir_intrinsic_load_point_coord: |
| 394 |
case nir_intrinsic_load_sample_pos: |
| 395 |
case nir_intrinsic_load_frag_coord: |
| 396 |
set_type(types, &instr->def, TYPE_FLOAT); |
| 397 |
break; |
| 398 |
case nir_intrinsic_load_front_face: |
| 399 |
case nir_intrinsic_elect: |
| 400 |
case nir_intrinsic_load_helper_invocation: |
| 401 |
case nir_intrinsic_is_helper_invocation: |
| 402 |
set_type(types, &instr->def, TYPE_BOOL); |
| 403 |
break; |
| 404 |
case nir_intrinsic_load_constant_agx: |
| 405 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 406 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 407 |
set_type(types, &instr->def, |
| 408 |
ti_type_from_pipe_format(nir_intrinsic_format(instr))); |
| 409 |
break; |
| 410 |
case nir_intrinsic_bindless_image_load: |
| 411 |
set_type(types, &instr->def, |
| 412 |
ti_type_from_nir(nir_intrinsic_dest_type(instr))); |
| 413 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 414 |
set_type(types, &instr->src[3], TYPE_UINT); |
| 415 |
break; |
| 416 |
case nir_intrinsic_bindless_image_store: |
| 417 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 418 |
set_type(types, &instr->src[3], |
| 419 |
ti_type_from_nir(nir_intrinsic_src_type(instr))); |
| 420 |
set_type(types, &instr->src[4], TYPE_UINT); |
| 421 |
break; |
| 422 |
case nir_intrinsic_demote_if: |
| 423 |
case nir_intrinsic_terminate_if: |
| 424 |
set_type(types, &instr->src[0], TYPE_BOOL); |
| 425 |
break; |
| 426 |
case nir_intrinsic_bindless_image_atomic: |
| 427 |
case nir_intrinsic_bindless_image_atomic_swap: { |
| 428 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 429 |
set_type(types, &instr->src[2], TYPE_UINT); |
| 430 |
ti_type type = |
| 431 |
ti_type_from_nir(nir_atomic_op_type(nir_intrinsic_atomic_op(instr))); |
| 432 |
set_type(types, &instr->src[3], type); |
| 433 |
if (instr->intrinsic == nir_intrinsic_bindless_image_atomic_swap) |
| 434 |
set_type(types, &instr->src[4], type); |
| 435 |
set_type(types, &instr->def, type); |
| 436 |
break; |
| 437 |
} |
| 438 |
case nir_intrinsic_ballot: |
| 439 |
set_type(types, &instr->src[0], TYPE_BOOL); |
| 440 |
set_type(types, &instr->def, TYPE_UINT); |
| 441 |
break; |
| 442 |
case nir_intrinsic_vote_all: |
| 443 |
case nir_intrinsic_vote_any: |
| 444 |
set_type(types, &instr->src[0], TYPE_BOOL); |
| 445 |
set_type(types, &instr->def, TYPE_BOOL); |
| 446 |
break; |
| 447 |
case nir_intrinsic_read_first_invocation: |
| 448 |
case nir_intrinsic_quad_swap_horizontal: |
| 449 |
case nir_intrinsic_quad_swap_vertical: |
| 450 |
case nir_intrinsic_quad_swap_diagonal: |
| 451 |
set_type(types, &instr->src[0], TYPE_GENERIC_DATA); |
| 452 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 453 |
break; |
| 454 |
case nir_intrinsic_read_invocation: |
| 455 |
case nir_intrinsic_quad_broadcast: |
| 456 |
case nir_intrinsic_shuffle: |
| 457 |
case nir_intrinsic_shuffle_down: |
| 458 |
case nir_intrinsic_shuffle_up: |
| 459 |
case nir_intrinsic_shuffle_xor: |
| 460 |
set_type(types, &instr->src[0], TYPE_GENERIC_DATA); |
| 461 |
set_type(types, &instr->def, TYPE_GENERIC_DATA); |
| 462 |
set_type(types, &instr->src[1], TYPE_UINT); |
| 463 |
break; |
| 464 |
case nir_intrinsic_reduce: |
| 465 |
switch (nir_intrinsic_reduction_op(instr)) { |
| 466 |
case nir_op_iand: |
| 467 |
case nir_op_ior: |
| 468 |
case nir_op_ixor: |
| 469 |
case nir_op_iadd: |
| 470 |
case nir_op_imul: |
| 471 |
set_type(types, &instr->src[0], TYPE_GENERIC_INT); |
| 472 |
set_type(types, &instr->def, TYPE_GENERIC_INT); |
| 473 |
break; |
| 474 |
case nir_op_imax: |
| 475 |
case nir_op_imin: |
| 476 |
set_type(types, &instr->src[0], TYPE_INT); |
| 477 |
set_type(types, &instr->def, TYPE_INT); |
| 478 |
break; |
| 479 |
case nir_op_umax: |
| 480 |
case nir_op_umin: |
| 481 |
set_type(types, &instr->src[0], TYPE_UINT); |
| 482 |
set_type(types, &instr->def, TYPE_UINT); |
| 483 |
break; |
| 484 |
case nir_op_fadd: |
| 485 |
case nir_op_fmax: |
| 486 |
case nir_op_fmin: |
| 487 |
case nir_op_fmul: |
| 488 |
set_type(types, &instr->src[0], TYPE_FLOAT); |
| 489 |
set_type(types, &instr->def, TYPE_FLOAT); |
| 490 |
break; |
| 491 |
default: |
| 492 |
break; |
| 493 |
} |
| 494 |
break; |
| 495 |
default: |
| 496 |
break; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
static void |
| 501 |
infer_types_from_tex(struct hash_table *types, nir_tex_instr *tex) |
| 502 |
{ |
| 503 |
set_type(types, &tex->def, ti_type_from_nir(tex->dest_type)); |
| 504 |
for (int i = 0; i < tex->num_srcs; i++) { |
| 505 |
nir_src *src = &tex->src[i].src; |
| 506 |
switch (tex->src[i].src_type) { |
| 507 |
case nir_tex_src_coord: |
| 508 |
if (tex->op == nir_texop_txf || tex->op == nir_texop_txf_ms) |
| 509 |
set_type(types, src, TYPE_UINT); |
| 510 |
else |
| 511 |
set_type(types, src, TYPE_FLOAT); |
| 512 |
break; |
| 513 |
case nir_tex_src_comparator: |
| 514 |
set_type(types, src, TYPE_FLOAT); |
| 515 |
break; |
| 516 |
case nir_tex_src_offset: |
| 517 |
set_type(types, src, TYPE_INT); |
| 518 |
break; |
| 519 |
case nir_tex_src_bias: |
| 520 |
set_type(types, src, TYPE_FLOAT); |
| 521 |
break; |
| 522 |
case nir_tex_src_lod: |
| 523 |
if (tex->op == nir_texop_txf || tex->op == nir_texop_txf_ms || |
| 524 |
tex->op == nir_texop_txs) |
| 525 |
set_type(types, src, TYPE_UINT); |
| 526 |
else |
| 527 |
set_type(types, src, TYPE_FLOAT); |
| 528 |
break; |
| 529 |
case nir_tex_src_min_lod: |
| 530 |
set_type(types, src, TYPE_FLOAT); |
| 531 |
break; |
| 532 |
case nir_tex_src_ms_index: |
| 533 |
set_type(types, src, TYPE_UINT); |
| 534 |
break; |
| 535 |
case nir_tex_src_ddx: |
| 536 |
case nir_tex_src_ddy: |
| 537 |
set_type(types, src, TYPE_FLOAT); |
| 538 |
break; |
| 539 |
default: |
| 540 |
break; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
static void |
| 546 |
infer_types_from_instr(struct hash_table *types, nir_instr *instr) |
| 547 |
{ |
| 548 |
switch (instr->type) { |
| 549 |
case nir_instr_type_alu: |
| 550 |
infer_types_from_alu(types, nir_instr_as_alu(instr)); |
| 551 |
return; |
| 552 |
case nir_instr_type_intrinsic: |
| 553 |
infer_types_from_intrinsic(types, nir_instr_as_intrinsic(instr)); |
| 554 |
return; |
| 555 |
case nir_instr_type_tex: |
| 556 |
infer_types_from_tex(types, nir_instr_as_tex(instr)); |
| 557 |
break; |
| 558 |
default: |
| 559 |
break; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
static bool |
| 564 |
propagate_types(struct hash_table *types, nir_instr *instr) |
| 565 |
{ |
| 566 |
bool progress = false; |
| 567 |
switch (instr->type) { |
| 568 |
case nir_instr_type_alu: { |
| 569 |
nir_alu_instr *alu = nir_instr_as_alu(instr); |
| 570 |
nir_op_info info = nir_op_infos[alu->op]; |
| 571 |
for (int i = 0; i < info.num_inputs; i++) { |
| 572 |
ti_type src_type = get_type(types, &alu->src[i].src); |
| 573 |
ti_type def_type = get_type(types, alu->src[i].src.ssa); |
| 574 |
ti_type unified_type = unify_types(src_type, def_type); |
| 575 |
nir_instr *parent_instr = nir_def_instr(alu->src[i].src.ssa); |
| 576 |
if (unified_type > src_type) { |
| 577 |
progress |= update_instr_type(types, instr, unified_type); |
| 578 |
} else if (unified_type > def_type) { |
| 579 |
progress |= update_instr_type(types, parent_instr, unified_type); |
| 580 |
} |
| 581 |
} |
| 582 |
break; |
| 583 |
} |
| 584 |
case nir_instr_type_intrinsic: { |
| 585 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 586 |
nir_intrinsic_info info = nir_intrinsic_infos[intr->intrinsic]; |
| 587 |
for (int i = 0; i < info.num_srcs; i++) { |
| 588 |
ti_type src_type = get_type(types, &intr->src[i]); |
| 589 |
ti_type def_type = get_type(types, intr->src[i].ssa); |
| 590 |
ti_type unified_type = unify_types(src_type, def_type); |
| 591 |
nir_instr *parent_instr = nir_def_instr(intr->src[i].ssa); |
| 592 |
if (unified_type > src_type) { |
| 593 |
progress |= update_instr_type(types, instr, unified_type); |
| 594 |
} else if (unified_type > def_type) { |
| 595 |
progress |= update_instr_type(types, parent_instr, unified_type); |
| 596 |
} |
| 597 |
} |
| 598 |
break; |
| 599 |
} |
| 600 |
case nir_instr_type_tex: { |
| 601 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 602 |
for (int i = 0; i < tex->num_srcs; i++) { |
| 603 |
ti_type src_type = get_type(types, &tex->src[i].src); |
| 604 |
ti_type def_type = get_type(types, tex->src[i].src.ssa); |
| 605 |
ti_type unified_type = unify_types(src_type, def_type); |
| 606 |
if (src_type == 0) |
| 607 |
continue; |
| 608 |
nir_instr *parent_instr = nir_def_instr(tex->src[i].src.ssa); |
| 609 |
if (unified_type > def_type) { |
| 610 |
progress |= update_instr_type(types, parent_instr, unified_type); |
| 611 |
} |
| 612 |
} |
| 613 |
break; |
| 614 |
} |
| 615 |
default: |
| 616 |
break; |
| 617 |
} |
| 618 |
return progress; |
| 619 |
} |
| 620 |
|
| 621 |
static const char *float_names[] = {"float", "float2", "float3", "float4"}; |
| 622 |
static const char *half_names[] = {"half", "half2", "half3", "half4"}; |
| 623 |
static const char *bool_names[] = {"bool", "bool2", "bool3", "bool4"}; |
| 624 |
static const char *int8_names[] = {"char", "char2", "char3", "char4"}; |
| 625 |
static const char *uint8_names[] = {"uchar", "uchar2", "uchar3", "uchar4"}; |
| 626 |
static const char *int16_names[] = {"short", "short2", "short3", "short4"}; |
| 627 |
static const char *uint16_names[] = {"ushort", "ushort2", "ushort3", "ushort4"}; |
| 628 |
static const char *int32_names[] = {"int", "int2", "int3", "int4"}; |
| 629 |
static const char *uint32_names[] = {"uint", "uint2", "uint3", "uint4"}; |
| 630 |
static const char *int64_names[] = {"long", "long2", "long3", "long4"}; |
| 631 |
static const char *uint64_names[] = {"ulong", "ulong2", "ulong3", "ulong4"}; |
| 632 |
|
| 633 |
static const char * |
| 634 |
ti_type_to_msl_type(ti_type type, uint8_t bit_width, uint8_t num_components) |
| 635 |
{ |
| 636 |
switch (type) { |
| 637 |
case TYPE_GENERIC_DATA: |
| 638 |
case TYPE_GENERIC_INT: |
| 639 |
case TYPE_GENERIC_INT_OR_BOOL: |
| 640 |
case TYPE_UINT: |
| 641 |
switch (bit_width) { |
| 642 |
case 1: |
| 643 |
return bool_names[num_components - 1]; |
| 644 |
case 8: |
| 645 |
return uint8_names[num_components - 1]; |
| 646 |
case 16: |
| 647 |
return uint16_names[num_components - 1]; |
| 648 |
case 32: |
| 649 |
return uint32_names[num_components - 1]; |
| 650 |
case 64: |
| 651 |
return uint64_names[num_components - 1]; |
| 652 |
default: |
| 653 |
assert(!"Bad uint length"); |
| 654 |
} |
| 655 |
break; |
| 656 |
case TYPE_BOOL: |
| 657 |
return bool_names[num_components - 1]; |
| 658 |
case TYPE_INT: |
| 659 |
switch (bit_width) { |
| 660 |
case 8: |
| 661 |
return int8_names[num_components - 1]; |
| 662 |
case 16: |
| 663 |
return int16_names[num_components - 1]; |
| 664 |
case 32: |
| 665 |
return int32_names[num_components - 1]; |
| 666 |
case 64: |
| 667 |
return int64_names[num_components - 1]; |
| 668 |
default: |
| 669 |
assert(!"Bad uint length"); |
| 670 |
} |
| 671 |
break; |
| 672 |
case TYPE_FLOAT: |
| 673 |
switch (bit_width) { |
| 674 |
case 16: |
| 675 |
return half_names[num_components - 1]; |
| 676 |
case 32: |
| 677 |
return float_names[num_components - 1]; |
| 678 |
default: |
| 679 |
assert(!"Bad float length"); |
| 680 |
} |
| 681 |
break; |
| 682 |
case TYPE_SAMPLER: |
| 683 |
return "sampler"; |
| 684 |
default: |
| 685 |
return NULL; |
| 686 |
} |
| 687 |
|
| 688 |
return NULL; |
| 689 |
} |
| 690 |
|
| 691 |
const char * |
| 692 |
msl_uint_type(uint8_t bit_size, uint8_t num_components) |
| 693 |
{ |
| 694 |
return ti_type_to_msl_type(TYPE_UINT, bit_size, num_components); |
| 695 |
} |
| 696 |
|
| 697 |
const char * |
| 698 |
msl_type_for_def(struct hash_table *types, nir_def *def) |
| 699 |
{ |
| 700 |
ti_type type = get_type(types, def); |
| 701 |
return ti_type_to_msl_type(type, def->bit_size, def->num_components); |
| 702 |
} |
| 703 |
|
| 704 |
const char * |
| 705 |
msl_type_for_src(struct hash_table *types, nir_src *src) |
| 706 |
{ |
| 707 |
ti_type type = get_type(types, src); |
| 708 |
|
| 709 |
return ti_type_to_msl_type(type, src->ssa->bit_size, |
| 710 |
src->ssa->num_components); |
| 711 |
} |
| 712 |
|
| 713 |
const char * |
| 714 |
msl_bitcast_for_src(struct hash_table *types, nir_src *src) |
| 715 |
{ |
| 716 |
ti_type src_type = get_type(types, src); |
| 717 |
ti_type def_type = get_type(types, src->ssa); |
| 718 |
if (nir_src_is_if(src)) |
| 719 |
return NULL; |
| 720 |
if (src_type != def_type) { |
| 721 |
|
| 722 |
if (src_type == TYPE_BOOL || def_type == TYPE_BOOL) |
| 723 |
return NULL; |
| 724 |
|
| 725 |
|
| 726 |
return ti_type_to_msl_type(src_type, src->ssa->bit_size, |
| 727 |
src->ssa->num_components); |
| 728 |
} else { |
| 729 |
return NULL; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
static void |
| 734 |
emit_src_component(struct nir_to_msl_ctx *ctx, nir_src *src, unsigned comp) |
| 735 |
{ |
| 736 |
ti_type type = get_type(ctx->types, src); |
| 737 |
switch (type) { |
| 738 |
case TYPE_FLOAT: { |
| 739 |
double v = nir_src_comp_as_float(*src, comp); |
| 740 |
if (isinf(v)) { |
| 741 |
P(ctx, "(INFINITY"); |
| 742 |
} else if (isnan(v)) { |
| 743 |
P(ctx, "(NAN"); |
| 744 |
} else { |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
if (src->ssa->bit_size == 16) { |
| 751 |
P(ctx, "half("); |
| 752 |
} else { |
| 753 |
P(ctx, "float("); |
| 754 |
} |
| 755 |
P(ctx, "%.*le", DBL_DECIMAL_DIG, nir_src_comp_as_float(*src, comp)); |
| 756 |
} |
| 757 |
break; |
| 758 |
} |
| 759 |
case TYPE_BOOL: |
| 760 |
P(ctx, "bool(%d", nir_src_comp_as_bool(*src, comp)); |
| 761 |
break; |
| 762 |
case TYPE_INT: |
| 763 |
switch (src->ssa->bit_size) { |
| 764 |
case 8: |
| 765 |
P(ctx, "char("); |
| 766 |
break; |
| 767 |
case 16: |
| 768 |
P(ctx, "short("); |
| 769 |
break; |
| 770 |
case 32: |
| 771 |
P(ctx, "int("); |
| 772 |
break; |
| 773 |
case 64: |
| 774 |
P(ctx, "long("); |
| 775 |
break; |
| 776 |
default: |
| 777 |
UNREACHABLE("Incorrect bit_size for TYPE_INT"); |
| 778 |
} |
| 779 |
P(ctx, "%" PRId64, nir_src_comp_as_int(*src, comp)); |
| 780 |
break; |
| 781 |
case TYPE_UINT: |
| 782 |
case TYPE_GENERIC_DATA: |
| 783 |
case TYPE_GENERIC_INT: |
| 784 |
case TYPE_GENERIC_INT_OR_BOOL: |
| 785 |
switch (src->ssa->bit_size) { |
| 786 |
case 8: |
| 787 |
P(ctx, "uchar("); |
| 788 |
break; |
| 789 |
case 16: |
| 790 |
P(ctx, "ushort("); |
| 791 |
break; |
| 792 |
case 32: |
| 793 |
P(ctx, "uint("); |
| 794 |
break; |
| 795 |
case 64: |
| 796 |
P(ctx, "ulong("); |
| 797 |
break; |
| 798 |
default: |
| 799 |
UNREACHABLE("Incorrect bit_size for TYPE_UINT"); |
| 800 |
} |
| 801 |
P(ctx, "%" PRIu64 "u", nir_src_comp_as_uint(*src, comp)); |
| 802 |
break; |
| 803 |
case TYPE_NONE: |
| 804 |
assert(0); |
| 805 |
P(ctx, "UNTYPED!"); |
| 806 |
break; |
| 807 |
default: |
| 808 |
return; |
| 809 |
} |
| 810 |
P(ctx, ")"); |
| 811 |
} |
| 812 |
|
| 813 |
void |
| 814 |
msl_src_as_const(struct nir_to_msl_ctx *ctx, nir_src *src) |
| 815 |
{ |
| 816 |
ti_type type = get_type(ctx->types, src); |
| 817 |
if (src->ssa->num_components == 1) { |
| 818 |
emit_src_component(ctx, src, 0); |
| 819 |
} else { |
| 820 |
P(ctx, "%s(", |
| 821 |
ti_type_to_msl_type(type, src->ssa->bit_size, |
| 822 |
src->ssa->num_components)); |
| 823 |
for (int i = 0; i < src->ssa->num_components; i++) { |
| 824 |
if (i) |
| 825 |
P(ctx, ", "); |
| 826 |
emit_src_component(ctx, src, i); |
| 827 |
} |
| 828 |
P(ctx, ")"); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
struct hash_table * |
| 833 |
msl_infer_types(nir_shader *shader) |
| 834 |
{ |
| 835 |
struct hash_table *types = _mesa_pointer_hash_table_create(NULL); |
| 836 |
bool progress = false; |
| 837 |
|
| 838 |
nir_foreach_function_impl(impl, shader) { |
| 839 |
nir_foreach_block(block, impl) { |
| 840 |
nir_foreach_instr(instr, block) { |
| 841 |
infer_types_from_instr(types, instr); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
do { |
| 847 |
progress = false; |
| 848 |
nir_foreach_function_impl(impl, shader) { |
| 849 |
nir_foreach_block(block, impl) { |
| 850 |
nir_foreach_instr(instr, block) { |
| 851 |
progress |= propagate_types(types, instr); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
} while (progress); |
| 856 |
return types; |
| 857 |
} |
| 858 |
|
| 859 |
bool |
| 860 |
msl_src_is_float(struct nir_to_msl_ctx *ctx, nir_src *src) |
| 861 |
{ |
| 862 |
return get_type(ctx->types, src) == TYPE_FLOAT; |
| 863 |
} |
| 864 |
|
| 865 |
bool |
| 866 |
msl_def_is_sampler(struct nir_to_msl_ctx *ctx, nir_def *def) |
| 867 |
{ |
| 868 |
return get_type(ctx->types, def) == TYPE_SAMPLER; |
| 869 |
} |