1cf9d597cf06460f7bf4fab8060c6e9821ba1668
[mesa.git] / src / compiler / spirv / vtn_variables.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 "spirv_info.h"
30
31 static struct vtn_access_chain *
32 vtn_access_chain_create(struct vtn_builder *b, unsigned length)
33 {
34 struct vtn_access_chain *chain;
35
36 /* Subtract 1 from the length since there's already one built in */
37 size_t size = sizeof(*chain) +
38 (MAX2(length, 1) - 1) * sizeof(chain->link[0]);
39 chain = rzalloc_size(b, size);
40 chain->length = length;
41
42 return chain;
43 }
44
45 static struct vtn_access_chain *
46 vtn_access_chain_extend(struct vtn_builder *b, struct vtn_access_chain *old,
47 unsigned new_ids)
48 {
49 struct vtn_access_chain *chain;
50
51 unsigned old_len = old ? old->length : 0;
52 chain = vtn_access_chain_create(b, old_len + new_ids);
53
54 for (unsigned i = 0; i < old_len; i++)
55 chain->link[i] = old->link[i];
56
57 return chain;
58 }
59
60 /* Dereference the given base pointer by the access chain */
61 static struct vtn_pointer *
62 vtn_access_chain_pointer_dereference(struct vtn_builder *b,
63 struct vtn_pointer *base,
64 struct vtn_access_chain *deref_chain)
65 {
66 struct vtn_access_chain *chain =
67 vtn_access_chain_extend(b, base->chain, deref_chain->length);
68 struct vtn_type *type = base->type;
69
70 /* OpPtrAccessChain is only allowed on things which support variable
71 * pointers. For everything else, the client is expected to just pass us
72 * the right access chain.
73 */
74 assert(!deref_chain->ptr_as_array);
75
76 unsigned start = base->chain ? base->chain->length : 0;
77 for (unsigned i = 0; i < deref_chain->length; i++) {
78 chain->link[start + i] = deref_chain->link[i];
79
80 if (glsl_type_is_struct(type->type)) {
81 assert(deref_chain->link[i].mode == vtn_access_mode_literal);
82 type = type->members[deref_chain->link[i].id];
83 } else {
84 type = type->array_element;
85 }
86 }
87
88 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
89 ptr->mode = base->mode;
90 ptr->type = type;
91 ptr->var = base->var;
92 ptr->chain = chain;
93
94 return ptr;
95 }
96
97 static nir_ssa_def *
98 vtn_access_link_as_ssa(struct vtn_builder *b, struct vtn_access_link link,
99 unsigned stride)
100 {
101 assert(stride > 0);
102 if (link.mode == vtn_access_mode_literal) {
103 return nir_imm_int(&b->nb, link.id * stride);
104 } else if (stride == 1) {
105 nir_ssa_def *ssa = vtn_ssa_value(b, link.id)->def;
106 if (ssa->bit_size != 32)
107 ssa = nir_u2u32(&b->nb, ssa);
108 return ssa;
109 } else {
110 nir_ssa_def *src0 = vtn_ssa_value(b, link.id)->def;
111 if (src0->bit_size != 32)
112 src0 = nir_u2u32(&b->nb, src0);
113 return nir_imul(&b->nb, src0, nir_imm_int(&b->nb, stride));
114 }
115 }
116
117 static nir_ssa_def *
118 vtn_variable_resource_index(struct vtn_builder *b, struct vtn_variable *var,
119 nir_ssa_def *desc_array_index)
120 {
121 if (!desc_array_index) {
122 assert(glsl_type_is_struct(var->type->type));
123 desc_array_index = nir_imm_int(&b->nb, 0);
124 }
125
126 nir_intrinsic_instr *instr =
127 nir_intrinsic_instr_create(b->nb.shader,
128 nir_intrinsic_vulkan_resource_index);
129 instr->src[0] = nir_src_for_ssa(desc_array_index);
130 nir_intrinsic_set_desc_set(instr, var->descriptor_set);
131 nir_intrinsic_set_binding(instr, var->binding);
132
133 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
134 nir_builder_instr_insert(&b->nb, &instr->instr);
135
136 return &instr->dest.ssa;
137 }
138
139 static struct vtn_pointer *
140 vtn_ssa_offset_pointer_dereference(struct vtn_builder *b,
141 struct vtn_pointer *base,
142 struct vtn_access_chain *deref_chain)
143 {
144 nir_ssa_def *block_index = base->block_index;
145 nir_ssa_def *offset = base->offset;
146 struct vtn_type *type = base->type;
147
148 unsigned idx = 0;
149 if (deref_chain->ptr_as_array) {
150 /* We need ptr_type for the stride */
151 assert(base->ptr_type);
152 /* This must be a pointer to an actual element somewhere */
153 assert(block_index && offset);
154 /* We need at least one element in the chain */
155 assert(deref_chain->length >= 1);
156
157 nir_ssa_def *elem_offset =
158 vtn_access_link_as_ssa(b, deref_chain->link[idx],
159 base->ptr_type->stride);
160 offset = nir_iadd(&b->nb, offset, elem_offset);
161 idx++;
162 }
163
164 if (!block_index) {
165 assert(base->var);
166 if (glsl_type_is_array(type->type)) {
167 /* We need at least one element in the chain */
168 assert(deref_chain->length >= 1);
169
170 nir_ssa_def *desc_arr_idx =
171 vtn_access_link_as_ssa(b, deref_chain->link[0], 1);
172 block_index = vtn_variable_resource_index(b, base->var, desc_arr_idx);
173 type = type->array_element;
174 idx++;
175 } else {
176 block_index = vtn_variable_resource_index(b, base->var, NULL);
177 }
178
179 /* This is the first access chain so we also need an offset */
180 assert(!offset);
181 offset = nir_imm_int(&b->nb, 0);
182 }
183 assert(offset);
184
185 for (; idx < deref_chain->length; idx++) {
186 switch (glsl_get_base_type(type->type)) {
187 case GLSL_TYPE_UINT:
188 case GLSL_TYPE_INT:
189 case GLSL_TYPE_UINT64:
190 case GLSL_TYPE_INT64:
191 case GLSL_TYPE_FLOAT:
192 case GLSL_TYPE_DOUBLE:
193 case GLSL_TYPE_BOOL:
194 case GLSL_TYPE_ARRAY: {
195 nir_ssa_def *elem_offset =
196 vtn_access_link_as_ssa(b, deref_chain->link[idx], type->stride);
197 offset = nir_iadd(&b->nb, offset, elem_offset);
198 type = type->array_element;
199 break;
200 }
201
202 case GLSL_TYPE_STRUCT: {
203 assert(deref_chain->link[idx].mode == vtn_access_mode_literal);
204 unsigned member = deref_chain->link[idx].id;
205 nir_ssa_def *mem_offset = nir_imm_int(&b->nb, type->offsets[member]);
206 offset = nir_iadd(&b->nb, offset, mem_offset);
207 type = type->members[member];
208 break;
209 }
210
211 default:
212 unreachable("Invalid type for deref");
213 }
214 }
215
216 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
217 ptr->mode = base->mode;
218 ptr->type = type;
219 ptr->block_index = block_index;
220 ptr->offset = offset;
221
222 return ptr;
223 }
224
225 /* Dereference the given base pointer by the access chain */
226 static struct vtn_pointer *
227 vtn_pointer_dereference(struct vtn_builder *b,
228 struct vtn_pointer *base,
229 struct vtn_access_chain *deref_chain)
230 {
231 if (vtn_pointer_uses_ssa_offset(base)) {
232 return vtn_ssa_offset_pointer_dereference(b, base, deref_chain);
233 } else {
234 return vtn_access_chain_pointer_dereference(b, base, deref_chain);
235 }
236 }
237
238 /* Crawls a chain of array derefs and rewrites the types so that the
239 * lengths stay the same but the terminal type is the one given by
240 * tail_type. This is useful for split structures.
241 */
242 static void
243 rewrite_deref_types(nir_deref *deref, const struct glsl_type *type)
244 {
245 deref->type = type;
246 if (deref->child) {
247 assert(deref->child->deref_type == nir_deref_type_array);
248 assert(glsl_type_is_array(deref->type));
249 rewrite_deref_types(deref->child, glsl_get_array_element(type));
250 }
251 }
252
253 struct vtn_pointer *
254 vtn_pointer_for_variable(struct vtn_builder *b,
255 struct vtn_variable *var, struct vtn_type *ptr_type)
256 {
257 struct vtn_pointer *pointer = rzalloc(b, struct vtn_pointer);
258
259 pointer->mode = var->mode;
260 pointer->type = var->type;
261 assert(ptr_type->base_type == vtn_base_type_pointer);
262 assert(ptr_type->deref->type == var->type->type);
263 pointer->ptr_type = ptr_type;
264 pointer->var = var;
265
266 return pointer;
267 }
268
269 nir_deref_var *
270 vtn_pointer_to_deref(struct vtn_builder *b, struct vtn_pointer *ptr)
271 {
272 /* Do on-the-fly copy propagation for samplers. */
273 if (ptr->var->copy_prop_sampler)
274 return vtn_pointer_to_deref(b, ptr->var->copy_prop_sampler);
275
276 nir_deref_var *deref_var;
277 if (ptr->var->var) {
278 deref_var = nir_deref_var_create(b, ptr->var->var);
279 /* Raw variable access */
280 if (!ptr->chain)
281 return deref_var;
282 } else {
283 assert(ptr->var->members);
284 /* Create the deref_var manually. It will get filled out later. */
285 deref_var = rzalloc(b, nir_deref_var);
286 deref_var->deref.deref_type = nir_deref_type_var;
287 }
288
289 struct vtn_access_chain *chain = ptr->chain;
290 assert(chain);
291
292 struct vtn_type *deref_type = ptr->var->type;
293 nir_deref *tail = &deref_var->deref;
294 nir_variable **members = ptr->var->members;
295
296 for (unsigned i = 0; i < chain->length; i++) {
297 enum glsl_base_type base_type = glsl_get_base_type(deref_type->type);
298 switch (base_type) {
299 case GLSL_TYPE_UINT:
300 case GLSL_TYPE_INT:
301 case GLSL_TYPE_UINT64:
302 case GLSL_TYPE_INT64:
303 case GLSL_TYPE_FLOAT:
304 case GLSL_TYPE_DOUBLE:
305 case GLSL_TYPE_BOOL:
306 case GLSL_TYPE_ARRAY: {
307 deref_type = deref_type->array_element;
308
309 nir_deref_array *deref_arr = nir_deref_array_create(b);
310 deref_arr->deref.type = deref_type->type;
311
312 if (chain->link[i].mode == vtn_access_mode_literal) {
313 deref_arr->deref_array_type = nir_deref_array_type_direct;
314 deref_arr->base_offset = chain->link[i].id;
315 } else {
316 assert(chain->link[i].mode == vtn_access_mode_id);
317 deref_arr->deref_array_type = nir_deref_array_type_indirect;
318 deref_arr->base_offset = 0;
319 deref_arr->indirect =
320 nir_src_for_ssa(vtn_ssa_value(b, chain->link[i].id)->def);
321 }
322 tail->child = &deref_arr->deref;
323 tail = tail->child;
324 break;
325 }
326
327 case GLSL_TYPE_STRUCT: {
328 assert(chain->link[i].mode == vtn_access_mode_literal);
329 unsigned idx = chain->link[i].id;
330 deref_type = deref_type->members[idx];
331 if (members) {
332 /* This is a pre-split structure. */
333 deref_var->var = members[idx];
334 rewrite_deref_types(&deref_var->deref, members[idx]->type);
335 assert(tail->type == deref_type->type);
336 members = NULL;
337 } else {
338 nir_deref_struct *deref_struct = nir_deref_struct_create(b, idx);
339 deref_struct->deref.type = deref_type->type;
340 tail->child = &deref_struct->deref;
341 tail = tail->child;
342 }
343 break;
344 }
345 default:
346 unreachable("Invalid type for deref");
347 }
348 }
349
350 assert(members == NULL);
351 return deref_var;
352 }
353
354 static void
355 _vtn_local_load_store(struct vtn_builder *b, bool load, nir_deref_var *deref,
356 nir_deref *tail, struct vtn_ssa_value *inout)
357 {
358 /* The deref tail may contain a deref to select a component of a vector (in
359 * other words, it might not be an actual tail) so we have to save it away
360 * here since we overwrite it later.
361 */
362 nir_deref *old_child = tail->child;
363
364 if (glsl_type_is_vector_or_scalar(tail->type)) {
365 /* Terminate the deref chain in case there is one more link to pick
366 * off a component of the vector.
367 */
368 tail->child = NULL;
369
370 nir_intrinsic_op op = load ? nir_intrinsic_load_var :
371 nir_intrinsic_store_var;
372
373 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
374 intrin->variables[0] = nir_deref_var_clone(deref, intrin);
375 intrin->num_components = glsl_get_vector_elements(tail->type);
376
377 if (load) {
378 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
379 intrin->num_components,
380 glsl_get_bit_size(tail->type),
381 NULL);
382 inout->def = &intrin->dest.ssa;
383 } else {
384 nir_intrinsic_set_write_mask(intrin, (1 << intrin->num_components) - 1);
385 intrin->src[0] = nir_src_for_ssa(inout->def);
386 }
387
388 nir_builder_instr_insert(&b->nb, &intrin->instr);
389 } else if (glsl_get_base_type(tail->type) == GLSL_TYPE_ARRAY ||
390 glsl_type_is_matrix(tail->type)) {
391 unsigned elems = glsl_get_length(tail->type);
392 nir_deref_array *deref_arr = nir_deref_array_create(b);
393 deref_arr->deref_array_type = nir_deref_array_type_direct;
394 deref_arr->deref.type = glsl_get_array_element(tail->type);
395 tail->child = &deref_arr->deref;
396 for (unsigned i = 0; i < elems; i++) {
397 deref_arr->base_offset = i;
398 _vtn_local_load_store(b, load, deref, tail->child, inout->elems[i]);
399 }
400 } else {
401 assert(glsl_get_base_type(tail->type) == GLSL_TYPE_STRUCT);
402 unsigned elems = glsl_get_length(tail->type);
403 nir_deref_struct *deref_struct = nir_deref_struct_create(b, 0);
404 tail->child = &deref_struct->deref;
405 for (unsigned i = 0; i < elems; i++) {
406 deref_struct->index = i;
407 deref_struct->deref.type = glsl_get_struct_field(tail->type, i);
408 _vtn_local_load_store(b, load, deref, tail->child, inout->elems[i]);
409 }
410 }
411
412 tail->child = old_child;
413 }
414
415 nir_deref_var *
416 vtn_nir_deref(struct vtn_builder *b, uint32_t id)
417 {
418 struct vtn_pointer *ptr = vtn_value(b, id, vtn_value_type_pointer)->pointer;
419 return vtn_pointer_to_deref(b, ptr);
420 }
421
422 /*
423 * Gets the NIR-level deref tail, which may have as a child an array deref
424 * selecting which component due to OpAccessChain supporting per-component
425 * indexing in SPIR-V.
426 */
427 static nir_deref *
428 get_deref_tail(nir_deref_var *deref)
429 {
430 nir_deref *cur = &deref->deref;
431 while (!glsl_type_is_vector_or_scalar(cur->type) && cur->child)
432 cur = cur->child;
433
434 return cur;
435 }
436
437 struct vtn_ssa_value *
438 vtn_local_load(struct vtn_builder *b, nir_deref_var *src)
439 {
440 nir_deref *src_tail = get_deref_tail(src);
441 struct vtn_ssa_value *val = vtn_create_ssa_value(b, src_tail->type);
442 _vtn_local_load_store(b, true, src, src_tail, val);
443
444 if (src_tail->child) {
445 nir_deref_array *vec_deref = nir_deref_as_array(src_tail->child);
446 assert(vec_deref->deref.child == NULL);
447 val->type = vec_deref->deref.type;
448 if (vec_deref->deref_array_type == nir_deref_array_type_direct)
449 val->def = vtn_vector_extract(b, val->def, vec_deref->base_offset);
450 else
451 val->def = vtn_vector_extract_dynamic(b, val->def,
452 vec_deref->indirect.ssa);
453 }
454
455 return val;
456 }
457
458 void
459 vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
460 nir_deref_var *dest)
461 {
462 nir_deref *dest_tail = get_deref_tail(dest);
463
464 if (dest_tail->child) {
465 struct vtn_ssa_value *val = vtn_create_ssa_value(b, dest_tail->type);
466 _vtn_local_load_store(b, true, dest, dest_tail, val);
467 nir_deref_array *deref = nir_deref_as_array(dest_tail->child);
468 assert(deref->deref.child == NULL);
469 if (deref->deref_array_type == nir_deref_array_type_direct)
470 val->def = vtn_vector_insert(b, val->def, src->def,
471 deref->base_offset);
472 else
473 val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
474 deref->indirect.ssa);
475 _vtn_local_load_store(b, false, dest, dest_tail, val);
476 } else {
477 _vtn_local_load_store(b, false, dest, dest_tail, src);
478 }
479 }
480
481 static nir_ssa_def *
482 get_vulkan_resource_index(struct vtn_builder *b, struct vtn_pointer *ptr,
483 struct vtn_type **type, unsigned *chain_idx)
484 {
485 /* Push constants have no explicit binding */
486 if (ptr->mode == vtn_variable_mode_push_constant) {
487 *chain_idx = 0;
488 *type = ptr->var->type;
489 return NULL;
490 }
491
492 if (glsl_type_is_array(ptr->var->type->type)) {
493 assert(ptr->chain->length > 0);
494 nir_ssa_def *desc_array_index =
495 vtn_access_link_as_ssa(b, ptr->chain->link[0], 1);
496 *chain_idx = 1;
497 *type = ptr->var->type->array_element;
498 return vtn_variable_resource_index(b, ptr->var, desc_array_index);
499 } else {
500 *chain_idx = 0;
501 *type = ptr->var->type;
502 return vtn_variable_resource_index(b, ptr->var, NULL);
503 }
504 }
505
506 nir_ssa_def *
507 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
508 nir_ssa_def **index_out, unsigned *end_idx_out)
509 {
510 if (ptr->offset) {
511 assert(ptr->block_index);
512 *index_out = ptr->block_index;
513 return ptr->offset;
514 }
515
516 unsigned idx = 0;
517 struct vtn_type *type;
518 *index_out = get_vulkan_resource_index(b, ptr, &type, &idx);
519
520 nir_ssa_def *offset = nir_imm_int(&b->nb, 0);
521 for (; idx < ptr->chain->length; idx++) {
522 enum glsl_base_type base_type = glsl_get_base_type(type->type);
523 switch (base_type) {
524 case GLSL_TYPE_UINT:
525 case GLSL_TYPE_INT:
526 case GLSL_TYPE_UINT64:
527 case GLSL_TYPE_INT64:
528 case GLSL_TYPE_FLOAT:
529 case GLSL_TYPE_DOUBLE:
530 case GLSL_TYPE_BOOL:
531 case GLSL_TYPE_ARRAY:
532 offset = nir_iadd(&b->nb, offset,
533 vtn_access_link_as_ssa(b, ptr->chain->link[idx],
534 type->stride));
535
536 type = type->array_element;
537 break;
538
539 case GLSL_TYPE_STRUCT: {
540 assert(ptr->chain->link[idx].mode == vtn_access_mode_literal);
541 unsigned member = ptr->chain->link[idx].id;
542 offset = nir_iadd(&b->nb, offset,
543 nir_imm_int(&b->nb, type->offsets[member]));
544 type = type->members[member];
545 break;
546 }
547
548 default:
549 unreachable("Invalid type for deref");
550 }
551 }
552
553 assert(type == ptr->type);
554 if (end_idx_out)
555 *end_idx_out = idx;
556
557 return offset;
558 }
559
560 /* Tries to compute the size of an interface block based on the strides and
561 * offsets that are provided to us in the SPIR-V source.
562 */
563 static unsigned
564 vtn_type_block_size(struct vtn_type *type)
565 {
566 enum glsl_base_type base_type = glsl_get_base_type(type->type);
567 switch (base_type) {
568 case GLSL_TYPE_UINT:
569 case GLSL_TYPE_INT:
570 case GLSL_TYPE_UINT64:
571 case GLSL_TYPE_INT64:
572 case GLSL_TYPE_FLOAT:
573 case GLSL_TYPE_BOOL:
574 case GLSL_TYPE_DOUBLE: {
575 unsigned cols = type->row_major ? glsl_get_vector_elements(type->type) :
576 glsl_get_matrix_columns(type->type);
577 if (cols > 1) {
578 assert(type->stride > 0);
579 return type->stride * cols;
580 } else if (base_type == GLSL_TYPE_DOUBLE ||
581 base_type == GLSL_TYPE_UINT64 ||
582 base_type == GLSL_TYPE_INT64) {
583 return glsl_get_vector_elements(type->type) * 8;
584 } else {
585 return glsl_get_vector_elements(type->type) * 4;
586 }
587 }
588
589 case GLSL_TYPE_STRUCT:
590 case GLSL_TYPE_INTERFACE: {
591 unsigned size = 0;
592 unsigned num_fields = glsl_get_length(type->type);
593 for (unsigned f = 0; f < num_fields; f++) {
594 unsigned field_end = type->offsets[f] +
595 vtn_type_block_size(type->members[f]);
596 size = MAX2(size, field_end);
597 }
598 return size;
599 }
600
601 case GLSL_TYPE_ARRAY:
602 assert(type->stride > 0);
603 assert(glsl_get_length(type->type) > 0);
604 return type->stride * glsl_get_length(type->type);
605
606 default:
607 unreachable("Invalid block type");
608 return 0;
609 }
610 }
611
612 static void
613 vtn_access_chain_get_offset_size(struct vtn_access_chain *chain,
614 struct vtn_type *type,
615 unsigned *access_offset,
616 unsigned *access_size)
617 {
618 *access_offset = 0;
619
620 for (unsigned i = 0; i < chain->length; i++) {
621 if (chain->link[i].mode != vtn_access_mode_literal)
622 break;
623
624 if (glsl_type_is_struct(type->type)) {
625 *access_offset += type->offsets[chain->link[i].id];
626 type = type->members[chain->link[i].id];
627 } else {
628 *access_offset += type->stride * chain->link[i].id;
629 type = type->array_element;
630 }
631 }
632
633 *access_size = vtn_type_block_size(type);
634 }
635
636 static void
637 _vtn_load_store_tail(struct vtn_builder *b, nir_intrinsic_op op, bool load,
638 nir_ssa_def *index, nir_ssa_def *offset,
639 unsigned access_offset, unsigned access_size,
640 struct vtn_ssa_value **inout, const struct glsl_type *type)
641 {
642 nir_intrinsic_instr *instr = nir_intrinsic_instr_create(b->nb.shader, op);
643 instr->num_components = glsl_get_vector_elements(type);
644
645 int src = 0;
646 if (!load) {
647 nir_intrinsic_set_write_mask(instr, (1 << instr->num_components) - 1);
648 instr->src[src++] = nir_src_for_ssa((*inout)->def);
649 }
650
651 if (op == nir_intrinsic_load_push_constant) {
652 assert(access_offset % 4 == 0);
653
654 nir_intrinsic_set_base(instr, access_offset);
655 nir_intrinsic_set_range(instr, access_size);
656 }
657
658 if (index)
659 instr->src[src++] = nir_src_for_ssa(index);
660
661 if (op == nir_intrinsic_load_push_constant) {
662 /* We need to subtract the offset from where the intrinsic will load the
663 * data. */
664 instr->src[src++] =
665 nir_src_for_ssa(nir_isub(&b->nb, offset,
666 nir_imm_int(&b->nb, access_offset)));
667 } else {
668 instr->src[src++] = nir_src_for_ssa(offset);
669 }
670
671 if (load) {
672 nir_ssa_dest_init(&instr->instr, &instr->dest,
673 instr->num_components,
674 glsl_get_bit_size(type), NULL);
675 (*inout)->def = &instr->dest.ssa;
676 }
677
678 nir_builder_instr_insert(&b->nb, &instr->instr);
679
680 if (load && glsl_get_base_type(type) == GLSL_TYPE_BOOL)
681 (*inout)->def = nir_ine(&b->nb, (*inout)->def, nir_imm_int(&b->nb, 0));
682 }
683
684 static void
685 _vtn_block_load_store(struct vtn_builder *b, nir_intrinsic_op op, bool load,
686 nir_ssa_def *index, nir_ssa_def *offset,
687 unsigned access_offset, unsigned access_size,
688 struct vtn_access_chain *chain, unsigned chain_idx,
689 struct vtn_type *type, struct vtn_ssa_value **inout)
690 {
691 if (chain && chain_idx >= chain->length)
692 chain = NULL;
693
694 if (load && chain == NULL && *inout == NULL)
695 *inout = vtn_create_ssa_value(b, type->type);
696
697 enum glsl_base_type base_type = glsl_get_base_type(type->type);
698 switch (base_type) {
699 case GLSL_TYPE_UINT:
700 case GLSL_TYPE_INT:
701 case GLSL_TYPE_UINT64:
702 case GLSL_TYPE_INT64:
703 case GLSL_TYPE_FLOAT:
704 case GLSL_TYPE_DOUBLE:
705 case GLSL_TYPE_BOOL:
706 /* This is where things get interesting. At this point, we've hit
707 * a vector, a scalar, or a matrix.
708 */
709 if (glsl_type_is_matrix(type->type)) {
710 /* Loading the whole matrix */
711 struct vtn_ssa_value *transpose;
712 unsigned num_ops, vec_width, col_stride;
713 if (type->row_major) {
714 num_ops = glsl_get_vector_elements(type->type);
715 vec_width = glsl_get_matrix_columns(type->type);
716 col_stride = type->array_element->stride;
717 if (load) {
718 const struct glsl_type *transpose_type =
719 glsl_matrix_type(base_type, vec_width, num_ops);
720 *inout = vtn_create_ssa_value(b, transpose_type);
721 } else {
722 transpose = vtn_ssa_transpose(b, *inout);
723 inout = &transpose;
724 }
725 } else {
726 num_ops = glsl_get_matrix_columns(type->type);
727 vec_width = glsl_get_vector_elements(type->type);
728 col_stride = type->stride;
729 }
730
731 for (unsigned i = 0; i < num_ops; i++) {
732 nir_ssa_def *elem_offset =
733 nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, i * col_stride));
734 _vtn_load_store_tail(b, op, load, index, elem_offset,
735 access_offset, access_size,
736 &(*inout)->elems[i],
737 glsl_vector_type(base_type, vec_width));
738 }
739
740 if (load && type->row_major)
741 *inout = vtn_ssa_transpose(b, *inout);
742 } else {
743 unsigned elems = glsl_get_vector_elements(type->type);
744 unsigned type_size = glsl_get_bit_size(type->type) / 8;
745 if (elems == 1 || type->stride == type_size) {
746 /* This is a tightly-packed normal scalar or vector load */
747 assert(glsl_type_is_vector_or_scalar(type->type));
748 _vtn_load_store_tail(b, op, load, index, offset,
749 access_offset, access_size,
750 inout, type->type);
751 } else {
752 /* This is a strided load. We have to load N things separately.
753 * This is the single column of a row-major matrix case.
754 */
755 assert(type->stride > type_size);
756 assert(type->stride % type_size == 0);
757
758 nir_ssa_def *per_comp[4];
759 for (unsigned i = 0; i < elems; i++) {
760 nir_ssa_def *elem_offset =
761 nir_iadd(&b->nb, offset,
762 nir_imm_int(&b->nb, i * type->stride));
763 struct vtn_ssa_value *comp, temp_val;
764 if (!load) {
765 temp_val.def = nir_channel(&b->nb, (*inout)->def, i);
766 temp_val.type = glsl_scalar_type(base_type);
767 }
768 comp = &temp_val;
769 _vtn_load_store_tail(b, op, load, index, elem_offset,
770 access_offset, access_size,
771 &comp, glsl_scalar_type(base_type));
772 per_comp[i] = comp->def;
773 }
774
775 if (load) {
776 if (*inout == NULL)
777 *inout = vtn_create_ssa_value(b, type->type);
778 (*inout)->def = nir_vec(&b->nb, per_comp, elems);
779 }
780 }
781 }
782 return;
783
784 case GLSL_TYPE_ARRAY: {
785 unsigned elems = glsl_get_length(type->type);
786 for (unsigned i = 0; i < elems; i++) {
787 nir_ssa_def *elem_off =
788 nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, i * type->stride));
789 _vtn_block_load_store(b, op, load, index, elem_off,
790 access_offset, access_size,
791 NULL, 0,
792 type->array_element, &(*inout)->elems[i]);
793 }
794 return;
795 }
796
797 case GLSL_TYPE_STRUCT: {
798 unsigned elems = glsl_get_length(type->type);
799 for (unsigned i = 0; i < elems; i++) {
800 nir_ssa_def *elem_off =
801 nir_iadd(&b->nb, offset, nir_imm_int(&b->nb, type->offsets[i]));
802 _vtn_block_load_store(b, op, load, index, elem_off,
803 access_offset, access_size,
804 NULL, 0,
805 type->members[i], &(*inout)->elems[i]);
806 }
807 return;
808 }
809
810 default:
811 unreachable("Invalid block member type");
812 }
813 }
814
815 static struct vtn_ssa_value *
816 vtn_block_load(struct vtn_builder *b, struct vtn_pointer *src)
817 {
818 nir_intrinsic_op op;
819 unsigned access_offset = 0, access_size = 0;
820 switch (src->mode) {
821 case vtn_variable_mode_ubo:
822 op = nir_intrinsic_load_ubo;
823 break;
824 case vtn_variable_mode_ssbo:
825 op = nir_intrinsic_load_ssbo;
826 break;
827 case vtn_variable_mode_push_constant:
828 op = nir_intrinsic_load_push_constant;
829 vtn_access_chain_get_offset_size(src->chain, src->var->type,
830 &access_offset, &access_size);
831 break;
832 default:
833 unreachable("Invalid block variable mode");
834 }
835
836 nir_ssa_def *offset, *index = NULL;
837 unsigned chain_idx;
838 offset = vtn_pointer_to_offset(b, src, &index, &chain_idx);
839
840 struct vtn_ssa_value *value = NULL;
841 _vtn_block_load_store(b, op, true, index, offset,
842 access_offset, access_size,
843 src->chain, chain_idx, src->type, &value);
844 return value;
845 }
846
847 static void
848 vtn_block_store(struct vtn_builder *b, struct vtn_ssa_value *src,
849 struct vtn_pointer *dst)
850 {
851 nir_ssa_def *offset, *index = NULL;
852 unsigned chain_idx;
853 offset = vtn_pointer_to_offset(b, dst, &index, &chain_idx);
854
855 _vtn_block_load_store(b, nir_intrinsic_store_ssbo, false, index, offset,
856 0, 0, dst->chain, chain_idx, dst->type, &src);
857 }
858
859 static bool
860 vtn_pointer_is_external_block(struct vtn_pointer *ptr)
861 {
862 return ptr->mode == vtn_variable_mode_ssbo ||
863 ptr->mode == vtn_variable_mode_ubo ||
864 ptr->mode == vtn_variable_mode_push_constant;
865 }
866
867 static void
868 _vtn_variable_load_store(struct vtn_builder *b, bool load,
869 struct vtn_pointer *ptr,
870 struct vtn_ssa_value **inout)
871 {
872 enum glsl_base_type base_type = glsl_get_base_type(ptr->type->type);
873 switch (base_type) {
874 case GLSL_TYPE_UINT:
875 case GLSL_TYPE_INT:
876 case GLSL_TYPE_UINT64:
877 case GLSL_TYPE_INT64:
878 case GLSL_TYPE_FLOAT:
879 case GLSL_TYPE_BOOL:
880 case GLSL_TYPE_DOUBLE:
881 /* At this point, we have a scalar, vector, or matrix so we know that
882 * there cannot be any structure splitting still in the way. By
883 * stopping at the matrix level rather than the vector level, we
884 * ensure that matrices get loaded in the optimal way even if they
885 * are storred row-major in a UBO.
886 */
887 if (load) {
888 *inout = vtn_local_load(b, vtn_pointer_to_deref(b, ptr));
889 } else {
890 vtn_local_store(b, *inout, vtn_pointer_to_deref(b, ptr));
891 }
892 return;
893
894 case GLSL_TYPE_ARRAY:
895 case GLSL_TYPE_STRUCT: {
896 unsigned elems = glsl_get_length(ptr->type->type);
897 if (load) {
898 assert(*inout == NULL);
899 *inout = rzalloc(b, struct vtn_ssa_value);
900 (*inout)->type = ptr->type->type;
901 (*inout)->elems = rzalloc_array(b, struct vtn_ssa_value *, elems);
902 }
903
904 struct vtn_access_chain chain = {
905 .length = 1,
906 .link = {
907 { .mode = vtn_access_mode_literal, },
908 }
909 };
910 for (unsigned i = 0; i < elems; i++) {
911 chain.link[0].id = i;
912 struct vtn_pointer *elem = vtn_pointer_dereference(b, ptr, &chain);
913 _vtn_variable_load_store(b, load, elem, &(*inout)->elems[i]);
914 }
915 return;
916 }
917
918 default:
919 unreachable("Invalid access chain type");
920 }
921 }
922
923 struct vtn_ssa_value *
924 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src)
925 {
926 if (vtn_pointer_is_external_block(src)) {
927 return vtn_block_load(b, src);
928 } else {
929 struct vtn_ssa_value *val = NULL;
930 _vtn_variable_load_store(b, true, src, &val);
931 return val;
932 }
933 }
934
935 void
936 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
937 struct vtn_pointer *dest)
938 {
939 if (vtn_pointer_is_external_block(dest)) {
940 assert(dest->mode == vtn_variable_mode_ssbo);
941 vtn_block_store(b, src, dest);
942 } else {
943 _vtn_variable_load_store(b, false, dest, &src);
944 }
945 }
946
947 static void
948 _vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
949 struct vtn_pointer *src)
950 {
951 assert(src->type->type == dest->type->type);
952 enum glsl_base_type base_type = glsl_get_base_type(src->type->type);
953 switch (base_type) {
954 case GLSL_TYPE_UINT:
955 case GLSL_TYPE_INT:
956 case GLSL_TYPE_UINT64:
957 case GLSL_TYPE_INT64:
958 case GLSL_TYPE_FLOAT:
959 case GLSL_TYPE_DOUBLE:
960 case GLSL_TYPE_BOOL:
961 /* At this point, we have a scalar, vector, or matrix so we know that
962 * there cannot be any structure splitting still in the way. By
963 * stopping at the matrix level rather than the vector level, we
964 * ensure that matrices get loaded in the optimal way even if they
965 * are storred row-major in a UBO.
966 */
967 vtn_variable_store(b, vtn_variable_load(b, src), dest);
968 return;
969
970 case GLSL_TYPE_ARRAY:
971 case GLSL_TYPE_STRUCT: {
972 struct vtn_access_chain chain = {
973 .length = 1,
974 .link = {
975 { .mode = vtn_access_mode_literal, },
976 }
977 };
978 unsigned elems = glsl_get_length(src->type->type);
979 for (unsigned i = 0; i < elems; i++) {
980 chain.link[0].id = i;
981 struct vtn_pointer *src_elem =
982 vtn_pointer_dereference(b, src, &chain);
983 struct vtn_pointer *dest_elem =
984 vtn_pointer_dereference(b, dest, &chain);
985
986 _vtn_variable_copy(b, dest_elem, src_elem);
987 }
988 return;
989 }
990
991 default:
992 unreachable("Invalid access chain type");
993 }
994 }
995
996 static void
997 vtn_variable_copy(struct vtn_builder *b, struct vtn_pointer *dest,
998 struct vtn_pointer *src)
999 {
1000 /* TODO: At some point, we should add a special-case for when we can
1001 * just emit a copy_var intrinsic.
1002 */
1003 _vtn_variable_copy(b, dest, src);
1004 }
1005
1006 static void
1007 set_mode_system_value(nir_variable_mode *mode)
1008 {
1009 assert(*mode == nir_var_system_value || *mode == nir_var_shader_in);
1010 *mode = nir_var_system_value;
1011 }
1012
1013 static void
1014 vtn_get_builtin_location(struct vtn_builder *b,
1015 SpvBuiltIn builtin, int *location,
1016 nir_variable_mode *mode)
1017 {
1018 switch (builtin) {
1019 case SpvBuiltInPosition:
1020 *location = VARYING_SLOT_POS;
1021 break;
1022 case SpvBuiltInPointSize:
1023 *location = VARYING_SLOT_PSIZ;
1024 break;
1025 case SpvBuiltInClipDistance:
1026 *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
1027 break;
1028 case SpvBuiltInCullDistance:
1029 *location = VARYING_SLOT_CULL_DIST0;
1030 break;
1031 case SpvBuiltInVertexIndex:
1032 *location = SYSTEM_VALUE_VERTEX_ID;
1033 set_mode_system_value(mode);
1034 break;
1035 case SpvBuiltInVertexId:
1036 /* Vulkan defines VertexID to be zero-based and reserves the new
1037 * builtin keyword VertexIndex to indicate the non-zero-based value.
1038 */
1039 *location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
1040 set_mode_system_value(mode);
1041 break;
1042 case SpvBuiltInInstanceIndex:
1043 *location = SYSTEM_VALUE_INSTANCE_INDEX;
1044 set_mode_system_value(mode);
1045 break;
1046 case SpvBuiltInInstanceId:
1047 *location = SYSTEM_VALUE_INSTANCE_ID;
1048 set_mode_system_value(mode);
1049 break;
1050 case SpvBuiltInPrimitiveId:
1051 if (b->shader->info.stage == MESA_SHADER_FRAGMENT) {
1052 assert(*mode == nir_var_shader_in);
1053 *location = VARYING_SLOT_PRIMITIVE_ID;
1054 } else if (*mode == nir_var_shader_out) {
1055 *location = VARYING_SLOT_PRIMITIVE_ID;
1056 } else {
1057 *location = SYSTEM_VALUE_PRIMITIVE_ID;
1058 set_mode_system_value(mode);
1059 }
1060 break;
1061 case SpvBuiltInInvocationId:
1062 *location = SYSTEM_VALUE_INVOCATION_ID;
1063 set_mode_system_value(mode);
1064 break;
1065 case SpvBuiltInLayer:
1066 *location = VARYING_SLOT_LAYER;
1067 if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1068 *mode = nir_var_shader_in;
1069 else if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1070 *mode = nir_var_shader_out;
1071 else
1072 unreachable("invalid stage for SpvBuiltInLayer");
1073 break;
1074 case SpvBuiltInViewportIndex:
1075 *location = VARYING_SLOT_VIEWPORT;
1076 if (b->shader->info.stage == MESA_SHADER_GEOMETRY)
1077 *mode = nir_var_shader_out;
1078 else if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
1079 *mode = nir_var_shader_in;
1080 else
1081 unreachable("invalid stage for SpvBuiltInViewportIndex");
1082 break;
1083 case SpvBuiltInTessLevelOuter:
1084 *location = VARYING_SLOT_TESS_LEVEL_OUTER;
1085 break;
1086 case SpvBuiltInTessLevelInner:
1087 *location = VARYING_SLOT_TESS_LEVEL_INNER;
1088 break;
1089 case SpvBuiltInTessCoord:
1090 *location = SYSTEM_VALUE_TESS_COORD;
1091 set_mode_system_value(mode);
1092 break;
1093 case SpvBuiltInPatchVertices:
1094 *location = SYSTEM_VALUE_VERTICES_IN;
1095 set_mode_system_value(mode);
1096 break;
1097 case SpvBuiltInFragCoord:
1098 *location = VARYING_SLOT_POS;
1099 assert(*mode == nir_var_shader_in);
1100 break;
1101 case SpvBuiltInPointCoord:
1102 *location = VARYING_SLOT_PNTC;
1103 assert(*mode == nir_var_shader_in);
1104 break;
1105 case SpvBuiltInFrontFacing:
1106 *location = SYSTEM_VALUE_FRONT_FACE;
1107 set_mode_system_value(mode);
1108 break;
1109 case SpvBuiltInSampleId:
1110 *location = SYSTEM_VALUE_SAMPLE_ID;
1111 set_mode_system_value(mode);
1112 break;
1113 case SpvBuiltInSamplePosition:
1114 *location = SYSTEM_VALUE_SAMPLE_POS;
1115 set_mode_system_value(mode);
1116 break;
1117 case SpvBuiltInSampleMask:
1118 if (*mode == nir_var_shader_out) {
1119 *location = FRAG_RESULT_SAMPLE_MASK;
1120 } else {
1121 *location = SYSTEM_VALUE_SAMPLE_MASK_IN;
1122 set_mode_system_value(mode);
1123 }
1124 break;
1125 case SpvBuiltInFragDepth:
1126 *location = FRAG_RESULT_DEPTH;
1127 assert(*mode == nir_var_shader_out);
1128 break;
1129 case SpvBuiltInHelperInvocation:
1130 *location = SYSTEM_VALUE_HELPER_INVOCATION;
1131 set_mode_system_value(mode);
1132 break;
1133 case SpvBuiltInNumWorkgroups:
1134 *location = SYSTEM_VALUE_NUM_WORK_GROUPS;
1135 set_mode_system_value(mode);
1136 break;
1137 case SpvBuiltInWorkgroupSize:
1138 /* This should already be handled */
1139 unreachable("unsupported builtin");
1140 break;
1141 case SpvBuiltInWorkgroupId:
1142 *location = SYSTEM_VALUE_WORK_GROUP_ID;
1143 set_mode_system_value(mode);
1144 break;
1145 case SpvBuiltInLocalInvocationId:
1146 *location = SYSTEM_VALUE_LOCAL_INVOCATION_ID;
1147 set_mode_system_value(mode);
1148 break;
1149 case SpvBuiltInLocalInvocationIndex:
1150 *location = SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
1151 set_mode_system_value(mode);
1152 break;
1153 case SpvBuiltInGlobalInvocationId:
1154 *location = SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
1155 set_mode_system_value(mode);
1156 break;
1157 case SpvBuiltInBaseVertex:
1158 *location = SYSTEM_VALUE_BASE_VERTEX;
1159 set_mode_system_value(mode);
1160 break;
1161 case SpvBuiltInBaseInstance:
1162 *location = SYSTEM_VALUE_BASE_INSTANCE;
1163 set_mode_system_value(mode);
1164 break;
1165 case SpvBuiltInDrawIndex:
1166 *location = SYSTEM_VALUE_DRAW_ID;
1167 set_mode_system_value(mode);
1168 break;
1169 case SpvBuiltInViewIndex:
1170 *location = SYSTEM_VALUE_VIEW_INDEX;
1171 set_mode_system_value(mode);
1172 break;
1173 default:
1174 unreachable("unsupported builtin");
1175 }
1176 }
1177
1178 static void
1179 apply_var_decoration(struct vtn_builder *b, nir_variable *nir_var,
1180 const struct vtn_decoration *dec)
1181 {
1182 switch (dec->decoration) {
1183 case SpvDecorationRelaxedPrecision:
1184 break; /* FIXME: Do nothing with this for now. */
1185 case SpvDecorationNoPerspective:
1186 nir_var->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
1187 break;
1188 case SpvDecorationFlat:
1189 nir_var->data.interpolation = INTERP_MODE_FLAT;
1190 break;
1191 case SpvDecorationCentroid:
1192 nir_var->data.centroid = true;
1193 break;
1194 case SpvDecorationSample:
1195 nir_var->data.sample = true;
1196 break;
1197 case SpvDecorationInvariant:
1198 nir_var->data.invariant = true;
1199 break;
1200 case SpvDecorationConstant:
1201 assert(nir_var->constant_initializer != NULL);
1202 nir_var->data.read_only = true;
1203 break;
1204 case SpvDecorationNonReadable:
1205 nir_var->data.image.write_only = true;
1206 break;
1207 case SpvDecorationNonWritable:
1208 nir_var->data.read_only = true;
1209 nir_var->data.image.read_only = true;
1210 break;
1211 case SpvDecorationComponent:
1212 nir_var->data.location_frac = dec->literals[0];
1213 break;
1214 case SpvDecorationIndex:
1215 nir_var->data.index = dec->literals[0];
1216 break;
1217 case SpvDecorationBuiltIn: {
1218 SpvBuiltIn builtin = dec->literals[0];
1219
1220 if (builtin == SpvBuiltInWorkgroupSize) {
1221 /* This shouldn't be a builtin. It's actually a constant. */
1222 nir_var->data.mode = nir_var_global;
1223 nir_var->data.read_only = true;
1224
1225 nir_constant *c = rzalloc(nir_var, nir_constant);
1226 c->values[0].u32[0] = b->shader->info.cs.local_size[0];
1227 c->values[0].u32[1] = b->shader->info.cs.local_size[1];
1228 c->values[0].u32[2] = b->shader->info.cs.local_size[2];
1229 nir_var->constant_initializer = c;
1230 break;
1231 }
1232
1233 nir_variable_mode mode = nir_var->data.mode;
1234 vtn_get_builtin_location(b, builtin, &nir_var->data.location, &mode);
1235 nir_var->data.mode = mode;
1236
1237 switch (builtin) {
1238 case SpvBuiltInTessLevelOuter:
1239 case SpvBuiltInTessLevelInner:
1240 nir_var->data.compact = true;
1241 break;
1242 case SpvBuiltInSamplePosition:
1243 nir_var->data.origin_upper_left = b->origin_upper_left;
1244 /* fallthrough */
1245 case SpvBuiltInFragCoord:
1246 nir_var->data.pixel_center_integer = b->pixel_center_integer;
1247 break;
1248 default:
1249 break;
1250 }
1251 }
1252
1253 case SpvDecorationSpecId:
1254 case SpvDecorationRowMajor:
1255 case SpvDecorationColMajor:
1256 case SpvDecorationMatrixStride:
1257 case SpvDecorationRestrict:
1258 case SpvDecorationAliased:
1259 case SpvDecorationVolatile:
1260 case SpvDecorationCoherent:
1261 case SpvDecorationUniform:
1262 case SpvDecorationStream:
1263 case SpvDecorationOffset:
1264 case SpvDecorationLinkageAttributes:
1265 break; /* Do nothing with these here */
1266
1267 case SpvDecorationPatch:
1268 nir_var->data.patch = true;
1269 break;
1270
1271 case SpvDecorationLocation:
1272 unreachable("Handled above");
1273
1274 case SpvDecorationBlock:
1275 case SpvDecorationBufferBlock:
1276 case SpvDecorationArrayStride:
1277 case SpvDecorationGLSLShared:
1278 case SpvDecorationGLSLPacked:
1279 break; /* These can apply to a type but we don't care about them */
1280
1281 case SpvDecorationBinding:
1282 case SpvDecorationDescriptorSet:
1283 case SpvDecorationNoContraction:
1284 case SpvDecorationInputAttachmentIndex:
1285 vtn_warn("Decoration not allowed for variable or structure member: %s",
1286 spirv_decoration_to_string(dec->decoration));
1287 break;
1288
1289 case SpvDecorationXfbBuffer:
1290 case SpvDecorationXfbStride:
1291 vtn_warn("Vulkan does not have transform feedback: %s",
1292 spirv_decoration_to_string(dec->decoration));
1293 break;
1294
1295 case SpvDecorationCPacked:
1296 case SpvDecorationSaturatedConversion:
1297 case SpvDecorationFuncParamAttr:
1298 case SpvDecorationFPRoundingMode:
1299 case SpvDecorationFPFastMathMode:
1300 case SpvDecorationAlignment:
1301 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1302 spirv_decoration_to_string(dec->decoration));
1303 break;
1304
1305 default:
1306 unreachable("Unhandled decoration");
1307 }
1308 }
1309
1310 static void
1311 var_is_patch_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1312 const struct vtn_decoration *dec, void *out_is_patch)
1313 {
1314 if (dec->decoration == SpvDecorationPatch) {
1315 *((bool *) out_is_patch) = true;
1316 }
1317 }
1318
1319 static void
1320 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
1321 const struct vtn_decoration *dec, void *void_var)
1322 {
1323 struct vtn_variable *vtn_var = void_var;
1324
1325 /* Handle decorations that apply to a vtn_variable as a whole */
1326 switch (dec->decoration) {
1327 case SpvDecorationBinding:
1328 vtn_var->binding = dec->literals[0];
1329 return;
1330 case SpvDecorationDescriptorSet:
1331 vtn_var->descriptor_set = dec->literals[0];
1332 return;
1333 case SpvDecorationInputAttachmentIndex:
1334 vtn_var->input_attachment_index = dec->literals[0];
1335 return;
1336 case SpvDecorationPatch:
1337 vtn_var->patch = true;
1338 break;
1339 default:
1340 break;
1341 }
1342
1343 if (val->value_type == vtn_value_type_pointer) {
1344 assert(val->pointer->var == void_var);
1345 assert(val->pointer->chain == NULL);
1346 assert(member == -1);
1347 } else {
1348 assert(val->value_type == vtn_value_type_type);
1349 }
1350
1351 /* Location is odd. If applied to a split structure, we have to walk the
1352 * whole thing and accumulate the location. It's easier to handle as a
1353 * special case.
1354 */
1355 if (dec->decoration == SpvDecorationLocation) {
1356 unsigned location = dec->literals[0];
1357 bool is_vertex_input;
1358 if (b->shader->info.stage == MESA_SHADER_FRAGMENT &&
1359 vtn_var->mode == vtn_variable_mode_output) {
1360 is_vertex_input = false;
1361 location += FRAG_RESULT_DATA0;
1362 } else if (b->shader->info.stage == MESA_SHADER_VERTEX &&
1363 vtn_var->mode == vtn_variable_mode_input) {
1364 is_vertex_input = true;
1365 location += VERT_ATTRIB_GENERIC0;
1366 } else if (vtn_var->mode == vtn_variable_mode_input ||
1367 vtn_var->mode == vtn_variable_mode_output) {
1368 is_vertex_input = false;
1369 location += vtn_var->patch ? VARYING_SLOT_PATCH0 : VARYING_SLOT_VAR0;
1370 } else {
1371 vtn_warn("Location must be on input or output variable");
1372 return;
1373 }
1374
1375 if (vtn_var->var) {
1376 /* This handles the member and lone variable cases */
1377 vtn_var->var->data.location = location;
1378 } else {
1379 /* This handles the structure member case */
1380 assert(vtn_var->members);
1381 unsigned length =
1382 glsl_get_length(glsl_without_array(vtn_var->type->type));
1383 for (unsigned i = 0; i < length; i++) {
1384 vtn_var->members[i]->data.location = location;
1385 location +=
1386 glsl_count_attribute_slots(vtn_var->members[i]->interface_type,
1387 is_vertex_input);
1388 }
1389 }
1390 return;
1391 } else {
1392 if (vtn_var->var) {
1393 assert(member <= 0);
1394 apply_var_decoration(b, vtn_var->var, dec);
1395 } else if (vtn_var->members) {
1396 if (member >= 0) {
1397 assert(vtn_var->members);
1398 apply_var_decoration(b, vtn_var->members[member], dec);
1399 } else {
1400 unsigned length =
1401 glsl_get_length(glsl_without_array(vtn_var->type->type));
1402 for (unsigned i = 0; i < length; i++)
1403 apply_var_decoration(b, vtn_var->members[i], dec);
1404 }
1405 } else {
1406 /* A few variables, those with external storage, have no actual
1407 * nir_variables associated with them. Fortunately, all decorations
1408 * we care about for those variables are on the type only.
1409 */
1410 assert(vtn_var->mode == vtn_variable_mode_ubo ||
1411 vtn_var->mode == vtn_variable_mode_ssbo ||
1412 vtn_var->mode == vtn_variable_mode_push_constant);
1413 }
1414 }
1415 }
1416
1417 static enum vtn_variable_mode
1418 vtn_storage_class_to_mode(SpvStorageClass class,
1419 struct vtn_type *interface_type,
1420 nir_variable_mode *nir_mode_out)
1421 {
1422 enum vtn_variable_mode mode;
1423 nir_variable_mode nir_mode;
1424 switch (class) {
1425 case SpvStorageClassUniform:
1426 if (interface_type->block) {
1427 mode = vtn_variable_mode_ubo;
1428 nir_mode = 0;
1429 } else if (interface_type->buffer_block) {
1430 mode = vtn_variable_mode_ssbo;
1431 nir_mode = 0;
1432 } else {
1433 unreachable("Invalid uniform variable type");
1434 }
1435 break;
1436 case SpvStorageClassStorageBuffer:
1437 mode = vtn_variable_mode_ssbo;
1438 nir_mode = 0;
1439 break;
1440 case SpvStorageClassUniformConstant:
1441 if (glsl_type_is_image(interface_type->type)) {
1442 mode = vtn_variable_mode_image;
1443 nir_mode = nir_var_uniform;
1444 } else if (glsl_type_is_sampler(interface_type->type)) {
1445 mode = vtn_variable_mode_sampler;
1446 nir_mode = nir_var_uniform;
1447 } else {
1448 unreachable("Invalid uniform constant variable type");
1449 }
1450 break;
1451 case SpvStorageClassPushConstant:
1452 mode = vtn_variable_mode_push_constant;
1453 nir_mode = nir_var_uniform;
1454 break;
1455 case SpvStorageClassInput:
1456 mode = vtn_variable_mode_input;
1457 nir_mode = nir_var_shader_in;
1458 break;
1459 case SpvStorageClassOutput:
1460 mode = vtn_variable_mode_output;
1461 nir_mode = nir_var_shader_out;
1462 break;
1463 case SpvStorageClassPrivate:
1464 mode = vtn_variable_mode_global;
1465 nir_mode = nir_var_global;
1466 break;
1467 case SpvStorageClassFunction:
1468 mode = vtn_variable_mode_local;
1469 nir_mode = nir_var_local;
1470 break;
1471 case SpvStorageClassWorkgroup:
1472 mode = vtn_variable_mode_workgroup;
1473 nir_mode = nir_var_shared;
1474 break;
1475 case SpvStorageClassCrossWorkgroup:
1476 case SpvStorageClassGeneric:
1477 case SpvStorageClassAtomicCounter:
1478 default:
1479 unreachable("Unhandled variable storage class");
1480 }
1481
1482 if (nir_mode_out)
1483 *nir_mode_out = nir_mode;
1484
1485 return mode;
1486 }
1487
1488 nir_ssa_def *
1489 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr)
1490 {
1491 /* This pointer needs to have a pointer type with actual storage */
1492 assert(ptr->ptr_type);
1493 assert(ptr->ptr_type->type);
1494
1495 if (ptr->offset && ptr->block_index) {
1496 return nir_vec2(&b->nb, ptr->block_index, ptr->offset);
1497 } else {
1498 /* If we don't have an offset or block index, then we must be a pointer
1499 * to the variable itself.
1500 */
1501 assert(!ptr->offset && !ptr->block_index);
1502
1503 /* We can't handle a pointer to an array of descriptors because we have
1504 * no way of knowing later on that we need to add to update the block
1505 * index when dereferencing.
1506 */
1507 assert(ptr->var && ptr->var->type->base_type == vtn_base_type_struct);
1508
1509 return nir_vec2(&b->nb, vtn_variable_resource_index(b, ptr->var, NULL),
1510 nir_imm_int(&b->nb, 0));
1511 }
1512 }
1513
1514 struct vtn_pointer *
1515 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
1516 struct vtn_type *ptr_type)
1517 {
1518 assert(ssa->num_components == 2 && ssa->bit_size == 32);
1519 assert(ptr_type->base_type == vtn_base_type_pointer);
1520 assert(ptr_type->deref->base_type != vtn_base_type_pointer);
1521 /* This pointer type needs to have actual storage */
1522 assert(ptr_type->type);
1523
1524 struct vtn_pointer *ptr = rzalloc(b, struct vtn_pointer);
1525 ptr->mode = vtn_storage_class_to_mode(ptr_type->storage_class,
1526 ptr_type, NULL);
1527 ptr->type = ptr_type->deref;
1528 ptr->ptr_type = ptr_type;
1529 ptr->block_index = nir_channel(&b->nb, ssa, 0);
1530 ptr->offset = nir_channel(&b->nb, ssa, 1);
1531
1532 return ptr;
1533 }
1534
1535 static bool
1536 is_per_vertex_inout(const struct vtn_variable *var, gl_shader_stage stage)
1537 {
1538 if (var->patch || !glsl_type_is_array(var->type->type))
1539 return false;
1540
1541 if (var->mode == vtn_variable_mode_input) {
1542 return stage == MESA_SHADER_TESS_CTRL ||
1543 stage == MESA_SHADER_TESS_EVAL ||
1544 stage == MESA_SHADER_GEOMETRY;
1545 }
1546
1547 if (var->mode == vtn_variable_mode_output)
1548 return stage == MESA_SHADER_TESS_CTRL;
1549
1550 return false;
1551 }
1552
1553 static void
1554 vtn_create_variable(struct vtn_builder *b, struct vtn_value *val,
1555 struct vtn_type *ptr_type, SpvStorageClass storage_class,
1556 nir_constant *initializer)
1557 {
1558 assert(ptr_type->base_type == vtn_base_type_pointer);
1559 struct vtn_type *type = ptr_type->deref;
1560
1561 struct vtn_type *without_array = type;
1562 while(glsl_type_is_array(without_array->type))
1563 without_array = without_array->array_element;
1564
1565 enum vtn_variable_mode mode;
1566 nir_variable_mode nir_mode;
1567 mode = vtn_storage_class_to_mode(storage_class, without_array, &nir_mode);
1568
1569 switch (mode) {
1570 case vtn_variable_mode_ubo:
1571 b->shader->info.num_ubos++;
1572 break;
1573 case vtn_variable_mode_ssbo:
1574 b->shader->info.num_ssbos++;
1575 break;
1576 case vtn_variable_mode_image:
1577 b->shader->info.num_images++;
1578 break;
1579 case vtn_variable_mode_sampler:
1580 b->shader->info.num_textures++;
1581 break;
1582 case vtn_variable_mode_push_constant:
1583 b->shader->num_uniforms = vtn_type_block_size(type);
1584 break;
1585 default:
1586 /* No tallying is needed */
1587 break;
1588 }
1589
1590 struct vtn_variable *var = rzalloc(b, struct vtn_variable);
1591 var->type = type;
1592 var->mode = mode;
1593
1594 assert(val->value_type == vtn_value_type_pointer);
1595 val->pointer = vtn_pointer_for_variable(b, var, ptr_type);
1596
1597 switch (var->mode) {
1598 case vtn_variable_mode_local:
1599 case vtn_variable_mode_global:
1600 case vtn_variable_mode_image:
1601 case vtn_variable_mode_sampler:
1602 case vtn_variable_mode_workgroup:
1603 /* For these, we create the variable normally */
1604 var->var = rzalloc(b->shader, nir_variable);
1605 var->var->name = ralloc_strdup(var->var, val->name);
1606 var->var->type = var->type->type;
1607 var->var->data.mode = nir_mode;
1608
1609 switch (var->mode) {
1610 case vtn_variable_mode_image:
1611 case vtn_variable_mode_sampler:
1612 var->var->interface_type = without_array->type;
1613 break;
1614 default:
1615 var->var->interface_type = NULL;
1616 break;
1617 }
1618 break;
1619
1620 case vtn_variable_mode_input:
1621 case vtn_variable_mode_output: {
1622 /* In order to know whether or not we're a per-vertex inout, we need
1623 * the patch qualifier. This means walking the variable decorations
1624 * early before we actually create any variables. Not a big deal.
1625 *
1626 * GLSLang really likes to place decorations in the most interior
1627 * thing it possibly can. In particular, if you have a struct, it
1628 * will place the patch decorations on the struct members. This
1629 * should be handled by the variable splitting below just fine.
1630 *
1631 * If you have an array-of-struct, things get even more weird as it
1632 * will place the patch decorations on the struct even though it's
1633 * inside an array and some of the members being patch and others not
1634 * makes no sense whatsoever. Since the only sensible thing is for
1635 * it to be all or nothing, we'll call it patch if any of the members
1636 * are declared patch.
1637 */
1638 var->patch = false;
1639 vtn_foreach_decoration(b, val, var_is_patch_cb, &var->patch);
1640 if (glsl_type_is_array(var->type->type) &&
1641 glsl_type_is_struct(without_array->type)) {
1642 vtn_foreach_decoration(b, without_array->val,
1643 var_is_patch_cb, &var->patch);
1644 }
1645
1646 /* For inputs and outputs, we immediately split structures. This
1647 * is for a couple of reasons. For one, builtins may all come in
1648 * a struct and we really want those split out into separate
1649 * variables. For another, interpolation qualifiers can be
1650 * applied to members of the top-level struct ane we need to be
1651 * able to preserve that information.
1652 */
1653
1654 int array_length = -1;
1655 struct vtn_type *interface_type = var->type;
1656 if (is_per_vertex_inout(var, b->shader->info.stage)) {
1657 /* In Geometry shaders (and some tessellation), inputs come
1658 * in per-vertex arrays. However, some builtins come in
1659 * non-per-vertex, hence the need for the is_array check. In
1660 * any case, there are no non-builtin arrays allowed so this
1661 * check should be sufficient.
1662 */
1663 interface_type = var->type->array_element;
1664 array_length = glsl_get_length(var->type->type);
1665 }
1666
1667 if (glsl_type_is_struct(interface_type->type)) {
1668 /* It's a struct. Split it. */
1669 unsigned num_members = glsl_get_length(interface_type->type);
1670 var->members = ralloc_array(b, nir_variable *, num_members);
1671
1672 for (unsigned i = 0; i < num_members; i++) {
1673 const struct glsl_type *mtype = interface_type->members[i]->type;
1674 if (array_length >= 0)
1675 mtype = glsl_array_type(mtype, array_length);
1676
1677 var->members[i] = rzalloc(b->shader, nir_variable);
1678 var->members[i]->name =
1679 ralloc_asprintf(var->members[i], "%s.%d", val->name, i);
1680 var->members[i]->type = mtype;
1681 var->members[i]->interface_type =
1682 interface_type->members[i]->type;
1683 var->members[i]->data.mode = nir_mode;
1684 var->members[i]->data.patch = var->patch;
1685 }
1686 } else {
1687 var->var = rzalloc(b->shader, nir_variable);
1688 var->var->name = ralloc_strdup(var->var, val->name);
1689 var->var->type = var->type->type;
1690 var->var->interface_type = interface_type->type;
1691 var->var->data.mode = nir_mode;
1692 var->var->data.patch = var->patch;
1693 }
1694
1695 /* For inputs and outputs, we need to grab locations and builtin
1696 * information from the interface type.
1697 */
1698 vtn_foreach_decoration(b, interface_type->val, var_decoration_cb, var);
1699 break;
1700 }
1701
1702 case vtn_variable_mode_param:
1703 unreachable("Not created through OpVariable");
1704
1705 case vtn_variable_mode_ubo:
1706 case vtn_variable_mode_ssbo:
1707 case vtn_variable_mode_push_constant:
1708 /* These don't need actual variables. */
1709 break;
1710 }
1711
1712 if (initializer) {
1713 var->var->constant_initializer =
1714 nir_constant_clone(initializer, var->var);
1715 }
1716
1717 vtn_foreach_decoration(b, val, var_decoration_cb, var);
1718
1719 if (var->mode == vtn_variable_mode_image ||
1720 var->mode == vtn_variable_mode_sampler) {
1721 /* XXX: We still need the binding information in the nir_variable
1722 * for these. We should fix that.
1723 */
1724 var->var->data.binding = var->binding;
1725 var->var->data.descriptor_set = var->descriptor_set;
1726 var->var->data.index = var->input_attachment_index;
1727
1728 if (var->mode == vtn_variable_mode_image)
1729 var->var->data.image.format = without_array->image_format;
1730 }
1731
1732 if (var->mode == vtn_variable_mode_local) {
1733 assert(var->members == NULL && var->var != NULL);
1734 nir_function_impl_add_variable(b->impl, var->var);
1735 } else if (var->var) {
1736 nir_shader_add_variable(b->shader, var->var);
1737 } else if (var->members) {
1738 unsigned count = glsl_get_length(without_array->type);
1739 for (unsigned i = 0; i < count; i++) {
1740 assert(var->members[i]->data.mode != nir_var_local);
1741 nir_shader_add_variable(b->shader, var->members[i]);
1742 }
1743 } else {
1744 assert(var->mode == vtn_variable_mode_ubo ||
1745 var->mode == vtn_variable_mode_ssbo ||
1746 var->mode == vtn_variable_mode_push_constant);
1747 }
1748 }
1749
1750 void
1751 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
1752 const uint32_t *w, unsigned count)
1753 {
1754 switch (opcode) {
1755 case SpvOpUndef: {
1756 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
1757 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
1758 break;
1759 }
1760
1761 case SpvOpVariable: {
1762 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1763
1764 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
1765
1766 SpvStorageClass storage_class = w[3];
1767 nir_constant *initializer = NULL;
1768 if (count > 4)
1769 initializer = vtn_value(b, w[4], vtn_value_type_constant)->constant;
1770
1771 vtn_create_variable(b, val, ptr_type, storage_class, initializer);
1772 break;
1773 }
1774
1775 case SpvOpAccessChain:
1776 case SpvOpPtrAccessChain:
1777 case SpvOpInBoundsAccessChain: {
1778 struct vtn_access_chain *chain = vtn_access_chain_create(b, count - 4);
1779 chain->ptr_as_array = (opcode == SpvOpPtrAccessChain);
1780
1781 unsigned idx = 0;
1782 for (int i = 4; i < count; i++) {
1783 struct vtn_value *link_val = vtn_untyped_value(b, w[i]);
1784 if (link_val->value_type == vtn_value_type_constant) {
1785 chain->link[idx].mode = vtn_access_mode_literal;
1786 chain->link[idx].id = link_val->constant->values[0].u32[0];
1787 } else {
1788 chain->link[idx].mode = vtn_access_mode_id;
1789 chain->link[idx].id = w[i];
1790
1791 }
1792 idx++;
1793 }
1794
1795 struct vtn_type *ptr_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1796 struct vtn_value *base_val = vtn_untyped_value(b, w[3]);
1797 if (base_val->value_type == vtn_value_type_sampled_image) {
1798 /* This is rather insane. SPIR-V allows you to use OpSampledImage
1799 * to combine an array of images with a single sampler to get an
1800 * array of sampled images that all share the same sampler.
1801 * Fortunately, this means that we can more-or-less ignore the
1802 * sampler when crawling the access chain, but it does leave us
1803 * with this rather awkward little special-case.
1804 */
1805 struct vtn_value *val =
1806 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1807 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1808 val->sampled_image->image =
1809 vtn_pointer_dereference(b, base_val->sampled_image->image, chain);
1810 val->sampled_image->sampler = base_val->sampled_image->sampler;
1811 } else {
1812 assert(base_val->value_type == vtn_value_type_pointer);
1813 struct vtn_value *val =
1814 vtn_push_value(b, w[2], vtn_value_type_pointer);
1815 val->pointer = vtn_pointer_dereference(b, base_val->pointer, chain);
1816 val->pointer->ptr_type = ptr_type;
1817 }
1818 break;
1819 }
1820
1821 case SpvOpCopyMemory: {
1822 struct vtn_value *dest = vtn_value(b, w[1], vtn_value_type_pointer);
1823 struct vtn_value *src = vtn_value(b, w[2], vtn_value_type_pointer);
1824
1825 vtn_variable_copy(b, dest->pointer, src->pointer);
1826 break;
1827 }
1828
1829 case SpvOpLoad: {
1830 struct vtn_type *res_type =
1831 vtn_value(b, w[1], vtn_value_type_type)->type;
1832 struct vtn_pointer *src =
1833 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1834
1835 if (src->mode == vtn_variable_mode_image ||
1836 src->mode == vtn_variable_mode_sampler) {
1837 vtn_push_value(b, w[2], vtn_value_type_pointer)->pointer = src;
1838 return;
1839 }
1840
1841 vtn_push_ssa(b, w[2], res_type, vtn_variable_load(b, src));
1842 break;
1843 }
1844
1845 case SpvOpStore: {
1846 struct vtn_pointer *dest =
1847 vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
1848
1849 if (glsl_type_is_sampler(dest->type->type)) {
1850 vtn_warn("OpStore of a sampler detected. Doing on-the-fly copy "
1851 "propagation to workaround the problem.");
1852 assert(dest->var->copy_prop_sampler == NULL);
1853 dest->var->copy_prop_sampler =
1854 vtn_value(b, w[2], vtn_value_type_pointer)->pointer;
1855 break;
1856 }
1857
1858 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
1859 vtn_variable_store(b, src, dest);
1860 break;
1861 }
1862
1863 case SpvOpArrayLength: {
1864 struct vtn_pointer *ptr =
1865 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1866
1867 const uint32_t offset = ptr->var->type->offsets[w[4]];
1868 const uint32_t stride = ptr->var->type->members[w[4]]->stride;
1869
1870 unsigned chain_idx;
1871 struct vtn_type *type;
1872 nir_ssa_def *index =
1873 get_vulkan_resource_index(b, ptr, &type, &chain_idx);
1874
1875 nir_intrinsic_instr *instr =
1876 nir_intrinsic_instr_create(b->nb.shader,
1877 nir_intrinsic_get_buffer_size);
1878 instr->src[0] = nir_src_for_ssa(index);
1879 nir_ssa_dest_init(&instr->instr, &instr->dest, 1, 32, NULL);
1880 nir_builder_instr_insert(&b->nb, &instr->instr);
1881 nir_ssa_def *buf_size = &instr->dest.ssa;
1882
1883 /* array_length = max(buffer_size - offset, 0) / stride */
1884 nir_ssa_def *array_length =
1885 nir_idiv(&b->nb,
1886 nir_imax(&b->nb,
1887 nir_isub(&b->nb,
1888 buf_size,
1889 nir_imm_int(&b->nb, offset)),
1890 nir_imm_int(&b->nb, 0u)),
1891 nir_imm_int(&b->nb, stride));
1892
1893 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1894 val->ssa = vtn_create_ssa_value(b, glsl_uint_type());
1895 val->ssa->def = array_length;
1896 break;
1897 }
1898
1899 case SpvOpCopyMemorySized:
1900 default:
1901 unreachable("Unhandled opcode");
1902 }
1903 }