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