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