• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #ifndef _VTN_PRIVATE_H_
25 #define _VTN_PRIVATE_H_
26 
27 #include <setjmp.h>
28 
29 #include "nir/nir.h"
30 #include "nir/nir_builder.h"
31 #include "util/u_dynarray.h"
32 #include "nir_spirv.h"
33 #include "spirv.h"
34 #include "vtn_generator_ids.h"
35 
36 extern uint32_t mesa_spirv_debug;
37 
38 #ifndef NDEBUG
39 #define MESA_SPIRV_DEBUG(flag) unlikely(mesa_spirv_debug & (MESA_SPIRV_DEBUG_ ## flag))
40 #else
41 #define MESA_SPIRV_DEBUG(flag) false
42 #endif
43 
44 #define MESA_SPIRV_DEBUG_STRUCTURED     (1u << 0)
45 
46 struct vtn_builder;
47 struct vtn_decoration;
48 
49 /* setjmp/longjmp is broken on MinGW: https://sourceforge.net/p/mingw-w64/bugs/406/ */
50 #if defined(__MINGW32__) && !defined(_UCRT)
51   #define vtn_setjmp __builtin_setjmp
52   #define vtn_longjmp __builtin_longjmp
53 #else
54   #define vtn_setjmp setjmp
55   #define vtn_longjmp longjmp
56 #endif
57 
58 void vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
59              size_t spirv_offset, const char *message);
60 
61 void vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
62               size_t spirv_offset, const char *fmt, ...) PRINTFLIKE(4, 5);
63 
64 #define vtn_info(...) vtn_logf(b, NIR_SPIRV_DEBUG_LEVEL_INFO, 0, __VA_ARGS__)
65 
66 void _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
67                const char *fmt, ...) PRINTFLIKE(4, 5);
68 #define vtn_warn(...) _vtn_warn(b, __FILE__, __LINE__, __VA_ARGS__)
69 
70 void _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
71                const char *fmt, ...) PRINTFLIKE(4, 5);
72 #define vtn_err(...) _vtn_err(b, __FILE__, __LINE__, __VA_ARGS__)
73 
74 /** Fail SPIR-V parsing
75  *
76  * This function logs an error and then bails out of the shader compile using
77  * longjmp.  This being safe relies on two things:
78  *
79  *  1) We must guarantee that setjmp is called after allocating the builder
80  *     and setting up b->debug (so that logging works) but before before any
81  *     errors have a chance to occur.
82  *
83  *  2) While doing the SPIR-V -> NIR conversion, we need to be careful to
84  *     ensure that all heap allocations happen through ralloc and are parented
85  *     to the builder.  This way they will get properly cleaned up on error.
86  *
87  *  3) We must ensure that _vtn_fail is never called while a mutex lock or a
88  *     reference to any other resource is held with the exception of ralloc
89  *     objects which are parented to the builder.
90  *
91  * So long as these two things continue to hold, we can easily longjmp back to
92  * spirv_to_nir(), clean up the builder, and return NULL.
93  */
94 NORETURN void
95 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
96              const char *fmt, ...) PRINTFLIKE(4, 5);
97 
98 #define vtn_fail(...) _vtn_fail(b, __FILE__, __LINE__, __VA_ARGS__)
99 
100 /** Fail if the given expression evaluates to true */
101 #define vtn_fail_if(expr, ...) \
102    do { \
103       if (unlikely(expr)) \
104          vtn_fail(__VA_ARGS__); \
105    } while (0)
106 
107 #define _vtn_fail_with(t, msg, v) \
108    vtn_fail("%s: %s (%u)\n", msg, spirv_ ## t ## _to_string(v), v)
109 
110 #define vtn_fail_with_decoration(msg, v) _vtn_fail_with(decoration, msg, v)
111 #define vtn_fail_with_opcode(msg, v)     _vtn_fail_with(op, msg, v)
112 
113 /** Assert that a condition is true and, if it isn't, vtn_fail
114  *
115  * This macro is transitional only and should not be used in new code.  Use
116  * vtn_fail_if and provide a real message instead.
117  */
118 #define vtn_assert(expr) \
119    do { \
120       if (!likely(expr)) \
121          vtn_fail("%s", #expr); \
122    } while (0)
123 
124 /* These are used to allocate data that can be dropped at the end of
125  * the parsing.  Any NIR data structure should keep using the ralloc,
126  * since they will outlive the parsing.
127  */
128 #define vtn_alloc(B, TYPE)               linear_alloc(B->lin_ctx, TYPE)
129 #define vtn_zalloc(B, TYPE)              linear_zalloc(B->lin_ctx, TYPE)
130 #define vtn_alloc_array(B, TYPE, ELEMS)  linear_alloc_array(B->lin_ctx, TYPE, ELEMS)
131 #define vtn_zalloc_array(B, TYPE, ELEMS) linear_zalloc_array(B->lin_ctx, TYPE, ELEMS)
132 #define vtn_alloc_size(B, SIZE)          linear_alloc_child(B->lin_ctx, SIZE)
133 #define vtn_zalloc_size(B, SIZE)         linear_zalloc_child(B->lin_ctx, SIZE)
134 
135 enum vtn_value_type {
136    vtn_value_type_invalid = 0,
137    vtn_value_type_undef,
138    vtn_value_type_string,
139    vtn_value_type_decoration_group,
140    vtn_value_type_type,
141    vtn_value_type_constant,
142    vtn_value_type_pointer,
143    vtn_value_type_function,
144    vtn_value_type_block,
145    vtn_value_type_ssa,
146    vtn_value_type_extension,
147    vtn_value_type_image_pointer,
148 };
149 
150 const char *vtn_value_type_to_string(enum vtn_value_type t);
151 
152 struct vtn_case {
153    struct list_head link;
154 
155    struct vtn_block *block;
156 
157    /* The uint32_t values that map to this case */
158    struct util_dynarray values;
159 
160    /* True if this is the default case */
161    bool is_default;
162 
163    /* Initialized to false; used when sorting the list of cases */
164    bool visited;
165 };
166 
167 struct vtn_block {
168    struct list_head link;
169 
170    /** A pointer to the label instruction */
171    const uint32_t *label;
172 
173    /** A pointer to the merge instruction (or NULL if non exists) */
174    const uint32_t *merge;
175 
176    /** A pointer to the branch instruction that ends this block */
177    const uint32_t *branch;
178 
179    /** Points to the switch case started by this block (if any) */
180    struct vtn_case *switch_case;
181 
182    /** Every block ends in a nop intrinsic so that we can find it again */
183    nir_intrinsic_instr *end_nop;
184 
185    /** attached nir_block */
186    struct nir_block *block;
187 
188    /* Inner-most construct that this block is part of. */
189    struct vtn_construct *parent;
190 
191    /* Blocks that succeed this block.  Used by structured control flow. */
192    struct vtn_successor *successors;
193    unsigned successors_count;
194 
195    /* Position of this block in the structured post-order traversal. */
196    unsigned pos;
197 
198    bool visited;
199 };
200 
201 struct vtn_function {
202    struct list_head link;
203 
204    struct vtn_type *type;
205 
206    bool referenced;
207    bool emitted;
208 
209    nir_function *nir_func;
210    struct vtn_block *start_block;
211 
212    struct list_head body;
213 
214    const uint32_t *end;
215 
216    SpvLinkageType linkage;
217    SpvFunctionControlMask control;
218 
219    unsigned block_count;
220 
221    /* Ordering of blocks to be processed by structured control flow.  See
222     * vtn_structured_cfg.c for details.
223     */
224    unsigned ordered_blocks_count;
225    struct vtn_block **ordered_blocks;
226 
227    /* Structured control flow constructs.  See struct vtn_construct. */
228    struct list_head constructs;
229 };
230 
231 #define vtn_foreach_function(func, func_list) \
232    list_for_each_entry(struct vtn_function, func, func_list, link)
233 
234 #define vtn_foreach_case(cse, case_list) \
235    list_for_each_entry(struct vtn_case, cse, case_list, link)
236 
237 #define vtn_foreach_case_safe(cse, case_list) \
238    list_for_each_entry_safe(struct vtn_case, cse, case_list, link)
239 
240 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
241                                         const uint32_t *, unsigned);
242 
243 void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
244                    const uint32_t *end);
245 void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
246                        vtn_instruction_handler instruction_handler);
247 void vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
248                               const uint32_t *w, unsigned count);
249 
250 bool vtn_cfg_handle_prepass_instruction(struct vtn_builder *b, SpvOp opcode,
251                                         const uint32_t *w, unsigned count);
252 void vtn_emit_cf_func_structured(struct vtn_builder *b, struct vtn_function *func,
253                                  vtn_instruction_handler handler);
254 bool vtn_handle_phis_first_pass(struct vtn_builder *b, SpvOp opcode,
255                                 const uint32_t *w, unsigned count);
256 void vtn_emit_ret_store(struct vtn_builder *b, const struct vtn_block *block);
257 void vtn_build_structured_cfg(struct vtn_builder *b, const uint32_t *words,
258                               const uint32_t *end);
259 
260 const uint32_t *
261 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
262                         const uint32_t *end, vtn_instruction_handler handler);
263 
264 struct vtn_ssa_value {
265    bool is_variable;
266 
267    union {
268       nir_def *def;
269       nir_variable *var;
270       struct vtn_ssa_value **elems;
271    };
272 
273    /* For matrices, if this is non-NULL, then this value is actually the
274     * transpose of some other value.  The value that `transposed` points to
275     * always dominates this value.
276     */
277    struct vtn_ssa_value *transposed;
278 
279    const struct glsl_type *type;
280 };
281 
282 enum vtn_base_type {
283    vtn_base_type_void,
284    vtn_base_type_scalar,
285    vtn_base_type_vector,
286    vtn_base_type_matrix,
287    vtn_base_type_array,
288    vtn_base_type_struct,
289    vtn_base_type_pointer,
290    vtn_base_type_image,
291    vtn_base_type_sampler,
292    vtn_base_type_sampled_image,
293    vtn_base_type_accel_struct,
294    vtn_base_type_ray_query,
295    vtn_base_type_function,
296    vtn_base_type_event,
297    vtn_base_type_cooperative_matrix,
298 };
299 
300 struct vtn_type {
301    enum vtn_base_type base_type;
302 
303    const struct glsl_type *type;
304 
305    /* The SPIR-V id of the given type. */
306    uint32_t id;
307 
308    /* Specifies the length of complex types.
309     *
310     * For Workgroup pointers, this is the size of the referenced type.
311     */
312    unsigned length;
313 
314    /* for arrays, matrices and pointers, the array stride */
315    unsigned stride;
316 
317    /* Access qualifiers */
318    enum gl_access_qualifier access;
319 
320    union {
321       /* Members for scalar, vector, and array-like types */
322       struct {
323          /* for arrays, the vtn_type for the elements of the array */
324          struct vtn_type *array_element;
325 
326          /* for matrices, whether the matrix is stored row-major */
327          bool row_major:1;
328 
329          /* Whether this type, or a parent type, has been decorated as a
330           * builtin
331           */
332          bool is_builtin:1;
333 
334          /* Which built-in to use */
335          SpvBuiltIn builtin;
336       };
337 
338       /* Members for struct types */
339       struct {
340          /* for structures, the vtn_type for each member */
341          struct vtn_type **members;
342 
343          /* for structs, the offset of each member */
344          unsigned *offsets;
345 
346          /* for structs, whether it was decorated as a "non-SSBO-like" block */
347          bool block:1;
348 
349          /* for structs, whether it was decorated as an "SSBO-like" block */
350          bool buffer_block:1;
351 
352          /* for structs with block == true, whether this is a builtin block
353           * (i.e. a block that contains only builtins).
354           */
355          bool builtin_block:1;
356 
357          /* for structs and unions it specifies the minimum alignment of the
358           * members. 0 means packed.
359           *
360           * Set by CPacked and Alignment Decorations in kernels.
361           */
362          bool packed:1;
363       };
364 
365       /* Members for pointer types */
366       struct {
367          /* For pointers, the vtn_type for dereferenced type */
368          struct vtn_type *deref;
369 
370          /* Storage class for pointers */
371          SpvStorageClass storage_class;
372 
373          /* Required alignment for pointers */
374          uint32_t align;
375       };
376 
377       /* Members for image types */
378       struct {
379          /* GLSL image type for this type.  This is not to be confused with
380           * vtn_type::type which is actually going to be the GLSL type for a
381           * pointer to an image, likely a uint32_t.
382           */
383          const struct glsl_type *glsl_image;
384 
385          /* Image format for image_load_store type images */
386          unsigned image_format;
387 
388          /* Access qualifier for storage images */
389          SpvAccessQualifier access_qualifier;
390       };
391 
392       /* Members for sampled image types */
393       struct {
394          /* For sampled images, the image type */
395          struct vtn_type *image;
396       };
397 
398       /* Members for function types */
399       struct {
400          /* For functions, the vtn_type for each parameter */
401          struct vtn_type **params;
402 
403          /* Return type for functions */
404          struct vtn_type *return_type;
405       };
406 
407       /* Members for cooperative matrix types. */
408       struct {
409          struct glsl_cmat_description desc;
410          struct vtn_type *component_type;
411       };
412    };
413 };
414 
415 bool vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type);
416 
417 bool vtn_types_compatible(struct vtn_builder *b,
418                           struct vtn_type *t1, struct vtn_type *t2);
419 
420 struct vtn_type *vtn_type_without_array(struct vtn_type *type);
421 
422 struct vtn_variable;
423 
424 enum vtn_access_mode {
425    vtn_access_mode_id,
426    vtn_access_mode_literal,
427 };
428 
429 struct vtn_access_link {
430    enum vtn_access_mode mode;
431    int64_t id;
432 };
433 
434 struct vtn_access_chain {
435    uint32_t length;
436 
437    /** Whether or not to treat the base pointer as an array.  This is only
438     * true if this access chain came from an OpPtrAccessChain.
439     */
440    bool ptr_as_array;
441 
442    /* Access qualifiers */
443    enum gl_access_qualifier access;
444 
445    bool in_bounds;
446 
447    /** Struct elements and array offsets.
448     *
449     * This is an array of 1 so that it can conveniently be created on the
450     * stack but the real length is given by the length field.
451     */
452    struct vtn_access_link link[1];
453 };
454 
455 enum vtn_variable_mode {
456    vtn_variable_mode_function,
457    vtn_variable_mode_private,
458    vtn_variable_mode_uniform,
459    vtn_variable_mode_atomic_counter,
460    vtn_variable_mode_ubo,
461    vtn_variable_mode_ssbo,
462    vtn_variable_mode_phys_ssbo,
463    vtn_variable_mode_push_constant,
464    vtn_variable_mode_workgroup,
465    vtn_variable_mode_cross_workgroup,
466    vtn_variable_mode_task_payload,
467    vtn_variable_mode_generic,
468    vtn_variable_mode_constant,
469    vtn_variable_mode_input,
470    vtn_variable_mode_output,
471    vtn_variable_mode_image,
472    vtn_variable_mode_accel_struct,
473    vtn_variable_mode_call_data,
474    vtn_variable_mode_call_data_in,
475    vtn_variable_mode_ray_payload,
476    vtn_variable_mode_ray_payload_in,
477    vtn_variable_mode_hit_attrib,
478    vtn_variable_mode_shader_record,
479    vtn_variable_mode_node_payload,
480 };
481 
482 struct vtn_pointer {
483    /** The variable mode for the referenced data */
484    enum vtn_variable_mode mode;
485 
486    /** The dereferenced type of this pointer */
487    struct vtn_type *type;
488 
489    /** The pointer type of this pointer
490     *
491     * This may be NULL for some temporary pointers constructed as part of a
492     * large load, store, or copy.  It MUST be valid for all pointers which are
493     * stored as SPIR-V SSA values.
494     */
495    struct vtn_type *ptr_type;
496 
497    /** The referenced variable, if known
498     *
499     * This field may be NULL if the pointer uses a (block_index, offset) pair
500     * instead of an access chain or if the access chain starts at a deref.
501     */
502    struct vtn_variable *var;
503 
504    /** The NIR deref corresponding to this pointer */
505    nir_deref_instr *deref;
506 
507    /** A (block_index, offset) pair representing a UBO or SSBO position. */
508    struct nir_def *block_index;
509    struct nir_def *offset;
510 
511    /* Access qualifiers */
512    enum gl_access_qualifier access;
513 };
514 
515 struct vtn_variable {
516    enum vtn_variable_mode mode;
517 
518    struct vtn_type *type;
519 
520    unsigned descriptor_set;
521    unsigned binding;
522    bool explicit_binding;
523    unsigned offset;
524    unsigned input_attachment_index;
525 
526    nir_variable *var;
527 
528    /* If the variable is a struct with a location set on it then this will be
529     * stored here. This will be used to calculate locations for members that
530     * don’t have their own explicit location.
531     */
532    int base_location;
533 
534    /**
535     * In some early released versions of GLSLang, it implemented all function
536     * calls by making copies of all parameters into temporary variables and
537     * passing those variables into the function.  It even did so for samplers
538     * and images which violates the SPIR-V spec.  Unfortunately, two games
539     * (Talos Principle and Doom) shipped with this old version of GLSLang and
540     * also happen to pass samplers into functions.  Talos Principle received
541     * an update fairly shortly after release with an updated GLSLang.  Doom,
542     * on the other hand, has never received an update so we need to work
543     * around this GLSLang issue in SPIR-V -> NIR.  Hopefully, we can drop this
544     * hack at some point in the future.
545     */
546    struct vtn_pointer *copy_prop_sampler;
547 
548    /* Access qualifiers. */
549    enum gl_access_qualifier access;
550 };
551 
552 const struct glsl_type *
553 vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type,
554                       enum vtn_variable_mode mode);
555 
556 mesa_scope
557 vtn_translate_scope(struct vtn_builder *b, SpvScope scope);
558 
559 struct vtn_image_pointer {
560    nir_deref_instr *image;
561    nir_def *coord;
562    nir_def *sample;
563    nir_def *lod;
564 };
565 
566 struct vtn_value {
567    enum vtn_value_type value_type;
568 
569    /* Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/3406
570     * Only set for OpImage / OpSampledImage. Note that this is in addition
571     * the existence of a NonUniform decoration on this value.*/
572    uint32_t propagated_non_uniform : 1;
573 
574    /* Valid for vtn_value_type_constant to indicate the value is OpConstantNull. */
575    bool is_null_constant:1;
576 
577    /* Valid when all the members of the value are undef. */
578    bool is_undef_constant:1;
579 
580    const char *name;
581    struct vtn_decoration *decoration;
582    struct vtn_type *type;
583    union {
584       const char *str;
585       nir_constant *constant;
586       struct vtn_pointer *pointer;
587       struct vtn_image_pointer *image;
588       struct vtn_function *func;
589       struct vtn_block *block;
590       struct vtn_ssa_value *ssa;
591       vtn_instruction_handler ext_handler;
592    };
593 };
594 
595 #define VTN_DEC_DECORATION -1
596 #define VTN_DEC_EXECUTION_MODE -2
597 #define VTN_DEC_STRUCT_MEMBER_NAME0 -3
598 #define VTN_DEC_STRUCT_MEMBER0 0
599 
600 struct vtn_decoration {
601    struct vtn_decoration *next;
602 
603    /* Different kinds of decorations are stored in a value,
604       the scope defines what decoration it refers to:
605 
606       - VTN_DEC_DECORATION:
607             decoration associated with the value
608       - VTN_DEC_EXECUTION_MODE:
609             an execution mode associated with an entrypoint value
610       - VTN_DEC_STRUCT_MEMBER0 + m:
611             decoration associated with member m of a struct value
612       - VTN_DEC_STRUCT_MEMBER_NAME0 - m:
613             name of m'th member of a struct value
614     */
615    int scope;
616 
617    uint32_t num_operands;
618    const uint32_t *operands;
619    struct vtn_value *group;
620 
621    union {
622       SpvDecoration decoration;
623       SpvExecutionMode exec_mode;
624       const char *member_name;
625    };
626 };
627 
628 struct vtn_builder {
629    nir_builder nb;
630 
631    linear_ctx *lin_ctx;
632 
633    /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
634    jmp_buf fail_jump;
635 
636    const uint32_t *spirv;
637    size_t spirv_word_count;
638    uint32_t version;
639 
640    nir_shader *shader;
641    struct spirv_to_nir_options *options;
642    struct vtn_block *block;
643 
644    /* Current offset, file, line, and column.  Useful for debugging.  Set
645     * automatically by vtn_foreach_instruction.
646     */
647    size_t spirv_offset;
648    const char *file;
649    int line, col;
650 
651    /*
652     * Map from phi instructions (pointer to the start of the instruction)
653     * to the variable corresponding to it.
654     */
655    struct hash_table *phi_table;
656 
657    /* In Vulkan, when lowering some modes variable access, the derefs of the
658     * variables are replaced with a resource index intrinsics, leaving the
659     * variable hanging.  This set keeps track of them so they can be filtered
660     * (and not removed) in nir_remove_dead_variables.
661     */
662    struct set *vars_used_indirectly;
663 
664    unsigned num_specializations;
665    struct nir_spirv_specialization *specializations;
666 
667    unsigned value_id_bound;
668    struct vtn_value *values;
669 
670    /* Information on the origin of the SPIR-V */
671    enum vtn_generator generator_id;
672    SpvSourceLanguage source_lang;
673 
674    /* True if we need to fix up CS OpControlBarrier */
675    bool wa_glslang_cs_barrier;
676 
677    /* True if we need to ignore undef initializers */
678    bool wa_llvm_spirv_ignore_workgroup_initializer;
679 
680    /* True if we need to ignore OpReturn after OpEmitMeshTasksEXT. */
681    bool wa_ignore_return_after_emit_mesh_tasks;
682 
683    /* Workaround discard bugs in HLSL -> SPIR-V compilers */
684    bool uses_demote_to_helper_invocation;
685    bool convert_discard_to_demote;
686 
687    gl_shader_stage entry_point_stage;
688    const char *entry_point_name;
689    struct vtn_value *entry_point;
690    struct vtn_value *workgroup_size_builtin;
691    bool variable_pointers;
692    bool image_gather_bias_lod;
693 
694    uint32_t *interface_ids;
695    size_t interface_ids_count;
696 
697    struct vtn_function *func;
698    struct list_head functions;
699 
700    /* Current function parameter index */
701    unsigned func_param_idx;
702 
703    /* false by default, set to true by the ContractionOff execution mode */
704    bool exact;
705 
706    /* when a physical memory model is choosen */
707    bool physical_ptrs;
708 
709    /* memory model specified by OpMemoryModel */
710    unsigned mem_model;
711 };
712 
713 const char *
714 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
715                    unsigned word_count, unsigned *words_used);
716 
717 nir_def *
718 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
719 struct vtn_pointer *
720 vtn_pointer_from_ssa(struct vtn_builder *b, nir_def *ssa,
721                      struct vtn_type *ptr_type);
722 
723 struct vtn_ssa_value *
724 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
725                     const struct glsl_type *type);
726 
727 static inline struct vtn_value *
vtn_untyped_value(struct vtn_builder * b,uint32_t value_id)728 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
729 {
730    vtn_fail_if(value_id >= b->value_id_bound,
731                "SPIR-V id %u is out-of-bounds", value_id);
732    return &b->values[value_id];
733 }
734 
735 static inline uint32_t
vtn_id_for_value(struct vtn_builder * b,struct vtn_value * value)736 vtn_id_for_value(struct vtn_builder *b, struct vtn_value *value)
737 {
738    vtn_fail_if(value <= b->values, "vtn_value pointer outside the range of valid values");
739    uint32_t value_id = value - b->values;
740    vtn_fail_if(value_id >= b->value_id_bound, "vtn_value pointer outside the range of valid values");
741    return value_id;
742 }
743 
744 /* Consider not using this function directly and instead use
745  * vtn_push_ssa/vtn_push_pointer so that appropriate applying of
746  * decorations is handled by common code.
747  */
748 static inline struct vtn_value *
vtn_push_value(struct vtn_builder * b,uint32_t value_id,enum vtn_value_type value_type)749 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
750                enum vtn_value_type value_type)
751 {
752    struct vtn_value *val = vtn_untyped_value(b, value_id);
753 
754    vtn_fail_if(value_type == vtn_value_type_ssa,
755                "Do not call vtn_push_value for value_type_ssa.  Use "
756                "vtn_push_ssa_value instead.");
757 
758    vtn_fail_if(val->value_type != vtn_value_type_invalid,
759                "SPIR-V id %u has already been written by another instruction",
760                value_id);
761 
762    val->value_type = value_type;
763 
764    return &b->values[value_id];
765 }
766 
767 /* These separated fail functions exist so the helpers like vtn_value()
768  * can be inlined with minimal code size impact.  This allows the failure
769  * handling to have more detailed output without harming callers.
770  */
771 
772 void _vtn_fail_value_type_mismatch(struct vtn_builder *b, uint32_t value_id,
773                                    enum vtn_value_type value_type);
774 void _vtn_fail_value_not_pointer(struct vtn_builder *b, uint32_t value_id);
775 
776 static inline struct vtn_value *
vtn_value(struct vtn_builder * b,uint32_t value_id,enum vtn_value_type value_type)777 vtn_value(struct vtn_builder *b, uint32_t value_id,
778           enum vtn_value_type value_type)
779 {
780    struct vtn_value *val = vtn_untyped_value(b, value_id);
781    if (unlikely(val->value_type != value_type))
782       _vtn_fail_value_type_mismatch(b, value_id, value_type);
783    return val;
784 }
785 
786 static inline struct vtn_value *
vtn_pointer_value(struct vtn_builder * b,uint32_t value_id)787 vtn_pointer_value(struct vtn_builder *b, uint32_t value_id)
788 {
789    struct vtn_value *val = vtn_untyped_value(b, value_id);
790    if (unlikely(val->value_type != vtn_value_type_pointer &&
791                 !val->is_null_constant))
792       _vtn_fail_value_not_pointer(b, value_id);
793    return val;
794 }
795 
796 static inline struct vtn_pointer *
vtn_value_to_pointer(struct vtn_builder * b,struct vtn_value * value)797 vtn_value_to_pointer(struct vtn_builder *b, struct vtn_value *value)
798 {
799    if (value->is_null_constant) {
800       vtn_assert(glsl_type_is_vector_or_scalar(value->type->type));
801       nir_def *const_ssa =
802          vtn_const_ssa_value(b, value->constant, value->type->type)->def;
803       return vtn_pointer_from_ssa(b, const_ssa, value->type);
804    }
805    vtn_assert(value->value_type == vtn_value_type_pointer);
806    return value->pointer;
807 }
808 
809 static inline struct vtn_pointer *
vtn_pointer(struct vtn_builder * b,uint32_t value_id)810 vtn_pointer(struct vtn_builder *b, uint32_t value_id)
811 {
812    return vtn_value_to_pointer(b, vtn_pointer_value(b, value_id));
813 }
814 
815 bool
816 vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
817                                 const uint32_t *w, unsigned count);
818 
819 static inline uint64_t
vtn_constant_uint(struct vtn_builder * b,uint32_t value_id)820 vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
821 {
822    struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
823 
824    vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
825                !glsl_type_is_integer(val->type->type),
826                "Expected id %u to be an integer constant", value_id);
827 
828    switch (glsl_get_bit_size(val->type->type)) {
829    case 8:  return val->constant->values[0].u8;
830    case 16: return val->constant->values[0].u16;
831    case 32: return val->constant->values[0].u32;
832    case 64: return val->constant->values[0].u64;
833    default: unreachable("Invalid bit size");
834    }
835 }
836 
837 static inline int64_t
vtn_constant_int(struct vtn_builder * b,uint32_t value_id)838 vtn_constant_int(struct vtn_builder *b, uint32_t value_id)
839 {
840    struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
841 
842    vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
843                !glsl_type_is_integer(val->type->type),
844                "Expected id %u to be an integer constant", value_id);
845 
846    switch (glsl_get_bit_size(val->type->type)) {
847    case 8:  return val->constant->values[0].i8;
848    case 16: return val->constant->values[0].i16;
849    case 32: return val->constant->values[0].i32;
850    case 64: return val->constant->values[0].i64;
851    default: unreachable("Invalid bit size");
852    }
853 }
854 
855 static inline struct vtn_type *
vtn_get_value_type(struct vtn_builder * b,uint32_t value_id)856 vtn_get_value_type(struct vtn_builder *b, uint32_t value_id)
857 {
858    struct vtn_value *val = vtn_untyped_value(b, value_id);
859    vtn_fail_if(val->type == NULL, "Value %u does not have a type", value_id);
860    return val->type;
861 }
862 
863 static inline struct vtn_type *
vtn_get_type(struct vtn_builder * b,uint32_t value_id)864 vtn_get_type(struct vtn_builder *b, uint32_t value_id)
865 {
866    return vtn_value(b, value_id, vtn_value_type_type)->type;
867 }
868 
869 static inline struct vtn_block *
vtn_block(struct vtn_builder * b,uint32_t value_id)870 vtn_block(struct vtn_builder *b, uint32_t value_id)
871 {
872    return vtn_value(b, value_id, vtn_value_type_block)->block;
873 }
874 
875 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
876 struct vtn_value *vtn_push_ssa_value(struct vtn_builder *b, uint32_t value_id,
877                                      struct vtn_ssa_value *ssa);
878 
879 nir_def *vtn_get_nir_ssa(struct vtn_builder *b, uint32_t value_id);
880 struct vtn_value *vtn_push_nir_ssa(struct vtn_builder *b, uint32_t value_id,
881                                    nir_def *def);
882 nir_deref_instr *vtn_get_deref_for_id(struct vtn_builder *b, uint32_t value_id);
883 nir_deref_instr *vtn_get_deref_for_ssa_value(struct vtn_builder *b, struct vtn_ssa_value *ssa);
884 struct vtn_value *vtn_push_var_ssa(struct vtn_builder *b, uint32_t value_id,
885                                    nir_variable *var);
886 
887 struct vtn_value *vtn_push_pointer(struct vtn_builder *b,
888                                    uint32_t value_id,
889                                    struct vtn_pointer *ptr);
890 
891 struct vtn_sampled_image {
892    nir_deref_instr *image;
893    nir_deref_instr *sampler;
894 };
895 
896 nir_def *vtn_sampled_image_to_nir_ssa(struct vtn_builder *b,
897                                           struct vtn_sampled_image si);
898 
899 void
900 vtn_copy_value(struct vtn_builder *b, uint32_t src_value_id,
901                uint32_t dst_value_id);
902 
903 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
904                                            const struct glsl_type *type);
905 void vtn_set_ssa_value_var(struct vtn_builder *b, struct vtn_ssa_value *ssa, nir_variable *var);
906 
907 struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
908                                         struct vtn_ssa_value *src);
909 
910 nir_deref_instr *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
911 
912 nir_deref_instr *vtn_pointer_to_deref(struct vtn_builder *b,
913                                       struct vtn_pointer *ptr);
914 nir_def *
915 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
916                       nir_def **index_out);
917 
918 nir_deref_instr *
919 vtn_get_call_payload_for_location(struct vtn_builder *b, uint32_t location_id);
920 
921 struct vtn_ssa_value *
922 vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
923                enum gl_access_qualifier access);
924 
925 void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
926                      nir_deref_instr *dest,
927                      enum gl_access_qualifier access);
928 
929 struct vtn_ssa_value *
930 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src,
931                   enum gl_access_qualifier access);
932 
933 void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
934                         struct vtn_pointer *dest, enum gl_access_qualifier access);
935 
936 void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
937                           const uint32_t *w, unsigned count);
938 
939 
940 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
941                                           struct vtn_value *,
942                                           int member,
943                                           const struct vtn_decoration *,
944                                           void *);
945 
946 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
947                             vtn_decoration_foreach_cb cb, void *data);
948 
949 typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
950                                               struct vtn_value *,
951                                               const struct vtn_decoration *,
952                                               void *);
953 
954 void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
955                                 vtn_execution_mode_foreach_cb cb, void *data);
956 
957 nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
958                                        SpvOp opcode, bool *swap, bool *exact,
959                                        unsigned src_bit_size, unsigned dst_bit_size);
960 
961 void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
962                     const uint32_t *w, unsigned count);
963 
964 void vtn_handle_integer_dot(struct vtn_builder *b, SpvOp opcode,
965                             const uint32_t *w, unsigned count);
966 
967 void vtn_handle_bitcast(struct vtn_builder *b, const uint32_t *w,
968                         unsigned count);
969 
970 void vtn_handle_no_contraction(struct vtn_builder *b, struct vtn_value *val);
971 
972 void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
973                          const uint32_t *w, unsigned count);
974 
975 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
976                                     const uint32_t *words, unsigned count);
977 
978 bool vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
979                                    const uint32_t *words, unsigned count);
980 bool vtn_handle_opencl_core_instruction(struct vtn_builder *b, SpvOp opcode,
981                                         const uint32_t *w, unsigned count);
982 
983 struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
984                                        gl_shader_stage stage, const char *entry_point_name,
985                                        const struct spirv_to_nir_options *options);
986 
987 void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
988                             unsigned count);
989 
990 void vtn_handle_debug_text(struct vtn_builder *b, SpvOp opcode,
991                            const uint32_t *w, unsigned count);
992 
993 void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
994                            const uint32_t *w, unsigned count);
995 
996 enum vtn_variable_mode vtn_storage_class_to_mode(struct vtn_builder *b,
997                                                  SpvStorageClass class,
998                                                  struct vtn_type *interface_type,
999                                                  nir_variable_mode *nir_mode_out);
1000 
1001 nir_address_format vtn_mode_to_address_format(struct vtn_builder *b,
1002                                               enum vtn_variable_mode);
1003 
1004 nir_rounding_mode vtn_rounding_mode_to_nir(struct vtn_builder *b,
1005                                            SpvFPRoundingMode mode);
1006 
1007 static inline uint32_t
vtn_align_u32(uint32_t v,uint32_t a)1008 vtn_align_u32(uint32_t v, uint32_t a)
1009 {
1010    assert(a != 0 && a == (a & -((int32_t) a)));
1011    return (v + a - 1) & ~(a - 1);
1012 }
1013 
1014 static inline uint64_t
vtn_u64_literal(const uint32_t * w)1015 vtn_u64_literal(const uint32_t *w)
1016 {
1017    return (uint64_t)w[1] << 32 | w[0];
1018 }
1019 
1020 bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1021                                            const uint32_t *words, unsigned count);
1022 
1023 bool vtn_handle_amd_shader_ballot_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1024                                               const uint32_t *w, unsigned count);
1025 
1026 bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1027 						      const uint32_t *words, unsigned count);
1028 
1029 bool vtn_handle_amd_shader_explicit_vertex_parameter_instruction(struct vtn_builder *b,
1030                                                                  SpvOp ext_opcode,
1031                                                                  const uint32_t *words,
1032                                                                  unsigned count);
1033 
1034 SpvMemorySemanticsMask vtn_mode_to_memory_semantics(enum vtn_variable_mode mode);
1035 
1036 void vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
1037                              SpvMemorySemanticsMask semantics);
1038 
1039 bool vtn_value_is_relaxed_precision(struct vtn_builder *b, struct vtn_value *val);
1040 nir_def *
1041 vtn_mediump_downconvert(struct vtn_builder *b, enum glsl_base_type base_type, nir_def *def);
1042 struct vtn_ssa_value *
1043 vtn_mediump_downconvert_value(struct vtn_builder *b, struct vtn_ssa_value *src);
1044 void vtn_mediump_upconvert_value(struct vtn_builder *b, struct vtn_ssa_value *value);
1045 
1046 static inline int
cmp_uint32_t(const void * pa,const void * pb)1047 cmp_uint32_t(const void *pa, const void *pb)
1048 {
1049    uint32_t a = *((const uint32_t *)pa);
1050    uint32_t b = *((const uint32_t *)pb);
1051    if (a < b)
1052       return -1;
1053    if (a > b)
1054       return 1;
1055    return 0;
1056 }
1057 
1058 void
1059 vtn_parse_switch(struct vtn_builder *b,
1060                  const uint32_t *branch,
1061                  struct list_head *case_list);
1062 
1063 bool vtn_get_mem_operands(struct vtn_builder *b, const uint32_t *w, unsigned count,
1064                           unsigned *idx, SpvMemoryAccessMask *access, unsigned *alignment,
1065                           SpvScope *dest_scope, SpvScope *src_scope);
1066 void vtn_emit_make_visible_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
1067                                    SpvScope scope, enum vtn_variable_mode mode);
1068 void vtn_emit_make_available_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
1069                                      SpvScope scope, enum vtn_variable_mode mode);
1070 
1071 
1072 void vtn_handle_cooperative_type(struct vtn_builder *b, struct vtn_value *val,
1073                                  SpvOp opcode, const uint32_t *w, unsigned count);
1074 void vtn_handle_cooperative_instruction(struct vtn_builder *b, SpvOp opcode,
1075                                         const uint32_t *w, unsigned count);
1076 void vtn_handle_cooperative_alu(struct vtn_builder *b, struct vtn_value *dest_val,
1077                                 const struct glsl_type *dest_type, SpvOp opcode,
1078                                 const uint32_t *w, unsigned count);
1079 struct vtn_ssa_value *vtn_cooperative_matrix_extract(struct vtn_builder *b, struct vtn_ssa_value *mat,
1080                                                      const uint32_t *indices, unsigned num_indices);
1081 struct vtn_ssa_value *vtn_cooperative_matrix_insert(struct vtn_builder *b, struct vtn_ssa_value *mat,
1082                                                     struct vtn_ssa_value *insert,
1083                                                     const uint32_t *indices, unsigned num_indices);
1084 nir_deref_instr *vtn_create_cmat_temporary(struct vtn_builder *b,
1085                                            const struct glsl_type *t, const char *name);
1086 
1087 gl_shader_stage vtn_stage_for_execution_model(SpvExecutionModel model);
1088 
1089 #endif /* _VTN_PRIVATE_H_ */
1090