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_
32 #include "nir/nir_builder.h"
33 #include "util/u_dynarray.h"
34 #include "nir_spirv.h"
38 struct vtn_decoration
;
41 vtn_value_type_invalid
= 0,
43 vtn_value_type_string
,
44 vtn_value_type_decoration_group
,
46 vtn_value_type_constant
,
47 vtn_value_type_pointer
,
48 vtn_value_type_function
,
51 vtn_value_type_extension
,
52 vtn_value_type_image_pointer
,
53 vtn_value_type_sampled_image
,
56 enum vtn_branch_type
{
58 vtn_branch_type_switch_break
,
59 vtn_branch_type_switch_fallthrough
,
60 vtn_branch_type_loop_break
,
61 vtn_branch_type_loop_continue
,
62 vtn_branch_type_discard
,
63 vtn_branch_type_return
,
66 enum vtn_cf_node_type
{
67 vtn_cf_node_type_block
,
69 vtn_cf_node_type_loop
,
70 vtn_cf_node_type_switch
,
74 struct list_head link
;
75 enum vtn_cf_node_type type
;
79 struct vtn_cf_node node
;
81 /* The main body of the loop */
82 struct list_head body
;
84 /* The "continue" part of the loop. This gets executed after the body
85 * and is where you go when you hit a continue.
87 struct list_head cont_body
;
89 SpvLoopControlMask control
;
93 struct vtn_cf_node node
;
97 enum vtn_branch_type then_type
;
98 struct list_head then_body
;
100 enum vtn_branch_type else_type
;
101 struct list_head else_body
;
103 SpvSelectionControlMask control
;
107 struct list_head link
;
109 struct list_head body
;
111 /* The block that starts this case */
112 struct vtn_block
*start_block
;
114 /* The fallthrough case, if any */
115 struct vtn_case
*fallthrough
;
117 /* The uint32_t values that map to this case */
118 struct util_dynarray values
;
120 /* True if this is the default case */
123 /* Initialized to false; used when sorting the list of cases */
128 struct vtn_cf_node node
;
132 struct list_head cases
;
136 struct vtn_cf_node node
;
138 /** A pointer to the label instruction */
139 const uint32_t *label
;
141 /** A pointer to the merge instruction (or NULL if non exists) */
142 const uint32_t *merge
;
144 /** A pointer to the branch instruction that ends this block */
145 const uint32_t *branch
;
147 enum vtn_branch_type branch_type
;
149 /** Points to the loop that this block starts (if it starts a loop) */
150 struct vtn_loop
*loop
;
152 /** Points to the switch case started by this block (if any) */
153 struct vtn_case
*switch_case
;
155 /** Every block ends in a nop intrinsic so that we can find it again */
156 nir_intrinsic_instr
*end_nop
;
159 struct vtn_function
{
160 struct exec_node node
;
162 nir_function_impl
*impl
;
163 struct vtn_block
*start_block
;
165 struct list_head body
;
169 SpvFunctionControlMask control
;
172 typedef bool (*vtn_instruction_handler
)(struct vtn_builder
*, uint32_t,
173 const uint32_t *, unsigned);
175 void vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
,
176 const uint32_t *end
);
177 void vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
178 vtn_instruction_handler instruction_handler
);
181 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
182 const uint32_t *end
, vtn_instruction_handler handler
);
184 struct vtn_ssa_value
{
187 struct vtn_ssa_value
**elems
;
190 /* For matrices, if this is non-NULL, then this value is actually the
191 * transpose of some other value. The value that `transposed` points to
192 * always dominates this value.
194 struct vtn_ssa_value
*transposed
;
196 const struct glsl_type
*type
;
200 const struct glsl_type
*type
;
202 /* The value that declares this type. Used for finding decorations */
203 struct vtn_value
*val
;
205 /* for matrices, whether the matrix is stored row-major */
208 /* for structs, the offset of each member */
211 /* for structs, whether it was decorated as a "non-SSBO-like" block */
214 /* for structs, whether it was decorated as an "SSBO-like" block */
217 /* for structs with block == true, whether this is a builtin block (i.e. a
218 * block that contains only builtins).
222 /* Image format for image_load_store type images */
223 unsigned image_format
;
225 /* Access qualifier for storage images */
226 SpvAccessQualifier access_qualifier
;
228 /* for arrays and matrices, the array stride */
231 /* for arrays, the vtn_type for the elements of the array */
232 struct vtn_type
*array_element
;
234 /* for structures, the vtn_type for each member */
235 struct vtn_type
**members
;
237 /* Whether this type, or a parent type, has been decorated as a builtin */
245 enum vtn_access_mode
{
247 vtn_access_mode_literal
,
250 struct vtn_access_link
{
251 enum vtn_access_mode mode
;
255 struct vtn_access_chain
{
258 /* Struct elements and array offsets */
259 struct vtn_access_link link
[0];
262 enum vtn_variable_mode
{
263 vtn_variable_mode_local
,
264 vtn_variable_mode_global
,
265 vtn_variable_mode_param
,
266 vtn_variable_mode_ubo
,
267 vtn_variable_mode_ssbo
,
268 vtn_variable_mode_push_constant
,
269 vtn_variable_mode_image
,
270 vtn_variable_mode_sampler
,
271 vtn_variable_mode_workgroup
,
272 vtn_variable_mode_input
,
273 vtn_variable_mode_output
,
277 /** The variable mode for the referenced data */
278 enum vtn_variable_mode mode
;
280 /** The dereferenced type of this pointer */
281 struct vtn_type
*type
;
283 /** The referenced variable */
284 struct vtn_variable
*var
;
286 /** An access chain describing how to get from var to the referenced data
288 * This field may be NULL if the pointer references the entire variable.
290 struct vtn_access_chain
*chain
;
293 struct vtn_variable
{
294 enum vtn_variable_mode mode
;
296 struct vtn_type
*type
;
298 unsigned descriptor_set
;
300 unsigned input_attachment_index
;
304 nir_variable
**members
;
307 * In some early released versions of GLSLang, it implemented all function
308 * calls by making copies of all parameters into temporary variables and
309 * passing those variables into the function. It even did so for samplers
310 * and images which violates the SPIR-V spec. Unfortunately, two games
311 * (Talos Principle and Doom) shipped with this old version of GLSLang and
312 * also happen to pass samplers into functions. Talos Principle received
313 * an update fairly shortly after release with an updated GLSLang. Doom,
314 * on the other hand, has never received an update so we need to work
315 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
316 * hack at some point in the future.
318 struct vtn_pointer
*copy_prop_sampler
;
321 struct vtn_image_pointer
{
322 struct vtn_pointer
*image
;
327 struct vtn_sampled_image
{
328 struct vtn_pointer
*image
; /* Image or array of images */
329 struct vtn_pointer
*sampler
; /* Sampler */
333 enum vtn_value_type value_type
;
335 struct vtn_decoration
*decoration
;
339 struct vtn_type
*type
;
341 nir_constant
*constant
;
342 const struct glsl_type
*const_type
;
344 struct vtn_pointer
*pointer
;
345 struct vtn_image_pointer
*image
;
346 struct vtn_sampled_image
*sampled_image
;
347 struct vtn_function
*func
;
348 struct vtn_block
*block
;
349 struct vtn_ssa_value
*ssa
;
350 vtn_instruction_handler ext_handler
;
354 #define VTN_DEC_DECORATION -1
355 #define VTN_DEC_EXECUTION_MODE -2
356 #define VTN_DEC_STRUCT_MEMBER0 0
358 struct vtn_decoration
{
359 struct vtn_decoration
*next
;
361 /* Specifies how to apply this decoration. Negative values represent a
362 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
363 * Non-negative values specify that it applies to a structure member.
367 const uint32_t *literals
;
368 struct vtn_value
*group
;
371 SpvDecoration decoration
;
372 SpvExecutionMode exec_mode
;
380 nir_function_impl
*impl
;
381 const struct nir_spirv_supported_extensions
*ext
;
382 struct vtn_block
*block
;
384 /* Current file, line, and column. Useful for debugging. Set
385 * automatically by vtn_foreach_instruction.
391 * In SPIR-V, constants are global, whereas in NIR, the load_const
392 * instruction we use is per-function. So while we parse each function, we
393 * keep a hash table of constants we've resolved to nir_ssa_value's so
394 * far, and we lazily resolve them when we see them used in a function.
396 struct hash_table
*const_table
;
399 * Map from phi instructions (pointer to the start of the instruction)
400 * to the variable corresponding to it.
402 struct hash_table
*phi_table
;
404 unsigned num_specializations
;
405 struct nir_spirv_specialization
*specializations
;
407 unsigned value_id_bound
;
408 struct vtn_value
*values
;
410 gl_shader_stage entry_point_stage
;
411 const char *entry_point_name
;
412 struct vtn_value
*entry_point
;
413 bool origin_upper_left
;
414 bool pixel_center_integer
;
416 struct vtn_function
*func
;
417 struct exec_list functions
;
419 /* Current function parameter index */
420 unsigned func_param_idx
;
422 bool has_loop_continue
;
425 static inline struct vtn_value
*
426 vtn_push_value(struct vtn_builder
*b
, uint32_t value_id
,
427 enum vtn_value_type value_type
)
429 assert(value_id
< b
->value_id_bound
);
430 assert(b
->values
[value_id
].value_type
== vtn_value_type_invalid
);
432 b
->values
[value_id
].value_type
= value_type
;
434 return &b
->values
[value_id
];
437 static inline struct vtn_value
*
438 vtn_untyped_value(struct vtn_builder
*b
, uint32_t value_id
)
440 assert(value_id
< b
->value_id_bound
);
441 return &b
->values
[value_id
];
444 static inline struct vtn_value
*
445 vtn_value(struct vtn_builder
*b
, uint32_t value_id
,
446 enum vtn_value_type value_type
)
448 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
449 assert(val
->value_type
== value_type
);
453 void _vtn_warn(const char *file
, int line
, const char *msg
, ...);
454 #define vtn_warn(...) _vtn_warn(__FILE__, __LINE__, __VA_ARGS__)
456 struct vtn_ssa_value
*vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
);
458 struct vtn_ssa_value
*vtn_create_ssa_value(struct vtn_builder
*b
,
459 const struct glsl_type
*type
);
461 struct vtn_ssa_value
*vtn_ssa_transpose(struct vtn_builder
*b
,
462 struct vtn_ssa_value
*src
);
464 nir_ssa_def
*vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
,
466 nir_ssa_def
*vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
468 nir_ssa_def
*vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
,
469 nir_ssa_def
*insert
, unsigned index
);
470 nir_ssa_def
*vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
471 nir_ssa_def
*insert
, nir_ssa_def
*index
);
473 nir_deref_var
*vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
);
475 struct vtn_pointer
*vtn_pointer_for_variable(struct vtn_builder
*b
,
476 struct vtn_variable
*var
);
478 nir_deref_var
*vtn_pointer_to_deref(struct vtn_builder
*b
,
479 struct vtn_pointer
*ptr
);
481 vtn_pointer_to_offset(struct vtn_builder
*b
, struct vtn_pointer
*ptr
,
482 nir_ssa_def
**index_out
, struct vtn_type
**type_out
,
483 unsigned *end_idx_out
, bool stop_at_matrix
);
485 struct vtn_ssa_value
*vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
);
487 void vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
488 nir_deref_var
*dest
);
490 struct vtn_ssa_value
*
491 vtn_variable_load(struct vtn_builder
*b
, struct vtn_pointer
*src
);
493 void vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
494 struct vtn_pointer
*dest
);
496 void vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
497 const uint32_t *w
, unsigned count
);
500 typedef void (*vtn_decoration_foreach_cb
)(struct vtn_builder
*,
503 const struct vtn_decoration
*,
506 void vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
507 vtn_decoration_foreach_cb cb
, void *data
);
509 typedef void (*vtn_execution_mode_foreach_cb
)(struct vtn_builder
*,
511 const struct vtn_decoration
*,
514 void vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
515 vtn_execution_mode_foreach_cb cb
, void *data
);
517 nir_op
vtn_nir_alu_op_for_spirv_opcode(SpvOp opcode
, bool *swap
,
518 nir_alu_type src
, nir_alu_type dst
);
520 void vtn_handle_alu(struct vtn_builder
*b
, SpvOp opcode
,
521 const uint32_t *w
, unsigned count
);
523 bool vtn_handle_glsl450_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
524 const uint32_t *words
, unsigned count
);
526 static inline uint64_t
527 vtn_u64_literal(const uint32_t *w
)
529 return (uint64_t)w
[1] << 32 | w
[0];
532 #endif /* _VTN_PRIVATE_H_ */