~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/compiler/spirv/vtn_private.h

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "vtn_generator_ids.h"
39
 
 
40
 
struct vtn_builder;
41
 
struct vtn_decoration;
42
 
 
43
 
/* setjmp/longjmp is broken on MinGW: https://sourceforge.net/p/mingw-w64/bugs/406/ */
44
 
#if defined(__MINGW32__) && !defined(_UCRT)
45
 
  #define vtn_setjmp __builtin_setjmp
46
 
  #define vtn_longjmp __builtin_longjmp
47
 
#else
48
 
  #define vtn_setjmp setjmp
49
 
  #define vtn_longjmp longjmp
50
 
#endif
51
 
 
52
 
void vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
53
 
             size_t spirv_offset, const char *message);
54
 
 
55
 
void vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
56
 
              size_t spirv_offset, const char *fmt, ...) PRINTFLIKE(4, 5);
57
 
 
58
 
#define vtn_info(...) vtn_logf(b, NIR_SPIRV_DEBUG_LEVEL_INFO, 0, __VA_ARGS__)
59
 
 
60
 
void _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
61
 
               const char *fmt, ...) PRINTFLIKE(4, 5);
62
 
#define vtn_warn(...) _vtn_warn(b, __FILE__, __LINE__, __VA_ARGS__)
63
 
 
64
 
void _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
65
 
               const char *fmt, ...) PRINTFLIKE(4, 5);
66
 
#define vtn_err(...) _vtn_err(b, __FILE__, __LINE__, __VA_ARGS__)
67
 
 
68
 
/** Fail SPIR-V parsing
69
 
 *
70
 
 * This function logs an error and then bails out of the shader compile using
71
 
 * longjmp.  This being safe relies on two things:
72
 
 *
73
 
 *  1) We must guarantee that setjmp is called after allocating the builder
74
 
 *     and setting up b->debug (so that logging works) but before before any
75
 
 *     errors have a chance to occur.
76
 
 *
77
 
 *  2) While doing the SPIR-V -> NIR conversion, we need to be careful to
78
 
 *     ensure that all heap allocations happen through ralloc and are parented
79
 
 *     to the builder.  This way they will get properly cleaned up on error.
80
 
 *
81
 
 *  3) We must ensure that _vtn_fail is never called while a mutex lock or a
82
 
 *     reference to any other resource is held with the exception of ralloc
83
 
 *     objects which are parented to the builder.
84
 
 *
85
 
 * So long as these two things continue to hold, we can easily longjmp back to
86
 
 * spirv_to_nir(), clean up the builder, and return NULL.
87
 
 */
88
 
NORETURN void
89
 
_vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
90
 
             const char *fmt, ...) PRINTFLIKE(4, 5);
91
 
 
92
 
#define vtn_fail(...) _vtn_fail(b, __FILE__, __LINE__, __VA_ARGS__)
93
 
 
94
 
/** Fail if the given expression evaluates to true */
95
 
#define vtn_fail_if(expr, ...) \
96
 
   do { \
97
 
      if (unlikely(expr)) \
98
 
         vtn_fail(__VA_ARGS__); \
99
 
   } while (0)
100
 
 
101
 
