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 "spirv_info.h"
35 #include "vtn_generator_ids.h"
36
37 extern uint32_t mesa_spirv_debug;
38
39 #define MESA_SPIRV_DEBUG(flag) unlikely(mesa_spirv_debug & (MESA_SPIRV_DEBUG_ ## flag))
40
41 #define MESA_SPIRV_DEBUG_STRUCTURED (1u << 0)
42 #define MESA_SPIRV_DEBUG_VALUES (1u << 1)
43 #define MESA_SPIRV_DEBUG_ASM (1u << 2)
44 #define MESA_SPIRV_DEBUG_COLOR (1u << 3)
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 of the object pointed to. */
368 struct vtn_type *pointed;
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 pointer type of this pointer */
487 struct vtn_type *type;
488
489 /** The referenced variable, if known
490 *
491 * This field may be NULL if the pointer uses a (block_index, offset) pair
492 * instead of an access chain or if the access chain starts at a deref.
493 */
494 struct vtn_variable *var;
495
496 /** The NIR deref corresponding to this pointer */
497 nir_deref_instr *deref;
498
499 /** A (block_index, offset) pair representing a UBO or SSBO position. */
500 struct nir_def *block_index;
501 struct nir_def *offset;
502
503 /* Access qualifiers */
504 enum gl_access_qualifier access;
505 };
506
507 struct vtn_variable {
508 enum vtn_variable_mode mode;
509
510 struct vtn_type *type;
511
512 unsigned descriptor_set;
513 unsigned binding;
514 bool explicit_binding;
515 unsigned offset;
516 unsigned input_attachment_index;
517
518 nir_variable *var;
519
520 /* If the variable is a struct with a location set on it then this will be
521 * stored here. This will be used to calculate locations for members that
522 * don’t have their own explicit location.
523 */
524 int base_location;
525
526 /**
527 * In some early released versions of GLSLang, it implemented all function
528 * calls by making copies of all parameters into temporary variables and
529 * passing those variables into the function. It even did so for samplers
530 * and images which violates the SPIR-V spec. Unfortunately, two games
531 * (Talos Principle and Doom) shipped with this old version of GLSLang and
532 * also happen to pass samplers into functions. Talos Principle received
533 * an update fairly shortly after release with an updated GLSLang. Doom,
534 * on the other hand, has never received an update so we need to work
535 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
536 * hack at some point in the future.
537 */
538 struct vtn_pointer *copy_prop_sampler;
539
540 /* Access qualifiers. */
541 enum gl_access_qualifier access;
542 };
543
544 const struct glsl_type *
545 vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type,
546 enum vtn_variable_mode mode);
547
548 mesa_scope
549 vtn_translate_scope(struct vtn_builder *b, SpvScope scope);
550
551 struct vtn_image_pointer {
552 nir_deref_instr *image;
553 nir_def *coord;
554 nir_def *sample;
555 nir_def *lod;
556 };
557
558 struct vtn_value {
559 enum vtn_value_type value_type;
560
561 /* Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/3406
562 * Only set for OpImage / OpSampledImage. Note that this is in addition
563 * the existence of a NonUniform decoration on this value.*/
564 uint32_t propagated_non_uniform : 1;
565
566 /* Valid for vtn_value_type_constant to indicate the value is OpConstantNull. */
567 bool is_null_constant:1;
568
569 /* Valid when all the members of the value are undef. */
570 bool is_undef_constant:1;
571
572 /* Marked as OpEntryPoint */
573 bool is_entrypoint:1;
574
575 const char *name;
576 struct vtn_decoration *decoration;
577 struct vtn_type *type;
578 union {
579 const char *str;
580 nir_constant *constant;
581 struct vtn_pointer *pointer;
582 struct vtn_image_pointer *image;
583 struct vtn_function *func;
584 struct vtn_block *block;
585 struct vtn_ssa_value *ssa;
586 vtn_instruction_handler ext_handler;
587 };
588 };
589
590 #define VTN_DEC_DECORATION -1
591 #define VTN_DEC_EXECUTION_MODE -2
592 #define VTN_DEC_STRUCT_MEMBER_NAME0 -3
593 #define VTN_DEC_STRUCT_MEMBER0 0
594
595 struct vtn_decoration {
596 struct vtn_decoration *next;
597
598 /* Different kinds of decorations are stored in a value,
599 the scope defines what decoration it refers to:
600
601 - VTN_DEC_DECORATION:
602 decoration associated with the value
603 - VTN_DEC_EXECUTION_MODE:
604 an execution mode associated with an entrypoint value
605 - VTN_DEC_STRUCT_MEMBER0 + m:
606 decoration associated with member m of a struct value
607 - VTN_DEC_STRUCT_MEMBER_NAME0 - m:
608 name of m'th member of a struct value
609 */
610 int scope;
611
612 uint32_t num_operands;
613 const uint32_t *operands;
614 struct vtn_value *group;
615
616 union {
617 SpvDecoration decoration;
618 SpvExecutionMode exec_mode;
619 const char *member_name;
620 };
621 };
622
623 struct vtn_builder {
624 nir_builder nb;
625
626 linear_ctx *lin_ctx;
627
628 /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
629 jmp_buf fail_jump;
630
631 const uint32_t *spirv;
632 size_t spirv_word_count;
633 uint32_t version;
634
635 nir_shader *shader;
636 struct spirv_to_nir_options *options;
637 struct vtn_block *block;
638
639 /* Current offset, file, line, and column. Useful for debugging. Set
640 * automatically by vtn_foreach_instruction.
641 */
642 size_t spirv_offset;
643 const char *file;
644 int line, col;
645
646 /*
647 * Map from phi instructions (pointer to the start of the instruction)
648 * to the variable corresponding to it.
649 */
650 struct hash_table *phi_table;
651
652 /* In Vulkan, when lowering some modes variable access, the derefs of the
653 * variables are replaced with a resource index intrinsics, leaving the
654 * variable hanging. This set keeps track of them so they can be filtered
655 * (and not removed) in nir_remove_dead_variables.
656 */
657 struct set *vars_used_indirectly;
658
659 unsigned num_specializations;
660 struct nir_spirv_specialization *specializations;
661
662 unsigned value_id_bound;
663 struct vtn_value *values;
664
665 /* Information on the origin of the SPIR-V */
666 enum vtn_generator generator_id;
667 SpvSourceLanguage source_lang;
668
669 struct spirv_capabilities supported_capabilities;
670 struct spirv_capabilities enabled_capabilities;
671
672 /* True if we need to fix up CS OpControlBarrier */
673 bool wa_glslang_cs_barrier;
674
675 /* True if we need to ignore undef initializers */
676 bool wa_llvm_spirv_ignore_workgroup_initializer;
677
678 /* True if we need to ignore OpReturn after OpEmitMeshTasksEXT. */
679 bool wa_ignore_return_after_emit_mesh_tasks;
680
681 /* Workaround discard bugs in HLSL -> SPIR-V compilers */
682 bool convert_discard_to_demote;
683
684 gl_shader_stage entry_point_stage;
685 const char *entry_point_name;
686 struct vtn_value *entry_point;
687 struct vtn_value *workgroup_size_builtin;
688
689 uint32_t *interface_ids;
690 size_t interface_ids_count;
691
692 struct vtn_function *func;
693 struct list_head functions;
694
695 struct hash_table *strings;
696
697 /* Current function parameter index */
698 unsigned func_param_idx;
699
700 /* false by default, set to true by the ContractionOff execution mode */
701 bool exact;
702
703 /* when a physical memory model is choosen */
704 bool physical_ptrs;
705
706 /* memory model specified by OpMemoryModel */
707 unsigned mem_model;
708 };
709
710 const char *
711 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
712 unsigned word_count, unsigned *words_used);
713
714 nir_def *
715 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
716 struct vtn_pointer *
717 vtn_pointer_from_ssa(struct vtn_builder *b, nir_def *ssa,
718 struct vtn_type *ptr_type);
719
720 struct vtn_ssa_value *
721 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
722 const struct glsl_type *type);
723
724 static inline struct vtn_value *
vtn_untyped_value(struct vtn_builder * b,uint32_t value_id)725 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
726 {
727 vtn_fail_if(value_id >= b->value_id_bound,
728 "SPIR-V id %u is out-of-bounds", value_id);
729 return &b->values[value_id];
730 }
731
732 void vtn_print_value(struct vtn_builder *b, struct vtn_value *val, FILE *f);
733 void vtn_dump_values(struct vtn_builder *b, FILE *f);
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_fp_fast_math(struct vtn_builder *b, struct vtn_value *val);
973
974 void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
975 const uint32_t *w, unsigned count);
976
977 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
978 const uint32_t *words, unsigned count);
979
980 bool vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
981 const uint32_t *words, unsigned count);
982 bool vtn_handle_opencl_core_instruction(struct vtn_builder *b, SpvOp opcode,
983 const uint32_t *w, unsigned count);
984
985 struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
986 gl_shader_stage stage, const char *entry_point_name,
987 const struct spirv_to_nir_options *options);
988
989 void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
990 unsigned count);
991
992 void vtn_handle_debug_text(struct vtn_builder *b, SpvOp opcode,
993 const uint32_t *w, unsigned count);
994
995 void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
996 const uint32_t *w, unsigned count);
997
998 enum vtn_variable_mode vtn_storage_class_to_mode(struct vtn_builder *b,
999 SpvStorageClass class,
1000 struct vtn_type *interface_type,
1001 nir_variable_mode *nir_mode_out);
1002
1003 nir_address_format vtn_mode_to_address_format(struct vtn_builder *b,
1004 enum vtn_variable_mode);
1005
1006 nir_rounding_mode vtn_rounding_mode_to_nir(struct vtn_builder *b,
1007 SpvFPRoundingMode mode);
1008
1009 static inline uint32_t
vtn_align_u32(uint32_t v,uint32_t a)1010 vtn_align_u32(uint32_t v, uint32_t a)
1011 {
1012 assert(a != 0 && a == (a & -((int32_t) a)));
1013 return (v + a - 1) & ~(a - 1);
1014 }
1015
1016 static inline uint64_t
vtn_u64_literal(const uint32_t * w)1017 vtn_u64_literal(const uint32_t *w)
1018 {
1019 return (uint64_t)w[1] << 32 | w[0];
1020 }
1021
1022 bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1023 const uint32_t *words, unsigned count);
1024
1025 bool vtn_handle_amd_shader_ballot_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1026 const uint32_t *w, unsigned count);
1027
1028 bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1029 const uint32_t *words, unsigned count);
1030
1031 bool vtn_handle_amd_shader_explicit_vertex_parameter_instruction(struct vtn_builder *b,
1032 SpvOp ext_opcode,
1033 const uint32_t *words,
1034 unsigned count);
1035
1036 SpvMemorySemanticsMask vtn_mode_to_memory_semantics(enum vtn_variable_mode mode);
1037
1038 void vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
1039 SpvMemorySemanticsMask semantics);
1040
1041 bool vtn_value_is_relaxed_precision(struct vtn_builder *b, struct vtn_value *val);
1042 nir_def *
1043 vtn_mediump_downconvert(struct vtn_builder *b, enum glsl_base_type base_type, nir_def *def);
1044 struct vtn_ssa_value *
1045 vtn_mediump_downconvert_value(struct vtn_builder *b, struct vtn_ssa_value *src);
1046 void vtn_mediump_upconvert_value(struct vtn_builder *b, struct vtn_ssa_value *value);
1047
1048 static inline int
cmp_uint32_t(const void * pa,const void * pb)1049 cmp_uint32_t(const void *pa, const void *pb)
1050 {
1051 uint32_t a = *((const uint32_t *)pa);
1052 uint32_t b = *((const uint32_t *)pb);
1053 if (a < b)
1054 return -1;
1055 if (a > b)
1056 return 1;
1057 return 0;
1058 }
1059
1060 void
1061 vtn_parse_switch(struct vtn_builder *b,
1062 const uint32_t *branch,
1063 struct list_head *case_list);
1064
1065 bool vtn_get_mem_operands(struct vtn_builder *b, const uint32_t *w, unsigned count,
1066 unsigned *idx, SpvMemoryAccessMask *access, unsigned *alignment,
1067 SpvScope *dest_scope, SpvScope *src_scope);
1068 void vtn_emit_make_visible_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
1069 SpvScope scope, enum vtn_variable_mode mode);
1070 void vtn_emit_make_available_barrier(struct vtn_builder *b, SpvMemoryAccessMask access,
1071 SpvScope scope, enum vtn_variable_mode mode);
1072
1073
1074 void vtn_handle_cooperative_type(struct vtn_builder *b, struct vtn_value *val,
1075 SpvOp opcode, const uint32_t *w, unsigned count);
1076 void vtn_handle_cooperative_instruction(struct vtn_builder *b, SpvOp opcode,
1077 const uint32_t *w, unsigned count);
1078 void vtn_handle_cooperative_alu(struct vtn_builder *b, struct vtn_value *dest_val,
1079 const struct glsl_type *dest_type, SpvOp opcode,
1080 const uint32_t *w, unsigned count);
1081 struct vtn_ssa_value *vtn_cooperative_matrix_extract(struct vtn_builder *b, struct vtn_ssa_value *mat,
1082 const uint32_t *indices, unsigned num_indices);
1083 struct vtn_ssa_value *vtn_cooperative_matrix_insert(struct vtn_builder *b, struct vtn_ssa_value *mat,
1084 struct vtn_ssa_value *insert,
1085 const uint32_t *indices, unsigned num_indices);
1086 nir_deref_instr *vtn_create_cmat_temporary(struct vtn_builder *b,
1087 const struct glsl_type *t, const char *name);
1088
1089 gl_shader_stage vtn_stage_for_execution_model(SpvExecutionModel model);
1090
1091 #endif /* _VTN_PRIVATE_H_ */
1092