spirv: Choose atomic deref type with pointer_uses_ssa_offset
[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 <stdio.h>
36
37 void
38 vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
39 size_t spirv_offset, const char *message)
40 {
41 if (b->options->debug.func) {
42 b->options->debug.func(b->options->debug.private_data,
43 level, spirv_offset, message);
44 }
45
46 #ifndef NDEBUG
47 if (level >= NIR_SPIRV_DEBUG_LEVEL_WARNING)
48 fprintf(stderr, "%s\n", message);
49 #endif
50 }
51
52 void
53 vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
54 size_t spirv_offset, const char *fmt, ...)
55 {
56 va_list args;
57 char *msg;
58
59 va_start(args, fmt);
60 msg = ralloc_vasprintf(NULL, fmt, args);
61 va_end(args);
62
63 vtn_log(b, level, spirv_offset, msg);
64
65 ralloc_free(msg);
66 }
67
68 static void
69 vtn_log_err(struct vtn_builder *b,
70 enum nir_spirv_debug_level level, const char *prefix,
71 const char *file, unsigned line,
72 const char *fmt, va_list args)
73 {
74 char *msg;
75
76 msg = ralloc_strdup(NULL, prefix);
77
78 #ifndef NDEBUG
79 ralloc_asprintf_append(&msg, " In file %s:%u\n", file, line);
80 #endif
81
82 ralloc_asprintf_append(&msg, " ");
83
84 ralloc_vasprintf_append(&msg, fmt, args);
85
86 ralloc_asprintf_append(&msg, "\n %zu bytes into the SPIR-V binary",
87 b->spirv_offset);
88
89 if (b->file) {
90 ralloc_asprintf_append(&msg,
91 "\n in SPIR-V source file %s, line %d, col %d",
92 b->file, b->line, b->col);
93 }
94
95 vtn_log(b, level, b->spirv_offset, msg);
96
97 ralloc_free(msg);
98 }
99
100 static void
101 vtn_dump_shader(struct vtn_builder *b, const char *path, const char *prefix)
102 {
103 static int idx = 0;
104
105 char filename[1024];
106 int len = snprintf(filename, sizeof(filename), "%s/%s-%d.spirv",
107 path, prefix, idx++);
108 if (len < 0 || len >= sizeof(filename))
109 return;
110
111 FILE *f = fopen(filename, "w");
112 if (f == NULL)
113 return;
114
115 fwrite(b->spirv, sizeof(*b->spirv), b->spirv_word_count, f);
116 fclose(f);
117
118 vtn_info("SPIR-V shader dumped to %s", filename);
119 }
120
121 void
122 _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
123 const char *fmt, ...)
124 {
125 va_list args;
126
127 va_start(args, fmt);
128 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_WARNING, "SPIR-V WARNING:\n",
129 file, line, fmt, args);
130 va_end(args);
131 }
132
133 void
134 _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
135 const char *fmt, ...)
136 {
137 va_list args;
138
139 va_start(args, fmt);
140 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V ERROR:\n",
141 file, line, fmt, args);
142 va_end(args);
143 }
144
145 void
146 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
147 const char *fmt, ...)
148 {
149 va_list args;
150
151 va_start(args, fmt);
152 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V parsing FAILED:\n",
153 file, line, fmt, args);
154 va_end(args);
155
156 const char *dump_path = getenv("MESA_SPIRV_FAIL_DUMP_PATH");
157 if (dump_path)
158 vtn_dump_shader(b, dump_path, "fail");
159
160 longjmp(b->fail_jump, 1);
161 }
162
163 struct spec_constant_value {
164 bool is_double;
165 union {
166 uint32_t data32;
167 uint64_t data64;
168 };
169 };
170
171 static struct vtn_ssa_value *
172 vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
173 {
174 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
175 val->type = type;
176
177 if (glsl_type_is_vector_or_scalar(type)) {
178 unsigned num_components = glsl_get_vector_elements(val->type);
179 unsigned bit_size = glsl_get_bit_size(val->type);
180 val->def = nir_ssa_undef(&b->nb, num_components, bit_size);
181 } else {
182 unsigned elems = glsl_get_length(val->type);
183 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
184 if (glsl_type_is_matrix(type)) {
185 const struct glsl_type *elem_type =
186 glsl_vector_type(glsl_get_base_type(type),
187 glsl_get_vector_elements(type));
188
189 for (unsigned i = 0; i < elems; i++)
190 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
191 } else if (glsl_type_is_array(type)) {
192 const struct glsl_type *elem_type = glsl_get_array_element(type);
193 for (unsigned i = 0; i < elems; i++)
194 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
195 } else {
196 for (unsigned i = 0; i < elems; i++) {
197 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
198 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
199 }
200 }
201 }
202
203 return val;
204 }
205
206 static struct vtn_ssa_value *
207 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
208 const struct glsl_type *type)
209 {
210 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
211
212 if (entry)
213 return entry->data;
214
215 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
216 val->type = type;
217
218 switch (glsl_get_base_type(type)) {
219 case GLSL_TYPE_INT:
220 case GLSL_TYPE_UINT:
221 case GLSL_TYPE_INT16:
222 case GLSL_TYPE_UINT16:
223 case GLSL_TYPE_UINT8:
224 case GLSL_TYPE_INT8:
225 case GLSL_TYPE_INT64:
226 case GLSL_TYPE_UINT64:
227 case GLSL_TYPE_BOOL:
228 case GLSL_TYPE_FLOAT:
229 case GLSL_TYPE_FLOAT16:
230 case GLSL_TYPE_DOUBLE: {
231 int bit_size = glsl_get_bit_size(type);
232 if (glsl_type_is_vector_or_scalar(type)) {
233 unsigned num_components = glsl_get_vector_elements(val->type);
234 nir_load_const_instr *load =
235 nir_load_const_instr_create(b->shader, num_components, bit_size);
236
237 load->value = constant->values[0];
238
239 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
240 val->def = &load->def;
241 } else {
242 assert(glsl_type_is_matrix(type));
243 unsigned rows = glsl_get_vector_elements(val->type);
244 unsigned columns = glsl_get_matrix_columns(val->type);
245 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
246
247 for (unsigned i = 0; i < columns; i++) {
248 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
249 col_val->type = glsl_get_column_type(val->type);
250 nir_load_const_instr *load =
251 nir_load_const_instr_create(b->shader, rows, bit_size);
252
253 load->value = constant->values[i];
254
255 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
256 col_val->def = &load->def;
257
258 val->elems[i] = col_val;
259 }
260 }
261 break;
262 }
263
264 case GLSL_TYPE_ARRAY: {
265 unsigned elems = glsl_get_length(val->type);
266 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
267 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
268 for (unsigned i = 0; i < elems; i++)
269 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
270 elem_type);
271 break;
272 }
273
274 case GLSL_TYPE_STRUCT: {
275 unsigned elems = glsl_get_length(val->type);
276 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
277 for (unsigned i = 0; i < elems; i++) {
278 const struct glsl_type *elem_type =
279 glsl_get_struct_field(val->type, i);
280 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
281 elem_type);
282 }
283 break;
284 }
285
286 default:
287 vtn_fail("bad constant type");
288 }
289
290 return val;
291 }
292
293 struct vtn_ssa_value *
294 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
295 {
296 struct vtn_value *val = vtn_untyped_value(b, value_id);
297 switch (val->value_type) {
298 case vtn_value_type_undef:
299 return vtn_undef_ssa_value(b, val->type->type);
300
301 case vtn_value_type_constant:
302 return vtn_const_ssa_value(b, val->constant, val->type->type);
303
304 case vtn_value_type_ssa:
305 return val->ssa;
306
307 case vtn_value_type_pointer:
308 vtn_assert(val->pointer->ptr_type && val->pointer->ptr_type->type);
309 struct vtn_ssa_value *ssa =
310 vtn_create_ssa_value(b, val->pointer->ptr_type->type);
311 ssa->def = vtn_pointer_to_ssa(b, val->pointer);
312 return ssa;
313
314 default:
315 vtn_fail("Invalid type for an SSA value");
316 }
317 }
318
319 static char *
320 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
321 unsigned word_count, unsigned *words_used)
322 {
323 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
324 if (words_used) {
325 /* Ammount of space taken by the string (including the null) */
326 unsigned len = strlen(dup) + 1;
327 *words_used = DIV_ROUND_UP(len, sizeof(*words));
328 }
329 return dup;
330 }
331
332 const uint32_t *
333 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
334 const uint32_t *end, vtn_instruction_handler handler)
335 {
336 b->file = NULL;
337 b->line = -1;
338 b->col = -1;
339
340 const uint32_t *w = start;
341 while (w < end) {
342 SpvOp opcode = w[0] & SpvOpCodeMask;
343 unsigned count = w[0] >> SpvWordCountShift;
344 vtn_assert(count >= 1 && w + count <= end);
345
346 b->spirv_offset = (uint8_t *)w - (uint8_t *)b->spirv;
347
348 switch (opcode) {
349 case SpvOpNop:
350 break; /* Do nothing */
351
352 case SpvOpLine:
353 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
354 b->line = w[2];
355 b->col = w[3];
356 break;
357
358 case SpvOpNoLine:
359 b->file = NULL;
360 b->line = -1;
361 b->col = -1;
362 break;
363
364 default:
365 if (!handler(b, opcode, w, count))
366 return w;
367 break;
368 }
369
370 w += count;
371 }
372
373 b->spirv_offset = 0;
374 b->file = NULL;
375 b->line = -1;
376 b->col = -1;
377
378 assert(w == end);
379 return w;
380 }
381
382 static void
383 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
384 const uint32_t *w, unsigned count)
385 {
386 const char *ext = (const char *)&w[2];
387 switch (opcode) {
388 case SpvOpExtInstImport: {
389 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
390 if (strcmp(ext, "GLSL.std.450") == 0) {
391 val->ext_handler = vtn_handle_glsl450_instruction;
392 } else if ((strcmp(ext, "SPV_AMD_gcn_shader") == 0)
393 && (b->options && b->options->caps.gcn_shader)) {
394 val->ext_handler = vtn_handle_amd_gcn_shader_instruction;
395 } else if ((strcmp(ext, "SPV_AMD_shader_trinary_minmax") == 0)
396 && (b->options && b->options->caps.trinary_minmax)) {
397 val->ext_handler = vtn_handle_amd_shader_trinary_minmax_instruction;
398 } else {
399 vtn_fail("Unsupported extension: %s", ext);
400 }
401 break;
402 }
403
404 case SpvOpExtInst: {
405 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
406 bool handled = val->ext_handler(b, w[4], w, count);
407 vtn_assert(handled);
408 break;
409 }
410
411 default:
412 vtn_fail("Unhandled opcode");
413 }
414 }
415
416 static void
417 _foreach_decoration_helper(struct vtn_builder *b,
418 struct vtn_value *base_value,
419 int parent_member,
420 struct vtn_value *value,
421 vtn_decoration_foreach_cb cb, void *data)
422 {
423 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
424 int member;
425 if (dec->scope == VTN_DEC_DECORATION) {
426 member = parent_member;
427 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
428 vtn_fail_if(value->value_type != vtn_value_type_type ||
429 value->type->base_type != vtn_base_type_struct,
430 "OpMemberDecorate and OpGroupMemberDecorate are only "
431 "allowed on OpTypeStruct");
432 /* This means we haven't recursed yet */
433 assert(value == base_value);
434
435 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
436
437 vtn_fail_if(member >= base_value->type->length,
438 "OpMemberDecorate specifies member %d but the "
439 "OpTypeStruct has only %u members",
440 member, base_value->type->length);
441 } else {
442 /* Not a decoration */
443 assert(dec->scope == VTN_DEC_EXECUTION_MODE);
444 continue;
445 }
446
447 if (dec->group) {
448 assert(dec->group->value_type == vtn_value_type_decoration_group);
449 _foreach_decoration_helper(b, base_value, member, dec->group,
450 cb, data);
451 } else {
452 cb(b, base_value, member, dec, data);
453 }
454 }
455 }
456
457 /** Iterates (recursively if needed) over all of the decorations on a value
458 *
459 * This function iterates over all of the decorations applied to a given
460 * value. If it encounters a decoration group, it recurses into the group
461 * and iterates over all of those decorations as well.
462 */
463 void
464 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
465 vtn_decoration_foreach_cb cb, void *data)
466 {
467 _foreach_decoration_helper(b, value, -1, value, cb, data);
468 }
469
470 void
471 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
472 vtn_execution_mode_foreach_cb cb, void *data)
473 {
474 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
475 if (dec->scope != VTN_DEC_EXECUTION_MODE)
476 continue;
477
478 assert(dec->group == NULL);
479 cb(b, value, dec, data);
480 }
481 }
482
483 void
484 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
485 const uint32_t *w, unsigned count)
486 {
487 const uint32_t *w_end = w + count;
488 const uint32_t target = w[1];
489 w += 2;
490
491 switch (opcode) {
492 case SpvOpDecorationGroup:
493 vtn_push_value(b, target, vtn_value_type_decoration_group);
494 break;
495
496 case SpvOpDecorate:
497 case SpvOpMemberDecorate:
498 case SpvOpDecorateStringGOOGLE:
499 case SpvOpMemberDecorateStringGOOGLE:
500 case SpvOpExecutionMode: {
501 struct vtn_value *val = vtn_untyped_value(b, target);
502
503 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
504 switch (opcode) {
505 case SpvOpDecorate:
506 case SpvOpDecorateStringGOOGLE:
507 dec->scope = VTN_DEC_DECORATION;
508 break;
509 case SpvOpMemberDecorate:
510 case SpvOpMemberDecorateStringGOOGLE:
511 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
512 vtn_fail_if(dec->scope < VTN_DEC_STRUCT_MEMBER0, /* overflow */
513 "Member argument of OpMemberDecorate too large");
514 break;
515 case SpvOpExecutionMode:
516 dec->scope = VTN_DEC_EXECUTION_MODE;
517 break;
518 default:
519 unreachable("Invalid decoration opcode");
520 }
521 dec->decoration = *(w++);
522 dec->literals = w;
523
524 /* Link into the list */
525 dec->next = val->decoration;
526 val->decoration = dec;
527 break;
528 }
529
530 case SpvOpGroupMemberDecorate:
531 case SpvOpGroupDecorate: {
532 struct vtn_value *group =
533 vtn_value(b, target, vtn_value_type_decoration_group);
534
535 for (; w < w_end; w++) {
536 struct vtn_value *val = vtn_untyped_value(b, *w);
537 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
538
539 dec->group = group;
540 if (opcode == SpvOpGroupDecorate) {
541 dec->scope = VTN_DEC_DECORATION;
542 } else {
543 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(++w);
544 vtn_fail_if(dec->scope < 0, /* Check for overflow */
545 "Member argument of OpGroupMemberDecorate too large");
546 }
547
548 /* Link into the list */
549 dec->next = val->decoration;
550 val->decoration = dec;
551 }
552 break;
553 }
554
555 default:
556 unreachable("Unhandled opcode");
557 }
558 }
559
560 struct member_decoration_ctx {
561 unsigned num_fields;
562 struct glsl_struct_field *fields;
563 struct vtn_type *type;
564 };
565
566 /**
567 * Returns true if the given type contains a struct decorated Block or
568 * BufferBlock
569 */
570 static bool
571 vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type)
572 {
573 switch (type->base_type) {
574 case vtn_base_type_array:
575 return vtn_type_contains_block(b, type->array_element);
576 case vtn_base_type_struct:
577 if (type->block || type->buffer_block)
578 return true;
579 for (unsigned i = 0; i < type->length; i++) {
580 if (vtn_type_contains_block(b, type->members[i]))
581 return true;
582 }
583 return false;
584 default:
585 return false;
586 }
587 }
588
589 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
590 * OpStore, or OpCopyMemory between them without breaking anything.
591 * Technically, the SPIR-V rules require the exact same type ID but this lets
592 * us internally be a bit looser.
593 */
594 bool
595 vtn_types_compatible(struct vtn_builder *b,
596 struct vtn_type *t1, struct vtn_type *t2)
597 {
598 if (t1->id == t2->id)
599 return true;
600
601 if (t1->base_type != t2->base_type)
602 return false;
603
604 switch (t1->base_type) {
605 case vtn_base_type_void:
606 case vtn_base_type_scalar:
607 case vtn_base_type_vector:
608 case vtn_base_type_matrix:
609 case vtn_base_type_image:
610 case vtn_base_type_sampler:
611 case vtn_base_type_sampled_image:
612 return t1->type == t2->type;
613
614 case vtn_base_type_array:
615 return t1->length == t2->length &&
616 vtn_types_compatible(b, t1->array_element, t2->array_element);
617
618 case vtn_base_type_pointer:
619 return vtn_types_compatible(b, t1->deref, t2->deref);
620
621 case vtn_base_type_struct:
622 if (t1->length != t2->length)
623 return false;
624
625 for (unsigned i = 0; i < t1->length; i++) {
626 if (!vtn_types_compatible(b, t1->members[i], t2->members[i]))
627 return false;
628 }
629 return true;
630
631 case vtn_base_type_function:
632 /* This case shouldn't get hit since you can't copy around function
633 * types. Just require them to be identical.
634 */
635 return false;
636 }
637
638 vtn_fail("Invalid base type");
639 }
640
641 /* does a shallow copy of a vtn_type */
642
643 static struct vtn_type *
644 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
645 {
646 struct vtn_type *dest = ralloc(b, struct vtn_type);
647 *dest = *src;
648
649 switch (src->base_type) {
650 case vtn_base_type_void:
651 case vtn_base_type_scalar:
652 case vtn_base_type_vector:
653 case vtn_base_type_matrix:
654 case vtn_base_type_array:
655 case vtn_base_type_pointer:
656 case vtn_base_type_image:
657 case vtn_base_type_sampler:
658 case vtn_base_type_sampled_image:
659 /* Nothing more to do */
660 break;
661
662 case vtn_base_type_struct:
663 dest->members = ralloc_array(b, struct vtn_type *, src->length);
664 memcpy(dest->members, src->members,
665 src->length * sizeof(src->members[0]));
666
667 dest->offsets = ralloc_array(b, unsigned, src->length);
668 memcpy(dest->offsets, src->offsets,
669 src->length * sizeof(src->offsets[0]));
670 break;
671
672 case vtn_base_type_function:
673 dest->params = ralloc_array(b, struct vtn_type *, src->length);
674 memcpy(dest->params, src->params, src->length * sizeof(src->params[0]));
675 break;
676 }
677
678 return dest;
679 }
680
681 static struct vtn_type *
682 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
683 {
684 type->members[member] = vtn_type_copy(b, type->members[member]);
685 type = type->members[member];
686
687 /* We may have an array of matrices.... Oh, joy! */
688 while (glsl_type_is_array(type->type)) {
689 type->array_element = vtn_type_copy(b, type->array_element);
690 type = type->array_element;
691 }
692
693 vtn_assert(glsl_type_is_matrix(type->type));
694
695 return type;
696 }
697
698 static void
699 vtn_handle_access_qualifier(struct vtn_builder *b, struct vtn_type *type,
700 int member, enum gl_access_qualifier access)
701 {
702 type->members[member] = vtn_type_copy(b, type->members[member]);
703 type = type->members[member];
704
705 type->access |= access;
706 }
707
708 static void
709 array_stride_decoration_cb(struct vtn_builder *b,
710 struct vtn_value *val, int member,
711 const struct vtn_decoration *dec, void *void_ctx)
712 {
713 struct vtn_type *type = val->type;
714
715 if (dec->decoration == SpvDecorationArrayStride) {
716 vtn_fail_if(dec->literals[0] == 0, "ArrayStride must be non-zero");
717 type->stride = dec->literals[0];
718 }
719 }
720
721 static void
722 struct_member_decoration_cb(struct vtn_builder *b,
723 struct vtn_value *val, int member,
724 const struct vtn_decoration *dec, void *void_ctx)
725 {
726 struct member_decoration_ctx *ctx = void_ctx;
727
728 if (member < 0)
729 return;
730
731 assert(member < ctx->num_fields);
732
733 switch (dec->decoration) {
734 case SpvDecorationRelaxedPrecision:
735 case SpvDecorationUniform:
736 break; /* FIXME: Do nothing with this for now. */
737 case SpvDecorationNonWritable:
738 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_WRITEABLE);
739 break;
740 case SpvDecorationNonReadable:
741 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_READABLE);
742 break;
743 case SpvDecorationVolatile:
744 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_VOLATILE);
745 break;
746 case SpvDecorationCoherent:
747 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_COHERENT);
748 break;
749 case SpvDecorationNoPerspective:
750 ctx->fields[member].interpolation = INTERP_MODE_NOPERSPECTIVE;
751 break;
752 case SpvDecorationFlat:
753 ctx->fields[member].interpolation = INTERP_MODE_FLAT;
754 break;
755 case SpvDecorationCentroid:
756 ctx->fields[member].centroid = true;
757 break;
758 case SpvDecorationSample:
759 ctx->fields[member].sample = true;
760 break;
761 case SpvDecorationStream:
762 /* Vulkan only allows one GS stream */
763 vtn_assert(dec->literals[0] == 0);
764 break;
765 case SpvDecorationLocation:
766 ctx->fields[member].location = dec->literals[0];
767 break;
768 case SpvDecorationComponent:
769 break; /* FIXME: What should we do with these? */
770 case SpvDecorationBuiltIn:
771 ctx->type->members[member] = vtn_type_copy(b, ctx->type->members[member]);
772 ctx->type->members[member]->is_builtin = true;
773 ctx->type->members[member]->builtin = dec->literals[0];
774 ctx->type->builtin_block = true;
775 break;
776 case SpvDecorationOffset:
777 ctx->type->offsets[member] = dec->literals[0];
778 ctx->fields[member].offset = dec->literals[0];
779 break;
780 case SpvDecorationMatrixStride:
781 /* Handled as a second pass */
782 break;
783 case SpvDecorationColMajor:
784 break; /* Nothing to do here. Column-major is the default. */
785 case SpvDecorationRowMajor:
786 mutable_matrix_member(b, ctx->type, member)->row_major = true;
787 break;
788
789 case SpvDecorationPatch:
790 break;
791
792 case SpvDecorationSpecId:
793 case SpvDecorationBlock:
794 case SpvDecorationBufferBlock:
795 case SpvDecorationArrayStride:
796 case SpvDecorationGLSLShared:
797 case SpvDecorationGLSLPacked:
798 case SpvDecorationInvariant:
799 case SpvDecorationRestrict:
800 case SpvDecorationAliased:
801 case SpvDecorationConstant:
802 case SpvDecorationIndex:
803 case SpvDecorationBinding:
804 case SpvDecorationDescriptorSet:
805 case SpvDecorationLinkageAttributes:
806 case SpvDecorationNoContraction:
807 case SpvDecorationInputAttachmentIndex:
808 vtn_warn("Decoration not allowed on struct members: %s",
809 spirv_decoration_to_string(dec->decoration));
810 break;
811
812 case SpvDecorationXfbBuffer:
813 case SpvDecorationXfbStride:
814 vtn_warn("Vulkan does not have transform feedback");
815 break;
816
817 case SpvDecorationCPacked:
818 case SpvDecorationSaturatedConversion:
819 case SpvDecorationFuncParamAttr:
820 case SpvDecorationFPRoundingMode:
821 case SpvDecorationFPFastMathMode:
822 case SpvDecorationAlignment:
823 vtn_warn("Decoration only allowed for CL-style kernels: %s",
824 spirv_decoration_to_string(dec->decoration));
825 break;
826
827 case SpvDecorationHlslSemanticGOOGLE:
828 /* HLSL semantic decorations can safely be ignored by the driver. */
829 break;
830
831 default:
832 vtn_fail("Unhandled decoration");
833 }
834 }
835
836 /** Chases the array type all the way down to the tail and rewrites the
837 * glsl_types to be based off the tail's glsl_type.
838 */
839 static void
840 vtn_array_type_rewrite_glsl_type(struct vtn_type *type)
841 {
842 if (type->base_type != vtn_base_type_array)
843 return;
844
845 vtn_array_type_rewrite_glsl_type(type->array_element);
846
847 type->type = glsl_array_type(type->array_element->type,
848 type->length, type->stride);
849 }
850
851 /* Matrix strides are handled as a separate pass because we need to know
852 * whether the matrix is row-major or not first.
853 */
854 static void
855 struct_member_matrix_stride_cb(struct vtn_builder *b,
856 struct vtn_value *val, int member,
857 const struct vtn_decoration *dec,
858 void *void_ctx)
859 {
860 if (dec->decoration != SpvDecorationMatrixStride)
861 return;
862
863 vtn_fail_if(member < 0,
864 "The MatrixStride decoration is only allowed on members "
865 "of OpTypeStruct");
866 vtn_fail_if(dec->literals[0] == 0, "MatrixStride must be non-zero");
867
868 struct member_decoration_ctx *ctx = void_ctx;
869
870 struct vtn_type *mat_type = mutable_matrix_member(b, ctx->type, member);
871 if (mat_type->row_major) {
872 mat_type->array_element = vtn_type_copy(b, mat_type->array_element);
873 mat_type->stride = mat_type->array_element->stride;
874 mat_type->array_element->stride = dec->literals[0];
875
876 mat_type->type = glsl_explicit_matrix_type(mat_type->type,
877 dec->literals[0], true);
878 mat_type->array_element->type = glsl_get_column_type(mat_type->type);
879 } else {
880 vtn_assert(mat_type->array_element->stride > 0);
881 mat_type->stride = dec->literals[0];
882
883 mat_type->type = glsl_explicit_matrix_type(mat_type->type,
884 dec->literals[0], false);
885 }
886
887 /* Now that we've replaced the glsl_type with a properly strided matrix
888 * type, rewrite the member type so that it's an array of the proper kind
889 * of glsl_type.
890 */
891 vtn_array_type_rewrite_glsl_type(ctx->type->members[member]);
892 ctx->fields[member].type = ctx->type->members[member]->type;
893 }
894
895 static void
896 type_decoration_cb(struct vtn_builder *b,
897 struct vtn_value *val, int member,
898 const struct vtn_decoration *dec, void *ctx)
899 {
900 struct vtn_type *type = val->type;
901
902 if (member != -1) {
903 /* This should have been handled by OpTypeStruct */
904 assert(val->type->base_type == vtn_base_type_struct);
905 assert(member >= 0 && member < val->type->length);
906 return;
907 }
908
909 switch (dec->decoration) {
910 case SpvDecorationArrayStride:
911 vtn_assert(type->base_type == vtn_base_type_array ||
912 type->base_type == vtn_base_type_pointer);
913 break;
914 case SpvDecorationBlock:
915 vtn_assert(type->base_type == vtn_base_type_struct);
916 type->block = true;
917 break;
918 case SpvDecorationBufferBlock:
919 vtn_assert(type->base_type == vtn_base_type_struct);
920 type->buffer_block = true;
921 break;
922 case SpvDecorationGLSLShared:
923 case SpvDecorationGLSLPacked:
924 /* Ignore these, since we get explicit offsets anyways */
925 break;
926
927 case SpvDecorationRowMajor:
928 case SpvDecorationColMajor:
929 case SpvDecorationMatrixStride:
930 case SpvDecorationBuiltIn:
931 case SpvDecorationNoPerspective:
932 case SpvDecorationFlat:
933 case SpvDecorationPatch:
934 case SpvDecorationCentroid:
935 case SpvDecorationSample:
936 case SpvDecorationVolatile:
937 case SpvDecorationCoherent:
938 case SpvDecorationNonWritable:
939 case SpvDecorationNonReadable:
940 case SpvDecorationUniform:
941 case SpvDecorationLocation:
942 case SpvDecorationComponent:
943 case SpvDecorationOffset:
944 case SpvDecorationXfbBuffer:
945 case SpvDecorationXfbStride:
946 case SpvDecorationHlslSemanticGOOGLE:
947 vtn_warn("Decoration only allowed for struct members: %s",
948 spirv_decoration_to_string(dec->decoration));
949 break;
950
951 case SpvDecorationStream:
952 /* We don't need to do anything here, as stream is filled up when
953 * aplying the decoration to a variable, just check that if it is not a
954 * struct member, it should be a struct.
955 */
956 vtn_assert(type->base_type == vtn_base_type_struct);
957 break;
958
959 case SpvDecorationRelaxedPrecision:
960 case SpvDecorationSpecId:
961 case SpvDecorationInvariant:
962 case SpvDecorationRestrict:
963 case SpvDecorationAliased:
964 case SpvDecorationConstant:
965 case SpvDecorationIndex:
966 case SpvDecorationBinding:
967 case SpvDecorationDescriptorSet:
968 case SpvDecorationLinkageAttributes:
969 case SpvDecorationNoContraction:
970 case SpvDecorationInputAttachmentIndex:
971 vtn_warn("Decoration not allowed on types: %s",
972 spirv_decoration_to_string(dec->decoration));
973 break;
974
975 case SpvDecorationCPacked:
976 case SpvDecorationSaturatedConversion:
977 case SpvDecorationFuncParamAttr:
978 case SpvDecorationFPRoundingMode:
979 case SpvDecorationFPFastMathMode:
980 case SpvDecorationAlignment:
981 vtn_warn("Decoration only allowed for CL-style kernels: %s",
982 spirv_decoration_to_string(dec->decoration));
983 break;
984
985 default:
986 vtn_fail("Unhandled decoration");
987 }
988 }
989
990 static unsigned
991 translate_image_format(struct vtn_builder *b, SpvImageFormat format)
992 {
993 switch (format) {
994 case SpvImageFormatUnknown: return 0; /* GL_NONE */
995 case SpvImageFormatRgba32f: return 0x8814; /* GL_RGBA32F */
996 case SpvImageFormatRgba16f: return 0x881A; /* GL_RGBA16F */
997 case SpvImageFormatR32f: return 0x822E; /* GL_R32F */
998 case SpvImageFormatRgba8: return 0x8058; /* GL_RGBA8 */
999 case SpvImageFormatRgba8Snorm: return 0x8F97; /* GL_RGBA8_SNORM */
1000 case SpvImageFormatRg32f: return 0x8230; /* GL_RG32F */
1001 case SpvImageFormatRg16f: return 0x822F; /* GL_RG16F */
1002 case SpvImageFormatR11fG11fB10f: return 0x8C3A; /* GL_R11F_G11F_B10F */
1003 case SpvImageFormatR16f: return 0x822D; /* GL_R16F */
1004 case SpvImageFormatRgba16: return 0x805B; /* GL_RGBA16 */
1005 case SpvImageFormatRgb10A2: return 0x8059; /* GL_RGB10_A2 */
1006 case SpvImageFormatRg16: return 0x822C; /* GL_RG16 */
1007 case SpvImageFormatRg8: return 0x822B; /* GL_RG8 */
1008 case SpvImageFormatR16: return 0x822A; /* GL_R16 */
1009 case SpvImageFormatR8: return 0x8229; /* GL_R8 */
1010 case SpvImageFormatRgba16Snorm: return 0x8F9B; /* GL_RGBA16_SNORM */
1011 case SpvImageFormatRg16Snorm: return 0x8F99; /* GL_RG16_SNORM */
1012 case SpvImageFormatRg8Snorm: return 0x8F95; /* GL_RG8_SNORM */
1013 case SpvImageFormatR16Snorm: return 0x8F98; /* GL_R16_SNORM */
1014 case SpvImageFormatR8Snorm: return 0x8F94; /* GL_R8_SNORM */
1015 case SpvImageFormatRgba32i: return 0x8D82; /* GL_RGBA32I */
1016 case SpvImageFormatRgba16i: return 0x8D88; /* GL_RGBA16I */
1017 case SpvImageFormatRgba8i: return 0x8D8E; /* GL_RGBA8I */
1018 case SpvImageFormatR32i: return 0x8235; /* GL_R32I */
1019 case SpvImageFormatRg32i: return 0x823B; /* GL_RG32I */
1020 case SpvImageFormatRg16i: return 0x8239; /* GL_RG16I */
1021 case SpvImageFormatRg8i: return 0x8237; /* GL_RG8I */
1022 case SpvImageFormatR16i: return 0x8233; /* GL_R16I */
1023 case SpvImageFormatR8i: return 0x8231; /* GL_R8I */
1024 case SpvImageFormatRgba32ui: return 0x8D70; /* GL_RGBA32UI */
1025 case SpvImageFormatRgba16ui: return 0x8D76; /* GL_RGBA16UI */
1026 case SpvImageFormatRgba8ui: return 0x8D7C; /* GL_RGBA8UI */
1027 case SpvImageFormatR32ui: return 0x8236; /* GL_R32UI */
1028 case SpvImageFormatRgb10a2ui: return 0x906F; /* GL_RGB10_A2UI */
1029 case SpvImageFormatRg32ui: return 0x823C; /* GL_RG32UI */
1030 case SpvImageFormatRg16ui: return 0x823A; /* GL_RG16UI */
1031 case SpvImageFormatRg8ui: return 0x8238; /* GL_RG8UI */
1032 case SpvImageFormatR16ui: return 0x8234; /* GL_R16UI */
1033 case SpvImageFormatR8ui: return 0x8232; /* GL_R8UI */
1034 default:
1035 vtn_fail("Invalid image format");
1036 }
1037 }
1038
1039 static struct vtn_type *
1040 vtn_type_layout_std430(struct vtn_builder *b, struct vtn_type *type,
1041 uint32_t *size_out, uint32_t *align_out)
1042 {
1043 switch (type->base_type) {
1044 case vtn_base_type_scalar: {
1045 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
1046 *size_out = comp_size;
1047 *align_out = comp_size;
1048 return type;
1049 }
1050
1051 case vtn_base_type_vector: {
1052 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
1053 unsigned align_comps = type->length == 3 ? 4 : type->length;
1054 *size_out = comp_size * type->length,
1055 *align_out = comp_size * align_comps;
1056 return type;
1057 }
1058
1059 case vtn_base_type_matrix:
1060 case vtn_base_type_array: {
1061 /* We're going to add an array stride */
1062 type = vtn_type_copy(b, type);
1063 uint32_t elem_size, elem_align;
1064 type->array_element = vtn_type_layout_std430(b, type->array_element,
1065 &elem_size, &elem_align);
1066 type->stride = vtn_align_u32(elem_size, elem_align);
1067 *size_out = type->stride * type->length;
1068 *align_out = elem_align;
1069 return type;
1070 }
1071
1072 case vtn_base_type_struct: {
1073 /* We're going to add member offsets */
1074 type = vtn_type_copy(b, type);
1075 uint32_t offset = 0;
1076 uint32_t align = 0;
1077 for (unsigned i = 0; i < type->length; i++) {
1078 uint32_t mem_size, mem_align;
1079 type->members[i] = vtn_type_layout_std430(b, type->members[i],
1080 &mem_size, &mem_align);
1081 offset = vtn_align_u32(offset, mem_align);
1082 type->offsets[i] = offset;
1083 offset += mem_size;
1084 align = MAX2(align, mem_align);
1085 }
1086 *size_out = offset;
1087 *align_out = align;
1088 return type;
1089 }
1090
1091 default:
1092 unreachable("Invalid SPIR-V type for std430");
1093 }
1094 }
1095
1096 static void
1097 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
1098 const uint32_t *w, unsigned count)
1099 {
1100 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
1101
1102 val->type = rzalloc(b, struct vtn_type);
1103 val->type->id = w[1];
1104
1105 switch (opcode) {
1106 case SpvOpTypeVoid:
1107 val->type->base_type = vtn_base_type_void;
1108 val->type->type = glsl_void_type();
1109 break;
1110 case SpvOpTypeBool:
1111 val->type->base_type = vtn_base_type_scalar;
1112 val->type->type = glsl_bool_type();
1113 val->type->length = 1;
1114 break;
1115 case SpvOpTypeInt: {
1116 int bit_size = w[2];
1117 const bool signedness = w[3];
1118 val->type->base_type = vtn_base_type_scalar;
1119 switch (bit_size) {
1120 case 64:
1121 val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
1122 break;
1123 case 32:
1124 val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
1125 break;
1126 case 16:
1127 val->type->type = (signedness ? glsl_int16_t_type() : glsl_uint16_t_type());
1128 break;
1129 case 8:
1130 val->type->type = (signedness ? glsl_int8_t_type() : glsl_uint8_t_type());
1131 break;
1132 default:
1133 vtn_fail("Invalid int bit size");
1134 }
1135 val->type->length = 1;
1136 break;
1137 }
1138
1139 case SpvOpTypeFloat: {
1140 int bit_size = w[2];
1141 val->type->base_type = vtn_base_type_scalar;
1142 switch (bit_size) {
1143 case 16:
1144 val->type->type = glsl_float16_t_type();
1145 break;
1146 case 32:
1147 val->type->type = glsl_float_type();
1148 break;
1149 case 64:
1150 val->type->type = glsl_double_type();
1151 break;
1152 default:
1153 vtn_fail("Invalid float bit size");
1154 }
1155 val->type->length = 1;
1156 break;
1157 }
1158
1159 case SpvOpTypeVector: {
1160 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1161 unsigned elems = w[3];
1162
1163 vtn_fail_if(base->base_type != vtn_base_type_scalar,
1164 "Base type for OpTypeVector must be a scalar");
1165 vtn_fail_if((elems < 2 || elems > 4) && (elems != 8) && (elems != 16),
1166 "Invalid component count for OpTypeVector");
1167
1168 val->type->base_type = vtn_base_type_vector;
1169 val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
1170 val->type->length = elems;
1171 val->type->stride = glsl_get_bit_size(base->type) / 8;
1172 val->type->array_element = base;
1173 break;
1174 }
1175
1176 case SpvOpTypeMatrix: {
1177 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1178 unsigned columns = w[3];
1179
1180 vtn_fail_if(base->base_type != vtn_base_type_vector,
1181 "Base type for OpTypeMatrix must be a vector");
1182 vtn_fail_if(columns < 2 || columns > 4,
1183 "Invalid column count for OpTypeMatrix");
1184
1185 val->type->base_type = vtn_base_type_matrix;
1186 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
1187 glsl_get_vector_elements(base->type),
1188 columns);
1189 vtn_fail_if(glsl_type_is_error(val->type->type),
1190 "Unsupported base type for OpTypeMatrix");
1191 assert(!glsl_type_is_error(val->type->type));
1192 val->type->length = columns;
1193 val->type->array_element = base;
1194 val->type->row_major = false;
1195 val->type->stride = 0;
1196 break;
1197 }
1198
1199 case SpvOpTypeRuntimeArray:
1200 case SpvOpTypeArray: {
1201 struct vtn_type *array_element =
1202 vtn_value(b, w[2], vtn_value_type_type)->type;
1203
1204 if (opcode == SpvOpTypeRuntimeArray) {
1205 /* A length of 0 is used to denote unsized arrays */
1206 val->type->length = 0;
1207 } else {
1208 val->type->length =
1209 vtn_value(b, w[3], vtn_value_type_constant)->constant->values[0].u32[0];
1210 }
1211
1212 val->type->base_type = vtn_base_type_array;
1213 val->type->array_element = array_element;
1214 val->type->stride = 0;
1215
1216 vtn_foreach_decoration(b, val, array_stride_decoration_cb, NULL);
1217 val->type->type = glsl_array_type(array_element->type, val->type->length,
1218 val->type->stride);
1219 break;
1220 }
1221
1222 case SpvOpTypeStruct: {
1223 unsigned num_fields = count - 2;
1224 val->type->base_type = vtn_base_type_struct;
1225 val->type->length = num_fields;
1226 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
1227 val->type->offsets = ralloc_array(b, unsigned, num_fields);
1228
1229 NIR_VLA(struct glsl_struct_field, fields, count);
1230 for (unsigned i = 0; i < num_fields; i++) {
1231 val->type->members[i] =
1232 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
1233 fields[i] = (struct glsl_struct_field) {
1234 .type = val->type->members[i]->type,
1235 .name = ralloc_asprintf(b, "field%d", i),
1236 .location = -1,
1237 };
1238 }
1239
1240 struct member_decoration_ctx ctx = {
1241 .num_fields = num_fields,
1242 .fields = fields,
1243 .type = val->type
1244 };
1245
1246 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
1247 vtn_foreach_decoration(b, val, struct_member_matrix_stride_cb, &ctx);
1248
1249 const char *name = val->name ? val->name : "struct";
1250
1251 val->type->type = glsl_struct_type(fields, num_fields, name);
1252 break;
1253 }
1254
1255 case SpvOpTypeFunction: {
1256 val->type->base_type = vtn_base_type_function;
1257 val->type->type = NULL;
1258
1259 val->type->return_type = vtn_value(b, w[2], vtn_value_type_type)->type;
1260
1261 const unsigned num_params = count - 3;
1262 val->type->length = num_params;
1263 val->type->params = ralloc_array(b, struct vtn_type *, num_params);
1264 for (unsigned i = 0; i < count - 3; i++) {
1265 val->type->params[i] =
1266 vtn_value(b, w[i + 3], vtn_value_type_type)->type;
1267 }
1268 break;
1269 }
1270
1271 case SpvOpTypePointer: {
1272 SpvStorageClass storage_class = w[2];
1273 struct vtn_type *deref_type =
1274 vtn_value(b, w[3], vtn_value_type_type)->type;
1275
1276 val->type->base_type = vtn_base_type_pointer;
1277 val->type->storage_class = storage_class;
1278 val->type->deref = deref_type;
1279
1280 vtn_foreach_decoration(b, val, array_stride_decoration_cb, NULL);
1281
1282 if (storage_class == SpvStorageClassUniform ||
1283 storage_class == SpvStorageClassStorageBuffer) {
1284 /* These can actually be stored to nir_variables and used as SSA
1285 * values so they need a real glsl_type.
1286 */
1287 val->type->type = glsl_vector_type(GLSL_TYPE_UINT, 2);
1288 }
1289
1290 if (storage_class == SpvStorageClassPushConstant) {
1291 /* These can actually be stored to nir_variables and used as SSA
1292 * values so they need a real glsl_type.
1293 */
1294 val->type->type = glsl_uint_type();
1295 }
1296
1297 if (storage_class == SpvStorageClassWorkgroup) {
1298 /* These can actually be stored to nir_variables and used as SSA
1299 * values so they need a real glsl_type.
1300 */
1301 val->type->type = glsl_uint_type();
1302 if (b->options->lower_workgroup_access_to_offsets) {
1303 uint32_t size, align;
1304 val->type->deref = vtn_type_layout_std430(b, val->type->deref,
1305 &size, &align);
1306 val->type->length = size;
1307 val->type->align = align;
1308 }
1309 }
1310 break;
1311 }
1312
1313 case SpvOpTypeImage: {
1314 val->type->base_type = vtn_base_type_image;
1315
1316 const struct vtn_type *sampled_type =
1317 vtn_value(b, w[2], vtn_value_type_type)->type;
1318
1319 vtn_fail_if(sampled_type->base_type != vtn_base_type_scalar ||
1320 glsl_get_bit_size(sampled_type->type) != 32,
1321 "Sampled type of OpTypeImage must be a 32-bit scalar");
1322
1323 enum glsl_sampler_dim dim;
1324 switch ((SpvDim)w[3]) {
1325 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
1326 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
1327 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
1328 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
1329 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
1330 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
1331 case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
1332 default:
1333 vtn_fail("Invalid SPIR-V image dimensionality");
1334 }
1335
1336 bool is_shadow = w[4];
1337 bool is_array = w[5];
1338 bool multisampled = w[6];
1339 unsigned sampled = w[7];
1340 SpvImageFormat format = w[8];
1341
1342 if (count > 9)
1343 val->type->access_qualifier = w[9];
1344 else
1345 val->type->access_qualifier = SpvAccessQualifierReadWrite;
1346
1347 if (multisampled) {
1348 if (dim == GLSL_SAMPLER_DIM_2D)
1349 dim = GLSL_SAMPLER_DIM_MS;
1350 else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
1351 dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
1352 else
1353 vtn_fail("Unsupported multisampled image type");
1354 }
1355
1356 val->type->image_format = translate_image_format(b, format);
1357
1358 enum glsl_base_type sampled_base_type =
1359 glsl_get_base_type(sampled_type->type);
1360 if (sampled == 1) {
1361 val->type->sampled = true;
1362 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
1363 sampled_base_type);
1364 } else if (sampled == 2) {
1365 vtn_assert(!is_shadow);
1366 val->type->sampled = false;
1367 val->type->type = glsl_image_type(dim, is_array, sampled_base_type);
1368 } else {
1369 vtn_fail("We need to know if the image will be sampled");
1370 }
1371 break;
1372 }
1373
1374 case SpvOpTypeSampledImage:
1375 val->type->base_type = vtn_base_type_sampled_image;
1376 val->type->image = vtn_value(b, w[2], vtn_value_type_type)->type;
1377 val->type->type = val->type->image->type;
1378 break;
1379
1380 case SpvOpTypeSampler:
1381 /* The actual sampler type here doesn't really matter. It gets
1382 * thrown away the moment you combine it with an image. What really
1383 * matters is that it's a sampler type as opposed to an integer type
1384 * so the backend knows what to do.
1385 */
1386 val->type->base_type = vtn_base_type_sampler;
1387 val->type->type = glsl_bare_sampler_type();
1388 break;
1389
1390 case SpvOpTypeOpaque:
1391 case SpvOpTypeEvent:
1392 case SpvOpTypeDeviceEvent:
1393 case SpvOpTypeReserveId:
1394 case SpvOpTypeQueue:
1395 case SpvOpTypePipe:
1396 default:
1397 vtn_fail("Unhandled opcode");
1398 }
1399
1400 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
1401
1402 if (val->type->base_type == vtn_base_type_struct &&
1403 (val->type->block || val->type->buffer_block)) {
1404 for (unsigned i = 0; i < val->type->length; i++) {
1405 vtn_fail_if(vtn_type_contains_block(b, val->type->members[i]),
1406 "Block and BufferBlock decorations cannot decorate a "
1407 "structure type that is nested at any level inside "
1408 "another structure type decorated with Block or "
1409 "BufferBlock.");
1410 }
1411 }
1412 }
1413
1414 static nir_constant *
1415 vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
1416 {
1417 nir_constant *c = rzalloc(b, nir_constant);
1418
1419 /* For pointers and other typeless things, we have to return something but
1420 * it doesn't matter what.
1421 */
1422 if (!type)
1423 return c;
1424
1425 switch (glsl_get_base_type(type)) {
1426 case GLSL_TYPE_INT:
1427 case GLSL_TYPE_UINT:
1428 case GLSL_TYPE_INT16:
1429 case GLSL_TYPE_UINT16:
1430 case GLSL_TYPE_UINT8:
1431 case GLSL_TYPE_INT8:
1432 case GLSL_TYPE_INT64:
1433 case GLSL_TYPE_UINT64:
1434 case GLSL_TYPE_BOOL:
1435 case GLSL_TYPE_FLOAT:
1436 case GLSL_TYPE_FLOAT16:
1437 case GLSL_TYPE_DOUBLE:
1438 /* Nothing to do here. It's already initialized to zero */
1439 break;
1440
1441 case GLSL_TYPE_ARRAY:
1442 vtn_assert(glsl_get_length(type) > 0);
1443 c->num_elements = glsl_get_length(type);
1444 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1445
1446 c->elements[0] = vtn_null_constant(b, glsl_get_array_element(type));
1447 for (unsigned i = 1; i < c->num_elements; i++)
1448 c->elements[i] = c->elements[0];
1449 break;
1450
1451 case GLSL_TYPE_STRUCT:
1452 c->num_elements = glsl_get_length(type);
1453 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1454
1455 for (unsigned i = 0; i < c->num_elements; i++) {
1456 c->elements[i] = vtn_null_constant(b, glsl_get_struct_field(type, i));
1457 }
1458 break;
1459
1460 default:
1461 vtn_fail("Invalid type for null constant");
1462 }
1463
1464 return c;
1465 }
1466
1467 static void
1468 spec_constant_decoration_cb(struct vtn_builder *b, struct vtn_value *v,
1469 int member, const struct vtn_decoration *dec,
1470 void *data)
1471 {
1472 vtn_assert(member == -1);
1473 if (dec->decoration != SpvDecorationSpecId)
1474 return;
1475
1476 struct spec_constant_value *const_value = data;
1477
1478 for (unsigned i = 0; i < b->num_specializations; i++) {
1479 if (b->specializations[i].id == dec->literals[0]) {
1480 if (const_value->is_double)
1481 const_value->data64 = b->specializations[i].data64;
1482 else
1483 const_value->data32 = b->specializations[i].data32;
1484 return;
1485 }
1486 }
1487 }
1488
1489 static uint32_t
1490 get_specialization(struct vtn_builder *b, struct vtn_value *val,
1491 uint32_t const_value)
1492 {
1493 struct spec_constant_value data;
1494 data.is_double = false;
1495 data.data32 = const_value;
1496 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1497 return data.data32;
1498 }
1499
1500 static uint64_t
1501 get_specialization64(struct vtn_builder *b, struct vtn_value *val,
1502 uint64_t const_value)
1503 {
1504 struct spec_constant_value data;
1505 data.is_double = true;
1506 data.data64 = const_value;
1507 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1508 return data.data64;
1509 }
1510
1511 static void
1512 handle_workgroup_size_decoration_cb(struct vtn_builder *b,
1513 struct vtn_value *val,
1514 int member,
1515 const struct vtn_decoration *dec,
1516 void *data)
1517 {
1518 vtn_assert(member == -1);
1519 if (dec->decoration != SpvDecorationBuiltIn ||
1520 dec->literals[0] != SpvBuiltInWorkgroupSize)
1521 return;
1522
1523 vtn_assert(val->type->type == glsl_vector_type(GLSL_TYPE_UINT, 3));
1524
1525 b->shader->info.cs.local_size[0] = val->constant->values[0].u32[0];
1526 b->shader->info.cs.local_size[1] = val->constant->values[0].u32[1];
1527 b->shader->info.cs.local_size[2] = val->constant->values[0].u32[2];
1528 }
1529
1530 static void
1531 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
1532 const uint32_t *w, unsigned count)
1533 {
1534 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
1535 val->constant = rzalloc(b, nir_constant);
1536 switch (opcode) {
1537 case SpvOpConstantTrue:
1538 case SpvOpConstantFalse:
1539 case SpvOpSpecConstantTrue:
1540 case SpvOpSpecConstantFalse: {
1541 vtn_fail_if(val->type->type != glsl_bool_type(),
1542 "Result type of %s must be OpTypeBool",
1543 spirv_op_to_string(opcode));
1544
1545 uint32_t int_val = (opcode == SpvOpConstantTrue ||
1546 opcode == SpvOpSpecConstantTrue);
1547
1548 if (opcode == SpvOpSpecConstantTrue ||
1549 opcode == SpvOpSpecConstantFalse)
1550 int_val = get_specialization(b, val, int_val);
1551
1552 val->constant->values[0].b[0] = int_val != 0;
1553 break;
1554 }
1555
1556 case SpvOpConstant: {
1557 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1558 "Result type of %s must be a scalar",
1559 spirv_op_to_string(opcode));
1560 int bit_size = glsl_get_bit_size(val->type->type);
1561 switch (bit_size) {
1562 case 64:
1563 val->constant->values->u64[0] = vtn_u64_literal(&w[3]);
1564 break;
1565 case 32:
1566 val->constant->values->u32[0] = w[3];
1567 break;
1568 case 16:
1569 val->constant->values->u16[0] = w[3];
1570 break;
1571 case 8:
1572 val->constant->values->u8[0] = w[3];
1573 break;
1574 default:
1575 vtn_fail("Unsupported SpvOpConstant bit size");
1576 }
1577 break;
1578 }
1579
1580 case SpvOpSpecConstant: {
1581 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1582 "Result type of %s must be a scalar",
1583 spirv_op_to_string(opcode));
1584 int bit_size = glsl_get_bit_size(val->type->type);
1585 switch (bit_size) {
1586 case 64:
1587 val->constant->values[0].u64[0] =
1588 get_specialization64(b, val, vtn_u64_literal(&w[3]));
1589 break;
1590 case 32:
1591 val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
1592 break;
1593 case 16:
1594 val->constant->values[0].u16[0] = get_specialization(b, val, w[3]);
1595 break;
1596 case 8:
1597 val->constant->values[0].u8[0] = get_specialization(b, val, w[3]);
1598 break;
1599 default:
1600 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1601 }
1602 break;
1603 }
1604
1605 case SpvOpSpecConstantComposite:
1606 case SpvOpConstantComposite: {
1607 unsigned elem_count = count - 3;
1608 vtn_fail_if(elem_count != val->type->length,
1609 "%s has %u constituents, expected %u",
1610 spirv_op_to_string(opcode), elem_count, val->type->length);
1611
1612 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
1613 for (unsigned i = 0; i < elem_count; i++) {
1614 struct vtn_value *val = vtn_untyped_value(b, w[i + 3]);
1615
1616 if (val->value_type == vtn_value_type_constant) {
1617 elems[i] = val->constant;
1618 } else {
1619 vtn_fail_if(val->value_type != vtn_value_type_undef,
1620 "only constants or undefs allowed for "
1621 "SpvOpConstantComposite");
1622 /* to make it easier, just insert a NULL constant for now */
1623 elems[i] = vtn_null_constant(b, val->type->type);
1624 }
1625 }
1626
1627 switch (val->type->base_type) {
1628 case vtn_base_type_vector: {
1629 assert(glsl_type_is_vector(val->type->type));
1630 int bit_size = glsl_get_bit_size(val->type->type);
1631 for (unsigned i = 0; i < elem_count; i++) {
1632 switch (bit_size) {
1633 case 64:
1634 val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
1635 break;
1636 case 32:
1637 val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
1638 break;
1639 case 16:
1640 val->constant->values[0].u16[i] = elems[i]->values[0].u16[0];
1641 break;
1642 case 8:
1643 val->constant->values[0].u8[i] = elems[i]->values[0].u8[0];
1644 break;
1645 case 1:
1646 val->constant->values[0].b[i] = elems[i]->values[0].b[0];
1647 break;
1648 default:
1649 vtn_fail("Invalid SpvOpConstantComposite bit size");
1650 }
1651 }
1652 break;
1653 }
1654
1655 case vtn_base_type_matrix:
1656 assert(glsl_type_is_matrix(val->type->type));
1657 for (unsigned i = 0; i < elem_count; i++)
1658 val->constant->values[i] = elems[i]->values[0];
1659 break;
1660
1661 case vtn_base_type_struct:
1662 case vtn_base_type_array:
1663 ralloc_steal(val->constant, elems);
1664 val->constant->num_elements = elem_count;
1665 val->constant->elements = elems;
1666 break;
1667
1668 default:
1669 vtn_fail("Result type of %s must be a composite type",
1670 spirv_op_to_string(opcode));
1671 }
1672 break;
1673 }
1674
1675 case SpvOpSpecConstantOp: {
1676 SpvOp opcode = get_specialization(b, val, w[3]);
1677 switch (opcode) {
1678 case SpvOpVectorShuffle: {
1679 struct vtn_value *v0 = &b->values[w[4]];
1680 struct vtn_value *v1 = &b->values[w[5]];
1681
1682 vtn_assert(v0->value_type == vtn_value_type_constant ||
1683 v0->value_type == vtn_value_type_undef);
1684 vtn_assert(v1->value_type == vtn_value_type_constant ||
1685 v1->value_type == vtn_value_type_undef);
1686
1687 unsigned len0 = glsl_get_vector_elements(v0->type->type);
1688 unsigned len1 = glsl_get_vector_elements(v1->type->type);
1689
1690 vtn_assert(len0 + len1 < 16);
1691
1692 unsigned bit_size = glsl_get_bit_size(val->type->type);
1693 unsigned bit_size0 = glsl_get_bit_size(v0->type->type);
1694 unsigned bit_size1 = glsl_get_bit_size(v1->type->type);
1695
1696 vtn_assert(bit_size == bit_size0 && bit_size == bit_size1);
1697 (void)bit_size0; (void)bit_size1;
1698
1699 if (bit_size == 64) {
1700 uint64_t u64[8];
1701 if (v0->value_type == vtn_value_type_constant) {
1702 for (unsigned i = 0; i < len0; i++)
1703 u64[i] = v0->constant->values[0].u64[i];
1704 }
1705 if (v1->value_type == vtn_value_type_constant) {
1706 for (unsigned i = 0; i < len1; i++)
1707 u64[len0 + i] = v1->constant->values[0].u64[i];
1708 }
1709
1710 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1711 uint32_t comp = w[i + 6];
1712 /* If component is not used, set the value to a known constant
1713 * to detect if it is wrongly used.
1714 */
1715 if (comp == (uint32_t)-1)
1716 val->constant->values[0].u64[j] = 0xdeadbeefdeadbeef;
1717 else
1718 val->constant->values[0].u64[j] = u64[comp];
1719 }
1720 } else {
1721 /* This is for both 32-bit and 16-bit values */
1722 uint32_t u32[8];
1723 if (v0->value_type == vtn_value_type_constant) {
1724 for (unsigned i = 0; i < len0; i++)
1725 u32[i] = v0->constant->values[0].u32[i];
1726 }
1727 if (v1->value_type == vtn_value_type_constant) {
1728 for (unsigned i = 0; i < len1; i++)
1729 u32[len0 + i] = v1->constant->values[0].u32[i];
1730 }
1731
1732 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1733 uint32_t comp = w[i + 6];
1734 /* If component is not used, set the value to a known constant
1735 * to detect if it is wrongly used.
1736 */
1737 if (comp == (uint32_t)-1)
1738 val->constant->values[0].u32[j] = 0xdeadbeef;
1739 else
1740 val->constant->values[0].u32[j] = u32[comp];
1741 }
1742 }
1743 break;
1744 }
1745
1746 case SpvOpCompositeExtract:
1747 case SpvOpCompositeInsert: {
1748 struct vtn_value *comp;
1749 unsigned deref_start;
1750 struct nir_constant **c;
1751 if (opcode == SpvOpCompositeExtract) {
1752 comp = vtn_value(b, w[4], vtn_value_type_constant);
1753 deref_start = 5;
1754 c = &comp->constant;
1755 } else {
1756 comp = vtn_value(b, w[5], vtn_value_type_constant);
1757 deref_start = 6;
1758 val->constant = nir_constant_clone(comp->constant,
1759 (nir_variable *)b);
1760 c = &val->constant;
1761 }
1762
1763 int elem = -1;
1764 int col = 0;
1765 const struct vtn_type *type = comp->type;
1766 for (unsigned i = deref_start; i < count; i++) {
1767 vtn_fail_if(w[i] > type->length,
1768 "%uth index of %s is %u but the type has only "
1769 "%u elements", i - deref_start,
1770 spirv_op_to_string(opcode), w[i], type->length);
1771
1772 switch (type->base_type) {
1773 case vtn_base_type_vector:
1774 elem = w[i];
1775 type = type->array_element;
1776 break;
1777
1778 case vtn_base_type_matrix:
1779 assert(col == 0 && elem == -1);
1780 col = w[i];
1781 elem = 0;
1782 type = type->array_element;
1783 break;
1784
1785 case vtn_base_type_array:
1786 c = &(*c)->elements[w[i]];
1787 type = type->array_element;
1788 break;
1789
1790 case vtn_base_type_struct:
1791 c = &(*c)->elements[w[i]];
1792 type = type->members[w[i]];
1793 break;
1794
1795 default:
1796 vtn_fail("%s must only index into composite types",
1797 spirv_op_to_string(opcode));
1798 }
1799 }
1800
1801 if (opcode == SpvOpCompositeExtract) {
1802 if (elem == -1) {
1803 val->constant = *c;
1804 } else {
1805 unsigned num_components = type->length;
1806 unsigned bit_size = glsl_get_bit_size(type->type);
1807 for (unsigned i = 0; i < num_components; i++)
1808 switch(bit_size) {
1809 case 64:
1810 val->constant->values[0].u64[i] = (*c)->values[col].u64[elem + i];
1811 break;
1812 case 32:
1813 val->constant->values[0].u32[i] = (*c)->values[col].u32[elem + i];
1814 break;
1815 case 16:
1816 val->constant->values[0].u16[i] = (*c)->values[col].u16[elem + i];
1817 break;
1818 case 8:
1819 val->constant->values[0].u8[i] = (*c)->values[col].u8[elem + i];
1820 break;
1821 case 1:
1822 val->constant->values[0].b[i] = (*c)->values[col].b[elem + i];
1823 break;
1824 default:
1825 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1826 }
1827 }
1828 } else {
1829 struct vtn_value *insert =
1830 vtn_value(b, w[4], vtn_value_type_constant);
1831 vtn_assert(insert->type == type);
1832 if (elem == -1) {
1833 *c = insert->constant;
1834 } else {
1835 unsigned num_components = type->length;
1836 unsigned bit_size = glsl_get_bit_size(type->type);
1837 for (unsigned i = 0; i < num_components; i++)
1838 switch (bit_size) {
1839 case 64:
1840 (*c)->values[col].u64[elem + i] = insert->constant->values[0].u64[i];
1841 break;
1842 case 32:
1843 (*c)->values[col].u32[elem + i] = insert->constant->values[0].u32[i];
1844 break;
1845 case 16:
1846 (*c)->values[col].u16[elem + i] = insert->constant->values[0].u16[i];
1847 break;
1848 case 8:
1849 (*c)->values[col].u8[elem + i] = insert->constant->values[0].u8[i];
1850 break;
1851 case 1:
1852 (*c)->values[col].b[elem + i] = insert->constant->values[0].b[i];
1853 break;
1854 default:
1855 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1856 }
1857 }
1858 }
1859 break;
1860 }
1861
1862 default: {
1863 bool swap;
1864 nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->type->type);
1865 nir_alu_type src_alu_type = dst_alu_type;
1866 unsigned num_components = glsl_get_vector_elements(val->type->type);
1867 unsigned bit_size;
1868
1869 vtn_assert(count <= 7);
1870
1871 switch (opcode) {
1872 case SpvOpSConvert:
1873 case SpvOpFConvert:
1874 /* We have a source in a conversion */
1875 src_alu_type =
1876 nir_get_nir_type_for_glsl_type(
1877 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1878 /* We use the bitsize of the conversion source to evaluate the opcode later */
1879 bit_size = glsl_get_bit_size(
1880 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1881 break;
1882 default:
1883 bit_size = glsl_get_bit_size(val->type->type);
1884 };
1885
1886 nir_op op = vtn_nir_alu_op_for_spirv_opcode(b, opcode, &swap,
1887 nir_alu_type_get_type_size(src_alu_type),
1888 nir_alu_type_get_type_size(dst_alu_type));
1889 nir_const_value src[4];
1890
1891 for (unsigned i = 0; i < count - 4; i++) {
1892 struct vtn_value *src_val =
1893 vtn_value(b, w[4 + i], vtn_value_type_constant);
1894
1895 /* If this is an unsized source, pull the bit size from the
1896 * source; otherwise, we'll use the bit size from the destination.
1897 */
1898 if (!nir_alu_type_get_type_size(nir_op_infos[op].input_types[i]))
1899 bit_size = glsl_get_bit_size(src_val->type->type);
1900
1901 unsigned j = swap ? 1 - i : i;
1902 src[j] = src_val->constant->values[0];
1903 }
1904
1905 /* fix up fixed size sources */
1906 switch (op) {
1907 case nir_op_ishl:
1908 case nir_op_ishr:
1909 case nir_op_ushr: {
1910 if (bit_size == 32)
1911 break;
1912 for (unsigned i = 0; i < num_components; ++i) {
1913 switch (bit_size) {
1914 case 64: src[1].u32[i] = src[1].u64[i]; break;
1915 case 16: src[1].u32[i] = src[1].u16[i]; break;
1916 case 8: src[1].u32[i] = src[1].u8[i]; break;
1917 }
1918 }
1919 break;
1920 }
1921 default:
1922 break;
1923 }
1924
1925 val->constant->values[0] =
1926 nir_eval_const_opcode(op, num_components, bit_size, src);
1927 break;
1928 } /* default */
1929 }
1930 break;
1931 }
1932
1933 case SpvOpConstantNull:
1934 val->constant = vtn_null_constant(b, val->type->type);
1935 break;
1936
1937 case SpvOpConstantSampler:
1938 vtn_fail("OpConstantSampler requires Kernel Capability");
1939 break;
1940
1941 default:
1942 vtn_fail("Unhandled opcode");
1943 }
1944
1945 /* Now that we have the value, update the workgroup size if needed */
1946 vtn_foreach_decoration(b, val, handle_workgroup_size_decoration_cb, NULL);
1947 }
1948
1949 struct vtn_ssa_value *
1950 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1951 {
1952 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1953 val->type = type;
1954
1955 if (!glsl_type_is_vector_or_scalar(type)) {
1956 unsigned elems = glsl_get_length(type);
1957 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1958 for (unsigned i = 0; i < elems; i++) {
1959 const struct glsl_type *child_type;
1960
1961 switch (glsl_get_base_type(type)) {
1962 case GLSL_TYPE_INT:
1963 case GLSL_TYPE_UINT:
1964 case GLSL_TYPE_INT16:
1965 case GLSL_TYPE_UINT16:
1966 case GLSL_TYPE_UINT8:
1967 case GLSL_TYPE_INT8:
1968 case GLSL_TYPE_INT64:
1969 case GLSL_TYPE_UINT64:
1970 case GLSL_TYPE_BOOL:
1971 case GLSL_TYPE_FLOAT:
1972 case GLSL_TYPE_FLOAT16:
1973 case GLSL_TYPE_DOUBLE:
1974 child_type = glsl_get_column_type(type);
1975 break;
1976 case GLSL_TYPE_ARRAY:
1977 child_type = glsl_get_array_element(type);
1978 break;
1979 case GLSL_TYPE_STRUCT:
1980 child_type = glsl_get_struct_field(type, i);
1981 break;
1982 default:
1983 vtn_fail("unkown base type");
1984 }
1985
1986 val->elems[i] = vtn_create_ssa_value(b, child_type);
1987 }
1988 }
1989
1990 return val;
1991 }
1992
1993 static nir_tex_src
1994 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1995 {
1996 nir_tex_src src;
1997 src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
1998 src.src_type = type;
1999 return src;
2000 }
2001
2002 static void
2003 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
2004 const uint32_t *w, unsigned count)
2005 {
2006 if (opcode == SpvOpSampledImage) {
2007 struct vtn_value *val =
2008 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
2009 val->sampled_image = ralloc(b, struct vtn_sampled_image);
2010 val->sampled_image->type =
2011 vtn_value(b, w[1], vtn_value_type_type)->type;
2012 val->sampled_image->image =
2013 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2014 val->sampled_image->sampler =
2015 vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
2016 return;
2017 } else if (opcode == SpvOpImage) {
2018 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
2019 struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
2020 if (src_val->value_type == vtn_value_type_sampled_image) {
2021 val->pointer = src_val->sampled_image->image;
2022 } else {
2023 vtn_assert(src_val->value_type == vtn_value_type_pointer);
2024 val->pointer = src_val->pointer;
2025 }
2026 return;
2027 }
2028
2029 struct vtn_type *ret_type = vtn_value(b, w[1], vtn_value_type_type)->type;
2030 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2031
2032 struct vtn_sampled_image sampled;
2033 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
2034 if (sampled_val->value_type == vtn_value_type_sampled_image) {
2035 sampled = *sampled_val->sampled_image;
2036 } else {
2037 vtn_assert(sampled_val->value_type == vtn_value_type_pointer);
2038 sampled.type = sampled_val->pointer->type;
2039 sampled.image = NULL;
2040 sampled.sampler = sampled_val->pointer;
2041 }
2042
2043 const struct glsl_type *image_type = sampled.type->type;
2044 const enum glsl_sampler_dim sampler_dim = glsl_get_sampler_dim(image_type);
2045 const bool is_array = glsl_sampler_type_is_array(image_type);
2046
2047 /* Figure out the base texture operation */
2048 nir_texop texop;
2049 switch (opcode) {
2050 case SpvOpImageSampleImplicitLod:
2051 case SpvOpImageSampleDrefImplicitLod:
2052 case SpvOpImageSampleProjImplicitLod:
2053 case SpvOpImageSampleProjDrefImplicitLod:
2054 texop = nir_texop_tex;
2055 break;
2056
2057 case SpvOpImageSampleExplicitLod:
2058 case SpvOpImageSampleDrefExplicitLod:
2059 case SpvOpImageSampleProjExplicitLod:
2060 case SpvOpImageSampleProjDrefExplicitLod:
2061 texop = nir_texop_txl;
2062 break;
2063
2064 case SpvOpImageFetch:
2065 if (glsl_get_sampler_dim(image_type) == GLSL_SAMPLER_DIM_MS) {
2066 texop = nir_texop_txf_ms;
2067 } else {
2068 texop = nir_texop_txf;
2069 }
2070 break;
2071
2072 case SpvOpImageGather:
2073 case SpvOpImageDrefGather:
2074 texop = nir_texop_tg4;
2075 break;
2076
2077 case SpvOpImageQuerySizeLod:
2078 case SpvOpImageQuerySize:
2079 texop = nir_texop_txs;
2080 break;
2081
2082 case SpvOpImageQueryLod:
2083 texop = nir_texop_lod;
2084 break;
2085
2086 case SpvOpImageQueryLevels:
2087 texop = nir_texop_query_levels;
2088 break;
2089
2090 case SpvOpImageQuerySamples:
2091 texop = nir_texop_texture_samples;
2092 break;
2093
2094 default:
2095 vtn_fail("Unhandled opcode");
2096 }
2097
2098 nir_tex_src srcs[10]; /* 10 should be enough */
2099 nir_tex_src *p = srcs;
2100
2101 nir_deref_instr *sampler = vtn_pointer_to_deref(b, sampled.sampler);
2102 nir_deref_instr *texture =
2103 sampled.image ? vtn_pointer_to_deref(b, sampled.image) : sampler;
2104
2105 p->src = nir_src_for_ssa(&texture->dest.ssa);
2106 p->src_type = nir_tex_src_texture_deref;
2107 p++;
2108
2109 switch (texop) {
2110 case nir_texop_tex:
2111 case nir_texop_txb:
2112 case nir_texop_txl:
2113 case nir_texop_txd:
2114 case nir_texop_tg4:
2115 /* These operations require a sampler */
2116 p->src = nir_src_for_ssa(&sampler->dest.ssa);
2117 p->src_type = nir_tex_src_sampler_deref;
2118 p++;
2119 break;
2120 case nir_texop_txf:
2121 case nir_texop_txf_ms:
2122 case nir_texop_txs:
2123 case nir_texop_lod:
2124 case nir_texop_query_levels:
2125 case nir_texop_texture_samples:
2126 case nir_texop_samples_identical:
2127 /* These don't */
2128 break;
2129 case nir_texop_txf_ms_mcs:
2130 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2131 }
2132
2133 unsigned idx = 4;
2134
2135 struct nir_ssa_def *coord;
2136 unsigned coord_components;
2137 switch (opcode) {
2138 case SpvOpImageSampleImplicitLod:
2139 case SpvOpImageSampleExplicitLod:
2140 case SpvOpImageSampleDrefImplicitLod:
2141 case SpvOpImageSampleDrefExplicitLod:
2142 case SpvOpImageSampleProjImplicitLod:
2143 case SpvOpImageSampleProjExplicitLod:
2144 case SpvOpImageSampleProjDrefImplicitLod:
2145 case SpvOpImageSampleProjDrefExplicitLod:
2146 case SpvOpImageFetch:
2147 case SpvOpImageGather:
2148 case SpvOpImageDrefGather:
2149 case SpvOpImageQueryLod: {
2150 /* All these types have the coordinate as their first real argument */
2151 switch (sampler_dim) {
2152 case GLSL_SAMPLER_DIM_1D:
2153 case GLSL_SAMPLER_DIM_BUF:
2154 coord_components = 1;
2155 break;
2156 case GLSL_SAMPLER_DIM_2D:
2157 case GLSL_SAMPLER_DIM_RECT:
2158 case GLSL_SAMPLER_DIM_MS:
2159 coord_components = 2;
2160 break;
2161 case GLSL_SAMPLER_DIM_3D:
2162 case GLSL_SAMPLER_DIM_CUBE:
2163 coord_components = 3;
2164 break;
2165 default:
2166 vtn_fail("Invalid sampler type");
2167 }
2168
2169 if (is_array && texop != nir_texop_lod)
2170 coord_components++;
2171
2172 coord = vtn_ssa_value(b, w[idx++])->def;
2173 p->src = nir_src_for_ssa(nir_channels(&b->nb, coord,
2174 (1 << coord_components) - 1));
2175 p->src_type = nir_tex_src_coord;
2176 p++;
2177 break;
2178 }
2179
2180 default:
2181 coord = NULL;
2182 coord_components = 0;
2183 break;
2184 }
2185
2186 switch (opcode) {
2187 case SpvOpImageSampleProjImplicitLod:
2188 case SpvOpImageSampleProjExplicitLod:
2189 case SpvOpImageSampleProjDrefImplicitLod:
2190 case SpvOpImageSampleProjDrefExplicitLod:
2191 /* These have the projector as the last coordinate component */
2192 p->src = nir_src_for_ssa(nir_channel(&b->nb, coord, coord_components));
2193 p->src_type = nir_tex_src_projector;
2194 p++;
2195 break;
2196
2197 default:
2198 break;
2199 }
2200
2201 bool is_shadow = false;
2202 unsigned gather_component = 0;
2203 switch (opcode) {
2204 case SpvOpImageSampleDrefImplicitLod:
2205 case SpvOpImageSampleDrefExplicitLod:
2206 case SpvOpImageSampleProjDrefImplicitLod:
2207 case SpvOpImageSampleProjDrefExplicitLod:
2208 case SpvOpImageDrefGather:
2209 /* These all have an explicit depth value as their next source */
2210 is_shadow = true;
2211 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparator);
2212 break;
2213
2214 case SpvOpImageGather:
2215 /* This has a component as its next source */
2216 gather_component =
2217 vtn_value(b, w[idx++], vtn_value_type_constant)->constant->values[0].u32[0];
2218 break;
2219
2220 default:
2221 break;
2222 }
2223
2224 /* For OpImageQuerySizeLod, we always have an LOD */
2225 if (opcode == SpvOpImageQuerySizeLod)
2226 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2227
2228 /* Now we need to handle some number of optional arguments */
2229 const struct vtn_ssa_value *gather_offsets = NULL;
2230 if (idx < count) {
2231 uint32_t operands = w[idx++];
2232
2233 if (operands & SpvImageOperandsBiasMask) {
2234 vtn_assert(texop == nir_texop_tex);
2235 texop = nir_texop_txb;
2236 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
2237 }
2238
2239 if (operands & SpvImageOperandsLodMask) {
2240 vtn_assert(texop == nir_texop_txl || texop == nir_texop_txf ||
2241 texop == nir_texop_txs);
2242 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2243 }
2244
2245 if (operands & SpvImageOperandsGradMask) {
2246 vtn_assert(texop == nir_texop_txl);
2247 texop = nir_texop_txd;
2248 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
2249 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
2250 }
2251
2252 if (operands & SpvImageOperandsOffsetMask ||
2253 operands & SpvImageOperandsConstOffsetMask)
2254 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
2255
2256 if (operands & SpvImageOperandsConstOffsetsMask) {
2257 nir_tex_src none = {0};
2258 gather_offsets = vtn_ssa_value(b, w[idx++]);
2259 (*p++) = none;
2260 }
2261
2262 if (operands & SpvImageOperandsSampleMask) {
2263 vtn_assert(texop == nir_texop_txf_ms);
2264 texop = nir_texop_txf_ms;
2265 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
2266 }
2267
2268 if (operands & SpvImageOperandsMinLodMask) {
2269 vtn_assert(texop == nir_texop_tex ||
2270 texop == nir_texop_txb ||
2271 texop == nir_texop_txd);
2272 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_min_lod);
2273 }
2274 }
2275 /* We should have now consumed exactly all of the arguments */
2276 vtn_assert(idx == count);
2277
2278 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
2279 instr->op = texop;
2280
2281 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
2282
2283 instr->coord_components = coord_components;
2284 instr->sampler_dim = sampler_dim;
2285 instr->is_array = is_array;
2286 instr->is_shadow = is_shadow;
2287 instr->is_new_style_shadow =
2288 is_shadow && glsl_get_components(ret_type->type) == 1;
2289 instr->component = gather_component;
2290
2291 switch (glsl_get_sampler_result_type(image_type)) {
2292 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
2293 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
2294 case GLSL_TYPE_UINT: instr->dest_type = nir_type_uint; break;
2295 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
2296 default:
2297 vtn_fail("Invalid base type for sampler result");
2298 }
2299
2300 nir_ssa_dest_init(&instr->instr, &instr->dest,
2301 nir_tex_instr_dest_size(instr), 32, NULL);
2302
2303 vtn_assert(glsl_get_vector_elements(ret_type->type) ==
2304 nir_tex_instr_dest_size(instr));
2305
2306 nir_ssa_def *def;
2307 nir_instr *instruction;
2308 if (gather_offsets) {
2309 vtn_assert(glsl_get_base_type(gather_offsets->type) == GLSL_TYPE_ARRAY);
2310 vtn_assert(glsl_get_length(gather_offsets->type) == 4);
2311 nir_tex_instr *instrs[4] = {instr, NULL, NULL, NULL};
2312
2313 /* Copy the current instruction 4x */
2314 for (uint32_t i = 1; i < 4; i++) {
2315 instrs[i] = nir_tex_instr_create(b->shader, instr->num_srcs);
2316 instrs[i]->op = instr->op;
2317 instrs[i]->coord_components = instr->coord_components;
2318 instrs[i]->sampler_dim = instr->sampler_dim;
2319 instrs[i]->is_array = instr->is_array;
2320 instrs[i]->is_shadow = instr->is_shadow;
2321 instrs[i]->is_new_style_shadow = instr->is_new_style_shadow;
2322 instrs[i]->component = instr->component;
2323 instrs[i]->dest_type = instr->dest_type;
2324
2325 memcpy(instrs[i]->src, srcs, instr->num_srcs * sizeof(*instr->src));
2326
2327 nir_ssa_dest_init(&instrs[i]->instr, &instrs[i]->dest,
2328 nir_tex_instr_dest_size(instr), 32, NULL);
2329 }
2330
2331 /* Fill in the last argument with the offset from the passed in offsets
2332 * and insert the instruction into the stream.
2333 */
2334 for (uint32_t i = 0; i < 4; i++) {
2335 nir_tex_src src;
2336 src.src = nir_src_for_ssa(gather_offsets->elems[i]->def);
2337 src.src_type = nir_tex_src_offset;
2338 instrs[i]->src[instrs[i]->num_srcs - 1] = src;
2339 nir_builder_instr_insert(&b->nb, &instrs[i]->instr);
2340 }
2341
2342 /* Combine the results of the 4 instructions by taking their .w
2343 * components
2344 */
2345 nir_alu_instr *vec4 = nir_alu_instr_create(b->shader, nir_op_vec4);
2346 nir_ssa_dest_init(&vec4->instr, &vec4->dest.dest, 4, 32, NULL);
2347 vec4->dest.write_mask = 0xf;
2348 for (uint32_t i = 0; i < 4; i++) {
2349 vec4->src[i].src = nir_src_for_ssa(&instrs[i]->dest.ssa);
2350 vec4->src[i].swizzle[0] = 3;
2351 }
2352 def = &vec4->dest.dest.ssa;
2353 instruction = &vec4->instr;
2354 } else {
2355 def = &instr->dest.ssa;
2356 instruction = &instr->instr;
2357 }
2358
2359 val->ssa = vtn_create_ssa_value(b, ret_type->type);
2360 val->ssa->def = def;
2361
2362 nir_builder_instr_insert(&b->nb, instruction);
2363 }
2364
2365 static void
2366 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
2367 const uint32_t *w, nir_src *src)
2368 {
2369 switch (opcode) {
2370 case SpvOpAtomicIIncrement:
2371 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
2372 break;
2373
2374 case SpvOpAtomicIDecrement:
2375 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
2376 break;
2377
2378 case SpvOpAtomicISub:
2379 src[0] =
2380 nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
2381 break;
2382
2383 case SpvOpAtomicCompareExchange:
2384 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[8])->def);
2385 src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
2386 break;
2387
2388 case SpvOpAtomicExchange:
2389 case SpvOpAtomicIAdd:
2390 case SpvOpAtomicSMin:
2391 case SpvOpAtomicUMin:
2392 case SpvOpAtomicSMax:
2393 case SpvOpAtomicUMax:
2394 case SpvOpAtomicAnd:
2395 case SpvOpAtomicOr:
2396 case SpvOpAtomicXor:
2397 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
2398 break;
2399
2400 default:
2401 vtn_fail("Invalid SPIR-V atomic");
2402 }
2403 }
2404
2405 static nir_ssa_def *
2406 get_image_coord(struct vtn_builder *b, uint32_t value)
2407 {
2408 struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
2409
2410 /* The image_load_store intrinsics assume a 4-dim coordinate */
2411 unsigned dim = glsl_get_vector_elements(coord->type);
2412 unsigned swizzle[4];
2413 for (unsigned i = 0; i < 4; i++)
2414 swizzle[i] = MIN2(i, dim - 1);
2415
2416 return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
2417 }
2418
2419 static nir_ssa_def *
2420 expand_to_vec4(nir_builder *b, nir_ssa_def *value)
2421 {
2422 if (value->num_components == 4)
2423 return value;
2424
2425 unsigned swiz[4];
2426 for (unsigned i = 0; i < 4; i++)
2427 swiz[i] = i < value->num_components ? i : 0;
2428 return nir_swizzle(b, value, swiz, 4, false);
2429 }
2430
2431 static void
2432 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
2433 const uint32_t *w, unsigned count)
2434 {
2435 /* Just get this one out of the way */
2436 if (opcode == SpvOpImageTexelPointer) {
2437 struct vtn_value *val =
2438 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
2439 val->image = ralloc(b, struct vtn_image_pointer);
2440
2441 val->image->image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2442 val->image->coord = get_image_coord(b, w[4]);
2443 val->image->sample = vtn_ssa_value(b, w[5])->def;
2444 return;
2445 }
2446
2447 struct vtn_image_pointer image;
2448
2449 switch (opcode) {
2450 case SpvOpAtomicExchange:
2451 case SpvOpAtomicCompareExchange:
2452 case SpvOpAtomicCompareExchangeWeak:
2453 case SpvOpAtomicIIncrement:
2454 case SpvOpAtomicIDecrement:
2455 case SpvOpAtomicIAdd:
2456 case SpvOpAtomicISub:
2457 case SpvOpAtomicLoad:
2458 case SpvOpAtomicSMin:
2459 case SpvOpAtomicUMin:
2460 case SpvOpAtomicSMax:
2461 case SpvOpAtomicUMax:
2462 case SpvOpAtomicAnd:
2463 case SpvOpAtomicOr:
2464 case SpvOpAtomicXor:
2465 image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
2466 break;
2467
2468 case SpvOpAtomicStore:
2469 image = *vtn_value(b, w[1], vtn_value_type_image_pointer)->image;
2470 break;
2471
2472 case SpvOpImageQuerySize:
2473 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2474 image.coord = NULL;
2475 image.sample = NULL;
2476 break;
2477
2478 case SpvOpImageRead:
2479 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2480 image.coord = get_image_coord(b, w[4]);
2481
2482 if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
2483 vtn_assert(w[5] == SpvImageOperandsSampleMask);
2484 image.sample = vtn_ssa_value(b, w[6])->def;
2485 } else {
2486 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2487 }
2488 break;
2489
2490 case SpvOpImageWrite:
2491 image.image = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2492 image.coord = get_image_coord(b, w[2]);
2493
2494 /* texel = w[3] */
2495
2496 if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
2497 vtn_assert(w[4] == SpvImageOperandsSampleMask);
2498 image.sample = vtn_ssa_value(b, w[5])->def;
2499 } else {
2500 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2501 }
2502 break;
2503
2504 default:
2505 vtn_fail("Invalid image opcode");
2506 }
2507
2508 nir_intrinsic_op op;
2509 switch (opcode) {
2510 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2511 OP(ImageQuerySize, size)
2512 OP(ImageRead, load)
2513 OP(ImageWrite, store)
2514 OP(AtomicLoad, load)
2515 OP(AtomicStore, store)
2516 OP(AtomicExchange, atomic_exchange)
2517 OP(AtomicCompareExchange, atomic_comp_swap)
2518 OP(AtomicIIncrement, atomic_add)
2519 OP(AtomicIDecrement, atomic_add)
2520 OP(AtomicIAdd, atomic_add)
2521 OP(AtomicISub, atomic_add)
2522 OP(AtomicSMin, atomic_min)
2523 OP(AtomicUMin, atomic_min)
2524 OP(AtomicSMax, atomic_max)
2525 OP(AtomicUMax, atomic_max)
2526 OP(AtomicAnd, atomic_and)
2527 OP(AtomicOr, atomic_or)
2528 OP(AtomicXor, atomic_xor)
2529 #undef OP
2530 default:
2531 vtn_fail("Invalid image opcode");
2532 }
2533
2534 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
2535
2536 nir_deref_instr *image_deref = vtn_pointer_to_deref(b, image.image);
2537 intrin->src[0] = nir_src_for_ssa(&image_deref->dest.ssa);
2538
2539 /* ImageQuerySize doesn't take any extra parameters */
2540 if (opcode != SpvOpImageQuerySize) {
2541 /* The image coordinate is always 4 components but we may not have that
2542 * many. Swizzle to compensate.
2543 */
2544 intrin->src[1] = nir_src_for_ssa(expand_to_vec4(&b->nb, image.coord));
2545 intrin->src[2] = nir_src_for_ssa(image.sample);
2546 }
2547
2548 switch (opcode) {
2549 case SpvOpAtomicLoad:
2550 case SpvOpImageQuerySize:
2551 case SpvOpImageRead:
2552 break;
2553 case SpvOpAtomicStore:
2554 case SpvOpImageWrite: {
2555 const uint32_t value_id = opcode == SpvOpAtomicStore ? w[4] : w[3];
2556 nir_ssa_def *value = vtn_ssa_value(b, value_id)->def;
2557 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2558 assert(op == nir_intrinsic_image_deref_store);
2559 intrin->num_components = 4;
2560 intrin->src[3] = nir_src_for_ssa(expand_to_vec4(&b->nb, value));
2561 break;
2562 }
2563
2564 case SpvOpAtomicCompareExchange:
2565 case SpvOpAtomicIIncrement:
2566 case SpvOpAtomicIDecrement:
2567 case SpvOpAtomicExchange:
2568 case SpvOpAtomicIAdd:
2569 case SpvOpAtomicISub:
2570 case SpvOpAtomicSMin:
2571 case SpvOpAtomicUMin:
2572 case SpvOpAtomicSMax:
2573 case SpvOpAtomicUMax:
2574 case SpvOpAtomicAnd:
2575 case SpvOpAtomicOr:
2576 case SpvOpAtomicXor:
2577 fill_common_atomic_sources(b, opcode, w, &intrin->src[3]);
2578 break;
2579
2580 default:
2581 vtn_fail("Invalid image opcode");
2582 }
2583
2584 if (opcode != SpvOpImageWrite && opcode != SpvOpAtomicStore) {
2585 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2586 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2587
2588 unsigned dest_components = glsl_get_vector_elements(type->type);
2589 intrin->num_components = nir_intrinsic_infos[op].dest_components;
2590 if (intrin->num_components == 0)
2591 intrin->num_components = dest_components;
2592
2593 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
2594 intrin->num_components, 32, NULL);
2595
2596 nir_builder_instr_insert(&b->nb, &intrin->instr);
2597
2598 nir_ssa_def *result = &intrin->dest.ssa;
2599 if (intrin->num_components != dest_components)
2600 result = nir_channels(&b->nb, result, (1 << dest_components) - 1);
2601
2602 val->ssa = vtn_create_ssa_value(b, type->type);
2603 val->ssa->def = result;
2604 } else {
2605 nir_builder_instr_insert(&b->nb, &intrin->instr);
2606 }
2607 }
2608
2609 static nir_intrinsic_op
2610 get_ssbo_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2611 {
2612 switch (opcode) {
2613 case SpvOpAtomicLoad: return nir_intrinsic_load_ssbo;
2614 case SpvOpAtomicStore: return nir_intrinsic_store_ssbo;
2615 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2616 OP(AtomicExchange, atomic_exchange)
2617 OP(AtomicCompareExchange, atomic_comp_swap)
2618 OP(AtomicIIncrement, atomic_add)
2619 OP(AtomicIDecrement, atomic_add)
2620 OP(AtomicIAdd, atomic_add)
2621 OP(AtomicISub, atomic_add)
2622 OP(AtomicSMin, atomic_imin)
2623 OP(AtomicUMin, atomic_umin)
2624 OP(AtomicSMax, atomic_imax)
2625 OP(AtomicUMax, atomic_umax)
2626 OP(AtomicAnd, atomic_and)
2627 OP(AtomicOr, atomic_or)
2628 OP(AtomicXor, atomic_xor)
2629 #undef OP
2630 default:
2631 vtn_fail("Invalid SSBO atomic");
2632 }
2633 }
2634
2635 static nir_intrinsic_op
2636 get_uniform_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2637 {
2638 switch (opcode) {
2639 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2640 OP(AtomicLoad, read_deref)
2641 OP(AtomicExchange, exchange)
2642 OP(AtomicCompareExchange, comp_swap)
2643 OP(AtomicIIncrement, inc_deref)
2644 OP(AtomicIDecrement, post_dec_deref)
2645 OP(AtomicIAdd, add_deref)
2646 OP(AtomicISub, add_deref)
2647 OP(AtomicUMin, min_deref)
2648 OP(AtomicUMax, max_deref)
2649 OP(AtomicAnd, and_deref)
2650 OP(AtomicOr, or_deref)
2651 OP(AtomicXor, xor_deref)
2652 #undef OP
2653 default:
2654 /* We left the following out: AtomicStore, AtomicSMin and
2655 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2656 * moment Atomic Counter support is needed for ARB_spirv support, so is
2657 * only need to support GLSL Atomic Counters that are uints and don't
2658 * allow direct storage.
2659 */
2660 unreachable("Invalid uniform atomic");
2661 }
2662 }
2663
2664 static nir_intrinsic_op
2665 get_shared_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2666 {
2667 switch (opcode) {
2668 case SpvOpAtomicLoad: return nir_intrinsic_load_shared;
2669 case SpvOpAtomicStore: return nir_intrinsic_store_shared;
2670 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2671 OP(AtomicExchange, atomic_exchange)
2672 OP(AtomicCompareExchange, atomic_comp_swap)
2673 OP(AtomicIIncrement, atomic_add)
2674 OP(AtomicIDecrement, atomic_add)
2675 OP(AtomicIAdd, atomic_add)
2676 OP(AtomicISub, atomic_add)
2677 OP(AtomicSMin, atomic_imin)
2678 OP(AtomicUMin, atomic_umin)
2679 OP(AtomicSMax, atomic_imax)
2680 OP(AtomicUMax, atomic_umax)
2681 OP(AtomicAnd, atomic_and)
2682 OP(AtomicOr, atomic_or)
2683 OP(AtomicXor, atomic_xor)
2684 #undef OP
2685 default:
2686 vtn_fail("Invalid shared atomic");
2687 }
2688 }
2689
2690 static nir_intrinsic_op
2691 get_deref_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2692 {
2693 switch (opcode) {
2694 case SpvOpAtomicLoad: return nir_intrinsic_load_deref;
2695 case SpvOpAtomicStore: return nir_intrinsic_store_deref;
2696 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2697 OP(AtomicExchange, atomic_exchange)
2698 OP(AtomicCompareExchange, atomic_comp_swap)
2699 OP(AtomicIIncrement, atomic_add)
2700 OP(AtomicIDecrement, atomic_add)
2701 OP(AtomicIAdd, atomic_add)
2702 OP(AtomicISub, atomic_add)
2703 OP(AtomicSMin, atomic_imin)
2704 OP(AtomicUMin, atomic_umin)
2705 OP(AtomicSMax, atomic_imax)
2706 OP(AtomicUMax, atomic_umax)
2707 OP(AtomicAnd, atomic_and)
2708 OP(AtomicOr, atomic_or)
2709 OP(AtomicXor, atomic_xor)
2710 #undef OP
2711 default:
2712 vtn_fail("Invalid shared atomic");
2713 }
2714 }
2715
2716 /*
2717 * Handles shared atomics, ssbo atomics and atomic counters.
2718 */
2719 static void
2720 vtn_handle_atomics(struct vtn_builder *b, SpvOp opcode,
2721 const uint32_t *w, unsigned count)
2722 {
2723 struct vtn_pointer *ptr;
2724 nir_intrinsic_instr *atomic;
2725
2726 switch (opcode) {
2727 case SpvOpAtomicLoad:
2728 case SpvOpAtomicExchange:
2729 case SpvOpAtomicCompareExchange:
2730 case SpvOpAtomicCompareExchangeWeak:
2731 case SpvOpAtomicIIncrement:
2732 case SpvOpAtomicIDecrement:
2733 case SpvOpAtomicIAdd:
2734 case SpvOpAtomicISub:
2735 case SpvOpAtomicSMin:
2736 case SpvOpAtomicUMin:
2737 case SpvOpAtomicSMax:
2738 case SpvOpAtomicUMax:
2739 case SpvOpAtomicAnd:
2740 case SpvOpAtomicOr:
2741 case SpvOpAtomicXor:
2742 ptr = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2743 break;
2744
2745 case SpvOpAtomicStore:
2746 ptr = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2747 break;
2748
2749 default:
2750 vtn_fail("Invalid SPIR-V atomic");
2751 }
2752
2753 /*
2754 SpvScope scope = w[4];
2755 SpvMemorySemanticsMask semantics = w[5];
2756 */
2757
2758 /* uniform as "atomic counter uniform" */
2759 if (ptr->mode == vtn_variable_mode_uniform) {
2760 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
2761 const struct glsl_type *deref_type = deref->type;
2762 nir_intrinsic_op op = get_uniform_nir_atomic_op(b, opcode);
2763 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2764 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
2765
2766 /* SSBO needs to initialize index/offset. In this case we don't need to,
2767 * as that info is already stored on the ptr->var->var nir_variable (see
2768 * vtn_create_variable)
2769 */
2770
2771 switch (opcode) {
2772 case SpvOpAtomicLoad:
2773 atomic->num_components = glsl_get_vector_elements(deref_type);
2774 break;
2775
2776 case SpvOpAtomicStore:
2777 atomic->num_components = glsl_get_vector_elements(deref_type);
2778 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2779 break;
2780
2781 case SpvOpAtomicExchange:
2782 case SpvOpAtomicCompareExchange:
2783 case SpvOpAtomicCompareExchangeWeak:
2784 case SpvOpAtomicIIncrement:
2785 case SpvOpAtomicIDecrement:
2786 case SpvOpAtomicIAdd:
2787 case SpvOpAtomicISub:
2788 case SpvOpAtomicSMin:
2789 case SpvOpAtomicUMin:
2790 case SpvOpAtomicSMax:
2791 case SpvOpAtomicUMax:
2792 case SpvOpAtomicAnd:
2793 case SpvOpAtomicOr:
2794 case SpvOpAtomicXor:
2795 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2796 * atomic counter uniforms doesn't have sources
2797 */
2798 break;
2799
2800 default:
2801 unreachable("Invalid SPIR-V atomic");
2802
2803 }
2804 } else if (vtn_pointer_uses_ssa_offset(b, ptr)) {
2805 nir_ssa_def *offset, *index;
2806 offset = vtn_pointer_to_offset(b, ptr, &index);
2807
2808 nir_intrinsic_op op;
2809 if (ptr->mode == vtn_variable_mode_ssbo) {
2810 op = get_ssbo_nir_atomic_op(b, opcode);
2811 } else {
2812 vtn_assert(ptr->mode == vtn_variable_mode_workgroup &&
2813 b->options->lower_workgroup_access_to_offsets);
2814 op = get_shared_nir_atomic_op(b, opcode);
2815 }
2816
2817 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2818
2819 int src = 0;
2820 switch (opcode) {
2821 case SpvOpAtomicLoad:
2822 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2823 nir_intrinsic_set_align(atomic, 4, 0);
2824 if (ptr->mode == vtn_variable_mode_ssbo)
2825 atomic->src[src++] = nir_src_for_ssa(index);
2826 atomic->src[src++] = nir_src_for_ssa(offset);
2827 break;
2828
2829 case SpvOpAtomicStore:
2830 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2831 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2832 nir_intrinsic_set_align(atomic, 4, 0);
2833 atomic->src[src++] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2834 if (ptr->mode == vtn_variable_mode_ssbo)
2835 atomic->src[src++] = nir_src_for_ssa(index);
2836 atomic->src[src++] = nir_src_for_ssa(offset);
2837 break;
2838
2839 case SpvOpAtomicExchange:
2840 case SpvOpAtomicCompareExchange:
2841 case SpvOpAtomicCompareExchangeWeak:
2842 case SpvOpAtomicIIncrement:
2843 case SpvOpAtomicIDecrement:
2844 case SpvOpAtomicIAdd:
2845 case SpvOpAtomicISub:
2846 case SpvOpAtomicSMin:
2847 case SpvOpAtomicUMin:
2848 case SpvOpAtomicSMax:
2849 case SpvOpAtomicUMax:
2850 case SpvOpAtomicAnd:
2851 case SpvOpAtomicOr:
2852 case SpvOpAtomicXor:
2853 if (ptr->mode == vtn_variable_mode_ssbo)
2854 atomic->src[src++] = nir_src_for_ssa(index);
2855 atomic->src[src++] = nir_src_for_ssa(offset);
2856 fill_common_atomic_sources(b, opcode, w, &atomic->src[src]);
2857 break;
2858
2859 default:
2860 vtn_fail("Invalid SPIR-V atomic");
2861 }
2862 } else {
2863 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
2864 const struct glsl_type *deref_type = deref->type;
2865 nir_intrinsic_op op = get_deref_nir_atomic_op(b, opcode);
2866 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2867 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
2868
2869 switch (opcode) {
2870 case SpvOpAtomicLoad:
2871 atomic->num_components = glsl_get_vector_elements(deref_type);
2872 break;
2873
2874 case SpvOpAtomicStore:
2875 atomic->num_components = glsl_get_vector_elements(deref_type);
2876 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2877 atomic->src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2878 break;
2879
2880 case SpvOpAtomicExchange:
2881 case SpvOpAtomicCompareExchange:
2882 case SpvOpAtomicCompareExchangeWeak:
2883 case SpvOpAtomicIIncrement:
2884 case SpvOpAtomicIDecrement:
2885 case SpvOpAtomicIAdd:
2886 case SpvOpAtomicISub:
2887 case SpvOpAtomicSMin:
2888 case SpvOpAtomicUMin:
2889 case SpvOpAtomicSMax:
2890 case SpvOpAtomicUMax:
2891 case SpvOpAtomicAnd:
2892 case SpvOpAtomicOr:
2893 case SpvOpAtomicXor:
2894 fill_common_atomic_sources(b, opcode, w, &atomic->src[1]);
2895 break;
2896
2897 default:
2898 vtn_fail("Invalid SPIR-V atomic");
2899 }
2900 }
2901
2902 if (opcode != SpvOpAtomicStore) {
2903 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2904
2905 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
2906 glsl_get_vector_elements(type->type),
2907 glsl_get_bit_size(type->type), NULL);
2908
2909 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2910 val->ssa = rzalloc(b, struct vtn_ssa_value);
2911 val->ssa->def = &atomic->dest.ssa;
2912 val->ssa->type = type->type;
2913 }
2914
2915 nir_builder_instr_insert(&b->nb, &atomic->instr);
2916 }
2917
2918 static nir_alu_instr *
2919 create_vec(struct vtn_builder *b, unsigned num_components, unsigned bit_size)
2920 {
2921 nir_op op;
2922 switch (num_components) {
2923 case 1: op = nir_op_imov; break;
2924 case 2: op = nir_op_vec2; break;
2925 case 3: op = nir_op_vec3; break;
2926 case 4: op = nir_op_vec4; break;
2927 default: vtn_fail("bad vector size");
2928 }
2929
2930 nir_alu_instr *vec = nir_alu_instr_create(b->shader, op);
2931 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
2932 bit_size, NULL);
2933 vec->dest.write_mask = (1 << num_components) - 1;
2934
2935 return vec;
2936 }
2937
2938 struct vtn_ssa_value *
2939 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
2940 {
2941 if (src->transposed)
2942 return src->transposed;
2943
2944 struct vtn_ssa_value *dest =
2945 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
2946
2947 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
2948 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type),
2949 glsl_get_bit_size(src->type));
2950 if (glsl_type_is_vector_or_scalar(src->type)) {
2951 vec->src[0].src = nir_src_for_ssa(src->def);
2952 vec->src[0].swizzle[0] = i;
2953 } else {
2954 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
2955 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
2956 vec->src[j].swizzle[0] = i;
2957 }
2958 }
2959 nir_builder_instr_insert(&b->nb, &vec->instr);
2960 dest->elems[i]->def = &vec->dest.dest.ssa;
2961 }
2962
2963 dest->transposed = src;
2964
2965 return dest;
2966 }
2967
2968 nir_ssa_def *
2969 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2970 {
2971 return nir_channel(&b->nb, src, index);
2972 }
2973
2974 nir_ssa_def *
2975 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2976 unsigned index)
2977 {
2978 nir_alu_instr *vec = create_vec(b, src->num_components,
2979 src->bit_size);
2980
2981 for (unsigned i = 0; i < src->num_components; i++) {
2982 if (i == index) {
2983 vec->src[i].src = nir_src_for_ssa(insert);
2984 } else {
2985 vec->src[i].src = nir_src_for_ssa(src);
2986 vec->src[i].swizzle[0] = i;
2987 }
2988 }
2989
2990 nir_builder_instr_insert(&b->nb, &vec->instr);
2991
2992 return &vec->dest.dest.ssa;
2993 }
2994
2995 static nir_ssa_def *
2996 nir_ieq_imm(nir_builder *b, nir_ssa_def *x, uint64_t i)
2997 {
2998 return nir_ieq(b, x, nir_imm_intN_t(b, i, x->bit_size));
2999 }
3000
3001 nir_ssa_def *
3002 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
3003 nir_ssa_def *index)
3004 {
3005 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
3006 for (unsigned i = 1; i < src->num_components; i++)
3007 dest = nir_bcsel(&b->nb, nir_ieq_imm(&b->nb, index, i),
3008 vtn_vector_extract(b, src, i), dest);
3009
3010 return dest;
3011 }
3012
3013 nir_ssa_def *
3014 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
3015 nir_ssa_def *insert, nir_ssa_def *index)
3016 {
3017 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
3018 for (unsigned i = 1; i < src->num_components; i++)
3019 dest = nir_bcsel(&b->nb, nir_ieq_imm(&b->nb, index, i),
3020 vtn_vector_insert(b, src, insert, i), dest);
3021
3022 return dest;
3023 }
3024
3025 static nir_ssa_def *
3026 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
3027 nir_ssa_def *src0, nir_ssa_def *src1,
3028 const uint32_t *indices)
3029 {
3030 nir_alu_instr *vec = create_vec(b, num_components, src0->bit_size);
3031
3032 for (unsigned i = 0; i < num_components; i++) {
3033 uint32_t index = indices[i];
3034 if (index == 0xffffffff) {
3035 vec->src[i].src =
3036 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
3037 } else if (index < src0->num_components) {
3038 vec->src[i].src = nir_src_for_ssa(src0);
3039 vec->src[i].swizzle[0] = index;
3040 } else {
3041 vec->src[i].src = nir_src_for_ssa(src1);
3042 vec->src[i].swizzle[0] = index - src0->num_components;
3043 }
3044 }
3045
3046 nir_builder_instr_insert(&b->nb, &vec->instr);
3047
3048 return &vec->dest.dest.ssa;
3049 }
3050
3051 /*
3052 * Concatentates a number of vectors/scalars together to produce a vector
3053 */
3054 static nir_ssa_def *
3055 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
3056 unsigned num_srcs, nir_ssa_def **srcs)
3057 {
3058 nir_alu_instr *vec = create_vec(b, num_components, srcs[0]->bit_size);
3059
3060 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3061 *
3062 * "When constructing a vector, there must be at least two Constituent
3063 * operands."
3064 */
3065 vtn_assert(num_srcs >= 2);
3066
3067 unsigned dest_idx = 0;
3068 for (unsigned i = 0; i < num_srcs; i++) {
3069 nir_ssa_def *src = srcs[i];
3070 vtn_assert(dest_idx + src->num_components <= num_components);
3071 for (unsigned j = 0; j < src->num_components; j++) {
3072 vec->src[dest_idx].src = nir_src_for_ssa(src);
3073 vec->src[dest_idx].swizzle[0] = j;
3074 dest_idx++;
3075 }
3076 }
3077
3078 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3079 *
3080 * "When constructing a vector, the total number of components in all
3081 * the operands must equal the number of components in Result Type."
3082 */
3083 vtn_assert(dest_idx == num_components);
3084
3085 nir_builder_instr_insert(&b->nb, &vec->instr);
3086
3087 return &vec->dest.dest.ssa;
3088 }
3089
3090 static struct vtn_ssa_value *
3091 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
3092 {
3093 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
3094 dest->type = src->type;
3095
3096 if (glsl_type_is_vector_or_scalar(src->type)) {
3097 dest->def = src->def;
3098 } else {
3099 unsigned elems = glsl_get_length(src->type);
3100
3101 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
3102 for (unsigned i = 0; i < elems; i++)
3103 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
3104 }
3105
3106 return dest;
3107 }
3108
3109 static struct vtn_ssa_value *
3110 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
3111 struct vtn_ssa_value *insert, const uint32_t *indices,
3112 unsigned num_indices)
3113 {
3114 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
3115
3116 struct vtn_ssa_value *cur = dest;
3117 unsigned i;
3118 for (i = 0; i < num_indices - 1; i++) {
3119 cur = cur->elems[indices[i]];
3120 }
3121
3122 if (glsl_type_is_vector_or_scalar(cur->type)) {
3123 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3124 * the component granularity. In that case, the last index will be
3125 * the index to insert the scalar into the vector.
3126 */
3127
3128 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
3129 } else {
3130 cur->elems[indices[i]] = insert;
3131 }
3132
3133 return dest;
3134 }
3135
3136 static struct vtn_ssa_value *
3137 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
3138 const uint32_t *indices, unsigned num_indices)
3139 {
3140 struct vtn_ssa_value *cur = src;
3141 for (unsigned i = 0; i < num_indices; i++) {
3142 if (glsl_type_is_vector_or_scalar(cur->type)) {
3143 vtn_assert(i == num_indices - 1);
3144 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3145 * the component granularity. The last index will be the index of the
3146 * vector to extract.
3147 */
3148
3149 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
3150 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
3151 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
3152 return ret;
3153 } else {
3154 cur = cur->elems[indices[i]];
3155 }
3156 }
3157
3158 return cur;
3159 }
3160
3161 static void
3162 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
3163 const uint32_t *w, unsigned count)
3164 {
3165 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
3166 const struct glsl_type *type =
3167 vtn_value(b, w[1], vtn_value_type_type)->type->type;
3168 val->ssa = vtn_create_ssa_value(b, type);
3169
3170 switch (opcode) {
3171 case SpvOpVectorExtractDynamic:
3172 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
3173 vtn_ssa_value(b, w[4])->def);
3174 break;
3175
3176 case SpvOpVectorInsertDynamic:
3177 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
3178 vtn_ssa_value(b, w[4])->def,
3179 vtn_ssa_value(b, w[5])->def);
3180 break;
3181
3182 case SpvOpVectorShuffle:
3183 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
3184 vtn_ssa_value(b, w[3])->def,
3185 vtn_ssa_value(b, w[4])->def,
3186 w + 5);
3187 break;
3188
3189 case SpvOpCompositeConstruct: {
3190 unsigned elems = count - 3;
3191 assume(elems >= 1);
3192 if (glsl_type_is_vector_or_scalar(type)) {
3193 nir_ssa_def *srcs[NIR_MAX_VEC_COMPONENTS];
3194 for (unsigned i = 0; i < elems; i++)
3195 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
3196 val->ssa->def =
3197 vtn_vector_construct(b, glsl_get_vector_elements(type),
3198 elems, srcs);
3199 } else {
3200 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
3201 for (unsigned i = 0; i < elems; i++)
3202 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
3203 }
3204 break;
3205 }
3206 case SpvOpCompositeExtract:
3207 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
3208 w + 4, count - 4);
3209 break;
3210
3211 case SpvOpCompositeInsert:
3212 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
3213 vtn_ssa_value(b, w[3]),
3214 w + 5, count - 5);
3215 break;
3216
3217 case SpvOpCopyObject:
3218 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
3219 break;
3220
3221 default:
3222 vtn_fail("unknown composite operation");
3223 }
3224 }
3225
3226 static void
3227 vtn_emit_barrier(struct vtn_builder *b, nir_intrinsic_op op)
3228 {
3229 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
3230 nir_builder_instr_insert(&b->nb, &intrin->instr);
3231 }
3232
3233 static void
3234 vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
3235 SpvMemorySemanticsMask semantics)
3236 {
3237 static const SpvMemorySemanticsMask all_memory_semantics =
3238 SpvMemorySemanticsUniformMemoryMask |
3239 SpvMemorySemanticsWorkgroupMemoryMask |
3240 SpvMemorySemanticsAtomicCounterMemoryMask |
3241 SpvMemorySemanticsImageMemoryMask;
3242
3243 /* If we're not actually doing a memory barrier, bail */
3244 if (!(semantics & all_memory_semantics))
3245 return;
3246
3247 /* GL and Vulkan don't have these */
3248 vtn_assert(scope != SpvScopeCrossDevice);
3249
3250 if (scope == SpvScopeSubgroup)
3251 return; /* Nothing to do here */
3252
3253 if (scope == SpvScopeWorkgroup) {
3254 vtn_emit_barrier(b, nir_intrinsic_group_memory_barrier);
3255 return;
3256 }
3257
3258 /* There's only two scopes thing left */
3259 vtn_assert(scope == SpvScopeInvocation || scope == SpvScopeDevice);
3260
3261 if ((semantics & all_memory_semantics) == all_memory_semantics) {
3262 vtn_emit_barrier(b, nir_intrinsic_memory_barrier);
3263 return;
3264 }
3265
3266 /* Issue a bunch of more specific barriers */
3267 uint32_t bits = semantics;
3268 while (bits) {
3269 SpvMemorySemanticsMask semantic = 1 << u_bit_scan(&bits);
3270 switch (semantic) {
3271 case SpvMemorySemanticsUniformMemoryMask:
3272 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_buffer);
3273 break;
3274 case SpvMemorySemanticsWorkgroupMemoryMask:
3275 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_shared);
3276 break;
3277 case SpvMemorySemanticsAtomicCounterMemoryMask:
3278 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_atomic_counter);
3279 break;
3280 case SpvMemorySemanticsImageMemoryMask:
3281 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_image);
3282 break;
3283 default:
3284 break;;
3285 }
3286 }
3287 }
3288
3289 static void
3290 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
3291 const uint32_t *w, unsigned count)
3292 {
3293 switch (opcode) {
3294 case SpvOpEmitVertex:
3295 case SpvOpEmitStreamVertex:
3296 case SpvOpEndPrimitive:
3297 case SpvOpEndStreamPrimitive: {
3298 nir_intrinsic_op intrinsic_op;
3299 switch (opcode) {
3300 case SpvOpEmitVertex:
3301 case SpvOpEmitStreamVertex:
3302 intrinsic_op = nir_intrinsic_emit_vertex;
3303 break;
3304 case SpvOpEndPrimitive:
3305 case SpvOpEndStreamPrimitive:
3306 intrinsic_op = nir_intrinsic_end_primitive;
3307 break;
3308 default:
3309 unreachable("Invalid opcode");
3310 }
3311
3312 nir_intrinsic_instr *intrin =
3313 nir_intrinsic_instr_create(b->shader, intrinsic_op);
3314
3315 switch (opcode) {
3316 case SpvOpEmitStreamVertex:
3317 case SpvOpEndStreamPrimitive: {
3318 unsigned stream = vtn_constant_value(b, w[1])->values[0].u32[0];
3319 nir_intrinsic_set_stream_id(intrin, stream);
3320 break;
3321 }
3322
3323 default:
3324 break;
3325 }
3326
3327 nir_builder_instr_insert(&b->nb, &intrin->instr);
3328 break;
3329 }
3330
3331 case SpvOpMemoryBarrier: {
3332 SpvScope scope = vtn_constant_value(b, w[1])->values[0].u32[0];
3333 SpvMemorySemanticsMask semantics =
3334 vtn_constant_value(b, w[2])->values[0].u32[0];
3335 vtn_emit_memory_barrier(b, scope, semantics);
3336 return;
3337 }
3338
3339 case SpvOpControlBarrier: {
3340 SpvScope execution_scope =
3341 vtn_constant_value(b, w[1])->values[0].u32[0];
3342 if (execution_scope == SpvScopeWorkgroup)
3343 vtn_emit_barrier(b, nir_intrinsic_barrier);
3344
3345 SpvScope memory_scope =
3346 vtn_constant_value(b, w[2])->values[0].u32[0];
3347 SpvMemorySemanticsMask memory_semantics =
3348 vtn_constant_value(b, w[3])->values[0].u32[0];
3349 vtn_emit_memory_barrier(b, memory_scope, memory_semantics);
3350 break;
3351 }
3352
3353 default:
3354 unreachable("unknown barrier instruction");
3355 }
3356 }
3357
3358 static unsigned
3359 gl_primitive_from_spv_execution_mode(struct vtn_builder *b,
3360 SpvExecutionMode mode)
3361 {
3362 switch (mode) {
3363 case SpvExecutionModeInputPoints:
3364 case SpvExecutionModeOutputPoints:
3365 return 0; /* GL_POINTS */
3366 case SpvExecutionModeInputLines:
3367 return 1; /* GL_LINES */
3368 case SpvExecutionModeInputLinesAdjacency:
3369 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3370 case SpvExecutionModeTriangles:
3371 return 4; /* GL_TRIANGLES */
3372 case SpvExecutionModeInputTrianglesAdjacency:
3373 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3374 case SpvExecutionModeQuads:
3375 return 7; /* GL_QUADS */
3376 case SpvExecutionModeIsolines:
3377 return 0x8E7A; /* GL_ISOLINES */
3378 case SpvExecutionModeOutputLineStrip:
3379 return 3; /* GL_LINE_STRIP */
3380 case SpvExecutionModeOutputTriangleStrip:
3381 return 5; /* GL_TRIANGLE_STRIP */
3382 default:
3383 vtn_fail("Invalid primitive type");
3384 }
3385 }
3386
3387 static unsigned
3388 vertices_in_from_spv_execution_mode(struct vtn_builder *b,
3389 SpvExecutionMode mode)
3390 {
3391 switch (mode) {
3392 case SpvExecutionModeInputPoints:
3393 return 1;
3394 case SpvExecutionModeInputLines:
3395 return 2;
3396 case SpvExecutionModeInputLinesAdjacency:
3397 return 4;
3398 case SpvExecutionModeTriangles:
3399 return 3;
3400 case SpvExecutionModeInputTrianglesAdjacency:
3401 return 6;
3402 default:
3403 vtn_fail("Invalid GS input mode");
3404 }
3405 }
3406
3407 static gl_shader_stage
3408 stage_for_execution_model(struct vtn_builder *b, SpvExecutionModel model)
3409 {
3410 switch (model) {
3411 case SpvExecutionModelVertex:
3412 return MESA_SHADER_VERTEX;
3413 case SpvExecutionModelTessellationControl:
3414 return MESA_SHADER_TESS_CTRL;
3415 case SpvExecutionModelTessellationEvaluation:
3416 return MESA_SHADER_TESS_EVAL;
3417 case SpvExecutionModelGeometry:
3418 return MESA_SHADER_GEOMETRY;
3419 case SpvExecutionModelFragment:
3420 return MESA_SHADER_FRAGMENT;
3421 case SpvExecutionModelGLCompute:
3422 return MESA_SHADER_COMPUTE;
3423 default:
3424 vtn_fail("Unsupported execution model");
3425 }
3426 }
3427
3428 #define spv_check_supported(name, cap) do { \
3429 if (!(b->options && b->options->caps.name)) \
3430 vtn_warn("Unsupported SPIR-V capability: %s", \
3431 spirv_capability_to_string(cap)); \
3432 } while(0)
3433
3434
3435 void
3436 vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
3437 unsigned count)
3438 {
3439 struct vtn_value *entry_point = &b->values[w[2]];
3440 /* Let this be a name label regardless */
3441 unsigned name_words;
3442 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
3443
3444 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
3445 stage_for_execution_model(b, w[1]) != b->entry_point_stage)
3446 return;
3447
3448 vtn_assert(b->entry_point == NULL);
3449 b->entry_point = entry_point;
3450 }
3451
3452 static bool
3453 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
3454 const uint32_t *w, unsigned count)
3455 {
3456 switch (opcode) {
3457 case SpvOpSource: {
3458 const char *lang;
3459 switch (w[1]) {
3460 default:
3461 case SpvSourceLanguageUnknown: lang = "unknown"; break;
3462 case SpvSourceLanguageESSL: lang = "ESSL"; break;
3463 case SpvSourceLanguageGLSL: lang = "GLSL"; break;
3464 case SpvSourceLanguageOpenCL_C: lang = "OpenCL C"; break;
3465 case SpvSourceLanguageOpenCL_CPP: lang = "OpenCL C++"; break;
3466 case SpvSourceLanguageHLSL: lang = "HLSL"; break;
3467 }
3468
3469 uint32_t version = w[2];
3470
3471 const char *file =
3472 (count > 3) ? vtn_value(b, w[3], vtn_value_type_string)->str : "";
3473
3474 vtn_info("Parsing SPIR-V from %s %u source file %s", lang, version, file);
3475 break;
3476 }
3477
3478 case SpvOpSourceExtension:
3479 case SpvOpSourceContinued:
3480 case SpvOpExtension:
3481 case SpvOpModuleProcessed:
3482 /* Unhandled, but these are for debug so that's ok. */
3483 break;
3484
3485 case SpvOpCapability: {
3486 SpvCapability cap = w[1];
3487 switch (cap) {
3488 case SpvCapabilityMatrix:
3489 case SpvCapabilityShader:
3490 case SpvCapabilityGeometry:
3491 case SpvCapabilityGeometryPointSize:
3492 case SpvCapabilityUniformBufferArrayDynamicIndexing:
3493 case SpvCapabilitySampledImageArrayDynamicIndexing:
3494 case SpvCapabilityStorageBufferArrayDynamicIndexing:
3495 case SpvCapabilityStorageImageArrayDynamicIndexing:
3496 case SpvCapabilityImageRect:
3497 case SpvCapabilitySampledRect:
3498 case SpvCapabilitySampled1D:
3499 case SpvCapabilityImage1D:
3500 case SpvCapabilitySampledCubeArray:
3501 case SpvCapabilityImageCubeArray:
3502 case SpvCapabilitySampledBuffer:
3503 case SpvCapabilityImageBuffer:
3504 case SpvCapabilityImageQuery:
3505 case SpvCapabilityDerivativeControl:
3506 case SpvCapabilityInterpolationFunction:
3507 case SpvCapabilityMultiViewport:
3508 case SpvCapabilitySampleRateShading:
3509 case SpvCapabilityClipDistance:
3510 case SpvCapabilityCullDistance:
3511 case SpvCapabilityInputAttachment:
3512 case SpvCapabilityImageGatherExtended:
3513 case SpvCapabilityStorageImageExtendedFormats:
3514 break;
3515
3516 case SpvCapabilityLinkage:
3517 case SpvCapabilityVector16:
3518 case SpvCapabilityFloat16Buffer:
3519 case SpvCapabilityFloat16:
3520 case SpvCapabilityInt8:
3521 case SpvCapabilitySparseResidency:
3522 vtn_warn("Unsupported SPIR-V capability: %s",
3523 spirv_capability_to_string(cap));
3524 break;
3525
3526 case SpvCapabilityMinLod:
3527 spv_check_supported(min_lod, cap);
3528 break;
3529
3530 case SpvCapabilityAtomicStorage:
3531 spv_check_supported(atomic_storage, cap);
3532 break;
3533
3534 case SpvCapabilityFloat64:
3535 spv_check_supported(float64, cap);
3536 break;
3537 case SpvCapabilityInt64:
3538 spv_check_supported(int64, cap);
3539 break;
3540 case SpvCapabilityInt16:
3541 spv_check_supported(int16, cap);
3542 break;
3543
3544 case SpvCapabilityTransformFeedback:
3545 spv_check_supported(transform_feedback, cap);
3546 break;
3547
3548 case SpvCapabilityGeometryStreams:
3549 spv_check_supported(geometry_streams, cap);
3550 break;
3551
3552 case SpvCapabilityInt64Atomics:
3553 spv_check_supported(int64_atomics, cap);
3554 break;
3555
3556 case SpvCapabilityStorageImageMultisample:
3557 spv_check_supported(storage_image_ms, cap);
3558 break;
3559
3560 case SpvCapabilityAddresses:
3561 case SpvCapabilityKernel:
3562 case SpvCapabilityImageBasic:
3563 case SpvCapabilityImageReadWrite:
3564 case SpvCapabilityImageMipmap:
3565 case SpvCapabilityPipes:
3566 case SpvCapabilityGroups:
3567 case SpvCapabilityDeviceEnqueue:
3568 case SpvCapabilityLiteralSampler:
3569 case SpvCapabilityGenericPointer:
3570 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3571 spirv_capability_to_string(cap));
3572 break;
3573
3574 case SpvCapabilityImageMSArray:
3575 spv_check_supported(image_ms_array, cap);
3576 break;
3577
3578 case SpvCapabilityTessellation:
3579 case SpvCapabilityTessellationPointSize:
3580 spv_check_supported(tessellation, cap);
3581 break;
3582
3583 case SpvCapabilityDrawParameters:
3584 spv_check_supported(draw_parameters, cap);
3585 break;
3586
3587 case SpvCapabilityStorageImageReadWithoutFormat:
3588 spv_check_supported(image_read_without_format, cap);
3589 break;
3590
3591 case SpvCapabilityStorageImageWriteWithoutFormat:
3592 spv_check_supported(image_write_without_format, cap);
3593 break;
3594
3595 case SpvCapabilityDeviceGroup:
3596 spv_check_supported(device_group, cap);
3597 break;
3598
3599 case SpvCapabilityMultiView:
3600 spv_check_supported(multiview, cap);
3601 break;
3602
3603 case SpvCapabilityGroupNonUniform:
3604 spv_check_supported(subgroup_basic, cap);
3605 break;
3606
3607 case SpvCapabilityGroupNonUniformVote:
3608 spv_check_supported(subgroup_vote, cap);
3609 break;
3610
3611 case SpvCapabilitySubgroupBallotKHR:
3612 case SpvCapabilityGroupNonUniformBallot:
3613 spv_check_supported(subgroup_ballot, cap);
3614 break;
3615
3616 case SpvCapabilityGroupNonUniformShuffle:
3617 case SpvCapabilityGroupNonUniformShuffleRelative:
3618 spv_check_supported(subgroup_shuffle, cap);
3619 break;
3620
3621 case SpvCapabilityGroupNonUniformQuad:
3622 spv_check_supported(subgroup_quad, cap);
3623 break;
3624
3625 case SpvCapabilityGroupNonUniformArithmetic:
3626 case SpvCapabilityGroupNonUniformClustered:
3627 spv_check_supported(subgroup_arithmetic, cap);
3628 break;
3629
3630 case SpvCapabilityVariablePointersStorageBuffer:
3631 case SpvCapabilityVariablePointers:
3632 spv_check_supported(variable_pointers, cap);
3633 b->variable_pointers = true;
3634 break;
3635
3636 case SpvCapabilityStorageUniformBufferBlock16:
3637 case SpvCapabilityStorageUniform16:
3638 case SpvCapabilityStoragePushConstant16:
3639 case SpvCapabilityStorageInputOutput16:
3640 spv_check_supported(storage_16bit, cap);
3641 break;
3642
3643 case SpvCapabilityShaderViewportIndexLayerEXT:
3644 spv_check_supported(shader_viewport_index_layer, cap);
3645 break;
3646
3647 case SpvCapabilityStorageBuffer8BitAccess:
3648 case SpvCapabilityUniformAndStorageBuffer8BitAccess:
3649 case SpvCapabilityStoragePushConstant8:
3650 spv_check_supported(storage_8bit, cap);
3651 break;
3652
3653 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT:
3654 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT:
3655 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT:
3656 spv_check_supported(descriptor_array_dynamic_indexing, cap);
3657 break;
3658
3659 case SpvCapabilityRuntimeDescriptorArrayEXT:
3660 spv_check_supported(runtime_descriptor_array, cap);
3661 break;
3662
3663 case SpvCapabilityStencilExportEXT:
3664 spv_check_supported(stencil_export, cap);
3665 break;
3666
3667 case SpvCapabilitySampleMaskPostDepthCoverage:
3668 spv_check_supported(post_depth_coverage, cap);
3669 break;
3670
3671 default:
3672 vtn_fail("Unhandled capability");
3673 }
3674 break;
3675 }
3676
3677 case SpvOpExtInstImport:
3678 vtn_handle_extension(b, opcode, w, count);
3679 break;
3680
3681 case SpvOpMemoryModel:
3682 vtn_assert(w[1] == SpvAddressingModelLogical);
3683 vtn_assert(w[2] == SpvMemoryModelSimple ||
3684 w[2] == SpvMemoryModelGLSL450);
3685 break;
3686
3687 case SpvOpEntryPoint:
3688 vtn_handle_entry_point(b, w, count);
3689 break;
3690
3691 case SpvOpString:
3692 vtn_push_value(b, w[1], vtn_value_type_string)->str =
3693 vtn_string_literal(b, &w[2], count - 2, NULL);
3694 break;
3695
3696 case SpvOpName:
3697 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
3698 break;
3699
3700 case SpvOpMemberName:
3701 /* TODO */
3702 break;
3703
3704 case SpvOpExecutionMode:
3705 case SpvOpDecorationGroup:
3706 case SpvOpDecorate:
3707 case SpvOpMemberDecorate:
3708 case SpvOpGroupDecorate:
3709 case SpvOpGroupMemberDecorate:
3710 case SpvOpDecorateStringGOOGLE:
3711 case SpvOpMemberDecorateStringGOOGLE:
3712 vtn_handle_decoration(b, opcode, w, count);
3713 break;
3714
3715 default:
3716 return false; /* End of preamble */
3717 }
3718
3719 return true;
3720 }
3721
3722 static void
3723 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
3724 const struct vtn_decoration *mode, void *data)
3725 {
3726 vtn_assert(b->entry_point == entry_point);
3727
3728 switch(mode->exec_mode) {
3729 case SpvExecutionModeOriginUpperLeft:
3730 case SpvExecutionModeOriginLowerLeft:
3731 b->origin_upper_left =
3732 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
3733 break;
3734
3735 case SpvExecutionModeEarlyFragmentTests:
3736 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3737 b->shader->info.fs.early_fragment_tests = true;
3738 break;
3739
3740 case SpvExecutionModePostDepthCoverage:
3741 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3742 b->shader->info.fs.post_depth_coverage = true;
3743 break;
3744
3745 case SpvExecutionModeInvocations:
3746 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3747 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
3748 break;
3749
3750 case SpvExecutionModeDepthReplacing:
3751 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3752 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
3753 break;
3754 case SpvExecutionModeDepthGreater:
3755 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3756 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
3757 break;
3758 case SpvExecutionModeDepthLess:
3759 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3760 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
3761 break;
3762 case SpvExecutionModeDepthUnchanged:
3763 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3764 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
3765 break;
3766
3767 case SpvExecutionModeLocalSize:
3768 vtn_assert(b->shader->info.stage == MESA_SHADER_COMPUTE);
3769 b->shader->info.cs.local_size[0] = mode->literals[0];
3770 b->shader->info.cs.local_size[1] = mode->literals[1];
3771 b->shader->info.cs.local_size[2] = mode->literals[2];
3772 break;
3773 case SpvExecutionModeLocalSizeHint:
3774 break; /* Nothing to do with this */
3775
3776 case SpvExecutionModeOutputVertices:
3777 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3778 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3779 b->shader->info.tess.tcs_vertices_out = mode->literals[0];
3780 } else {
3781 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3782 b->shader->info.gs.vertices_out = mode->literals[0];
3783 }
3784 break;
3785
3786 case SpvExecutionModeInputPoints:
3787 case SpvExecutionModeInputLines:
3788 case SpvExecutionModeInputLinesAdjacency:
3789 case SpvExecutionModeTriangles:
3790 case SpvExecutionModeInputTrianglesAdjacency:
3791 case SpvExecutionModeQuads:
3792 case SpvExecutionModeIsolines:
3793 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3794 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3795 b->shader->info.tess.primitive_mode =
3796 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3797 } else {
3798 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3799 b->shader->info.gs.vertices_in =
3800 vertices_in_from_spv_execution_mode(b, mode->exec_mode);
3801 b->shader->info.gs.input_primitive =
3802 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3803 }
3804 break;
3805
3806 case SpvExecutionModeOutputPoints:
3807 case SpvExecutionModeOutputLineStrip:
3808 case SpvExecutionModeOutputTriangleStrip:
3809 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3810 b->shader->info.gs.output_primitive =
3811 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3812 break;
3813
3814 case SpvExecutionModeSpacingEqual:
3815 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3816 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3817 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
3818 break;
3819 case SpvExecutionModeSpacingFractionalEven:
3820 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3821 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3822 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
3823 break;
3824 case SpvExecutionModeSpacingFractionalOdd:
3825 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3826 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3827 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
3828 break;
3829 case SpvExecutionModeVertexOrderCw:
3830 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3831 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3832 b->shader->info.tess.ccw = false;
3833 break;
3834 case SpvExecutionModeVertexOrderCcw:
3835 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3836 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3837 b->shader->info.tess.ccw = true;
3838 break;
3839 case SpvExecutionModePointMode:
3840 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3841 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3842 b->shader->info.tess.point_mode = true;
3843 break;
3844
3845 case SpvExecutionModePixelCenterInteger:
3846 b->pixel_center_integer = true;
3847 break;
3848
3849 case SpvExecutionModeXfb:
3850 b->shader->info.has_transform_feedback_varyings = true;
3851 break;
3852
3853 case SpvExecutionModeVecTypeHint:
3854 case SpvExecutionModeContractionOff:
3855 break; /* OpenCL */
3856
3857 case SpvExecutionModeStencilRefReplacingEXT:
3858 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3859 break;
3860
3861 default:
3862 vtn_fail("Unhandled execution mode");
3863 }
3864 }
3865
3866 static bool
3867 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
3868 const uint32_t *w, unsigned count)
3869 {
3870 vtn_set_instruction_result_type(b, opcode, w, count);
3871
3872 switch (opcode) {
3873 case SpvOpSource:
3874 case SpvOpSourceContinued:
3875 case SpvOpSourceExtension:
3876 case SpvOpExtension:
3877 case SpvOpCapability:
3878 case SpvOpExtInstImport:
3879 case SpvOpMemoryModel:
3880 case SpvOpEntryPoint:
3881 case SpvOpExecutionMode:
3882 case SpvOpString:
3883 case SpvOpName:
3884 case SpvOpMemberName:
3885 case SpvOpDecorationGroup:
3886 case SpvOpDecorate:
3887 case SpvOpMemberDecorate:
3888 case SpvOpGroupDecorate:
3889 case SpvOpGroupMemberDecorate:
3890 case SpvOpDecorateStringGOOGLE:
3891 case SpvOpMemberDecorateStringGOOGLE:
3892 vtn_fail("Invalid opcode types and variables section");
3893 break;
3894
3895 case SpvOpTypeVoid:
3896 case SpvOpTypeBool:
3897 case SpvOpTypeInt:
3898 case SpvOpTypeFloat:
3899 case SpvOpTypeVector:
3900 case SpvOpTypeMatrix:
3901 case SpvOpTypeImage:
3902 case SpvOpTypeSampler:
3903 case SpvOpTypeSampledImage:
3904 case SpvOpTypeArray:
3905 case SpvOpTypeRuntimeArray:
3906 case SpvOpTypeStruct:
3907 case SpvOpTypeOpaque:
3908 case SpvOpTypePointer:
3909 case SpvOpTypeFunction:
3910 case SpvOpTypeEvent:
3911 case SpvOpTypeDeviceEvent:
3912 case SpvOpTypeReserveId:
3913 case SpvOpTypeQueue:
3914 case SpvOpTypePipe:
3915 vtn_handle_type(b, opcode, w, count);
3916 break;
3917
3918 case SpvOpConstantTrue:
3919 case SpvOpConstantFalse:
3920 case SpvOpConstant:
3921 case SpvOpConstantComposite:
3922 case SpvOpConstantSampler:
3923 case SpvOpConstantNull:
3924 case SpvOpSpecConstantTrue:
3925 case SpvOpSpecConstantFalse:
3926 case SpvOpSpecConstant:
3927 case SpvOpSpecConstantComposite:
3928 case SpvOpSpecConstantOp:
3929 vtn_handle_constant(b, opcode, w, count);
3930 break;
3931
3932 case SpvOpUndef:
3933 case SpvOpVariable:
3934 vtn_handle_variables(b, opcode, w, count);
3935 break;
3936
3937 default:
3938 return false; /* End of preamble */
3939 }
3940
3941 return true;
3942 }
3943
3944 static bool
3945 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
3946 const uint32_t *w, unsigned count)
3947 {
3948 switch (opcode) {
3949 case SpvOpLabel:
3950 break;
3951
3952 case SpvOpLoopMerge:
3953 case SpvOpSelectionMerge:
3954 /* This is handled by cfg pre-pass and walk_blocks */
3955 break;
3956
3957 case SpvOpUndef: {
3958 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
3959 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
3960 break;
3961 }
3962
3963 case SpvOpExtInst:
3964 vtn_handle_extension(b, opcode, w, count);
3965 break;
3966
3967 case SpvOpVariable:
3968 case SpvOpLoad:
3969 case SpvOpStore:
3970 case SpvOpCopyMemory:
3971 case SpvOpCopyMemorySized:
3972 case SpvOpAccessChain:
3973 case SpvOpPtrAccessChain:
3974 case SpvOpInBoundsAccessChain:
3975 case SpvOpArrayLength:
3976 vtn_handle_variables(b, opcode, w, count);
3977 break;
3978
3979 case SpvOpFunctionCall:
3980 vtn_handle_function_call(b, opcode, w, count);
3981 break;
3982
3983 case SpvOpSampledImage:
3984 case SpvOpImage:
3985 case SpvOpImageSampleImplicitLod:
3986 case SpvOpImageSampleExplicitLod:
3987 case SpvOpImageSampleDrefImplicitLod:
3988 case SpvOpImageSampleDrefExplicitLod:
3989 case SpvOpImageSampleProjImplicitLod:
3990 case SpvOpImageSampleProjExplicitLod:
3991 case SpvOpImageSampleProjDrefImplicitLod:
3992 case SpvOpImageSampleProjDrefExplicitLod:
3993 case SpvOpImageFetch:
3994 case SpvOpImageGather:
3995 case SpvOpImageDrefGather:
3996 case SpvOpImageQuerySizeLod:
3997 case SpvOpImageQueryLod:
3998 case SpvOpImageQueryLevels:
3999 case SpvOpImageQuerySamples:
4000 vtn_handle_texture(b, opcode, w, count);
4001 break;
4002
4003 case SpvOpImageRead:
4004 case SpvOpImageWrite:
4005 case SpvOpImageTexelPointer:
4006 vtn_handle_image(b, opcode, w, count);
4007 break;
4008
4009 case SpvOpImageQuerySize: {
4010 struct vtn_pointer *image =
4011 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
4012 if (glsl_type_is_image(image->type->type)) {
4013 vtn_handle_image(b, opcode, w, count);
4014 } else {
4015 vtn_assert(glsl_type_is_sampler(image->type->type));
4016 vtn_handle_texture(b, opcode, w, count);
4017 }
4018 break;
4019 }
4020
4021 case SpvOpAtomicLoad:
4022 case SpvOpAtomicExchange:
4023 case SpvOpAtomicCompareExchange:
4024 case SpvOpAtomicCompareExchangeWeak:
4025 case SpvOpAtomicIIncrement:
4026 case SpvOpAtomicIDecrement:
4027 case SpvOpAtomicIAdd:
4028 case SpvOpAtomicISub:
4029 case SpvOpAtomicSMin:
4030 case SpvOpAtomicUMin:
4031 case SpvOpAtomicSMax:
4032 case SpvOpAtomicUMax:
4033 case SpvOpAtomicAnd:
4034 case SpvOpAtomicOr:
4035 case SpvOpAtomicXor: {
4036 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
4037 if (pointer->value_type == vtn_value_type_image_pointer) {
4038 vtn_handle_image(b, opcode, w, count);
4039 } else {
4040 vtn_assert(pointer->value_type == vtn_value_type_pointer);
4041 vtn_handle_atomics(b, opcode, w, count);
4042 }
4043 break;
4044 }
4045
4046 case SpvOpAtomicStore: {
4047 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
4048 if (pointer->value_type == vtn_value_type_image_pointer) {
4049 vtn_handle_image(b, opcode, w, count);
4050 } else {
4051 vtn_assert(pointer->value_type == vtn_value_type_pointer);
4052 vtn_handle_atomics(b, opcode, w, count);
4053 }
4054 break;
4055 }
4056
4057 case SpvOpSelect: {
4058 /* Handle OpSelect up-front here because it needs to be able to handle
4059 * pointers and not just regular vectors and scalars.
4060 */
4061 struct vtn_value *res_val = vtn_untyped_value(b, w[2]);
4062 struct vtn_value *sel_val = vtn_untyped_value(b, w[3]);
4063 struct vtn_value *obj1_val = vtn_untyped_value(b, w[4]);
4064 struct vtn_value *obj2_val = vtn_untyped_value(b, w[5]);
4065
4066 const struct glsl_type *sel_type;
4067 switch (res_val->type->base_type) {
4068 case vtn_base_type_scalar:
4069 sel_type = glsl_bool_type();
4070 break;
4071 case vtn_base_type_vector:
4072 sel_type = glsl_vector_type(GLSL_TYPE_BOOL, res_val->type->length);
4073 break;
4074 case vtn_base_type_pointer:
4075 /* We need to have actual storage for pointer types */
4076 vtn_fail_if(res_val->type->type == NULL,
4077 "Invalid pointer result type for OpSelect");
4078 sel_type = glsl_bool_type();
4079 break;
4080 default:
4081 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
4082 }
4083
4084 if (unlikely(sel_val->type->type != sel_type)) {
4085 if (sel_val->type->type == glsl_bool_type()) {
4086 /* This case is illegal but some older versions of GLSLang produce
4087 * it. The GLSLang issue was fixed on March 30, 2017:
4088 *
4089 * https://github.com/KhronosGroup/glslang/issues/809
4090 *
4091 * Unfortunately, there are applications in the wild which are
4092 * shipping with this bug so it isn't nice to fail on them so we
4093 * throw a warning instead. It's not actually a problem for us as
4094 * nir_builder will just splat the condition out which is most
4095 * likely what the client wanted anyway.
4096 */
4097 vtn_warn("Condition type of OpSelect must have the same number "
4098 "of components as Result Type");
4099 } else {
4100 vtn_fail("Condition type of OpSelect must be a scalar or vector "
4101 "of Boolean type. It must have the same number of "
4102 "components as Result Type");
4103 }
4104 }
4105
4106 vtn_fail_if(obj1_val->type != res_val->type ||
4107 obj2_val->type != res_val->type,
4108 "Object types must match the result type in OpSelect");
4109
4110 struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
4111 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, res_type->type);
4112 ssa->def = nir_bcsel(&b->nb, vtn_ssa_value(b, w[3])->def,
4113 vtn_ssa_value(b, w[4])->def,
4114 vtn_ssa_value(b, w[5])->def);
4115 vtn_push_ssa(b, w[2], res_type, ssa);
4116 break;
4117 }
4118
4119 case SpvOpSNegate:
4120 case SpvOpFNegate:
4121 case SpvOpNot:
4122 case SpvOpAny:
4123 case SpvOpAll:
4124 case SpvOpConvertFToU:
4125 case SpvOpConvertFToS:
4126 case SpvOpConvertSToF:
4127 case SpvOpConvertUToF:
4128 case SpvOpUConvert:
4129 case SpvOpSConvert:
4130 case SpvOpFConvert:
4131 case SpvOpQuantizeToF16:
4132 case SpvOpConvertPtrToU:
4133 case SpvOpConvertUToPtr:
4134 case SpvOpPtrCastToGeneric:
4135 case SpvOpGenericCastToPtr:
4136 case SpvOpBitcast:
4137 case SpvOpIsNan:
4138 case SpvOpIsInf:
4139 case SpvOpIsFinite:
4140 case SpvOpIsNormal:
4141 case SpvOpSignBitSet:
4142 case SpvOpLessOrGreater:
4143 case SpvOpOrdered:
4144 case SpvOpUnordered:
4145 case SpvOpIAdd:
4146 case SpvOpFAdd:
4147 case SpvOpISub:
4148 case SpvOpFSub:
4149 case SpvOpIMul:
4150 case SpvOpFMul:
4151 case SpvOpUDiv:
4152 case SpvOpSDiv:
4153 case SpvOpFDiv:
4154 case SpvOpUMod:
4155 case SpvOpSRem:
4156 case SpvOpSMod:
4157 case SpvOpFRem:
4158 case SpvOpFMod:
4159 case SpvOpVectorTimesScalar:
4160 case SpvOpDot:
4161 case SpvOpIAddCarry:
4162 case SpvOpISubBorrow:
4163 case SpvOpUMulExtended:
4164 case SpvOpSMulExtended:
4165 case SpvOpShiftRightLogical:
4166 case SpvOpShiftRightArithmetic:
4167 case SpvOpShiftLeftLogical:
4168 case SpvOpLogicalEqual:
4169 case SpvOpLogicalNotEqual:
4170 case SpvOpLogicalOr:
4171 case SpvOpLogicalAnd:
4172 case SpvOpLogicalNot:
4173 case SpvOpBitwiseOr:
4174 case SpvOpBitwiseXor:
4175 case SpvOpBitwiseAnd:
4176 case SpvOpIEqual:
4177 case SpvOpFOrdEqual:
4178 case SpvOpFUnordEqual:
4179 case SpvOpINotEqual:
4180 case SpvOpFOrdNotEqual:
4181 case SpvOpFUnordNotEqual:
4182 case SpvOpULessThan:
4183 case SpvOpSLessThan:
4184 case SpvOpFOrdLessThan:
4185 case SpvOpFUnordLessThan:
4186 case SpvOpUGreaterThan:
4187 case SpvOpSGreaterThan:
4188 case SpvOpFOrdGreaterThan:
4189 case SpvOpFUnordGreaterThan:
4190 case SpvOpULessThanEqual:
4191 case SpvOpSLessThanEqual:
4192 case SpvOpFOrdLessThanEqual:
4193 case SpvOpFUnordLessThanEqual:
4194 case SpvOpUGreaterThanEqual:
4195 case SpvOpSGreaterThanEqual:
4196 case SpvOpFOrdGreaterThanEqual:
4197 case SpvOpFUnordGreaterThanEqual:
4198 case SpvOpDPdx:
4199 case SpvOpDPdy:
4200 case SpvOpFwidth:
4201 case SpvOpDPdxFine:
4202 case SpvOpDPdyFine:
4203 case SpvOpFwidthFine:
4204 case SpvOpDPdxCoarse:
4205 case SpvOpDPdyCoarse:
4206 case SpvOpFwidthCoarse:
4207 case SpvOpBitFieldInsert:
4208 case SpvOpBitFieldSExtract:
4209 case SpvOpBitFieldUExtract:
4210 case SpvOpBitReverse:
4211 case SpvOpBitCount:
4212 case SpvOpTranspose:
4213 case SpvOpOuterProduct:
4214 case SpvOpMatrixTimesScalar:
4215 case SpvOpVectorTimesMatrix:
4216 case SpvOpMatrixTimesVector:
4217 case SpvOpMatrixTimesMatrix:
4218 vtn_handle_alu(b, opcode, w, count);
4219 break;
4220
4221 case SpvOpVectorExtractDynamic:
4222 case SpvOpVectorInsertDynamic:
4223 case SpvOpVectorShuffle:
4224 case SpvOpCompositeConstruct:
4225 case SpvOpCompositeExtract:
4226 case SpvOpCompositeInsert:
4227 case SpvOpCopyObject:
4228 vtn_handle_composite(b, opcode, w, count);
4229 break;
4230
4231 case SpvOpEmitVertex:
4232 case SpvOpEndPrimitive:
4233 case SpvOpEmitStreamVertex:
4234 case SpvOpEndStreamPrimitive:
4235 case SpvOpControlBarrier:
4236 case SpvOpMemoryBarrier:
4237 vtn_handle_barrier(b, opcode, w, count);
4238 break;
4239
4240 case SpvOpGroupNonUniformElect:
4241 case SpvOpGroupNonUniformAll:
4242 case SpvOpGroupNonUniformAny:
4243 case SpvOpGroupNonUniformAllEqual:
4244 case SpvOpGroupNonUniformBroadcast:
4245 case SpvOpGroupNonUniformBroadcastFirst:
4246 case SpvOpGroupNonUniformBallot:
4247 case SpvOpGroupNonUniformInverseBallot:
4248 case SpvOpGroupNonUniformBallotBitExtract:
4249 case SpvOpGroupNonUniformBallotBitCount:
4250 case SpvOpGroupNonUniformBallotFindLSB:
4251 case SpvOpGroupNonUniformBallotFindMSB:
4252 case SpvOpGroupNonUniformShuffle:
4253 case SpvOpGroupNonUniformShuffleXor:
4254 case SpvOpGroupNonUniformShuffleUp:
4255 case SpvOpGroupNonUniformShuffleDown:
4256 case SpvOpGroupNonUniformIAdd:
4257 case SpvOpGroupNonUniformFAdd:
4258 case SpvOpGroupNonUniformIMul:
4259 case SpvOpGroupNonUniformFMul:
4260 case SpvOpGroupNonUniformSMin:
4261 case SpvOpGroupNonUniformUMin:
4262 case SpvOpGroupNonUniformFMin:
4263 case SpvOpGroupNonUniformSMax:
4264 case SpvOpGroupNonUniformUMax:
4265 case SpvOpGroupNonUniformFMax:
4266 case SpvOpGroupNonUniformBitwiseAnd:
4267 case SpvOpGroupNonUniformBitwiseOr:
4268 case SpvOpGroupNonUniformBitwiseXor:
4269 case SpvOpGroupNonUniformLogicalAnd:
4270 case SpvOpGroupNonUniformLogicalOr:
4271 case SpvOpGroupNonUniformLogicalXor:
4272 case SpvOpGroupNonUniformQuadBroadcast:
4273 case SpvOpGroupNonUniformQuadSwap:
4274 vtn_handle_subgroup(b, opcode, w, count);
4275 break;
4276
4277 default:
4278 vtn_fail("Unhandled opcode");
4279 }
4280
4281 return true;
4282 }
4283
4284 struct vtn_builder*
4285 vtn_create_builder(const uint32_t *words, size_t word_count,
4286 gl_shader_stage stage, const char *entry_point_name,
4287 const struct spirv_to_nir_options *options)
4288 {
4289 /* Initialize the vtn_builder object */
4290 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
4291 b->spirv = words;
4292 b->spirv_word_count = word_count;
4293 b->file = NULL;
4294 b->line = -1;
4295 b->col = -1;
4296 exec_list_make_empty(&b->functions);
4297 b->entry_point_stage = stage;
4298 b->entry_point_name = entry_point_name;
4299 b->options = options;
4300
4301 /*
4302 * Handle the SPIR-V header (first 5 dwords).
4303 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4304 */
4305 if (word_count <= 5)
4306 goto fail;
4307
4308 if (words[0] != SpvMagicNumber) {
4309 vtn_err("words[0] was 0x%x, want 0x%x", words[0], SpvMagicNumber);
4310 goto fail;
4311 }
4312 if (words[1] < 0x10000) {
4313 vtn_err("words[1] was 0x%x, want >= 0x10000", words[1]);
4314 goto fail;
4315 }
4316
4317 /* words[2] == generator magic */
4318 unsigned value_id_bound = words[3];
4319 if (words[4] != 0) {
4320 vtn_err("words[4] was %u, want 0", words[4]);
4321 goto fail;
4322 }
4323
4324 b->value_id_bound = value_id_bound;
4325 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
4326
4327 return b;
4328 fail:
4329 ralloc_free(b);
4330 return NULL;
4331 }
4332
4333 nir_function *
4334 spirv_to_nir(const uint32_t *words, size_t word_count,
4335 struct nir_spirv_specialization *spec, unsigned num_spec,
4336 gl_shader_stage stage, const char *entry_point_name,
4337 const struct spirv_to_nir_options *options,
4338 const nir_shader_compiler_options *nir_options)
4339
4340 {
4341 const uint32_t *word_end = words + word_count;
4342
4343 struct vtn_builder *b = vtn_create_builder(words, word_count,
4344 stage, entry_point_name,
4345 options);
4346
4347 if (b == NULL)
4348 return NULL;
4349
4350 /* See also _vtn_fail() */
4351 if (setjmp(b->fail_jump)) {
4352 ralloc_free(b);
4353 return NULL;
4354 }
4355
4356 /* Skip the SPIR-V header, handled at vtn_create_builder */
4357 words+= 5;
4358
4359 /* Handle all the preamble instructions */
4360 words = vtn_foreach_instruction(b, words, word_end,
4361 vtn_handle_preamble_instruction);
4362
4363 if (b->entry_point == NULL) {
4364 vtn_fail("Entry point not found");
4365 ralloc_free(b);
4366 return NULL;
4367 }
4368
4369 b->shader = nir_shader_create(b, stage, nir_options, NULL);
4370
4371 /* Set shader info defaults */
4372 b->shader->info.gs.invocations = 1;
4373
4374 /* Parse execution modes */
4375 vtn_foreach_execution_mode(b, b->entry_point,
4376 vtn_handle_execution_mode, NULL);
4377
4378 b->specializations = spec;
4379 b->num_specializations = num_spec;
4380
4381 /* Handle all variable, type, and constant instructions */
4382 words = vtn_foreach_instruction(b, words, word_end,
4383 vtn_handle_variable_or_type_instruction);
4384
4385 /* Set types on all vtn_values */
4386 vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_type);
4387
4388 vtn_build_cfg(b, words, word_end);
4389
4390 assert(b->entry_point->value_type == vtn_value_type_function);
4391 b->entry_point->func->referenced = true;
4392
4393 bool progress;
4394 do {
4395 progress = false;
4396 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
4397 if (func->referenced && !func->emitted) {
4398 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
4399 _mesa_key_pointer_equal);
4400
4401 vtn_function_emit(b, func, vtn_handle_body_instruction);
4402 progress = true;
4403 }
4404 }
4405 } while (progress);
4406
4407 /* We sometimes generate bogus derefs that, while never used, give the
4408 * validator a bit of heartburn. Run dead code to get rid of them.
4409 */
4410 nir_opt_dce(b->shader);
4411
4412 vtn_assert(b->entry_point->value_type == vtn_value_type_function);
4413 nir_function *entry_point = b->entry_point->func->impl->function;
4414 vtn_assert(entry_point);
4415
4416 /* Unparent the shader from the vtn_builder before we delete the builder */
4417 ralloc_steal(NULL, b->shader);
4418
4419 ralloc_free(b);
4420
4421 return entry_point;
4422 }