#define _vtn_fail_with(t, msg, v) \
102
 
   vtn_fail("%s: %s (%u)\n", msg, spirv_ ## t ## _to_string(v), v)
103
 
 
104
 
#define vtn_fail_with_decoration(msg, v) _vtn_fail_with(decoration, msg, v)
105
 
#define vtn_fail_with_opcode(msg, v)     _vtn_fail_with(op, msg, v)
106
 
 
107
 
/** Assert that a condition is true and, if it isn't, vtn_fail
108
 
 *
109
 
 * This macro is transitional only and should not be used in new code.  Use
110
 
 * vtn_fail_if and provide a real message instead.
111
 
 */
112
 
#define vtn_assert(expr) \
113
 
   do { \
114
 
      if (!likely(expr)) \
115
 
         vtn_fail("%s", #expr); \
116
 
   } while (0)
117
 
 
118
 
enum vtn_value_type {
119
 
   vtn_value_type_invalid = 0,
120
 
   vtn_value_type_undef,
121
 
   vtn_value_type_string,
122
 
   vtn_value_type_decoration_group,
123
 
   vtn_value_type_type,
124
 
   vtn_value_type_constant,
125
 
   vtn_value_type_pointer,
126
 
   vtn_value_type_function,
127
 
   vtn_value_type_block,
128
 
   vtn_value_type_ssa,
129
 
   vtn_value_type_extension,
130
 
   vtn_value_type_image_pointer,
131
 
};
132
 
 
133
 
enum vtn_branch_type {
134
 
   vtn_branch_type_none,
135
 
   vtn_branch_type_if_merge,
136
 
   vtn_branch_type_switch_break,
137
 
   vtn_branch_type_switch_fallthrough,
138
 
   vtn_branch_type_loop_break,
139
 
   vtn_branch_type_loop_continue,
140
 
   vtn_branch_type_loop_back_edge,
141
 
   vtn_branch_type_discard,
142
 
   vtn_branch_type_terminate_invocation,
143
 
   vtn_branch_type_ignore_intersection,
144
 
   vtn_branch_type_terminate_ray,
145
 
   vtn_branch_type_return,
146
 
};
147
 
 
148
 
enum vtn_cf_node_type {
149
 
   vtn_cf_node_type_block,
150
 
   vtn_cf_node_type_if,
151
 
   vtn_cf_node_type_loop,
152
 
   vtn_cf_node_type_case,
153
 
   vtn_cf_node_type_switch,
154
 
   vtn_cf_node_type_function,
155
 
};
156
 
 
157
 
struct vtn_cf_node {
158
 
   struct list_head link;
159
 
   struct vtn_cf_node *parent;
160
 
   enum vtn_cf_node_type type;
161
 
};
162
 
 
163
 
struct vtn_loop {
164
 
   struct vtn_cf_node node;
165
 
 
166
 
   /* The main body of the loop */
167
 
   struct list_head body;
168
 
 
169
 
   /* The "continue" part of the loop.  This gets executed after the body
170
 
    * and is where you go when you hit a continue.
171
 
    */
172
 
   struct list_head cont_body;
173
 
 
174
 
   struct vtn_block *header_block;
175
 
   struct vtn_block *cont_block;
176
 
   struct vtn_block *break_block;
177
 
 
178
 
   SpvLoopControlMask control;
179
 
};
180
 
 
181
 
struct vtn_if {
182
 
   struct vtn_cf_node node;
183
 
 
184
 
   enum vtn_branch_type then_type;
185
 
   struct list_head then_body;
186
 
 
187
 
   enum vtn_branch_type else_type;
188
 
   struct list_head else_body;
189
 
 
190
 
   struct vtn_block *header_block;
191
 
   struct vtn_block *merge_block;
192
 
 
193
 
   SpvSelectionControlMask control;
194
 
};
195
 
 
196
 
struct vtn_case {
197
 
   struct vtn_cf_node node;
198
 
 
199
 
   struct vtn_block *block;
200
 
 
201
 
   enum vtn_branch_type type;
202
 
   struct list_head body;
203
 
 
204
 
   /* The fallthrough case, if any */
205
 
   struct vtn_case *fallthrough;
206
 
 
207
 
   /* The uint32_t values that map to this case */
208
 
   struct util_dynarray values;
209
 
 
210
 
   /* True if this is the default case */
211
 
   bool is_default;
212
 
 
213
 
   /* Initialized to false; used when sorting the list of cases */
214
 
   bool visited;
215
 
};
216
 
 
217
 
struct vtn_switch {
218
 
   struct vtn_cf_node node;
219
 
 
220
 
   uint32_t selector;
221
 
 
222
 
   struct list_head cases;
223
 
 
224
 
   struct vtn_block *break_block;
225
 
};
226
 
 
227
 
struct vtn_block {
228
 
   struct vtn_cf_node node;
229
 
 
230
 
   /** A pointer to the label instruction */
231
 
   const uint32_t *label;
232
 
 
233
 
   /** A pointer to the merge instruction (or NULL if non exists) */
234
 
   const uint32_t *merge;
235
 
 
236
 
   /** A pointer to the branch instruction that ends this block */
237
 
   const uint32_t *branch;
238
 
 
239
 
   enum vtn_branch_type branch_type;
240
 
 
241
 
   /* The CF node for which this is a merge target
242
 
    *
243
 
    * The SPIR-V spec requires that any given block can be the merge target
244
 
    * for at most one merge instruction.  If this block is a merge target,
245
 
    * this points back to the block containing that merge instruction.
246
 
    */
247
 
   struct vtn_cf_node *merge_cf_node;
248
 
 
249
 
   /** Points to the loop that this block starts (if it starts a loop) */
250
 
   struct vtn_loop *loop;
251
 
 
252
 
   /** Points to the switch case started by this block (if any) */
253
 
   struct vtn_case *switch_case;
254
 
 
255
 
   /** Every block ends in a nop intrinsic so that we can find it again */
256
 
   nir_intrinsic_instr *end_nop;
257
 
 
258
 
   /** attached nir_block */
259
 
   struct nir_block *block;
260
 
};
261
 
 
262
 
struct vtn_function {
263
 
   struct vtn_cf_node node;
264
 
 
265
 
   struct vtn_type *type;
266
 
 
267
 
   bool referenced;
268
 
   bool emitted;
269
 
 
270
 
   nir_function *nir_func;
271
 
   struct vtn_block *start_block;
272
 
 
273
 
   struct list_head body;
274
 
 
275
 
   const uint32_t *end;
276
 
 
277
 
   SpvLinkageType linkage;
278
 
   SpvFunctionControlMask control;
279
 
};
280
 
 
281
 
#define VTN_DECL_CF_NODE_CAST(_type)               \
282
 
static inline struct vtn_##_type *                 \
283
 
vtn_cf_node_as_##_type(struct vtn_cf_node *node)   \
284
 
{                                                  \
285
 
   assert(node->type == vtn_cf_node_type_##_type); \
286
 
   return (struct vtn_##_type *)node;              \
287
 
}
288
 
 
289
 
VTN_DECL_CF_NODE_CAST(block)
290
 
VTN_DECL_CF_NODE_CAST(loop)
291
 
VTN_DECL_CF_NODE_CAST(if)
292
 
VTN_DECL_CF_NODE_CAST(case)
293
 
VTN_DECL_CF_NODE_CAST(switch)
294
 
VTN_DECL_CF_NODE_CAST(function)
295
 
 
296
 
#define vtn_foreach_cf_node(node, cf_list) \
297
 
   list_for_each_entry(struct vtn_cf_node, node, cf_list, link)
298
 
 
299
 
typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
300
 
                                        const uint32_t *, unsigned);
301
 
 
302
 
void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
303
 
                   const uint32_t *end);
304
 
void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
305
 
                       vtn_instruction_handler instruction_handler);
306
 
void vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
307
 
                              const uint32_t *w, unsigned count);
