nir/spirv: Rename some things from access_chain to pointer
[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 "nir/nir.h"
32 #include "nir/nir_builder.h"
33 #include "util/u_dynarray.h"
34 #include "nir_spirv.h"
35 #include "spirv.h"
36
37 struct vtn_builder;
38 struct vtn_decoration;
39
40 enum vtn_value_type {
41 vtn_value_type_invalid = 0,
42 vtn_value_type_undef,
43 vtn_value_type_string,
44 vtn_value_type_decoration_group,
45 vtn_value_type_type,
46 vtn_value_type_constant,
47 vtn_value_type_pointer,
48 vtn_value_type_function,
49 vtn_value_type_block,
50 vtn_value_type_ssa,
51 vtn_value_type_extension,
52 vtn_value_type_image_pointer,
53 vtn_value_type_sampled_image,
54 };
55
56 enum vtn_branch_type {
57 vtn_branch_type_none,
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,
64 };
65
66 enum vtn_cf_node_type {
67 vtn_cf_node_type_block,
68 vtn_cf_node_type_if,
69 vtn_cf_node_type_loop,
70 vtn_cf_node_type_switch,
71 };
72
73 struct vtn_cf_node {
74 struct list_head link;
75 enum vtn_cf_node_type type;
76 };
77
78 struct vtn_loop {
79 struct vtn_cf_node node;
80
81 /* The main body of the loop */
82 struct list_head body;
83
84 /* The "continue" part of the loop. This gets executed after the body
85 * and is where you go when you hit a continue.
86 */
87 struct list_head cont_body;
88
89 SpvLoopControlMask control;
90 };
91
92 struct vtn_if {
93 struct vtn_cf_node node;
94
95 uint32_t condition;
96
97 enum vtn_branch_type then_type;
98 struct list_head then_body;
99
100 enum vtn_branch_type else_type;
101 struct list_head else_body;
102
103 SpvSelectionControlMask control;
104 };
105
106 struct vtn_case {
107 struct list_head link;
108
109 struct list_head body;
110
111 /* The block that starts this case */
112 struct vtn_block *start_block;
113
114 /* The fallthrough case, if any */
115 struct vtn_case *fallthrough;
116
117 /* The uint32_t values that map to this case */
118 struct util_dynarray values;
119
120 /* True if this is the default case */
121 bool is_default;
122
123 /* Initialized to false; used when sorting the list of cases */
124 bool visited;
125 };
126
127 struct vtn_switch {
128 struct vtn_cf_node node;
129
130 uint32_t selector;
131
132 struct list_head cases;
133 };
134
135 struct vtn_block {
136 struct vtn_cf_node node;
137
138 /** A pointer to the label instruction */
139 const uint32_t *label;
140
141 /** A pointer to the merge instruction (or NULL if non exists) */
142 const uint32_t *merge;
143
144 /** A pointer to the branch instruction that ends this block */
145 const uint32_t *branch;
146
147 enum vtn_branch_type branch_type;
148
149 /** Points to the loop that this block starts (if it starts a loop) */
150 struct vtn_loop *loop;
151
152 /** Points to the switch case started by this block (if any) */
153 struct vtn_case *switch_case;
154
155 /** Every block ends in a nop intrinsic so that we can find it again */
156 nir_intrinsic_instr *end_nop;
157 };
158
159 struct vtn_function {
160 struct exec_node node;
161
162 nir_function_impl *impl;
163 struct vtn_block *start_block;
164
165 struct list_head body;
166
167 const uint32_t *end;
168
169 SpvFunctionControlMask control;
170 };
171
172 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, uint32_t,
173 const uint32_t *, unsigned);
174
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);
179
180 const uint32_t *
181 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
182 const uint32_t *end, vtn_instruction_handler handler);
183
184 struct vtn_ssa_value {
185 union {
186 nir_ssa_def *def;
187 struct vtn_ssa_value **elems;
188 };
189
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.
193 */
194 struct vtn_ssa_value *transposed;
195
196 const struct glsl_type *type;
197 };
198
199 struct vtn_type {
200 const struct glsl_type *type;
201
202 /* The value that declares this type. Used for finding decorations */
203 struct vtn_value *val;
204
205 /* for matrices, whether the matrix is stored row-major */
206 bool row_major;
207
208 /* for structs, the offset of each member */
209 unsigned *offsets;
210
211 /* for structs, whether it was decorated as a "non-SSBO-like" block */
212 bool block;
213
214 /* for structs, whether it was decorated as an "SSBO-like" block */
215 bool buffer_block;
216
217 /* for structs with block == true, whether this is a builtin block (i.e. a
218 * block that contains only builtins).
219 */
220 bool builtin_block;
221
222 /* Image format for image_load_store type images */
223 unsigned image_format;
224
225 /* Access qualifier for storage images */
226 SpvAccessQualifier access_qualifier;
227
228 /* for arrays and matrices, the array stride */
229 unsigned stride;
230
231 /* for arrays, the vtn_type for the elements of the array */
232 struct vtn_type *array_element;
233
234 /* for structures, the vtn_type for each member */
235 struct vtn_type **members;
236
237 /* Whether this type, or a parent type, has been decorated as a builtin */
238 bool is_builtin;
239
240 SpvBuiltIn builtin;
241 };
242
243 struct vtn_variable;
244
245 enum vtn_access_mode {
246 vtn_access_mode_id,
247 vtn_access_mode_literal,
248 };
249
250 struct vtn_access_link {
251 enum vtn_access_mode mode;
252 uint32_t id;
253 };
254
255 struct vtn_access_chain {
256 struct vtn_variable *var;
257
258 uint32_t length;
259
260 /* Struct elements and array offsets */
261 struct vtn_access_link link[0];
262 };
263
264 enum vtn_variable_mode {
265 vtn_variable_mode_local,
266 vtn_variable_mode_global,
267 vtn_variable_mode_param,
268 vtn_variable_mode_ubo,
269 vtn_variable_mode_ssbo,
270 vtn_variable_mode_push_constant,
271 vtn_variable_mode_image,
272 vtn_variable_mode_sampler,
273 vtn_variable_mode_workgroup,
274 vtn_variable_mode_input,
275 vtn_variable_mode_output,
276 };
277
278 struct vtn_variable {
279 enum vtn_variable_mode mode;
280
281 struct vtn_type *type;
282
283 unsigned descriptor_set;
284 unsigned binding;
285 unsigned input_attachment_index;
286 bool patch;
287
288 nir_variable *var;
289 nir_variable **members;
290
291 /**
292 * In some early released versions of GLSLang, it implemented all function
293 * calls by making copies of all parameters into temporary variables and
294 * passing those variables into the function. It even did so for samplers
295 * and images which violates the SPIR-V spec. Unfortunately, two games
296 * (Talos Principle and Doom) shipped with this old version of GLSLang and
297 * also happen to pass samplers into functions. Talos Principle received
298 * an update fairly shortly after release with an updated GLSLang. Doom,
299 * on the other hand, has never received an update so we need to work
300 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
301 * hack at some point in the future.
302 */
303 struct vtn_access_chain *copy_prop_sampler;
304
305 struct vtn_access_chain ptr;
306 };
307
308 struct vtn_image_pointer {
309 struct vtn_access_chain *image;
310 nir_ssa_def *coord;
311 nir_ssa_def *sample;
312 };
313
314 struct vtn_sampled_image {
315 struct vtn_access_chain *image; /* Image or array of images */
316 struct vtn_access_chain *sampler; /* Sampler */
317 };
318
319 struct vtn_value {
320 enum vtn_value_type value_type;
321 const char *name;
322 struct vtn_decoration *decoration;
323 union {
324 void *ptr;
325 char *str;
326 struct vtn_type *type;
327 struct {
328 nir_constant *constant;
329 const struct glsl_type *const_type;
330 };
331 struct vtn_access_chain *pointer;
332 struct vtn_image_pointer *image;
333 struct vtn_sampled_image *sampled_image;
334 struct vtn_function *func;
335 struct vtn_block *block;
336 struct vtn_ssa_value *ssa;
337 vtn_instruction_handler ext_handler;
338 };
339 };
340
341 #define VTN_DEC_DECORATION -1
342 #define VTN_DEC_EXECUTION_MODE -2
343 #define VTN_DEC_STRUCT_MEMBER0 0
344
345 struct vtn_decoration {
346 struct vtn_decoration *next;
347
348 /* Specifies how to apply this decoration. Negative values represent a
349 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
350 * Non-negative values specify that it applies to a structure member.
351 */
352 int scope;
353
354 const uint32_t *literals;
355 struct vtn_value *group;
356
357 union {
358 SpvDecoration decoration;
359 SpvExecutionMode exec_mode;
360 };
361 };
362
363 struct vtn_builder {
364 nir_builder nb;
365
366 nir_shader *shader;
367 nir_function_impl *impl;
368 const struct nir_spirv_supported_extensions *ext;
369 struct vtn_block *block;
370
371 /* Current file, line, and column. Useful for debugging. Set
372 * automatically by vtn_foreach_instruction.
373 */
374 char *file;
375 int line, col;
376
377 /*
378 * In SPIR-V, constants are global, whereas in NIR, the load_const
379 * instruction we use is per-function. So while we parse each function, we
380 * keep a hash table of constants we've resolved to nir_ssa_value's so
381 * far, and we lazily resolve them when we see them used in a function.
382 */
383 struct hash_table *const_table;
384
385 /*
386 * Map from phi instructions (pointer to the start of the instruction)
387 * to the variable corresponding to it.
388 */
389 struct hash_table *phi_table;
390
391 unsigned num_specializations;
392 struct nir_spirv_specialization *specializations;
393
394 unsigned value_id_bound;
395 struct vtn_value *values;
396
397 gl_shader_stage entry_point_stage;
398 const char *entry_point_name;
399 struct vtn_value *entry_point;
400 bool origin_upper_left;
401 bool pixel_center_integer;
402
403 struct vtn_function *func;
404 struct exec_list functions;
405
406 /* Current function parameter index */
407 unsigned func_param_idx;
408
409 bool has_loop_continue;
410 };
411
412 static inline struct vtn_value *
413 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
414 enum vtn_value_type value_type)
415 {
416 assert(value_id < b->value_id_bound);
417 assert(b->values[value_id].value_type == vtn_value_type_invalid);
418
419 b->values[value_id].value_type = value_type;
420
421 return &b->values[value_id];
422 }
423
424 static inline struct vtn_value *
425 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
426 {
427 assert(value_id < b->value_id_bound);
428 return &b->values[value_id];
429 }
430
431 static inline struct vtn_value *
432 vtn_value(struct vtn_builder *b, uint32_t value_id,
433 enum vtn_value_type value_type)
434 {
435 struct vtn_value *val = vtn_untyped_value(b, value_id);
436 assert(val->value_type == value_type);
437 return val;
438 }
439
440 void _vtn_warn(const char *file, int line, const char *msg, ...);
441 #define vtn_warn(...) _vtn_warn(__FILE__, __LINE__, __VA_ARGS__)
442
443 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
444
445 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
446 const struct glsl_type *type);
447
448 struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
449 struct vtn_ssa_value *src);
450
451 nir_ssa_def *vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src,
452 unsigned index);
453 nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
454 nir_ssa_def *index);
455 nir_ssa_def *vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src,
456 nir_ssa_def *insert, unsigned index);
457 nir_ssa_def *vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
458 nir_ssa_def *insert, nir_ssa_def *index);
459
460 nir_deref_var *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
461
462 nir_deref_var *vtn_pointer_to_deref(struct vtn_builder *b,
463 struct vtn_access_chain *ptr);
464 nir_ssa_def *
465 vtn_pointer_to_offset(struct vtn_builder *b,
466 struct vtn_access_chain *ptr,
467 nir_ssa_def **index_out, struct vtn_type **type_out,
468 unsigned *end_idx_out, bool stop_at_matrix);
469
470 struct vtn_ssa_value *vtn_local_load(struct vtn_builder *b, nir_deref_var *src);
471
472 void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
473 nir_deref_var *dest);
474
475 struct vtn_ssa_value *
476 vtn_variable_load(struct vtn_builder *b, struct vtn_access_chain *src);
477
478 void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
479 struct vtn_access_chain *dest);
480
481 void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
482 const uint32_t *w, unsigned count);
483
484
485 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
486 struct vtn_value *,
487 int member,
488 const struct vtn_decoration *,
489 void *);
490
491 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
492 vtn_decoration_foreach_cb cb, void *data);
493
494 typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
495 struct vtn_value *,
496 const struct vtn_decoration *,
497 void *);
498
499 void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
500 vtn_execution_mode_foreach_cb cb, void *data);
501
502 nir_op vtn_nir_alu_op_for_spirv_opcode(SpvOp opcode, bool *swap,
503 nir_alu_type src, nir_alu_type dst);
504
505 void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
506 const uint32_t *w, unsigned count);
507
508 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
509 const uint32_t *words, unsigned count);
510
511 static inline uint64_t
512 vtn_u64_literal(const uint32_t *w)
513 {
514 return (uint64_t)w[1] << 32 | w[0];
515 }
516
517 #endif /* _VTN_PRIVATE_H_ */