nir/vtn: Add type constant to image intrinsics
[mesa.git] / src / compiler / spirv / spirv_to_nir.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
34
35 #include "util/format/u_format.h"
36 #include "util/u_math.h"
37
38 #include <stdio.h>
39
40 void
41 vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
42 size_t spirv_offset, const char *message)
43 {
44 if (b->options->debug.func) {
45 b->options->debug.func(b->options->debug.private_data,
46 level, spirv_offset, message);
47 }
48
49 #ifndef NDEBUG
50 if (level >= NIR_SPIRV_DEBUG_LEVEL_WARNING)
51 fprintf(stderr, "%s\n", message);
52 #endif
53 }
54
55 void
56 vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
57 size_t spirv_offset, const char *fmt, ...)
58 {
59 va_list args;
60 char *msg;
61
62 va_start(args, fmt);
63 msg = ralloc_vasprintf(NULL, fmt, args);
64 va_end(args);
65
66 vtn_log(b, level, spirv_offset, msg);
67
68 ralloc_free(msg);
69 }
70
71 static void
72 vtn_log_err(struct vtn_builder *b,
73 enum nir_spirv_debug_level level, const char *prefix,
74 const char *file, unsigned line,
75 const char *fmt, va_list args)
76 {
77 char *msg;
78
79 msg = ralloc_strdup(NULL, prefix);
80
81 #ifndef NDEBUG
82 ralloc_asprintf_append(&msg, " In file %s:%u\n", file, line);
83 #endif
84
85 ralloc_asprintf_append(&msg, " ");
86
87 ralloc_vasprintf_append(&msg, fmt, args);
88
89 ralloc_asprintf_append(&msg, "\n %zu bytes into the SPIR-V binary",
90 b->spirv_offset);
91
92 if (b->file) {
93 ralloc_asprintf_append(&msg,
94 "\n in SPIR-V source file %s, line %d, col %d",
95 b->file, b->line, b->col);
96 }
97
98 vtn_log(b, level, b->spirv_offset, msg);
99
100 ralloc_free(msg);
101 }
102
103 static void
104 vtn_dump_shader(struct vtn_builder *b, const char *path, const char *prefix)
105 {
106 static int idx = 0;
107
108 char filename[1024];
109 int len = snprintf(filename, sizeof(filename), "%s/%s-%d.spirv",
110 path, prefix, idx++);
111 if (len < 0 || len >= sizeof(filename))
112 return;
113
114 FILE *f = fopen(filename, "w");
115 if (f == NULL)
116 return;
117
118 fwrite(b->spirv, sizeof(*b->spirv), b->spirv_word_count, f);
119 fclose(f);
120
121 vtn_info("SPIR-V shader dumped to %s", filename);
122 }
123
124 void
125 _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
126 const char *fmt, ...)
127 {
128 va_list args;
129
130 va_start(args, fmt);
131 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_WARNING, "SPIR-V WARNING:\n",
132 file, line, fmt, args);
133 va_end(args);
134 }
135
136 void
137 _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
138 const char *fmt, ...)
139 {
140 va_list args;
141
142 va_start(args, fmt);
143 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V ERROR:\n",
144 file, line, fmt, args);
145 va_end(args);
146 }
147
148 void
149 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
150 const char *fmt, ...)
151 {
152 va_list args;
153
154 va_start(args, fmt);
155 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V parsing FAILED:\n",
156 file, line, fmt, args);
157 va_end(args);
158
159 const char *dump_path = getenv("MESA_SPIRV_FAIL_DUMP_PATH");
160 if (dump_path)
161 vtn_dump_shader(b, dump_path, "fail");
162
163 longjmp(b->fail_jump, 1);
164 }
165
166 static struct vtn_ssa_value *
167 vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
168 {
169 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
170 val->type = glsl_get_bare_type(type);
171
172 if (glsl_type_is_vector_or_scalar(type)) {
173 unsigned num_components = glsl_get_vector_elements(val->type);
174 unsigned bit_size = glsl_get_bit_size(val->type);
175 val->def = nir_ssa_undef(&b->nb, num_components, bit_size);
176 } else {
177 unsigned elems = glsl_get_length(val->type);
178 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
179 if (glsl_type_is_array_or_matrix(type)) {
180 const struct glsl_type *elem_type = glsl_get_array_element(type);
181 for (unsigned i = 0; i < elems; i++)
182 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
183 } else {
184 vtn_assert(glsl_type_is_struct_or_ifc(type));
185 for (unsigned i = 0; i < elems; i++) {
186 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
187 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
188 }
189 }
190 }
191
192 return val;
193 }
194
195 static struct vtn_ssa_value *
196 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
197 const struct glsl_type *type)
198 {
199 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
200
201 if (entry)
202 return entry->data;
203
204 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
205 val->type = glsl_get_bare_type(type);
206
207 if (glsl_type_is_vector_or_scalar(type)) {
208 unsigned num_components = glsl_get_vector_elements(val->type);
209 unsigned bit_size = glsl_get_bit_size(type);
210 nir_load_const_instr *load =
211 nir_load_const_instr_create(b->shader, num_components, bit_size);
212
213 memcpy(load->value, constant->values,
214 sizeof(nir_const_value) * num_components);
215
216 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
217 val->def = &load->def;
218 } else {
219 unsigned elems = glsl_get_length(val->type);
220 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
221 if (glsl_type_is_array_or_matrix(type)) {
222 const struct glsl_type *elem_type = glsl_get_array_element(type);
223 for (unsigned i = 0; i < elems; i++) {
224 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
225 elem_type);
226 }
227 } else {
228 vtn_assert(glsl_type_is_struct_or_ifc(type));
229 for (unsigned i = 0; i < elems; i++) {
230 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
231 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
232 elem_type);
233 }
234 }
235 }
236
237 return val;
238 }
239
240 struct vtn_ssa_value *
241 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
242 {
243 struct vtn_value *val = vtn_untyped_value(b, value_id);
244 switch (val->value_type) {
245 case vtn_value_type_undef:
246 return vtn_undef_ssa_value(b, val->type->type);
247
248 case vtn_value_type_constant:
249 return vtn_const_ssa_value(b, val->constant, val->type->type);
250
251 case vtn_value_type_ssa:
252 return val->ssa;
253
254 case vtn_value_type_pointer:
255 vtn_assert(val->pointer->ptr_type && val->pointer->ptr_type->type);
256 struct vtn_ssa_value *ssa =
257 vtn_create_ssa_value(b, val->pointer->ptr_type->type);
258 ssa->def = vtn_pointer_to_ssa(b, val->pointer);
259 return ssa;
260
261 default:
262 vtn_fail("Invalid type for an SSA value");
263 }
264 }
265
266 struct vtn_value *
267 vtn_push_ssa_value(struct vtn_builder *b, uint32_t value_id,
268 struct vtn_ssa_value *ssa)
269 {
270 struct vtn_type *type = vtn_get_value_type(b, value_id);
271
272 /* See vtn_create_ssa_value */
273 vtn_fail_if(ssa->type != glsl_get_bare_type(type->type),
274 "Type mismatch for SPIR-V SSA value");
275
276 struct vtn_value *val;
277 if (type->base_type == vtn_base_type_pointer) {
278 val = vtn_push_pointer(b, value_id, vtn_pointer_from_ssa(b, ssa->def, type));
279 } else {
280 /* Don't trip the value_type_ssa check in vtn_push_value */
281 val = vtn_push_value(b, value_id, vtn_value_type_invalid);
282 val->value_type = vtn_value_type_ssa;
283 val->ssa = ssa;
284 }
285
286 return val;
287 }
288
289 nir_ssa_def *
290 vtn_get_nir_ssa(struct vtn_builder *b, uint32_t value_id)
291 {
292 struct vtn_ssa_value *ssa = vtn_ssa_value(b, value_id);
293 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa->type),
294 "Expected a vector or scalar type");
295 return ssa->def;
296 }
297
298 struct vtn_value *
299 vtn_push_nir_ssa(struct vtn_builder *b, uint32_t value_id, nir_ssa_def *def)
300 {
301 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
302 * type will be valid by the time we get here.
303 */
304 struct vtn_type *type = vtn_get_value_type(b, value_id);
305 vtn_fail_if(def->num_components != glsl_get_vector_elements(type->type) ||
306 def->bit_size != glsl_get_bit_size(type->type),
307 "Mismatch between NIR and SPIR-V type.");
308 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, type->type);
309 ssa->def = def;
310 return vtn_push_ssa_value(b, value_id, ssa);
311 }
312
313 static nir_deref_instr *
314 vtn_get_image(struct vtn_builder *b, uint32_t value_id)
315 {
316 struct vtn_type *type = vtn_get_value_type(b, value_id);
317 vtn_assert(type->base_type == vtn_base_type_image);
318 return nir_build_deref_cast(&b->nb, vtn_get_nir_ssa(b, value_id),
319 nir_var_uniform, type->glsl_image, 0);
320 }
321
322 static void
323 vtn_push_image(struct vtn_builder *b, uint32_t value_id,
324 nir_deref_instr *deref)
325 {
326 struct vtn_type *type = vtn_get_value_type(b, value_id);
327 vtn_assert(type->base_type == vtn_base_type_image);
328 vtn_push_nir_ssa(b, value_id, &deref->dest.ssa);
329 }
330
331 static nir_deref_instr *
332 vtn_get_sampler(struct vtn_builder *b, uint32_t value_id)
333 {
334 struct vtn_type *type = vtn_get_value_type(b, value_id);
335 vtn_assert(type->base_type == vtn_base_type_sampler);
336 return nir_build_deref_cast(&b->nb, vtn_get_nir_ssa(b, value_id),
337 nir_var_uniform, glsl_bare_sampler_type(), 0);
338 }
339
340 nir_ssa_def *
341 vtn_sampled_image_to_nir_ssa(struct vtn_builder *b,
342 struct vtn_sampled_image si)
343 {
344 return nir_vec2(&b->nb, &si.image->dest.ssa, &si.sampler->dest.ssa);
345 }
346
347 static void
348 vtn_push_sampled_image(struct vtn_builder *b, uint32_t value_id,
349 struct vtn_sampled_image si)
350 {
351 struct vtn_type *type = vtn_get_value_type(b, value_id);
352 vtn_assert(type->base_type == vtn_base_type_sampled_image);
353 vtn_push_nir_ssa(b, value_id, vtn_sampled_image_to_nir_ssa(b, si));
354 }
355
356 static struct vtn_sampled_image
357 vtn_get_sampled_image(struct vtn_builder *b, uint32_t value_id)
358 {
359 struct vtn_type *type = vtn_get_value_type(b, value_id);
360 vtn_assert(type->base_type == vtn_base_type_sampled_image);
361 nir_ssa_def *si_vec2 = vtn_get_nir_ssa(b, value_id);
362
363 struct vtn_sampled_image si = { NULL, };
364 si.image = nir_build_deref_cast(&b->nb, nir_channel(&b->nb, si_vec2, 0),
365 nir_var_uniform,
366 type->image->glsl_image, 0);
367 si.sampler = nir_build_deref_cast(&b->nb, nir_channel(&b->nb, si_vec2, 1),
368 nir_var_uniform,
369 glsl_bare_sampler_type(), 0);
370 return si;
371 }
372
373 static char *
374 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
375 unsigned word_count, unsigned *words_used)
376 {
377 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
378 if (words_used) {
379 /* Ammount of space taken by the string (including the null) */
380 unsigned len = strlen(dup) + 1;
381 *words_used = DIV_ROUND_UP(len, sizeof(*words));
382 }
383 return dup;
384 }
385
386 const uint32_t *
387 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
388 const uint32_t *end, vtn_instruction_handler handler)
389 {
390 b->file = NULL;
391 b->line = -1;
392 b->col = -1;
393
394 const uint32_t *w = start;
395 while (w < end) {
396 SpvOp opcode = w[0] & SpvOpCodeMask;
397 unsigned count = w[0] >> SpvWordCountShift;
398 vtn_assert(count >= 1 && w + count <= end);
399
400 b->spirv_offset = (uint8_t *)w - (uint8_t *)b->spirv;
401
402 switch (opcode) {
403 case SpvOpNop:
404 break; /* Do nothing */
405
406 case SpvOpLine:
407 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
408 b->line = w[2];
409 b->col = w[3];
410 break;
411
412 case SpvOpNoLine:
413 b->file = NULL;
414 b->line = -1;
415 b->col = -1;
416 break;
417
418 default:
419 if (!handler(b, opcode, w, count))
420 return w;
421 break;
422 }
423
424 w += count;
425 }
426
427 b->spirv_offset = 0;
428 b->file = NULL;
429 b->line = -1;
430 b->col = -1;
431
432 assert(w == end);
433 return w;
434 }
435
436 static bool
437 vtn_handle_non_semantic_instruction(struct vtn_builder *b, SpvOp ext_opcode,
438 const uint32_t *w, unsigned count)
439 {
440 /* Do nothing. */
441 return true;
442 }
443
444 static void
445 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
446 const uint32_t *w, unsigned count)
447 {
448 const char *ext = (const char *)&w[2];
449 switch (opcode) {
450 case SpvOpExtInstImport: {
451 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
452 if (strcmp(ext, "GLSL.std.450") == 0) {
453 val->ext_handler = vtn_handle_glsl450_instruction;
454 } else if ((strcmp(ext, "SPV_AMD_gcn_shader") == 0)
455 && (b->options && b->options->caps.amd_gcn_shader)) {
456 val->ext_handler = vtn_handle_amd_gcn_shader_instruction;
457 } else if ((strcmp(ext, "SPV_AMD_shader_ballot") == 0)
458 && (b->options && b->options->caps.amd_shader_ballot)) {
459 val->ext_handler = vtn_handle_amd_shader_ballot_instruction;
460 } else if ((strcmp(ext, "SPV_AMD_shader_trinary_minmax") == 0)
461 && (b->options && b->options->caps.amd_trinary_minmax)) {
462 val->ext_handler = vtn_handle_amd_shader_trinary_minmax_instruction;
463 } else if ((strcmp(ext, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
464 && (b->options && b->options->caps.amd_shader_explicit_vertex_parameter)) {
465 val->ext_handler = vtn_handle_amd_shader_explicit_vertex_parameter_instruction;
466 } else if (strcmp(ext, "OpenCL.std") == 0) {
467 val->ext_handler = vtn_handle_opencl_instruction;
468 } else if (strstr(ext, "NonSemantic.") == ext) {
469 val->ext_handler = vtn_handle_non_semantic_instruction;
470 } else {
471 vtn_fail("Unsupported extension: %s", ext);
472 }
473 break;
474 }
475
476 case SpvOpExtInst: {
477 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
478 bool handled = val->ext_handler(b, w[4], w, count);
479 vtn_assert(handled);
480 break;
481 }
482
483 default:
484 vtn_fail_with_opcode("Unhandled opcode", opcode);
485 }
486 }
487
488 static void
489 _foreach_decoration_helper(struct vtn_builder *b,
490 struct vtn_value *base_value,
491 int parent_member,
492 struct vtn_value *value,
493 vtn_decoration_foreach_cb cb, void *data)
494 {
495 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
496 int member;
497 if (dec->scope == VTN_DEC_DECORATION) {
498 member = parent_member;
499 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
500 vtn_fail_if(value->value_type != vtn_value_type_type ||
501 value->type->base_type != vtn_base_type_struct,
502 "OpMemberDecorate and OpGroupMemberDecorate are only "
503 "allowed on OpTypeStruct");
504 /* This means we haven't recursed yet */
505 assert(value == base_value);
506
507 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
508
509 vtn_fail_if(member >= base_value->type->length,
510 "OpMemberDecorate specifies member %d but the "
511 "OpTypeStruct has only %u members",
512 member, base_value->type->length);
513 } else {
514 /* Not a decoration */
515 assert(dec->scope == VTN_DEC_EXECUTION_MODE);
516 continue;
517 }
518
519 if (dec->group) {
520 assert(dec->group->value_type == vtn_value_type_decoration_group);
521 _foreach_decoration_helper(b, base_value, member, dec->group,
522 cb, data);
523 } else {
524 cb(b, base_value, member, dec, data);
525 }
526 }
527 }
528
529 /** Iterates (recursively if needed) over all of the decorations on a value
530 *
531 * This function iterates over all of the decorations applied to a given
532 * value. If it encounters a decoration group, it recurses into the group
533 * and iterates over all of those decorations as well.
534 */
535 void
536 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
537 vtn_decoration_foreach_cb cb, void *data)
538 {
539 _foreach_decoration_helper(b, value, -1, value, cb, data);
540 }
541
542 void
543 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
544 vtn_execution_mode_foreach_cb cb, void *data)
545 {
546 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
547 if (dec->scope != VTN_DEC_EXECUTION_MODE)
548 continue;
549
550 assert(dec->group == NULL);
551 cb(b, value, dec, data);
552 }
553 }
554
555 void
556 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
557 const uint32_t *w, unsigned count)
558 {
559 const uint32_t *w_end = w + count;
560 const uint32_t target = w[1];
561 w += 2;
562
563 switch (opcode) {
564 case SpvOpDecorationGroup:
565 vtn_push_value(b, target, vtn_value_type_decoration_group);
566 break;
567
568 case SpvOpDecorate:
569 case SpvOpDecorateId:
570 case SpvOpMemberDecorate:
571 case SpvOpDecorateString:
572 case SpvOpMemberDecorateString:
573 case SpvOpExecutionMode:
574 case SpvOpExecutionModeId: {
575 struct vtn_value *val = vtn_untyped_value(b, target);
576
577 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
578 switch (opcode) {
579 case SpvOpDecorate:
580 case SpvOpDecorateId:
581 case SpvOpDecorateString:
582 dec->scope = VTN_DEC_DECORATION;
583 break;
584 case SpvOpMemberDecorate:
585 case SpvOpMemberDecorateString:
586 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
587 vtn_fail_if(dec->scope < VTN_DEC_STRUCT_MEMBER0, /* overflow */
588 "Member argument of OpMemberDecorate too large");
589 break;
590 case SpvOpExecutionMode:
591 case SpvOpExecutionModeId:
592 dec->scope = VTN_DEC_EXECUTION_MODE;
593 break;
594 default:
595 unreachable("Invalid decoration opcode");
596 }
597 dec->decoration = *(w++);
598 dec->operands = w;
599
600 /* Link into the list */
601 dec->next = val->decoration;
602 val->decoration = dec;
603 break;
604 }
605
606 case SpvOpGroupMemberDecorate:
607 case SpvOpGroupDecorate: {
608 struct vtn_value *group =
609 vtn_value(b, target, vtn_value_type_decoration_group);
610
611 for (; w < w_end; w++) {
612 struct vtn_value *val = vtn_untyped_value(b, *w);
613 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
614
615 dec->group = group;
616 if (opcode == SpvOpGroupDecorate) {
617 dec->scope = VTN_DEC_DECORATION;
618 } else {
619 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(++w);
620 vtn_fail_if(dec->scope < 0, /* Check for overflow */
621 "Member argument of OpGroupMemberDecorate too large");
622 }
623
624 /* Link into the list */
625 dec->next = val->decoration;
626 val->decoration = dec;
627 }
628 break;
629 }
630
631 default:
632 unreachable("Unhandled opcode");
633 }
634 }
635
636 struct member_decoration_ctx {
637 unsigned num_fields;
638 struct glsl_struct_field *fields;
639 struct vtn_type *type;
640 };
641
642 /**
643 * Returns true if the given type contains a struct decorated Block or
644 * BufferBlock
645 */
646 bool
647 vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type)
648 {
649 switch (type->base_type) {
650 case vtn_base_type_array:
651 return vtn_type_contains_block(b, type->array_element);
652 case vtn_base_type_struct:
653 if (type->block || type->buffer_block)
654 return true;
655 for (unsigned i = 0; i < type->length; i++) {
656 if (vtn_type_contains_block(b, type->members[i]))
657 return true;
658 }
659 return false;
660 default:
661 return false;
662 }
663 }
664
665 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
666 * OpStore, or OpCopyMemory between them without breaking anything.
667 * Technically, the SPIR-V rules require the exact same type ID but this lets
668 * us internally be a bit looser.
669 */
670 bool
671 vtn_types_compatible(struct vtn_builder *b,
672 struct vtn_type *t1, struct vtn_type *t2)
673 {
674 if (t1->id == t2->id)
675 return true;
676
677 if (t1->base_type != t2->base_type)
678 return false;
679
680 switch (t1->base_type) {
681 case vtn_base_type_void:
682 case vtn_base_type_scalar:
683 case vtn_base_type_vector:
684 case vtn_base_type_matrix:
685 case vtn_base_type_image:
686 case vtn_base_type_sampler:
687 case vtn_base_type_sampled_image:
688 return t1->type == t2->type;
689
690 case vtn_base_type_array:
691 return t1->length == t2->length &&
692 vtn_types_compatible(b, t1->array_element, t2->array_element);
693
694 case vtn_base_type_pointer:
695 return vtn_types_compatible(b, t1->deref, t2->deref);
696
697 case vtn_base_type_struct:
698 if (t1->length != t2->length)
699 return false;
700
701 for (unsigned i = 0; i < t1->length; i++) {
702 if (!vtn_types_compatible(b, t1->members[i], t2->members[i]))
703 return false;
704 }
705 return true;
706
707 case vtn_base_type_function:
708 /* This case shouldn't get hit since you can't copy around function
709 * types. Just require them to be identical.
710 */
711 return false;
712 }
713
714 vtn_fail("Invalid base type");
715 }
716
717 struct vtn_type *
718 vtn_type_without_array(struct vtn_type *type)
719 {
720 while (type->base_type == vtn_base_type_array)
721 type = type->array_element;
722 return type;
723 }
724
725 /* does a shallow copy of a vtn_type */
726
727 static struct vtn_type *
728 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
729 {
730 struct vtn_type *dest = ralloc(b, struct vtn_type);
731 *dest = *src;
732
733 switch (src->base_type) {
734 case vtn_base_type_void:
735 case vtn_base_type_scalar:
736 case vtn_base_type_vector:
737 case vtn_base_type_matrix:
738 case vtn_base_type_array:
739 case vtn_base_type_pointer:
740 case vtn_base_type_image:
741 case vtn_base_type_sampler:
742 case vtn_base_type_sampled_image:
743 /* Nothing more to do */
744 break;
745
746 case vtn_base_type_struct:
747 dest->members = ralloc_array(b, struct vtn_type *, src->length);
748 memcpy(dest->members, src->members,
749 src->length * sizeof(src->members[0]));
750
751 dest->offsets = ralloc_array(b, unsigned, src->length);
752 memcpy(dest->offsets, src->offsets,
753 src->length * sizeof(src->offsets[0]));
754 break;
755
756 case vtn_base_type_function:
757 dest->params = ralloc_array(b, struct vtn_type *, src->length);
758 memcpy(dest->params, src->params, src->length * sizeof(src->params[0]));
759 break;
760 }
761
762 return dest;
763 }
764
765 static const struct glsl_type *
766 wrap_type_in_array(const struct glsl_type *type,
767 const struct glsl_type *array_type)
768 {
769 if (!glsl_type_is_array(array_type))
770 return type;
771
772 const struct glsl_type *elem_type =
773 wrap_type_in_array(type, glsl_get_array_element(array_type));
774 return glsl_array_type(elem_type, glsl_get_length(array_type),
775 glsl_get_explicit_stride(array_type));
776 }
777
778 static bool
779 vtn_type_needs_explicit_layout(struct vtn_builder *b, enum vtn_variable_mode mode)
780 {
781 /* For OpenCL we never want to strip the info from the types, and it makes
782 * type comparisons easier in later stages.
783 */
784 if (b->options->environment == NIR_SPIRV_OPENCL)
785 return true;
786
787 switch (mode) {
788 case vtn_variable_mode_input:
789 case vtn_variable_mode_output:
790 /* Layout decorations kept because we need offsets for XFB arrays of
791 * blocks.
792 */
793 return b->shader->info.has_transform_feedback_varyings;
794
795 case vtn_variable_mode_ssbo:
796 case vtn_variable_mode_phys_ssbo:
797 case vtn_variable_mode_ubo:
798 return true;
799
800 default:
801 return false;
802 }
803 }
804
805 const struct glsl_type *
806 vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type,
807 enum vtn_variable_mode mode)
808 {
809 if (mode == vtn_variable_mode_atomic_counter) {
810 vtn_fail_if(glsl_without_array(type->type) != glsl_uint_type(),
811 "Variables in the AtomicCounter storage class should be "
812 "(possibly arrays of arrays of) uint.");
813 return wrap_type_in_array(glsl_atomic_uint_type(), type->type);
814 }
815
816 if (mode == vtn_variable_mode_uniform) {
817 switch (type->base_type) {
818 case vtn_base_type_array: {
819 const struct glsl_type *elem_type =
820 vtn_type_get_nir_type(b, type->array_element, mode);
821
822 return glsl_array_type(elem_type, type->length,
823 glsl_get_explicit_stride(type->type));
824 }
825
826 case vtn_base_type_struct: {
827 bool need_new_struct = false;
828 const uint32_t num_fields = type->length;
829 NIR_VLA(struct glsl_struct_field, fields, num_fields);
830 for (unsigned i = 0; i < num_fields; i++) {
831 fields[i] = *glsl_get_struct_field_data(type->type, i);
832 const struct glsl_type *field_nir_type =
833 vtn_type_get_nir_type(b, type->members[i], mode);
834 if (fields[i].type != field_nir_type) {
835 fields[i].type = field_nir_type;
836 need_new_struct = true;
837 }
838 }
839 if (need_new_struct) {
840 if (glsl_type_is_interface(type->type)) {
841 return glsl_interface_type(fields, num_fields,
842 /* packing */ 0, false,
843 glsl_get_type_name(type->type));
844 } else {
845 return glsl_struct_type(fields, num_fields,
846 glsl_get_type_name(type->type),
847 glsl_struct_type_is_packed(type->type));
848 }
849 } else {
850 /* No changes, just pass it on */
851 return type->type;
852 }
853 }
854
855 case vtn_base_type_image:
856 return type->glsl_image;
857
858 case vtn_base_type_sampler:
859 return glsl_bare_sampler_type();
860
861 case vtn_base_type_sampled_image:
862 return type->image->glsl_image;
863
864 default:
865 return type->type;
866 }
867 }
868
869 /* Layout decorations are allowed but ignored in certain conditions,
870 * to allow SPIR-V generators perform type deduplication. Discard
871 * unnecessary ones when passing to NIR.
872 */
873 if (!vtn_type_needs_explicit_layout(b, mode))
874 return glsl_get_bare_type(type->type);
875
876 return type->type;
877 }
878
879 static struct vtn_type *
880 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
881 {
882 type->members[member] = vtn_type_copy(b, type->members[member]);
883 type = type->members[member];
884
885 /* We may have an array of matrices.... Oh, joy! */
886 while (glsl_type_is_array(type->type)) {
887 type->array_element = vtn_type_copy(b, type->array_element);
888 type = type->array_element;
889 }
890
891 vtn_assert(glsl_type_is_matrix(type->type));
892
893 return type;
894 }
895
896 static void
897 vtn_handle_access_qualifier(struct vtn_builder *b, struct vtn_type *type,
898 int member, enum gl_access_qualifier access)
899 {
900 type->members[member] = vtn_type_copy(b, type->members[member]);
901 type = type->members[member];
902
903 type->access |= access;
904 }
905
906 static void
907 array_stride_decoration_cb(struct vtn_builder *b,
908 struct vtn_value *val, int member,
909 const struct vtn_decoration *dec, void *void_ctx)
910 {
911 struct vtn_type *type = val->type;
912
913 if (dec->decoration == SpvDecorationArrayStride) {
914 if (vtn_type_contains_block(b, type)) {
915 vtn_warn("The ArrayStride decoration cannot be applied to an array "
916 "type which contains a structure type decorated Block "
917 "or BufferBlock");
918 /* Ignore the decoration */
919 } else {
920 vtn_fail_if(dec->operands[0] == 0, "ArrayStride must be non-zero");
921 type->stride = dec->operands[0];
922 }
923 }
924 }
925
926 static void
927 struct_member_decoration_cb(struct vtn_builder *b,
928 UNUSED struct vtn_value *val, int member,
929 const struct vtn_decoration *dec, void *void_ctx)
930 {
931 struct member_decoration_ctx *ctx = void_ctx;
932
933 if (member < 0)
934 return;
935
936 assert(member < ctx->num_fields);
937
938 switch (dec->decoration) {
939 case SpvDecorationRelaxedPrecision:
940 case SpvDecorationUniform:
941 case SpvDecorationUniformId:
942 break; /* FIXME: Do nothing with this for now. */
943 case SpvDecorationNonWritable:
944 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_WRITEABLE);
945 break;
946 case SpvDecorationNonReadable:
947 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_READABLE);
948 break;
949 case SpvDecorationVolatile:
950 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_VOLATILE);
951 break;
952 case SpvDecorationCoherent:
953 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_COHERENT);
954 break;
955 case SpvDecorationNoPerspective:
956 ctx->fields[member].interpolation = INTERP_MODE_NOPERSPECTIVE;
957 break;
958 case SpvDecorationFlat:
959 ctx->fields[member].interpolation = INTERP_MODE_FLAT;
960 break;
961 case SpvDecorationExplicitInterpAMD:
962 ctx->fields[member].interpolation = INTERP_MODE_EXPLICIT;
963 break;
964 case SpvDecorationCentroid:
965 ctx->fields[member].centroid = true;
966 break;
967 case SpvDecorationSample:
968 ctx->fields[member].sample = true;
969 break;
970 case SpvDecorationStream:
971 /* This is handled later by var_decoration_cb in vtn_variables.c */
972 break;
973 case SpvDecorationLocation:
974 ctx->fields[member].location = dec->operands[0];
975 break;
976 case SpvDecorationComponent:
977 break; /* FIXME: What should we do with these? */
978 case SpvDecorationBuiltIn:
979 ctx->type->members[member] = vtn_type_copy(b, ctx->type->members[member]);
980 ctx->type->members[member]->is_builtin = true;
981 ctx->type->members[member]->builtin = dec->operands[0];
982 ctx->type->builtin_block = true;
983 break;
984 case SpvDecorationOffset:
985 ctx->type->offsets[member] = dec->operands[0];
986 ctx->fields[member].offset = dec->operands[0];
987 break;
988 case SpvDecorationMatrixStride:
989 /* Handled as a second pass */
990 break;
991 case SpvDecorationColMajor:
992 break; /* Nothing to do here. Column-major is the default. */
993 case SpvDecorationRowMajor:
994 mutable_matrix_member(b, ctx->type, member)->row_major = true;
995 break;
996
997 case SpvDecorationPatch:
998 break;
999
1000 case SpvDecorationSpecId:
1001 case SpvDecorationBlock:
1002 case SpvDecorationBufferBlock:
1003 case SpvDecorationArrayStride:
1004 case SpvDecorationGLSLShared:
1005 case SpvDecorationGLSLPacked:
1006 case SpvDecorationInvariant:
1007 case SpvDecorationRestrict:
1008 case SpvDecorationAliased:
1009 case SpvDecorationConstant:
1010 case SpvDecorationIndex:
1011 case SpvDecorationBinding:
1012 case SpvDecorationDescriptorSet:
1013 case SpvDecorationLinkageAttributes:
1014 case SpvDecorationNoContraction:
1015 case SpvDecorationInputAttachmentIndex:
1016 vtn_warn("Decoration not allowed on struct members: %s",
1017 spirv_decoration_to_string(dec->decoration));
1018 break;
1019
1020 case SpvDecorationXfbBuffer:
1021 case SpvDecorationXfbStride:
1022 /* This is handled later by var_decoration_cb in vtn_variables.c */
1023 break;
1024
1025 case SpvDecorationCPacked:
1026 if (b->shader->info.stage != MESA_SHADER_KERNEL)
1027 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1028 spirv_decoration_to_string(dec->decoration));
1029 else
1030 ctx->type->packed = true;
1031 break;
1032
1033 case SpvDecorationSaturatedConversion:
1034 case SpvDecorationFuncParamAttr:
1035 case SpvDecorationFPRoundingMode:
1036 case SpvDecorationFPFastMathMode:
1037 case SpvDecorationAlignment:
1038 if (b->shader->info.stage != MESA_SHADER_KERNEL) {
1039 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1040 spirv_decoration_to_string(dec->decoration));
1041 }
1042 break;
1043
1044 case SpvDecorationUserSemantic:
1045 case SpvDecorationUserTypeGOOGLE:
1046 /* User semantic decorations can safely be ignored by the driver. */
1047 break;
1048
1049 default:
1050 vtn_fail_with_decoration("Unhandled decoration", dec->decoration);
1051 }
1052 }
1053
1054 /** Chases the array type all the way down to the tail and rewrites the
1055 * glsl_types to be based off the tail's glsl_type.
1056 */
1057 static void
1058 vtn_array_type_rewrite_glsl_type(struct vtn_type *type)
1059 {
1060 if (type->base_type != vtn_base_type_array)
1061 return;
1062
1063 vtn_array_type_rewrite_glsl_type(type->array_element);
1064
1065 type->type = glsl_array_type(type->array_element->type,
1066 type->length, type->stride);
1067 }
1068
1069 /* Matrix strides are handled as a separate pass because we need to know
1070 * whether the matrix is row-major or not first.
1071 */
1072 static void
1073 struct_member_matrix_stride_cb(struct vtn_builder *b,
1074 UNUSED struct vtn_value *val, int member,
1075 const struct vtn_decoration *dec,
1076 void *void_ctx)
1077 {
1078 if (dec->decoration != SpvDecorationMatrixStride)
1079 return;
1080
1081 vtn_fail_if(member < 0,
1082 "The MatrixStride decoration is only allowed on members "
1083 "of OpTypeStruct");
1084 vtn_fail_if(dec->operands[0] == 0, "MatrixStride must be non-zero");
1085
1086 struct member_decoration_ctx *ctx = void_ctx;
1087
1088 struct vtn_type *mat_type = mutable_matrix_member(b, ctx->type, member);
1089 if (mat_type->row_major) {
1090 mat_type->array_element = vtn_type_copy(b, mat_type->array_element);
1091 mat_type->stride = mat_type->array_element->stride;
1092 mat_type->array_element->stride = dec->operands[0];
1093
1094 mat_type->type = glsl_explicit_matrix_type(mat_type->type,
1095 dec->operands[0], true);
1096 mat_type->array_element->type = glsl_get_column_type(mat_type->type);
1097 } else {
1098 vtn_assert(mat_type->array_element->stride > 0);
1099 mat_type->stride = dec->operands[0];
1100
1101 mat_type->type = glsl_explicit_matrix_type(mat_type->type,
1102 dec->operands[0], false);
1103 }
1104
1105 /* Now that we've replaced the glsl_type with a properly strided matrix
1106 * type, rewrite the member type so that it's an array of the proper kind
1107 * of glsl_type.
1108 */
1109 vtn_array_type_rewrite_glsl_type(ctx->type->members[member]);
1110 ctx->fields[member].type = ctx->type->members[member]->type;
1111 }
1112
1113 static void
1114 struct_block_decoration_cb(struct vtn_builder *b,
1115 struct vtn_value *val, int member,
1116 const struct vtn_decoration *dec, void *ctx)
1117 {
1118 if (member != -1)
1119 return;
1120
1121 struct vtn_type *type = val->type;
1122 if (dec->decoration == SpvDecorationBlock)
1123 type->block = true;
1124 else if (dec->decoration == SpvDecorationBufferBlock)
1125 type->buffer_block = true;
1126 }
1127
1128 static void
1129 type_decoration_cb(struct vtn_builder *b,
1130 struct vtn_value *val, int member,
1131 const struct vtn_decoration *dec, UNUSED void *ctx)
1132 {
1133 struct vtn_type *type = val->type;
1134
1135 if (member != -1) {
1136 /* This should have been handled by OpTypeStruct */
1137 assert(val->type->base_type == vtn_base_type_struct);
1138 assert(member >= 0 && member < val->type->length);
1139 return;
1140 }
1141
1142 switch (dec->decoration) {
1143 case SpvDecorationArrayStride:
1144 vtn_assert(type->base_type == vtn_base_type_array ||
1145 type->base_type == vtn_base_type_pointer);
1146 break;
1147 case SpvDecorationBlock:
1148 vtn_assert(type->base_type == vtn_base_type_struct);
1149 vtn_assert(type->block);
1150 break;
1151 case SpvDecorationBufferBlock:
1152 vtn_assert(type->base_type == vtn_base_type_struct);
1153 vtn_assert(type->buffer_block);
1154 break;
1155 case SpvDecorationGLSLShared:
1156 case SpvDecorationGLSLPacked:
1157 /* Ignore these, since we get explicit offsets anyways */
1158 break;
1159
1160 case SpvDecorationRowMajor:
1161 case SpvDecorationColMajor:
1162 case SpvDecorationMatrixStride:
1163 case SpvDecorationBuiltIn:
1164 case SpvDecorationNoPerspective:
1165 case SpvDecorationFlat:
1166 case SpvDecorationPatch:
1167 case SpvDecorationCentroid:
1168 case SpvDecorationSample:
1169 case SpvDecorationExplicitInterpAMD:
1170 case SpvDecorationVolatile:
1171 case SpvDecorationCoherent:
1172 case SpvDecorationNonWritable:
1173 case SpvDecorationNonReadable:
1174 case SpvDecorationUniform:
1175 case SpvDecorationUniformId:
1176 case SpvDecorationLocation:
1177 case SpvDecorationComponent:
1178 case SpvDecorationOffset:
1179 case SpvDecorationXfbBuffer:
1180 case SpvDecorationXfbStride:
1181 case SpvDecorationUserSemantic:
1182 vtn_warn("Decoration only allowed for struct members: %s",
1183 spirv_decoration_to_string(dec->decoration));
1184 break;
1185
1186 case SpvDecorationStream:
1187 /* We don't need to do anything here, as stream is filled up when
1188 * aplying the decoration to a variable, just check that if it is not a
1189 * struct member, it should be a struct.
1190 */
1191 vtn_assert(type->base_type == vtn_base_type_struct);
1192 break;
1193
1194 case SpvDecorationRelaxedPrecision:
1195 case SpvDecorationSpecId:
1196 case SpvDecorationInvariant:
1197 case SpvDecorationRestrict:
1198 case SpvDecorationAliased:
1199 case SpvDecorationConstant:
1200 case SpvDecorationIndex:
1201 case SpvDecorationBinding:
1202 case SpvDecorationDescriptorSet:
1203 case SpvDecorationLinkageAttributes:
1204 case SpvDecorationNoContraction:
1205 case SpvDecorationInputAttachmentIndex:
1206 vtn_warn("Decoration not allowed on types: %s",
1207 spirv_decoration_to_string(dec->decoration));
1208 break;
1209
1210 case SpvDecorationCPacked:
1211 if (b->shader->info.stage != MESA_SHADER_KERNEL)
1212 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1213 spirv_decoration_to_string(dec->decoration));
1214 else
1215 type->packed = true;
1216 break;
1217
1218 case SpvDecorationSaturatedConversion:
1219 case SpvDecorationFuncParamAttr:
1220 case SpvDecorationFPRoundingMode:
1221 case SpvDecorationFPFastMathMode:
1222 case SpvDecorationAlignment:
1223 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1224 spirv_decoration_to_string(dec->decoration));
1225 break;
1226
1227 case SpvDecorationUserTypeGOOGLE:
1228 /* User semantic decorations can safely be ignored by the driver. */
1229 break;
1230
1231 default:
1232 vtn_fail_with_decoration("Unhandled decoration", dec->decoration);
1233 }
1234 }
1235
1236 static unsigned
1237 translate_image_format(struct vtn_builder *b, SpvImageFormat format)
1238 {
1239 switch (format) {
1240 case SpvImageFormatUnknown: return PIPE_FORMAT_NONE;
1241 case SpvImageFormatRgba32f: return PIPE_FORMAT_R32G32B32A32_FLOAT;
1242 case SpvImageFormatRgba16f: return PIPE_FORMAT_R16G16B16A16_FLOAT;
1243 case SpvImageFormatR32f: return PIPE_FORMAT_R32_FLOAT;
1244 case SpvImageFormatRgba8: return PIPE_FORMAT_R8G8B8A8_UNORM;
1245 case SpvImageFormatRgba8Snorm: return PIPE_FORMAT_R8G8B8A8_SNORM;
1246 case SpvImageFormatRg32f: return PIPE_FORMAT_R32G32_FLOAT;
1247 case SpvImageFormatRg16f: return PIPE_FORMAT_R16G16_FLOAT;
1248 case SpvImageFormatR11fG11fB10f: return PIPE_FORMAT_R11G11B10_FLOAT;
1249 case SpvImageFormatR16f: return PIPE_FORMAT_R16_FLOAT;
1250 case SpvImageFormatRgba16: return PIPE_FORMAT_R16G16B16A16_UNORM;
1251 case SpvImageFormatRgb10A2: return PIPE_FORMAT_R10G10B10A2_UNORM;
1252 case SpvImageFormatRg16: return PIPE_FORMAT_R16G16_UNORM;
1253 case SpvImageFormatRg8: return PIPE_FORMAT_R8G8_UNORM;
1254 case SpvImageFormatR16: return PIPE_FORMAT_R16_UNORM;
1255 case SpvImageFormatR8: return PIPE_FORMAT_R8_UNORM;
1256 case SpvImageFormatRgba16Snorm: return PIPE_FORMAT_R16G16B16A16_SNORM;
1257 case SpvImageFormatRg16Snorm: return PIPE_FORMAT_R16G16_SNORM;
1258 case SpvImageFormatRg8Snorm: return PIPE_FORMAT_R8G8_SNORM;
1259 case SpvImageFormatR16Snorm: return PIPE_FORMAT_R16_SNORM;
1260 case SpvImageFormatR8Snorm: return PIPE_FORMAT_R8_SNORM;
1261 case SpvImageFormatRgba32i: return PIPE_FORMAT_R32G32B32A32_SINT;
1262 case SpvImageFormatRgba16i: return PIPE_FORMAT_R16G16B16A16_SINT;
1263 case SpvImageFormatRgba8i: return PIPE_FORMAT_R8G8B8A8_SINT;
1264 case SpvImageFormatR32i: return PIPE_FORMAT_R32_SINT;
1265 case SpvImageFormatRg32i: return PIPE_FORMAT_R32G32_SINT;
1266 case SpvImageFormatRg16i: return PIPE_FORMAT_R16G16_SINT;
1267 case SpvImageFormatRg8i: return PIPE_FORMAT_R8G8_SINT;
1268 case SpvImageFormatR16i: return PIPE_FORMAT_R16_SINT;
1269 case SpvImageFormatR8i: return PIPE_FORMAT_R8_SINT;
1270 case SpvImageFormatRgba32ui: return PIPE_FORMAT_R32G32B32A32_UINT;
1271 case SpvImageFormatRgba16ui: return PIPE_FORMAT_R16G16B16A16_UINT;
1272 case SpvImageFormatRgba8ui: return PIPE_FORMAT_R8G8B8A8_UINT;
1273 case SpvImageFormatR32ui: return PIPE_FORMAT_R32_UINT;
1274 case SpvImageFormatRgb10a2ui: return PIPE_FORMAT_R10G10B10A2_UINT;
1275 case SpvImageFormatRg32ui: return PIPE_FORMAT_R32G32_UINT;
1276 case SpvImageFormatRg16ui: return PIPE_FORMAT_R16G16_UINT;
1277 case SpvImageFormatRg8ui: return PIPE_FORMAT_R8G8_UINT;
1278 case SpvImageFormatR16ui: return PIPE_FORMAT_R16_UINT;
1279 case SpvImageFormatR8ui: return PIPE_FORMAT_R8_UINT;
1280 default:
1281 vtn_fail("Invalid image format: %s (%u)",
1282 spirv_imageformat_to_string(format), format);
1283 }
1284 }
1285
1286 static void
1287 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
1288 const uint32_t *w, unsigned count)
1289 {
1290 struct vtn_value *val = NULL;
1291
1292 /* In order to properly handle forward declarations, we have to defer
1293 * allocation for pointer types.
1294 */
1295 if (opcode != SpvOpTypePointer && opcode != SpvOpTypeForwardPointer) {
1296 val = vtn_push_value(b, w[1], vtn_value_type_type);
1297 vtn_fail_if(val->type != NULL,
1298 "Only pointers can have forward declarations");
1299 val->type = rzalloc(b, struct vtn_type);
1300 val->type->id = w[1];
1301 }
1302
1303 switch (opcode) {
1304 case SpvOpTypeVoid:
1305 val->type->base_type = vtn_base_type_void;
1306 val->type->type = glsl_void_type();
1307 break;
1308 case SpvOpTypeBool:
1309 val->type->base_type = vtn_base_type_scalar;
1310 val->type->type = glsl_bool_type();
1311 val->type->length = 1;
1312 break;
1313 case SpvOpTypeInt: {
1314 int bit_size = w[2];
1315 const bool signedness = w[3];
1316 val->type->base_type = vtn_base_type_scalar;
1317 switch (bit_size) {
1318 case 64:
1319 val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
1320 break;
1321 case 32:
1322 val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
1323 break;
1324 case 16:
1325 val->type->type = (signedness ? glsl_int16_t_type() : glsl_uint16_t_type());
1326 break;
1327 case 8:
1328 val->type->type = (signedness ? glsl_int8_t_type() : glsl_uint8_t_type());
1329 break;
1330 default:
1331 vtn_fail("Invalid int bit size: %u", bit_size);
1332 }
1333 val->type->length = 1;
1334 break;
1335 }
1336
1337 case SpvOpTypeFloat: {
1338 int bit_size = w[2];
1339 val->type->base_type = vtn_base_type_scalar;
1340 switch (bit_size) {
1341 case 16:
1342 val->type->type = glsl_float16_t_type();
1343 break;
1344 case 32:
1345 val->type->type = glsl_float_type();
1346 break;
1347 case 64:
1348 val->type->type = glsl_double_type();
1349 break;
1350 default:
1351 vtn_fail("Invalid float bit size: %u", bit_size);
1352 }
1353 val->type->length = 1;
1354 break;
1355 }
1356
1357 case SpvOpTypeVector: {
1358 struct vtn_type *base = vtn_get_type(b, w[2]);
1359 unsigned elems = w[3];
1360
1361 vtn_fail_if(base->base_type != vtn_base_type_scalar,
1362 "Base type for OpTypeVector must be a scalar");
1363 vtn_fail_if((elems < 2 || elems > 4) && (elems != 8) && (elems != 16),
1364 "Invalid component count for OpTypeVector");
1365
1366 val->type->base_type = vtn_base_type_vector;
1367 val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
1368 val->type->length = elems;
1369 val->type->stride = glsl_type_is_boolean(val->type->type)
1370 ? 4 : glsl_get_bit_size(base->type) / 8;
1371 val->type->array_element = base;
1372 break;
1373 }
1374
1375 case SpvOpTypeMatrix: {
1376 struct vtn_type *base = vtn_get_type(b, w[2]);
1377 unsigned columns = w[3];
1378
1379 vtn_fail_if(base->base_type != vtn_base_type_vector,
1380 "Base type for OpTypeMatrix must be a vector");
1381 vtn_fail_if(columns < 2 || columns > 4,
1382 "Invalid column count for OpTypeMatrix");
1383
1384 val->type->base_type = vtn_base_type_matrix;
1385 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
1386 glsl_get_vector_elements(base->type),
1387 columns);
1388 vtn_fail_if(glsl_type_is_error(val->type->type),
1389 "Unsupported base type for OpTypeMatrix");
1390 assert(!glsl_type_is_error(val->type->type));
1391 val->type->length = columns;
1392 val->type->array_element = base;
1393 val->type->row_major = false;
1394 val->type->stride = 0;
1395 break;
1396 }
1397
1398 case SpvOpTypeRuntimeArray:
1399 case SpvOpTypeArray: {
1400 struct vtn_type *array_element = vtn_get_type(b, w[2]);
1401
1402 if (opcode == SpvOpTypeRuntimeArray) {
1403 /* A length of 0 is used to denote unsized arrays */
1404 val->type->length = 0;
1405 } else {
1406 val->type->length = vtn_constant_uint(b, w[3]);
1407 }
1408
1409 val->type->base_type = vtn_base_type_array;
1410 val->type->array_element = array_element;
1411 if (b->shader->info.stage == MESA_SHADER_KERNEL)
1412 val->type->stride = glsl_get_cl_size(array_element->type);
1413
1414 vtn_foreach_decoration(b, val, array_stride_decoration_cb, NULL);
1415 val->type->type = glsl_array_type(array_element->type, val->type->length,
1416 val->type->stride);
1417 break;
1418 }
1419
1420 case SpvOpTypeStruct: {
1421 unsigned num_fields = count - 2;
1422 val->type->base_type = vtn_base_type_struct;
1423 val->type->length = num_fields;
1424 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
1425 val->type->offsets = ralloc_array(b, unsigned, num_fields);
1426 val->type->packed = false;
1427
1428 NIR_VLA(struct glsl_struct_field, fields, count);
1429 for (unsigned i = 0; i < num_fields; i++) {
1430 val->type->members[i] = vtn_get_type(b, w[i + 2]);
1431 fields[i] = (struct glsl_struct_field) {
1432 .type = val->type->members[i]->type,
1433 .name = ralloc_asprintf(b, "field%d", i),
1434 .location = -1,
1435 .offset = -1,
1436 };
1437 }
1438
1439 if (b->shader->info.stage == MESA_SHADER_KERNEL) {
1440 unsigned offset = 0;
1441 for (unsigned i = 0; i < num_fields; i++) {
1442 offset = align(offset, glsl_get_cl_alignment(fields[i].type));
1443 fields[i].offset = offset;
1444 offset += glsl_get_cl_size(fields[i].type);
1445 }
1446 }
1447
1448 struct member_decoration_ctx ctx = {
1449 .num_fields = num_fields,
1450 .fields = fields,
1451 .type = val->type
1452 };
1453
1454 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
1455 vtn_foreach_decoration(b, val, struct_member_matrix_stride_cb, &ctx);
1456
1457 vtn_foreach_decoration(b, val, struct_block_decoration_cb, NULL);
1458
1459 const char *name = val->name;
1460
1461 if (val->type->block || val->type->buffer_block) {
1462 /* Packing will be ignored since types coming from SPIR-V are
1463 * explicitly laid out.
1464 */
1465 val->type->type = glsl_interface_type(fields, num_fields,
1466 /* packing */ 0, false,
1467 name ? name : "block");
1468 } else {
1469 val->type->type = glsl_struct_type(fields, num_fields,
1470 name ? name : "struct", false);
1471 }
1472 break;
1473 }
1474
1475 case SpvOpTypeFunction: {
1476 val->type->base_type = vtn_base_type_function;
1477 val->type->type = NULL;
1478
1479 val->type->return_type = vtn_get_type(b, w[2]);
1480
1481 const unsigned num_params = count - 3;
1482 val->type->length = num_params;
1483 val->type->params = ralloc_array(b, struct vtn_type *, num_params);
1484 for (unsigned i = 0; i < count - 3; i++) {
1485 val->type->params[i] = vtn_get_type(b, w[i + 3]);
1486 }
1487 break;
1488 }
1489
1490 case SpvOpTypePointer:
1491 case SpvOpTypeForwardPointer: {
1492 /* We can't blindly push the value because it might be a forward
1493 * declaration.
1494 */
1495 val = vtn_untyped_value(b, w[1]);
1496
1497 SpvStorageClass storage_class = w[2];
1498
1499 if (val->value_type == vtn_value_type_invalid) {
1500 val->value_type = vtn_value_type_type;
1501 val->type = rzalloc(b, struct vtn_type);
1502 val->type->id = w[1];
1503 val->type->base_type = vtn_base_type_pointer;
1504 val->type->storage_class = storage_class;
1505
1506 /* These can actually be stored to nir_variables and used as SSA
1507 * values so they need a real glsl_type.
1508 */
1509 enum vtn_variable_mode mode = vtn_storage_class_to_mode(
1510 b, storage_class, NULL, NULL);
1511 val->type->type = nir_address_format_to_glsl_type(
1512 vtn_mode_to_address_format(b, mode));
1513 } else {
1514 vtn_fail_if(val->type->storage_class != storage_class,
1515 "The storage classes of an OpTypePointer and any "
1516 "OpTypeForwardPointers that provide forward "
1517 "declarations of it must match.");
1518 }
1519
1520 if (opcode == SpvOpTypePointer) {
1521 vtn_fail_if(val->type->deref != NULL,
1522 "While OpTypeForwardPointer can be used to provide a "
1523 "forward declaration of a pointer, OpTypePointer can "
1524 "only be used once for a given id.");
1525
1526 val->type->deref = vtn_get_type(b, w[3]);
1527
1528 /* Only certain storage classes use ArrayStride. The others (in
1529 * particular Workgroup) are expected to be laid out by the driver.
1530 */
1531 switch (storage_class) {
1532 case SpvStorageClassUniform:
1533 case SpvStorageClassPushConstant:
1534 case SpvStorageClassStorageBuffer:
1535 case SpvStorageClassPhysicalStorageBuffer:
1536 vtn_foreach_decoration(b, val, array_stride_decoration_cb, NULL);
1537 break;
1538 default:
1539 /* Nothing to do. */
1540 break;
1541 }
1542
1543 if (b->physical_ptrs) {
1544 switch (storage_class) {
1545 case SpvStorageClassFunction:
1546 case SpvStorageClassWorkgroup:
1547 case SpvStorageClassCrossWorkgroup:
1548 case SpvStorageClassUniformConstant:
1549 val->type->stride = align(glsl_get_cl_size(val->type->deref->type),
1550 glsl_get_cl_alignment(val->type->deref->type));
1551 break;
1552 default:
1553 break;
1554 }
1555 }
1556 }
1557 break;
1558 }
1559
1560 case SpvOpTypeImage: {
1561 val->type->base_type = vtn_base_type_image;
1562
1563 /* Images are represented in NIR as a scalar SSA value that is the
1564 * result of a deref instruction. An OpLoad on an OpTypeImage pointer
1565 * from UniformConstant memory just takes the NIR deref from the pointer
1566 * and turns it into an SSA value.
1567 */
1568 val->type->type = nir_address_format_to_glsl_type(
1569 vtn_mode_to_address_format(b, vtn_variable_mode_function));
1570
1571 const struct vtn_type *sampled_type = vtn_get_type(b, w[2]);
1572 vtn_fail_if(sampled_type->base_type != vtn_base_type_scalar ||
1573 glsl_get_bit_size(sampled_type->type) != 32,
1574 "Sampled type of OpTypeImage must be a 32-bit scalar");
1575
1576 enum glsl_sampler_dim dim;
1577 switch ((SpvDim)w[3]) {
1578 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
1579 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
1580 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
1581 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
1582 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
1583 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
1584 case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
1585 default:
1586 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1587 spirv_dim_to_string((SpvDim)w[3]), w[3]);
1588 }
1589
1590 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1591 * The “Depth” operand of OpTypeImage is ignored.
1592 */
1593 bool is_array = w[5];
1594 bool multisampled = w[6];
1595 unsigned sampled = w[7];
1596 SpvImageFormat format = w[8];
1597
1598 if (count > 9)
1599 val->type->access_qualifier = w[9];
1600 else
1601 val->type->access_qualifier = SpvAccessQualifierReadWrite;
1602
1603 if (multisampled) {
1604 if (dim == GLSL_SAMPLER_DIM_2D)
1605 dim = GLSL_SAMPLER_DIM_MS;
1606 else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
1607 dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
1608 else
1609 vtn_fail("Unsupported multisampled image type");
1610 }
1611
1612 val->type->image_format = translate_image_format(b, format);
1613
1614 enum glsl_base_type sampled_base_type =
1615 glsl_get_base_type(sampled_type->type);
1616 if (sampled == 1) {
1617 val->type->glsl_image = glsl_sampler_type(dim, false, is_array,
1618 sampled_base_type);
1619 } else if (sampled == 2) {
1620 val->type->glsl_image = glsl_image_type(dim, is_array,
1621 sampled_base_type);
1622 } else {
1623 vtn_fail("We need to know if the image will be sampled");
1624 }
1625 break;
1626 }
1627
1628 case SpvOpTypeSampledImage: {
1629 val->type->base_type = vtn_base_type_sampled_image;
1630 val->type->image = vtn_get_type(b, w[2]);
1631
1632 /* Sampled images are represented NIR as a vec2 SSA value where each
1633 * component is the result of a deref instruction. The first component
1634 * is the image and the second is the sampler. An OpLoad on an
1635 * OpTypeSampledImage pointer from UniformConstant memory just takes
1636 * the NIR deref from the pointer and duplicates it to both vector
1637 * components.
1638 */
1639 nir_address_format addr_format =
1640 vtn_mode_to_address_format(b, vtn_variable_mode_function);
1641 assert(nir_address_format_num_components(addr_format) == 1);
1642 unsigned bit_size = nir_address_format_bit_size(addr_format);
1643 assert(bit_size == 32 || bit_size == 64);
1644
1645 enum glsl_base_type base_type =
1646 bit_size == 32 ? GLSL_TYPE_UINT : GLSL_TYPE_UINT64;
1647 val->type->type = glsl_vector_type(base_type, 2);
1648 break;
1649 }
1650
1651 case SpvOpTypeSampler:
1652 val->type->base_type = vtn_base_type_sampler;
1653
1654 /* Samplers are represented in NIR as a scalar SSA value that is the
1655 * result of a deref instruction. An OpLoad on an OpTypeSampler pointer
1656 * from UniformConstant memory just takes the NIR deref from the pointer
1657 * and turns it into an SSA value.
1658 */
1659 val->type->type = nir_address_format_to_glsl_type(
1660 vtn_mode_to_address_format(b, vtn_variable_mode_function));
1661 break;
1662
1663 case SpvOpTypeOpaque:
1664 case SpvOpTypeEvent:
1665 case SpvOpTypeDeviceEvent:
1666 case SpvOpTypeReserveId:
1667 case SpvOpTypeQueue:
1668 case SpvOpTypePipe:
1669 default:
1670 vtn_fail_with_opcode("Unhandled opcode", opcode);
1671 }
1672
1673 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
1674
1675 if (val->type->base_type == vtn_base_type_struct &&
1676 (val->type->block || val->type->buffer_block)) {
1677 for (unsigned i = 0; i < val->type->length; i++) {
1678 vtn_fail_if(vtn_type_contains_block(b, val->type->members[i]),
1679 "Block and BufferBlock decorations cannot decorate a "
1680 "structure type that is nested at any level inside "
1681 "another structure type decorated with Block or "
1682 "BufferBlock.");
1683 }
1684 }
1685 }
1686
1687 static nir_constant *
1688 vtn_null_constant(struct vtn_builder *b, struct vtn_type *type)
1689 {
1690 nir_constant *c = rzalloc(b, nir_constant);
1691
1692 switch (type->base_type) {
1693 case vtn_base_type_scalar:
1694 case vtn_base_type_vector:
1695 /* Nothing to do here. It's already initialized to zero */
1696 break;
1697
1698 case vtn_base_type_pointer: {
1699 enum vtn_variable_mode mode = vtn_storage_class_to_mode(
1700 b, type->storage_class, type->deref, NULL);
1701 nir_address_format addr_format = vtn_mode_to_address_format(b, mode);
1702
1703 const nir_const_value *null_value = nir_address_format_null_value(addr_format);
1704 memcpy(c->values, null_value,
1705 sizeof(nir_const_value) * nir_address_format_num_components(addr_format));
1706 break;
1707 }
1708
1709 case vtn_base_type_void:
1710 case vtn_base_type_image:
1711 case vtn_base_type_sampler:
1712 case vtn_base_type_sampled_image:
1713 case vtn_base_type_function:
1714 /* For those we have to return something but it doesn't matter what. */
1715 break;
1716
1717 case vtn_base_type_matrix:
1718 case vtn_base_type_array:
1719 vtn_assert(type->length > 0);
1720 c->num_elements = type->length;
1721 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1722
1723 c->elements[0] = vtn_null_constant(b, type->array_element);
1724 for (unsigned i = 1; i < c->num_elements; i++)
1725 c->elements[i] = c->elements[0];
1726 break;
1727
1728 case vtn_base_type_struct:
1729 c->num_elements = type->length;
1730 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1731 for (unsigned i = 0; i < c->num_elements; i++)
1732 c->elements[i] = vtn_null_constant(b, type->members[i]);
1733 break;
1734
1735 default:
1736 vtn_fail("Invalid type for null constant");
1737 }
1738
1739 return c;
1740 }
1741
1742 static void
1743 spec_constant_decoration_cb(struct vtn_builder *b, UNUSED struct vtn_value *val,
1744 ASSERTED int member,
1745 const struct vtn_decoration *dec, void *data)
1746 {
1747 vtn_assert(member == -1);
1748 if (dec->decoration != SpvDecorationSpecId)
1749 return;
1750
1751 nir_const_value *value = data;
1752 for (unsigned i = 0; i < b->num_specializations; i++) {
1753 if (b->specializations[i].id == dec->operands[0]) {
1754 *value = b->specializations[i].value;
1755 return;
1756 }
1757 }
1758 }
1759
1760 static void
1761 handle_workgroup_size_decoration_cb(struct vtn_builder *b,
1762 struct vtn_value *val,
1763 ASSERTED int member,
1764 const struct vtn_decoration *dec,
1765 UNUSED void *data)
1766 {
1767 vtn_assert(member == -1);
1768 if (dec->decoration != SpvDecorationBuiltIn ||
1769 dec->operands[0] != SpvBuiltInWorkgroupSize)
1770 return;
1771
1772 vtn_assert(val->type->type == glsl_vector_type(GLSL_TYPE_UINT, 3));
1773 b->workgroup_size_builtin = val;
1774 }
1775
1776 static void
1777 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
1778 const uint32_t *w, unsigned count)
1779 {
1780 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
1781 val->constant = rzalloc(b, nir_constant);
1782 switch (opcode) {
1783 case SpvOpConstantTrue:
1784 case SpvOpConstantFalse:
1785 case SpvOpSpecConstantTrue:
1786 case SpvOpSpecConstantFalse: {
1787 vtn_fail_if(val->type->type != glsl_bool_type(),
1788 "Result type of %s must be OpTypeBool",
1789 spirv_op_to_string(opcode));
1790
1791 bool bval = (opcode == SpvOpConstantTrue ||
1792 opcode == SpvOpSpecConstantTrue);
1793
1794 nir_const_value u32val = nir_const_value_for_uint(bval, 32);
1795
1796 if (opcode == SpvOpSpecConstantTrue ||
1797 opcode == SpvOpSpecConstantFalse)
1798 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &u32val);
1799
1800 val->constant->values[0].b = u32val.u32 != 0;
1801 break;
1802 }
1803
1804 case SpvOpConstant:
1805 case SpvOpSpecConstant: {
1806 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1807 "Result type of %s must be a scalar",
1808 spirv_op_to_string(opcode));
1809 int bit_size = glsl_get_bit_size(val->type->type);
1810 switch (bit_size) {
1811 case 64:
1812 val->constant->values[0].u64 = vtn_u64_literal(&w[3]);
1813 break;
1814 case 32:
1815 val->constant->values[0].u32 = w[3];
1816 break;
1817 case 16:
1818 val->constant->values[0].u16 = w[3];
1819 break;
1820 case 8:
1821 val->constant->values[0].u8 = w[3];
1822 break;
1823 default:
1824 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size);
1825 }
1826
1827 if (opcode == SpvOpSpecConstant)
1828 vtn_foreach_decoration(b, val, spec_constant_decoration_cb,
1829 &val->constant->values[0]);
1830 break;
1831 }
1832
1833 case SpvOpSpecConstantComposite:
1834 case SpvOpConstantComposite: {
1835 unsigned elem_count = count - 3;
1836 vtn_fail_if(elem_count != val->type->length,
1837 "%s has %u constituents, expected %u",
1838 spirv_op_to_string(opcode), elem_count, val->type->length);
1839
1840 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
1841 for (unsigned i = 0; i < elem_count; i++) {
1842 struct vtn_value *val = vtn_untyped_value(b, w[i + 3]);
1843
1844 if (val->value_type == vtn_value_type_constant) {
1845 elems[i] = val->constant;
1846 } else {
1847 vtn_fail_if(val->value_type != vtn_value_type_undef,
1848 "only constants or undefs allowed for "
1849 "SpvOpConstantComposite");
1850 /* to make it easier, just insert a NULL constant for now */
1851 elems[i] = vtn_null_constant(b, val->type);
1852 }
1853 }
1854
1855 switch (val->type->base_type) {
1856 case vtn_base_type_vector: {
1857 assert(glsl_type_is_vector(val->type->type));
1858 for (unsigned i = 0; i < elem_count; i++)
1859 val->constant->values[i] = elems[i]->values[0];
1860 break;
1861 }
1862
1863 case vtn_base_type_matrix:
1864 case vtn_base_type_struct:
1865 case vtn_base_type_array:
1866 ralloc_steal(val->constant, elems);
1867 val->constant->num_elements = elem_count;
1868 val->constant->elements = elems;
1869 break;
1870
1871 default:
1872 vtn_fail("Result type of %s must be a composite type",
1873 spirv_op_to_string(opcode));
1874 }
1875 break;
1876 }
1877
1878 case SpvOpSpecConstantOp: {
1879 nir_const_value u32op = nir_const_value_for_uint(w[3], 32);
1880 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &u32op);
1881 SpvOp opcode = u32op.u32;
1882 switch (opcode) {
1883 case SpvOpVectorShuffle: {
1884 struct vtn_value *v0 = &b->values[w[4]];
1885 struct vtn_value *v1 = &b->values[w[5]];
1886
1887 vtn_assert(v0->value_type == vtn_value_type_constant ||
1888 v0->value_type == vtn_value_type_undef);
1889 vtn_assert(v1->value_type == vtn_value_type_constant ||
1890 v1->value_type == vtn_value_type_undef);
1891
1892 unsigned len0 = glsl_get_vector_elements(v0->type->type);
1893 unsigned len1 = glsl_get_vector_elements(v1->type->type);
1894
1895 vtn_assert(len0 + len1 < 16);
1896
1897 unsigned bit_size = glsl_get_bit_size(val->type->type);
1898 unsigned bit_size0 = glsl_get_bit_size(v0->type->type);
1899 unsigned bit_size1 = glsl_get_bit_size(v1->type->type);
1900
1901 vtn_assert(bit_size == bit_size0 && bit_size == bit_size1);
1902 (void)bit_size0; (void)bit_size1;
1903
1904 nir_const_value undef = { .u64 = 0xdeadbeefdeadbeef };
1905 nir_const_value combined[NIR_MAX_VEC_COMPONENTS * 2];
1906
1907 if (v0->value_type == vtn_value_type_constant) {
1908 for (unsigned i = 0; i < len0; i++)
1909 combined[i] = v0->constant->values[i];
1910 }
1911 if (v1->value_type == vtn_value_type_constant) {
1912 for (unsigned i = 0; i < len1; i++)
1913 combined[len0 + i] = v1->constant->values[i];
1914 }
1915
1916 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1917 uint32_t comp = w[i + 6];
1918 if (comp == (uint32_t)-1) {
1919 /* If component is not used, set the value to a known constant
1920 * to detect if it is wrongly used.
1921 */
1922 val->constant->values[j] = undef;
1923 } else {
1924 vtn_fail_if(comp >= len0 + len1,
1925 "All Component literals must either be FFFFFFFF "
1926 "or in [0, N - 1] (inclusive).");
1927 val->constant->values[j] = combined[comp];
1928 }
1929 }
1930 break;
1931 }
1932
1933 case SpvOpCompositeExtract:
1934 case SpvOpCompositeInsert: {
1935 struct vtn_value *comp;
1936 unsigned deref_start;
1937 struct nir_constant **c;
1938 if (opcode == SpvOpCompositeExtract) {
1939 comp = vtn_value(b, w[4], vtn_value_type_constant);
1940 deref_start = 5;
1941 c = &comp->constant;
1942 } else {
1943 comp = vtn_value(b, w[5], vtn_value_type_constant);
1944 deref_start = 6;
1945 val->constant = nir_constant_clone(comp->constant,
1946 (nir_variable *)b);
1947 c = &val->constant;
1948 }
1949
1950 int elem = -1;
1951 const struct vtn_type *type = comp->type;
1952 for (unsigned i = deref_start; i < count; i++) {
1953 vtn_fail_if(w[i] > type->length,
1954 "%uth index of %s is %u but the type has only "
1955 "%u elements", i - deref_start,
1956 spirv_op_to_string(opcode), w[i], type->length);
1957
1958 switch (type->base_type) {
1959 case vtn_base_type_vector:
1960 elem = w[i];
1961 type = type->array_element;
1962 break;
1963
1964 case vtn_base_type_matrix:
1965 case vtn_base_type_array:
1966 c = &(*c)->elements[w[i]];
1967 type = type->array_element;
1968 break;
1969
1970 case vtn_base_type_struct:
1971 c = &(*c)->elements[w[i]];
1972 type = type->members[w[i]];
1973 break;
1974
1975 default:
1976 vtn_fail("%s must only index into composite types",
1977 spirv_op_to_string(opcode));
1978 }
1979 }
1980
1981 if (opcode == SpvOpCompositeExtract) {
1982 if (elem == -1) {
1983 val->constant = *c;
1984 } else {
1985 unsigned num_components = type->length;
1986 for (unsigned i = 0; i < num_components; i++)
1987 val->constant->values[i] = (*c)->values[elem + i];
1988 }
1989 } else {
1990 struct vtn_value *insert =
1991 vtn_value(b, w[4], vtn_value_type_constant);
1992 vtn_assert(insert->type == type);
1993 if (elem == -1) {
1994 *c = insert->constant;
1995 } else {
1996 unsigned num_components = type->length;
1997 for (unsigned i = 0; i < num_components; i++)
1998 (*c)->values[elem + i] = insert->constant->values[i];
1999 }
2000 }
2001 break;
2002 }
2003
2004 default: {
2005 bool swap;
2006 nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->type->type);
2007 nir_alu_type src_alu_type = dst_alu_type;
2008 unsigned num_components = glsl_get_vector_elements(val->type->type);
2009 unsigned bit_size;
2010
2011 vtn_assert(count <= 7);
2012
2013 switch (opcode) {
2014 case SpvOpSConvert:
2015 case SpvOpFConvert:
2016 case SpvOpUConvert:
2017 /* We have a source in a conversion */
2018 src_alu_type =
2019 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b, w[4])->type);
2020 /* We use the bitsize of the conversion source to evaluate the opcode later */
2021 bit_size = glsl_get_bit_size(vtn_get_value_type(b, w[4])->type);
2022 break;
2023 default:
2024 bit_size = glsl_get_bit_size(val->type->type);
2025 };
2026
2027 nir_op op = vtn_nir_alu_op_for_spirv_opcode(b, opcode, &swap,
2028 nir_alu_type_get_type_size(src_alu_type),
2029 nir_alu_type_get_type_size(dst_alu_type));
2030 nir_const_value src[3][NIR_MAX_VEC_COMPONENTS];
2031
2032 for (unsigned i = 0; i < count - 4; i++) {
2033 struct vtn_value *src_val =
2034 vtn_value(b, w[4 + i], vtn_value_type_constant);
2035
2036 /* If this is an unsized source, pull the bit size from the
2037 * source; otherwise, we'll use the bit size from the destination.
2038 */
2039 if (!nir_alu_type_get_type_size(nir_op_infos[op].input_types[i]))
2040 bit_size = glsl_get_bit_size(src_val->type->type);
2041
2042 unsigned src_comps = nir_op_infos[op].input_sizes[i] ?
2043 nir_op_infos[op].input_sizes[i] :
2044 num_components;
2045
2046 unsigned j = swap ? 1 - i : i;
2047 for (unsigned c = 0; c < src_comps; c++)
2048 src[j][c] = src_val->constant->values[c];
2049 }
2050
2051 /* fix up fixed size sources */
2052 switch (op) {
2053 case nir_op_ishl:
2054 case nir_op_ishr:
2055 case nir_op_ushr: {
2056 if (bit_size == 32)
2057 break;
2058 for (unsigned i = 0; i < num_components; ++i) {
2059 switch (bit_size) {
2060 case 64: src[1][i].u32 = src[1][i].u64; break;
2061 case 16: src[1][i].u32 = src[1][i].u16; break;
2062 case 8: src[1][i].u32 = src[1][i].u8; break;
2063 }
2064 }
2065 break;
2066 }
2067 default:
2068 break;
2069 }
2070
2071 nir_const_value *srcs[3] = {
2072 src[0], src[1], src[2],
2073 };
2074 nir_eval_const_opcode(op, val->constant->values,
2075 num_components, bit_size, srcs,
2076 b->shader->info.float_controls_execution_mode);
2077 break;
2078 } /* default */
2079 }
2080 break;
2081 }
2082
2083 case SpvOpConstantNull:
2084 val->constant = vtn_null_constant(b, val->type);
2085 break;
2086
2087 case SpvOpConstantSampler:
2088 vtn_fail("OpConstantSampler requires Kernel Capability");
2089 break;
2090
2091 default:
2092 vtn_fail_with_opcode("Unhandled opcode", opcode);
2093 }
2094
2095 /* Now that we have the value, update the workgroup size if needed */
2096 vtn_foreach_decoration(b, val, handle_workgroup_size_decoration_cb, NULL);
2097 }
2098
2099 SpvMemorySemanticsMask
2100 vtn_storage_class_to_memory_semantics(SpvStorageClass sc)
2101 {
2102 switch (sc) {
2103 case SpvStorageClassStorageBuffer:
2104 case SpvStorageClassPhysicalStorageBuffer:
2105 return SpvMemorySemanticsUniformMemoryMask;
2106 case SpvStorageClassWorkgroup:
2107 return SpvMemorySemanticsWorkgroupMemoryMask;
2108 default:
2109 return SpvMemorySemanticsMaskNone;
2110 }
2111 }
2112
2113 static void
2114 vtn_split_barrier_semantics(struct vtn_builder *b,
2115 SpvMemorySemanticsMask semantics,
2116 SpvMemorySemanticsMask *before,
2117 SpvMemorySemanticsMask *after)
2118 {
2119 /* For memory semantics embedded in operations, we split them into up to
2120 * two barriers, to be added before and after the operation. This is less
2121 * strict than if we propagated until the final backend stage, but still
2122 * result in correct execution.
2123 *
2124 * A further improvement could be pipe this information (and use!) into the
2125 * next compiler layers, at the expense of making the handling of barriers
2126 * more complicated.
2127 */
2128
2129 *before = SpvMemorySemanticsMaskNone;
2130 *after = SpvMemorySemanticsMaskNone;
2131
2132 SpvMemorySemanticsMask order_semantics =
2133 semantics & (SpvMemorySemanticsAcquireMask |
2134 SpvMemorySemanticsReleaseMask |
2135 SpvMemorySemanticsAcquireReleaseMask |
2136 SpvMemorySemanticsSequentiallyConsistentMask);
2137
2138 if (util_bitcount(order_semantics) > 1) {
2139 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2140 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2141 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2142 */
2143 vtn_warn("Multiple memory ordering semantics specified, "
2144 "assuming AcquireRelease.");
2145 order_semantics = SpvMemorySemanticsAcquireReleaseMask;
2146 }
2147
2148 const SpvMemorySemanticsMask av_vis_semantics =
2149 semantics & (SpvMemorySemanticsMakeAvailableMask |
2150 SpvMemorySemanticsMakeVisibleMask);
2151
2152 const SpvMemorySemanticsMask storage_semantics =
2153 semantics & (SpvMemorySemanticsUniformMemoryMask |
2154 SpvMemorySemanticsSubgroupMemoryMask |
2155 SpvMemorySemanticsWorkgroupMemoryMask |
2156 SpvMemorySemanticsCrossWorkgroupMemoryMask |
2157 SpvMemorySemanticsAtomicCounterMemoryMask |
2158 SpvMemorySemanticsImageMemoryMask |
2159 SpvMemorySemanticsOutputMemoryMask);
2160
2161 const SpvMemorySemanticsMask other_semantics =
2162 semantics & ~(order_semantics | av_vis_semantics | storage_semantics);
2163
2164 if (other_semantics)
2165 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics);
2166
2167 /* SequentiallyConsistent is treated as AcquireRelease. */
2168
2169 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2170 * associated with a Store. All the write operations with a matching
2171 * semantics will not be reordered after the Store.
2172 */
2173 if (order_semantics & (SpvMemorySemanticsReleaseMask |
2174 SpvMemorySemanticsAcquireReleaseMask |
2175 SpvMemorySemanticsSequentiallyConsistentMask)) {
2176 *before |= SpvMemorySemanticsReleaseMask | storage_semantics;
2177 }
2178
2179 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2180 * associated with a Load. All the operations with a matching semantics
2181 * will not be reordered before the Load.
2182 */
2183 if (order_semantics & (SpvMemorySemanticsAcquireMask |
2184 SpvMemorySemanticsAcquireReleaseMask |
2185 SpvMemorySemanticsSequentiallyConsistentMask)) {
2186 *after |= SpvMemorySemanticsAcquireMask | storage_semantics;
2187 }
2188
2189 if (av_vis_semantics & SpvMemorySemanticsMakeVisibleMask)
2190 *before |= SpvMemorySemanticsMakeVisibleMask | storage_semantics;
2191
2192 if (av_vis_semantics & SpvMemorySemanticsMakeAvailableMask)
2193 *after |= SpvMemorySemanticsMakeAvailableMask | storage_semantics;
2194 }
2195
2196 static nir_memory_semantics
2197 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder *b,
2198 SpvMemorySemanticsMask semantics)
2199 {
2200 nir_memory_semantics nir_semantics = 0;
2201
2202 SpvMemorySemanticsMask order_semantics =
2203 semantics & (SpvMemorySemanticsAcquireMask |
2204 SpvMemorySemanticsReleaseMask |
2205 SpvMemorySemanticsAcquireReleaseMask |
2206 SpvMemorySemanticsSequentiallyConsistentMask);
2207
2208 if (util_bitcount(order_semantics) > 1) {
2209 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2210 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2211 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2212 */
2213 vtn_warn("Multiple memory ordering semantics bits specified, "
2214 "assuming AcquireRelease.");
2215 order_semantics = SpvMemorySemanticsAcquireReleaseMask;
2216 }
2217
2218 switch (order_semantics) {
2219 case 0:
2220 /* Not an ordering barrier. */
2221 break;
2222
2223 case SpvMemorySemanticsAcquireMask:
2224 nir_semantics = NIR_MEMORY_ACQUIRE;
2225 break;
2226
2227 case SpvMemorySemanticsReleaseMask:
2228 nir_semantics = NIR_MEMORY_RELEASE;
2229 break;
2230
2231 case SpvMemorySemanticsSequentiallyConsistentMask:
2232 /* Fall through. Treated as AcquireRelease in Vulkan. */
2233 case SpvMemorySemanticsAcquireReleaseMask:
2234 nir_semantics = NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE;
2235 break;
2236
2237 default:
2238 unreachable("Invalid memory order semantics");
2239 }
2240
2241 if (semantics & SpvMemorySemanticsMakeAvailableMask) {
2242 vtn_fail_if(!b->options->caps.vk_memory_model,
2243 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2244 "capability must be declared.");
2245 nir_semantics |= NIR_MEMORY_MAKE_AVAILABLE;
2246 }
2247
2248 if (semantics & SpvMemorySemanticsMakeVisibleMask) {
2249 vtn_fail_if(!b->options->caps.vk_memory_model,
2250 "To use MakeVisible memory semantics the VulkanMemoryModel "
2251 "capability must be declared.");
2252 nir_semantics |= NIR_MEMORY_MAKE_VISIBLE;
2253 }
2254
2255 return nir_semantics;
2256 }
2257
2258 static nir_variable_mode
2259 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder *b,
2260 SpvMemorySemanticsMask semantics)
2261 {
2262 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2263 * and AtomicCounterMemory are ignored".
2264 */
2265 semantics &= ~(SpvMemorySemanticsSubgroupMemoryMask |
2266 SpvMemorySemanticsCrossWorkgroupMemoryMask |
2267 SpvMemorySemanticsAtomicCounterMemoryMask);
2268
2269 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2270 * for SpvMemorySemanticsImageMemoryMask.
2271 */
2272
2273 nir_variable_mode modes = 0;
2274 if (semantics & (SpvMemorySemanticsUniformMemoryMask |
2275 SpvMemorySemanticsImageMemoryMask)) {
2276 modes |= nir_var_uniform |
2277 nir_var_mem_ubo |
2278 nir_var_mem_ssbo |
2279 nir_var_mem_global;
2280 }
2281 if (semantics & SpvMemorySemanticsWorkgroupMemoryMask)
2282 modes |= nir_var_mem_shared;
2283 if (semantics & SpvMemorySemanticsOutputMemoryMask) {
2284 modes |= nir_var_shader_out;
2285 }
2286
2287 return modes;
2288 }
2289
2290 static nir_scope
2291 vtn_scope_to_nir_scope(struct vtn_builder *b, SpvScope scope)
2292 {
2293 nir_scope nir_scope;
2294 switch (scope) {
2295 case SpvScopeDevice:
2296 vtn_fail_if(b->options->caps.vk_memory_model &&
2297 !b->options->caps.vk_memory_model_device_scope,
2298 "If the Vulkan memory model is declared and any instruction "
2299 "uses Device scope, the VulkanMemoryModelDeviceScope "
2300 "capability must be declared.");
2301 nir_scope = NIR_SCOPE_DEVICE;
2302 break;
2303
2304 case SpvScopeQueueFamily:
2305 vtn_fail_if(!b->options->caps.vk_memory_model,
2306 "To use Queue Family scope, the VulkanMemoryModel capability "
2307 "must be declared.");
2308 nir_scope = NIR_SCOPE_QUEUE_FAMILY;
2309 break;
2310
2311 case SpvScopeWorkgroup:
2312 nir_scope = NIR_SCOPE_WORKGROUP;
2313 break;
2314
2315 case SpvScopeSubgroup:
2316 nir_scope = NIR_SCOPE_SUBGROUP;
2317 break;
2318
2319 case SpvScopeInvocation:
2320 nir_scope = NIR_SCOPE_INVOCATION;
2321 break;
2322
2323 default:
2324 vtn_fail("Invalid memory scope");
2325 }
2326
2327 return nir_scope;
2328 }
2329
2330 static void
2331 vtn_emit_scoped_control_barrier(struct vtn_builder *b, SpvScope exec_scope,
2332 SpvScope mem_scope,
2333 SpvMemorySemanticsMask semantics)
2334 {
2335 nir_memory_semantics nir_semantics =
2336 vtn_mem_semantics_to_nir_mem_semantics(b, semantics);
2337 nir_variable_mode modes = vtn_mem_sematics_to_nir_var_modes(b, semantics);
2338 nir_scope nir_exec_scope = vtn_scope_to_nir_scope(b, exec_scope);
2339
2340 /* Memory semantics is optional for OpControlBarrier. */
2341 nir_scope nir_mem_scope;
2342 if (nir_semantics == 0 || modes == 0)
2343 nir_mem_scope = NIR_SCOPE_NONE;
2344 else
2345 nir_mem_scope = vtn_scope_to_nir_scope(b, mem_scope);
2346
2347 nir_scoped_barrier(&b->nb, nir_exec_scope, nir_mem_scope, nir_semantics, modes);
2348 }
2349
2350 static void
2351 vtn_emit_scoped_memory_barrier(struct vtn_builder *b, SpvScope scope,
2352 SpvMemorySemanticsMask semantics)
2353 {
2354 nir_variable_mode modes = vtn_mem_sematics_to_nir_var_modes(b, semantics);
2355 nir_memory_semantics nir_semantics =
2356 vtn_mem_semantics_to_nir_mem_semantics(b, semantics);
2357
2358 /* No barrier to add. */
2359 if (nir_semantics == 0 || modes == 0)
2360 return;
2361
2362 nir_scope nir_mem_scope = vtn_scope_to_nir_scope(b, scope);
2363 nir_scoped_barrier(&b->nb, NIR_SCOPE_NONE, nir_mem_scope, nir_semantics, modes);
2364 }
2365
2366 struct vtn_ssa_value *
2367 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
2368 {
2369 /* Always use bare types for SSA values for a couple of reasons:
2370 *
2371 * 1. Code which emits deref chains should never listen to the explicit
2372 * layout information on the SSA value if any exists. If we've
2373 * accidentally been relying on this, we want to find those bugs.
2374 *
2375 * 2. We want to be able to quickly check that an SSA value being assigned
2376 * to a SPIR-V value has the right type. Using bare types everywhere
2377 * ensures that we can pointer-compare.
2378 */
2379 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
2380 val->type = glsl_get_bare_type(type);
2381
2382
2383 if (!glsl_type_is_vector_or_scalar(type)) {
2384 unsigned elems = glsl_get_length(val->type);
2385 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
2386 if (glsl_type_is_array_or_matrix(type)) {
2387 const struct glsl_type *elem_type = glsl_get_array_element(type);
2388 for (unsigned i = 0; i < elems; i++)
2389 val->elems[i] = vtn_create_ssa_value(b, elem_type);
2390 } else {
2391 vtn_assert(glsl_type_is_struct_or_ifc(type));
2392 for (unsigned i = 0; i < elems; i++) {
2393 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
2394 val->elems[i] = vtn_create_ssa_value(b, elem_type);
2395 }
2396 }
2397 }
2398
2399 return val;
2400 }
2401
2402 static nir_tex_src
2403 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
2404 {
2405 nir_tex_src src;
2406 src.src = nir_src_for_ssa(vtn_get_nir_ssa(b, index));
2407 src.src_type = type;
2408 return src;
2409 }
2410
2411 static uint32_t
2412 image_operand_arg(struct vtn_builder *b, const uint32_t *w, uint32_t count,
2413 uint32_t mask_idx, SpvImageOperandsMask op)
2414 {
2415 static const SpvImageOperandsMask ops_with_arg =
2416 SpvImageOperandsBiasMask |
2417 SpvImageOperandsLodMask |
2418 SpvImageOperandsGradMask |
2419 SpvImageOperandsConstOffsetMask |
2420 SpvImageOperandsOffsetMask |
2421 SpvImageOperandsConstOffsetsMask |
2422 SpvImageOperandsSampleMask |
2423 SpvImageOperandsMinLodMask |
2424 SpvImageOperandsMakeTexelAvailableMask |
2425 SpvImageOperandsMakeTexelVisibleMask;
2426
2427 assert(util_bitcount(op) == 1);
2428 assert(w[mask_idx] & op);
2429 assert(op & ops_with_arg);
2430
2431 uint32_t idx = util_bitcount(w[mask_idx] & (op - 1) & ops_with_arg) + 1;
2432
2433 /* Adjust indices for operands with two arguments. */
2434 static const SpvImageOperandsMask ops_with_two_args =
2435 SpvImageOperandsGradMask;
2436 idx += util_bitcount(w[mask_idx] & (op - 1) & ops_with_two_args);
2437
2438 idx += mask_idx;
2439
2440 vtn_fail_if(idx + (op & ops_with_two_args ? 1 : 0) >= count,
2441 "Image op claims to have %s but does not enough "
2442 "following operands", spirv_imageoperands_to_string(op));
2443
2444 return idx;
2445 }
2446
2447 static void
2448 non_uniform_decoration_cb(struct vtn_builder *b,
2449 struct vtn_value *val, int member,
2450 const struct vtn_decoration *dec, void *void_ctx)
2451 {
2452 enum gl_access_qualifier *access = void_ctx;
2453 switch (dec->decoration) {
2454 case SpvDecorationNonUniformEXT:
2455 *access |= ACCESS_NON_UNIFORM;
2456 break;
2457
2458 default:
2459 break;
2460 }
2461 }
2462
2463 static void
2464 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
2465 const uint32_t *w, unsigned count)
2466 {
2467 struct vtn_type *ret_type = vtn_get_type(b, w[1]);
2468
2469 if (opcode == SpvOpSampledImage) {
2470 struct vtn_sampled_image si = {
2471 .image = vtn_get_image(b, w[3]),
2472 .sampler = vtn_get_sampler(b, w[4]),
2473 };
2474 vtn_push_sampled_image(b, w[2], si);
2475 return;
2476 } else if (opcode == SpvOpImage) {
2477 struct vtn_sampled_image si = vtn_get_sampled_image(b, w[3]);
2478 vtn_push_image(b, w[2], si.image);
2479 return;
2480 }
2481
2482 nir_deref_instr *image = NULL, *sampler = NULL;
2483 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
2484 if (sampled_val->type->base_type == vtn_base_type_sampled_image) {
2485 struct vtn_sampled_image si = vtn_get_sampled_image(b, w[3]);
2486 image = si.image;
2487 sampler = si.sampler;
2488 } else {
2489 image = vtn_get_image(b, w[3]);
2490 }
2491
2492 const enum glsl_sampler_dim sampler_dim = glsl_get_sampler_dim(image->type);
2493 const bool is_array = glsl_sampler_type_is_array(image->type);
2494 nir_alu_type dest_type = nir_type_invalid;
2495
2496 /* Figure out the base texture operation */
2497 nir_texop texop;
2498 switch (opcode) {
2499 case SpvOpImageSampleImplicitLod:
2500 case SpvOpImageSampleDrefImplicitLod:
2501 case SpvOpImageSampleProjImplicitLod:
2502 case SpvOpImageSampleProjDrefImplicitLod:
2503 texop = nir_texop_tex;
2504 break;
2505
2506 case SpvOpImageSampleExplicitLod:
2507 case SpvOpImageSampleDrefExplicitLod:
2508 case SpvOpImageSampleProjExplicitLod:
2509 case SpvOpImageSampleProjDrefExplicitLod:
2510 texop = nir_texop_txl;
2511 break;
2512
2513 case SpvOpImageFetch:
2514 if (sampler_dim == GLSL_SAMPLER_DIM_MS) {
2515 texop = nir_texop_txf_ms;
2516 } else {
2517 texop = nir_texop_txf;
2518 }
2519 break;
2520
2521 case SpvOpImageGather:
2522 case SpvOpImageDrefGather:
2523 texop = nir_texop_tg4;
2524 break;
2525
2526 case SpvOpImageQuerySizeLod:
2527 case SpvOpImageQuerySize:
2528 texop = nir_texop_txs;
2529 dest_type = nir_type_int;
2530 break;
2531
2532 case SpvOpImageQueryLod:
2533 texop = nir_texop_lod;
2534 dest_type = nir_type_float;
2535 break;
2536
2537 case SpvOpImageQueryLevels:
2538 texop = nir_texop_query_levels;
2539 dest_type = nir_type_int;
2540 break;
2541
2542 case SpvOpImageQuerySamples:
2543 texop = nir_texop_texture_samples;
2544 dest_type = nir_type_int;
2545 break;
2546
2547 case SpvOpFragmentFetchAMD:
2548 texop = nir_texop_fragment_fetch;
2549 break;
2550
2551 case SpvOpFragmentMaskFetchAMD:
2552 texop = nir_texop_fragment_mask_fetch;
2553 break;
2554
2555 default:
2556 vtn_fail_with_opcode("Unhandled opcode", opcode);
2557 }
2558
2559 nir_tex_src srcs[10]; /* 10 should be enough */
2560 nir_tex_src *p = srcs;
2561
2562 p->src = nir_src_for_ssa(&image->dest.ssa);
2563 p->src_type = nir_tex_src_texture_deref;
2564 p++;
2565
2566 switch (texop) {
2567 case nir_texop_tex:
2568 case nir_texop_txb:
2569 case nir_texop_txl:
2570 case nir_texop_txd:
2571 case nir_texop_tg4:
2572 case nir_texop_lod:
2573 vtn_fail_if(sampler == NULL,
2574 "%s requires an image of type OpTypeSampledImage",
2575 spirv_op_to_string(opcode));
2576 p->src = nir_src_for_ssa(&sampler->dest.ssa);
2577 p->src_type = nir_tex_src_sampler_deref;
2578 p++;
2579 break;
2580 case nir_texop_txf:
2581 case nir_texop_txf_ms:
2582 case nir_texop_txs:
2583 case nir_texop_query_levels:
2584 case nir_texop_texture_samples:
2585 case nir_texop_samples_identical:
2586 case nir_texop_fragment_fetch:
2587 case nir_texop_fragment_mask_fetch:
2588 /* These don't */
2589 break;
2590 case nir_texop_txf_ms_fb:
2591 vtn_fail("unexpected nir_texop_txf_ms_fb");
2592 break;
2593 case nir_texop_txf_ms_mcs:
2594 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2595 case nir_texop_tex_prefetch:
2596 vtn_fail("unexpected nir_texop_tex_prefetch");
2597 }
2598
2599 unsigned idx = 4;
2600
2601 struct nir_ssa_def *coord;
2602 unsigned coord_components;
2603 switch (opcode) {
2604 case SpvOpImageSampleImplicitLod:
2605 case SpvOpImageSampleExplicitLod:
2606 case SpvOpImageSampleDrefImplicitLod:
2607 case SpvOpImageSampleDrefExplicitLod:
2608 case SpvOpImageSampleProjImplicitLod:
2609 case SpvOpImageSampleProjExplicitLod:
2610 case SpvOpImageSampleProjDrefImplicitLod:
2611 case SpvOpImageSampleProjDrefExplicitLod:
2612 case SpvOpImageFetch:
2613 case SpvOpImageGather:
2614 case SpvOpImageDrefGather:
2615 case SpvOpImageQueryLod:
2616 case SpvOpFragmentFetchAMD:
2617 case SpvOpFragmentMaskFetchAMD: {
2618 /* All these types have the coordinate as their first real argument */
2619 coord_components = glsl_get_sampler_dim_coordinate_components(sampler_dim);
2620
2621 if (is_array && texop != nir_texop_lod)
2622 coord_components++;
2623
2624 coord = vtn_get_nir_ssa(b, w[idx++]);
2625 p->src = nir_src_for_ssa(nir_channels(&b->nb, coord,
2626 (1 << coord_components) - 1));
2627 p->src_type = nir_tex_src_coord;
2628 p++;
2629 break;
2630 }
2631
2632 default:
2633 coord = NULL;
2634 coord_components = 0;
2635 break;
2636 }
2637
2638 switch (opcode) {
2639 case SpvOpImageSampleProjImplicitLod:
2640 case SpvOpImageSampleProjExplicitLod:
2641 case SpvOpImageSampleProjDrefImplicitLod:
2642 case SpvOpImageSampleProjDrefExplicitLod:
2643 /* These have the projector as the last coordinate component */
2644 p->src = nir_src_for_ssa(nir_channel(&b->nb, coord, coord_components));
2645 p->src_type = nir_tex_src_projector;
2646 p++;
2647 break;
2648
2649 default:
2650 break;
2651 }
2652
2653 bool is_shadow = false;
2654 unsigned gather_component = 0;
2655 switch (opcode) {
2656 case SpvOpImageSampleDrefImplicitLod:
2657 case SpvOpImageSampleDrefExplicitLod:
2658 case SpvOpImageSampleProjDrefImplicitLod:
2659 case SpvOpImageSampleProjDrefExplicitLod:
2660 case SpvOpImageDrefGather:
2661 /* These all have an explicit depth value as their next source */
2662 is_shadow = true;
2663 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparator);
2664 break;
2665
2666 case SpvOpImageGather:
2667 /* This has a component as its next source */
2668 gather_component = vtn_constant_uint(b, w[idx++]);
2669 break;
2670
2671 default:
2672 break;
2673 }
2674
2675 /* For OpImageQuerySizeLod, we always have an LOD */
2676 if (opcode == SpvOpImageQuerySizeLod)
2677 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2678
2679 /* For OpFragmentFetchAMD, we always have a multisample index */
2680 if (opcode == SpvOpFragmentFetchAMD)
2681 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
2682
2683 /* Now we need to handle some number of optional arguments */
2684 struct vtn_value *gather_offsets = NULL;
2685 if (idx < count) {
2686 uint32_t operands = w[idx];
2687
2688 if (operands & SpvImageOperandsBiasMask) {
2689 vtn_assert(texop == nir_texop_tex ||
2690 texop == nir_texop_tg4);
2691 if (texop == nir_texop_tex)
2692 texop = nir_texop_txb;
2693 uint32_t arg = image_operand_arg(b, w, count, idx,
2694 SpvImageOperandsBiasMask);
2695 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_bias);
2696 }
2697
2698 if (operands & SpvImageOperandsLodMask) {
2699 vtn_assert(texop == nir_texop_txl || texop == nir_texop_txf ||
2700 texop == nir_texop_txs || texop == nir_texop_tg4);
2701 uint32_t arg = image_operand_arg(b, w, count, idx,
2702 SpvImageOperandsLodMask);
2703 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_lod);
2704 }
2705
2706 if (operands & SpvImageOperandsGradMask) {
2707 vtn_assert(texop == nir_texop_txl);
2708 texop = nir_texop_txd;
2709 uint32_t arg = image_operand_arg(b, w, count, idx,
2710 SpvImageOperandsGradMask);
2711 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_ddx);
2712 (*p++) = vtn_tex_src(b, w[arg + 1], nir_tex_src_ddy);
2713 }
2714
2715 vtn_fail_if(util_bitcount(operands & (SpvImageOperandsConstOffsetsMask |
2716 SpvImageOperandsOffsetMask |
2717 SpvImageOperandsConstOffsetMask)) > 1,
2718 "At most one of the ConstOffset, Offset, and ConstOffsets "
2719 "image operands can be used on a given instruction.");
2720
2721 if (operands & SpvImageOperandsOffsetMask) {
2722 uint32_t arg = image_operand_arg(b, w, count, idx,
2723 SpvImageOperandsOffsetMask);
2724 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_offset);
2725 }
2726
2727 if (operands & SpvImageOperandsConstOffsetMask) {
2728 uint32_t arg = image_operand_arg(b, w, count, idx,
2729 SpvImageOperandsConstOffsetMask);
2730 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_offset);
2731 }
2732
2733 if (operands & SpvImageOperandsConstOffsetsMask) {
2734 vtn_assert(texop == nir_texop_tg4);
2735 uint32_t arg = image_operand_arg(b, w, count, idx,
2736 SpvImageOperandsConstOffsetsMask);
2737 gather_offsets = vtn_value(b, w[arg], vtn_value_type_constant);
2738 }
2739
2740 if (operands & SpvImageOperandsSampleMask) {
2741 vtn_assert(texop == nir_texop_txf_ms);
2742 uint32_t arg = image_operand_arg(b, w, count, idx,
2743 SpvImageOperandsSampleMask);
2744 texop = nir_texop_txf_ms;
2745 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_ms_index);
2746 }
2747
2748 if (operands & SpvImageOperandsMinLodMask) {
2749 vtn_assert(texop == nir_texop_tex ||
2750 texop == nir_texop_txb ||
2751 texop == nir_texop_txd);
2752 uint32_t arg = image_operand_arg(b, w, count, idx,
2753 SpvImageOperandsMinLodMask);
2754 (*p++) = vtn_tex_src(b, w[arg], nir_tex_src_min_lod);
2755 }
2756 }
2757
2758 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
2759 instr->op = texop;
2760
2761 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
2762
2763 instr->coord_components = coord_components;
2764 instr->sampler_dim = sampler_dim;
2765 instr->is_array = is_array;
2766 instr->is_shadow = is_shadow;
2767 instr->is_new_style_shadow =
2768 is_shadow && glsl_get_components(ret_type->type) == 1;
2769 instr->component = gather_component;
2770
2771 /* The Vulkan spec says:
2772 *
2773 * "If an instruction loads from or stores to a resource (including
2774 * atomics and image instructions) and the resource descriptor being
2775 * accessed is not dynamically uniform, then the operand corresponding
2776 * to that resource (e.g. the pointer or sampled image operand) must be
2777 * decorated with NonUniform."
2778 *
2779 * It's very careful to specify that the exact operand must be decorated
2780 * NonUniform. The SPIR-V parser is not expected to chase through long
2781 * chains to find the NonUniform decoration. It's either right there or we
2782 * can assume it doesn't exist.
2783 */
2784 enum gl_access_qualifier access = 0;
2785 vtn_foreach_decoration(b, sampled_val, non_uniform_decoration_cb, &access);
2786
2787 if (image && (access & ACCESS_NON_UNIFORM))
2788 instr->texture_non_uniform = true;
2789
2790 if (sampler && (access & ACCESS_NON_UNIFORM))
2791 instr->sampler_non_uniform = true;
2792
2793 /* for non-query ops, get dest_type from sampler type */
2794 if (dest_type == nir_type_invalid) {
2795 switch (glsl_get_sampler_result_type(image->type)) {
2796 case GLSL_TYPE_FLOAT: dest_type = nir_type_float; break;
2797 case GLSL_TYPE_INT: dest_type = nir_type_int; break;
2798 case GLSL_TYPE_UINT: dest_type = nir_type_uint; break;
2799 case GLSL_TYPE_BOOL: dest_type = nir_type_bool; break;
2800 default:
2801 vtn_fail("Invalid base type for sampler result");
2802 }
2803 }
2804
2805 instr->dest_type = dest_type;
2806
2807 nir_ssa_dest_init(&instr->instr, &instr->dest,
2808 nir_tex_instr_dest_size(instr), 32, NULL);
2809
2810 vtn_assert(glsl_get_vector_elements(ret_type->type) ==
2811 nir_tex_instr_dest_size(instr));
2812
2813 if (gather_offsets) {
2814 vtn_fail_if(gather_offsets->type->base_type != vtn_base_type_array ||
2815 gather_offsets->type->length != 4,
2816 "ConstOffsets must be an array of size four of vectors "
2817 "of two integer components");
2818
2819 struct vtn_type *vec_type = gather_offsets->type->array_element;
2820 vtn_fail_if(vec_type->base_type != vtn_base_type_vector ||
2821 vec_type->length != 2 ||
2822 !glsl_type_is_integer(vec_type->type),
2823 "ConstOffsets must be an array of size four of vectors "
2824 "of two integer components");
2825
2826 unsigned bit_size = glsl_get_bit_size(vec_type->type);
2827 for (uint32_t i = 0; i < 4; i++) {
2828 const nir_const_value *cvec =
2829 gather_offsets->constant->elements[i]->values;
2830 for (uint32_t j = 0; j < 2; j++) {
2831 switch (bit_size) {
2832 case 8: instr->tg4_offsets[i][j] = cvec[j].i8; break;
2833 case 16: instr->tg4_offsets[i][j] = cvec[j].i16; break;
2834 case 32: instr->tg4_offsets[i][j] = cvec[j].i32; break;
2835 case 64: instr->tg4_offsets[i][j] = cvec[j].i64; break;
2836 default:
2837 vtn_fail("Unsupported bit size: %u", bit_size);
2838 }
2839 }
2840 }
2841 }
2842
2843 nir_builder_instr_insert(&b->nb, &instr->instr);
2844
2845 vtn_push_nir_ssa(b, w[2], &instr->dest.ssa);
2846 }
2847
2848 static void
2849 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
2850 const uint32_t *w, nir_src *src)
2851 {
2852 switch (opcode) {
2853 case SpvOpAtomicIIncrement:
2854 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
2855 break;
2856
2857 case SpvOpAtomicIDecrement:
2858 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
2859 break;
2860
2861 case SpvOpAtomicISub:
2862 src[0] =
2863 nir_src_for_ssa(nir_ineg(&b->nb, vtn_get_nir_ssa(b, w[6])));
2864 break;
2865
2866 case SpvOpAtomicCompareExchange:
2867 case SpvOpAtomicCompareExchangeWeak:
2868 src[0] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[8]));
2869 src[1] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[7]));
2870 break;
2871
2872 case SpvOpAtomicExchange:
2873 case SpvOpAtomicIAdd:
2874 case SpvOpAtomicSMin:
2875 case SpvOpAtomicUMin:
2876 case SpvOpAtomicSMax:
2877 case SpvOpAtomicUMax:
2878 case SpvOpAtomicAnd:
2879 case SpvOpAtomicOr:
2880 case SpvOpAtomicXor:
2881 case SpvOpAtomicFAddEXT:
2882 src[0] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[6]));
2883 break;
2884
2885 default:
2886 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode);
2887 }
2888 }
2889
2890 static nir_ssa_def *
2891 get_image_coord(struct vtn_builder *b, uint32_t value)
2892 {
2893 nir_ssa_def *coord = vtn_get_nir_ssa(b, value);
2894
2895 /* The image_load_store intrinsics assume a 4-dim coordinate */
2896 unsigned swizzle[4];
2897 for (unsigned i = 0; i < 4; i++)
2898 swizzle[i] = MIN2(i, coord->num_components - 1);
2899
2900 return nir_swizzle(&b->nb, coord, swizzle, 4);
2901 }
2902
2903 static nir_ssa_def *
2904 expand_to_vec4(nir_builder *b, nir_ssa_def *value)
2905 {
2906 if (value->num_components == 4)
2907 return value;
2908
2909 unsigned swiz[4];
2910 for (unsigned i = 0; i < 4; i++)
2911 swiz[i] = i < value->num_components ? i : 0;
2912 return nir_swizzle(b, value, swiz, 4);
2913 }
2914
2915 static void
2916 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
2917 const uint32_t *w, unsigned count)
2918 {
2919 /* Just get this one out of the way */
2920 if (opcode == SpvOpImageTexelPointer) {
2921 struct vtn_value *val =
2922 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
2923 val->image = ralloc(b, struct vtn_image_pointer);
2924
2925 val->image->image = vtn_nir_deref(b, w[3]);
2926 val->image->coord = get_image_coord(b, w[4]);
2927 val->image->sample = vtn_get_nir_ssa(b, w[5]);
2928 val->image->lod = nir_imm_int(&b->nb, 0);
2929 return;
2930 }
2931
2932 struct vtn_image_pointer image;
2933 SpvScope scope = SpvScopeInvocation;
2934 SpvMemorySemanticsMask semantics = 0;
2935
2936 enum gl_access_qualifier access = 0;
2937
2938 struct vtn_value *res_val;
2939 switch (opcode) {
2940 case SpvOpAtomicExchange:
2941 case SpvOpAtomicCompareExchange:
2942 case SpvOpAtomicCompareExchangeWeak:
2943 case SpvOpAtomicIIncrement:
2944 case SpvOpAtomicIDecrement:
2945 case SpvOpAtomicIAdd:
2946 case SpvOpAtomicISub:
2947 case SpvOpAtomicLoad:
2948 case SpvOpAtomicSMin:
2949 case SpvOpAtomicUMin:
2950 case SpvOpAtomicSMax:
2951 case SpvOpAtomicUMax:
2952 case SpvOpAtomicAnd:
2953 case SpvOpAtomicOr:
2954 case SpvOpAtomicXor:
2955 case SpvOpAtomicFAddEXT:
2956 res_val = vtn_value(b, w[3], vtn_value_type_image_pointer);
2957 image = *res_val->image;
2958 scope = vtn_constant_uint(b, w[4]);
2959 semantics = vtn_constant_uint(b, w[5]);
2960 access |= ACCESS_COHERENT;
2961 break;
2962
2963 case SpvOpAtomicStore:
2964 res_val = vtn_value(b, w[1], vtn_value_type_image_pointer);
2965 image = *res_val->image;
2966 scope = vtn_constant_uint(b, w[2]);
2967 semantics = vtn_constant_uint(b, w[3]);
2968 access |= ACCESS_COHERENT;
2969 break;
2970
2971 case SpvOpImageQuerySize:
2972 res_val = vtn_untyped_value(b, w[3]);
2973 image.image = vtn_get_image(b, w[3]);
2974 image.coord = NULL;
2975 image.sample = NULL;
2976 image.lod = NULL;
2977 break;
2978
2979 case SpvOpImageRead: {
2980 res_val = vtn_untyped_value(b, w[3]);
2981 image.image = vtn_get_image(b, w[3]);
2982 image.coord = get_image_coord(b, w[4]);
2983
2984 const SpvImageOperandsMask operands =
2985 count > 5 ? w[5] : SpvImageOperandsMaskNone;
2986
2987 if (operands & SpvImageOperandsSampleMask) {
2988 uint32_t arg = image_operand_arg(b, w, count, 5,
2989 SpvImageOperandsSampleMask);
2990 image.sample = vtn_get_nir_ssa(b, w[arg]);
2991 } else {
2992 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2993 }
2994
2995 if (operands & SpvImageOperandsMakeTexelVisibleMask) {
2996 vtn_fail_if((operands & SpvImageOperandsNonPrivateTexelMask) == 0,
2997 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2998 uint32_t arg = image_operand_arg(b, w, count, 5,
2999 SpvImageOperandsMakeTexelVisibleMask);
3000 semantics = SpvMemorySemanticsMakeVisibleMask;
3001 scope = vtn_constant_uint(b, w[arg]);
3002 }
3003
3004 if (operands & SpvImageOperandsLodMask) {
3005 uint32_t arg = image_operand_arg(b, w, count, 5,
3006 SpvImageOperandsLodMask);
3007 image.lod = vtn_get_nir_ssa(b, w[arg]);
3008 } else {
3009 image.lod = nir_imm_int(&b->nb, 0);
3010 }
3011
3012 /* TODO: Volatile. */
3013
3014 break;
3015 }
3016
3017 case SpvOpImageWrite: {
3018 res_val = vtn_untyped_value(b, w[1]);
3019 image.image = vtn_get_image(b, w[1]);
3020 image.coord = get_image_coord(b, w[2]);
3021
3022 /* texel = w[3] */
3023
3024 const SpvImageOperandsMask operands =
3025 count > 4 ? w[4] : SpvImageOperandsMaskNone;
3026
3027 if (operands & SpvImageOperandsSampleMask) {
3028 uint32_t arg = image_operand_arg(b, w, count, 4,
3029 SpvImageOperandsSampleMask);
3030 image.sample = vtn_get_nir_ssa(b, w[arg]);
3031 } else {
3032 image.sample = nir_ssa_undef(&b->nb, 1, 32);
3033 }
3034
3035 if (operands & SpvImageOperandsMakeTexelAvailableMask) {
3036 vtn_fail_if((operands & SpvImageOperandsNonPrivateTexelMask) == 0,
3037 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
3038 uint32_t arg = image_operand_arg(b, w, count, 4,
3039 SpvImageOperandsMakeTexelAvailableMask);
3040 semantics = SpvMemorySemanticsMakeAvailableMask;
3041 scope = vtn_constant_uint(b, w[arg]);
3042 }
3043
3044 if (operands & SpvImageOperandsLodMask) {
3045 uint32_t arg = image_operand_arg(b, w, count, 4,
3046 SpvImageOperandsLodMask);
3047 image.lod = vtn_get_nir_ssa(b, w[arg]);
3048 } else {
3049 image.lod = nir_imm_int(&b->nb, 0);
3050 }
3051
3052 /* TODO: Volatile. */
3053
3054 break;
3055 }
3056
3057 default:
3058 vtn_fail_with_opcode("Invalid image opcode", opcode);
3059 }
3060
3061 nir_intrinsic_op op;
3062 switch (opcode) {
3063 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
3064 OP(ImageQuerySize, size)
3065 OP(ImageRead, load)
3066 OP(ImageWrite, store)
3067 OP(AtomicLoad, load)
3068 OP(AtomicStore, store)
3069 OP(AtomicExchange, atomic_exchange)
3070 OP(AtomicCompareExchange, atomic_comp_swap)
3071 OP(AtomicCompareExchangeWeak, atomic_comp_swap)
3072 OP(AtomicIIncrement, atomic_add)
3073 OP(AtomicIDecrement, atomic_add)
3074 OP(AtomicIAdd, atomic_add)
3075 OP(AtomicISub, atomic_add)
3076 OP(AtomicSMin, atomic_imin)
3077 OP(AtomicUMin, atomic_umin)
3078 OP(AtomicSMax, atomic_imax)
3079 OP(AtomicUMax, atomic_umax)
3080 OP(AtomicAnd, atomic_and)
3081 OP(AtomicOr, atomic_or)
3082 OP(AtomicXor, atomic_xor)
3083 OP(AtomicFAddEXT, atomic_fadd)
3084 #undef OP
3085 default:
3086 vtn_fail_with_opcode("Invalid image opcode", opcode);
3087 }
3088
3089 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
3090
3091 intrin->src[0] = nir_src_for_ssa(&image.image->dest.ssa);
3092
3093 if (opcode == SpvOpImageQuerySize) {
3094 /* ImageQuerySize only has an LOD which is currently always 0 */
3095 intrin->src[1] = nir_src_for_ssa(nir_imm_int(&b->nb, 0));
3096 } else {
3097 /* The image coordinate is always 4 components but we may not have that
3098 * many. Swizzle to compensate.
3099 */
3100 intrin->src[1] = nir_src_for_ssa(expand_to_vec4(&b->nb, image.coord));
3101 intrin->src[2] = nir_src_for_ssa(image.sample);
3102 }
3103
3104 /* The Vulkan spec says:
3105 *
3106 * "If an instruction loads from or stores to a resource (including
3107 * atomics and image instructions) and the resource descriptor being
3108 * accessed is not dynamically uniform, then the operand corresponding
3109 * to that resource (e.g. the pointer or sampled image operand) must be
3110 * decorated with NonUniform."
3111 *
3112 * It's very careful to specify that the exact operand must be decorated
3113 * NonUniform. The SPIR-V parser is not expected to chase through long
3114 * chains to find the NonUniform decoration. It's either right there or we
3115 * can assume it doesn't exist.
3116 */
3117 vtn_foreach_decoration(b, res_val, non_uniform_decoration_cb, &access);
3118 nir_intrinsic_set_access(intrin, access);
3119
3120 switch (opcode) {
3121 case SpvOpAtomicLoad:
3122 case SpvOpImageQuerySize:
3123 case SpvOpImageRead:
3124 if (opcode == SpvOpImageRead || opcode == SpvOpAtomicLoad) {
3125 /* Only OpImageRead can support a lod parameter if
3126 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3127 * intrinsics definition for atomics requires us to set it for
3128 * OpAtomicLoad.
3129 */
3130 intrin->src[3] = nir_src_for_ssa(image.lod);
3131 }
3132 break;
3133 case SpvOpAtomicStore:
3134 case SpvOpImageWrite: {
3135 const uint32_t value_id = opcode == SpvOpAtomicStore ? w[4] : w[3];
3136 struct vtn_ssa_value *value = vtn_ssa_value(b, value_id);
3137 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3138 assert(op == nir_intrinsic_image_deref_store);
3139 intrin->num_components = 4;
3140 intrin->src[3] = nir_src_for_ssa(expand_to_vec4(&b->nb, value->def));
3141 /* Only OpImageWrite can support a lod parameter if
3142 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3143 * intrinsics definition for atomics requires us to set it for
3144 * OpAtomicStore.
3145 */
3146 intrin->src[4] = nir_src_for_ssa(image.lod);
3147
3148 if (opcode == SpvOpImageWrite)
3149 nir_intrinsic_set_type(intrin, nir_get_nir_type_for_glsl_type(value->type));
3150 break;
3151 }
3152
3153 case SpvOpAtomicCompareExchange:
3154 case SpvOpAtomicCompareExchangeWeak:
3155 case SpvOpAtomicIIncrement:
3156 case SpvOpAtomicIDecrement:
3157 case SpvOpAtomicExchange:
3158 case SpvOpAtomicIAdd:
3159 case SpvOpAtomicISub:
3160 case SpvOpAtomicSMin:
3161 case SpvOpAtomicUMin:
3162 case SpvOpAtomicSMax:
3163 case SpvOpAtomicUMax:
3164 case SpvOpAtomicAnd:
3165 case SpvOpAtomicOr:
3166 case SpvOpAtomicXor:
3167 case SpvOpAtomicFAddEXT:
3168 fill_common_atomic_sources(b, opcode, w, &intrin->src[3]);
3169 break;
3170
3171 default:
3172 vtn_fail_with_opcode("Invalid image opcode", opcode);
3173 }
3174
3175 /* Image operations implicitly have the Image storage memory semantics. */
3176 semantics |= SpvMemorySemanticsImageMemoryMask;
3177
3178 SpvMemorySemanticsMask before_semantics;
3179 SpvMemorySemanticsMask after_semantics;
3180 vtn_split_barrier_semantics(b, semantics, &before_semantics, &after_semantics);
3181
3182 if (before_semantics)
3183 vtn_emit_memory_barrier(b, scope, before_semantics);
3184
3185 if (opcode != SpvOpImageWrite && opcode != SpvOpAtomicStore) {
3186 struct vtn_type *type = vtn_get_type(b, w[1]);
3187
3188 unsigned dest_components = glsl_get_vector_elements(type->type);
3189 if (nir_intrinsic_infos[op].dest_components == 0)
3190 intrin->num_components = dest_components;
3191
3192 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
3193 nir_intrinsic_dest_components(intrin), 32, NULL);
3194
3195 nir_builder_instr_insert(&b->nb, &intrin->instr);
3196
3197 nir_ssa_def *result = &intrin->dest.ssa;
3198 if (nir_intrinsic_dest_components(intrin) != dest_components)
3199 result = nir_channels(&b->nb, result, (1 << dest_components) - 1);
3200
3201 vtn_push_nir_ssa(b, w[2], result);
3202
3203 if (opcode == SpvOpImageRead)
3204 nir_intrinsic_set_type(intrin, nir_get_nir_type_for_glsl_type(type->type));
3205 } else {
3206 nir_builder_instr_insert(&b->nb, &intrin->instr);
3207 }
3208
3209 if (after_semantics)
3210 vtn_emit_memory_barrier(b, scope, after_semantics);
3211 }
3212
3213 static nir_intrinsic_op
3214 get_ssbo_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
3215 {
3216 switch (opcode) {
3217 case SpvOpAtomicLoad: return nir_intrinsic_load_ssbo;
3218 case SpvOpAtomicStore: return nir_intrinsic_store_ssbo;
3219 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3220 OP(AtomicExchange, atomic_exchange)
3221 OP(AtomicCompareExchange, atomic_comp_swap)
3222 OP(AtomicCompareExchangeWeak, atomic_comp_swap)
3223 OP(AtomicIIncrement, atomic_add)
3224 OP(AtomicIDecrement, atomic_add)
3225 OP(AtomicIAdd, atomic_add)
3226 OP(AtomicISub, atomic_add)
3227 OP(AtomicSMin, atomic_imin)
3228 OP(AtomicUMin, atomic_umin)
3229 OP(AtomicSMax, atomic_imax)
3230 OP(AtomicUMax, atomic_umax)
3231 OP(AtomicAnd, atomic_and)
3232 OP(AtomicOr, atomic_or)
3233 OP(AtomicXor, atomic_xor)
3234 OP(AtomicFAddEXT, atomic_fadd)
3235 #undef OP
3236 default:
3237 vtn_fail_with_opcode("Invalid SSBO atomic", opcode);
3238 }
3239 }
3240
3241 static nir_intrinsic_op
3242 get_uniform_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
3243 {
3244 switch (opcode) {
3245 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3246 OP(AtomicLoad, read_deref)
3247 OP(AtomicExchange, exchange)
3248 OP(AtomicCompareExchange, comp_swap)
3249 OP(AtomicCompareExchangeWeak, comp_swap)
3250 OP(AtomicIIncrement, inc_deref)
3251 OP(AtomicIDecrement, post_dec_deref)
3252 OP(AtomicIAdd, add_deref)
3253 OP(AtomicISub, add_deref)
3254 OP(AtomicUMin, min_deref)
3255 OP(AtomicUMax, max_deref)
3256 OP(AtomicAnd, and_deref)
3257 OP(AtomicOr, or_deref)
3258 OP(AtomicXor, xor_deref)
3259 #undef OP
3260 default:
3261 /* We left the following out: AtomicStore, AtomicSMin and
3262 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3263 * moment Atomic Counter support is needed for ARB_spirv support, so is
3264 * only need to support GLSL Atomic Counters that are uints and don't
3265 * allow direct storage.
3266 */
3267 vtn_fail("Invalid uniform atomic");
3268 }
3269 }
3270
3271 static nir_intrinsic_op
3272 get_deref_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
3273 {
3274 switch (opcode) {
3275 case SpvOpAtomicLoad: return nir_intrinsic_load_deref;
3276 case SpvOpAtomicStore: return nir_intrinsic_store_deref;
3277 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3278 OP(AtomicExchange, atomic_exchange)
3279 OP(AtomicCompareExchange, atomic_comp_swap)
3280 OP(AtomicCompareExchangeWeak, atomic_comp_swap)
3281 OP(AtomicIIncrement, atomic_add)
3282 OP(AtomicIDecrement, atomic_add)
3283 OP(AtomicIAdd, atomic_add)
3284 OP(AtomicISub, atomic_add)
3285 OP(AtomicSMin, atomic_imin)
3286 OP(AtomicUMin, atomic_umin)
3287 OP(AtomicSMax, atomic_imax)
3288 OP(AtomicUMax, atomic_umax)
3289 OP(AtomicAnd, atomic_and)
3290 OP(AtomicOr, atomic_or)
3291 OP(AtomicXor, atomic_xor)
3292 OP(AtomicFAddEXT, atomic_fadd)
3293 #undef OP
3294 default:
3295 vtn_fail_with_opcode("Invalid shared atomic", opcode);
3296 }
3297 }
3298
3299 /*
3300 * Handles shared atomics, ssbo atomics and atomic counters.
3301 */
3302 static void
3303 vtn_handle_atomics(struct vtn_builder *b, SpvOp opcode,
3304 const uint32_t *w, UNUSED unsigned count)
3305 {
3306 struct vtn_pointer *ptr;
3307 nir_intrinsic_instr *atomic;
3308
3309 SpvScope scope = SpvScopeInvocation;
3310 SpvMemorySemanticsMask semantics = 0;
3311
3312 switch (opcode) {
3313 case SpvOpAtomicLoad:
3314 case SpvOpAtomicExchange:
3315 case SpvOpAtomicCompareExchange:
3316 case SpvOpAtomicCompareExchangeWeak:
3317 case SpvOpAtomicIIncrement:
3318 case SpvOpAtomicIDecrement:
3319 case SpvOpAtomicIAdd:
3320 case SpvOpAtomicISub:
3321 case SpvOpAtomicSMin:
3322 case SpvOpAtomicUMin:
3323 case SpvOpAtomicSMax:
3324 case SpvOpAtomicUMax:
3325 case SpvOpAtomicAnd:
3326 case SpvOpAtomicOr:
3327 case SpvOpAtomicXor:
3328 case SpvOpAtomicFAddEXT:
3329 ptr = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
3330 scope = vtn_constant_uint(b, w[4]);
3331 semantics = vtn_constant_uint(b, w[5]);
3332 break;
3333
3334 case SpvOpAtomicStore:
3335 ptr = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
3336 scope = vtn_constant_uint(b, w[2]);
3337 semantics = vtn_constant_uint(b, w[3]);
3338 break;
3339
3340 default:
3341 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode);
3342 }
3343
3344 /* uniform as "atomic counter uniform" */
3345 if (ptr->mode == vtn_variable_mode_atomic_counter) {
3346 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
3347 nir_intrinsic_op op = get_uniform_nir_atomic_op(b, opcode);
3348 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
3349 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
3350
3351 /* SSBO needs to initialize index/offset. In this case we don't need to,
3352 * as that info is already stored on the ptr->var->var nir_variable (see
3353 * vtn_create_variable)
3354 */
3355
3356 switch (opcode) {
3357 case SpvOpAtomicLoad:
3358 case SpvOpAtomicExchange:
3359 case SpvOpAtomicCompareExchange:
3360 case SpvOpAtomicCompareExchangeWeak:
3361 case SpvOpAtomicIIncrement:
3362 case SpvOpAtomicIDecrement:
3363 case SpvOpAtomicIAdd:
3364 case SpvOpAtomicISub:
3365 case SpvOpAtomicSMin:
3366 case SpvOpAtomicUMin:
3367 case SpvOpAtomicSMax:
3368 case SpvOpAtomicUMax:
3369 case SpvOpAtomicAnd:
3370 case SpvOpAtomicOr:
3371 case SpvOpAtomicXor:
3372 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3373 * atomic counter uniforms doesn't have sources
3374 */
3375 break;
3376
3377 default:
3378 unreachable("Invalid SPIR-V atomic");
3379
3380 }
3381 } else if (vtn_pointer_uses_ssa_offset(b, ptr)) {
3382 nir_ssa_def *offset, *index;
3383 offset = vtn_pointer_to_offset(b, ptr, &index);
3384
3385 assert(ptr->mode == vtn_variable_mode_ssbo);
3386
3387 nir_intrinsic_op op = get_ssbo_nir_atomic_op(b, opcode);
3388 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
3389
3390 nir_intrinsic_set_access(atomic, ACCESS_COHERENT);
3391
3392 int src = 0;
3393 switch (opcode) {
3394 case SpvOpAtomicLoad:
3395 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
3396 nir_intrinsic_set_align(atomic, 4, 0);
3397 if (ptr->mode == vtn_variable_mode_ssbo)
3398 atomic->src[src++] = nir_src_for_ssa(index);
3399 atomic->src[src++] = nir_src_for_ssa(offset);
3400 break;
3401
3402 case SpvOpAtomicStore:
3403 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
3404 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
3405 nir_intrinsic_set_align(atomic, 4, 0);
3406 atomic->src[src++] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[4]));
3407 if (ptr->mode == vtn_variable_mode_ssbo)
3408 atomic->src[src++] = nir_src_for_ssa(index);
3409 atomic->src[src++] = nir_src_for_ssa(offset);
3410 break;
3411
3412 case SpvOpAtomicExchange:
3413 case SpvOpAtomicCompareExchange:
3414 case SpvOpAtomicCompareExchangeWeak:
3415 case SpvOpAtomicIIncrement:
3416 case SpvOpAtomicIDecrement:
3417 case SpvOpAtomicIAdd:
3418 case SpvOpAtomicISub:
3419 case SpvOpAtomicSMin:
3420 case SpvOpAtomicUMin:
3421 case SpvOpAtomicSMax:
3422 case SpvOpAtomicUMax:
3423 case SpvOpAtomicAnd:
3424 case SpvOpAtomicOr:
3425 case SpvOpAtomicXor:
3426 case SpvOpAtomicFAddEXT:
3427 if (ptr->mode == vtn_variable_mode_ssbo)
3428 atomic->src[src++] = nir_src_for_ssa(index);
3429 atomic->src[src++] = nir_src_for_ssa(offset);
3430 fill_common_atomic_sources(b, opcode, w, &atomic->src[src]);
3431 break;
3432
3433 default:
3434 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode);
3435 }
3436 } else {
3437 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
3438 const struct glsl_type *deref_type = deref->type;
3439 nir_intrinsic_op op = get_deref_nir_atomic_op(b, opcode);
3440 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
3441 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
3442
3443 if (ptr->mode != vtn_variable_mode_workgroup)
3444 nir_intrinsic_set_access(atomic, ACCESS_COHERENT);
3445
3446 switch (opcode) {
3447 case SpvOpAtomicLoad:
3448 atomic->num_components = glsl_get_vector_elements(deref_type);
3449 break;
3450
3451 case SpvOpAtomicStore:
3452 atomic->num_components = glsl_get_vector_elements(deref_type);
3453 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
3454 atomic->src[1] = nir_src_for_ssa(vtn_get_nir_ssa(b, w[4]));
3455 break;
3456
3457 case SpvOpAtomicExchange:
3458 case SpvOpAtomicCompareExchange:
3459 case SpvOpAtomicCompareExchangeWeak:
3460 case SpvOpAtomicIIncrement:
3461 case SpvOpAtomicIDecrement:
3462 case SpvOpAtomicIAdd:
3463 case SpvOpAtomicISub:
3464 case SpvOpAtomicSMin:
3465 case SpvOpAtomicUMin:
3466 case SpvOpAtomicSMax:
3467 case SpvOpAtomicUMax:
3468 case SpvOpAtomicAnd:
3469 case SpvOpAtomicOr:
3470 case SpvOpAtomicXor:
3471 case SpvOpAtomicFAddEXT:
3472 fill_common_atomic_sources(b, opcode, w, &atomic->src[1]);
3473 break;
3474
3475 default:
3476 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode);
3477 }
3478 }
3479
3480 /* Atomic ordering operations will implicitly apply to the atomic operation
3481 * storage class, so include that too.
3482 */
3483 semantics |= vtn_storage_class_to_memory_semantics(ptr->ptr_type->storage_class);
3484
3485 SpvMemorySemanticsMask before_semantics;
3486 SpvMemorySemanticsMask after_semantics;
3487 vtn_split_barrier_semantics(b, semantics, &before_semantics, &after_semantics);
3488
3489 if (before_semantics)
3490 vtn_emit_memory_barrier(b, scope, before_semantics);
3491
3492 if (opcode != SpvOpAtomicStore) {
3493 struct vtn_type *type = vtn_get_type(b, w[1]);
3494
3495 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
3496 glsl_get_vector_elements(type->type),
3497 glsl_get_bit_size(type->type), NULL);
3498
3499 vtn_push_nir_ssa(b, w[2], &atomic->dest.ssa);
3500 }
3501
3502 nir_builder_instr_insert(&b->nb, &atomic->instr);
3503
3504 if (after_semantics)
3505 vtn_emit_memory_barrier(b, scope, after_semantics);
3506 }
3507
3508 static nir_alu_instr *
3509 create_vec(struct vtn_builder *b, unsigned num_components, unsigned bit_size)
3510 {
3511 nir_op op = nir_op_vec(num_components);
3512 nir_alu_instr *vec = nir_alu_instr_create(b->shader, op);
3513 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
3514 bit_size, NULL);
3515 vec->dest.write_mask = (1 << num_components) - 1;
3516
3517 return vec;
3518 }
3519
3520 struct vtn_ssa_value *
3521 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
3522 {
3523 if (src->transposed)
3524 return src->transposed;
3525
3526 struct vtn_ssa_value *dest =
3527 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
3528
3529 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
3530 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type),
3531 glsl_get_bit_size(src->type));
3532 if (glsl_type_is_vector_or_scalar(src->type)) {
3533 vec->src[0].src = nir_src_for_ssa(src->def);
3534 vec->src[0].swizzle[0] = i;
3535 } else {
3536 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
3537 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
3538 vec->src[j].swizzle[0] = i;
3539 }
3540 }
3541 nir_builder_instr_insert(&b->nb, &vec->instr);
3542 dest->elems[i]->def = &vec->dest.dest.ssa;
3543 }
3544
3545 dest->transposed = src;
3546
3547 return dest;
3548 }
3549
3550 static nir_ssa_def *
3551 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
3552 nir_ssa_def *src0, nir_ssa_def *src1,
3553 const uint32_t *indices)
3554 {
3555 nir_alu_instr *vec = create_vec(b, num_components, src0->bit_size);
3556
3557 for (unsigned i = 0; i < num_components; i++) {
3558 uint32_t index = indices[i];
3559 if (index == 0xffffffff) {
3560 vec->src[i].src =
3561 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
3562 } else if (index < src0->num_components) {
3563 vec->src[i].src = nir_src_for_ssa(src0);
3564 vec->src[i].swizzle[0] = index;
3565 } else {
3566 vec->src[i].src = nir_src_for_ssa(src1);
3567 vec->src[i].swizzle[0] = index - src0->num_components;
3568 }
3569 }
3570
3571 nir_builder_instr_insert(&b->nb, &vec->instr);
3572
3573 return &vec->dest.dest.ssa;
3574 }
3575
3576 /*
3577 * Concatentates a number of vectors/scalars together to produce a vector
3578 */
3579 static nir_ssa_def *
3580 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
3581 unsigned num_srcs, nir_ssa_def **srcs)
3582 {
3583 nir_alu_instr *vec = create_vec(b, num_components, srcs[0]->bit_size);
3584
3585 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3586 *
3587 * "When constructing a vector, there must be at least two Constituent
3588 * operands."
3589 */
3590 vtn_assert(num_srcs >= 2);
3591
3592 unsigned dest_idx = 0;
3593 for (unsigned i = 0; i < num_srcs; i++) {
3594 nir_ssa_def *src = srcs[i];
3595 vtn_assert(dest_idx + src->num_components <= num_components);
3596 for (unsigned j = 0; j < src->num_components; j++) {
3597 vec->src[dest_idx].src = nir_src_for_ssa(src);
3598 vec->src[dest_idx].swizzle[0] = j;
3599 dest_idx++;
3600 }
3601 }
3602
3603 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3604 *
3605 * "When constructing a vector, the total number of components in all
3606 * the operands must equal the number of components in Result Type."
3607 */
3608 vtn_assert(dest_idx == num_components);
3609
3610 nir_builder_instr_insert(&b->nb, &vec->instr);
3611
3612 return &vec->dest.dest.ssa;
3613 }
3614
3615 static struct vtn_ssa_value *
3616 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
3617 {
3618 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
3619 dest->type = src->type;
3620
3621 if (glsl_type_is_vector_or_scalar(src->type)) {
3622 dest->def = src->def;
3623 } else {
3624 unsigned elems = glsl_get_length(src->type);
3625
3626 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
3627 for (unsigned i = 0; i < elems; i++)
3628 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
3629 }
3630
3631 return dest;
3632 }
3633
3634 static struct vtn_ssa_value *
3635 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
3636 struct vtn_ssa_value *insert, const uint32_t *indices,
3637 unsigned num_indices)
3638 {
3639 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
3640
3641 struct vtn_ssa_value *cur = dest;
3642 unsigned i;
3643 for (i = 0; i < num_indices - 1; i++) {
3644 /* If we got a vector here, that means the next index will be trying to
3645 * dereference a scalar.
3646 */
3647 vtn_fail_if(glsl_type_is_vector_or_scalar(cur->type),
3648 "OpCompositeInsert has too many indices.");
3649 vtn_fail_if(indices[i] >= glsl_get_length(cur->type),
3650 "All indices in an OpCompositeInsert must be in-bounds");
3651 cur = cur->elems[indices[i]];
3652 }
3653
3654 if (glsl_type_is_vector_or_scalar(cur->type)) {
3655 vtn_fail_if(indices[i] >= glsl_get_vector_elements(cur->type),
3656 "All indices in an OpCompositeInsert must be in-bounds");
3657
3658 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3659 * the component granularity. In that case, the last index will be
3660 * the index to insert the scalar into the vector.
3661 */
3662
3663 cur->def = nir_vector_insert_imm(&b->nb, cur->def, insert->def, indices[i]);
3664 } else {
3665 vtn_fail_if(indices[i] >= glsl_get_length(cur->type),
3666 "All indices in an OpCompositeInsert must be in-bounds");
3667 cur->elems[indices[i]] = insert;
3668 }
3669
3670 return dest;
3671 }
3672
3673 static struct vtn_ssa_value *
3674 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
3675 const uint32_t *indices, unsigned num_indices)
3676 {
3677 struct vtn_ssa_value *cur = src;
3678 for (unsigned i = 0; i < num_indices; i++) {
3679 if (glsl_type_is_vector_or_scalar(cur->type)) {
3680 vtn_assert(i == num_indices - 1);
3681 vtn_fail_if(indices[i] >= glsl_get_vector_elements(cur->type),
3682 "All indices in an OpCompositeExtract must be in-bounds");
3683
3684 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3685 * the component granularity. The last index will be the index of the
3686 * vector to extract.
3687 */
3688
3689 const struct glsl_type *scalar_type =
3690 glsl_scalar_type(glsl_get_base_type(cur->type));
3691 struct vtn_ssa_value *ret = vtn_create_ssa_value(b, scalar_type);
3692 ret->def = nir_channel(&b->nb, cur->def, indices[i]);
3693 return ret;
3694 } else {
3695 vtn_fail_if(indices[i] >= glsl_get_length(cur->type),
3696 "All indices in an OpCompositeExtract must be in-bounds");
3697 cur = cur->elems[indices[i]];
3698 }
3699 }
3700
3701 return cur;
3702 }
3703
3704 static void
3705 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
3706 const uint32_t *w, unsigned count)
3707 {
3708 struct vtn_type *type = vtn_get_type(b, w[1]);
3709 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, type->type);
3710
3711 switch (opcode) {
3712 case SpvOpVectorExtractDynamic:
3713 ssa->def = nir_vector_extract(&b->nb, vtn_get_nir_ssa(b, w[3]),
3714 vtn_get_nir_ssa(b, w[4]));
3715 break;
3716
3717 case SpvOpVectorInsertDynamic:
3718 ssa->def = nir_vector_insert(&b->nb, vtn_get_nir_ssa(b, w[3]),
3719 vtn_get_nir_ssa(b, w[4]),
3720 vtn_get_nir_ssa(b, w[5]));
3721 break;
3722
3723 case SpvOpVectorShuffle:
3724 ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type->type),
3725 vtn_get_nir_ssa(b, w[3]),
3726 vtn_get_nir_ssa(b, w[4]),
3727 w + 5);
3728 break;
3729
3730 case SpvOpCompositeConstruct: {
3731 unsigned elems = count - 3;
3732 assume(elems >= 1);
3733 if (glsl_type_is_vector_or_scalar(type->type)) {
3734 nir_ssa_def *srcs[NIR_MAX_VEC_COMPONENTS];
3735 for (unsigned i = 0; i < elems; i++)
3736 srcs[i] = vtn_get_nir_ssa(b, w[3 + i]);
3737 ssa->def =
3738 vtn_vector_construct(b, glsl_get_vector_elements(type->type),
3739 elems, srcs);
3740 } else {
3741 ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
3742 for (unsigned i = 0; i < elems; i++)
3743 ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
3744 }
3745 break;
3746 }
3747 case SpvOpCompositeExtract:
3748 ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
3749 w + 4, count - 4);
3750 break;
3751
3752 case SpvOpCompositeInsert:
3753 ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
3754 vtn_ssa_value(b, w[3]),
3755 w + 5, count - 5);
3756 break;
3757
3758 case SpvOpCopyLogical:
3759 ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
3760 break;
3761 case SpvOpCopyObject:
3762 vtn_copy_value(b, w[3], w[2]);
3763 return;
3764
3765 default:
3766 vtn_fail_with_opcode("unknown composite operation", opcode);
3767 }
3768
3769 vtn_push_ssa_value(b, w[2], ssa);
3770 }
3771
3772 static void
3773 vtn_emit_barrier(struct vtn_builder *b, nir_intrinsic_op op)
3774 {
3775 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
3776 nir_builder_instr_insert(&b->nb, &intrin->instr);
3777 }
3778
3779 void
3780 vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
3781 SpvMemorySemanticsMask semantics)
3782 {
3783 if (b->shader->options->use_scoped_barrier) {
3784 vtn_emit_scoped_memory_barrier(b, scope, semantics);
3785 return;
3786 }
3787
3788 static const SpvMemorySemanticsMask all_memory_semantics =
3789 SpvMemorySemanticsUniformMemoryMask |
3790 SpvMemorySemanticsWorkgroupMemoryMask |
3791 SpvMemorySemanticsAtomicCounterMemoryMask |
3792 SpvMemorySemanticsImageMemoryMask |
3793 SpvMemorySemanticsOutputMemoryMask;
3794
3795 /* If we're not actually doing a memory barrier, bail */
3796 if (!(semantics & all_memory_semantics))
3797 return;
3798
3799 /* GL and Vulkan don't have these */
3800 vtn_assert(scope != SpvScopeCrossDevice);
3801
3802 if (scope == SpvScopeSubgroup)
3803 return; /* Nothing to do here */
3804
3805 if (scope == SpvScopeWorkgroup) {
3806 vtn_emit_barrier(b, nir_intrinsic_group_memory_barrier);
3807 return;
3808 }
3809
3810 /* There's only two scopes thing left */
3811 vtn_assert(scope == SpvScopeInvocation || scope == SpvScopeDevice);
3812
3813 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3814 * semantic to the corresponding NIR one.
3815 */
3816 if (util_bitcount(semantics & all_memory_semantics) > 1) {
3817 vtn_emit_barrier(b, nir_intrinsic_memory_barrier);
3818 if (semantics & SpvMemorySemanticsOutputMemoryMask) {
3819 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3820 * TCS outputs, so we have to emit it's own intrinsic for that. We
3821 * then need to emit another memory_barrier to prevent moving
3822 * non-output operations to before the tcs_patch barrier.
3823 */
3824 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_tcs_patch);
3825 vtn_emit_barrier(b, nir_intrinsic_memory_barrier);
3826 }
3827 return;
3828 }
3829
3830 /* Issue a more specific barrier */
3831 switch (semantics & all_memory_semantics) {
3832 case SpvMemorySemanticsUniformMemoryMask:
3833 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_buffer);
3834 break;
3835 case SpvMemorySemanticsWorkgroupMemoryMask:
3836 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_shared);
3837 break;
3838 case SpvMemorySemanticsAtomicCounterMemoryMask:
3839 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_atomic_counter);
3840 break;
3841 case SpvMemorySemanticsImageMemoryMask:
3842 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_image);
3843 break;
3844 case SpvMemorySemanticsOutputMemoryMask:
3845 if (b->nb.shader->info.stage == MESA_SHADER_TESS_CTRL)
3846 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_tcs_patch);
3847 break;
3848 default:
3849 break;
3850 }
3851 }
3852
3853 static void
3854 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
3855 const uint32_t *w, UNUSED unsigned count)
3856 {
3857 switch (opcode) {
3858 case SpvOpEmitVertex:
3859 case SpvOpEmitStreamVertex:
3860 case SpvOpEndPrimitive:
3861 case SpvOpEndStreamPrimitive: {
3862 nir_intrinsic_op intrinsic_op;
3863 switch (opcode) {
3864 case SpvOpEmitVertex:
3865 case SpvOpEmitStreamVertex:
3866 intrinsic_op = nir_intrinsic_emit_vertex;
3867 break;
3868 case SpvOpEndPrimitive:
3869 case SpvOpEndStreamPrimitive:
3870 intrinsic_op = nir_intrinsic_end_primitive;
3871 break;
3872 default:
3873 unreachable("Invalid opcode");
3874 }
3875
3876 nir_intrinsic_instr *intrin =
3877 nir_intrinsic_instr_create(b->shader, intrinsic_op);
3878
3879 switch (opcode) {
3880 case SpvOpEmitStreamVertex:
3881 case SpvOpEndStreamPrimitive: {
3882 unsigned stream = vtn_constant_uint(b, w[1]);
3883 nir_intrinsic_set_stream_id(intrin, stream);
3884 break;
3885 }
3886
3887 default:
3888 break;
3889 }
3890
3891 nir_builder_instr_insert(&b->nb, &intrin->instr);
3892 break;
3893 }
3894
3895 case SpvOpMemoryBarrier: {
3896 SpvScope scope = vtn_constant_uint(b, w[1]);
3897 SpvMemorySemanticsMask semantics = vtn_constant_uint(b, w[2]);
3898 vtn_emit_memory_barrier(b, scope, semantics);
3899 return;
3900 }
3901
3902 case SpvOpControlBarrier: {
3903 SpvScope execution_scope = vtn_constant_uint(b, w[1]);
3904 SpvScope memory_scope = vtn_constant_uint(b, w[2]);
3905 SpvMemorySemanticsMask memory_semantics = vtn_constant_uint(b, w[3]);
3906
3907 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3908 * memory semantics of None for GLSL barrier().
3909 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3910 * Device instead of Workgroup for execution scope.
3911 */
3912 if (b->wa_glslang_cs_barrier &&
3913 b->nb.shader->info.stage == MESA_SHADER_COMPUTE &&
3914 (execution_scope == SpvScopeWorkgroup ||
3915 execution_scope == SpvScopeDevice) &&
3916 memory_semantics == SpvMemorySemanticsMaskNone) {
3917 execution_scope = SpvScopeWorkgroup;
3918 memory_scope = SpvScopeWorkgroup;
3919 memory_semantics = SpvMemorySemanticsAcquireReleaseMask |
3920 SpvMemorySemanticsWorkgroupMemoryMask;
3921 }
3922
3923 /* From the SPIR-V spec:
3924 *
3925 * "When used with the TessellationControl execution model, it also
3926 * implicitly synchronizes the Output Storage Class: Writes to Output
3927 * variables performed by any invocation executed prior to a
3928 * OpControlBarrier will be visible to any other invocation after
3929 * return from that OpControlBarrier."
3930 */
3931 if (b->nb.shader->info.stage == MESA_SHADER_TESS_CTRL) {
3932 memory_semantics &= ~(SpvMemorySemanticsAcquireMask |
3933 SpvMemorySemanticsReleaseMask |
3934 SpvMemorySemanticsAcquireReleaseMask |
3935 SpvMemorySemanticsSequentiallyConsistentMask);
3936 memory_semantics |= SpvMemorySemanticsAcquireReleaseMask |
3937 SpvMemorySemanticsOutputMemoryMask;
3938 }
3939
3940 if (b->shader->options->use_scoped_barrier) {
3941 vtn_emit_scoped_control_barrier(b, execution_scope, memory_scope,
3942 memory_semantics);
3943 } else {
3944 vtn_emit_memory_barrier(b, memory_scope, memory_semantics);
3945
3946 if (execution_scope == SpvScopeWorkgroup)
3947 vtn_emit_barrier(b, nir_intrinsic_control_barrier);
3948 }
3949 break;
3950 }
3951
3952 default:
3953 unreachable("unknown barrier instruction");
3954 }
3955 }
3956
3957 static unsigned
3958 gl_primitive_from_spv_execution_mode(struct vtn_builder *b,
3959 SpvExecutionMode mode)
3960 {
3961 switch (mode) {
3962 case SpvExecutionModeInputPoints:
3963 case SpvExecutionModeOutputPoints:
3964 return 0; /* GL_POINTS */
3965 case SpvExecutionModeInputLines:
3966 return 1; /* GL_LINES */
3967 case SpvExecutionModeInputLinesAdjacency:
3968 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3969 case SpvExecutionModeTriangles:
3970 return 4; /* GL_TRIANGLES */
3971 case SpvExecutionModeInputTrianglesAdjacency:
3972 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3973 case SpvExecutionModeQuads:
3974 return 7; /* GL_QUADS */
3975 case SpvExecutionModeIsolines:
3976 return 0x8E7A; /* GL_ISOLINES */
3977 case SpvExecutionModeOutputLineStrip:
3978 return 3; /* GL_LINE_STRIP */
3979 case SpvExecutionModeOutputTriangleStrip:
3980 return 5; /* GL_TRIANGLE_STRIP */
3981 default:
3982 vtn_fail("Invalid primitive type: %s (%u)",
3983 spirv_executionmode_to_string(mode), mode);
3984 }
3985 }
3986
3987 static unsigned
3988 vertices_in_from_spv_execution_mode(struct vtn_builder *b,
3989 SpvExecutionMode mode)
3990 {
3991 switch (mode) {
3992 case SpvExecutionModeInputPoints:
3993 return 1;
3994 case SpvExecutionModeInputLines:
3995 return 2;
3996 case SpvExecutionModeInputLinesAdjacency:
3997 return 4;
3998 case SpvExecutionModeTriangles:
3999 return 3;
4000 case SpvExecutionModeInputTrianglesAdjacency:
4001 return 6;
4002 default:
4003 vtn_fail("Invalid GS input mode: %s (%u)",
4004 spirv_executionmode_to_string(mode), mode);
4005 }
4006 }
4007
4008 static gl_shader_stage
4009 stage_for_execution_model(struct vtn_builder *b, SpvExecutionModel model)
4010 {
4011 switch (model) {
4012 case SpvExecutionModelVertex:
4013 return MESA_SHADER_VERTEX;
4014 case SpvExecutionModelTessellationControl:
4015 return MESA_SHADER_TESS_CTRL;
4016 case SpvExecutionModelTessellationEvaluation:
4017 return MESA_SHADER_TESS_EVAL;
4018 case SpvExecutionModelGeometry:
4019 return MESA_SHADER_GEOMETRY;
4020 case SpvExecutionModelFragment:
4021 return MESA_SHADER_FRAGMENT;
4022 case SpvExecutionModelGLCompute:
4023 return MESA_SHADER_COMPUTE;
4024 case SpvExecutionModelKernel:
4025 return MESA_SHADER_KERNEL;
4026 default:
4027 vtn_fail("Unsupported execution model: %s (%u)",
4028 spirv_executionmodel_to_string(model), model);
4029 }
4030 }
4031
4032 #define spv_check_supported(name, cap) do { \
4033 if (!(b->options && b->options->caps.name)) \
4034 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
4035 spirv_capability_to_string(cap), cap); \
4036 } while(0)
4037
4038
4039 void
4040 vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
4041 unsigned count)
4042 {
4043 struct vtn_value *entry_point = &b->values[w[2]];
4044 /* Let this be a name label regardless */
4045 unsigned name_words;
4046 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
4047
4048 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
4049 stage_for_execution_model(b, w[1]) != b->entry_point_stage)
4050 return;
4051
4052 vtn_assert(b->entry_point == NULL);
4053 b->entry_point = entry_point;
4054 }
4055
4056 static bool
4057 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
4058 const uint32_t *w, unsigned count)
4059 {
4060 switch (opcode) {
4061 case SpvOpSource: {
4062 const char *lang;
4063 switch (w[1]) {
4064 default:
4065 case SpvSourceLanguageUnknown: lang = "unknown"; break;
4066 case SpvSourceLanguageESSL: lang = "ESSL"; break;
4067 case SpvSourceLanguageGLSL: lang = "GLSL"; break;
4068 case SpvSourceLanguageOpenCL_C: lang = "OpenCL C"; break;
4069 case SpvSourceLanguageOpenCL_CPP: lang = "OpenCL C++"; break;
4070 case SpvSourceLanguageHLSL: lang = "HLSL"; break;
4071 }
4072
4073 uint32_t version = w[2];
4074
4075 const char *file =
4076 (count > 3) ? vtn_value(b, w[3], vtn_value_type_string)->str : "";
4077
4078 vtn_info("Parsing SPIR-V from %s %u source file %s", lang, version, file);
4079 break;
4080 }
4081
4082 case SpvOpSourceExtension:
4083 case SpvOpSourceContinued:
4084 case SpvOpExtension:
4085 case SpvOpModuleProcessed:
4086 /* Unhandled, but these are for debug so that's ok. */
4087 break;
4088
4089 case SpvOpCapability: {
4090 SpvCapability cap = w[1];
4091 switch (cap) {
4092 case SpvCapabilityMatrix:
4093 case SpvCapabilityShader:
4094 case SpvCapabilityGeometry:
4095 case SpvCapabilityGeometryPointSize:
4096 case SpvCapabilityUniformBufferArrayDynamicIndexing:
4097 case SpvCapabilitySampledImageArrayDynamicIndexing:
4098 case SpvCapabilityStorageBufferArrayDynamicIndexing:
4099 case SpvCapabilityStorageImageArrayDynamicIndexing:
4100 case SpvCapabilityImageRect:
4101 case SpvCapabilitySampledRect:
4102 case SpvCapabilitySampled1D:
4103 case SpvCapabilityImage1D:
4104 case SpvCapabilitySampledCubeArray:
4105 case SpvCapabilityImageCubeArray:
4106 case SpvCapabilitySampledBuffer:
4107 case SpvCapabilityImageBuffer:
4108 case SpvCapabilityImageQuery:
4109 case SpvCapabilityDerivativeControl:
4110 case SpvCapabilityInterpolationFunction:
4111 case SpvCapabilityMultiViewport:
4112 case SpvCapabilitySampleRateShading:
4113 case SpvCapabilityClipDistance:
4114 case SpvCapabilityCullDistance:
4115 case SpvCapabilityInputAttachment:
4116 case SpvCapabilityImageGatherExtended:
4117 case SpvCapabilityStorageImageExtendedFormats:
4118 case SpvCapabilityVector16:
4119 break;
4120
4121 case SpvCapabilityLinkage:
4122 case SpvCapabilityFloat16Buffer:
4123 case SpvCapabilitySparseResidency:
4124 vtn_warn("Unsupported SPIR-V capability: %s",
4125 spirv_capability_to_string(cap));
4126 break;
4127
4128 case SpvCapabilityMinLod:
4129 spv_check_supported(min_lod, cap);
4130 break;
4131
4132 case SpvCapabilityAtomicStorage:
4133 spv_check_supported(atomic_storage, cap);
4134 break;
4135
4136 case SpvCapabilityFloat64:
4137 spv_check_supported(float64, cap);
4138 break;
4139 case SpvCapabilityInt64:
4140 spv_check_supported(int64, cap);
4141 break;
4142 case SpvCapabilityInt16:
4143 spv_check_supported(int16, cap);
4144 break;
4145 case SpvCapabilityInt8:
4146 spv_check_supported(int8, cap);
4147 break;
4148
4149 case SpvCapabilityTransformFeedback:
4150 spv_check_supported(transform_feedback, cap);
4151 break;
4152
4153 case SpvCapabilityGeometryStreams:
4154 spv_check_supported(geometry_streams, cap);
4155 break;
4156
4157 case SpvCapabilityInt64Atomics:
4158 spv_check_supported(int64_atomics, cap);
4159 break;
4160
4161 case SpvCapabilityStorageImageMultisample:
4162 spv_check_supported(storage_image_ms, cap);
4163 break;
4164
4165 case SpvCapabilityAddresses:
4166 spv_check_supported(address, cap);
4167 break;
4168
4169 case SpvCapabilityKernel:
4170 spv_check_supported(kernel, cap);
4171 break;
4172
4173 case SpvCapabilityImageBasic:
4174 case SpvCapabilityImageReadWrite:
4175 case SpvCapabilityImageMipmap:
4176 case SpvCapabilityPipes:
4177 case SpvCapabilityDeviceEnqueue:
4178 case SpvCapabilityLiteralSampler:
4179 case SpvCapabilityGenericPointer:
4180 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4181 spirv_capability_to_string(cap));
4182 break;
4183
4184 case SpvCapabilityImageMSArray:
4185 spv_check_supported(image_ms_array, cap);
4186 break;
4187
4188 case SpvCapabilityTessellation:
4189 case SpvCapabilityTessellationPointSize:
4190 spv_check_supported(tessellation, cap);
4191 break;
4192
4193 case SpvCapabilityDrawParameters:
4194 spv_check_supported(draw_parameters, cap);
4195 break;
4196
4197 case SpvCapabilityStorageImageReadWithoutFormat:
4198 spv_check_supported(image_read_without_format, cap);
4199 break;
4200
4201 case SpvCapabilityStorageImageWriteWithoutFormat:
4202 spv_check_supported(image_write_without_format, cap);
4203 break;
4204
4205 case SpvCapabilityDeviceGroup:
4206 spv_check_supported(device_group, cap);
4207 break;
4208
4209 case SpvCapabilityMultiView:
4210 spv_check_supported(multiview, cap);
4211 break;
4212
4213 case SpvCapabilityGroupNonUniform:
4214 spv_check_supported(subgroup_basic, cap);
4215 break;
4216
4217 case SpvCapabilitySubgroupVoteKHR:
4218 case SpvCapabilityGroupNonUniformVote:
4219 spv_check_supported(subgroup_vote, cap);
4220 break;
4221
4222 case SpvCapabilitySubgroupBallotKHR:
4223 case SpvCapabilityGroupNonUniformBallot:
4224 spv_check_supported(subgroup_ballot, cap);
4225 break;
4226
4227 case SpvCapabilityGroupNonUniformShuffle:
4228 case SpvCapabilityGroupNonUniformShuffleRelative:
4229 spv_check_supported(subgroup_shuffle, cap);
4230 break;
4231
4232 case SpvCapabilityGroupNonUniformQuad:
4233 spv_check_supported(subgroup_quad, cap);
4234 break;
4235
4236 case SpvCapabilityGroupNonUniformArithmetic:
4237 case SpvCapabilityGroupNonUniformClustered:
4238 spv_check_supported(subgroup_arithmetic, cap);
4239 break;
4240
4241 case SpvCapabilityGroups:
4242 spv_check_supported(amd_shader_ballot, cap);
4243 break;
4244
4245 case SpvCapabilityVariablePointersStorageBuffer:
4246 case SpvCapabilityVariablePointers:
4247 spv_check_supported(variable_pointers, cap);
4248 b->variable_pointers = true;
4249 break;
4250
4251 case SpvCapabilityStorageUniformBufferBlock16:
4252 case SpvCapabilityStorageUniform16:
4253 case SpvCapabilityStoragePushConstant16:
4254 case SpvCapabilityStorageInputOutput16:
4255 spv_check_supported(storage_16bit, cap);
4256 break;
4257
4258 case SpvCapabilityShaderLayer:
4259 case SpvCapabilityShaderViewportIndex:
4260 case SpvCapabilityShaderViewportIndexLayerEXT:
4261 spv_check_supported(shader_viewport_index_layer, cap);
4262 break;
4263
4264 case SpvCapabilityStorageBuffer8BitAccess:
4265 case SpvCapabilityUniformAndStorageBuffer8BitAccess:
4266 case SpvCapabilityStoragePushConstant8:
4267 spv_check_supported(storage_8bit, cap);
4268 break;
4269
4270 case SpvCapabilityShaderNonUniformEXT:
4271 spv_check_supported(descriptor_indexing, cap);
4272 break;
4273
4274 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT:
4275 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT:
4276 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT:
4277 spv_check_supported(descriptor_array_dynamic_indexing, cap);
4278 break;
4279
4280 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT:
4281 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT:
4282 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT:
4283 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT:
4284 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT:
4285 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT:
4286 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT:
4287 spv_check_supported(descriptor_array_non_uniform_indexing, cap);
4288 break;
4289
4290 case SpvCapabilityRuntimeDescriptorArrayEXT:
4291 spv_check_supported(runtime_descriptor_array, cap);
4292 break;
4293
4294 case SpvCapabilityStencilExportEXT:
4295 spv_check_supported(stencil_export, cap);
4296 break;
4297
4298 case SpvCapabilitySampleMaskPostDepthCoverage:
4299 spv_check_supported(post_depth_coverage, cap);
4300 break;
4301
4302 case SpvCapabilityDenormFlushToZero:
4303 case SpvCapabilityDenormPreserve:
4304 case SpvCapabilitySignedZeroInfNanPreserve:
4305 case SpvCapabilityRoundingModeRTE:
4306 case SpvCapabilityRoundingModeRTZ:
4307 spv_check_supported(float_controls, cap);
4308 break;
4309
4310 case SpvCapabilityPhysicalStorageBufferAddresses:
4311 spv_check_supported(physical_storage_buffer_address, cap);
4312 break;
4313
4314 case SpvCapabilityComputeDerivativeGroupQuadsNV:
4315 case SpvCapabilityComputeDerivativeGroupLinearNV:
4316 spv_check_supported(derivative_group, cap);
4317 break;
4318
4319 case SpvCapabilityFloat16:
4320 spv_check_supported(float16, cap);
4321 break;
4322
4323 case SpvCapabilityFragmentShaderSampleInterlockEXT:
4324 spv_check_supported(fragment_shader_sample_interlock, cap);
4325 break;
4326
4327 case SpvCapabilityFragmentShaderPixelInterlockEXT:
4328 spv_check_supported(fragment_shader_pixel_interlock, cap);
4329 break;
4330
4331 case SpvCapabilityDemoteToHelperInvocationEXT:
4332 spv_check_supported(demote_to_helper_invocation, cap);
4333 break;
4334
4335 case SpvCapabilityShaderClockKHR:
4336 spv_check_supported(shader_clock, cap);
4337 break;
4338
4339 case SpvCapabilityVulkanMemoryModel:
4340 spv_check_supported(vk_memory_model, cap);
4341 break;
4342
4343 case SpvCapabilityVulkanMemoryModelDeviceScope:
4344 spv_check_supported(vk_memory_model_device_scope, cap);
4345 break;
4346
4347 case SpvCapabilityImageReadWriteLodAMD:
4348 spv_check_supported(amd_image_read_write_lod, cap);
4349 break;
4350
4351 case SpvCapabilityIntegerFunctions2INTEL:
4352 spv_check_supported(integer_functions2, cap);
4353 break;
4354
4355 case SpvCapabilityFragmentMaskAMD:
4356 spv_check_supported(amd_fragment_mask, cap);
4357 break;
4358
4359 case SpvCapabilityImageGatherBiasLodAMD:
4360 spv_check_supported(amd_image_gather_bias_lod, cap);
4361 break;
4362
4363 case SpvCapabilityAtomicFloat32AddEXT:
4364 spv_check_supported(float32_atomic_add, cap);
4365 break;
4366
4367 case SpvCapabilityAtomicFloat64AddEXT:
4368 spv_check_supported(float64_atomic_add, cap);
4369 break;
4370
4371 default:
4372 vtn_fail("Unhandled capability: %s (%u)",
4373 spirv_capability_to_string(cap), cap);
4374 }
4375 break;
4376 }
4377
4378 case SpvOpExtInstImport:
4379 vtn_handle_extension(b, opcode, w, count);
4380 break;
4381
4382 case SpvOpMemoryModel:
4383 switch (w[1]) {
4384 case SpvAddressingModelPhysical32:
4385 vtn_fail_if(b->shader->info.stage != MESA_SHADER_KERNEL,
4386 "AddressingModelPhysical32 only supported for kernels");
4387 b->shader->info.cs.ptr_size = 32;
4388 b->physical_ptrs = true;
4389 assert(nir_address_format_bit_size(b->options->global_addr_format) == 32);
4390 assert(nir_address_format_num_components(b->options->global_addr_format) == 1);
4391 assert(nir_address_format_bit_size(b->options->shared_addr_format) == 32);
4392 assert(nir_address_format_num_components(b->options->shared_addr_format) == 1);
4393 if (!b->options->constant_as_global) {
4394 assert(nir_address_format_bit_size(b->options->ubo_addr_format) == 32);
4395 assert(nir_address_format_num_components(b->options->ubo_addr_format) == 1);
4396 }
4397 break;
4398 case SpvAddressingModelPhysical64:
4399 vtn_fail_if(b->shader->info.stage != MESA_SHADER_KERNEL,
4400 "AddressingModelPhysical64 only supported for kernels");
4401 b->shader->info.cs.ptr_size = 64;
4402 b->physical_ptrs = true;
4403 assert(nir_address_format_bit_size(b->options->global_addr_format) == 64);
4404 assert(nir_address_format_num_components(b->options->global_addr_format) == 1);
4405 assert(nir_address_format_bit_size(b->options->shared_addr_format) == 64);
4406 assert(nir_address_format_num_components(b->options->shared_addr_format) == 1);
4407 if (!b->options->constant_as_global) {
4408 assert(nir_address_format_bit_size(b->options->ubo_addr_format) == 64);
4409 assert(nir_address_format_num_components(b->options->ubo_addr_format) == 1);
4410 }
4411 break;
4412 case SpvAddressingModelLogical:
4413 vtn_fail_if(b->shader->info.stage == MESA_SHADER_KERNEL,
4414 "AddressingModelLogical only supported for shaders");
4415 b->physical_ptrs = false;
4416 break;
4417 case SpvAddressingModelPhysicalStorageBuffer64:
4418 vtn_fail_if(!b->options ||
4419 !b->options->caps.physical_storage_buffer_address,
4420 "AddressingModelPhysicalStorageBuffer64 not supported");
4421 break;
4422 default:
4423 vtn_fail("Unknown addressing model: %s (%u)",
4424 spirv_addressingmodel_to_string(w[1]), w[1]);
4425 break;
4426 }
4427
4428 b->mem_model = w[2];
4429 switch (w[2]) {
4430 case SpvMemoryModelSimple:
4431 case SpvMemoryModelGLSL450:
4432 case SpvMemoryModelOpenCL:
4433 break;
4434 case SpvMemoryModelVulkan:
4435 vtn_fail_if(!b->options->caps.vk_memory_model,
4436 "Vulkan memory model is unsupported by this driver");
4437 break;
4438 default:
4439 vtn_fail("Unsupported memory model: %s",
4440 spirv_memorymodel_to_string(w[2]));
4441 break;
4442 }
4443 break;
4444
4445 case SpvOpEntryPoint:
4446 vtn_handle_entry_point(b, w, count);
4447 break;
4448
4449 case SpvOpString:
4450 vtn_push_value(b, w[1], vtn_value_type_string)->str =
4451 vtn_string_literal(b, &w[2], count - 2, NULL);
4452 break;
4453
4454 case SpvOpName:
4455 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
4456 break;
4457
4458 case SpvOpMemberName:
4459 /* TODO */
4460 break;
4461
4462 case SpvOpExecutionMode:
4463 case SpvOpExecutionModeId:
4464 case SpvOpDecorationGroup:
4465 case SpvOpDecorate:
4466 case SpvOpDecorateId:
4467 case SpvOpMemberDecorate:
4468 case SpvOpGroupDecorate:
4469 case SpvOpGroupMemberDecorate:
4470 case SpvOpDecorateString:
4471 case SpvOpMemberDecorateString:
4472 vtn_handle_decoration(b, opcode, w, count);
4473 break;
4474
4475 case SpvOpExtInst: {
4476 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
4477 if (val->ext_handler == vtn_handle_non_semantic_instruction) {
4478 /* NonSemantic extended instructions are acceptable in preamble. */
4479 vtn_handle_non_semantic_instruction(b, w[4], w, count);
4480 return true;
4481 } else {
4482 return false; /* End of preamble. */
4483 }
4484 }
4485
4486 default:
4487 return false; /* End of preamble */
4488 }
4489
4490 return true;
4491 }
4492
4493 static void
4494 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
4495 const struct vtn_decoration *mode, UNUSED void *data)
4496 {
4497 vtn_assert(b->entry_point == entry_point);
4498
4499 switch(mode->exec_mode) {
4500 case SpvExecutionModeOriginUpperLeft:
4501 case SpvExecutionModeOriginLowerLeft:
4502 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4503 b->shader->info.fs.origin_upper_left =
4504 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
4505 break;
4506
4507 case SpvExecutionModeEarlyFragmentTests:
4508 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4509 b->shader->info.fs.early_fragment_tests = true;
4510 break;
4511
4512 case SpvExecutionModePostDepthCoverage:
4513 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4514 b->shader->info.fs.post_depth_coverage = true;
4515 break;
4516
4517 case SpvExecutionModeInvocations:
4518 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
4519 b->shader->info.gs.invocations = MAX2(1, mode->operands[0]);
4520 break;
4521
4522 case SpvExecutionModeDepthReplacing:
4523 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4524 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
4525 break;
4526 case SpvExecutionModeDepthGreater:
4527 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4528 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
4529 break;
4530 case SpvExecutionModeDepthLess:
4531 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4532 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
4533 break;
4534 case SpvExecutionModeDepthUnchanged:
4535 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4536 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
4537 break;
4538
4539 case SpvExecutionModeLocalSize:
4540 vtn_assert(gl_shader_stage_is_compute(b->shader->info.stage));
4541 b->shader->info.cs.local_size[0] = mode->operands[0];
4542 b->shader->info.cs.local_size[1] = mode->operands[1];
4543 b->shader->info.cs.local_size[2] = mode->operands[2];
4544 break;
4545
4546 case SpvExecutionModeLocalSizeHint:
4547 break; /* Nothing to do with this */
4548
4549 case SpvExecutionModeOutputVertices:
4550 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4551 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4552 b->shader->info.tess.tcs_vertices_out = mode->operands[0];
4553 } else {
4554 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
4555 b->shader->info.gs.vertices_out = mode->operands[0];
4556 }
4557 break;
4558
4559 case SpvExecutionModeInputPoints:
4560 case SpvExecutionModeInputLines:
4561 case SpvExecutionModeInputLinesAdjacency:
4562 case SpvExecutionModeTriangles:
4563 case SpvExecutionModeInputTrianglesAdjacency:
4564 case SpvExecutionModeQuads:
4565 case SpvExecutionModeIsolines:
4566 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4567 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
4568 b->shader->info.tess.primitive_mode =
4569 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
4570 } else {
4571 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
4572 b->shader->info.gs.vertices_in =
4573 vertices_in_from_spv_execution_mode(b, mode->exec_mode);
4574 b->shader->info.gs.input_primitive =
4575 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
4576 }
4577 break;
4578
4579 case SpvExecutionModeOutputPoints:
4580 case SpvExecutionModeOutputLineStrip:
4581 case SpvExecutionModeOutputTriangleStrip:
4582 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
4583 b->shader->info.gs.output_primitive =
4584 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
4585 break;
4586
4587 case SpvExecutionModeSpacingEqual:
4588 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4589 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4590 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
4591 break;
4592 case SpvExecutionModeSpacingFractionalEven:
4593 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4594 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4595 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
4596 break;
4597 case SpvExecutionModeSpacingFractionalOdd:
4598 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4599 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4600 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
4601 break;
4602 case SpvExecutionModeVertexOrderCw:
4603 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4604 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4605 b->shader->info.tess.ccw = false;
4606 break;
4607 case SpvExecutionModeVertexOrderCcw:
4608 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4609 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4610 b->shader->info.tess.ccw = true;
4611 break;
4612 case SpvExecutionModePointMode:
4613 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
4614 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
4615 b->shader->info.tess.point_mode = true;
4616 break;
4617
4618 case SpvExecutionModePixelCenterInteger:
4619 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4620 b->shader->info.fs.pixel_center_integer = true;
4621 break;
4622
4623 case SpvExecutionModeXfb:
4624 b->shader->info.has_transform_feedback_varyings = true;
4625 break;
4626
4627 case SpvExecutionModeVecTypeHint:
4628 break; /* OpenCL */
4629
4630 case SpvExecutionModeContractionOff:
4631 if (b->shader->info.stage != MESA_SHADER_KERNEL)
4632 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4633 spirv_executionmode_to_string(mode->exec_mode));
4634 else
4635 b->exact = true;
4636 break;
4637
4638 case SpvExecutionModeStencilRefReplacingEXT:
4639 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4640 break;
4641
4642 case SpvExecutionModeDerivativeGroupQuadsNV:
4643 vtn_assert(b->shader->info.stage == MESA_SHADER_COMPUTE);
4644 b->shader->info.cs.derivative_group = DERIVATIVE_GROUP_QUADS;
4645 break;
4646
4647 case SpvExecutionModeDerivativeGroupLinearNV:
4648 vtn_assert(b->shader->info.stage == MESA_SHADER_COMPUTE);
4649 b->shader->info.cs.derivative_group = DERIVATIVE_GROUP_LINEAR;
4650 break;
4651
4652 case SpvExecutionModePixelInterlockOrderedEXT:
4653 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4654 b->shader->info.fs.pixel_interlock_ordered = true;
4655 break;
4656
4657 case SpvExecutionModePixelInterlockUnorderedEXT:
4658 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4659 b->shader->info.fs.pixel_interlock_unordered = true;
4660 break;
4661
4662 case SpvExecutionModeSampleInterlockOrderedEXT:
4663 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4664 b->shader->info.fs.sample_interlock_ordered = true;
4665 break;
4666
4667 case SpvExecutionModeSampleInterlockUnorderedEXT:
4668 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
4669 b->shader->info.fs.sample_interlock_unordered = true;
4670 break;
4671
4672 case SpvExecutionModeDenormPreserve:
4673 case SpvExecutionModeDenormFlushToZero:
4674 case SpvExecutionModeSignedZeroInfNanPreserve:
4675 case SpvExecutionModeRoundingModeRTE:
4676 case SpvExecutionModeRoundingModeRTZ: {
4677 unsigned execution_mode = 0;
4678 switch (mode->exec_mode) {
4679 case SpvExecutionModeDenormPreserve:
4680 switch (mode->operands[0]) {
4681 case 16: execution_mode = FLOAT_CONTROLS_DENORM_PRESERVE_FP16; break;
4682 case 32: execution_mode = FLOAT_CONTROLS_DENORM_PRESERVE_FP32; break;
4683 case 64: execution_mode = FLOAT_CONTROLS_DENORM_PRESERVE_FP64; break;
4684 default: vtn_fail("Floating point type not supported");
4685 }
4686 break;
4687 case SpvExecutionModeDenormFlushToZero:
4688 switch (mode->operands[0]) {
4689 case 16: execution_mode = FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16; break;
4690 case 32: execution_mode = FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32; break;
4691 case 64: execution_mode = FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64; break;
4692 default: vtn_fail("Floating point type not supported");
4693 }
4694 break;
4695 case SpvExecutionModeSignedZeroInfNanPreserve:
4696 switch (mode->operands[0]) {
4697 case 16: execution_mode = FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16; break;
4698 case 32: execution_mode = FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32; break;
4699 case 64: execution_mode = FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64; break;
4700 default: vtn_fail("Floating point type not supported");
4701 }
4702 break;
4703 case SpvExecutionModeRoundingModeRTE:
4704 switch (mode->operands[0]) {
4705 case 16: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16; break;
4706 case 32: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32; break;
4707 case 64: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64; break;
4708 default: vtn_fail("Floating point type not supported");
4709 }
4710 break;
4711 case SpvExecutionModeRoundingModeRTZ:
4712 switch (mode->operands[0]) {
4713 case 16: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16; break;
4714 case 32: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32; break;
4715 case 64: execution_mode = FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64; break;
4716 default: vtn_fail("Floating point type not supported");
4717 }
4718 break;
4719 default:
4720 break;
4721 }
4722
4723 b->shader->info.float_controls_execution_mode |= execution_mode;
4724 break;
4725 }
4726
4727 case SpvExecutionModeLocalSizeId:
4728 case SpvExecutionModeLocalSizeHintId:
4729 /* Handled later by vtn_handle_execution_mode_id(). */
4730 break;
4731
4732 default:
4733 vtn_fail("Unhandled execution mode: %s (%u)",
4734 spirv_executionmode_to_string(mode->exec_mode),
4735 mode->exec_mode);
4736 }
4737 }
4738
4739 static void
4740 vtn_handle_execution_mode_id(struct vtn_builder *b, struct vtn_value *entry_point,
4741 const struct vtn_decoration *mode, UNUSED void *data)
4742 {
4743
4744 vtn_assert(b->entry_point == entry_point);
4745
4746 switch (mode->exec_mode) {
4747 case SpvExecutionModeLocalSizeId:
4748 b->shader->info.cs.local_size[0] = vtn_constant_uint(b, mode->operands[0]);
4749 b->shader->info.cs.local_size[1] = vtn_constant_uint(b, mode->operands[1]);
4750 b->shader->info.cs.local_size[2] = vtn_constant_uint(b, mode->operands[2]);
4751 break;
4752
4753 case SpvExecutionModeLocalSizeHintId:
4754 /* Nothing to do with this hint. */
4755 break;
4756
4757 default:
4758 /* Nothing to do. Literal execution modes already handled by
4759 * vtn_handle_execution_mode(). */
4760 break;
4761 }
4762 }
4763
4764 static bool
4765 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
4766 const uint32_t *w, unsigned count)
4767 {
4768 vtn_set_instruction_result_type(b, opcode, w, count);
4769
4770 switch (opcode) {
4771 case SpvOpSource:
4772 case SpvOpSourceContinued:
4773 case SpvOpSourceExtension:
4774 case SpvOpExtension:
4775 case SpvOpCapability:
4776 case SpvOpExtInstImport:
4777 case SpvOpMemoryModel:
4778 case SpvOpEntryPoint:
4779 case SpvOpExecutionMode:
4780 case SpvOpString:
4781 case SpvOpName:
4782 case SpvOpMemberName:
4783 case SpvOpDecorationGroup:
4784 case SpvOpDecorate:
4785 case SpvOpDecorateId:
4786 case SpvOpMemberDecorate:
4787 case SpvOpGroupDecorate:
4788 case SpvOpGroupMemberDecorate:
4789 case SpvOpDecorateString:
4790 case SpvOpMemberDecorateString:
4791 vtn_fail("Invalid opcode types and variables section");
4792 break;
4793
4794 case SpvOpTypeVoid:
4795 case SpvOpTypeBool:
4796 case SpvOpTypeInt:
4797 case SpvOpTypeFloat:
4798 case SpvOpTypeVector:
4799 case SpvOpTypeMatrix:
4800 case SpvOpTypeImage:
4801 case SpvOpTypeSampler:
4802 case SpvOpTypeSampledImage:
4803 case SpvOpTypeArray:
4804 case SpvOpTypeRuntimeArray:
4805 case SpvOpTypeStruct:
4806 case SpvOpTypeOpaque:
4807 case SpvOpTypePointer:
4808 case SpvOpTypeForwardPointer:
4809 case SpvOpTypeFunction:
4810 case SpvOpTypeEvent:
4811 case SpvOpTypeDeviceEvent:
4812 case SpvOpTypeReserveId:
4813 case SpvOpTypeQueue:
4814 case SpvOpTypePipe:
4815 vtn_handle_type(b, opcode, w, count);
4816 break;
4817
4818 case SpvOpConstantTrue:
4819 case SpvOpConstantFalse:
4820 case SpvOpConstant:
4821 case SpvOpConstantComposite:
4822 case SpvOpConstantSampler:
4823 case SpvOpConstantNull:
4824 case SpvOpSpecConstantTrue:
4825 case SpvOpSpecConstantFalse:
4826 case SpvOpSpecConstant:
4827 case SpvOpSpecConstantComposite:
4828 case SpvOpSpecConstantOp:
4829 vtn_handle_constant(b, opcode, w, count);
4830 break;
4831
4832 case SpvOpUndef:
4833 case SpvOpVariable:
4834 vtn_handle_variables(b, opcode, w, count);
4835 break;
4836
4837 case SpvOpExtInst: {
4838 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
4839 /* NonSemantic extended instructions are acceptable in preamble, others
4840 * will indicate the end of preamble.
4841 */
4842 return val->ext_handler == vtn_handle_non_semantic_instruction;
4843 }
4844
4845 default:
4846 return false; /* End of preamble */
4847 }
4848
4849 return true;
4850 }
4851
4852 static struct vtn_ssa_value *
4853 vtn_nir_select(struct vtn_builder *b, struct vtn_ssa_value *src0,
4854 struct vtn_ssa_value *src1, struct vtn_ssa_value *src2)
4855 {
4856 struct vtn_ssa_value *dest = rzalloc(b, struct vtn_ssa_value);
4857 dest->type = src1->type;
4858
4859 if (glsl_type_is_vector_or_scalar(src1->type)) {
4860 dest->def = nir_bcsel(&b->nb, src0->def, src1->def, src2->def);
4861 } else {
4862 unsigned elems = glsl_get_length(src1->type);
4863
4864 dest->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
4865 for (unsigned i = 0; i < elems; i++) {
4866 dest->elems[i] = vtn_nir_select(b, src0,
4867 src1->elems[i], src2->elems[i]);
4868 }
4869 }
4870
4871 return dest;
4872 }
4873
4874 static void
4875 vtn_handle_select(struct vtn_builder *b, SpvOp opcode,
4876 const uint32_t *w, unsigned count)
4877 {
4878 /* Handle OpSelect up-front here because it needs to be able to handle
4879 * pointers and not just regular vectors and scalars.
4880 */
4881 struct vtn_value *res_val = vtn_untyped_value(b, w[2]);
4882 struct vtn_value *cond_val = vtn_untyped_value(b, w[3]);
4883 struct vtn_value *obj1_val = vtn_untyped_value(b, w[4]);
4884 struct vtn_value *obj2_val = vtn_untyped_value(b, w[5]);
4885
4886 vtn_fail_if(obj1_val->type != res_val->type ||
4887 obj2_val->type != res_val->type,
4888 "Object types must match the result type in OpSelect");
4889
4890 vtn_fail_if((cond_val->type->base_type != vtn_base_type_scalar &&
4891 cond_val->type->base_type != vtn_base_type_vector) ||
4892 !glsl_type_is_boolean(cond_val->type->type),
4893 "OpSelect must have either a vector of booleans or "
4894 "a boolean as Condition type");
4895
4896 vtn_fail_if(cond_val->type->base_type == vtn_base_type_vector &&
4897 (res_val->type->base_type != vtn_base_type_vector ||
4898 res_val->type->length != cond_val->type->length),
4899 "When Condition type in OpSelect is a vector, the Result "
4900 "type must be a vector of the same length");
4901
4902 switch (res_val->type->base_type) {
4903 case vtn_base_type_scalar:
4904 case vtn_base_type_vector:
4905 case vtn_base_type_matrix:
4906 case vtn_base_type_array:
4907 case vtn_base_type_struct:
4908 /* OK. */
4909 break;
4910 case vtn_base_type_pointer:
4911 /* We need to have actual storage for pointer types. */
4912 vtn_fail_if(res_val->type->type == NULL,
4913 "Invalid pointer result type for OpSelect");
4914 break;
4915 default:
4916 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4917 }
4918
4919 vtn_push_ssa_value(b, w[2],
4920 vtn_nir_select(b, vtn_ssa_value(b, w[3]),
4921 vtn_ssa_value(b, w[4]),
4922 vtn_ssa_value(b, w[5])));
4923 }
4924
4925 static void
4926 vtn_handle_ptr(struct vtn_builder *b, SpvOp opcode,
4927 const uint32_t *w, unsigned count)
4928 {
4929 struct vtn_type *type1 = vtn_get_value_type(b, w[3]);
4930 struct vtn_type *type2 = vtn_get_value_type(b, w[4]);
4931 vtn_fail_if(type1->base_type != vtn_base_type_pointer ||
4932 type2->base_type != vtn_base_type_pointer,
4933 "%s operands must have pointer types",
4934 spirv_op_to_string(opcode));
4935 vtn_fail_if(type1->storage_class != type2->storage_class,
4936 "%s operands must have the same storage class",
4937 spirv_op_to_string(opcode));
4938
4939 struct vtn_type *vtn_type = vtn_get_type(b, w[1]);
4940 const struct glsl_type *type = vtn_type->type;
4941
4942 nir_address_format addr_format = vtn_mode_to_address_format(
4943 b, vtn_storage_class_to_mode(b, type1->storage_class, NULL, NULL));
4944
4945 nir_ssa_def *def;
4946
4947 switch (opcode) {
4948 case SpvOpPtrDiff: {
4949 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4950 unsigned elem_size, elem_align;
4951 glsl_get_natural_size_align_bytes(type1->deref->type,
4952 &elem_size, &elem_align);
4953
4954 def = nir_build_addr_isub(&b->nb,
4955 vtn_get_nir_ssa(b, w[3]),
4956 vtn_get_nir_ssa(b, w[4]),
4957 addr_format);
4958 def = nir_idiv(&b->nb, def, nir_imm_intN_t(&b->nb, elem_size, def->bit_size));
4959 def = nir_i2i(&b->nb, def, glsl_get_bit_size(type));
4960 break;
4961 }
4962
4963 case SpvOpPtrEqual:
4964 case SpvOpPtrNotEqual: {
4965 def = nir_build_addr_ieq(&b->nb,
4966 vtn_get_nir_ssa(b, w[3]),
4967 vtn_get_nir_ssa(b, w[4]),
4968 addr_format);
4969 if (opcode == SpvOpPtrNotEqual)
4970 def = nir_inot(&b->nb, def);
4971 break;
4972 }
4973
4974 default:
4975 unreachable("Invalid ptr operation");
4976 }
4977
4978 vtn_push_nir_ssa(b, w[2], def);
4979 }
4980
4981 static bool
4982 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
4983 const uint32_t *w, unsigned count)
4984 {
4985 switch (opcode) {
4986 case SpvOpLabel:
4987 break;
4988
4989 case SpvOpLoopMerge:
4990 case SpvOpSelectionMerge:
4991 /* This is handled by cfg pre-pass and walk_blocks */
4992 break;
4993
4994 case SpvOpUndef: {
4995 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
4996 val->type = vtn_get_type(b, w[1]);
4997 break;
4998 }
4999
5000 case SpvOpExtInst:
5001 vtn_handle_extension(b, opcode, w, count);
5002 break;
5003
5004 case SpvOpVariable:
5005 case SpvOpLoad:
5006 case SpvOpStore:
5007 case SpvOpCopyMemory:
5008 case SpvOpCopyMemorySized:
5009 case SpvOpAccessChain:
5010 case SpvOpPtrAccessChain:
5011 case SpvOpInBoundsAccessChain:
5012 case SpvOpInBoundsPtrAccessChain:
5013 case SpvOpArrayLength:
5014 case SpvOpConvertPtrToU:
5015 case SpvOpConvertUToPtr:
5016 vtn_handle_variables(b, opcode, w, count);
5017 break;
5018
5019 case SpvOpFunctionCall:
5020 vtn_handle_function_call(b, opcode, w, count);
5021 break;
5022
5023 case SpvOpSampledImage:
5024 case SpvOpImage:
5025 case SpvOpImageSampleImplicitLod:
5026 case SpvOpImageSampleExplicitLod:
5027 case SpvOpImageSampleDrefImplicitLod:
5028 case SpvOpImageSampleDrefExplicitLod:
5029 case SpvOpImageSampleProjImplicitLod:
5030 case SpvOpImageSampleProjExplicitLod:
5031 case SpvOpImageSampleProjDrefImplicitLod:
5032 case SpvOpImageSampleProjDrefExplicitLod:
5033 case SpvOpImageFetch:
5034 case SpvOpImageGather:
5035 case SpvOpImageDrefGather:
5036 case SpvOpImageQuerySizeLod:
5037 case SpvOpImageQueryLod:
5038 case SpvOpImageQueryLevels:
5039 case SpvOpImageQuerySamples:
5040 vtn_handle_texture(b, opcode, w, count);
5041 break;
5042
5043 case SpvOpImageRead:
5044 case SpvOpImageWrite:
5045 case SpvOpImageTexelPointer:
5046 vtn_handle_image(b, opcode, w, count);
5047 break;
5048
5049 case SpvOpImageQuerySize: {
5050 struct vtn_type *image_type = vtn_get_value_type(b, w[3]);
5051 vtn_assert(image_type->base_type == vtn_base_type_image);
5052 if (glsl_type_is_image(image_type->glsl_image)) {
5053 vtn_handle_image(b, opcode, w, count);
5054 } else {
5055 vtn_assert(glsl_type_is_sampler(image_type->glsl_image));
5056 vtn_handle_texture(b, opcode, w, count);
5057 }
5058 break;
5059 }
5060
5061 case SpvOpFragmentMaskFetchAMD:
5062 case SpvOpFragmentFetchAMD:
5063 vtn_handle_texture(b, opcode, w, count);
5064 break;
5065
5066 case SpvOpAtomicLoad:
5067 case SpvOpAtomicExchange:
5068 case SpvOpAtomicCompareExchange:
5069 case SpvOpAtomicCompareExchangeWeak:
5070 case SpvOpAtomicIIncrement:
5071 case SpvOpAtomicIDecrement:
5072 case SpvOpAtomicIAdd:
5073 case SpvOpAtomicISub:
5074 case SpvOpAtomicSMin:
5075 case SpvOpAtomicUMin:
5076 case SpvOpAtomicSMax:
5077 case SpvOpAtomicUMax:
5078 case SpvOpAtomicAnd:
5079 case SpvOpAtomicOr:
5080 case SpvOpAtomicXor:
5081 case SpvOpAtomicFAddEXT: {
5082 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
5083 if (pointer->value_type == vtn_value_type_image_pointer) {
5084 vtn_handle_image(b, opcode, w, count);
5085 } else {
5086 vtn_assert(pointer->value_type == vtn_value_type_pointer);
5087 vtn_handle_atomics(b, opcode, w, count);
5088 }
5089 break;
5090 }
5091
5092 case SpvOpAtomicStore: {
5093 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
5094 if (pointer->value_type == vtn_value_type_image_pointer) {
5095 vtn_handle_image(b, opcode, w, count);
5096 } else {
5097 vtn_assert(pointer->value_type == vtn_value_type_pointer);
5098 vtn_handle_atomics(b, opcode, w, count);
5099 }
5100 break;
5101 }
5102
5103 case SpvOpSelect:
5104 vtn_handle_select(b, opcode, w, count);
5105 break;
5106
5107 case SpvOpSNegate:
5108 case SpvOpFNegate:
5109 case SpvOpNot:
5110 case SpvOpAny:
5111 case SpvOpAll:
5112 case SpvOpConvertFToU:
5113 case SpvOpConvertFToS:
5114 case SpvOpConvertSToF:
5115 case SpvOpConvertUToF:
5116 case SpvOpUConvert:
5117 case SpvOpSConvert:
5118 case SpvOpFConvert:
5119 case SpvOpQuantizeToF16:
5120 case SpvOpPtrCastToGeneric:
5121 case SpvOpGenericCastToPtr:
5122 case SpvOpIsNan:
5123 case SpvOpIsInf:
5124 case SpvOpIsFinite:
5125 case SpvOpIsNormal:
5126 case SpvOpSignBitSet:
5127 case SpvOpLessOrGreater:
5128 case SpvOpOrdered:
5129 case SpvOpUnordered:
5130 case SpvOpIAdd:
5131 case SpvOpFAdd:
5132 case SpvOpISub:
5133 case SpvOpFSub:
5134 case SpvOpIMul:
5135 case SpvOpFMul:
5136 case SpvOpUDiv:
5137 case SpvOpSDiv:
5138 case SpvOpFDiv:
5139 case SpvOpUMod:
5140 case SpvOpSRem:
5141 case SpvOpSMod:
5142 case SpvOpFRem:
5143 case SpvOpFMod:
5144 case SpvOpVectorTimesScalar:
5145 case SpvOpDot:
5146 case SpvOpIAddCarry:
5147 case SpvOpISubBorrow:
5148 case SpvOpUMulExtended:
5149 case SpvOpSMulExtended:
5150 case SpvOpShiftRightLogical:
5151 case SpvOpShiftRightArithmetic:
5152 case SpvOpShiftLeftLogical:
5153 case SpvOpLogicalEqual:
5154 case SpvOpLogicalNotEqual:
5155 case SpvOpLogicalOr:
5156 case SpvOpLogicalAnd:
5157 case SpvOpLogicalNot:
5158 case SpvOpBitwiseOr:
5159 case SpvOpBitwiseXor:
5160 case SpvOpBitwiseAnd:
5161 case SpvOpIEqual:
5162 case SpvOpFOrdEqual:
5163 case SpvOpFUnordEqual:
5164 case SpvOpINotEqual:
5165 case SpvOpFOrdNotEqual:
5166 case SpvOpFUnordNotEqual:
5167 case SpvOpULessThan:
5168 case SpvOpSLessThan:
5169 case SpvOpFOrdLessThan:
5170 case SpvOpFUnordLessThan:
5171 case SpvOpUGreaterThan:
5172 case SpvOpSGreaterThan:
5173 case SpvOpFOrdGreaterThan:
5174 case SpvOpFUnordGreaterThan:
5175 case SpvOpULessThanEqual:
5176 case SpvOpSLessThanEqual:
5177 case SpvOpFOrdLessThanEqual:
5178 case SpvOpFUnordLessThanEqual:
5179 case SpvOpUGreaterThanEqual:
5180 case SpvOpSGreaterThanEqual:
5181 case SpvOpFOrdGreaterThanEqual:
5182 case SpvOpFUnordGreaterThanEqual:
5183 case SpvOpDPdx:
5184 case SpvOpDPdy:
5185 case SpvOpFwidth:
5186 case SpvOpDPdxFine:
5187 case SpvOpDPdyFine:
5188 case SpvOpFwidthFine:
5189 case SpvOpDPdxCoarse:
5190 case SpvOpDPdyCoarse:
5191 case SpvOpFwidthCoarse:
5192 case SpvOpBitFieldInsert:
5193 case SpvOpBitFieldSExtract:
5194 case SpvOpBitFieldUExtract:
5195 case SpvOpBitReverse:
5196 case SpvOpBitCount:
5197 case SpvOpTranspose:
5198 case SpvOpOuterProduct:
5199 case SpvOpMatrixTimesScalar:
5200 case SpvOpVectorTimesMatrix:
5201 case SpvOpMatrixTimesVector:
5202 case SpvOpMatrixTimesMatrix:
5203 case SpvOpUCountLeadingZerosINTEL:
5204 case SpvOpUCountTrailingZerosINTEL:
5205 case SpvOpAbsISubINTEL:
5206 case SpvOpAbsUSubINTEL:
5207 case SpvOpIAddSatINTEL:
5208 case SpvOpUAddSatINTEL:
5209 case SpvOpIAverageINTEL:
5210 case SpvOpUAverageINTEL:
5211 case SpvOpIAverageRoundedINTEL:
5212 case SpvOpUAverageRoundedINTEL:
5213 case SpvOpISubSatINTEL:
5214 case SpvOpUSubSatINTEL:
5215 case SpvOpIMul32x16INTEL:
5216 case SpvOpUMul32x16INTEL:
5217 vtn_handle_alu(b, opcode, w, count);
5218 break;
5219
5220 case SpvOpBitcast:
5221 vtn_handle_bitcast(b, w, count);
5222 break;
5223
5224 case SpvOpVectorExtractDynamic:
5225 case SpvOpVectorInsertDynamic:
5226 case SpvOpVectorShuffle:
5227 case SpvOpCompositeConstruct:
5228 case SpvOpCompositeExtract:
5229 case SpvOpCompositeInsert:
5230 case SpvOpCopyLogical:
5231 case SpvOpCopyObject:
5232 vtn_handle_composite(b, opcode, w, count);
5233 break;
5234
5235 case SpvOpEmitVertex:
5236 case SpvOpEndPrimitive:
5237 case SpvOpEmitStreamVertex:
5238 case SpvOpEndStreamPrimitive:
5239 case SpvOpControlBarrier:
5240 case SpvOpMemoryBarrier:
5241 vtn_handle_barrier(b, opcode, w, count);
5242 break;
5243
5244 case SpvOpGroupNonUniformElect:
5245 case SpvOpGroupNonUniformAll:
5246 case SpvOpGroupNonUniformAny:
5247 case SpvOpGroupNonUniformAllEqual:
5248 case SpvOpGroupNonUniformBroadcast:
5249 case SpvOpGroupNonUniformBroadcastFirst:
5250 case SpvOpGroupNonUniformBallot:
5251 case SpvOpGroupNonUniformInverseBallot:
5252 case SpvOpGroupNonUniformBallotBitExtract:
5253 case SpvOpGroupNonUniformBallotBitCount:
5254 case SpvOpGroupNonUniformBallotFindLSB:
5255 case SpvOpGroupNonUniformBallotFindMSB:
5256 case SpvOpGroupNonUniformShuffle:
5257 case SpvOpGroupNonUniformShuffleXor:
5258 case SpvOpGroupNonUniformShuffleUp:
5259 case SpvOpGroupNonUniformShuffleDown:
5260 case SpvOpGroupNonUniformIAdd:
5261 case SpvOpGroupNonUniformFAdd:
5262 case SpvOpGroupNonUniformIMul:
5263 case SpvOpGroupNonUniformFMul:
5264 case SpvOpGroupNonUniformSMin:
5265 case SpvOpGroupNonUniformUMin:
5266 case SpvOpGroupNonUniformFMin:
5267 case SpvOpGroupNonUniformSMax:
5268 case SpvOpGroupNonUniformUMax:
5269 case SpvOpGroupNonUniformFMax:
5270 case SpvOpGroupNonUniformBitwiseAnd:
5271 case SpvOpGroupNonUniformBitwiseOr:
5272 case SpvOpGroupNonUniformBitwiseXor:
5273 case SpvOpGroupNonUniformLogicalAnd:
5274 case SpvOpGroupNonUniformLogicalOr:
5275 case SpvOpGroupNonUniformLogicalXor:
5276 case SpvOpGroupNonUniformQuadBroadcast:
5277 case SpvOpGroupNonUniformQuadSwap:
5278 case SpvOpGroupAll:
5279 case SpvOpGroupAny:
5280 case SpvOpGroupBroadcast:
5281 case SpvOpGroupIAdd:
5282 case SpvOpGroupFAdd:
5283 case SpvOpGroupFMin:
5284 case SpvOpGroupUMin:
5285 case SpvOpGroupSMin:
5286 case SpvOpGroupFMax:
5287 case SpvOpGroupUMax:
5288 case SpvOpGroupSMax:
5289 case SpvOpSubgroupBallotKHR:
5290 case SpvOpSubgroupFirstInvocationKHR:
5291 case SpvOpSubgroupReadInvocationKHR:
5292 case SpvOpSubgroupAllKHR:
5293 case SpvOpSubgroupAnyKHR:
5294 case SpvOpSubgroupAllEqualKHR:
5295 case SpvOpGroupIAddNonUniformAMD:
5296 case SpvOpGroupFAddNonUniformAMD:
5297 case SpvOpGroupFMinNonUniformAMD:
5298 case SpvOpGroupUMinNonUniformAMD:
5299 case SpvOpGroupSMinNonUniformAMD:
5300 case SpvOpGroupFMaxNonUniformAMD:
5301 case SpvOpGroupUMaxNonUniformAMD:
5302 case SpvOpGroupSMaxNonUniformAMD:
5303 vtn_handle_subgroup(b, opcode, w, count);
5304 break;
5305
5306 case SpvOpPtrDiff:
5307 case SpvOpPtrEqual:
5308 case SpvOpPtrNotEqual:
5309 vtn_handle_ptr(b, opcode, w, count);
5310 break;
5311
5312 case SpvOpBeginInvocationInterlockEXT:
5313 vtn_emit_barrier(b, nir_intrinsic_begin_invocation_interlock);
5314 break;
5315
5316 case SpvOpEndInvocationInterlockEXT:
5317 vtn_emit_barrier(b, nir_intrinsic_end_invocation_interlock);
5318 break;
5319
5320 case SpvOpDemoteToHelperInvocationEXT: {
5321 nir_intrinsic_instr *intrin =
5322 nir_intrinsic_instr_create(b->shader, nir_intrinsic_demote);
5323 nir_builder_instr_insert(&b->nb, &intrin->instr);
5324 break;
5325 }
5326
5327 case SpvOpIsHelperInvocationEXT: {
5328 nir_intrinsic_instr *intrin =
5329 nir_intrinsic_instr_create(b->shader, nir_intrinsic_is_helper_invocation);
5330 nir_ssa_dest_init(&intrin->instr, &intrin->dest, 1, 1, NULL);
5331 nir_builder_instr_insert(&b->nb, &intrin->instr);
5332
5333 vtn_push_nir_ssa(b, w[2], &intrin->dest.ssa);
5334 break;
5335 }
5336
5337 case SpvOpReadClockKHR: {
5338 SpvScope scope = vtn_constant_uint(b, w[3]);
5339 nir_scope nir_scope;
5340
5341 switch (scope) {
5342 case SpvScopeDevice:
5343 nir_scope = NIR_SCOPE_DEVICE;
5344 break;
5345 case SpvScopeSubgroup:
5346 nir_scope = NIR_SCOPE_SUBGROUP;
5347 break;
5348 default:
5349 vtn_fail("invalid read clock scope");
5350 }
5351
5352 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5353 * intrinsic gives uvec2, so pack the result for the other case.
5354 */
5355 nir_intrinsic_instr *intrin =
5356 nir_intrinsic_instr_create(b->nb.shader, nir_intrinsic_shader_clock);
5357 nir_ssa_dest_init(&intrin->instr, &intrin->dest, 2, 32, NULL);
5358 nir_intrinsic_set_memory_scope(intrin, nir_scope);
5359 nir_builder_instr_insert(&b->nb, &intrin->instr);
5360
5361 struct vtn_type *type = vtn_get_type(b, w[1]);
5362 const struct glsl_type *dest_type = type->type;
5363 nir_ssa_def *result;
5364
5365 if (glsl_type_is_vector(dest_type)) {
5366 assert(dest_type == glsl_vector_type(GLSL_TYPE_UINT, 2));
5367 result = &intrin->dest.ssa;
5368 } else {
5369 assert(glsl_type_is_scalar(dest_type));
5370 assert(glsl_get_base_type(dest_type) == GLSL_TYPE_UINT64);
5371 result = nir_pack_64_2x32(&b->nb, &intrin->dest.ssa);
5372 }
5373
5374 vtn_push_nir_ssa(b, w[2], result);
5375 break;
5376 }
5377
5378 case SpvOpLifetimeStart:
5379 case SpvOpLifetimeStop:
5380 break;
5381
5382 default:
5383 vtn_fail_with_opcode("Unhandled opcode", opcode);
5384 }
5385
5386 return true;
5387 }
5388
5389 struct vtn_builder*
5390 vtn_create_builder(const uint32_t *words, size_t word_count,
5391 gl_shader_stage stage, const char *entry_point_name,
5392 const struct spirv_to_nir_options *options)
5393 {
5394 /* Initialize the vtn_builder object */
5395 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
5396 struct spirv_to_nir_options *dup_options =
5397 ralloc(b, struct spirv_to_nir_options);
5398 *dup_options = *options;
5399
5400 b->spirv = words;
5401 b->spirv_word_count = word_count;
5402 b->file = NULL;
5403 b->line = -1;
5404 b->col = -1;
5405 list_inithead(&b->functions);
5406 b->entry_point_stage = stage;
5407 b->entry_point_name = entry_point_name;
5408 b->options = dup_options;
5409
5410 /*
5411 * Handle the SPIR-V header (first 5 dwords).
5412 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5413 */
5414 if (word_count <= 5)
5415 goto fail;
5416
5417 if (words[0] != SpvMagicNumber) {
5418 vtn_err("words[0] was 0x%x, want 0x%x", words[0], SpvMagicNumber);
5419 goto fail;
5420 }
5421 if (words[1] < 0x10000) {
5422 vtn_err("words[1] was 0x%x, want >= 0x10000", words[1]);
5423 goto fail;
5424 }
5425
5426 uint16_t generator_id = words[2] >> 16;
5427 uint16_t generator_version = words[2];
5428
5429 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5430 * to provide correct memory semantics on compute shader barrier()
5431 * commands. Prior to that, we need to fix them up ourselves. This
5432 * GLSLang fix caused them to bump to generator version 3.
5433 */
5434 b->wa_glslang_cs_barrier = (generator_id == 8 && generator_version < 3);
5435
5436 /* words[2] == generator magic */
5437 unsigned value_id_bound = words[3];
5438 if (words[4] != 0) {
5439 vtn_err("words[4] was %u, want 0", words[4]);
5440 goto fail;
5441 }
5442
5443 b->value_id_bound = value_id_bound;
5444 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
5445
5446 return b;
5447 fail:
5448 ralloc_free(b);
5449 return NULL;
5450 }
5451
5452 static nir_function *
5453 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder *b,
5454 nir_function *entry_point)
5455 {
5456 vtn_assert(entry_point == b->entry_point->func->impl->function);
5457 vtn_fail_if(!entry_point->name, "entry points are required to have a name");
5458 const char *func_name =
5459 ralloc_asprintf(b->shader, "__wrapped_%s", entry_point->name);
5460
5461 /* we shouldn't have any inputs yet */
5462 vtn_assert(!entry_point->shader->num_inputs);
5463 vtn_assert(b->shader->info.stage == MESA_SHADER_KERNEL);
5464
5465 nir_function *main_entry_point = nir_function_create(b->shader, func_name);
5466 main_entry_point->impl = nir_function_impl_create(main_entry_point);
5467 nir_builder_init(&b->nb, main_entry_point->impl);
5468 b->nb.cursor = nir_after_cf_list(&main_entry_point->impl->body);
5469 b->func_param_idx = 0;
5470
5471 nir_call_instr *call = nir_call_instr_create(b->nb.shader, entry_point);
5472
5473 for (unsigned i = 0; i < entry_point->num_params; ++i) {
5474 struct vtn_type *param_type = b->entry_point->func->type->params[i];
5475
5476 /* consider all pointers to function memory to be parameters passed
5477 * by value
5478 */
5479 bool is_by_val = param_type->base_type == vtn_base_type_pointer &&
5480 param_type->storage_class == SpvStorageClassFunction;
5481
5482 /* input variable */
5483 nir_variable *in_var = rzalloc(b->nb.shader, nir_variable);
5484 in_var->data.mode = nir_var_uniform;
5485 in_var->data.read_only = true;
5486 in_var->data.location = i;
5487
5488 if (is_by_val)
5489 in_var->type = param_type->deref->type;
5490 else
5491 in_var->type = param_type->type;
5492
5493 nir_shader_add_variable(b->nb.shader, in_var);
5494 b->nb.shader->num_inputs++;
5495
5496 /* we have to copy the entire variable into function memory */
5497 if (is_by_val) {
5498 nir_variable *copy_var =
5499 nir_local_variable_create(main_entry_point->impl, in_var->type,
5500 "copy_in");
5501 nir_copy_var(&b->nb, copy_var, in_var);
5502 call->params[i] =
5503 nir_src_for_ssa(&nir_build_deref_var(&b->nb, copy_var)->dest.ssa);
5504 } else {
5505 call->params[i] = nir_src_for_ssa(nir_load_var(&b->nb, in_var));
5506 }
5507 }
5508
5509 nir_builder_instr_insert(&b->nb, &call->instr);
5510
5511 return main_entry_point;
5512 }
5513
5514 nir_shader *
5515 spirv_to_nir(const uint32_t *words, size_t word_count,
5516 struct nir_spirv_specialization *spec, unsigned num_spec,
5517 gl_shader_stage stage, const char *entry_point_name,
5518 const struct spirv_to_nir_options *options,
5519 const nir_shader_compiler_options *nir_options)
5520
5521 {
5522 const uint32_t *word_end = words + word_count;
5523
5524 struct vtn_builder *b = vtn_create_builder(words, word_count,
5525 stage, entry_point_name,
5526 options);
5527
5528 if (b == NULL)
5529 return NULL;
5530
5531 /* See also _vtn_fail() */
5532 if (setjmp(b->fail_jump)) {
5533 ralloc_free(b);
5534 return NULL;
5535 }
5536
5537 /* Skip the SPIR-V header, handled at vtn_create_builder */
5538 words+= 5;
5539
5540 b->shader = nir_shader_create(b, stage, nir_options, NULL);
5541
5542 /* Handle all the preamble instructions */
5543 words = vtn_foreach_instruction(b, words, word_end,
5544 vtn_handle_preamble_instruction);
5545
5546 if (b->entry_point == NULL) {
5547 vtn_fail("Entry point not found");
5548 ralloc_free(b);
5549 return NULL;
5550 }
5551
5552 /* Ensure a sane address mode is being used for function temps */
5553 assert(nir_address_format_bit_size(b->options->temp_addr_format) == nir_get_ptr_bitsize(b->shader));
5554 assert(nir_address_format_num_components(b->options->temp_addr_format) == 1);
5555
5556 /* Set shader info defaults */
5557 if (stage == MESA_SHADER_GEOMETRY)
5558 b->shader->info.gs.invocations = 1;
5559
5560 /* Parse execution modes. */
5561 vtn_foreach_execution_mode(b, b->entry_point,
5562 vtn_handle_execution_mode, NULL);
5563
5564 b->specializations = spec;
5565 b->num_specializations = num_spec;
5566
5567 /* Handle all variable, type, and constant instructions */
5568 words = vtn_foreach_instruction(b, words, word_end,
5569 vtn_handle_variable_or_type_instruction);
5570
5571 /* Parse execution modes that depend on IDs. Must happen after we have
5572 * constants parsed.
5573 */
5574 vtn_foreach_execution_mode(b, b->entry_point,
5575 vtn_handle_execution_mode_id, NULL);
5576
5577 if (b->workgroup_size_builtin) {
5578 vtn_assert(b->workgroup_size_builtin->type->type ==
5579 glsl_vector_type(GLSL_TYPE_UINT, 3));
5580
5581 nir_const_value *const_size =
5582 b->workgroup_size_builtin->constant->values;
5583
5584 b->shader->info.cs.local_size[0] = const_size[0].u32;
5585 b->shader->info.cs.local_size[1] = const_size[1].u32;
5586 b->shader->info.cs.local_size[2] = const_size[2].u32;
5587 }
5588
5589 /* Set types on all vtn_values */
5590 vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_type);
5591
5592 vtn_build_cfg(b, words, word_end);
5593
5594 assert(b->entry_point->value_type == vtn_value_type_function);
5595 b->entry_point->func->referenced = true;
5596
5597 bool progress;
5598 do {
5599 progress = false;
5600 vtn_foreach_cf_node(node, &b->functions) {
5601 struct vtn_function *func = vtn_cf_node_as_function(node);
5602 if (func->referenced && !func->emitted) {
5603 b->const_table = _mesa_pointer_hash_table_create(b);
5604
5605 vtn_function_emit(b, func, vtn_handle_body_instruction);
5606 progress = true;
5607 }
5608 }
5609 } while (progress);
5610
5611 vtn_assert(b->entry_point->value_type == vtn_value_type_function);
5612 nir_function *entry_point = b->entry_point->func->impl->function;
5613 vtn_assert(entry_point);
5614
5615 /* post process entry_points with input params */
5616 if (entry_point->num_params && b->shader->info.stage == MESA_SHADER_KERNEL)
5617 entry_point = vtn_emit_kernel_entry_point_wrapper(b, entry_point);
5618
5619 /* structurize the CFG */
5620 nir_lower_goto_ifs(b->shader);
5621
5622 entry_point->is_entrypoint = true;
5623
5624 /* When multiple shader stages exist in the same SPIR-V module, we
5625 * generate input and output variables for every stage, in the same
5626 * NIR program. These dead variables can be invalid NIR. For example,
5627 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5628 * VS output variables wouldn't be.
5629 *
5630 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5631 * right away. In order to do so, we must lower any constant initializers
5632 * on outputs so nir_remove_dead_variables sees that they're written to.
5633 */
5634 nir_lower_variable_initializers(b->shader, nir_var_shader_out);
5635 nir_remove_dead_variables(b->shader,
5636 nir_var_shader_in | nir_var_shader_out, NULL);
5637
5638 /* We sometimes generate bogus derefs that, while never used, give the
5639 * validator a bit of heartburn. Run dead code to get rid of them.
5640 */
5641 nir_opt_dce(b->shader);
5642
5643 /* Unparent the shader from the vtn_builder before we delete the builder */
5644 ralloc_steal(NULL, b->shader);
5645
5646 nir_shader *shader = b->shader;
5647 ralloc_free(b);
5648
5649 return shader;
5650 }