308
 
 
309
 
const uint32_t *
310
 
vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
311
 
                        const uint32_t *end, vtn_instruction_handler handler);
312
 
 
313
 
struct vtn_ssa_value {
314
 
   union {
315
 
      nir_ssa_def *def;
316
 
      struct vtn_ssa_value **elems;
317
 
   };
318
 
 
319
 
   /* For matrices, if this is non-NULL, then this value is actually the
320
 
    * transpose of some other value.  The value that `transposed` points to
321
 
    * always dominates this value.
322
 
    */
323
 
   struct vtn_ssa_value *transposed;
324
 
 
325
 
   const struct glsl_type *type;
326
 
};
327
 
 
328
 
enum vtn_base_type {
329
 
   vtn_base_type_void,
330
 
   vtn_base_type_scalar,
331
 
   vtn_base_type_vector,
332
 
   vtn_base_type_matrix,
333
 
   vtn_base_type_array,
334
 
   vtn_base_type_struct,
335
 
   vtn_base_type_pointer,
336
 
   vtn_base_type_image,
337
 
   vtn_base_type_sampler,
338
 
   vtn_base_type_sampled_image,
339
 
   vtn_base_type_accel_struct,
340
 
   vtn_base_type_ray_query,
341
 
   vtn_base_type_function,
342
 
   vtn_base_type_event,
343
 
};
344
 
 
345
 
struct vtn_type {
346
 
   enum vtn_base_type base_type;
347
 
 
348
 
   const struct glsl_type *type;
349
 
 
350
 
   /* The SPIR-V id of the given type. */
351
 
   uint32_t id;
352
 
 
353
 
   /* Specifies the length of complex types.
354
 
    *
355
 
    * For Workgroup pointers, this is the size of the referenced type.
356
 
    */
357
 
   unsigned length;
358
 
 
359
 
   /* for arrays, matrices and pointers, the array stride */
360
 
   unsigned stride;
361
 
 
362
 
   /* Access qualifiers */
363
 
   enum gl_access_qualifier access;
364
 
 
365
 
   union {
366
 
      /* Members for scalar, vector, and array-like types */
367
 
      struct {
368
 
         /* for arrays, the vtn_type for the elements of the array */
369
 
         struct vtn_type *array_element;
370
 
 
371
 
         /* for matrices, whether the matrix is stored row-major */
372
 
         bool row_major:1;
373
 
 
374
 
         /* Whether this type, or a parent type, has been decorated as a
375
 
          * builtin
376
 
          */
377
 
         bool is_builtin:1;
378
 
 
379
 
         /* Which built-in to use */
380
 
         SpvBuiltIn builtin;
381
 
      };
382
 
 
383
 
      /* Members for struct types */
384
 
      struct {
385
 
         /* for structures, the vtn_type for each member */
386
 
         struct vtn_type **members;
387
 
 
388
 
         /* for structs, the offset of each member */
389
 
         unsigned *offsets;
390
 
 
391
 
         /* for structs, whether it was decorated as a "non-SSBO-like" block */
392
 
         bool block:1;
393
 
 
394
 
         /* for structs, whether it was decorated as an "SSBO-like" block */
395
 
         bool buffer_block:1;
396
 
 
397
 
         /* for structs with block == true, whether this is a builtin block
398
 
          * (i.e. a block that contains only builtins).
399
 
          */
400
 
         bool builtin_block:1;
401
 
 
402
 
         /* for structs and unions it specifies the minimum alignment of the
403
 
          * members. 0 means packed.
404
 
          *
405
 
          * Set by CPacked and Alignment Decorations in kernels.
406
 
          */
407
 
         bool packed:1;
408
 
      };
409
 
 
410
 
      /* Members for pointer types */
411
 
      struct {
412
 
         /* For pointers, the vtn_type for dereferenced type */
413
 
         struct vtn_type *deref;
414
 
 
415
 
         /* Storage class for pointers */
416
 
         SpvStorageClass storage_class;
417
 
 
418
 
         /* Required alignment for pointers */
419
 
         uint32_t align;
420
 
      };
421
 
 
422
 
      /* Members for image types */
423
 
      struct {
424
 
         /* GLSL image type for this type.  This is not to be confused with
425
 
          * vtn_type::type which is actually going to be the GLSL type for a
426
 
          * pointer to an image, likely a uint32_t.
427
 
          */
428
 
         const struct glsl_type *glsl_image;
429
 
 
430
 
