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