spirv: Only emit functions which are actually used
[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 bool referenced;
163 bool emitted;
164
165 nir_function_impl *impl;
166 struct vtn_block *start_block;
167
168 struct list_head body;
169
170 const uint32_t *end;
171
172 SpvFunctionControlMask control;
173 };
174
175 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, uint32_t,
176 const uint32_t *, unsigned);
177
178 void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
179 const uint32_t *end);
180 void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
181 vtn_instruction_handler instruction_handler);
182
183 const uint32_t *
184 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
185 const uint32_t *end, vtn_instruction_handler handler);
186
187 struct vtn_ssa_value {
188 union {
189 nir_ssa_def *def;
190 struct vtn_ssa_value **elems;
191 };
192
193 /* For matrices, if this is non-NULL, then this value is actually the
194 * transpose of some other value. The value that `transposed` points to
195 * always dominates this value.
196 */
197 struct vtn_ssa_value *transposed;
198
199 const struct glsl_type *type;
200 };
201
202 enum vtn_base_type {
203 vtn_base_type_void,
204 vtn_base_type_scalar,
205 vtn_base_type_vector,
206 vtn_base_type_matrix,
207 vtn_base_type_array,
208 vtn_base_type_struct,
209 vtn_base_type_pointer,
210 vtn_base_type_image,
211 vtn_base_type_sampler,
212 vtn_base_type_function,
213 };
214
215 struct vtn_type {
216 enum vtn_base_type base_type;
217
218 const struct glsl_type *type;
219
220 /* The value that declares this type. Used for finding decorations */
221 struct vtn_value *val;
222
223 /* Specifies the length of complex types. */
224 unsigned length;
225
226 /* for arrays, matrices and pointers, the array stride */
227 unsigned stride;
228
229 union {
230 /* Members for scalar, vector, and array-like types */
231 struct {
232 /* for arrays, the vtn_type for the elements of the array */
233 struct vtn_type *array_element;
234
235 /* for matrices, whether the matrix is stored row-major */
236 bool row_major:1;
237
238 /* Whether this type, or a parent type, has been decorated as a
239 * builtin
240 */
241 bool is_builtin:1;
242
243 /* Which built-in to use */
244 SpvBuiltIn builtin;
245 };
246
247 /* Members for struct types */
248 struct {
249 /* for structures, the vtn_type for each member */
250 struct vtn_type **members;
251
252 /* for structs, the offset of each member */
253 unsigned *offsets;
254
255 /* for structs, whether it was decorated as a "non-SSBO-like" block */
256 bool block:1;
257
258 /* for structs, whether it was decorated as an "SSBO-like" block */
259 bool buffer_block:1;
260
261 /* for structs with block == true, whether this is a builtin block
262 * (i.e. a block that contains only builtins).
263 */
264 bool builtin_block:1;
265 };
266
267 /* Members for pointer types */
268 struct {
269 /* For pointers, the vtn_type for dereferenced type */
270 struct vtn_type *deref;
271
272 /* Storage class for pointers */
273 SpvStorageClass storage_class;
274 };
275
276 /* Members for image types */
277 struct {
278 /* For images, indicates whether it's sampled or storage */
279 bool sampled;
280
281 /* Image format for image_load_store type images */
282 unsigned image_format;
283
284 /* Access qualifier for storage images */
285 SpvAccessQualifier access_qualifier;
286 };
287
288 /* Members for function types */
289 struct {
290 /* For functions, the vtn_type for each parameter */
291 struct vtn_type **params;
292
293 /* Return type for functions */
294 struct vtn_type *return_type;
295 };
296 };
297 };
298
299 struct vtn_variable;
300
301 enum vtn_access_mode {
302 vtn_access_mode_id,
303 vtn_access_mode_literal,
304 };
305
306 struct vtn_access_link {
307 enum vtn_access_mode mode;
308 uint32_t id;
309 };
310
311 struct vtn_access_chain {
312 uint32_t length;
313
314 /** Whether or not to treat the base pointer as an array. This is only
315 * true if this access chain came from an OpPtrAccessChain.
316 */
317 bool ptr_as_array;
318
319 /** Struct elements and array offsets.
320 *
321 * This is an array of 1 so that it can conveniently be created on the
322 * stack but the real length is given by the length field.
323 */
324 struct vtn_access_link link[1];
325 };
326
327 enum vtn_variable_mode {
328 vtn_variable_mode_local,
329 vtn_variable_mode_global,
330 vtn_variable_mode_param,
331 vtn_variable_mode_ubo,
332 vtn_variable_mode_ssbo,
333 vtn_variable_mode_push_constant,
334 vtn_variable_mode_image,
335 vtn_variable_mode_sampler,
336 vtn_variable_mode_workgroup,
337 vtn_variable_mode_input,
338 vtn_variable_mode_output,
339 };
340
341 struct vtn_pointer {
342 /** The variable mode for the referenced data */
343 enum vtn_variable_mode mode;
344
345 /** The dereferenced type of this pointer */
346 struct vtn_type *type;
347
348 /** The pointer type of this pointer
349 *
350 * This may be NULL for some temporary pointers constructed as part of a
351 * large load, store, or copy. It MUST be valid for all pointers which are
352 * stored as SPIR-V SSA values.
353 */
354 struct vtn_type *ptr_type;
355
356 /** The referenced variable, if known
357 *
358 * This field may be NULL if the pointer uses a (block_index, offset) pair
359 * instead of an access chain.
360 */
361 struct vtn_variable *var;
362
363 /** An access chain describing how to get from var to the referenced data
364 *
365 * This field may be NULL if the pointer references the entire variable or
366 * if a (block_index, offset) pair is used instead of an access chain.
367 */
368 struct vtn_access_chain *chain;
369
370 /** A (block_index, offset) pair representing a UBO or SSBO position. */
371 struct nir_ssa_def *block_index;
372 struct nir_ssa_def *offset;
373 };
374
375 static inline bool
376 vtn_pointer_uses_ssa_offset(struct vtn_pointer *ptr)
377 {
378 return ptr->mode == vtn_variable_mode_ubo ||
379 ptr->mode == vtn_variable_mode_ssbo;
380 }
381
382 struct vtn_variable {
383 enum vtn_variable_mode mode;
384
385 struct vtn_type *type;
386
387 unsigned descriptor_set;
388 unsigned binding;
389 unsigned input_attachment_index;
390 bool patch;
391
392 nir_variable *var;
393 nir_variable **members;
394
395 /**
396 * In some early released versions of GLSLang, it implemented all function
397 * calls by making copies of all parameters into temporary variables and
398 * passing those variables into the function. It even did so for samplers
399 * and images which violates the SPIR-V spec. Unfortunately, two games
400 * (Talos Principle and Doom) shipped with this old version of GLSLang and
401 * also happen to pass samplers into functions. Talos Principle received
402 * an update fairly shortly after release with an updated GLSLang. Doom,
403 * on the other hand, has never received an update so we need to work
404 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
405 * hack at some point in the future.
406 */
407 struct vtn_pointer *copy_prop_sampler;
408 };
409
410 struct vtn_image_pointer {
411 struct vtn_pointer *image;
412 nir_ssa_def *coord;
413 nir_ssa_def *sample;
414 };
415
416 struct vtn_sampled_image {
417 struct vtn_type *type;
418 struct vtn_pointer *image; /* Image or array of images */
419 struct vtn_pointer *sampler; /* Sampler */
420 };
421
422 struct vtn_value {
423 enum vtn_value_type value_type;
424 const char *name;
425 struct vtn_decoration *decoration;
426 union {
427 void *ptr;
428 char *str;
429 struct vtn_type *type;
430 struct {
431 nir_constant *constant;
432 const struct glsl_type *const_type;
433 };
434 struct vtn_pointer *pointer;
435 struct vtn_image_pointer *image;
436 struct vtn_sampled_image *sampled_image;
437 struct vtn_function *func;
438 struct vtn_block *block;
439 struct vtn_ssa_value *ssa;
440 vtn_instruction_handler ext_handler;
441 };
442 };
443
444 #define VTN_DEC_DECORATION -1
445 #define VTN_DEC_EXECUTION_MODE -2
446 #define VTN_DEC_STRUCT_MEMBER0 0
447
448 struct vtn_decoration {
449 struct vtn_decoration *next;
450
451 /* Specifies how to apply this decoration. Negative values represent a
452 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
453 * Non-negative values specify that it applies to a structure member.
454 */
455 int scope;
456
457 const uint32_t *literals;
458 struct vtn_value *group;
459
460 union {
461 SpvDecoration decoration;
462 SpvExecutionMode exec_mode;
463 };
464 };
465
466 struct vtn_builder {
467 nir_builder nb;
468
469 nir_shader *shader;
470 const struct nir_spirv_supported_extensions *ext;
471 struct vtn_block *block;
472
473 /* Current file, line, and column. Useful for debugging. Set
474 * automatically by vtn_foreach_instruction.
475 */
476 char *file;
477 int line, col;
478
479 /*
480 * In SPIR-V, constants are global, whereas in NIR, the load_const
481 * instruction we use is per-function. So while we parse each function, we
482 * keep a hash table of constants we've resolved to nir_ssa_value's so
483 * far, and we lazily resolve them when we see them used in a function.
484 */
485 struct hash_table *const_table;
486
487 /*
488 * Map from phi instructions (pointer to the start of the instruction)
489 * to the variable corresponding to it.
490 */
491 struct hash_table *phi_table;
492
493 unsigned num_specializations;
494 struct nir_spirv_specialization *specializations;
495
496 unsigned value_id_bound;
497 struct vtn_value *values;
498
499 gl_shader_stage entry_point_stage;
500 const char *entry_point_name;
501 struct vtn_value *entry_point;
502 bool origin_upper_left;
503 bool pixel_center_integer;
504
505 struct vtn_function *func;
506 struct exec_list functions;
507
508 /* Current function parameter index */
509 unsigned func_param_idx;
510
511 bool has_loop_continue;
512 };
513
514 nir_ssa_def *
515 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
516 struct vtn_pointer *
517 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
518 struct vtn_type *ptr_type);
519
520 static inline struct vtn_value *
521 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
522 enum vtn_value_type value_type)
523 {
524 assert(value_id < b->value_id_bound);
525 assert(b->values[value_id].value_type == vtn_value_type_invalid);
526
527 b->values[value_id].value_type = value_type;
528
529 return &b->values[value_id];
530 }
531
532 static inline struct vtn_value *
533 vtn_push_ssa(struct vtn_builder *b, uint32_t value_id,
534 struct vtn_type *type, struct vtn_ssa_value *ssa)
535 {
536 struct vtn_value *val;
537 if (type->base_type == vtn_base_type_pointer) {
538 val = vtn_push_value(b, value_id, vtn_value_type_pointer);
539 val->pointer = vtn_pointer_from_ssa(b, ssa->def, type);
540 } else {
541 val = vtn_push_value(b, value_id, vtn_value_type_ssa);
542 val->ssa = ssa;
543 }
544 return val;
545 }
546
547 static inline struct vtn_value *
548 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
549 {
550 assert(value_id < b->value_id_bound);
551 return &b->values[value_id];
552 }
553
554 static inline struct vtn_value *
555 vtn_value(struct vtn_builder *b, uint32_t value_id,
556 enum vtn_value_type value_type)
557 {
558 struct vtn_value *val = vtn_untyped_value(b, value_id);
559 assert(val->value_type == value_type);
560 return val;
561 }
562
563 void _vtn_warn(const char *file, int line, const char *msg, ...);
564 #define vtn_warn(...) _vtn_warn(__FILE__, __LINE__, __VA_ARGS__)
565
566 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
567
568 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
569 const struct glsl_type *type);
570
571 struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
572 struct vtn_ssa_value *src);
573
574 nir_ssa_def *vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src,
575 unsigned index);
576 nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
577 nir_ssa_def *index);
578 nir_ssa_def *vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src,
579 nir_ssa_def *insert, unsigned index);
580 nir_ssa_def *vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
581 nir_ssa_def *insert, nir_ssa_def *index);
582
583 nir_deref_var *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
584
585 struct vtn_pointer *vtn_pointer_for_variable(struct vtn_builder *b,
586 struct vtn_variable *var,
587 struct vtn_type *ptr_type);
588
589 nir_deref_var *vtn_pointer_to_deref(struct vtn_builder *b,
590 struct vtn_pointer *ptr);
591 nir_ssa_def *
592 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
593 nir_ssa_def **index_out, unsigned *end_idx_out);
594
595 struct vtn_ssa_value *vtn_local_load(struct vtn_builder *b, nir_deref_var *src);
596
597 void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
598 nir_deref_var *dest);
599
600 struct vtn_ssa_value *
601 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src);
602
603 void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
604 struct vtn_pointer *dest);
605
606 void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
607 const uint32_t *w, unsigned count);
608
609
610 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
611 struct vtn_value *,
612 int member,
613 const struct vtn_decoration *,
614 void *);
615
616 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
617 vtn_decoration_foreach_cb cb, void *data);
618
619 typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
620 struct vtn_value *,
621 const struct vtn_decoration *,
622 void *);
623
624 void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
625 vtn_execution_mode_foreach_cb cb, void *data);
626
627 nir_op vtn_nir_alu_op_for_spirv_opcode(SpvOp opcode, bool *swap,
628 nir_alu_type src, nir_alu_type dst);
629
630 void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
631 const uint32_t *w, unsigned count);
632
633 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
634 const uint32_t *words, unsigned count);
635
636 static inline uint64_t
637 vtn_u64_literal(const uint32_t *w)
638 {
639 return (uint64_t)w[1] << 32 | w[0];
640 }
641
642 #endif /* _VTN_PRIVATE_H_ */