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