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