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
39struct vtn_builder;
40struct vtn_decoration;
41
42void vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
43             size_t spirv_offset, const char *message);
44
45void 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
50void _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
54void _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 */
78NORETURN 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
108enum 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
124enum 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
134enum 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
141struct vtn_cf_node {
142   struct list_head link;
143   enum vtn_cf_node_type type;
144};
145
146struct 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
160struct 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
174struct 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
195struct vtn_switch {
196   struct vtn_cf_node node;
197
198   uint32_t selector;
199
200   struct list_head cases;
201};
202
203struct 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
227struct 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
245typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
246                                        const uint32_t *, unsigned);
247
248void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
249                   const uint32_t *end);
250void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
251                       vtn_instruction_handler instruction_handler);
252void vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
253                              const uint32_t *w, unsigned count);
254
255const uint32_t *
256vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
257                        const uint32_t *end, vtn_instruction_handler handler);
258
259struct vtn_ssa_value {
260   union {
261      nir_ssa_def *def;
262      struct vtn_ssa_value **elems;
263   };
264
265   /* For matrices, if this is non-NULL, then this value is actually the
266    * transpose of some other value.  The value that `transposed` points to
267    * always dominates this value.
268    */
269   struct vtn_ssa_value *transposed;
270
271   const struct glsl_type *type;
272
273   /* Access qualifiers */
274   enum gl_access_qualifier access;
275};
276
277enum vtn_base_type {
278   vtn_base_type_void,
279   vtn_base_type_scalar,
280   vtn_base_type_vector,
281   vtn_base_type_matrix,
282   vtn_base_type_array,
283   vtn_base_type_struct,
284   vtn_base_type_pointer,
285   vtn_base_type_image,
286   vtn_base_type_sampler,
287   vtn_base_type_sampled_image,
288   vtn_base_type_function,
289};
290
291struct vtn_type {
292   enum vtn_base_type base_type;
293
294   const struct glsl_type *type;
295
296   /* The SPIR-V id of the given type. */
297   uint32_t id;
298
299   /* Specifies the length of complex types.
300    *
301    * For Workgroup pointers, this is the size of the referenced type.
302    */
303   unsigned length;
304
305   /* for arrays, matrices and pointers, the array stride */
306   unsigned stride;
307
308   /* Access qualifiers */
309   enum gl_access_qualifier access;
310
311   union {
312      /* Members for scalar, vector, and array-like types */
313      struct {
314         /* for arrays, the vtn_type for the elements of the array */
315         struct vtn_type *array_element;
316
317         /* for matrices, whether the matrix is stored row-major */
318         bool row_major:1;
319
320         /* Whether this type, or a parent type, has been decorated as a
321          * builtin
322          */
323         bool is_builtin:1;
324
325         /* Which built-in to use */
326         SpvBuiltIn builtin;
327      };
328
329      /* Members for struct types */
330      struct {
331         /* for structures, the vtn_type for each member */
332         struct vtn_type **members;
333
334         /* for structs, the offset of each member */
335         unsigned *offsets;
336
337         /* for structs, whether it was decorated as a "non-SSBO-like" block */
338         bool block:1;
339
340         /* for structs, whether it was decorated as an "SSBO-like" block */
341         bool buffer_block:1;
342
343         /* for structs with block == true, whether this is a builtin block
344          * (i.e. a block that contains only builtins).
345          */
346         bool builtin_block:1;
347
348         /* for structs and unions it specifies the minimum alignment of the
349          * members. 0 means packed.
350          *
351          * Set by CPacked and Alignment Decorations in kernels.
352          */
353         bool packed:1;
354      };
355
356      /* Members for pointer types */
357      struct {
358         /* For pointers, the vtn_type for dereferenced type */
359         struct vtn_type *deref;
360
361         /* Storage class for pointers */
362         SpvStorageClass storage_class;
363
364         /* Required alignment for pointers */
365         uint32_t align;
366      };
367
368      /* Members for image types */
369      struct {
370         /* For images, indicates whether it's sampled or storage */
371         bool sampled;
372
373         /* Image format for image_load_store type images */
374         unsigned image_format;
375
376         /* Access qualifier for storage images */
377         SpvAccessQualifier access_qualifier;
378      };
379
380      /* Members for sampled image types */
381      struct {
382         /* For sampled images, the image type */
383         struct vtn_type *image;
384      };
385
386      /* Members for function types */
387      struct {
388         /* For functions, the vtn_type for each parameter */
389         struct vtn_type **params;
390
391         /* Return type for functions */
392         struct vtn_type *return_type;
393      };
394   };
395};
396
397bool vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type);
398
399bool vtn_types_compatible(struct vtn_builder *b,
400                          struct vtn_type *t1, struct vtn_type *t2);
401
402struct vtn_variable;
403
404enum vtn_access_mode {
405   vtn_access_mode_id,
406   vtn_access_mode_literal,
407};
408
409struct vtn_access_link {
410   enum vtn_access_mode mode;
411   int64_t id;
412};
413
414struct vtn_access_chain {
415   uint32_t length;
416
417   /** Whether or not to treat the base pointer as an array.  This is only
418    * true if this access chain came from an OpPtrAccessChain.
419    */
420   bool ptr_as_array;
421
422   /* Access qualifiers */
423   enum gl_access_qualifier access;
424
425   /** Struct elements and array offsets.
426    *
427    * This is an array of 1 so that it can conveniently be created on the
428    * stack but the real length is given by the length field.
429    */
430   struct vtn_access_link link[1];
431};
432
433enum vtn_variable_mode {
434   vtn_variable_mode_function,
435   vtn_variable_mode_private,
436   vtn_variable_mode_uniform,
437   vtn_variable_mode_ubo,
438   vtn_variable_mode_ssbo,
439   vtn_variable_mode_phys_ssbo,
440   vtn_variable_mode_push_constant,
441   vtn_variable_mode_workgroup,
442   vtn_variable_mode_cross_workgroup,
443   vtn_variable_mode_input,
444   vtn_variable_mode_output,
445};
446
447struct vtn_pointer {
448   /** The variable mode for the referenced data */
449   enum vtn_variable_mode mode;
450
451   /** The dereferenced type of this pointer */
452   struct vtn_type *type;
453
454   /** The pointer type of this pointer
455    *
456    * This may be NULL for some temporary pointers constructed as part of a
457    * large load, store, or copy.  It MUST be valid for all pointers which are
458    * stored as SPIR-V SSA values.
459    */
460   struct vtn_type *ptr_type;
461
462   /** The referenced variable, if known
463    *
464    * This field may be NULL if the pointer uses a (block_index, offset) pair
465    * instead of an access chain or if the access chain starts at a deref.
466    */
467   struct vtn_variable *var;
468
469   /** The NIR deref corresponding to this pointer */
470   nir_deref_instr *deref;
471
472   /** A (block_index, offset) pair representing a UBO or SSBO position. */
473   struct nir_ssa_def *block_index;
474   struct nir_ssa_def *offset;
475
476   /* Access qualifiers */
477   enum gl_access_qualifier access;
478};
479
480bool vtn_pointer_uses_ssa_offset(struct vtn_builder *b,
481                                 struct vtn_pointer *ptr);
482
483struct vtn_variable {
484   enum vtn_variable_mode mode;
485
486   struct vtn_type *type;
487
488   unsigned descriptor_set;
489   unsigned binding;
490   bool explicit_binding;
491   unsigned offset;
492   unsigned input_attachment_index;
493   bool patch;
494
495   nir_variable *var;
496
497   /* If the variable is a struct with a location set on it then this will be
498    * stored here. This will be used to calculate locations for members that
499    * don’t have their own explicit location.
500    */
501   int base_location;
502
503   int shared_location;
504
505   /**
506    * In some early released versions of GLSLang, it implemented all function
507    * calls by making copies of all parameters into temporary variables and
508    * passing those variables into the function.  It even did so for samplers
509    * and images which violates the SPIR-V spec.  Unfortunately, two games
510    * (Talos Principle and Doom) shipped with this old version of GLSLang and
511    * also happen to pass samplers into functions.  Talos Principle received
512    * an update fairly shortly after release with an updated GLSLang.  Doom,
513    * on the other hand, has never received an update so we need to work
514    * around this GLSLang issue in SPIR-V -> NIR.  Hopefully, we can drop this
515    * hack at some point in the future.
516    */
517   struct vtn_pointer *copy_prop_sampler;
518
519   /* Access qualifiers. */
520   enum gl_access_qualifier access;
521};
522
523struct vtn_image_pointer {
524   struct vtn_pointer *image;
525   nir_ssa_def *coord;
526   nir_ssa_def *sample;
527};
528
529struct vtn_sampled_image {
530   struct vtn_type *type;
531   struct vtn_pointer *image; /* Image or array of images */
532   struct vtn_pointer *sampler; /* Sampler */
533};
534
535struct vtn_value {
536   enum vtn_value_type value_type;
537   const char *name;
538   struct vtn_decoration *decoration;
539   struct vtn_type *type;
540   union {
541      void *ptr;
542      char *str;
543      nir_constant *constant;
544      struct vtn_pointer *pointer;
545      struct vtn_image_pointer *image;
546      struct vtn_sampled_image *sampled_image;
547      struct vtn_function *func;
548      struct vtn_block *block;
549      struct vtn_ssa_value *ssa;
550      vtn_instruction_handler ext_handler;
551   };
552};
553
554#define VTN_DEC_DECORATION -1
555#define VTN_DEC_EXECUTION_MODE -2
556#define VTN_DEC_STRUCT_MEMBER0 0
557
558struct vtn_decoration {
559   struct vtn_decoration *next;
560
561   /* Specifies how to apply this decoration.  Negative values represent a
562    * decoration or execution mode. (See the VTN_DEC_ #defines above.)
563    * Non-negative values specify that it applies to a structure member.
564    */
565   int scope;
566
567   const uint32_t *operands;
568   struct vtn_value *group;
569
570   union {
571      SpvDecoration decoration;
572      SpvExecutionMode exec_mode;
573   };
574};
575
576struct vtn_builder {
577   nir_builder nb;
578
579   /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
580   jmp_buf fail_jump;
581
582   const uint32_t *spirv;
583   size_t spirv_word_count;
584
585   nir_shader *shader;
586   struct spirv_to_nir_options *options;
587   struct vtn_block *block;
588
589   /* Current offset, file, line, and column.  Useful for debugging.  Set
590    * automatically by vtn_foreach_instruction.
591    */
592   size_t spirv_offset;
593   char *file;
594   int line, col;
595
596   /*
597    * In SPIR-V, constants are global, whereas in NIR, the load_const
598    * instruction we use is per-function. So while we parse each function, we
599    * keep a hash table of constants we've resolved to nir_ssa_value's so
600    * far, and we lazily resolve them when we see them used in a function.
601    */
602   struct hash_table *const_table;
603
604   /*
605    * Map from phi instructions (pointer to the start of the instruction)
606    * to the variable corresponding to it.
607    */
608   struct hash_table *phi_table;
609
610   unsigned num_specializations;
611   struct nir_spirv_specialization *specializations;
612
613   unsigned value_id_bound;
614   struct vtn_value *values;
615
616   /* True if we should watch out for GLSLang issue #179 */
617   bool wa_glslang_179;
618
619   gl_shader_stage entry_point_stage;
620   const char *entry_point_name;
621   struct vtn_value *entry_point;
622   struct vtn_value *workgroup_size_builtin;
623   bool variable_pointers;
624
625   struct vtn_function *func;
626   struct exec_list functions;
627
628   /* Current function parameter index */
629   unsigned func_param_idx;
630
631   bool has_loop_continue;
632
633   /* false by default, set to true by the ContractionOff execution mode */
634   bool exact;
635
636   /* when a physical memory model is choosen */
637   bool physical_ptrs;
638};
639
640nir_ssa_def *
641vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
642struct vtn_pointer *
643vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
644                     struct vtn_type *ptr_type);
645
646static inline struct vtn_value *
647vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
648{
649   vtn_fail_if(value_id >= b->value_id_bound,
650               "SPIR-V id %u is out-of-bounds", value_id);
651   return &b->values[value_id];
652}
653
654/* Consider not using this function directly and instead use
655 * vtn_push_ssa/vtn_push_value_pointer so that appropriate applying of
656 * decorations is handled by common code.
657 */
658static inline struct vtn_value *
659vtn_push_value(struct vtn_builder *b, uint32_t value_id,
660               enum vtn_value_type value_type)
661{
662   struct vtn_value *val = vtn_untyped_value(b, value_id);
663
664   vtn_fail_if(val->value_type != vtn_value_type_invalid,
665               "SPIR-V id %u has already been written by another instruction",
666               value_id);
667
668   val->value_type = value_type;
669
670   return &b->values[value_id];
671}
672
673static inline struct vtn_value *
674vtn_value(struct vtn_builder *b, uint32_t value_id,
675          enum vtn_value_type value_type)
676{
677   struct vtn_value *val = vtn_untyped_value(b, value_id);
678   vtn_fail_if(val->value_type != value_type,
679               "SPIR-V id %u is the wrong kind of value", value_id);
680   return val;
681}
682
683bool
684vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
685                                const uint32_t *w, unsigned count);
686
687static inline uint64_t
688vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
689{
690   struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
691
692   vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
693               !glsl_type_is_integer(val->type->type),
694               "Expected id %u to be an integer constant", value_id);
695
696   switch (glsl_get_bit_size(val->type->type)) {
697   case 8:  return val->constant->values[0][0].u8;
698   case 16: return val->constant->values[0][0].u16;
699   case 32: return val->constant->values[0][0].u32;
700   case 64: return val->constant->values[0][0].u64;
701   default: unreachable("Invalid bit size");
702   }
703}
704
705static inline enum gl_access_qualifier vtn_value_access(struct vtn_value *value)
706{
707   switch (value->value_type) {
708   case vtn_value_type_invalid:
709   case vtn_value_type_undef:
710   case vtn_value_type_string:
711   case vtn_value_type_decoration_group:
712   case vtn_value_type_constant:
713   case vtn_value_type_function:
714   case vtn_value_type_block:
715   case vtn_value_type_extension:
716      return 0;
717   case vtn_value_type_type:
718      return value->type->access;
719   case vtn_value_type_pointer:
720      return value->pointer->access;
721   case vtn_value_type_ssa:
722      return value->ssa->access;
723   case vtn_value_type_image_pointer:
724      return value->image->image->access;
725   case vtn_value_type_sampled_image:
726      return value->sampled_image->image->access |
727         value->sampled_image->sampler->access;
728   }
729
730   unreachable("invalid type");
731}
732
733struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
734
735struct vtn_value *vtn_push_value_pointer(struct vtn_builder *b,
736                                         uint32_t value_id,
737                                         struct vtn_pointer *ptr);
738
739struct vtn_value *vtn_push_ssa(struct vtn_builder *b, uint32_t value_id,
740                               struct vtn_type *type, struct vtn_ssa_value *ssa);
741
742struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
743                                           const struct glsl_type *type);
744
745struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
746                                        struct vtn_ssa_value *src);
747
748nir_ssa_def *vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src,
749                                unsigned index);
750nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
751                                        nir_ssa_def *index);
752nir_ssa_def *vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src,
753                               nir_ssa_def *insert, unsigned index);
754nir_ssa_def *vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
755                                       nir_ssa_def *insert, nir_ssa_def *index);
756
757nir_deref_instr *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
758
759struct vtn_pointer *vtn_pointer_for_variable(struct vtn_builder *b,
760                                             struct vtn_variable *var,
761                                             struct vtn_type *ptr_type);
762
763nir_deref_instr *vtn_pointer_to_deref(struct vtn_builder *b,
764                                      struct vtn_pointer *ptr);
765nir_ssa_def *
766vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
767                      nir_ssa_def **index_out);
768
769struct vtn_ssa_value *
770vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
771               enum gl_access_qualifier access);
772
773void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
774                     nir_deref_instr *dest,
775                     enum gl_access_qualifier access);
776
777struct vtn_ssa_value *
778vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src);
779
780void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
781                        struct vtn_pointer *dest);
782
783void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
784                          const uint32_t *w, unsigned count);
785
786
787typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
788                                          struct vtn_value *,
789                                          int member,
790                                          const struct vtn_decoration *,
791                                          void *);
792
793void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
794                            vtn_decoration_foreach_cb cb, void *data);
795
796typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
797                                              struct vtn_value *,
798                                              const struct vtn_decoration *,
799                                              void *);
800
801void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
802                                vtn_execution_mode_foreach_cb cb, void *data);
803
804nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
805                                       SpvOp opcode, bool *swap,
806                                       unsigned src_bit_size, unsigned dst_bit_size);
807
808void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
809                    const uint32_t *w, unsigned count);
810
811void vtn_handle_bitcast(struct vtn_builder *b, const uint32_t *w,
812                        unsigned count);
813
814void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
815                         const uint32_t *w, unsigned count);
816
817bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
818                                    const uint32_t *words, unsigned count);
819
820bool vtn_handle_opencl_instruction(struct vtn_builder *b, uint32_t ext_opcode,
821                                   const uint32_t *words, unsigned count);
822
823struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
824                                       gl_shader_stage stage, const char *entry_point_name,
825                                       const struct spirv_to_nir_options *options);
826
827void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
828                            unsigned count);
829
830void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
831                           const uint32_t *w, unsigned count);
832
833static inline uint32_t
834vtn_align_u32(uint32_t v, uint32_t a)
835{
836   assert(a != 0 && a == (a & -((int32_t) a)));
837   return (v + a - 1) & ~(a - 1);
838}
839
840static inline uint64_t
841vtn_u64_literal(const uint32_t *w)
842{
843   return (uint64_t)w[1] << 32 | w[0];
844}
845
846bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
847                                           const uint32_t *words, unsigned count);
848
849bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
850						      const uint32_t *words, unsigned count);
851#endif /* _VTN_PRIVATE_H_ */
852