         /* Image format for image_load_store type images */
431
 
         unsigned image_format;
432
 
 
433
 
         /* Access qualifier for storage images */
434
 
         SpvAccessQualifier access_qualifier;
435
 
      };
436
 
 
437
 
      /* Members for sampled image types */
438
 
      struct {
439
 
         /* For sampled images, the image type */
440
 
         struct vtn_type *image;
441
 
      };
442
 
 
443
 
      /* Members for function types */
444
 
      struct {
445
 
         /* For functions, the vtn_type for each parameter */
446
 
         struct vtn_type **params;
447
 
 
448
 
         /* Return type for functions */
449
 
         struct vtn_type *return_type;
450
 
      };
451
 
   };
452
 
};
453
 
 
454
 
bool vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type);
455
 
 
456
 
bool vtn_types_compatible(struct vtn_builder *b,
457
 
                          struct vtn_type *t1, struct vtn_type *t2);
458
 
 
459
 
struct vtn_type *vtn_type_without_array(struct vtn_type *type);
460
 
 
461
 
struct vtn_variable;
462
 
 
463
 
enum vtn_access_mode {
464
 
   vtn_access_mode_id,
465
 
   vtn_access_mode_literal,
466
 
};
467
 
 
468
 
struct vtn_access_link {
469
 
   enum vtn_access_mode mode;
470
 
   int64_t id;
471
 
};
472
 
 
473
 
struct vtn_access_chain {
474
 
   uint32_t length;
475
 
 
476
 
   /** Whether or not to treat the base pointer as an array.  This is only
477
 
    * true if this access chain came from an OpPtrAccessChain.
478
 
    */
479
 
   bool ptr_as_array;
480
 
 
481
 
   /* Access qualifiers */
482
 
   enum gl_access_qualifier access;
483
 
 
484
 
   /** Struct elements and array offsets.
485
 
    *
486
 
    * This is an array of 1 so that it can conveniently be created on the
487
 
    * stack but the real length is given by the length field.
488
 
    */
489
 
   struct vtn_access_link link[1];
490
 
};
491
 
 
492
 
enum vtn_variable_mode {
493
 
   vtn_variable_mode_function,
494
 
   vtn_variable_mode_private,
495
 
   vtn_variable_mode_uniform,
496
 
   vtn_variable_mode_atomic_counter,
497
 
   vtn_variable_mode_ubo,
498
 
   vtn_variable_mode_ssbo,
499
 
   vtn_variable_mode_phys_ssbo,
500
 
   vtn_variable_mode_push_constant,
501
 
   vtn_variable_mode_workgroup,
502
 
   vtn_variable_mode_cross_workgroup,
503
 
   vtn_variable_mode_task_payload,
504
 
   vtn_variable_mode_generic,
505
 
   vtn_variable_mode_constant,
506
 
   vtn_variable_mode_input,
507
 
   vtn_variable_mode_output,
508
 
   vtn_variable_mode_image,
509
 
   vtn_variable_mode_accel_struct,
510
 
   vtn_variable_mode_call_data,
511
 
   vtn_variable_mode_call_data_in,
512
 
   vtn_variable_mode_ray_payload,
513
 
   vtn_variable_mode_ray_payload_in,
514
 
   vtn_variable_mode_hit_attrib,
515
 
   vtn_variable_mode_shader_record,
516
 
};
517
 
 
518
 
struct vtn_pointer {
519
 
   /** The variable mode for the referenced data */
520
 
   enum vtn_variable_mode mode;
521
 
 
522
 
   /** The dereferenced type of this pointer */
523
 
   struct vtn_type *type;
524
 
 
525
 
   /** The pointer type of this pointer
526
 
    *
527
 
    * This may be NULL for some temporary pointers constructed as part of a
528
 
    * large load, store, or copy.  It MUST be valid for all pointers which are
529
 
    * stored as SPIR-V SSA values.
530
 
    */
531
 
   struct vtn_type *ptr_type;
532
 
 
533
 
   /** The referenced variable, if known
534
 
    *
535
 
    * This field may be NULL if the pointer uses a (block_index, offset) pair
536
 
    * instead of an access chain or if the access chain starts at a deref.
537
 
    */
538
 
   struct vtn_variable *var;
539
 
 
540
 
   /** The NIR deref corresponding to this pointer */
541
 
   nir_deref_instr *deref;
542
 
 
543
 
   /** A (block_index, offset) pair representing a UBO or SSBO position. */
544
 
   struct nir_ssa_def *block_index;
545
 
   struct nir_ssa_def *offset;
546
 
 
547
 
   /* Access qualifiers */
548
 
   enum gl_access_qualifier access;
549
 
};
550
 
 
551
 
struct vtn_variable {
552
 
   enum vtn_variable_mode mode;
553
 
 
554
 
   struct vtn_type *type;
555
 
 
556
 
   unsigned descriptor_set;
557
 
   unsigned binding;
558
 
   bool explicit_binding;
559
 
   unsigned offset;
560
 
   unsigned input_attachment_index;
561
 
 
562
 
   nir_variable *var;
563
 
 
564
 
