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