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