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