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