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