2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #ifndef _VTN_PRIVATE_H_
29 #define _VTN_PRIVATE_H_
34 #include "nir/nir_builder.h"
35 #include "util/u_dynarray.h"
36 #include "nir_spirv.h"
40 struct vtn_decoration
;
42 void vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
43 size_t spirv_offset
, const char *message
);
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);
48 #define vtn_info(...) vtn_logf(b, NIR_SPIRV_DEBUG_LEVEL_INFO, 0, __VA_ARGS__)
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__)
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__)
58 /** Fail SPIR-V parsing
60 * This function logs an error and then bails out of the shader compile using
61 * longjmp. This being safe relies on two things:
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.
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.
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.
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.
79 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
80 const char *fmt
, ...) PRINTFLIKE(4, 5);
82 #define vtn_fail(...) _vtn_fail(b, __FILE__, __LINE__, __VA_ARGS__)
84 /** Fail if the given expression evaluates to true */
85 #define vtn_fail_if(expr, ...) \
88 vtn_fail(__VA_ARGS__); \
91 /** Assert that a condition is true and, if it isn't, vtn_fail
93 * This macro is transitional only and should not be used in new code. Use
94 * vtn_fail_if and provide a real message instead.
96 #define vtn_assert(expr) \
99 vtn_fail("%s", #expr); \
102 enum vtn_value_type
{
103 vtn_value_type_invalid
= 0,
104 vtn_value_type_undef
,
105 vtn_value_type_string
,
106 vtn_value_type_decoration_group
,
108 vtn_value_type_constant
,
109 vtn_value_type_pointer
,
110 vtn_value_type_function
,
111 vtn_value_type_block
,
113 vtn_value_type_extension
,
114 vtn_value_type_image_pointer
,
115 vtn_value_type_sampled_image
,
118 enum vtn_branch_type
{
119 vtn_branch_type_none
,
120 vtn_branch_type_switch_break
,
121 vtn_branch_type_switch_fallthrough
,
122 vtn_branch_type_loop_break
,
123 vtn_branch_type_loop_continue
,
124 vtn_branch_type_discard
,
125 vtn_branch_type_return
,
128 enum vtn_cf_node_type
{
129 vtn_cf_node_type_block
,
131 vtn_cf_node_type_loop
,
132 vtn_cf_node_type_switch
,
136 struct list_head link
;
137 enum vtn_cf_node_type type
;
141 struct vtn_cf_node node
;
143 /* The main body of the loop */
144 struct list_head body
;
146 /* The "continue" part of the loop. This gets executed after the body
147 * and is where you go when you hit a continue.
149 struct list_head cont_body
;
151 SpvLoopControlMask control
;
155 struct vtn_cf_node node
;
159 enum vtn_branch_type then_type
;
160 struct list_head then_body
;
162 enum vtn_branch_type else_type
;
163 struct list_head else_body
;
165 SpvSelectionControlMask control
;
169 struct list_head link
;
171 struct list_head body
;
173 /* The block that starts this case */
174 struct vtn_block
*start_block
;
176 /* The fallthrough case, if any */
177 struct vtn_case
*fallthrough
;
179 /* The uint32_t values that map to this case */
180 struct util_dynarray values
;
182 /* True if this is the default case */
185 /* Initialized to false; used when sorting the list of cases */
190 struct vtn_cf_node node
;
194 struct list_head cases
;
198 struct vtn_cf_node node
;
200 /** A pointer to the label instruction */
201 const uint32_t *label
;
203 /** A pointer to the merge instruction (or NULL if non exists) */
204 const uint32_t *merge
;
206 /** A pointer to the branch instruction that ends this block */
207 const uint32_t *branch
;
209 enum vtn_branch_type branch_type
;
211 /** Points to the loop that this block starts (if it starts a loop) */
212 struct vtn_loop
*loop
;
214 /** Points to the switch case started by this block (if any) */
215 struct vtn_case
*switch_case
;
217 /** Every block ends in a nop intrinsic so that we can find it again */
218 nir_intrinsic_instr
*end_nop
;
221 struct vtn_function
{
222 struct exec_node node
;
224 struct vtn_type
*type
;
229 nir_function_impl
*impl
;
230 struct vtn_block
*start_block
;
232 struct list_head body
;
236 SpvFunctionControlMask control
;
239 typedef bool (*vtn_instruction_handler
)(struct vtn_builder
*, SpvOp
,
240 const uint32_t *, unsigned);
242 void vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
,
243 const uint32_t *end
);
244 void vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
245 vtn_instruction_handler instruction_handler
);
246 void vtn_handle_function_call(struct vtn_builder
*b
, SpvOp opcode
,
247 const uint32_t *w
, unsigned count
);
250 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
251 const uint32_t *end
, vtn_instruction_handler handler
);
253 struct vtn_ssa_value
{
256 struct vtn_ssa_value
**elems
;
259 /* For matrices, if this is non-NULL, then this value is actually the
260 * transpose of some other value. The value that `transposed` points to
261 * always dominates this value.
263 struct vtn_ssa_value
*transposed
;
265 const struct glsl_type
*type
;
270 vtn_base_type_scalar
,
271 vtn_base_type_vector
,
272 vtn_base_type_matrix
,
274 vtn_base_type_struct
,
275 vtn_base_type_pointer
,
277 vtn_base_type_sampler
,
278 vtn_base_type_sampled_image
,
279 vtn_base_type_function
,
283 enum vtn_base_type base_type
;
285 const struct glsl_type
*type
;
287 /* The SPIR-V id of the given type. */
290 /* Specifies the length of complex types.
292 * For Workgroup pointers, this is the size of the referenced type.
296 /* for arrays, matrices and pointers, the array stride */
299 /* Access qualifiers */
300 enum gl_access_qualifier access
;
303 /* Members for scalar, vector, and array-like types */
305 /* for arrays, the vtn_type for the elements of the array */
306 struct vtn_type
*array_element
;
308 /* for matrices, whether the matrix is stored row-major */
311 /* Whether this type, or a parent type, has been decorated as a
316 /* Which built-in to use */
320 /* Members for struct types */
322 /* for structures, the vtn_type for each member */
323 struct vtn_type
**members
;
325 /* for structs, the offset of each member */
328 /* for structs, whether it was decorated as a "non-SSBO-like" block */
331 /* for structs, whether it was decorated as an "SSBO-like" block */
334 /* for structs with block == true, whether this is a builtin block
335 * (i.e. a block that contains only builtins).
337 bool builtin_block
:1;
339 /* for structs and unions it specifies the minimum alignment of the
340 * members. 0 means packed.
342 * Set by CPacked and Alignment Decorations in kernels.
347 /* Members for pointer types */
349 /* For pointers, the vtn_type for dereferenced type */
350 struct vtn_type
*deref
;
352 /* Storage class for pointers */
353 SpvStorageClass storage_class
;
355 /* Required alignment for pointers */
359 /* Members for image types */
361 /* For images, indicates whether it's sampled or storage */
364 /* Image format for image_load_store type images */
365 unsigned image_format
;
367 /* Access qualifier for storage images */
368 SpvAccessQualifier access_qualifier
;
371 /* Members for sampled image types */
373 /* For sampled images, the image type */
374 struct vtn_type
*image
;
377 /* Members for function types */
379 /* For functions, the vtn_type for each parameter */
380 struct vtn_type
**params
;
382 /* Return type for functions */
383 struct vtn_type
*return_type
;
388 bool vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
);
390 bool vtn_types_compatible(struct vtn_builder
*b
,
391 struct vtn_type
*t1
, struct vtn_type
*t2
);
395 enum vtn_access_mode
{
397 vtn_access_mode_literal
,
400 struct vtn_access_link
{
401 enum vtn_access_mode mode
;
405 struct vtn_access_chain
{
408 /** Whether or not to treat the base pointer as an array. This is only
409 * true if this access chain came from an OpPtrAccessChain.
413 /** Struct elements and array offsets.
415 * This is an array of 1 so that it can conveniently be created on the
416 * stack but the real length is given by the length field.
418 struct vtn_access_link link
[1];
421 enum vtn_variable_mode
{
422 vtn_variable_mode_function
,
423 vtn_variable_mode_private
,
424 vtn_variable_mode_uniform
,
425 vtn_variable_mode_ubo
,
426 vtn_variable_mode_ssbo
,
427 vtn_variable_mode_phys_ssbo
,
428 vtn_variable_mode_push_constant
,
429 vtn_variable_mode_workgroup
,
430 vtn_variable_mode_cross_workgroup
,
431 vtn_variable_mode_input
,
432 vtn_variable_mode_output
,
436 /** The variable mode for the referenced data */
437 enum vtn_variable_mode mode
;
439 /** The dereferenced type of this pointer */
440 struct vtn_type
*type
;
442 /** The pointer type of this pointer
444 * This may be NULL for some temporary pointers constructed as part of a
445 * large load, store, or copy. It MUST be valid for all pointers which are
446 * stored as SPIR-V SSA values.
448 struct vtn_type
*ptr_type
;
450 /** The referenced variable, if known
452 * This field may be NULL if the pointer uses a (block_index, offset) pair
453 * instead of an access chain or if the access chain starts at a deref.
455 struct vtn_variable
*var
;
457 /** The NIR deref corresponding to this pointer */
458 nir_deref_instr
*deref
;
460 /** A (block_index, offset) pair representing a UBO or SSBO position. */
461 struct nir_ssa_def
*block_index
;
462 struct nir_ssa_def
*offset
;
464 /* Access qualifiers */
465 enum gl_access_qualifier access
;
468 bool vtn_pointer_uses_ssa_offset(struct vtn_builder
*b
,
469 struct vtn_pointer
*ptr
);
471 struct vtn_variable
{
472 enum vtn_variable_mode mode
;
474 struct vtn_type
*type
;
476 unsigned descriptor_set
;
478 bool explicit_binding
;
480 unsigned input_attachment_index
;
485 /* If the variable is a struct with a location set on it then this will be
486 * stored here. This will be used to calculate locations for members that
487 * don’t have their own explicit location.
494 * In some early released versions of GLSLang, it implemented all function
495 * calls by making copies of all parameters into temporary variables and
496 * passing those variables into the function. It even did so for samplers
497 * and images which violates the SPIR-V spec. Unfortunately, two games
498 * (Talos Principle and Doom) shipped with this old version of GLSLang and
499 * also happen to pass samplers into functions. Talos Principle received
500 * an update fairly shortly after release with an updated GLSLang. Doom,
501 * on the other hand, has never received an update so we need to work
502 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
503 * hack at some point in the future.
505 struct vtn_pointer
*copy_prop_sampler
;
507 /* Access qualifiers. */
508 enum gl_access_qualifier access
;
511 struct vtn_image_pointer
{
512 struct vtn_pointer
*image
;
517 struct vtn_sampled_image
{
518 struct vtn_type
*type
;
519 struct vtn_pointer
*image
; /* Image or array of images */
520 struct vtn_pointer
*sampler
; /* Sampler */
524 enum vtn_value_type value_type
;
526 struct vtn_decoration
*decoration
;
527 struct vtn_type
*type
;
531 nir_constant
*constant
;
532 struct vtn_pointer
*pointer
;
533 struct vtn_image_pointer
*image
;
534 struct vtn_sampled_image
*sampled_image
;
535 struct vtn_function
*func
;
536 struct vtn_block
*block
;
537 struct vtn_ssa_value
*ssa
;
538 vtn_instruction_handler ext_handler
;
542 #define VTN_DEC_DECORATION -1
543 #define VTN_DEC_EXECUTION_MODE -2
544 #define VTN_DEC_STRUCT_MEMBER0 0
546 struct vtn_decoration
{
547 struct vtn_decoration
*next
;
549 /* Specifies how to apply this decoration. Negative values represent a
550 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
551 * Non-negative values specify that it applies to a structure member.
555 const uint32_t *literals
;
556 struct vtn_value
*group
;
559 SpvDecoration decoration
;
560 SpvExecutionMode exec_mode
;
567 /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
570 const uint32_t *spirv
;
571 size_t spirv_word_count
;
574 struct spirv_to_nir_options
*options
;
575 struct vtn_block
*block
;
577 /* Current offset, file, line, and column. Useful for debugging. Set
578 * automatically by vtn_foreach_instruction.
585 * In SPIR-V, constants are global, whereas in NIR, the load_const
586 * instruction we use is per-function. So while we parse each function, we
587 * keep a hash table of constants we've resolved to nir_ssa_value's so
588 * far, and we lazily resolve them when we see them used in a function.
590 struct hash_table
*const_table
;
593 * Map from phi instructions (pointer to the start of the instruction)
594 * to the variable corresponding to it.
596 struct hash_table
*phi_table
;
598 unsigned num_specializations
;
599 struct nir_spirv_specialization
*specializations
;
601 unsigned value_id_bound
;
602 struct vtn_value
*values
;
604 /* True if we should watch out for GLSLang issue #179 */
607 gl_shader_stage entry_point_stage
;
608 const char *entry_point_name
;
609 struct vtn_value
*entry_point
;
610 struct vtn_value
*workgroup_size_builtin
;
611 bool variable_pointers
;
613 struct vtn_function
*func
;
614 struct exec_list functions
;
616 /* Current function parameter index */
617 unsigned func_param_idx
;
619 bool has_loop_continue
;
621 /* false by default, set to true by the ContractionOff execution mode */
624 /* when a physical memory model is choosen */
629 vtn_pointer_to_ssa(struct vtn_builder
*b
, struct vtn_pointer
*ptr
);
631 vtn_pointer_from_ssa(struct vtn_builder
*b
, nir_ssa_def
*ssa
,
632 struct vtn_type
*ptr_type
);
634 static inline struct vtn_value
*
635 vtn_untyped_value(struct vtn_builder
*b
, uint32_t value_id
)
637 vtn_fail_if(value_id
>= b
->value_id_bound
,
638 "SPIR-V id %u is out-of-bounds", value_id
);
639 return &b
->values
[value_id
];
642 static inline struct vtn_value
*
643 vtn_push_value(struct vtn_builder
*b
, uint32_t value_id
,
644 enum vtn_value_type value_type
)
646 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
648 vtn_fail_if(val
->value_type
!= vtn_value_type_invalid
,
649 "SPIR-V id %u has already been written by another instruction",
652 val
->value_type
= value_type
;
653 return &b
->values
[value_id
];
656 static inline struct vtn_value
*
657 vtn_push_ssa(struct vtn_builder
*b
, uint32_t value_id
,
658 struct vtn_type
*type
, struct vtn_ssa_value
*ssa
)
660 struct vtn_value
*val
;
661 if (type
->base_type
== vtn_base_type_pointer
) {
662 val
= vtn_push_value(b
, value_id
, vtn_value_type_pointer
);
663 val
->pointer
= vtn_pointer_from_ssa(b
, ssa
->def
, type
);
665 val
= vtn_push_value(b
, value_id
, vtn_value_type_ssa
);
671 static inline struct vtn_value
*
672 vtn_value(struct vtn_builder
*b
, uint32_t value_id
,
673 enum vtn_value_type value_type
)
675 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
676 vtn_fail_if(val
->value_type
!= value_type
,
677 "SPIR-V id %u is the wrong kind of value", value_id
);
682 vtn_set_instruction_result_type(struct vtn_builder
*b
, SpvOp opcode
,
683 const uint32_t *w
, unsigned count
);
685 static inline uint64_t
686 vtn_constant_uint(struct vtn_builder
*b
, uint32_t value_id
)
688 struct vtn_value
*val
= vtn_value(b
, value_id
, vtn_value_type_constant
);
690 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
||
691 !glsl_type_is_integer(val
->type
->type
),
692 "Expected id %u to be an integer constant", value_id
);
694 switch (glsl_get_bit_size(val
->type
->type
)) {
695 case 8: return val
->constant
->values
[0].u8
[0];
696 case 16: return val
->constant
->values
[0].u16
[0];
697 case 32: return val
->constant
->values
[0].u32
[0];
698 case 64: return val
->constant
->values
[0].u64
[0];
699 default: unreachable("Invalid bit size");
703 struct vtn_ssa_value
*vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
);
705 struct vtn_ssa_value
*vtn_create_ssa_value(struct vtn_builder
*b
,
706 const struct glsl_type
*type
);
708 struct vtn_ssa_value
*vtn_ssa_transpose(struct vtn_builder
*b
,
709 struct vtn_ssa_value
*src
);
711 nir_ssa_def
*vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
,
713 nir_ssa_def
*vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
715 nir_ssa_def
*vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
,
716 nir_ssa_def
*insert
, unsigned index
);
717 nir_ssa_def
*vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
718 nir_ssa_def
*insert
, nir_ssa_def
*index
);
720 nir_deref_instr
*vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
);
722 struct vtn_pointer
*vtn_pointer_for_variable(struct vtn_builder
*b
,
723 struct vtn_variable
*var
,
724 struct vtn_type
*ptr_type
);
726 nir_deref_instr
*vtn_pointer_to_deref(struct vtn_builder
*b
,
727 struct vtn_pointer
*ptr
);
729 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
730 nir_ssa_def
**index_out
);
732 struct vtn_ssa_value
*
733 vtn_local_load(struct vtn_builder
*b
, nir_deref_instr
*src
);
735 void vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
736 nir_deref_instr
*dest
);
738 struct vtn_ssa_value
*
739 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
);
741 void vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
742 struct vtn_pointer
*dest
);
744 void vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
745 const uint32_t *w
, unsigned count
);
748 typedef void (*vtn_decoration_foreach_cb
)(struct vtn_builder
*,
751 const struct vtn_decoration
*,
754 void vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
755 vtn_decoration_foreach_cb cb
, void *data
);
757 typedef void (*vtn_execution_mode_foreach_cb
)(struct vtn_builder
*,
759 const struct vtn_decoration
*,
762 void vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
763 vtn_execution_mode_foreach_cb cb
, void *data
);
765 nir_op
vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder
*b
,
766 SpvOp opcode
, bool *swap
,
767 unsigned src_bit_size
, unsigned dst_bit_size
);
769 void vtn_handle_alu(struct vtn_builder
*b
, SpvOp opcode
,
770 const uint32_t *w
, unsigned count
);
772 void vtn_handle_subgroup(struct vtn_builder
*b
, SpvOp opcode
,
773 const uint32_t *w
, unsigned count
);
775 bool vtn_handle_glsl450_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
776 const uint32_t *words
, unsigned count
);
778 bool vtn_handle_opencl_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
779 const uint32_t *words
, unsigned count
);
781 struct vtn_builder
* vtn_create_builder(const uint32_t *words
, size_t word_count
,
782 gl_shader_stage stage
, const char *entry_point_name
,
783 const struct spirv_to_nir_options
*options
);
785 void vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
788 void vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
789 const uint32_t *w
, unsigned count
);
791 static inline uint32_t
792 vtn_align_u32(uint32_t v
, uint32_t a
)
794 assert(a
!= 0 && a
== (a
& -((int32_t) a
)));
795 return (v
+ a
- 1) & ~(a
- 1);
798 static inline uint64_t
799 vtn_u64_literal(const uint32_t *w
)
801 return (uint64_t)w
[1] << 32 | w
[0];
804 bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
805 const uint32_t *words
, unsigned count
);
807 bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
808 const uint32_t *words
, unsigned count
);
809 #endif /* _VTN_PRIVATE_H_ */