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