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