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