spirv: Refactor a couple of pointer query helpers
[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 <setjmp.h>
32
33 #include "nir/nir.h"
34 #include "nir/nir_builder.h"
35 #include "util/u_dynarray.h"
36 #include "nir_spirv.h"
37 #include "spirv.h"
38
39 struct vtn_builder;
40 struct vtn_decoration;
41
42 void vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
43 size_t spirv_offset, const char *message);
44
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);
47
48 #define vtn_info(...) vtn_logf(b, NIR_SPIRV_DEBUG_LEVEL_INFO, 0, __VA_ARGS__)
49
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__)
53
54 /** Fail SPIR-V parsing
55 *
56 * This function logs an error and then bails out of the shader compile using
57 * longjmp. This being safe relies on two things:
58 *
59 * 1) We must guarantee that setjmp is called after allocating the builder
60 * and setting up b->debug (so that logging works) but before before any
61 * errors have a chance to occur.
62 *
63 * 2) While doing the SPIR-V -> NIR conversion, we need to be careful to
64 * ensure that all heap allocations happen through ralloc and are parented
65 * to the builder. This way they will get properly cleaned up on error.
66 *
67 * 3) We must ensure that _vtn_fail is never called while a mutex lock or a
68 * reference to any other resource is held with the exception of ralloc
69 * objects which are parented to the builder.
70 *
71 * So long as these two things continue to hold, we can easily longjmp back to
72 * spirv_to_nir(), clean up the builder, and return NULL.
73 */
74 void _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
75 const char *fmt, ...) NORETURN PRINTFLIKE(4, 5);
76 #define vtn_fail(...) _vtn_fail(b, __FILE__, __LINE__, __VA_ARGS__)
77
78 /** Fail if the given expression evaluates to true */
79 #define vtn_fail_if(expr, ...) \
80 do { \
81 if (unlikely(expr)) \
82 vtn_fail(__VA_ARGS__); \
83 } while (0)
84
85 /** Assert that a condition is true and, if it isn't, vtn_fail
86 *
87 * This macro is transitional only and should not be used in new code. Use
88 * vtn_fail_if and provide a real message instead.
89 */
90 #define vtn_assert(expr) \
91 do { \
92 if (!likely(expr)) \
93 vtn_fail("%s", #expr); \
94 } while (0)
95
96 enum vtn_value_type {
97 vtn_value_type_invalid = 0,
98 vtn_value_type_undef,
99 vtn_value_type_string,
100 vtn_value_type_decoration_group,
101 vtn_value_type_type,
102 vtn_value_type_constant,
103 vtn_value_type_pointer,
104 vtn_value_type_function,
105 vtn_value_type_block,
106 vtn_value_type_ssa,
107 vtn_value_type_extension,
108 vtn_value_type_image_pointer,
109 vtn_value_type_sampled_image,
110 };
111
112 enum vtn_branch_type {
113 vtn_branch_type_none,
114 vtn_branch_type_switch_break,
115 vtn_branch_type_switch_fallthrough,
116 vtn_branch_type_loop_break,
117 vtn_branch_type_loop_continue,
118 vtn_branch_type_discard,
119 vtn_branch_type_return,
120 };
121
122 enum vtn_cf_node_type {
123 vtn_cf_node_type_block,
124 vtn_cf_node_type_if,
125 vtn_cf_node_type_loop,
126 vtn_cf_node_type_switch,
127 };
128
129 struct vtn_cf_node {
130 struct list_head link;
131 enum vtn_cf_node_type type;
132 };
133
134 struct vtn_loop {
135 struct vtn_cf_node node;
136
137 /* The main body of the loop */
138 struct list_head body;
139
140 /* The "continue" part of the loop. This gets executed after the body
141 * and is where you go when you hit a continue.
142 */
143 struct list_head cont_body;
144
145 SpvLoopControlMask control;
146 };
147
148 struct vtn_if {
149 struct vtn_cf_node node;
150
151 uint32_t condition;
152
153 enum vtn_branch_type then_type;
154 struct list_head then_body;
155
156 enum vtn_branch_type else_type;
157 struct list_head else_body;
158
159 SpvSelectionControlMask control;
160 };
161
162 struct vtn_case {
163 struct list_head link;
164
165 struct list_head body;
166
167 /* The block that starts this case */
168 struct vtn_block *start_block;
169
170 /* The fallthrough case, if any */
171 struct vtn_case *fallthrough;
172
173 /* The uint32_t values that map to this case */
174 struct util_dynarray values;
175
176 /* True if this is the default case */
177 bool is_default;
178
179 /* Initialized to false; used when sorting the list of cases */
180 bool visited;
181 };
182
183 struct vtn_switch {
184 struct vtn_cf_node node;
185
186 uint32_t selector;
187
188 struct list_head cases;
189 };
190
191 struct vtn_block {
192 struct vtn_cf_node node;
193
194 /** A pointer to the label instruction */
195 const uint32_t *label;
196
197 /** A pointer to the merge instruction (or NULL if non exists) */
198 const uint32_t *merge;
199
200 /** A pointer to the branch instruction that ends this block */
201 const uint32_t *branch;
202
203 enum vtn_branch_type branch_type;
204
205 /** Points to the loop that this block starts (if it starts a loop) */
206 struct vtn_loop *loop;
207
208 /** Points to the switch case started by this block (if any) */
209 struct vtn_case *switch_case;
210
211 /** Every block ends in a nop intrinsic so that we can find it again */
212 nir_intrinsic_instr *end_nop;
213 };
214
215 struct vtn_function {
216 struct exec_node node;
217
218 bool referenced;
219 bool emitted;
220
221 nir_function_impl *impl;
222 struct vtn_block *start_block;
223
224 struct list_head body;
225
226 const uint32_t *end;
227
228 SpvFunctionControlMask control;
229 };
230
231 typedef bool (*vtn_instruction_handler)(struct vtn_builder *, uint32_t,
232 const uint32_t *, unsigned);
233
234 void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
235 const uint32_t *end);
236 void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
237 vtn_instruction_handler instruction_handler);
238
239 const uint32_t *
240 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
241 const uint32_t *end, vtn_instruction_handler handler);
242
243 struct vtn_ssa_value {
244 union {
245 nir_ssa_def *def;
246 struct vtn_ssa_value **elems;
247 };
248
249 /* For matrices, if this is non-NULL, then this value is actually the
250 * transpose of some other value. The value that `transposed` points to
251 * always dominates this value.
252 */
253 struct vtn_ssa_value *transposed;
254
255 const struct glsl_type *type;
256 };
257
258 enum vtn_base_type {
259 vtn_base_type_void,
260 vtn_base_type_scalar,
261 vtn_base_type_vector,
262 vtn_base_type_matrix,
263 vtn_base_type_array,
264 vtn_base_type_struct,
265 vtn_base_type_pointer,
266 vtn_base_type_image,
267 vtn_base_type_sampler,
268 vtn_base_type_function,
269 };
270
271 struct vtn_type {
272 enum vtn_base_type base_type;
273
274 const struct glsl_type *type;
275
276 /* The value that declares this type. Used for finding decorations */
277 struct vtn_value *val;
278
279 /* Specifies the length of complex types. */
280 unsigned length;
281
282 /* for arrays, matrices and pointers, the array stride */
283 unsigned stride;
284
285 union {
286 /* Members for scalar, vector, and array-like types */
287 struct {
288 /* for arrays, the vtn_type for the elements of the array */
289 struct vtn_type *array_element;
290
291 /* for matrices, whether the matrix is stored row-major */
292 bool row_major:1;
293
294 /* Whether this type, or a parent type, has been decorated as a
295 * builtin
296 */
297 bool is_builtin:1;
298
299 /* Which built-in to use */
300 SpvBuiltIn builtin;
301 };
302
303 /* Members for struct types */
304 struct {
305 /* for structures, the vtn_type for each member */
306 struct vtn_type **members;
307
308 /* for structs, the offset of each member */
309 unsigned *offsets;
310
311 /* for structs, whether it was decorated as a "non-SSBO-like" block */
312 bool block:1;
313
314 /* for structs, whether it was decorated as an "SSBO-like" block */
315 bool buffer_block:1;
316
317 /* for structs with block == true, whether this is a builtin block
318 * (i.e. a block that contains only builtins).
319 */
320 bool builtin_block:1;
321 };
322
323 /* Members for pointer types */
324 struct {
325 /* For pointers, the vtn_type for dereferenced type */
326 struct vtn_type *deref;
327
328 /* Storage class for pointers */
329 SpvStorageClass storage_class;
330 };
331
332 /* Members for image types */
333 struct {
334 /* For images, indicates whether it's sampled or storage */
335 bool sampled;
336
337 /* Image format for image_load_store type images */
338 unsigned image_format;
339
340 /* Access qualifier for storage images */
341 SpvAccessQualifier access_qualifier;
342 };
343
344 /* Members for function types */
345 struct {
346 /* For functions, the vtn_type for each parameter */
347 struct vtn_type **params;
348
349 /* Return type for functions */
350 struct vtn_type *return_type;
351 };
352 };
353 };
354
355 struct vtn_variable;
356
357 enum vtn_access_mode {
358 vtn_access_mode_id,
359 vtn_access_mode_literal,
360 };
361
362 struct vtn_access_link {
363 enum vtn_access_mode mode;
364 uint32_t id;
365 };
366
367 struct vtn_access_chain {
368 uint32_t length;
369
370 /** Whether or not to treat the base pointer as an array. This is only
371 * true if this access chain came from an OpPtrAccessChain.
372 */
373 bool ptr_as_array;
374
375 /** Struct elements and array offsets.
376 *
377 * This is an array of 1 so that it can conveniently be created on the
378 * stack but the real length is given by the length field.
379 */
380 struct vtn_access_link link[1];
381 };
382
383 enum vtn_variable_mode {
384 vtn_variable_mode_local,
385 vtn_variable_mode_global,
386 vtn_variable_mode_param,
387 vtn_variable_mode_ubo,
388 vtn_variable_mode_ssbo,
389 vtn_variable_mode_push_constant,
390 vtn_variable_mode_image,
391 vtn_variable_mode_sampler,
392 vtn_variable_mode_workgroup,
393 vtn_variable_mode_input,
394 vtn_variable_mode_output,
395 };
396
397 struct vtn_pointer {
398 /** The variable mode for the referenced data */
399 enum vtn_variable_mode mode;
400
401 /** The dereferenced type of this pointer */
402 struct vtn_type *type;
403
404 /** The pointer type of this pointer
405 *
406 * This may be NULL for some temporary pointers constructed as part of a
407 * large load, store, or copy. It MUST be valid for all pointers which are
408 * stored as SPIR-V SSA values.
409 */
410 struct vtn_type *ptr_type;
411
412 /** The referenced variable, if known
413 *
414 * This field may be NULL if the pointer uses a (block_index, offset) pair
415 * instead of an access chain.
416 */
417 struct vtn_variable *var;
418
419 /** An access chain describing how to get from var to the referenced data
420 *
421 * This field may be NULL if the pointer references the entire variable or
422 * if a (block_index, offset) pair is used instead of an access chain.
423 */
424 struct vtn_access_chain *chain;
425
426 /** A (block_index, offset) pair representing a UBO or SSBO position. */
427 struct nir_ssa_def *block_index;
428 struct nir_ssa_def *offset;
429 };
430
431 struct vtn_variable {
432 enum vtn_variable_mode mode;
433
434 struct vtn_type *type;
435
436 unsigned descriptor_set;
437 unsigned binding;
438 unsigned input_attachment_index;
439 bool patch;
440
441 nir_variable *var;
442 nir_variable **members;
443
444 /**
445 * In some early released versions of GLSLang, it implemented all function
446 * calls by making copies of all parameters into temporary variables and
447 * passing those variables into the function. It even did so for samplers
448 * and images which violates the SPIR-V spec. Unfortunately, two games
449 * (Talos Principle and Doom) shipped with this old version of GLSLang and
450 * also happen to pass samplers into functions. Talos Principle received
451 * an update fairly shortly after release with an updated GLSLang. Doom,
452 * on the other hand, has never received an update so we need to work
453 * around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
454 * hack at some point in the future.
455 */
456 struct vtn_pointer *copy_prop_sampler;
457 };
458
459 struct vtn_image_pointer {
460 struct vtn_pointer *image;
461 nir_ssa_def *coord;
462 nir_ssa_def *sample;
463 };
464
465 struct vtn_sampled_image {
466 struct vtn_type *type;
467 struct vtn_pointer *image; /* Image or array of images */
468 struct vtn_pointer *sampler; /* Sampler */
469 };
470
471 struct vtn_value {
472 enum vtn_value_type value_type;
473 const char *name;
474 struct vtn_decoration *decoration;
475 union {
476 void *ptr;
477 char *str;
478 struct vtn_type *type;
479 struct {
480 nir_constant *constant;
481 const struct glsl_type *const_type;
482 };
483 struct vtn_pointer *pointer;
484 struct vtn_image_pointer *image;
485 struct vtn_sampled_image *sampled_image;
486 struct vtn_function *func;
487 struct vtn_block *block;
488 struct vtn_ssa_value *ssa;
489 vtn_instruction_handler ext_handler;
490 };
491 };
492
493 #define VTN_DEC_DECORATION -1
494 #define VTN_DEC_EXECUTION_MODE -2
495 #define VTN_DEC_STRUCT_MEMBER0 0
496
497 struct vtn_decoration {
498 struct vtn_decoration *next;
499
500 /* Specifies how to apply this decoration. Negative values represent a
501 * decoration or execution mode. (See the VTN_DEC_ #defines above.)
502 * Non-negative values specify that it applies to a structure member.
503 */
504 int scope;
505
506 const uint32_t *literals;
507 struct vtn_value *group;
508
509 union {
510 SpvDecoration decoration;
511 SpvExecutionMode exec_mode;
512 };
513 };
514
515 struct vtn_builder {
516 nir_builder nb;
517
518 /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
519 jmp_buf fail_jump;
520
521 const uint32_t *spirv;
522
523 nir_shader *shader;
524 const struct spirv_to_nir_options *options;
525 struct vtn_block *block;
526
527 /* Current offset, file, line, and column. Useful for debugging. Set
528 * automatically by vtn_foreach_instruction.
529 */
530 size_t spirv_offset;
531 char *file;
532 int line, col;
533
534 /*
535 * In SPIR-V, constants are global, whereas in NIR, the load_const
536 * instruction we use is per-function. So while we parse each function, we
537 * keep a hash table of constants we've resolved to nir_ssa_value's so
538 * far, and we lazily resolve them when we see them used in a function.
539 */
540 struct hash_table *const_table;
541
542 /*
543 * Map from phi instructions (pointer to the start of the instruction)
544 * to the variable corresponding to it.
545 */
546 struct hash_table *phi_table;
547
548 unsigned num_specializations;
549 struct nir_spirv_specialization *specializations;
550
551 unsigned value_id_bound;
552 struct vtn_value *values;
553
554 gl_shader_stage entry_point_stage;
555 const char *entry_point_name;
556 struct vtn_value *entry_point;
557 bool origin_upper_left;
558 bool pixel_center_integer;
559
560 struct vtn_function *func;
561 struct exec_list functions;
562
563 /* Current function parameter index */
564 unsigned func_param_idx;
565
566 bool has_loop_continue;
567 };
568
569 nir_ssa_def *
570 vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
571 struct vtn_pointer *
572 vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
573 struct vtn_type *ptr_type);
574
575 static inline struct vtn_value *
576 vtn_push_value(struct vtn_builder *b, uint32_t value_id,
577 enum vtn_value_type value_type)
578 {
579 assert(value_id < b->value_id_bound);
580 assert(b->values[value_id].value_type == vtn_value_type_invalid);
581
582 b->values[value_id].value_type = value_type;
583
584 return &b->values[value_id];
585 }
586
587 static inline struct vtn_value *
588 vtn_push_ssa(struct vtn_builder *b, uint32_t value_id,
589 struct vtn_type *type, struct vtn_ssa_value *ssa)
590 {
591 struct vtn_value *val;
592 if (type->base_type == vtn_base_type_pointer) {
593 val = vtn_push_value(b, value_id, vtn_value_type_pointer);
594 val->pointer = vtn_pointer_from_ssa(b, ssa->def, type);
595 } else {
596 val = vtn_push_value(b, value_id, vtn_value_type_ssa);
597 val->ssa = ssa;
598 }
599 return val;
600 }
601
602 static inline struct vtn_value *
603 vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
604 {
605 assert(value_id < b->value_id_bound);
606 return &b->values[value_id];
607 }
608
609 static inline struct vtn_value *
610 vtn_value(struct vtn_builder *b, uint32_t value_id,
611 enum vtn_value_type value_type)
612 {
613 struct vtn_value *val = vtn_untyped_value(b, value_id);
614 assert(val->value_type == value_type);
615 return val;
616 }
617
618 struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
619
620 struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
621 const struct glsl_type *type);
622
623 struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
624 struct vtn_ssa_value *src);
625
626 nir_ssa_def *vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src,
627 unsigned index);
628 nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
629 nir_ssa_def *index);
630 nir_ssa_def *vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src,
631 nir_ssa_def *insert, unsigned index);
632 nir_ssa_def *vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
633 nir_ssa_def *insert, nir_ssa_def *index);
634
635 nir_deref_var *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
636
637 struct vtn_pointer *vtn_pointer_for_variable(struct vtn_builder *b,
638 struct vtn_variable *var,
639 struct vtn_type *ptr_type);
640
641 nir_deref_var *vtn_pointer_to_deref(struct vtn_builder *b,
642 struct vtn_pointer *ptr);
643 nir_ssa_def *
644 vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
645 nir_ssa_def **index_out, unsigned *end_idx_out);
646
647 struct vtn_ssa_value *vtn_local_load(struct vtn_builder *b, nir_deref_var *src);
648
649 void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
650 nir_deref_var *dest);
651
652 struct vtn_ssa_value *
653 vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src);
654
655 void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
656 struct vtn_pointer *dest);
657
658 void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
659 const uint32_t *w, unsigned count);
660
661
662 typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
663 struct vtn_value *,
664 int member,
665 const struct vtn_decoration *,
666 void *);
667
668 void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
669 vtn_decoration_foreach_cb cb, void *data);
670
671 typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
672 struct vtn_value *,
673 const struct vtn_decoration *,
674 void *);
675
676 void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
677 vtn_execution_mode_foreach_cb cb, void *data);
678
679 nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
680 SpvOp opcode, bool *swap,
681 nir_alu_type src, nir_alu_type dst);
682
683 void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
684 const uint32_t *w, unsigned count);
685
686 bool vtn_handle_glsl450_instruction(struct vtn_builder *b, uint32_t ext_opcode,
687 const uint32_t *words, unsigned count);
688
689 static inline uint64_t
690 vtn_u64_literal(const uint32_t *w)
691 {
692 return (uint64_t)w[1] << 32 | w[0];
693 }
694
695 #endif /* _VTN_PRIVATE_H_ */