   /* If the variable is a struct with a location set on it then this will be
565
 
    * stored here. This will be used to calculate locations for members that
566
 
    * don’t have their own explicit location.
567
 
    */
568
 
   int base_location;
569
 
 
570
 
   /**
571
 
    * In some early released versions of GLSLang, it implemented all function
572
 
    * calls by making copies of all parameters into temporary variables and
573
 
    * passing those variables into the function.  It even did so for samplers
574
 
    * and images which violates the SPIR-V spec.  Unfortunately, two games
575
 
    * (Talos Principle and Doom) shipped with this old version of GLSLang and
576
 
    * also happen to pass samplers into functions.  Talos Principle received
577
 
    * an update fairly shortly after release with an updated GLSLang.  Doom,
578
 
    * on the other hand, has never received an update so we need to work
579
 
    * around this GLSLang issue in SPIR-V -> NIR.  Hopefully, we can drop this
580
 
    * hack at some point in the future.
581
 
    */
582
 
   struct vtn_pointer *copy_prop_sampler;
583
 
 
584
 
   /* Access qualifiers. */
585
 
   enum gl_access_qualifier access;
586
 
};
587
 
 
588
 
const struct glsl_type *
589
 
vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type,
590
 
                      enum vtn_variable_mode mode);
591
 
 
592
 
struct vtn_image_pointer {
593
 
   nir_deref_instr *image;
594
 
   nir_ssa_def *coord;
595
 
   nir_ssa_def *sample;
596
 
   nir_ssa_def *lod;
597
 
};
598
 
 
599
 
struct vtn_value {
600
 
   enum vtn_value_type value_type;
601
 
 
602
 
   /* Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/3406
603
 
    * Only set for OpImage / OpSampledImage. Note that this is in addition
604
 
    * the existence of a NonUniform decoration on this value.*/
605
 
   uint32_t propagated_non_uniform : 1;
606
 
 
607
 
   /* Valid for vtn_value_type_constant to indicate the value is OpConstantNull. */
608
 
   bool is_null_constant:1;
609
 
 
610
 
   /* Valid when all the members of the value are undef. */
611
 
   bool is_undef_constant:1;
612
 
 
613
 
   const char *name;
614
 
   struct vtn_decoration *decoration;
615
 
   struct vtn_type *type;
616
 
   union {
617
 
      const char *str;
618
 
      nir_constant *constant;
619
 
      struct vtn_pointer *pointer;
620
 
      struct vtn_image_pointer *image;
621
 
      struct vtn_function *func;
622
 
      struct vtn_block *block;
623
 
      struct vtn_ssa_value *ssa;
624
 
      vtn_instruction_handler ext_handler;
625
 
   };
626
 
};
627
 
 
628
 
#define VTN_DEC_DECORATION -1
629
 
#define VTN_DEC_EXECUTION_MODE -2
630
 
#define VTN_DEC_STRUCT_MEMBER_NAME0 -3
631
 
#define VTN_DEC_STRUCT_MEMBER0 0
632
 
 
633
 
struct vtn_decoration {
634
 
   struct vtn_decoration *next;
635
 
 
636
 
   /* Different kinds of decorations are stored in a value,
637
 
      the scope defines what decoration it refers to:
638
 
 
639
 
      - VTN_DEC_DECORATION:
640
 
            decoration associated with the value
641
 
      - VTN_DEC_EXECUTION_MODE:
642
 
            an execution mode associated with an entrypoint value
643
 
      - VTN_DEC_STRUCT_MEMBER0 + m:
644
 
            decoration associated with member m of a struct value
645
 
      - VTN_DEC_STRUCT_MEMBER_NAME0 - m:
646
 
            name of m'th member of a struct value
647
 
    */
648
 
   int scope;
649
 
 
650
 
   uint32_t num_operands;
651
 
   const uint32_t *operands;
652
 
   struct vtn_value *group;
653
 
 
654
 
   union {
655
 
      SpvDecoration decoration;
656
 
      SpvExecutionMode exec_mode;
657
 
      const char *member_name;
658
 
   };
659
 
};
660
 
 
661
 
struct vtn_builder {
662
 
   nir_builder nb;
663
 
 
664
 
   /* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
665
 
   jmp_buf fail_jump;
666
 
 
667
 
   const uint32_t *spirv;
668
 
   size_t spirv_word_count;
669
 
   uint32_t version;
670
 
 
671
 
   nir_shader *shader;
672
 
   struct spirv_to_nir_options *options;
673
 
   struct vtn_block *block;
674
 
 
675
 
   /* Current offset, file, line, and column.  Useful for debugging.  Set
676
 
    * automatically by vtn_foreach_instruction.
677
 
    */
678
 
   size_t spirv_offset;
679
 
   const char *file;
680
 
   int line, col;
681
 
 
682
 
   /*
683
 
    * In SPIR-V, constants are global, whereas in NIR, the load_const
684
 
    * instruction we use is per-function. So while we parse each function, we
685
 
    * keep a hash table of constants we've resolved to nir_ssa_value's so
686
 
    * far, and we lazily resolve them when we see them used in a function.
687
 
    */
688
 
   struct hash_table *const_table;
689
 
 
690
 
