41182a7003eadcaeae3cb85374716d1a6317b281
[mesa.git] / src / glsl / nir / 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 "spirv_to_nir_private.h"
29 #include "nir_vla.h"
30
31 static struct vtn_ssa_value *
32 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
33 const struct glsl_type *type)
34 {
35 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
36
37 if (entry)
38 return entry->data;
39
40 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
41 val->type = type;
42
43 switch (glsl_get_base_type(type)) {
44 case GLSL_TYPE_INT:
45 case GLSL_TYPE_UINT:
46 case GLSL_TYPE_BOOL:
47 case GLSL_TYPE_FLOAT:
48 case GLSL_TYPE_DOUBLE:
49 if (glsl_type_is_vector_or_scalar(type)) {
50 unsigned num_components = glsl_get_vector_elements(val->type);
51 nir_load_const_instr *load =
52 nir_load_const_instr_create(b->shader, num_components);
53
54 for (unsigned i = 0; i < num_components; i++)
55 load->value.u[i] = constant->value.u[i];
56
57 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
58 val->def = &load->def;
59 } else {
60 assert(glsl_type_is_matrix(type));
61 unsigned rows = glsl_get_vector_elements(val->type);
62 unsigned columns = glsl_get_matrix_columns(val->type);
63 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
64
65 for (unsigned i = 0; i < columns; i++) {
66 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
67 col_val->type = glsl_get_column_type(val->type);
68 nir_load_const_instr *load =
69 nir_load_const_instr_create(b->shader, rows);
70
71 for (unsigned j = 0; j < rows; j++)
72 load->value.u[j] = constant->value.u[rows * i + j];
73
74 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
75 col_val->def = &load->def;
76
77 val->elems[i] = col_val;
78 }
79 }
80 break;
81
82 case GLSL_TYPE_ARRAY: {
83 unsigned elems = glsl_get_length(val->type);
84 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
85 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
86 for (unsigned i = 0; i < elems; i++)
87 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
88 elem_type);
89 break;
90 }
91
92 case GLSL_TYPE_STRUCT: {
93 unsigned elems = glsl_get_length(val->type);
94 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
95 for (unsigned i = 0; i < elems; i++) {
96 const struct glsl_type *elem_type =
97 glsl_get_struct_field(val->type, i);
98 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
99 elem_type);
100 }
101 break;
102 }
103
104 default:
105 unreachable("bad constant type");
106 }
107
108 return val;
109 }
110
111 struct vtn_ssa_value *
112 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
113 {
114 struct vtn_value *val = vtn_untyped_value(b, value_id);
115 switch (val->value_type) {
116 case vtn_value_type_constant:
117 return vtn_const_ssa_value(b, val->constant, val->const_type);
118
119 case vtn_value_type_ssa:
120 return val->ssa;
121 default:
122 unreachable("Invalid type for an SSA value");
123 }
124 }
125
126 static char *
127 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
128 unsigned word_count)
129 {
130 return ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
131 }
132
133 static const uint32_t *
134 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
135 const uint32_t *end, vtn_instruction_handler handler)
136 {
137 const uint32_t *w = start;
138 while (w < end) {
139 SpvOp opcode = w[0] & SpvOpCodeMask;
140 unsigned count = w[0] >> SpvWordCountShift;
141 assert(count >= 1 && w + count <= end);
142
143 if (!handler(b, opcode, w, count))
144 return w;
145
146 w += count;
147 }
148 assert(w == end);
149 return w;
150 }
151
152 static void
153 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
154 const uint32_t *w, unsigned count)
155 {
156 switch (opcode) {
157 case SpvOpExtInstImport: {
158 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
159 if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
160 val->ext_handler = vtn_handle_glsl450_instruction;
161 } else {
162 assert(!"Unsupported extension");
163 }
164 break;
165 }
166
167 case SpvOpExtInst: {
168 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
169 bool handled = val->ext_handler(b, w[4], w, count);
170 (void)handled;
171 assert(handled);
172 break;
173 }
174
175 default:
176 unreachable("Unhandled opcode");
177 }
178 }
179
180 static void
181 _foreach_decoration_helper(struct vtn_builder *b,
182 struct vtn_value *base_value,
183 int member,
184 struct vtn_value *value,
185 vtn_decoration_foreach_cb cb, void *data)
186 {
187 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
188 if (dec->member >= 0) {
189 assert(member == -1);
190 member = dec->member;
191 }
192
193 if (dec->group) {
194 assert(dec->group->value_type == vtn_value_type_decoration_group);
195 _foreach_decoration_helper(b, base_value, member, dec->group, cb, data);
196 } else {
197 cb(b, base_value, member, dec, data);
198 }
199 }
200 }
201
202 /** Iterates (recursively if needed) over all of the decorations on a value
203 *
204 * This function iterates over all of the decorations applied to a given
205 * value. If it encounters a decoration group, it recurses into the group
206 * and iterates over all of those decorations as well.
207 */
208 void
209 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
210 vtn_decoration_foreach_cb cb, void *data)
211 {
212 _foreach_decoration_helper(b, value, -1, value, cb, data);
213 }
214
215 static void
216 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
217 const uint32_t *w, unsigned count)
218 {
219 const uint32_t *w_end = w + count;
220 const uint32_t target = w[1];
221 w += 2;
222
223 int member = -1;
224 switch (opcode) {
225 case SpvOpDecorationGroup:
226 vtn_push_value(b, target, vtn_value_type_undef);
227 break;
228
229 case SpvOpMemberDecorate:
230 member = *(w++);
231 /* fallthrough */
232 case SpvOpDecorate: {
233 struct vtn_value *val = &b->values[target];
234
235 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
236 dec->member = member;
237 dec->decoration = *(w++);
238 dec->literals = w;
239
240 /* Link into the list */
241 dec->next = val->decoration;
242 val->decoration = dec;
243 break;
244 }
245
246 case SpvOpGroupMemberDecorate:
247 member = *(w++);
248 /* fallthrough */
249 case SpvOpGroupDecorate: {
250 struct vtn_value *group = &b->values[target];
251 assert(group->value_type == vtn_value_type_decoration_group);
252
253 for (; w < w_end; w++) {
254 struct vtn_value *val = &b->values[*w];
255 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
256 dec->member = member;
257 dec->group = group;
258
259 /* Link into the list */
260 dec->next = val->decoration;
261 val->decoration = dec;
262 }
263 break;
264 }
265
266 default:
267 unreachable("Unhandled opcode");
268 }
269 }
270
271 static void
272 struct_member_decoration_cb(struct vtn_builder *b,
273 struct vtn_value *val, int member,
274 const struct vtn_decoration *dec, void *void_fields)
275 {
276 struct glsl_struct_field *fields = void_fields;
277
278 if (member < 0)
279 return;
280
281 switch (dec->decoration) {
282 case SpvDecorationPrecisionLow:
283 case SpvDecorationPrecisionMedium:
284 case SpvDecorationPrecisionHigh:
285 break; /* FIXME: Do nothing with these for now. */
286 case SpvDecorationSmooth:
287 fields[member].interpolation = INTERP_QUALIFIER_SMOOTH;
288 break;
289 case SpvDecorationNoperspective:
290 fields[member].interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
291 break;
292 case SpvDecorationFlat:
293 fields[member].interpolation = INTERP_QUALIFIER_FLAT;
294 break;
295 case SpvDecorationCentroid:
296 fields[member].centroid = true;
297 break;
298 case SpvDecorationSample:
299 fields[member].sample = true;
300 break;
301 case SpvDecorationLocation:
302 fields[member].location = dec->literals[0];
303 break;
304 default:
305 unreachable("Unhandled member decoration");
306 }
307 }
308
309 static void
310 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
311 const uint32_t *w, unsigned count)
312 {
313 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
314
315 val->type = ralloc(b, struct vtn_type);
316 val->type->is_builtin = false;
317
318 switch (opcode) {
319 case SpvOpTypeVoid:
320 val->type->type = glsl_void_type();
321 return;
322 case SpvOpTypeBool:
323 val->type->type = glsl_bool_type();
324 return;
325 case SpvOpTypeInt:
326 val->type->type = glsl_int_type();
327 return;
328 case SpvOpTypeFloat:
329 val->type->type = glsl_float_type();
330 return;
331
332 case SpvOpTypeVector: {
333 const struct glsl_type *base =
334 vtn_value(b, w[2], vtn_value_type_type)->type->type;
335 unsigned elems = w[3];
336
337 assert(glsl_type_is_scalar(base));
338 val->type->type = glsl_vector_type(glsl_get_base_type(base), elems);
339 return;
340 }
341
342 case SpvOpTypeMatrix: {
343 const struct glsl_type *base =
344 vtn_value(b, w[2], vtn_value_type_type)->type->type;
345 unsigned columns = w[3];
346
347 assert(glsl_type_is_vector(base));
348 val->type->type = glsl_matrix_type(glsl_get_base_type(base),
349 glsl_get_vector_elements(base),
350 columns);
351 val->type->row_major = false;
352 val->type->stride = 0;
353 return;
354 }
355
356 case SpvOpTypeArray: {
357 struct vtn_type *array_element =
358 vtn_value(b, w[2], vtn_value_type_type)->type;
359 val->type->type = glsl_array_type(array_element->type, w[3]);
360 val->type->array_element = array_element;
361 val->type->stride = 0;
362 return;
363 }
364
365 case SpvOpTypeStruct: {
366 unsigned num_fields = count - 2;
367 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
368
369 NIR_VLA(struct glsl_struct_field, fields, count);
370 for (unsigned i = 0; i < num_fields; i++) {
371 /* TODO: Handle decorators */
372 val->type->members[i] =
373 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
374 fields[i].type = val->type->members[i]->type;
375 fields[i].name = ralloc_asprintf(b, "field%d", i);
376 fields[i].location = -1;
377 fields[i].interpolation = 0;
378 fields[i].centroid = 0;
379 fields[i].sample = 0;
380 fields[i].matrix_layout = 2;
381 fields[i].stream = -1;
382 }
383
384 vtn_foreach_decoration(b, val, struct_member_decoration_cb, fields);
385
386 const char *name = val->name ? val->name : "struct";
387
388 val->type->type = glsl_struct_type(fields, count, name);
389 return;
390 }
391
392 case SpvOpTypeFunction: {
393 const struct glsl_type *return_type =
394 vtn_value(b, w[2], vtn_value_type_type)->type->type;
395 NIR_VLA(struct glsl_function_param, params, count - 3);
396 for (unsigned i = 0; i < count - 3; i++) {
397 params[i].type = vtn_value(b, w[i + 3], vtn_value_type_type)->type->type;
398
399 /* FIXME: */
400 params[i].in = true;
401 params[i].out = true;
402 }
403 val->type->type = glsl_function_type(return_type, params, count - 3);
404 return;
405 }
406
407 case SpvOpTypePointer:
408 /* FIXME: For now, we'll just do the really lame thing and return
409 * the same type. The validator should ensure that the proper number
410 * of dereferences happen
411 */
412 val->type = vtn_value(b, w[3], vtn_value_type_type)->type;
413 return;
414
415 case SpvOpTypeSampler: {
416 const struct glsl_type *sampled_type =
417 vtn_value(b, w[2], vtn_value_type_type)->type->type;
418
419 assert(glsl_type_is_vector_or_scalar(sampled_type));
420
421 enum glsl_sampler_dim dim;
422 switch ((SpvDim)w[3]) {
423 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
424 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
425 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
426 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
427 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
428 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
429 default:
430 unreachable("Invalid SPIR-V Sampler dimension");
431 }
432
433 /* TODO: Handle the various texture image/filter options */
434 (void)w[4];
435
436 bool is_array = w[5];
437 bool is_shadow = w[6];
438
439 assert(w[7] == 0 && "FIXME: Handl multi-sampled textures");
440
441 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
442 glsl_get_base_type(sampled_type));
443 return;
444 }
445
446 case SpvOpTypeRuntimeArray:
447 case SpvOpTypeOpaque:
448 case SpvOpTypeEvent:
449 case SpvOpTypeDeviceEvent:
450 case SpvOpTypeReserveId:
451 case SpvOpTypeQueue:
452 case SpvOpTypePipe:
453 default:
454 unreachable("Unhandled opcode");
455 }
456 }
457
458 static void
459 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
460 const uint32_t *w, unsigned count)
461 {
462 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
463 val->const_type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
464 val->constant = ralloc(b, nir_constant);
465 switch (opcode) {
466 case SpvOpConstantTrue:
467 assert(val->const_type == glsl_bool_type());
468 val->constant->value.u[0] = NIR_TRUE;
469 break;
470 case SpvOpConstantFalse:
471 assert(val->const_type == glsl_bool_type());
472 val->constant->value.u[0] = NIR_FALSE;
473 break;
474 case SpvOpConstant:
475 assert(glsl_type_is_scalar(val->const_type));
476 val->constant->value.u[0] = w[3];
477 break;
478 case SpvOpConstantComposite: {
479 unsigned elem_count = count - 3;
480 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
481 for (unsigned i = 0; i < elem_count; i++)
482 elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
483
484 switch (glsl_get_base_type(val->const_type)) {
485 case GLSL_TYPE_UINT:
486 case GLSL_TYPE_INT:
487 case GLSL_TYPE_FLOAT:
488 case GLSL_TYPE_BOOL:
489 if (glsl_type_is_matrix(val->const_type)) {
490 unsigned rows = glsl_get_vector_elements(val->const_type);
491 assert(glsl_get_matrix_columns(val->const_type) == elem_count);
492 for (unsigned i = 0; i < elem_count; i++)
493 for (unsigned j = 0; j < rows; j++)
494 val->constant->value.u[rows * i + j] = elems[i]->value.u[j];
495 } else {
496 assert(glsl_type_is_vector(val->const_type));
497 assert(glsl_get_vector_elements(val->const_type) == elem_count);
498 for (unsigned i = 0; i < elem_count; i++)
499 val->constant->value.u[i] = elems[i]->value.u[0];
500 }
501 ralloc_free(elems);
502 break;
503
504 case GLSL_TYPE_STRUCT:
505 case GLSL_TYPE_ARRAY:
506 ralloc_steal(val->constant, elems);
507 val->constant->elements = elems;
508 break;
509
510 default:
511 unreachable("Unsupported type for constants");
512 }
513 break;
514 }
515
516 default:
517 unreachable("Unhandled opcode");
518 }
519 }
520
521 static void
522 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
523 const struct vtn_decoration *dec, void *void_var)
524 {
525 assert(val->value_type == vtn_value_type_deref);
526 assert(val->deref->deref.child == NULL);
527 assert(val->deref->var == void_var);
528
529 nir_variable *var = void_var;
530 switch (dec->decoration) {
531 case SpvDecorationPrecisionLow:
532 case SpvDecorationPrecisionMedium:
533 case SpvDecorationPrecisionHigh:
534 break; /* FIXME: Do nothing with these for now. */
535 case SpvDecorationSmooth:
536 var->data.interpolation = INTERP_QUALIFIER_SMOOTH;
537 break;
538 case SpvDecorationNoperspective:
539 var->data.interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
540 break;
541 case SpvDecorationFlat:
542 var->data.interpolation = INTERP_QUALIFIER_FLAT;
543 break;
544 case SpvDecorationCentroid:
545 var->data.centroid = true;
546 break;
547 case SpvDecorationSample:
548 var->data.sample = true;
549 break;
550 case SpvDecorationInvariant:
551 var->data.invariant = true;
552 break;
553 case SpvDecorationConstant:
554 assert(var->constant_initializer != NULL);
555 var->data.read_only = true;
556 break;
557 case SpvDecorationNonwritable:
558 var->data.read_only = true;
559 break;
560 case SpvDecorationLocation:
561 var->data.explicit_location = true;
562 var->data.location = dec->literals[0];
563 break;
564 case SpvDecorationComponent:
565 var->data.location_frac = dec->literals[0];
566 break;
567 case SpvDecorationIndex:
568 var->data.explicit_index = true;
569 var->data.index = dec->literals[0];
570 break;
571 case SpvDecorationBinding:
572 var->data.explicit_binding = true;
573 var->data.binding = dec->literals[0];
574 break;
575 case SpvDecorationDescriptorSet:
576 var->data.descriptor_set = dec->literals[0];
577 break;
578 case SpvDecorationBuiltIn:
579 var->data.mode = nir_var_system_value;
580 var->data.read_only = true;
581 switch ((SpvBuiltIn)dec->literals[0]) {
582 case SpvBuiltInFrontFacing:
583 var->data.location = SYSTEM_VALUE_FRONT_FACE;
584 break;
585 case SpvBuiltInVertexId:
586 var->data.location = SYSTEM_VALUE_VERTEX_ID;
587 break;
588 case SpvBuiltInInstanceId:
589 var->data.location = SYSTEM_VALUE_INSTANCE_ID;
590 break;
591 case SpvBuiltInSampleId:
592 var->data.location = SYSTEM_VALUE_SAMPLE_ID;
593 break;
594 case SpvBuiltInSamplePosition:
595 var->data.location = SYSTEM_VALUE_SAMPLE_POS;
596 break;
597 case SpvBuiltInSampleMask:
598 var->data.location = SYSTEM_VALUE_SAMPLE_MASK_IN;
599 break;
600 case SpvBuiltInInvocationId:
601 var->data.location = SYSTEM_VALUE_INVOCATION_ID;
602 break;
603 case SpvBuiltInPrimitiveId:
604 case SpvBuiltInPosition:
605 case SpvBuiltInPointSize:
606 case SpvBuiltInClipVertex:
607 case SpvBuiltInClipDistance:
608 case SpvBuiltInCullDistance:
609 case SpvBuiltInLayer:
610 case SpvBuiltInViewportIndex:
611 case SpvBuiltInTessLevelOuter:
612 case SpvBuiltInTessLevelInner:
613 case SpvBuiltInTessCoord:
614 case SpvBuiltInPatchVertices:
615 case SpvBuiltInFragCoord:
616 case SpvBuiltInPointCoord:
617 case SpvBuiltInFragColor:
618 case SpvBuiltInFragDepth:
619 case SpvBuiltInHelperInvocation:
620 case SpvBuiltInNumWorkgroups:
621 case SpvBuiltInWorkgroupSize:
622 case SpvBuiltInWorkgroupId:
623 case SpvBuiltInLocalInvocationId:
624 case SpvBuiltInGlobalInvocationId:
625 case SpvBuiltInLocalInvocationIndex:
626 case SpvBuiltInWorkDim:
627 case SpvBuiltInGlobalSize:
628 case SpvBuiltInEnqueuedWorkgroupSize:
629 case SpvBuiltInGlobalOffset:
630 case SpvBuiltInGlobalLinearId:
631 case SpvBuiltInWorkgroupLinearId:
632 case SpvBuiltInSubgroupSize:
633 case SpvBuiltInSubgroupMaxSize:
634 case SpvBuiltInNumSubgroups:
635 case SpvBuiltInNumEnqueuedSubgroups:
636 case SpvBuiltInSubgroupId:
637 case SpvBuiltInSubgroupLocalInvocationId:
638 unreachable("Unhandled builtin enum");
639 }
640 break;
641 case SpvDecorationNoStaticUse:
642 /* This can safely be ignored */
643 break;
644 case SpvDecorationBlock:
645 case SpvDecorationBufferBlock:
646 case SpvDecorationRowMajor:
647 case SpvDecorationColMajor:
648 case SpvDecorationGLSLShared:
649 case SpvDecorationGLSLStd140:
650 case SpvDecorationGLSLStd430:
651 case SpvDecorationGLSLPacked:
652 case SpvDecorationPatch:
653 case SpvDecorationRestrict:
654 case SpvDecorationAliased:
655 case SpvDecorationVolatile:
656 case SpvDecorationCoherent:
657 case SpvDecorationNonreadable:
658 case SpvDecorationUniform:
659 /* This is really nice but we have no use for it right now. */
660 case SpvDecorationCPacked:
661 case SpvDecorationSaturatedConversion:
662 case SpvDecorationStream:
663 case SpvDecorationOffset:
664 case SpvDecorationAlignment:
665 case SpvDecorationXfbBuffer:
666 case SpvDecorationStride:
667 case SpvDecorationFuncParamAttr:
668 case SpvDecorationFPRoundingMode:
669 case SpvDecorationFPFastMathMode:
670 case SpvDecorationLinkageAttributes:
671 case SpvDecorationSpecId:
672 break;
673 default:
674 unreachable("Unhandled variable decoration");
675 }
676 }
677
678 static struct vtn_ssa_value *
679 _vtn_variable_load(struct vtn_builder *b,
680 nir_deref_var *src_deref, nir_deref *src_deref_tail)
681 {
682 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
683 val->type = src_deref_tail->type;
684
685 /* The deref tail may contain a deref to select a component of a vector (in
686 * other words, it might not be an actual tail) so we have to save it away
687 * here since we overwrite it later.
688 */
689 nir_deref *old_child = src_deref_tail->child;
690
691 if (glsl_type_is_vector_or_scalar(val->type)) {
692 nir_intrinsic_instr *load =
693 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
694 load->variables[0] =
695 nir_deref_as_var(nir_copy_deref(load, &src_deref->deref));
696 load->num_components = glsl_get_vector_elements(val->type);
697 nir_ssa_dest_init(&load->instr, &load->dest, load->num_components, NULL);
698
699 nir_builder_instr_insert(&b->nb, &load->instr);
700
701 if (src_deref->var->data.mode == nir_var_uniform &&
702 glsl_get_base_type(val->type) == GLSL_TYPE_BOOL) {
703 /* Uniform boolean loads need to be fixed up since they're defined
704 * to be zero/nonzero rather than NIR_FALSE/NIR_TRUE.
705 */
706 val->def = nir_ine(&b->nb, &load->dest.ssa, nir_imm_int(&b->nb, 0));
707 } else {
708 val->def = &load->dest.ssa;
709 }
710 } else if (glsl_get_base_type(val->type) == GLSL_TYPE_ARRAY ||
711 glsl_type_is_matrix(val->type)) {
712 unsigned elems = glsl_get_length(val->type);
713 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
714
715 nir_deref_array *deref = nir_deref_array_create(b);
716 deref->deref_array_type = nir_deref_array_type_direct;
717 deref->deref.type = glsl_get_array_element(val->type);
718 src_deref_tail->child = &deref->deref;
719 for (unsigned i = 0; i < elems; i++) {
720 deref->base_offset = i;
721 val->elems[i] = _vtn_variable_load(b, src_deref, &deref->deref);
722 }
723 } else {
724 assert(glsl_get_base_type(val->type) == GLSL_TYPE_STRUCT);
725 unsigned elems = glsl_get_length(val->type);
726 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
727
728 nir_deref_struct *deref = nir_deref_struct_create(b, 0);
729 src_deref_tail->child = &deref->deref;
730 for (unsigned i = 0; i < elems; i++) {
731 deref->index = i;
732 deref->deref.type = glsl_get_struct_field(val->type, i);
733 val->elems[i] = _vtn_variable_load(b, src_deref, &deref->deref);
734 }
735 }
736
737 src_deref_tail->child = old_child;
738
739 return val;
740 }
741
742 static void
743 _vtn_variable_store(struct vtn_builder *b, nir_deref_var *dest_deref,
744 nir_deref *dest_deref_tail, struct vtn_ssa_value *src)
745 {
746 nir_deref *old_child = dest_deref_tail->child;
747
748 if (glsl_type_is_vector_or_scalar(src->type)) {
749 nir_intrinsic_instr *store =
750 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
751 store->variables[0] =
752 nir_deref_as_var(nir_copy_deref(store, &dest_deref->deref));
753 store->src[0] = nir_src_for_ssa(src->def);
754
755 nir_builder_instr_insert(&b->nb, &store->instr);
756 } else if (glsl_get_base_type(src->type) == GLSL_TYPE_ARRAY ||
757 glsl_type_is_matrix(src->type)) {
758 unsigned elems = glsl_get_length(src->type);
759
760 nir_deref_array *deref = nir_deref_array_create(b);
761 deref->deref_array_type = nir_deref_array_type_direct;
762 deref->deref.type = glsl_get_array_element(src->type);
763 dest_deref_tail->child = &deref->deref;
764 for (unsigned i = 0; i < elems; i++) {
765 deref->base_offset = i;
766 _vtn_variable_store(b, dest_deref, &deref->deref, src->elems[i]);
767 }
768 } else {
769 assert(glsl_get_base_type(src->type) == GLSL_TYPE_STRUCT);
770 unsigned elems = glsl_get_length(src->type);
771
772 nir_deref_struct *deref = nir_deref_struct_create(b, 0);
773 dest_deref_tail->child = &deref->deref;
774 for (unsigned i = 0; i < elems; i++) {
775 deref->index = i;
776 deref->deref.type = glsl_get_struct_field(src->type, i);
777 _vtn_variable_store(b, dest_deref, &deref->deref, src->elems[i]);
778 }
779 }
780
781 dest_deref_tail->child = old_child;
782 }
783
784 /*
785 * Gets the NIR-level deref tail, which may have as a child an array deref
786 * selecting which component due to OpAccessChain supporting per-component
787 * indexing in SPIR-V.
788 */
789
790 static nir_deref *
791 get_deref_tail(nir_deref_var *deref)
792 {
793 nir_deref *cur = &deref->deref;
794 while (!glsl_type_is_vector_or_scalar(cur->type) && cur->child)
795 cur = cur->child;
796
797 return cur;
798 }
799
800 static nir_ssa_def *vtn_vector_extract(struct vtn_builder *b,
801 nir_ssa_def *src, unsigned index);
802
803 static nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b,
804 nir_ssa_def *src,
805 nir_ssa_def *index);
806
807 static struct vtn_ssa_value *
808 vtn_variable_load(struct vtn_builder *b, nir_deref_var *src)
809 {
810 nir_deref *src_tail = get_deref_tail(src);
811 struct vtn_ssa_value *val = _vtn_variable_load(b, src, src_tail);
812
813 if (src_tail->child) {
814 nir_deref_array *vec_deref = nir_deref_as_array(src_tail->child);
815 assert(vec_deref->deref.child == NULL);
816 val->type = vec_deref->deref.type;
817 if (vec_deref->deref_array_type == nir_deref_array_type_direct)
818 val->def = vtn_vector_extract(b, val->def, vec_deref->base_offset);
819 else
820 val->def = vtn_vector_extract_dynamic(b, val->def,
821 vec_deref->indirect.ssa);
822 }
823
824 return val;
825 }
826
827 static nir_ssa_def * vtn_vector_insert(struct vtn_builder *b,
828 nir_ssa_def *src, nir_ssa_def *insert,
829 unsigned index);
830
831 static nir_ssa_def * vtn_vector_insert_dynamic(struct vtn_builder *b,
832 nir_ssa_def *src,
833 nir_ssa_def *insert,
834 nir_ssa_def *index);
835 static void
836 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
837 nir_deref_var *dest)
838 {
839 nir_deref *dest_tail = get_deref_tail(dest);
840 if (dest_tail->child) {
841 struct vtn_ssa_value *val = _vtn_variable_load(b, dest, dest_tail);
842 nir_deref_array *deref = nir_deref_as_array(dest_tail->child);
843 assert(deref->deref.child == NULL);
844 if (deref->deref_array_type == nir_deref_array_type_direct)
845 val->def = vtn_vector_insert(b, val->def, src->def,
846 deref->base_offset);
847 else
848 val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
849 deref->indirect.ssa);
850 _vtn_variable_store(b, dest, dest_tail, val);
851 } else {
852 _vtn_variable_store(b, dest, dest_tail, src);
853 }
854 }
855
856 static void
857 vtn_variable_copy(struct vtn_builder *b, nir_deref_var *src,
858 nir_deref_var *dest)
859 {
860 nir_deref *src_tail = get_deref_tail(src);
861
862 if (src_tail->child) {
863 assert(get_deref_tail(dest)->child);
864 struct vtn_ssa_value *val = vtn_variable_load(b, src);
865 vtn_variable_store(b, val, dest);
866 } else {
867 nir_intrinsic_instr *copy =
868 nir_intrinsic_instr_create(b->shader, nir_intrinsic_copy_var);
869 copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
870 copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
871
872 nir_builder_instr_insert(&b->nb, &copy->instr);
873 }
874 }
875
876 static void
877 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
878 const uint32_t *w, unsigned count)
879 {
880 switch (opcode) {
881 case SpvOpVariable: {
882 const struct glsl_type *type =
883 vtn_value(b, w[1], vtn_value_type_type)->type->type;
884 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
885
886 nir_variable *var = ralloc(b->shader, nir_variable);
887
888 var->type = type;
889 var->name = ralloc_strdup(var, val->name);
890
891 switch ((SpvStorageClass)w[3]) {
892 case SpvStorageClassUniform:
893 case SpvStorageClassUniformConstant:
894 var->data.mode = nir_var_uniform;
895 var->data.read_only = true;
896 var->interface_type = type;
897 break;
898 case SpvStorageClassInput:
899 var->data.mode = nir_var_shader_in;
900 var->data.read_only = true;
901 break;
902 case SpvStorageClassOutput:
903 var->data.mode = nir_var_shader_out;
904 break;
905 case SpvStorageClassPrivateGlobal:
906 var->data.mode = nir_var_global;
907 break;
908 case SpvStorageClassFunction:
909 var->data.mode = nir_var_local;
910 break;
911 case SpvStorageClassWorkgroupLocal:
912 case SpvStorageClassWorkgroupGlobal:
913 case SpvStorageClassGeneric:
914 case SpvStorageClassPrivate:
915 case SpvStorageClassAtomicCounter:
916 default:
917 unreachable("Unhandled variable storage class");
918 }
919
920 if (count > 4) {
921 assert(count == 5);
922 var->constant_initializer =
923 vtn_value(b, w[4], vtn_value_type_constant)->constant;
924 }
925
926 val->deref = nir_deref_var_create(b, var);
927
928 vtn_foreach_decoration(b, val, var_decoration_cb, var);
929
930 if (b->execution_model == SpvExecutionModelFragment &&
931 var->data.mode == nir_var_shader_out) {
932 var->data.location += FRAG_RESULT_DATA0;
933 } else if (b->execution_model == SpvExecutionModelVertex &&
934 var->data.mode == nir_var_shader_in) {
935 var->data.location += VERT_ATTRIB_GENERIC0;
936 } else if (var->data.mode == nir_var_shader_in ||
937 var->data.mode == nir_var_shader_out) {
938 var->data.location += VARYING_SLOT_VAR0;
939 }
940
941 switch (var->data.mode) {
942 case nir_var_shader_in:
943 exec_list_push_tail(&b->shader->inputs, &var->node);
944 break;
945 case nir_var_shader_out:
946 exec_list_push_tail(&b->shader->outputs, &var->node);
947 break;
948 case nir_var_global:
949 exec_list_push_tail(&b->shader->globals, &var->node);
950 break;
951 case nir_var_local:
952 exec_list_push_tail(&b->impl->locals, &var->node);
953 break;
954 case nir_var_uniform:
955 exec_list_push_tail(&b->shader->uniforms, &var->node);
956 break;
957 case nir_var_system_value:
958 exec_list_push_tail(&b->shader->system_values, &var->node);
959 break;
960 }
961 break;
962 }
963
964 case SpvOpAccessChain:
965 case SpvOpInBoundsAccessChain: {
966 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
967 nir_deref_var *base = vtn_value(b, w[3], vtn_value_type_deref)->deref;
968 val->deref = nir_deref_as_var(nir_copy_deref(b, &base->deref));
969
970 nir_deref *tail = &val->deref->deref;
971 while (tail->child)
972 tail = tail->child;
973
974 for (unsigned i = 0; i < count - 4; i++) {
975 assert(w[i + 4] < b->value_id_bound);
976 struct vtn_value *idx_val = &b->values[w[i + 4]];
977
978 enum glsl_base_type base_type = glsl_get_base_type(tail->type);
979 switch (base_type) {
980 case GLSL_TYPE_UINT:
981 case GLSL_TYPE_INT:
982 case GLSL_TYPE_FLOAT:
983 case GLSL_TYPE_DOUBLE:
984 case GLSL_TYPE_BOOL:
985 case GLSL_TYPE_ARRAY: {
986 nir_deref_array *deref_arr = nir_deref_array_create(b);
987 if (base_type == GLSL_TYPE_ARRAY) {
988 deref_arr->deref.type = glsl_get_array_element(tail->type);
989 } else if (glsl_type_is_matrix(tail->type)) {
990 deref_arr->deref.type = glsl_get_column_type(tail->type);
991 } else {
992 assert(glsl_type_is_vector(tail->type));
993 deref_arr->deref.type = glsl_scalar_type(base_type);
994 }
995
996 if (idx_val->value_type == vtn_value_type_constant) {
997 unsigned idx = idx_val->constant->value.u[0];
998 deref_arr->deref_array_type = nir_deref_array_type_direct;
999 deref_arr->base_offset = idx;
1000 } else {
1001 assert(idx_val->value_type == vtn_value_type_ssa);
1002 deref_arr->deref_array_type = nir_deref_array_type_indirect;
1003 deref_arr->base_offset = 0;
1004 deref_arr->indirect =
1005 nir_src_for_ssa(vtn_ssa_value(b, w[1])->def);
1006 }
1007 tail->child = &deref_arr->deref;
1008 break;
1009 }
1010
1011 case GLSL_TYPE_STRUCT: {
1012 assert(idx_val->value_type == vtn_value_type_constant);
1013 unsigned idx = idx_val->constant->value.u[0];
1014 nir_deref_struct *deref_struct = nir_deref_struct_create(b, idx);
1015 deref_struct->deref.type = glsl_get_struct_field(tail->type, idx);
1016 tail->child = &deref_struct->deref;
1017 break;
1018 }
1019 default:
1020 unreachable("Invalid type for deref");
1021 }
1022 tail = tail->child;
1023 }
1024 break;
1025 }
1026
1027 case SpvOpCopyMemory: {
1028 nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
1029 nir_deref_var *src = vtn_value(b, w[2], vtn_value_type_deref)->deref;
1030
1031 vtn_variable_copy(b, src, dest);
1032 break;
1033 }
1034
1035 case SpvOpLoad: {
1036 nir_deref_var *src = vtn_value(b, w[3], vtn_value_type_deref)->deref;
1037 const struct glsl_type *src_type = nir_deref_tail(&src->deref)->type;
1038
1039 if (glsl_get_base_type(src_type) == GLSL_TYPE_SAMPLER) {
1040 vtn_push_value(b, w[2], vtn_value_type_deref)->deref = src;
1041 return;
1042 }
1043
1044 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1045 val->ssa = vtn_variable_load(b, src);
1046 break;
1047 }
1048
1049 case SpvOpStore: {
1050 nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
1051 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
1052 vtn_variable_store(b, src, dest);
1053 break;
1054 }
1055
1056 case SpvOpVariableArray:
1057 case SpvOpCopyMemorySized:
1058 case SpvOpArrayLength:
1059 case SpvOpImagePointer:
1060 default:
1061 unreachable("Unhandled opcode");
1062 }
1063 }
1064
1065 static void
1066 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
1067 const uint32_t *w, unsigned count)
1068 {
1069 unreachable("Unhandled opcode");
1070 }
1071
1072 static struct vtn_ssa_value *
1073 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1074 {
1075 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1076 val->type = type;
1077
1078 if (!glsl_type_is_vector_or_scalar(type)) {
1079 unsigned elems = glsl_get_length(type);
1080 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1081 for (unsigned i = 0; i < elems; i++) {
1082 const struct glsl_type *child_type;
1083
1084 switch (glsl_get_base_type(type)) {
1085 case GLSL_TYPE_INT:
1086 case GLSL_TYPE_UINT:
1087 case GLSL_TYPE_BOOL:
1088 case GLSL_TYPE_FLOAT:
1089 case GLSL_TYPE_DOUBLE:
1090 child_type = glsl_get_column_type(type);
1091 break;
1092 case GLSL_TYPE_ARRAY:
1093 child_type = glsl_get_array_element(type);
1094 break;
1095 case GLSL_TYPE_STRUCT:
1096 child_type = glsl_get_struct_field(type, i);
1097 break;
1098 default:
1099 unreachable("unkown base type");
1100 }
1101
1102 val->elems[i] = vtn_create_ssa_value(b, child_type);
1103 }
1104 }
1105
1106 return val;
1107 }
1108
1109 static nir_tex_src
1110 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1111 {
1112 nir_tex_src src;
1113 src.src = nir_src_for_ssa(vtn_value(b, index, vtn_value_type_ssa)->ssa->def);
1114 src.src_type = type;
1115 return src;
1116 }
1117
1118 static void
1119 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1120 const uint32_t *w, unsigned count)
1121 {
1122 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1123 nir_deref_var *sampler = vtn_value(b, w[3], vtn_value_type_deref)->deref;
1124
1125 nir_tex_src srcs[8]; /* 8 should be enough */
1126 nir_tex_src *p = srcs;
1127
1128 unsigned coord_components = 0;
1129 switch (opcode) {
1130 case SpvOpTextureSample:
1131 case SpvOpTextureSampleDref:
1132 case SpvOpTextureSampleLod:
1133 case SpvOpTextureSampleProj:
1134 case SpvOpTextureSampleGrad:
1135 case SpvOpTextureSampleOffset:
1136 case SpvOpTextureSampleProjLod:
1137 case SpvOpTextureSampleProjGrad:
1138 case SpvOpTextureSampleLodOffset:
1139 case SpvOpTextureSampleProjOffset:
1140 case SpvOpTextureSampleGradOffset:
1141 case SpvOpTextureSampleProjLodOffset:
1142 case SpvOpTextureSampleProjGradOffset:
1143 case SpvOpTextureFetchTexelLod:
1144 case SpvOpTextureFetchTexelOffset:
1145 case SpvOpTextureFetchSample:
1146 case SpvOpTextureFetchTexel:
1147 case SpvOpTextureGather:
1148 case SpvOpTextureGatherOffset:
1149 case SpvOpTextureGatherOffsets:
1150 case SpvOpTextureQueryLod: {
1151 /* All these types have the coordinate as their first real argument */
1152 struct vtn_ssa_value *coord = vtn_ssa_value(b, w[4]);
1153 coord_components = glsl_get_vector_elements(coord->type);
1154 p->src = nir_src_for_ssa(coord->def);
1155 p->src_type = nir_tex_src_coord;
1156 p++;
1157 break;
1158 }
1159
1160 default:
1161 break;
1162 }
1163
1164 nir_texop texop;
1165 switch (opcode) {
1166 case SpvOpTextureSample:
1167 texop = nir_texop_tex;
1168
1169 if (count == 6) {
1170 texop = nir_texop_txb;
1171 *p++ = vtn_tex_src(b, w[5], nir_tex_src_bias);
1172 }
1173 break;
1174
1175 case SpvOpTextureSampleDref:
1176 case SpvOpTextureSampleLod:
1177 case SpvOpTextureSampleProj:
1178 case SpvOpTextureSampleGrad:
1179 case SpvOpTextureSampleOffset:
1180 case SpvOpTextureSampleProjLod:
1181 case SpvOpTextureSampleProjGrad:
1182 case SpvOpTextureSampleLodOffset:
1183 case SpvOpTextureSampleProjOffset:
1184 case SpvOpTextureSampleGradOffset:
1185 case SpvOpTextureSampleProjLodOffset:
1186 case SpvOpTextureSampleProjGradOffset:
1187 case SpvOpTextureFetchTexelLod:
1188 case SpvOpTextureFetchTexelOffset:
1189 case SpvOpTextureFetchSample:
1190 case SpvOpTextureFetchTexel:
1191 case SpvOpTextureGather:
1192 case SpvOpTextureGatherOffset:
1193 case SpvOpTextureGatherOffsets:
1194 case SpvOpTextureQuerySizeLod:
1195 case SpvOpTextureQuerySize:
1196 case SpvOpTextureQueryLod:
1197 case SpvOpTextureQueryLevels:
1198 case SpvOpTextureQuerySamples:
1199 default:
1200 unreachable("Unhandled opcode");
1201 }
1202
1203 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
1204
1205 const struct glsl_type *sampler_type = nir_deref_tail(&sampler->deref)->type;
1206 instr->sampler_dim = glsl_get_sampler_dim(sampler_type);
1207
1208 switch (glsl_get_sampler_result_type(sampler_type)) {
1209 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
1210 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
1211 case GLSL_TYPE_UINT: instr->dest_type = nir_type_unsigned; break;
1212 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
1213 default:
1214 unreachable("Invalid base type for sampler result");
1215 }
1216
1217 instr->op = texop;
1218 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
1219 instr->coord_components = coord_components;
1220 instr->is_array = glsl_sampler_type_is_array(sampler_type);
1221 instr->is_shadow = glsl_sampler_type_is_shadow(sampler_type);
1222
1223 instr->sampler = nir_deref_as_var(nir_copy_deref(instr, &sampler->deref));
1224
1225 nir_ssa_dest_init(&instr->instr, &instr->dest, 4, NULL);
1226 val->ssa = vtn_create_ssa_value(b, glsl_vector_type(GLSL_TYPE_FLOAT, 4));
1227 val->ssa->def = &instr->dest.ssa;
1228
1229 nir_builder_instr_insert(&b->nb, &instr->instr);
1230 }
1231
1232
1233 static nir_alu_instr *
1234 create_vec(void *mem_ctx, unsigned num_components)
1235 {
1236 nir_op op;
1237 switch (num_components) {
1238 case 1: op = nir_op_fmov; break;
1239 case 2: op = nir_op_vec2; break;
1240 case 3: op = nir_op_vec3; break;
1241 case 4: op = nir_op_vec4; break;
1242 default: unreachable("bad vector size");
1243 }
1244
1245 nir_alu_instr *vec = nir_alu_instr_create(mem_ctx, op);
1246 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
1247
1248 return vec;
1249 }
1250
1251 static struct vtn_ssa_value *
1252 vtn_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
1253 {
1254 if (src->transposed)
1255 return src->transposed;
1256
1257 struct vtn_ssa_value *dest =
1258 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
1259
1260 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
1261 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type));
1262 if (glsl_type_is_vector_or_scalar(src->type)) {
1263 vec->src[0].src = nir_src_for_ssa(src->def);
1264 vec->src[0].swizzle[0] = i;
1265 } else {
1266 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
1267 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
1268 vec->src[j].swizzle[0] = i;
1269 }
1270 }
1271 nir_builder_instr_insert(&b->nb, &vec->instr);
1272 dest->elems[i]->def = &vec->dest.dest.ssa;
1273 }
1274
1275 dest->transposed = src;
1276
1277 return dest;
1278 }
1279
1280 /*
1281 * Normally, column vectors in SPIR-V correspond to a single NIR SSA
1282 * definition. But for matrix multiplies, we want to do one routine for
1283 * multiplying a matrix by a matrix and then pretend that vectors are matrices
1284 * with one column. So we "wrap" these things, and unwrap the result before we
1285 * send it off.
1286 */
1287
1288 static struct vtn_ssa_value *
1289 vtn_wrap_matrix(struct vtn_builder *b, struct vtn_ssa_value *val)
1290 {
1291 if (val == NULL)
1292 return NULL;
1293
1294 if (glsl_type_is_matrix(val->type))
1295 return val;
1296
1297 struct vtn_ssa_value *dest = rzalloc(b, struct vtn_ssa_value);
1298 dest->type = val->type;
1299 dest->elems = ralloc_array(b, struct vtn_ssa_value *, 1);
1300 dest->elems[0] = val;
1301
1302 return dest;
1303 }
1304
1305 static struct vtn_ssa_value *
1306 vtn_unwrap_matrix(struct vtn_ssa_value *val)
1307 {
1308 if (glsl_type_is_matrix(val->type))
1309 return val;
1310
1311 return val->elems[0];
1312 }
1313
1314 static struct vtn_ssa_value *
1315 vtn_matrix_multiply(struct vtn_builder *b,
1316 struct vtn_ssa_value *_src0, struct vtn_ssa_value *_src1)
1317 {
1318
1319 struct vtn_ssa_value *src0 = vtn_wrap_matrix(b, _src0);
1320 struct vtn_ssa_value *src1 = vtn_wrap_matrix(b, _src1);
1321 struct vtn_ssa_value *src0_transpose = vtn_wrap_matrix(b, _src0->transposed);
1322 struct vtn_ssa_value *src1_transpose = vtn_wrap_matrix(b, _src1->transposed);
1323
1324 unsigned src0_rows = glsl_get_vector_elements(src0->type);
1325 unsigned src0_columns = glsl_get_matrix_columns(src0->type);
1326 unsigned src1_columns = glsl_get_matrix_columns(src1->type);
1327
1328 struct vtn_ssa_value *dest =
1329 vtn_create_ssa_value(b, glsl_matrix_type(glsl_get_base_type(src0->type),
1330 src0_rows, src1_columns));
1331
1332 dest = vtn_wrap_matrix(b, dest);
1333
1334 bool transpose_result = false;
1335 if (src0_transpose && src1_transpose) {
1336 /* transpose(A) * transpose(B) = transpose(B * A) */
1337 src1 = src0_transpose;
1338 src0 = src1_transpose;
1339 src0_transpose = NULL;
1340 src1_transpose = NULL;
1341 transpose_result = true;
1342 }
1343
1344 if (src0_transpose && !src1_transpose &&
1345 glsl_get_base_type(src0->type) == GLSL_TYPE_FLOAT) {
1346 /* We already have the rows of src0 and the columns of src1 available,
1347 * so we can just take the dot product of each row with each column to
1348 * get the result.
1349 */
1350
1351 for (unsigned i = 0; i < src1_columns; i++) {
1352 nir_alu_instr *vec = create_vec(b, src0_rows);
1353 for (unsigned j = 0; j < src0_rows; j++) {
1354 vec->src[j].src =
1355 nir_src_for_ssa(nir_fdot(&b->nb, src0_transpose->elems[j]->def,
1356 src1->elems[i]->def));
1357 }
1358
1359 nir_builder_instr_insert(&b->nb, &vec->instr);
1360 dest->elems[i]->def = &vec->dest.dest.ssa;
1361 }
1362 } else {
1363 /* We don't handle the case where src1 is transposed but not src0, since
1364 * the general case only uses individual components of src1 so the
1365 * optimizer should chew through the transpose we emitted for src1.
1366 */
1367
1368 for (unsigned i = 0; i < src1_columns; i++) {
1369 /* dest[i] = sum(src0[j] * src1[i][j] for all j) */
1370 dest->elems[i]->def =
1371 nir_fmul(&b->nb, src0->elems[0]->def,
1372 vtn_vector_extract(b, src1->elems[i]->def, 0));
1373 for (unsigned j = 1; j < src0_columns; j++) {
1374 dest->elems[i]->def =
1375 nir_fadd(&b->nb, dest->elems[i]->def,
1376 nir_fmul(&b->nb, src0->elems[j]->def,
1377 vtn_vector_extract(b,
1378 src1->elems[i]->def, j)));
1379 }
1380 }
1381 }
1382
1383 dest = vtn_unwrap_matrix(dest);
1384
1385 if (transpose_result)
1386 dest = vtn_transpose(b, dest);
1387
1388 return dest;
1389 }
1390
1391 static struct vtn_ssa_value *
1392 vtn_mat_times_scalar(struct vtn_builder *b,
1393 struct vtn_ssa_value *mat,
1394 nir_ssa_def *scalar)
1395 {
1396 struct vtn_ssa_value *dest = vtn_create_ssa_value(b, mat->type);
1397 for (unsigned i = 0; i < glsl_get_matrix_columns(mat->type); i++) {
1398 if (glsl_get_base_type(mat->type) == GLSL_TYPE_FLOAT)
1399 dest->elems[i]->def = nir_fmul(&b->nb, mat->elems[i]->def, scalar);
1400 else
1401 dest->elems[i]->def = nir_imul(&b->nb, mat->elems[i]->def, scalar);
1402 }
1403
1404 return dest;
1405 }
1406
1407 static void
1408 vtn_handle_matrix_alu(struct vtn_builder *b, SpvOp opcode,
1409 const uint32_t *w, unsigned count)
1410 {
1411 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1412
1413 switch (opcode) {
1414 case SpvOpTranspose: {
1415 struct vtn_ssa_value *src = vtn_ssa_value(b, w[3]);
1416 val->ssa = vtn_transpose(b, src);
1417 break;
1418 }
1419
1420 case SpvOpOuterProduct: {
1421 struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
1422 struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
1423
1424 val->ssa = vtn_matrix_multiply(b, src0, vtn_transpose(b, src1));
1425 break;
1426 }
1427
1428 case SpvOpMatrixTimesScalar: {
1429 struct vtn_ssa_value *mat = vtn_ssa_value(b, w[3]);
1430 struct vtn_ssa_value *scalar = vtn_ssa_value(b, w[4]);
1431
1432 if (mat->transposed) {
1433 val->ssa = vtn_transpose(b, vtn_mat_times_scalar(b, mat->transposed,
1434 scalar->def));
1435 } else {
1436 val->ssa = vtn_mat_times_scalar(b, mat, scalar->def);
1437 }
1438 break;
1439 }
1440
1441 case SpvOpVectorTimesMatrix:
1442 case SpvOpMatrixTimesVector:
1443 case SpvOpMatrixTimesMatrix: {
1444 struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
1445 struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
1446
1447 val->ssa = vtn_matrix_multiply(b, src0, src1);
1448 break;
1449 }
1450
1451 default: unreachable("unknown matrix opcode");
1452 }
1453 }
1454
1455 static void
1456 vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
1457 const uint32_t *w, unsigned count)
1458 {
1459 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1460 const struct glsl_type *type =
1461 vtn_value(b, w[1], vtn_value_type_type)->type->type;
1462 val->ssa = vtn_create_ssa_value(b, type);
1463
1464 /* Collect the various SSA sources */
1465 unsigned num_inputs = count - 3;
1466 nir_ssa_def *src[4];
1467 for (unsigned i = 0; i < num_inputs; i++)
1468 src[i] = vtn_ssa_value(b, w[i + 3])->def;
1469
1470 /* Indicates that the first two arguments should be swapped. This is
1471 * used for implementing greater-than and less-than-or-equal.
1472 */
1473 bool swap = false;
1474
1475 nir_op op;
1476 switch (opcode) {
1477 /* Basic ALU operations */
1478 case SpvOpSNegate: op = nir_op_ineg; break;
1479 case SpvOpFNegate: op = nir_op_fneg; break;
1480 case SpvOpNot: op = nir_op_inot; break;
1481
1482 case SpvOpAny:
1483 switch (src[0]->num_components) {
1484 case 1: op = nir_op_imov; break;
1485 case 2: op = nir_op_bany2; break;
1486 case 3: op = nir_op_bany3; break;
1487 case 4: op = nir_op_bany4; break;
1488 }
1489 break;
1490
1491 case SpvOpAll:
1492 switch (src[0]->num_components) {
1493 case 1: op = nir_op_imov; break;
1494 case 2: op = nir_op_ball2; break;
1495 case 3: op = nir_op_ball3; break;
1496 case 4: op = nir_op_ball4; break;
1497 }
1498 break;
1499
1500 case SpvOpIAdd: op = nir_op_iadd; break;
1501 case SpvOpFAdd: op = nir_op_fadd; break;
1502 case SpvOpISub: op = nir_op_isub; break;
1503 case SpvOpFSub: op = nir_op_fsub; break;
1504 case SpvOpIMul: op = nir_op_imul; break;
1505 case SpvOpFMul: op = nir_op_fmul; break;
1506 case SpvOpUDiv: op = nir_op_udiv; break;
1507 case SpvOpSDiv: op = nir_op_idiv; break;
1508 case SpvOpFDiv: op = nir_op_fdiv; break;
1509 case SpvOpUMod: op = nir_op_umod; break;
1510 case SpvOpSMod: op = nir_op_umod; break; /* FIXME? */
1511 case SpvOpFMod: op = nir_op_fmod; break;
1512
1513 case SpvOpDot:
1514 assert(src[0]->num_components == src[1]->num_components);
1515 switch (src[0]->num_components) {
1516 case 1: op = nir_op_fmul; break;
1517 case 2: op = nir_op_fdot2; break;
1518 case 3: op = nir_op_fdot3; break;
1519 case 4: op = nir_op_fdot4; break;
1520 }
1521 break;
1522
1523 case SpvOpShiftRightLogical: op = nir_op_ushr; break;
1524 case SpvOpShiftRightArithmetic: op = nir_op_ishr; break;
1525 case SpvOpShiftLeftLogical: op = nir_op_ishl; break;
1526 case SpvOpLogicalOr: op = nir_op_ior; break;
1527 case SpvOpLogicalXor: op = nir_op_ixor; break;
1528 case SpvOpLogicalAnd: op = nir_op_iand; break;
1529 case SpvOpBitwiseOr: op = nir_op_ior; break;
1530 case SpvOpBitwiseXor: op = nir_op_ixor; break;
1531 case SpvOpBitwiseAnd: op = nir_op_iand; break;
1532 case SpvOpSelect: op = nir_op_bcsel; break;
1533 case SpvOpIEqual: op = nir_op_ieq; break;
1534
1535 /* Comparisons: (TODO: How do we want to handled ordered/unordered?) */
1536 case SpvOpFOrdEqual: op = nir_op_feq; break;
1537 case SpvOpFUnordEqual: op = nir_op_feq; break;
1538 case SpvOpINotEqual: op = nir_op_ine; break;
1539 case SpvOpFOrdNotEqual: op = nir_op_fne; break;
1540 case SpvOpFUnordNotEqual: op = nir_op_fne; break;
1541 case SpvOpULessThan: op = nir_op_ult; break;
1542 case SpvOpSLessThan: op = nir_op_ilt; break;
1543 case SpvOpFOrdLessThan: op = nir_op_flt; break;
1544 case SpvOpFUnordLessThan: op = nir_op_flt; break;
1545 case SpvOpUGreaterThan: op = nir_op_ult; swap = true; break;
1546 case SpvOpSGreaterThan: op = nir_op_ilt; swap = true; break;
1547 case SpvOpFOrdGreaterThan: op = nir_op_flt; swap = true; break;
1548 case SpvOpFUnordGreaterThan: op = nir_op_flt; swap = true; break;
1549 case SpvOpULessThanEqual: op = nir_op_uge; swap = true; break;
1550 case SpvOpSLessThanEqual: op = nir_op_ige; swap = true; break;
1551 case SpvOpFOrdLessThanEqual: op = nir_op_fge; swap = true; break;
1552 case SpvOpFUnordLessThanEqual: op = nir_op_fge; swap = true; break;
1553 case SpvOpUGreaterThanEqual: op = nir_op_uge; break;
1554 case SpvOpSGreaterThanEqual: op = nir_op_ige; break;
1555 case SpvOpFOrdGreaterThanEqual: op = nir_op_fge; break;
1556 case SpvOpFUnordGreaterThanEqual:op = nir_op_fge; break;
1557
1558 /* Conversions: */
1559 case SpvOpConvertFToU: op = nir_op_f2u; break;
1560 case SpvOpConvertFToS: op = nir_op_f2i; break;
1561 case SpvOpConvertSToF: op = nir_op_i2f; break;
1562 case SpvOpConvertUToF: op = nir_op_u2f; break;
1563 case SpvOpBitcast: op = nir_op_imov; break;
1564 case SpvOpUConvert:
1565 case SpvOpSConvert:
1566 op = nir_op_imov; /* TODO: NIR is 32-bit only; these are no-ops. */
1567 break;
1568 case SpvOpFConvert:
1569 op = nir_op_fmov;
1570 break;
1571
1572 /* Derivatives: */
1573 case SpvOpDPdx: op = nir_op_fddx; break;
1574 case SpvOpDPdy: op = nir_op_fddy; break;
1575 case SpvOpDPdxFine: op = nir_op_fddx_fine; break;
1576 case SpvOpDPdyFine: op = nir_op_fddy_fine; break;
1577 case SpvOpDPdxCoarse: op = nir_op_fddx_coarse; break;
1578 case SpvOpDPdyCoarse: op = nir_op_fddy_coarse; break;
1579 case SpvOpFwidth:
1580 val->ssa->def = nir_fadd(&b->nb,
1581 nir_fabs(&b->nb, nir_fddx(&b->nb, src[0])),
1582 nir_fabs(&b->nb, nir_fddx(&b->nb, src[1])));
1583 return;
1584 case SpvOpFwidthFine:
1585 val->ssa->def = nir_fadd(&b->nb,
1586 nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[0])),
1587 nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[1])));
1588 return;
1589 case SpvOpFwidthCoarse:
1590 val->ssa->def = nir_fadd(&b->nb,
1591 nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[0])),
1592 nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[1])));
1593 return;
1594
1595 case SpvOpVectorTimesScalar:
1596 /* The builder will take care of splatting for us. */
1597 val->ssa->def = nir_fmul(&b->nb, src[0], src[1]);
1598 return;
1599
1600 case SpvOpSRem:
1601 case SpvOpFRem:
1602 unreachable("No NIR equivalent");
1603
1604 case SpvOpIsNan:
1605 case SpvOpIsInf:
1606 case SpvOpIsFinite:
1607 case SpvOpIsNormal:
1608 case SpvOpSignBitSet:
1609 case SpvOpLessOrGreater:
1610 case SpvOpOrdered:
1611 case SpvOpUnordered:
1612 default:
1613 unreachable("Unhandled opcode");
1614 }
1615
1616 if (swap) {
1617 nir_ssa_def *tmp = src[0];
1618 src[0] = src[1];
1619 src[1] = tmp;
1620 }
1621
1622 nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
1623 nir_ssa_dest_init(&instr->instr, &instr->dest.dest,
1624 glsl_get_vector_elements(type), val->name);
1625 val->ssa->def = &instr->dest.dest.ssa;
1626
1627 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
1628 instr->src[i].src = nir_src_for_ssa(src[i]);
1629
1630 nir_builder_instr_insert(&b->nb, &instr->instr);
1631 }
1632
1633 static nir_ssa_def *
1634 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
1635 {
1636 unsigned swiz[4] = { index };
1637 return nir_swizzle(&b->nb, src, swiz, 1, true);
1638 }
1639
1640
1641 static nir_ssa_def *
1642 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
1643 unsigned index)
1644 {
1645 nir_alu_instr *vec = create_vec(b->shader, src->num_components);
1646
1647 for (unsigned i = 0; i < src->num_components; i++) {
1648 if (i == index) {
1649 vec->src[i].src = nir_src_for_ssa(insert);
1650 } else {
1651 vec->src[i].src = nir_src_for_ssa(src);
1652 vec->src[i].swizzle[0] = i;
1653 }
1654 }
1655
1656 nir_builder_instr_insert(&b->nb, &vec->instr);
1657
1658 return &vec->dest.dest.ssa;
1659 }
1660
1661 static nir_ssa_def *
1662 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
1663 nir_ssa_def *index)
1664 {
1665 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
1666 for (unsigned i = 1; i < src->num_components; i++)
1667 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
1668 vtn_vector_extract(b, src, i), dest);
1669
1670 return dest;
1671 }
1672
1673 static nir_ssa_def *
1674 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
1675 nir_ssa_def *insert, nir_ssa_def *index)
1676 {
1677 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
1678 for (unsigned i = 1; i < src->num_components; i++)
1679 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
1680 vtn_vector_insert(b, src, insert, i), dest);
1681
1682 return dest;
1683 }
1684
1685 static nir_ssa_def *
1686 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
1687 nir_ssa_def *src0, nir_ssa_def *src1,
1688 const uint32_t *indices)
1689 {
1690 nir_alu_instr *vec = create_vec(b->shader, num_components);
1691
1692 nir_ssa_undef_instr *undef = nir_ssa_undef_instr_create(b->shader, 1);
1693 nir_builder_instr_insert(&b->nb, &undef->instr);
1694
1695 for (unsigned i = 0; i < num_components; i++) {
1696 uint32_t index = indices[i];
1697 if (index == 0xffffffff) {
1698 vec->src[i].src = nir_src_for_ssa(&undef->def);
1699 } else if (index < src0->num_components) {
1700 vec->src[i].src = nir_src_for_ssa(src0);
1701 vec->src[i].swizzle[0] = index;
1702 } else {
1703 vec->src[i].src = nir_src_for_ssa(src1);
1704 vec->src[i].swizzle[0] = index - src0->num_components;
1705 }
1706 }
1707
1708 nir_builder_instr_insert(&b->nb, &vec->instr);
1709
1710 return &vec->dest.dest.ssa;
1711 }
1712
1713 /*
1714 * Concatentates a number of vectors/scalars together to produce a vector
1715 */
1716 static nir_ssa_def *
1717 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
1718 unsigned num_srcs, nir_ssa_def **srcs)
1719 {
1720 nir_alu_instr *vec = create_vec(b->shader, num_components);
1721
1722 unsigned dest_idx = 0;
1723 for (unsigned i = 0; i < num_srcs; i++) {
1724 nir_ssa_def *src = srcs[i];
1725 for (unsigned j = 0; j < src->num_components; j++) {
1726 vec->src[dest_idx].src = nir_src_for_ssa(src);
1727 vec->src[dest_idx].swizzle[0] = j;
1728 dest_idx++;
1729 }
1730 }
1731
1732 nir_builder_instr_insert(&b->nb, &vec->instr);
1733
1734 return &vec->dest.dest.ssa;
1735 }
1736
1737 static struct vtn_ssa_value *
1738 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
1739 {
1740 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
1741 dest->type = src->type;
1742
1743 if (glsl_type_is_vector_or_scalar(src->type)) {
1744 dest->def = src->def;
1745 } else {
1746 unsigned elems = glsl_get_length(src->type);
1747
1748 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
1749 for (unsigned i = 0; i < elems; i++)
1750 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
1751 }
1752
1753 return dest;
1754 }
1755
1756 static struct vtn_ssa_value *
1757 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
1758 struct vtn_ssa_value *insert, const uint32_t *indices,
1759 unsigned num_indices)
1760 {
1761 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
1762
1763 struct vtn_ssa_value *cur = dest;
1764 unsigned i;
1765 for (i = 0; i < num_indices - 1; i++) {
1766 cur = cur->elems[indices[i]];
1767 }
1768
1769 if (glsl_type_is_vector_or_scalar(cur->type)) {
1770 /* According to the SPIR-V spec, OpCompositeInsert may work down to
1771 * the component granularity. In that case, the last index will be
1772 * the index to insert the scalar into the vector.
1773 */
1774
1775 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
1776 } else {
1777 cur->elems[indices[i]] = insert;
1778 }
1779
1780 return dest;
1781 }
1782
1783 static struct vtn_ssa_value *
1784 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
1785 const uint32_t *indices, unsigned num_indices)
1786 {
1787 struct vtn_ssa_value *cur = src;
1788 for (unsigned i = 0; i < num_indices; i++) {
1789 if (glsl_type_is_vector_or_scalar(cur->type)) {
1790 assert(i == num_indices - 1);
1791 /* According to the SPIR-V spec, OpCompositeExtract may work down to
1792 * the component granularity. The last index will be the index of the
1793 * vector to extract.
1794 */
1795
1796 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
1797 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
1798 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
1799 return ret;
1800 }
1801 }
1802
1803 return cur;
1804 }
1805
1806 static void
1807 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
1808 const uint32_t *w, unsigned count)
1809 {
1810 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1811 const struct glsl_type *type =
1812 vtn_value(b, w[1], vtn_value_type_type)->type->type;
1813 val->ssa = vtn_create_ssa_value(b, type);
1814
1815 switch (opcode) {
1816 case SpvOpVectorExtractDynamic:
1817 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
1818 vtn_ssa_value(b, w[4])->def);
1819 break;
1820
1821 case SpvOpVectorInsertDynamic:
1822 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
1823 vtn_ssa_value(b, w[4])->def,
1824 vtn_ssa_value(b, w[5])->def);
1825 break;
1826
1827 case SpvOpVectorShuffle:
1828 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
1829 vtn_ssa_value(b, w[3])->def,
1830 vtn_ssa_value(b, w[4])->def,
1831 w + 5);
1832 break;
1833
1834 case SpvOpCompositeConstruct: {
1835 val->ssa = rzalloc(b, struct vtn_ssa_value);
1836 unsigned elems = count - 3;
1837 if (glsl_type_is_vector_or_scalar(type)) {
1838 nir_ssa_def *srcs[4];
1839 for (unsigned i = 0; i < elems; i++)
1840 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
1841 val->ssa->def =
1842 vtn_vector_construct(b, glsl_get_vector_elements(type),
1843 elems, srcs);
1844 } else {
1845 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1846 for (unsigned i = 0; i < elems; i++)
1847 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
1848 }
1849 break;
1850 }
1851 case SpvOpCompositeExtract:
1852 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
1853 w + 4, count - 4);
1854 break;
1855
1856 case SpvOpCompositeInsert:
1857 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
1858 vtn_ssa_value(b, w[3]),
1859 w + 5, count - 5);
1860 break;
1861
1862 case SpvOpCopyObject:
1863 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
1864 break;
1865
1866 default:
1867 unreachable("unknown composite operation");
1868 }
1869 }
1870
1871 static void
1872 vtn_phi_node_init(struct vtn_builder *b, struct vtn_ssa_value *val)
1873 {
1874 if (glsl_type_is_vector_or_scalar(val->type)) {
1875 nir_phi_instr *phi = nir_phi_instr_create(b->shader);
1876 nir_ssa_dest_init(&phi->instr, &phi->dest,
1877 glsl_get_vector_elements(val->type), NULL);
1878 exec_list_make_empty(&phi->srcs);
1879 nir_builder_instr_insert(&b->nb, &phi->instr);
1880 val->def = &phi->dest.ssa;
1881 } else {
1882 unsigned elems = glsl_get_length(val->type);
1883 for (unsigned i = 0; i < elems; i++)
1884 vtn_phi_node_init(b, val->elems[i]);
1885 }
1886 }
1887
1888 static struct vtn_ssa_value *
1889 vtn_phi_node_create(struct vtn_builder *b, const struct glsl_type *type)
1890 {
1891 struct vtn_ssa_value *val = vtn_create_ssa_value(b, type);
1892 vtn_phi_node_init(b, val);
1893 return val;
1894 }
1895
1896 static void
1897 vtn_handle_phi_first_pass(struct vtn_builder *b, const uint32_t *w)
1898 {
1899 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1900 const struct glsl_type *type =
1901 vtn_value(b, w[1], vtn_value_type_type)->type->type;
1902 val->ssa = vtn_phi_node_create(b, type);
1903 }
1904
1905 static void
1906 vtn_phi_node_add_src(struct vtn_ssa_value *phi, const nir_block *pred,
1907 struct vtn_ssa_value *val)
1908 {
1909 assert(phi->type == val->type);
1910 if (glsl_type_is_vector_or_scalar(phi->type)) {
1911 nir_phi_instr *phi_instr = nir_instr_as_phi(phi->def->parent_instr);
1912 nir_phi_src *src = ralloc(phi_instr, nir_phi_src);
1913 src->pred = (nir_block *) pred;
1914 src->src = nir_src_for_ssa(val->def);
1915 exec_list_push_tail(&phi_instr->srcs, &src->node);
1916 } else {
1917 unsigned elems = glsl_get_length(phi->type);
1918 for (unsigned i = 0; i < elems; i++)
1919 vtn_phi_node_add_src(phi->elems[i], pred, val->elems[i]);
1920 }
1921 }
1922
1923 static struct vtn_ssa_value *
1924 vtn_get_phi_node_src(struct vtn_builder *b, nir_block *block,
1925 const struct glsl_type *type, const uint32_t *w,
1926 unsigned count)
1927 {
1928 struct hash_entry *entry = _mesa_hash_table_search(b->block_table, block);
1929 if (entry) {
1930 struct vtn_block *spv_block = entry->data;
1931 for (unsigned off = 4; off < count; off += 2) {
1932 if (spv_block == vtn_value(b, w[off], vtn_value_type_block)->block) {
1933 return vtn_ssa_value(b, w[off - 1]);
1934 }
1935 }
1936 }
1937
1938 nir_builder_insert_before_block(&b->nb, block);
1939 struct vtn_ssa_value *phi = vtn_phi_node_create(b, type);
1940
1941 struct set_entry *entry2;
1942 set_foreach(block->predecessors, entry2) {
1943 nir_block *pred = (nir_block *) entry2->key;
1944 struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, type, w,
1945 count);
1946 vtn_phi_node_add_src(phi, pred, val);
1947 }
1948
1949 return phi;
1950 }
1951
1952 static bool
1953 vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
1954 const uint32_t *w, unsigned count)
1955 {
1956 if (opcode == SpvOpLabel) {
1957 b->block = vtn_value(b, w[1], vtn_value_type_block)->block;
1958 return true;
1959 }
1960
1961 if (opcode != SpvOpPhi)
1962 return true;
1963
1964 struct vtn_ssa_value *phi = vtn_value(b, w[2], vtn_value_type_ssa)->ssa;
1965
1966 struct set_entry *entry;
1967 set_foreach(b->block->block->predecessors, entry) {
1968 nir_block *pred = (nir_block *) entry->key;
1969
1970 struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, phi->type, w,
1971 count);
1972 vtn_phi_node_add_src(phi, pred, val);
1973 }
1974
1975 return true;
1976 }
1977
1978 static bool
1979 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
1980 const uint32_t *w, unsigned count)
1981 {
1982 switch (opcode) {
1983 case SpvOpSource:
1984 case SpvOpSourceExtension:
1985 case SpvOpCompileFlag:
1986 case SpvOpExtension:
1987 /* Unhandled, but these are for debug so that's ok. */
1988 break;
1989
1990 case SpvOpExtInstImport:
1991 vtn_handle_extension(b, opcode, w, count);
1992 break;
1993
1994 case SpvOpMemoryModel:
1995 assert(w[1] == SpvAddressingModelLogical);
1996 assert(w[2] == SpvMemoryModelGLSL450);
1997 break;
1998
1999 case SpvOpEntryPoint:
2000 assert(b->entry_point == NULL);
2001 b->entry_point = &b->values[w[2]];
2002 b->execution_model = w[1];
2003 break;
2004
2005 case SpvOpExecutionMode:
2006 unreachable("Execution modes not yet implemented");
2007 break;
2008
2009 case SpvOpString:
2010 vtn_push_value(b, w[1], vtn_value_type_string)->str =
2011 vtn_string_literal(b, &w[2], count - 2);
2012 break;
2013
2014 case SpvOpName:
2015 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2);
2016 break;
2017
2018 case SpvOpMemberName:
2019 /* TODO */
2020 break;
2021
2022 case SpvOpLine:
2023 break; /* Ignored for now */
2024
2025 case SpvOpDecorationGroup:
2026 case SpvOpDecorate:
2027 case SpvOpMemberDecorate:
2028 case SpvOpGroupDecorate:
2029 case SpvOpGroupMemberDecorate:
2030 vtn_handle_decoration(b, opcode, w, count);
2031 break;
2032
2033 case SpvOpTypeVoid:
2034 case SpvOpTypeBool:
2035 case SpvOpTypeInt:
2036 case SpvOpTypeFloat:
2037 case SpvOpTypeVector:
2038 case SpvOpTypeMatrix:
2039 case SpvOpTypeSampler:
2040 case SpvOpTypeArray:
2041 case SpvOpTypeRuntimeArray:
2042 case SpvOpTypeStruct:
2043 case SpvOpTypeOpaque:
2044 case SpvOpTypePointer:
2045 case SpvOpTypeFunction:
2046 case SpvOpTypeEvent:
2047 case SpvOpTypeDeviceEvent:
2048 case SpvOpTypeReserveId:
2049 case SpvOpTypeQueue:
2050 case SpvOpTypePipe:
2051 vtn_handle_type(b, opcode, w, count);
2052 break;
2053
2054 case SpvOpConstantTrue:
2055 case SpvOpConstantFalse:
2056 case SpvOpConstant:
2057 case SpvOpConstantComposite:
2058 case SpvOpConstantSampler:
2059 case SpvOpConstantNullPointer:
2060 case SpvOpConstantNullObject:
2061 case SpvOpSpecConstantTrue:
2062 case SpvOpSpecConstantFalse:
2063 case SpvOpSpecConstant:
2064 case SpvOpSpecConstantComposite:
2065 vtn_handle_constant(b, opcode, w, count);
2066 break;
2067
2068 case SpvOpVariable:
2069 vtn_handle_variables(b, opcode, w, count);
2070 break;
2071
2072 default:
2073 return false; /* End of preamble */
2074 }
2075
2076 return true;
2077 }
2078
2079 static bool
2080 vtn_handle_first_cfg_pass_instruction(struct vtn_builder *b, SpvOp opcode,
2081 const uint32_t *w, unsigned count)
2082 {
2083 switch (opcode) {
2084 case SpvOpFunction: {
2085 assert(b->func == NULL);
2086 b->func = rzalloc(b, struct vtn_function);
2087
2088 const struct glsl_type *result_type =
2089 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2090 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_function);
2091 const struct glsl_type *func_type =
2092 vtn_value(b, w[4], vtn_value_type_type)->type->type;
2093
2094 assert(glsl_get_function_return_type(func_type) == result_type);
2095
2096 nir_function *func =
2097 nir_function_create(b->shader, ralloc_strdup(b->shader, val->name));
2098
2099 nir_function_overload *overload = nir_function_overload_create(func);
2100 overload->num_params = glsl_get_length(func_type);
2101 overload->params = ralloc_array(overload, nir_parameter,
2102 overload->num_params);
2103 for (unsigned i = 0; i < overload->num_params; i++) {
2104 const struct glsl_function_param *param =
2105 glsl_get_function_param(func_type, i);
2106 overload->params[i].type = param->type;
2107 if (param->in) {
2108 if (param->out) {
2109 overload->params[i].param_type = nir_parameter_inout;
2110 } else {
2111 overload->params[i].param_type = nir_parameter_in;
2112 }
2113 } else {
2114 if (param->out) {
2115 overload->params[i].param_type = nir_parameter_out;
2116 } else {
2117 assert(!"Parameter is neither in nor out");
2118 }
2119 }
2120 }
2121 b->func->overload = overload;
2122 break;
2123 }
2124
2125 case SpvOpFunctionEnd:
2126 b->func->end = w;
2127 b->func = NULL;
2128 break;
2129
2130 case SpvOpFunctionParameter:
2131 break; /* Does nothing */
2132
2133 case SpvOpLabel: {
2134 assert(b->block == NULL);
2135 b->block = rzalloc(b, struct vtn_block);
2136 b->block->label = w;
2137 vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
2138
2139 if (b->func->start_block == NULL) {
2140 /* This is the first block encountered for this function. In this
2141 * case, we set the start block and add it to the list of
2142 * implemented functions that we'll walk later.
2143 */
2144 b->func->start_block = b->block;
2145 exec_list_push_tail(&b->functions, &b->func->node);
2146 }
2147 break;
2148 }
2149
2150 case SpvOpBranch:
2151 case SpvOpBranchConditional:
2152 case SpvOpSwitch:
2153 case SpvOpKill:
2154 case SpvOpReturn:
2155 case SpvOpReturnValue:
2156 case SpvOpUnreachable:
2157 assert(b->block);
2158 b->block->branch = w;
2159 b->block = NULL;
2160 break;
2161
2162 case SpvOpSelectionMerge:
2163 case SpvOpLoopMerge:
2164 assert(b->block && b->block->merge_op == SpvOpNop);
2165 b->block->merge_op = opcode;
2166 b->block->merge_block_id = w[1];
2167 break;
2168
2169 default:
2170 /* Continue on as per normal */
2171 return true;
2172 }
2173
2174 return true;
2175 }
2176
2177 static bool
2178 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
2179 const uint32_t *w, unsigned count)
2180 {
2181 switch (opcode) {
2182 case SpvOpLabel: {
2183 struct vtn_block *block = vtn_value(b, w[1], vtn_value_type_block)->block;
2184 assert(block->block == NULL);
2185
2186 struct exec_node *list_tail = exec_list_get_tail(b->nb.cf_node_list);
2187 nir_cf_node *tail_node = exec_node_data(nir_cf_node, list_tail, node);
2188 assert(tail_node->type == nir_cf_node_block);
2189 block->block = nir_cf_node_as_block(tail_node);
2190 break;
2191 }
2192
2193 case SpvOpLoopMerge:
2194 case SpvOpSelectionMerge:
2195 /* This is handled by cfg pre-pass and walk_blocks */
2196 break;
2197
2198 case SpvOpUndef:
2199 vtn_push_value(b, w[2], vtn_value_type_undef);
2200 break;
2201
2202 case SpvOpExtInst:
2203 vtn_handle_extension(b, opcode, w, count);
2204 break;
2205
2206 case SpvOpVariable:
2207 case SpvOpVariableArray:
2208 case SpvOpLoad:
2209 case SpvOpStore:
2210 case SpvOpCopyMemory:
2211 case SpvOpCopyMemorySized:
2212 case SpvOpAccessChain:
2213 case SpvOpInBoundsAccessChain:
2214 case SpvOpArrayLength:
2215 case SpvOpImagePointer:
2216 vtn_handle_variables(b, opcode, w, count);
2217 break;
2218
2219 case SpvOpFunctionCall:
2220 vtn_handle_function_call(b, opcode, w, count);
2221 break;
2222
2223 case SpvOpTextureSample:
2224 case SpvOpTextureSampleDref:
2225 case SpvOpTextureSampleLod:
2226 case SpvOpTextureSampleProj:
2227 case SpvOpTextureSampleGrad:
2228 case SpvOpTextureSampleOffset:
2229 case SpvOpTextureSampleProjLod:
2230 case SpvOpTextureSampleProjGrad:
2231 case SpvOpTextureSampleLodOffset:
2232 case SpvOpTextureSampleProjOffset:
2233 case SpvOpTextureSampleGradOffset:
2234 case SpvOpTextureSampleProjLodOffset:
2235 case SpvOpTextureSampleProjGradOffset:
2236 case SpvOpTextureFetchTexelLod:
2237 case SpvOpTextureFetchTexelOffset:
2238 case SpvOpTextureFetchSample:
2239 case SpvOpTextureFetchTexel:
2240 case SpvOpTextureGather:
2241 case SpvOpTextureGatherOffset:
2242 case SpvOpTextureGatherOffsets:
2243 case SpvOpTextureQuerySizeLod:
2244 case SpvOpTextureQuerySize:
2245 case SpvOpTextureQueryLod:
2246 case SpvOpTextureQueryLevels:
2247 case SpvOpTextureQuerySamples:
2248 vtn_handle_texture(b, opcode, w, count);
2249 break;
2250
2251 case SpvOpSNegate:
2252 case SpvOpFNegate:
2253 case SpvOpNot:
2254 case SpvOpAny:
2255 case SpvOpAll:
2256 case SpvOpConvertFToU:
2257 case SpvOpConvertFToS:
2258 case SpvOpConvertSToF:
2259 case SpvOpConvertUToF:
2260 case SpvOpUConvert:
2261 case SpvOpSConvert:
2262 case SpvOpFConvert:
2263 case SpvOpConvertPtrToU:
2264 case SpvOpConvertUToPtr:
2265 case SpvOpPtrCastToGeneric:
2266 case SpvOpGenericCastToPtr:
2267 case SpvOpBitcast:
2268 case SpvOpIsNan:
2269 case SpvOpIsInf:
2270 case SpvOpIsFinite:
2271 case SpvOpIsNormal:
2272 case SpvOpSignBitSet:
2273 case SpvOpLessOrGreater:
2274 case SpvOpOrdered:
2275 case SpvOpUnordered:
2276 case SpvOpIAdd:
2277 case SpvOpFAdd:
2278 case SpvOpISub:
2279 case SpvOpFSub:
2280 case SpvOpIMul:
2281 case SpvOpFMul:
2282 case SpvOpUDiv:
2283 case SpvOpSDiv:
2284 case SpvOpFDiv:
2285 case SpvOpUMod:
2286 case SpvOpSRem:
2287 case SpvOpSMod:
2288 case SpvOpFRem:
2289 case SpvOpFMod:
2290 case SpvOpVectorTimesScalar:
2291 case SpvOpDot:
2292 case SpvOpShiftRightLogical:
2293 case SpvOpShiftRightArithmetic:
2294 case SpvOpShiftLeftLogical:
2295 case SpvOpLogicalOr:
2296 case SpvOpLogicalXor:
2297 case SpvOpLogicalAnd:
2298 case SpvOpBitwiseOr:
2299 case SpvOpBitwiseXor:
2300 case SpvOpBitwiseAnd:
2301 case SpvOpSelect:
2302 case SpvOpIEqual:
2303 case SpvOpFOrdEqual:
2304 case SpvOpFUnordEqual:
2305 case SpvOpINotEqual:
2306 case SpvOpFOrdNotEqual:
2307 case SpvOpFUnordNotEqual:
2308 case SpvOpULessThan:
2309 case SpvOpSLessThan:
2310 case SpvOpFOrdLessThan:
2311 case SpvOpFUnordLessThan:
2312 case SpvOpUGreaterThan:
2313 case SpvOpSGreaterThan:
2314 case SpvOpFOrdGreaterThan:
2315 case SpvOpFUnordGreaterThan:
2316 case SpvOpULessThanEqual:
2317 case SpvOpSLessThanEqual:
2318 case SpvOpFOrdLessThanEqual:
2319 case SpvOpFUnordLessThanEqual:
2320 case SpvOpUGreaterThanEqual:
2321 case SpvOpSGreaterThanEqual:
2322 case SpvOpFOrdGreaterThanEqual:
2323 case SpvOpFUnordGreaterThanEqual:
2324 case SpvOpDPdx:
2325 case SpvOpDPdy:
2326 case SpvOpFwidth:
2327 case SpvOpDPdxFine:
2328 case SpvOpDPdyFine:
2329 case SpvOpFwidthFine:
2330 case SpvOpDPdxCoarse:
2331 case SpvOpDPdyCoarse:
2332 case SpvOpFwidthCoarse:
2333 vtn_handle_alu(b, opcode, w, count);
2334 break;
2335
2336 case SpvOpTranspose:
2337 case SpvOpOuterProduct:
2338 case SpvOpMatrixTimesScalar:
2339 case SpvOpVectorTimesMatrix:
2340 case SpvOpMatrixTimesVector:
2341 case SpvOpMatrixTimesMatrix:
2342 vtn_handle_matrix_alu(b, opcode, w, count);
2343 break;
2344
2345 case SpvOpVectorExtractDynamic:
2346 case SpvOpVectorInsertDynamic:
2347 case SpvOpVectorShuffle:
2348 case SpvOpCompositeConstruct:
2349 case SpvOpCompositeExtract:
2350 case SpvOpCompositeInsert:
2351 case SpvOpCopyObject:
2352 vtn_handle_composite(b, opcode, w, count);
2353 break;
2354
2355 case SpvOpPhi:
2356 vtn_handle_phi_first_pass(b, w);
2357 break;
2358
2359 default:
2360 unreachable("Unhandled opcode");
2361 }
2362
2363 return true;
2364 }
2365
2366 static void
2367 vtn_walk_blocks(struct vtn_builder *b, struct vtn_block *start,
2368 struct vtn_block *break_block, struct vtn_block *cont_block,
2369 struct vtn_block *end_block)
2370 {
2371 struct vtn_block *block = start;
2372 while (block != end_block) {
2373 if (block->merge_op == SpvOpLoopMerge) {
2374 /* This is the jump into a loop. */
2375 struct vtn_block *new_cont_block = block;
2376 struct vtn_block *new_break_block =
2377 vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
2378
2379 nir_loop *loop = nir_loop_create(b->shader);
2380 nir_cf_node_insert_end(b->nb.cf_node_list, &loop->cf_node);
2381
2382 struct exec_list *old_list = b->nb.cf_node_list;
2383
2384 /* Reset the merge_op to prerevent infinite recursion */
2385 block->merge_op = SpvOpNop;
2386
2387 nir_builder_insert_after_cf_list(&b->nb, &loop->body);
2388 vtn_walk_blocks(b, block, new_break_block, new_cont_block, NULL);
2389
2390 nir_builder_insert_after_cf_list(&b->nb, old_list);
2391 block = new_break_block;
2392 continue;
2393 }
2394
2395 const uint32_t *w = block->branch;
2396 SpvOp branch_op = w[0] & SpvOpCodeMask;
2397
2398 b->block = block;
2399 vtn_foreach_instruction(b, block->label, block->branch,
2400 vtn_handle_body_instruction);
2401
2402 nir_cf_node *cur_cf_node =
2403 exec_node_data(nir_cf_node, exec_list_get_tail(b->nb.cf_node_list),
2404 node);
2405 nir_block *cur_block = nir_cf_node_as_block(cur_cf_node);
2406 _mesa_hash_table_insert(b->block_table, cur_block, block);
2407
2408 switch (branch_op) {
2409 case SpvOpBranch: {
2410 struct vtn_block *branch_block =
2411 vtn_value(b, w[1], vtn_value_type_block)->block;
2412
2413 if (branch_block == break_block) {
2414 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2415 nir_jump_break);
2416 nir_builder_instr_insert(&b->nb, &jump->instr);
2417
2418 return;
2419 } else if (branch_block == cont_block) {
2420 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2421 nir_jump_continue);
2422 nir_builder_instr_insert(&b->nb, &jump->instr);
2423
2424 return;
2425 } else if (branch_block == end_block) {
2426 /* We're branching to the merge block of an if, since for loops
2427 * and functions end_block == NULL, so we're done here.
2428 */
2429 return;
2430 } else {
2431 /* We're branching to another block, and according to the rules,
2432 * we can only branch to another block with one predecessor (so
2433 * we're the only one jumping to it) so we can just process it
2434 * next.
2435 */
2436 block = branch_block;
2437 continue;
2438 }
2439 }
2440
2441 case SpvOpBranchConditional: {
2442 /* Gather up the branch blocks */
2443 struct vtn_block *then_block =
2444 vtn_value(b, w[2], vtn_value_type_block)->block;
2445 struct vtn_block *else_block =
2446 vtn_value(b, w[3], vtn_value_type_block)->block;
2447
2448 nir_if *if_stmt = nir_if_create(b->shader);
2449 if_stmt->condition = nir_src_for_ssa(vtn_ssa_value(b, w[1])->def);
2450 nir_cf_node_insert_end(b->nb.cf_node_list, &if_stmt->cf_node);
2451
2452 if (then_block == break_block) {
2453 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2454 nir_jump_break);
2455 nir_instr_insert_after_cf_list(&if_stmt->then_list,
2456 &jump->instr);
2457 block = else_block;
2458 } else if (else_block == break_block) {
2459 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2460 nir_jump_break);
2461 nir_instr_insert_after_cf_list(&if_stmt->else_list,
2462 &jump->instr);
2463 block = then_block;
2464 } else if (then_block == cont_block) {
2465 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2466 nir_jump_continue);
2467 nir_instr_insert_after_cf_list(&if_stmt->then_list,
2468 &jump->instr);
2469 block = else_block;
2470 } else if (else_block == cont_block) {
2471 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2472 nir_jump_continue);
2473 nir_instr_insert_after_cf_list(&if_stmt->else_list,
2474 &jump->instr);
2475 block = then_block;
2476 } else {
2477 /* According to the rules we're branching to two blocks that don't
2478 * have any other predecessors, so we can handle this as a
2479 * conventional if.
2480 */
2481 assert(block->merge_op == SpvOpSelectionMerge);
2482 struct vtn_block *merge_block =
2483 vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
2484
2485 struct exec_list *old_list = b->nb.cf_node_list;
2486
2487 nir_builder_insert_after_cf_list(&b->nb, &if_stmt->then_list);
2488 vtn_walk_blocks(b, then_block, break_block, cont_block, merge_block);
2489
2490 nir_builder_insert_after_cf_list(&b->nb, &if_stmt->else_list);
2491 vtn_walk_blocks(b, else_block, break_block, cont_block, merge_block);
2492
2493 nir_builder_insert_after_cf_list(&b->nb, old_list);
2494 block = merge_block;
2495 continue;
2496 }
2497
2498 /* If we got here then we inserted a predicated break or continue
2499 * above and we need to handle the other case. We already set
2500 * `block` above to indicate what block to visit after the
2501 * predicated break.
2502 */
2503
2504 /* It's possible that the other branch is also a break/continue.
2505 * If it is, we handle that here.
2506 */
2507 if (block == break_block) {
2508 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2509 nir_jump_break);
2510 nir_builder_instr_insert(&b->nb, &jump->instr);
2511
2512 return;
2513 } else if (block == cont_block) {
2514 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2515 nir_jump_continue);
2516 nir_builder_instr_insert(&b->nb, &jump->instr);
2517
2518 return;
2519 }
2520
2521 /* If we got here then there was a predicated break/continue but
2522 * the other half of the if has stuff in it. `block` was already
2523 * set above so there is nothing left for us to do.
2524 */
2525 continue;
2526 }
2527
2528 case SpvOpReturn: {
2529 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2530 nir_jump_return);
2531 nir_builder_instr_insert(&b->nb, &jump->instr);
2532 return;
2533 }
2534
2535 case SpvOpKill: {
2536 nir_intrinsic_instr *discard =
2537 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
2538 nir_builder_instr_insert(&b->nb, &discard->instr);
2539 return;
2540 }
2541
2542 case SpvOpSwitch:
2543 case SpvOpReturnValue:
2544 case SpvOpUnreachable:
2545 default:
2546 unreachable("Unhandled opcode");
2547 }
2548 }
2549 }
2550
2551 nir_shader *
2552 spirv_to_nir(const uint32_t *words, size_t word_count,
2553 const nir_shader_compiler_options *options)
2554 {
2555 const uint32_t *word_end = words + word_count;
2556
2557 /* Handle the SPIR-V header (first 4 dwords) */
2558 assert(word_count > 5);
2559
2560 assert(words[0] == SpvMagicNumber);
2561 assert(words[1] == 99);
2562 /* words[2] == generator magic */
2563 unsigned value_id_bound = words[3];
2564 assert(words[4] == 0);
2565
2566 words+= 5;
2567
2568 nir_shader *shader = nir_shader_create(NULL, options);
2569
2570 /* Initialize the stn_builder object */
2571 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
2572 b->shader = shader;
2573 b->value_id_bound = value_id_bound;
2574 b->values = ralloc_array(b, struct vtn_value, value_id_bound);
2575 exec_list_make_empty(&b->functions);
2576
2577 /* Handle all the preamble instructions */
2578 words = vtn_foreach_instruction(b, words, word_end,
2579 vtn_handle_preamble_instruction);
2580
2581 /* Do a very quick CFG analysis pass */
2582 vtn_foreach_instruction(b, words, word_end,
2583 vtn_handle_first_cfg_pass_instruction);
2584
2585 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
2586 b->impl = nir_function_impl_create(func->overload);
2587 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
2588 _mesa_key_pointer_equal);
2589 b->block_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
2590 _mesa_key_pointer_equal);
2591 nir_builder_init(&b->nb, b->impl);
2592 nir_builder_insert_after_cf_list(&b->nb, &b->impl->body);
2593 vtn_walk_blocks(b, func->start_block, NULL, NULL, NULL);
2594 vtn_foreach_instruction(b, func->start_block->label, func->end,
2595 vtn_handle_phi_second_pass);
2596 }
2597
2598 ralloc_free(b);
2599
2600 return shader;
2601 }