a042afe4bbe971dcff5675ae9a0b0755db8151d3
[mesa.git] / src / compiler / spirv / vtn_private.h
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 #ifndef _VTN_PRIVATE_H_
29 #define _VTN_PRIVATE_H_
30
31 #include <setjmp.h>
32
33 #include "nir/nir.h"
34 #include "nir/nir_builder.h"
35 #include "util/u_dynarray.h"
36 #include "nir_spirv.h"
37 #include "spirv.h"
38
39 struct vtn_builder;
40 struct vtn_decoration;
41
42 void vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
43 size_t spirv_offset, const char *message);
44
45 void vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
46 size_t spirv_offset, const char *fmt, ...) PRINTFLIKE(4, 5);
47
48 #define vtn_info(...) vtn_logf(b, NIR_SPIRV_DEBUG_LEVEL_INFO, 0, __VA_ARGS__)
49
50 void _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
51 const char *fmt, ...) PRINTFLIKE(4, 5);
52 #define vtn_warn(...) _vtn_warn(b, __FILE__, __LINE__, __VA_ARGS__)
53
54 void _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
55 const char *fmt, ...) PRINTFLIKE(4, 5);
56 #define vtn_err(...) _vtn_err(b, __FILE__, __LINE__, __VA_ARGS__)
57
58 /** Fail SPIR-V parsing
59 *
60 * This function logs an error and then bails out of the shader compile using
61 * longjmp. This being safe relies on two things:
62 *
63 * 1) We must guarantee that setjmp is called after allocating the builder
64 * and setting up b->debug (so that logging works) but before before any
65 * errors have a chance to occur.
66 *
67 * 2) While doing the SPIR-V -> NIR conversion, we need to be careful to
68 * ensure that all heap allocations happen through ralloc and are parented
69 * to the builder. This way they will get properly cleaned up on error.
70 *
71 * 3) We must ensure that _vtn_fail is never called while a mutex lock or a
72 * reference to any other resource is held with the exception of ralloc
73 * objects which are parented to the builder.
74 *
75 * So long as these two things continue to hold, we can easily longjmp back to
76 * spirv_to_nir(), clean up the builder, and return NULL.
77 */
78 NORETURN void
79 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
80 const char *fmt, ...) PRINTFLIKE(4, 5);
81
82 #define vtn_fail(...) _vtn_fail(b, __FILE__, __LINE__, __VA_ARGS__)
83
84 /** Fail if the given expression evaluates to true */
85 #define vtn_fail_if(expr, ...) \
86 do { \
87 if (unlikely(expr)) \
88 vtn_fail(__VA_ARGS__); \
89 } while (0)
90
91 #define _vtn_fail_with(t, msg, v) \
92 vtn_fail("%s: %s (%u)\n", msg, spirv_ ## t ## _to_string(v), v)
93
94 #define vtn_fail_with_decoration(msg, v) _vtn_fail_with(decoration, msg, v)
95 #define vtn_fail_with_opcode(msg, v) _vtn_fail_with(op, msg, v)
96
97 /** Assert that a condition is true and, if it isn't, vtn_fail
98 *
99 * This macro is transitional only and should not be used in new code. Use
100 * vtn_fail_if and provide a real message instead.
101 */
102 #define vtn_assert(expr) \
103 do { \
104 if (!likely(expr)) \
105 vtn_fail("%s", #expr); \
106 } while (0)
107
108 enum vtn_value_type {
109 vtn_value_type_invalid = 0,
110 vtn_value_type_undef,
111 vtn_value_type_string,
112 vtn_value_type_decoration_group,
113 vtn_value_type_type,
114 vtn_value_type_constant,
115 vtn_value_type_pointer,
116 vtn_value_type_function,
117 vtn_value_type_block,
118 vtn_value_type_ssa,
119 vtn_value_type_extension,
120 vtn_value_type_image_pointer,
121 vtn_value_type_sampled_image,
122 };
123
124 enum vtn_branch_type {
125 vtn_branch_type_none,
126 vtn_branch_type_switch_break,
127 vtn_branch_type_switch_fallthrough,
128 vtn_branch_type_loop_break,
129 vtn_branch_type_loop_continue,
130 vtn_branch_type_discard,
131 vtn_branch_type_return,
132 };
133
134 enum vtn_cf_node_type {
135 vtn_cf_node_type_block,
136 vtn_cf_node_type_if,
137 vtn_cf_node_type_loop,
138 vtn_cf_node_type_switch,
139 };
140
141 struct vtn_cf_node {
142 struct list_head link;
143 enum vtn_cf_node_type type;
144 };
145
146 struct vtn_loop {
147 struct vtn_cf_node node;
148
149 /* The main body of the loop */
150 struct list_head body;
151
152 /* The "continue" part of the loop. This gets executed after the body
153 * and is where you go when you hit a continue.
154 */
155 struct list_head cont_body;
156
157 SpvLoopControlMask control;
158 };
159
160 struct vtn_if {
161 struct vtn_cf_node node;
162
163 uint32_t condition;
164
165 enum vtn_branch_type then_type;
166 struct list_head then_body;
167
168 enum vtn_branch_type else_type;
169 struct list_head else_body;
170
171 SpvSelectionControlMask control;
172 };
173
174 struct vtn_case {
175 struct list_head link;
176
177 struct list_head body;
178
179 /* The block that starts this case */
180 struct vtn_block *start_block;
181
182 /* The fallthrough case, if any */
183 struct vtn_case *fallthrough;
184
185 /* The uint32_t values that map to this case */
186 struct util_dynarray values;
187
188 /* True if this is the default case */
189 bool is_default;
190
191 /* Initialized to false; used when sorting the list of cases */
192 bool visited;
193 };
194
195 struct vtn_switch {
196 struct vtn_cf_node node;
197
198 uint32_t selector;
199
200 struct list_head cases;
201 };
202
203 struct vtn_block {
204 struct vtn_cf_node node;
205
206 /** A pointer to the label instruction */
207 const uint32_t *label;
208
209 /** A pointer to the merge instruction (or NULL if non exists) */
210 const uint32_t *merge;
211
212 /** A pointer to the branch instruction that ends this block */
213 const uint32_t *branch;
214
215 enum vtn_branch_type branch_type;
216
217 /** Points to the loop that this block starts (if it starts a loop) */
218 struct vtn_loop *loop;
219
220 /** Points to the switch case started by this block (if any) */
221 struct vtn_case *switch_case;
222
223 /** Every block ends in a nop intrinsic so that we can find it again */
224 nir_intrinsic_instr *end_nop;
225 };
226
227 struct vtn_function {
228 struct exec_node node;
229
230 struct vtn_type *type;
231
232 bool referenced;
233 bool emitted;
234
235 nir_function_impl *impl;
236 struct vtn_block *start_block;
237
238 struct list_head body;
239
240 const uint32_t *end;
241
242 SpvFunctionControlMask control;
243 };
244
245 #define VTN_DECL_CF_NODE_CAST(_type) \
246 static inline struct vtn_##_type * \
247 vtn_cf_node_as_##_type(struct vtn_cf_node *node) \
248 { \
249 assert(node->type == vtn_cf_node_type_##_type); \
250 return (struct vtn_##_type *)node; \
251 }
252
253 VTN_DECL_CF_NODE_CAST(block)
254 VTN_DECL_CF_NODE_CAST(loop)
255 VTN_DECL_CF_NODE_CAST(if)
256 VTN_DECL_CF_NODE_CAST(switch)
257
258 #define vtn_foreach_cf_node(node, cf_list) \
259 list_for_each_entry(struct vtn_cf_node, node, cf_list, link)
260
261 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
262 const uint32_t *, unsigned);
263
264 void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
265 const uint32_t *end);
266 void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
267 vtn_instruction_handler instruction_handler);
268 void vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
269 const uint32_t *w, unsigned count);
270
271 const uint32_t *
272 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
273 const uint32_t *end, vtn_instruction_handler handler);
274
275 struct vtn_ssa_value {
276 union {
277 nir_ssa_def *def;
278 struct vtn_ssa_value **elems;
279 };
280
281 /* For matrices, if this is non-NULL, then this value is actually the
282 * transpose of some other value. The value that `transposed` points to
283 * always dominates this value.
284 */
285 struct vtn_ssa_value *transposed;
286
287 const struct glsl_type *type;
288
289 /* Access qualifiers */
290 enum gl_access_qualifier access;
291 };
292
293 enum vtn_base_type {
294 vtn_base_type_void,
295 vtn_base_type_scalar,
296 vtn_base_type_vector,
297 vtn_base_type_matrix,
298 vtn_base_type_array,
299 vtn_base_type_struct,
300 vtn_base_type_pointer,
301 vtn_base_type_image,
302 vtn_base_type_sampler,
303 vtn_base_type_sampled_image,
304 vtn_base_type_function,
305 };
306
307 struct vtn_type {
308 enum vtn_base_type base_type;
309
310 const struct glsl_type *type;
311
312 /* The SPIR-V id of the given type. */
313 uint32_t id;
314
315 /* Specifies the length of complex types.
316 *
317 * For Workgroup pointers, this is the size of the referenced type.
318 */
319 unsigned length;
320
321 /* for arrays, matrices and pointers, the array stride */
322 unsigned stride;
323
324 /* Access qualifiers */
325 enum gl_access_qualifier access;
326
327 union {
328 /* Members for scalar, vector, and array-like types */
329 struct {
330 /* for arrays, the vtn_type for the elements of the array */
331 struct vtn_type *array_element;
332
333 /* for matrices, whether the matrix is stored row-major */
334 bool row_major:1;
335
336 /* Whether this type, or a parent type, has been decorated as a
337 * builtin
338 */
339 bool is_builtin:1;
340
341 /* Which built-in to use */
342 SpvBuiltIn builtin;
343 };
344
345 /* Members for struct types */
346 struct {
347 /* for structures, the vtn_type for each member */
348 struct vtn_type **members;
349
350 /* for structs, the offset of each member */
351 unsigned *offsets;
352
353 /* for structs, whether it was decorated as a "non-SSBO-like" block */
354 bool block:1;
355
356 /* for structs, whether it was decorated as an "SSBO-like" block */
357 bool buffer_block:1;
358
359 /* for structs with block == true, whether this is a builtin block
360 * (i.e. a block that contains only builtins).
361 */
362 bool builtin_block:1;
363
364 /* for structs and unions it specifies the minimum alignment of the
365 * members. 0 means packed.
366 *
367 * Set by CPacked and Alignment Decorations in kernels.
368 */
369 bool packed:1;
370 };
371
372 /* Members for pointer types */
373 struct {
374 /* For pointers, the vtn_type for dereferenced type */
375 struct vtn_type *deref;
376
377 /* Storage class for pointers */
378 SpvStorageClass storage_class;
379
380 /* Required alignment for pointers */
381 uint32_t align;
382 };
383
384 /* Members for image types */
385 struct {
386 /* For images, indicates whether it's sampled or storage */
387 bool sampled;
388
389 /* Image format for image_load_store type images */
390 unsigned image_format;
391
392 /* Access qualifier for storage images */
393 SpvAccessQualifier access_qualifier;
394 };
395
396 /* Members for sampled image types */
397 struct {
398 /* For sampled images, the image type */
399 struct vtn_type *image;
400 };
401
402 /* Members for function types */
403 struct {
404 /* For functions, the vtn_type for each parameter */
405 struct vtn_type **params;
406
407 /* Return type for functions */
408 struct vtn_type *return_type;
409 };
410 };
411 };
412
413 bool vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type);
414
415 bool vtn_types_compatible(struct vtn_builder *b,
416 struct vtn_type *t1, struct vtn_type *t2);
417
418 struct vtn_type *vtn_type_without_array(struct vtn_type *type);
419
420 struct vtn_variable;
421
422 enum vtn_access_mode {
423 vtn_access_mode_id,
424 vtn_access_mode_literal,
425 };
426
427 struct vtn_access_link {
428 enum vtn_access_mode mode;
429 int64_t id;
430 };
431
432 struct vtn_access_chain {
433 uint32_t length;
434
435 /** Whether or not to treat the base pointer as an array. This is only
436 * true if this access chain came from an OpPtrAccessChain.
437 */
438 bool ptr_as_array;
439
440 /* Access qualifiers */
441 enum gl_access_qualifier access;
442
443 /** Struct elements and array offsets.
444 *
445 * This is an array of 1 so that it can conveniently be created on the
446 * stack but the real length is given by the length field.
447 */
448 struct vtn_access_link link[1];
449 };
450
451 enum vtn_variable_mode {
452 vtn_variable_mode_function,
453 vtn_variable_mode_private,
454 vtn_variable_mode_uniform,
455 vtn_variable_mode_ubo,
456 vtn_variable_mode_ssbo,
457 vtn_variable_mode_phys_ssbo,
458 vtn_variable_mode_push_constant,
459 vtn_variable_mode_workgroup,
460 vtn_variable_mode_cross_workgroup,
461 vtn_variable_mode_input,
462 vtn_variable_mode_output,
463 vtn_variable_mode_image,
464 };
465
466 struct vtn_pointer {
467 /** The variable mode for the referenced data */
468 enum vtn_variable_mode mode;
469
470 /** The dereferenced type of this pointer */
471 struct vtn_type *type;
472
473 /** The pointer type of this pointer
474 *
475 * This may be NULL for some temporary pointers constructed as part of a
476 * large load, store, or copy. It MUST be valid for all pointers which are
477 * stored as SPIR-V SSA values.
478 */
479 struct vtn_type *ptr_type;
480
481 /** The referenced variable, if known
482 *
483 * This field may be NULL if the pointer uses a (block_index, offset) pair
484 * instead of an access chain or if the access chain starts at a deref.
485 */
486 struct vtn_variable *var;
487
488 /** The NIR deref corresponding to this pointer */
489 nir_deref_instr *deref;
490
491 /** A (block_index, offset) pair representing a UBO or SSBO position. */
492 struct nir_ssa_def *block_index;
493 struct nir_ssa_def *offset;
494
495 /* Access qualifiers */
496 enum gl_access_qualifier access;
497 };
498
499 bool vtn_mode_uses_ssa_offset(struct vtn_builder *b,
500 enum vtn_variable_mode mode);
501
502 static inline bool vtn_pointer_uses_ssa_offset(struct vtn_builder *b,
503 struct vtn_pointer *ptr)
504 {
505 return vtn_mode_uses_ssa_offset(b, ptr->mode);
506 }
507
508
509 struct vtn_variable {
510 enum vtn_variable_mode mode;
511
512 struct vtn_type *type;
513
514 unsigned descriptor_set;
515 unsigned binding;
516 bool explicit_binding;
517 unsigned offset;
518 unsigned input_attachment_index;
519 bool patch;
520
521 nir_variable *var;
522
523 /* If the variable is a struct with a location set on it then this will be
524 * stored here. This will be used to calculate locations for members that
525 * don’t have their own explicit location.
526 */
527 int base_location;
528
529 int shared_location;
530
531 /**
532 * In some early released versions of GLSLang, it implemented all function
533 * calls by making copies of all parameters into temporary variables and
534 * passing those variables into the function. It even did so for samplers
535 * and images which violates the SPIR-V spec. Unfortunately, two games
536 * (Talos Principle and Doom) shipped with this old version of GLSLang and
537 * also happen to pass samplers into functions. Talos Principle received
538 * an update fairly shortly after release with an updated GLSLang. Doom,
539 * on the other hand, has never received an update so we need to work
540 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
541 * hack at some point in the future.
542 */
543 struct vtn_pointer *copy_prop_sampler;
544
545 /* Access qualifiers. */
546 enum gl_access_qualifier access;
547 };
548
549 struct vtn_image_pointer {
550 struct vtn_pointer *image;
551 nir_ssa_def *coord;
552 nir_ssa_def *sample;
553 nir_ssa_def *lod;
554 };
555
556 struct vtn_sampled_image {
557 struct vtn_pointer *image; /* Image or array of images */
558 struct vtn_pointer *sampler; /* Sampler */
559 };
560
561 struct vtn_value {
562 enum vtn_value_type value_type;
563 const char *name;
564 struct vtn_decoration *decoration;
565 struct vtn_type *type;
566 union {
567 void *ptr;
568 char *str;
569 nir_constant *constant;
570 struct vtn_pointer *pointer;
571 struct vtn_image_pointer *image;
572 struct vtn_sampled_image *sampled_image;
573 struct vtn_function *func;
574 struct vtn_block *block;
575 struct vtn_ssa_value *ssa;
576 vtn_instruction_handler ext_handler;
577 };
578 };
579
580 #define VTN_DEC_DECORATION -1
581 #define VTN_DEC_EXECUTION_MODE -2
582 #define VTN_DEC_STRUCT_MEMBER0 0
583
584 struct vtn_decoration {
585 struct vtn_decoration *next;
586
587 /* Specifies how to apply this decoration. Negative values represent a
588 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
589 * Non-negative values specify that it applies to a structure member.
590 */
591 int scope;
592
593 const uint32_t *operands;
594 struct vtn_value *group;
595
596 union {
597 SpvDecoration decoration;
598 SpvExecutionMode exec_mode;
599 };
600 };
601
602 struct vtn_builder {
603 nir_builder nb;
604
605 /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
606 jmp_buf fail_jump;
607
608 const uint32_t *spirv;
609 size_t spirv_word_count;
610
611 nir_shader *shader;
612 struct spirv_to_nir_options *options;
613 struct vtn_block *block;
614
615 /* Current offset, file, line, and column. Useful for debugging. Set
616 * automatically by vtn_foreach_instruction.
617 */
618 size_t spirv_offset;
619 char *file;
620 int line, col;
621
622 /*
623 * In SPIR-V, constants are global, whereas in NIR, the load_const
624 * instruction we use is per-function. So while we parse each function, we
625 * keep a hash table of constants we've resolved to nir_ssa_value's so
626 * far, and we lazily resolve them when we see them used in a function.
627 */
628 struct hash_table *const_table;
629
630 /*
631 * Map from phi instructions (pointer to the start of the instruction)
632 * to the variable corresponding to it.
633 */
634 struct hash_table *phi_table;
635
636 unsigned num_specializations;
637 struct nir_spirv_specialization *specializations;
638
639 unsigned value_id_bound;
640 struct vtn_value *values;
641
642 /* True if we should watch out for GLSLang issue #179 */
643 bool wa_glslang_179;
644
645 /* True if we need to fix up CS OpControlBarrier */
646 bool wa_glslang_cs_barrier;
647
648 gl_shader_stage entry_point_stage;
649 const char *entry_point_name;
650 struct vtn_value *entry_point;
651 struct vtn_value *workgroup_size_builtin;
652 bool variable_pointers;
653
654 struct vtn_function *func;
655 struct exec_list functions;
656
657 /* Current function parameter index */
658 unsigned func_param_idx;
659
660 bool has_loop_continue;
661
662 /* false by default, set to true by the ContractionOff execution mode */
663 bool exact;
664
665 /* when a physical memory model is choosen */
666 bool physical_ptrs;
667 };
668
669 nir_ssa_def *
670 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
671 struct vtn_pointer *
672 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
673 struct vtn_type *ptr_type);
674
675 static inline struct vtn_value *
676 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
677 {
678 vtn_fail_if(value_id >= b->value_id_bound,
679 "SPIR-V id %u is out-of-bounds", value_id);
680 return &b->values[value_id];
681 }
682
683 /* Consider not using this function directly and instead use
684 * vtn_push_ssa/vtn_push_value_pointer so that appropriate applying of
685 * decorations is handled by common code.
686 */
687 static inline struct vtn_value *
688 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
689 enum vtn_value_type value_type)
690 {
691 struct vtn_value *val = vtn_untyped_value(b, value_id);
692
693 vtn_fail_if(val->value_type != vtn_value_type_invalid,
694 "SPIR-V id %u has already been written by another instruction",
695 value_id);
696
697 val->value_type = value_type;
698
699 return &b->values[value_id];
700 }
701
702 static inline struct vtn_value *
703 vtn_value(struct vtn_builder *b, uint32_t value_id,
704 enum vtn_value_type value_type)
705 {
706 struct vtn_value *val = vtn_untyped_value(b, value_id);
707 vtn_fail_if(val->value_type != value_type,
708 "SPIR-V id %u is the wrong kind of value", value_id);
709 return val;
710 }
711
712 bool
713 vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
714 const uint32_t *w, unsigned count);
715
716 static inline uint64_t
717 vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
718 {
719 struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
720
721 vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
722 !glsl_type_is_integer(val->type->type),
723 "Expected id %u to be an integer constant", value_id);
724
725 switch (glsl_get_bit_size(val->type->type)) {
726 case 8: return val->constant->values[0].u8;
727 case 16: return val->constant->values[0].u16;
728 case 32: return val->constant->values[0].u32;
729 case 64: return val->constant->values[0].u64;
730 default: unreachable("Invalid bit size");
731 }
732 }
733
734 static inline int64_t
735 vtn_constant_int(struct vtn_builder *b, uint32_t value_id)
736 {
737 struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
738
739 vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
740 !glsl_type_is_integer(val->type->type),
741 "Expected id %u to be an integer constant", value_id);
742
743 switch (glsl_get_bit_size(val->type->type)) {
744 case 8: return val->constant->values[0].i8;
745 case 16: return val->constant->values[0].i16;
746 case 32: return val->constant->values[0].i32;
747 case 64: return val->constant->values[0].i64;
748 default: unreachable("Invalid bit size");
749 }
750 }
751
752 static inline enum gl_access_qualifier vtn_value_access(struct vtn_value *value)
753 {
754 switch (value->value_type) {
755 case vtn_value_type_invalid:
756 case vtn_value_type_undef:
757 case vtn_value_type_string:
758 case vtn_value_type_decoration_group:
759 case vtn_value_type_constant:
760 case vtn_value_type_function:
761 case vtn_value_type_block:
762 case vtn_value_type_extension:
763 return 0;
764 case vtn_value_type_type:
765 return value->type->access;
766 case vtn_value_type_pointer:
767 return value->pointer->access;
768 case vtn_value_type_ssa:
769 return value->ssa->access;
770 case vtn_value_type_image_pointer:
771 return value->image->image->access;
772 case vtn_value_type_sampled_image:
773 return value->sampled_image->image->access |
774 value->sampled_image->sampler->access;
775 }
776
777 unreachable("invalid type");
778 }
779
780 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
781
782 struct vtn_value *vtn_push_value_pointer(struct vtn_builder *b,
783 uint32_t value_id,
784 struct vtn_pointer *ptr);
785
786 struct vtn_value *vtn_push_ssa(struct vtn_builder *b, uint32_t value_id,
787 struct vtn_type *type, struct vtn_ssa_value *ssa);
788
789 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
790 const struct glsl_type *type);
791
792 struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
793 struct vtn_ssa_value *src);
794
795 nir_ssa_def *vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src,
796 unsigned index);
797 nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
798 nir_ssa_def *index);
799 nir_ssa_def *vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src,
800 nir_ssa_def *insert, unsigned index);
801 nir_ssa_def *vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
802 nir_ssa_def *insert, nir_ssa_def *index);
803
804 nir_deref_instr *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
805
806 struct vtn_pointer *vtn_pointer_for_variable(struct vtn_builder *b,
807 struct vtn_variable *var,
808 struct vtn_type *ptr_type);
809
810 nir_deref_instr *vtn_pointer_to_deref(struct vtn_builder *b,
811 struct vtn_pointer *ptr);
812 nir_ssa_def *
813 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
814 nir_ssa_def **index_out);
815
816 struct vtn_ssa_value *
817 vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
818 enum gl_access_qualifier access);
819
820 void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
821 nir_deref_instr *dest,
822 enum gl_access_qualifier access);
823
824 struct vtn_ssa_value *
825 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src);
826
827 void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
828 struct vtn_pointer *dest);
829
830 void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
831 const uint32_t *w, unsigned count);
832
833
834 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
835 struct vtn_value *,
836 int member,
837 const struct vtn_decoration *,
838 void *);
839
840 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
841 vtn_decoration_foreach_cb cb, void *data);
842
843 typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
844 struct vtn_value *,
845 const struct vtn_decoration *,
846 void *);
847
848 void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
849 vtn_execution_mode_foreach_cb cb, void *data);
850
851 nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
852 SpvOp opcode, bool *swap,
853 unsigned src_bit_size, unsigned dst_bit_size);
854
855 void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
856 const uint32_t *w, unsigned count);
857
858 void vtn_handle_bitcast(struct vtn_builder *b, const uint32_t *w,
859 unsigned count);
860
861 void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
862 const uint32_t *w, unsigned count);
863
864 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
865 const uint32_t *words, unsigned count);
866
867 bool vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
868 const uint32_t *words, unsigned count);
869
870 struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
871 gl_shader_stage stage, const char *entry_point_name,
872 const struct spirv_to_nir_options *options);
873
874 void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
875 unsigned count);
876
877 void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
878 const uint32_t *w, unsigned count);
879
880 enum vtn_variable_mode vtn_storage_class_to_mode(struct vtn_builder *b,
881 SpvStorageClass class,
882 struct vtn_type *interface_type,
883 nir_variable_mode *nir_mode_out);
884
885 nir_address_format vtn_mode_to_address_format(struct vtn_builder *b,
886 enum vtn_variable_mode);
887
888 static inline uint32_t
889 vtn_align_u32(uint32_t v, uint32_t a)
890 {
891 assert(a != 0 && a == (a & -((int32_t) a)));
892 return (v + a - 1) & ~(a - 1);
893 }
894
895 static inline uint64_t
896 vtn_u64_literal(const uint32_t *w)
897 {
898 return (uint64_t)w[1] << 32 | w[0];
899 }
900
901 bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
902 const uint32_t *words, unsigned count);
903
904 bool vtn_handle_amd_shader_ballot_instruction(struct vtn_builder *b, SpvOp ext_opcode,
905 const uint32_t *w, unsigned count);
906
907 bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
908 const uint32_t *words, unsigned count);
909
910 bool vtn_handle_amd_shader_explicit_vertex_parameter_instruction(struct vtn_builder *b,
911 SpvOp ext_opcode,
912 const uint32_t *words,
913 unsigned count);
914
915 SpvMemorySemanticsMask vtn_storage_class_to_memory_semantics(SpvStorageClass sc);
916
917 void vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
918 SpvMemorySemanticsMask semantics);
919
920 #endif /* _VTN_PRIVATE_H_ */