   /*
691
 
    * Map from phi instructions (pointer to the start of the instruction)
692
 
    * to the variable corresponding to it.
693
 
    */
694
 
   struct hash_table *phi_table;
695
 
 
696
 
   /* In Vulkan, when lowering some modes variable access, the derefs of the
697
 
    * variables are replaced with a resource index intrinsics, leaving the
698
 
    * variable hanging.  This set keeps track of them so they can be filtered
699
 
    * (and not removed) in nir_remove_dead_variables.
700
 
    */
701
 
   struct set *vars_used_indirectly;
702
 
 
703
 
   unsigned num_specializations;
704
 
   struct nir_spirv_specialization *specializations;
705
 
 
706
 
   unsigned value_id_bound;
707
 
   struct vtn_value *values;
708
 
 
709
 
   /* Information on the origin of the SPIR-V */
710
 
   enum vtn_generator generator_id;
711
 
   SpvSourceLanguage source_lang;
712
 
 
713
 
   /* True if we need to fix up CS OpControlBarrier */
714
 
   bool wa_glslang_cs_barrier;
715
 
 
716
 
   /* True if we need to ignore undef initializers */
717
 
   bool wa_llvm_spirv_ignore_workgroup_initializer;
718
 
 
719
 
   /* Workaround discard bugs in HLSL -> SPIR-V compilers */
720
 
   bool uses_demote_to_helper_invocation;
721
 
   bool convert_discard_to_demote;
722
 
 
723
 
   gl_shader_stage entry_point_stage;
724
 
   const char *entry_point_name;
725
 
   struct vtn_value *entry_point;
726
 
   struct vtn_value *workgroup_size_builtin;
727
 
   bool variable_pointers;
728
 
 
729
 
   uint32_t *interface_ids;
730
 
   size_t interface_ids_count;
731
 
 
732
 
   struct vtn_function *func;
733
 
   struct list_head functions;
734
 
 
735
 
   /* Current function parameter index */
736
 
   unsigned func_param_idx;
737
 
 
738
 
   /* false by default, set to true by the ContractionOff execution mode */
739
 
   bool exact;
740
 
 
741
 
   /* when a physical memory model is choosen */
742
 
   bool physical_ptrs;
743
 
 
744
 
   /* memory model specified by OpMemoryModel */
745
 
   unsigned mem_model;
746
 
};
747
 
 
748
 
const char *
749
 
vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
750
 
                   unsigned word_count, unsigned *words_used);
751
 
 
752
 
nir_ssa_def *
753
 
vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
754
 
struct vtn_pointer *
755
 
vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
756
 
                     struct vtn_type *ptr_type);
757
 
 
758
 
struct vtn_ssa_value *
759
 
vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
760
 
                    const struct glsl_type *type);
761
 
 
762
 
static inline struct vtn_value *
763
 
vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
764
 
{
765
 
   vtn_fail_if(value_id >= b->value_id_bound,
766
 
               "SPIR-V id %u is out-of-bounds", value_id);
767
 
   return &b->values[value_id];
768
 
}
769
 
 
770
 
static inline uint32_t
771
 
vtn_id_for_value(struct vtn_builder *b, struct vtn_value *value)
772
 
{
773
 
   vtn_fail_if(value <= b->values, "vtn_value pointer outside the range of valid values");
774
 
   uint32_t value_id = value - b->values;
775
 
   vtn_fail_if(value_id >= b->value_id_bound, "vtn_value pointer outside the range of valid values");
776
 
   return value_id;
777
 
}
778
 
 
779
 
/* Consider not using this function directly and instead use
780
 
 * vtn_push_ssa/vtn_push_pointer so that appropriate applying of
781
 
 * decorations is handled by common code.
782
 
 */
783
 
static inline struct vtn_value *
784
 
vtn_push_value(struct vtn_builder *b, uint32_t value_id,
785
 
               enum vtn_value_type value_type)
786
 
{
787
 
   struct vtn_value *val = vtn_untyped_value(b, value_id);
788
 
 
789
 
   vtn_fail_if(value_type == vtn_value_type_ssa,
790
 
               "Do not call vtn_push_value for value_type_ssa.  Use "
791
 
               "vtn_push_ssa_value instead.");
792
 
 
793
 
   vtn_fail_if(val->value_type != vtn_value_type_invalid,
794
 
               "SPIR-V id %u has already been written by another instruction",
795
 
               value_id);
796
 
 
797
 
   val->value_type = value_type;
798
 
 
799
 
   return &b->values[value_id];
800
 
}
801
 
 
802
 
static inline struct vtn_value *
803
 
vtn_value(struct vtn_builder *b, uint32_t value_id,
804
 
          enum vtn_value_type value_type)
805
 
{
806
 
   struct vtn_value *val = vtn_untyped_value(b, value_id);
807
 
   vtn_fail_if(val->value_type != value_type,
808
 
               "SPIR-V id %u is the wrong kind of value", value_id);
809
 
   return val;
810
 
}
811
 
 
812
 
static inline struct vtn_value *
813
 
vtn_pointer_value(struct vtn_builder *b, uint32_t value_id)
814
 
