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)
29 #include "nir/nir_builder.h"
30 #include "nir/nir_array.h"
31 #include "nir_spirv.h"
35 struct vtn_decoration
;
38 vtn_value_type_invalid
= 0,
40 vtn_value_type_string
,
41 vtn_value_type_decoration_group
,
43 vtn_value_type_constant
,
44 vtn_value_type_access_chain
,
45 vtn_value_type_function
,
48 vtn_value_type_extension
,
49 vtn_value_type_image_pointer
,
50 vtn_value_type_sampled_image
,
53 enum vtn_branch_type
{
55 vtn_branch_type_switch_break
,
56 vtn_branch_type_switch_fallthrough
,
57 vtn_branch_type_loop_break
,
58 vtn_branch_type_loop_continue
,
59 vtn_branch_type_discard
,
60 vtn_branch_type_return
,
63 enum vtn_cf_node_type
{
64 vtn_cf_node_type_block
,
66 vtn_cf_node_type_loop
,
67 vtn_cf_node_type_switch
,
71 struct list_head link
;
72 enum vtn_cf_node_type type
;
76 struct vtn_cf_node node
;
78 /* The main body of the loop */
79 struct list_head body
;
81 /* The "continue" part of the loop. This gets executed after the body
82 * and is where you go when you hit a continue.
84 struct list_head cont_body
;
86 SpvLoopControlMask control
;
90 struct vtn_cf_node node
;
94 enum vtn_branch_type then_type
;
95 struct list_head then_body
;
97 enum vtn_branch_type else_type
;
98 struct list_head else_body
;
100 SpvSelectionControlMask control
;
104 struct list_head link
;
106 struct list_head body
;
108 /* The block that starts this case */
109 struct vtn_block
*start_block
;
111 /* The fallthrough case, if any */
112 struct vtn_case
*fallthrough
;
114 /* The uint32_t values that map to this case */
117 /* True if this is the default case */
120 /* Initialized to false; used when sorting the list of cases */
125 struct vtn_cf_node node
;
129 struct list_head cases
;
133 struct vtn_cf_node node
;
135 /** A pointer to the label instruction */
136 const uint32_t *label
;
138 /** A pointer to the merge instruction (or NULL if non exists) */
139 const uint32_t *merge
;
141 /** A pointer to the branch instruction that ends this block */
142 const uint32_t *branch
;
144 enum vtn_branch_type branch_type
;
146 /** Points to the loop that this block starts (if it starts a loop) */
147 struct vtn_loop
*loop
;
149 /** Points to the switch case started by this block (if any) */
150 struct vtn_case
*switch_case
;
152 /** The last block in this SPIR-V block. */
153 nir_block
*end_block
;
156 struct vtn_function
{
157 struct exec_node node
;
159 nir_function_impl
*impl
;
160 struct vtn_block
*start_block
;
162 struct list_head body
;
166 SpvFunctionControlMask control
;
169 typedef bool (*vtn_instruction_handler
)(struct vtn_builder
*, uint32_t,
170 const uint32_t *, unsigned);
172 void vtn_build_cfg(struct vtn_builder
*b
, const uint32_t *words
,
173 const uint32_t *end
);
174 void vtn_function_emit(struct vtn_builder
*b
, struct vtn_function
*func
,
175 vtn_instruction_handler instruction_handler
);
178 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
179 const uint32_t *end
, vtn_instruction_handler handler
);
181 struct vtn_ssa_value
{
184 struct vtn_ssa_value
**elems
;
187 /* For matrices, if this is non-NULL, then this value is actually the
188 * transpose of some other value. The value that `transposed` points to
189 * always dominates this value.
191 struct vtn_ssa_value
*transposed
;
193 const struct glsl_type
*type
;
197 const struct glsl_type
*type
;
199 /* for matrices, whether the matrix is stored row-major */
202 /* for structs, the offset of each member */
205 /* for structs, whether it was decorated as a "non-SSBO-like" block */
208 /* for structs, whether it was decorated as an "SSBO-like" block */
211 /* for structs with block == true, whether this is a builtin block (i.e. a
212 * block that contains only builtins).
216 /* Image format for image_load_store type images */
217 unsigned image_format
;
219 /* Access qualifier for storage images */
220 SpvAccessQualifier access_qualifier
;
222 /* for arrays and matrices, the array stride */
225 /* for arrays, the vtn_type for the elements of the array */
226 struct vtn_type
*array_element
;
228 /* for structures, the vtn_type for each member */
229 struct vtn_type
**members
;
231 /* Whether this type, or a parent type, has been decorated as a builtin */
239 enum vtn_access_mode
{
241 vtn_access_mode_literal
,
244 struct vtn_access_link
{
245 enum vtn_access_mode mode
;
249 struct vtn_access_chain
{
250 struct vtn_variable
*var
;
254 /* Struct elements and array offsets */
255 struct vtn_access_link link
[0];
258 enum vtn_variable_mode
{
259 vtn_variable_mode_local
,
260 vtn_variable_mode_global
,
261 vtn_variable_mode_param
,
262 vtn_variable_mode_ubo
,
263 vtn_variable_mode_ssbo
,
264 vtn_variable_mode_push_constant
,
265 vtn_variable_mode_image
,
266 vtn_variable_mode_sampler
,
267 vtn_variable_mode_workgroup
,
268 vtn_variable_mode_input
,
269 vtn_variable_mode_output
,
272 struct vtn_variable
{
273 enum vtn_variable_mode mode
;
275 struct vtn_type
*type
;
277 unsigned descriptor_set
;
281 nir_variable
**members
;
283 struct vtn_access_chain chain
;
286 struct vtn_image_pointer
{
287 struct vtn_access_chain
*image
;
292 struct vtn_sampled_image
{
293 struct vtn_access_chain
*image
; /* Image or array of images */
294 struct vtn_access_chain
*sampler
; /* Sampler */
298 enum vtn_value_type value_type
;
300 struct vtn_decoration
*decoration
;
304 struct vtn_type
*type
;
306 nir_constant
*constant
;
307 const struct glsl_type
*const_type
;
309 struct vtn_access_chain
*access_chain
;
310 struct vtn_image_pointer
*image
;
311 struct vtn_sampled_image
*sampled_image
;
312 struct vtn_function
*func
;
313 struct vtn_block
*block
;
314 struct vtn_ssa_value
*ssa
;
315 vtn_instruction_handler ext_handler
;
319 #define VTN_DEC_DECORATION -1
320 #define VTN_DEC_EXECUTION_MODE -2
321 #define VTN_DEC_STRUCT_MEMBER0 0
323 struct vtn_decoration
{
324 struct vtn_decoration
*next
;
326 /* Specifies how to apply this decoration. Negative values represent a
327 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
328 * Non-negative values specify that it applies to a structure member.
332 const uint32_t *literals
;
333 struct vtn_value
*group
;
336 SpvDecoration decoration
;
337 SpvExecutionMode exec_mode
;
345 nir_function_impl
*impl
;
346 struct vtn_block
*block
;
348 /* Current file, line, and column. Useful for debugging. Set
349 * automatically by vtn_foreach_instruction.
355 * In SPIR-V, constants are global, whereas in NIR, the load_const
356 * instruction we use is per-function. So while we parse each function, we
357 * keep a hash table of constants we've resolved to nir_ssa_value's so
358 * far, and we lazily resolve them when we see them used in a function.
360 struct hash_table
*const_table
;
363 * Map from phi instructions (pointer to the start of the instruction)
364 * to the variable corresponding to it.
366 struct hash_table
*phi_table
;
368 unsigned num_specializations
;
369 struct nir_spirv_specialization
*specializations
;
371 unsigned value_id_bound
;
372 struct vtn_value
*values
;
374 gl_shader_stage entry_point_stage
;
375 const char *entry_point_name
;
376 struct vtn_value
*entry_point
;
377 bool origin_upper_left
;
379 struct vtn_function
*func
;
380 struct exec_list functions
;
382 /* Current function parameter index */
383 unsigned func_param_idx
;
385 bool has_loop_continue
;
388 static inline struct vtn_value
*
389 vtn_push_value(struct vtn_builder
*b
, uint32_t value_id
,
390 enum vtn_value_type value_type
)
392 assert(value_id
< b
->value_id_bound
);
393 assert(b
->values
[value_id
].value_type
== vtn_value_type_invalid
);
395 b
->values
[value_id
].value_type
= value_type
;
397 return &b
->values
[value_id
];
400 static inline struct vtn_value
*
401 vtn_untyped_value(struct vtn_builder
*b
, uint32_t value_id
)
403 assert(value_id
< b
->value_id_bound
);
404 return &b
->values
[value_id
];
407 static inline struct vtn_value
*
408 vtn_value(struct vtn_builder
*b
, uint32_t value_id
,
409 enum vtn_value_type value_type
)
411 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
412 assert(val
->value_type
== value_type
);
416 struct vtn_ssa_value
*vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
);
418 struct vtn_ssa_value
*vtn_create_ssa_value(struct vtn_builder
*b
,
419 const struct glsl_type
*type
);
421 struct vtn_ssa_value
*vtn_ssa_transpose(struct vtn_builder
*b
,
422 struct vtn_ssa_value
*src
);
424 nir_ssa_def
*vtn_vector_extract(struct vtn_builder
*b
, nir_ssa_def
*src
,
426 nir_ssa_def
*vtn_vector_extract_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
428 nir_ssa_def
*vtn_vector_insert(struct vtn_builder
*b
, nir_ssa_def
*src
,
429 nir_ssa_def
*insert
, unsigned index
);
430 nir_ssa_def
*vtn_vector_insert_dynamic(struct vtn_builder
*b
, nir_ssa_def
*src
,
431 nir_ssa_def
*insert
, nir_ssa_def
*index
);
433 nir_deref_var
*vtn_nir_deref(struct vtn_builder
*b
, uint32_t id
);
435 nir_deref_var
*vtn_access_chain_to_deref(struct vtn_builder
*b
,
436 struct vtn_access_chain
*chain
);
438 vtn_access_chain_to_offset(struct vtn_builder
*b
,
439 struct vtn_access_chain
*chain
,
440 nir_ssa_def
**index_out
, struct vtn_type
**type_out
,
441 unsigned *end_idx_out
, bool stop_at_matrix
);
443 struct vtn_ssa_value
*vtn_local_load(struct vtn_builder
*b
, nir_deref_var
*src
);
445 void vtn_local_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
446 nir_deref_var
*dest
);
448 struct vtn_ssa_value
*
449 vtn_variable_load(struct vtn_builder
*b
, struct vtn_access_chain
*src
);
451 void vtn_variable_store(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
452 struct vtn_access_chain
*dest
);
454 void vtn_handle_variables(struct vtn_builder
*b
, SpvOp opcode
,
455 const uint32_t *w
, unsigned count
);
458 typedef void (*vtn_decoration_foreach_cb
)(struct vtn_builder
*,
461 const struct vtn_decoration
*,
464 void vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
465 vtn_decoration_foreach_cb cb
, void *data
);
467 typedef void (*vtn_execution_mode_foreach_cb
)(struct vtn_builder
*,
469 const struct vtn_decoration
*,
472 void vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
473 vtn_execution_mode_foreach_cb cb
, void *data
);
475 nir_op
vtn_nir_alu_op_for_spirv_opcode(SpvOp opcode
, bool *swap
);
477 void vtn_handle_alu(struct vtn_builder
*b
, SpvOp opcode
,
478 const uint32_t *w
, unsigned count
);
480 bool vtn_handle_glsl450_instruction(struct vtn_builder
*b
, uint32_t ext_opcode
,
481 const uint32_t *words
, unsigned count
);