{
815
 
   struct vtn_value *val = vtn_untyped_value(b, value_id);
816
 
   vtn_fail_if(val->value_type != vtn_value_type_pointer &&
817
 
               !val->is_null_constant,
818
 
               "SPIR-V id %u is the wrong kind of value", value_id);
819
 
   return val;
820
 
}
821
 
 
822
 
static inline struct vtn_pointer *
823
 
vtn_value_to_pointer(struct vtn_builder *b, struct vtn_value *value)
824
 
{
825
 
   if (value->is_null_constant) {
826
 
      vtn_assert(glsl_type_is_vector_or_scalar(value->type->type));
827
 
      nir_ssa_def *const_ssa =
828
 
         vtn_const_ssa_value(b, value->constant, value->type->type)->def;
829
 
      return vtn_pointer_from_ssa(b, const_ssa, value->type);
830
 
   }
831
 
   vtn_assert(value->value_type == vtn_value_type_pointer);
832
 
   return value->pointer;
833
 
}
834
 
 
835
 
static inline struct vtn_pointer *
836
 
vtn_pointer(struct vtn_builder *b, uint32_t value_id)
837
 
{
838
 
   return vtn_value_to_pointer(b, vtn_pointer_value(b, value_id));
839
 
}
840
 
 
841
 
bool
842
 
vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
843
 
                                const uint32_t *w, unsigned count);
844
 
 
845
 
static inline uint64_t
846
 
vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
847
 
{
848
 
   struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
849
 
 
850
 
   vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
851
 
               !glsl_type_is_integer(val->type->type),
852
 
               "Expected id %u to be an integer constant", value_id);
853
 
 
854
 
   switch (glsl_get_bit_size(val->type->type)) {
855
 
   case 8:  return val->constant->values[0].u8;
856
 
   case 16: return val->constant->values[0].u16;
857
 
   case 32: return val->constant->values[0].u32;
858
 
   case 64: return val->constant->values[0].u64;
859
 
   default: unreachable("Invalid bit size");
860
 
   }
861
 
}
862
 
 
863
 
static inline int64_t
864
 
vtn_constant_int(struct vtn_builder *b, uint32_t value_id)
865
 
{
866
 
   struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
867
 
 
868
 
   vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
869
 
               !glsl_type_is_integer(val->type->type),
870
 
               "Expected id %u to be an integer constant", value_id);
871
 
 
872
 
   switch (glsl_get_bit_size(val->type->type)) {
873
 
   case 8:  return val->constant->values[0].i8;
874
 
   case 16: return val->constant->values[0].i16;
875
 
   case 32: return val->constant->values[0].i32;
876
 
   case 64: return val->constant->values[0].i64;
877
 
   default: unreachable("Invalid bit size");
878
 
   }
879
 
}
880
 
 
881
 
static inline struct vtn_type *
882
 
vtn_get_value_type(struct vtn_builder *b, uint32_t value_id)
883
 
{
884
 
   struct vtn_value *val = vtn_untyped_value(b, value_id);
885
 
   vtn_fail_if(val->type == NULL, "Value %u does not have a type", value_id);
886
 
   return val->type;
887
 
}
888
 
 
889
 
static inline struct vtn_type *
890
 
vtn_get_type(struct vtn_builder *b, uint32_t value_id)
891
 
{
892
 
   return vtn_value(b, value_id, vtn_value_type_type)->type;
893
 
}
894
 
 
895
 
struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
896
 
struct vtn_value *vtn_push_ssa_value(struct vtn_builder *b, uint32_t value_id,
897
 
                                     struct vtn_ssa_value *ssa);
898
 
 
899
 
nir_ssa_def *vtn_get_nir_ssa(struct vtn_builder *b, uint32_t value_id);
900
 
struct vtn_value *vtn_push_nir_ssa(struct vtn_builder *b, uint32_t value_id,
901
 
                                   nir_ssa_def *def);
902
 
 
903
 
struct vtn_value *vtn_push_pointer(struct vtn_builder *b,
904
 
                                   uint32_t value_id,
905
 
                                   struct vtn_pointer *ptr);
906
 
 
907
 
struct vtn_sampled_image {
908
 
   nir_deref_instr *image;
909
 
   nir_deref_instr *sampler;
910
 
};
911
 
 
912
 
nir_ssa_def *vtn_sampled_image_to_nir_ssa(struct vtn_builder *b,
913
 
                                          struct vtn_sampled_image si);
914
 
 
915
 
void
916
 
vtn_copy_value(struct vtn_builder *b, uint32_t src_value_id,
917
 
               uint32_t dst_value_id);
918
 
 
919
 
struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
920
 
                                           const struct glsl_type *type);
921
 
 
922
 
struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
923
 
                                        struct vtn_ssa_value *src);
924
 
 
925
 
nir_deref_instr *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
926
 
 
927
 
nir_deref_instr *vtn_pointer_to_deref(struct vtn_builder *b,
928
 
                                      struct vtn_pointer *ptr);
929
 
nir_ssa_def *
930
 
vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
931
 
                      nir_ssa_def **index_out);
932
 
 
933
 
nir_deref_instr *
934
 
vtn_get_call_payload_for_location(struct vtn_builder *b, uint32_t location_id);
935
 
 
936
 
struct vtn_ssa_value *
937
 
vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
938
 
               enum gl_access_qualifier access);
939
 
 
940
 
void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
941
 
                     nir_deref_instr *dest,
942
 
                     enum gl_access_qualifier access);
943
 
 
944
 
struct vtn_ssa_value *
945
 
vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src,
946
 
                  enum gl_access_qualifier access);
947
 
 
948
 
void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
949
 
                        struct vtn_pointer *dest, enum gl_access_qualifier access);
950
 
 
951
 
void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
952
 
                          const uint32_t *w, unsigned count);
953
 
 
954
 
 
955
 
typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
956
 
                                          struct vtn_value *,
957
 
                                          int member,
958
 
                                          const struct vtn_decoration *,
959
 
                                          void *);
960
 
 
961
 
void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
962
 
                            vtn_decoration_foreach_cb cb, void *data);
963
 
 
964
 
typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
965
 
                                              struct vtn_value *,
966
 
                                              const struct vtn_decoration *,
967
 
                                              void *);
968
 
 
969
 
void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
970
 
                                vtn_execution_mode_foreach_cb cb, void *data);
971
 
 
972
 
nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
973
 
                                       SpvOp opcode, bool *swap, bool *exact,
974
 
                                       unsigned src_bit_size, unsigned dst_bit_size);
975
 
 
976
 
void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
977
 
                    const uint32_t *w, unsigned count);
978
 
 
979
 
void vtn_handle_integer_dot(struct vtn_builder *b, SpvOp opcode,
980
 
                            const uint32_t *w, unsigned count);
981
 
 
982
 
void vtn_handle_bitcast(struct vtn_builder *b, const uint32_t *w,
983
 
                        unsigned count);
984
 
 
985
 
void vtn_handle_no_contraction(struct vtn_builder *b, struct vtn_value *val);
986
 
 
987
 
void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
988
 
                         const uint32_t *w, unsigned count);
989
 
 
990
 
bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
991
 
                                    const uint32_t *words, unsigned count);
992
 
 
993
 
bool vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
994
 
                                   const uint32_t *words, unsigned count);
995
 
bool vtn_handle_opencl_core_instruction(struct vtn_builder *b, SpvOp opcode,
996
 
                                        const uint32_t *w, unsigned count);
997
 
 
998
 
struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
999
 
                                       gl_shader_stage stage, const char *entry_point_name,
1000
 
                                       const struct spirv_to_nir_options *options);
1001
 
 
1002
 
void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
1003
 
                            unsigned count);
1004
 
 
1005
 
void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
1006
 
                           const uint32_t *w, unsigned count);
1007
 
 
1008
 
enum vtn_variable_mode vtn_storage_class_to_mode(struct vtn_builder *b,
1009
 
                                                 SpvStorageClass class,
1010
 
                                                 struct vtn_type *interface_type,
1011
 
                                                 nir_variable_mode *nir_mode_out);
1012
 
 
1013
 
nir_address_format vtn_mode_to_address_format(struct vtn_builder *b,
1014
 
                                              enum vtn_variable_mode);
1015
 
 
1016
 
nir_rounding_mode vtn_rounding_mode_to_nir(struct vtn_builder *b,
1017
 
                                           SpvFPRoundingMode mode);
1018
 
 
1019
 
static inline uint32_t
1020
 
vtn_align_u32(uint32_t v, uint32_t a)
1021
 
{
1022
 
   assert(a != 0 && a == (a & -((int32_t) a)));
1023
 
   return (v + a - 1) & ~(a - 1);
1024
 
}
1025
 
 
1026
 
static inline uint64_t
1027
 
vtn_u64_literal(const uint32_t *w)
1028
 
{
1029
 
   return (uint64_t)w[1] << 32 | w[0];
1030
 
}
1031
 
 
1032
 
bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1033
 
                                           const uint32_t *words, unsigned count);
1034
 
 
1035
 
bool vtn_handle_amd_shader_ballot_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1036
 
                                              const uint32_t *w, unsigned count);
1037
 
 
1038
 
bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
1039
 
                                                      const uint32_t *words, unsigned count);
1040
 
 
1041
 
bool vtn_handle_amd_shader_explicit_vertex_parameter_instruction(struct vtn_builder *b,
1042
 
                                                                 SpvOp ext_opcode,
1043
 
                                                                 const uint32_t *words,
1044
 
                                                                 unsigned count);
1045
 
 
1046
 
SpvMemorySemanticsMask vtn_mode_to_memory_semantics(enum vtn_variable_mode mode);
1047
 
 
1048
 
void vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
1049
 
                             SpvMemorySemanticsMask semantics);
1050
 
 
1051
 
static inline int
1052
 
cmp_uint32_t(const void *pa, const void *pb)
1053
 
{
1054
 
   uint32_t a = *((const uint32_t *)pa);
1055
 
   uint32_t b = *((const uint32_t *)pb);
1056
 
   if (a < b)
1057
 
      return -1;
1058
 
   if (a > b)
1059
 
      return 1;
1060
 
   return 0;
1061
 
}
1062
 
 
1063
 
#endif /* _VTN_PRIVATE_H_ */