Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/compiler/spirv/vtn_private.h
4545 views
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 ([email protected])
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
#ifdef __MINGW32__
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
SpvFunctionControlMask control;
278
};
279
280
#define VTN_DECL_CF_NODE_CAST(_type) \
281
static inline struct vtn_##_type * \
282
vtn_cf_node_as_##_type(struct vtn_cf_node *node) \
283
{ \
284
assert(node->type == vtn_cf_node_type_##_type); \
285
return (struct vtn_##_type *)node; \
286
}
287
288
VTN_DECL_CF_NODE_CAST(block)
289
VTN_DECL_CF_NODE_CAST(loop)
290
VTN_DECL_CF_NODE_CAST(if)
291
VTN_DECL_CF_NODE_CAST(case)
292
VTN_DECL_CF_NODE_CAST(switch)
293
VTN_DECL_CF_NODE_CAST(function)
294
295
#define vtn_foreach_cf_node(node, cf_list) \
296
list_for_each_entry(struct vtn_cf_node, node, cf_list, link)
297
298
typedef bool (*vtn_instruction_handler)(struct vtn_builder *, SpvOp,
299
const uint32_t *, unsigned);
300
301
void vtn_build_cfg(struct vtn_builder *b, const uint32_t *words,
302
const uint32_t *end);
303
void vtn_function_emit(struct vtn_builder *b, struct vtn_function *func,
304
vtn_instruction_handler instruction_handler);
305
void vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
306
const uint32_t *w, unsigned count);
307
308
const uint32_t *
309
vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
310
const uint32_t *end, vtn_instruction_handler handler);
311
312
struct vtn_ssa_value {
313
union {
314
nir_ssa_def *def;
315
struct vtn_ssa_value **elems;
316
};
317
318
/* For matrices, if this is non-NULL, then this value is actually the
319
* transpose of some other value. The value that `transposed` points to
320
* always dominates this value.
321
*/
322
struct vtn_ssa_value *transposed;
323
324
const struct glsl_type *type;
325
};
326
327
enum vtn_base_type {
328
vtn_base_type_void,
329
vtn_base_type_scalar,
330
vtn_base_type_vector,
331
vtn_base_type_matrix,
332
vtn_base_type_array,
333
vtn_base_type_struct,
334
vtn_base_type_pointer,
335
vtn_base_type_image,
336
vtn_base_type_sampler,
337
vtn_base_type_sampled_image,
338
vtn_base_type_accel_struct,
339
vtn_base_type_function,
340
vtn_base_type_event,
341
};
342
343
struct vtn_type {
344
enum vtn_base_type base_type;
345
346
const struct glsl_type *type;
347
348
/* The SPIR-V id of the given type. */
349
uint32_t id;
350
351
/* Specifies the length of complex types.
352
*
353
* For Workgroup pointers, this is the size of the referenced type.
354
*/
355
unsigned length;
356
357
/* for arrays, matrices and pointers, the array stride */
358
unsigned stride;
359
360
/* Access qualifiers */
361
enum gl_access_qualifier access;
362
363
union {
364
/* Members for scalar, vector, and array-like types */
365
struct {
366
/* for arrays, the vtn_type for the elements of the array */
367
struct vtn_type *array_element;
368
369
/* for matrices, whether the matrix is stored row-major */
370
bool row_major:1;
371
372
/* Whether this type, or a parent type, has been decorated as a
373
* builtin
374
*/
375
bool is_builtin:1;
376
377
/* Which built-in to use */
378
SpvBuiltIn builtin;
379
};
380
381
/* Members for struct types */
382
struct {
383
/* for structures, the vtn_type for each member */
384
struct vtn_type **members;
385
386
/* for structs, the offset of each member */
387
unsigned *offsets;
388
389
/* for structs, whether it was decorated as a "non-SSBO-like" block */
390
bool block:1;
391
392
/* for structs, whether it was decorated as an "SSBO-like" block */
393
bool buffer_block:1;
394
395
/* for structs with block == true, whether this is a builtin block
396
* (i.e. a block that contains only builtins).
397
*/
398
bool builtin_block:1;
399
400
/* for structs and unions it specifies the minimum alignment of the
401
* members. 0 means packed.
402
*
403
* Set by CPacked and Alignment Decorations in kernels.
404
*/
405
bool packed:1;
406
};
407
408
/* Members for pointer types */
409
struct {
410
/* For pointers, the vtn_type for dereferenced type */
411
struct vtn_type *deref;
412
413
/* Storage class for pointers */
414
SpvStorageClass storage_class;
415
416
/* Required alignment for pointers */
417
uint32_t align;
418
};
419
420
/* Members for image types */
421
struct {
422
/* GLSL image type for this type. This is not to be confused with
423
* vtn_type::type which is actually going to be the GLSL type for a
424
* pointer to an image, likely a uint32_t.
425
*/
426
const struct glsl_type *glsl_image;
427
428
/* Image format for image_load_store type images */
429
unsigned image_format;
430
431
/* Access qualifier for storage images */
432
SpvAccessQualifier access_qualifier;
433
};
434
435
/* Members for sampled image types */
436
struct {
437
/* For sampled images, the image type */
438
struct vtn_type *image;
439
};
440
441
/* Members for function types */
442
struct {
443
/* For functions, the vtn_type for each parameter */
444
struct vtn_type **params;
445
446
/* Return type for functions */
447
struct vtn_type *return_type;
448
};
449
};
450
};
451
452
bool vtn_type_contains_block(struct vtn_builder *b, struct vtn_type *type);
453
454
bool vtn_types_compatible(struct vtn_builder *b,
455
struct vtn_type *t1, struct vtn_type *t2);
456
457
struct vtn_type *vtn_type_without_array(struct vtn_type *type);
458
459
struct vtn_variable;
460
461
enum vtn_access_mode {
462
vtn_access_mode_id,
463
vtn_access_mode_literal,
464
};
465
466
struct vtn_access_link {
467
enum vtn_access_mode mode;
468
int64_t id;
469
};
470
471
struct vtn_access_chain {
472
uint32_t length;
473
474
/** Whether or not to treat the base pointer as an array. This is only
475
* true if this access chain came from an OpPtrAccessChain.
476
*/
477
bool ptr_as_array;
478
479
/* Access qualifiers */
480
enum gl_access_qualifier access;
481
482
/** Struct elements and array offsets.
483
*
484
* This is an array of 1 so that it can conveniently be created on the
485
* stack but the real length is given by the length field.
486
*/
487
struct vtn_access_link link[1];
488
};
489
490
enum vtn_variable_mode {
491
vtn_variable_mode_function,
492
vtn_variable_mode_private,
493
vtn_variable_mode_uniform,
494
vtn_variable_mode_atomic_counter,
495
vtn_variable_mode_ubo,
496
vtn_variable_mode_ssbo,
497
vtn_variable_mode_phys_ssbo,
498
vtn_variable_mode_push_constant,
499
vtn_variable_mode_workgroup,
500
vtn_variable_mode_cross_workgroup,
501
vtn_variable_mode_generic,
502
vtn_variable_mode_constant,
503
vtn_variable_mode_input,
504
vtn_variable_mode_output,
505
vtn_variable_mode_image,
506
vtn_variable_mode_accel_struct,
507
vtn_variable_mode_call_data,
508
vtn_variable_mode_call_data_in,
509
vtn_variable_mode_ray_payload,
510
vtn_variable_mode_ray_payload_in,
511
vtn_variable_mode_hit_attrib,
512
vtn_variable_mode_shader_record,
513
};
514
515
struct vtn_pointer {
516
/** The variable mode for the referenced data */
517
enum vtn_variable_mode mode;
518
519
/** The dereferenced type of this pointer */
520
struct vtn_type *type;
521
522
/** The pointer type of this pointer
523
*
524
* This may be NULL for some temporary pointers constructed as part of a
525
* large load, store, or copy. It MUST be valid for all pointers which are
526
* stored as SPIR-V SSA values.
527
*/
528
struct vtn_type *ptr_type;
529
530
/** The referenced variable, if known
531
*
532
* This field may be NULL if the pointer uses a (block_index, offset) pair
533
* instead of an access chain or if the access chain starts at a deref.
534
*/
535
struct vtn_variable *var;
536
537
/** The NIR deref corresponding to this pointer */
538
nir_deref_instr *deref;
539
540
/** A (block_index, offset) pair representing a UBO or SSBO position. */
541
struct nir_ssa_def *block_index;
542
struct nir_ssa_def *offset;
543
544
/* Access qualifiers */
545
enum gl_access_qualifier access;
546
};
547
548
struct vtn_variable {
549
enum vtn_variable_mode mode;
550
551
struct vtn_type *type;
552
553
unsigned descriptor_set;
554
unsigned binding;
555
bool explicit_binding;
556
unsigned offset;
557
unsigned input_attachment_index;
558
559
nir_variable *var;
560
561
/* If the variable is a struct with a location set on it then this will be
562
* stored here. This will be used to calculate locations for members that
563
* don’t have their own explicit location.
564
*/
565
int base_location;
566
567
/**
568
* In some early released versions of GLSLang, it implemented all function
569
* calls by making copies of all parameters into temporary variables and
570
* passing those variables into the function. It even did so for samplers
571
* and images which violates the SPIR-V spec. Unfortunately, two games
572
* (Talos Principle and Doom) shipped with this old version of GLSLang and
573
* also happen to pass samplers into functions. Talos Principle received
574
* an update fairly shortly after release with an updated GLSLang. Doom,
575
* on the other hand, has never received an update so we need to work
576
* around this GLSLang issue in SPIR-V -> NIR. Hopefully, we can drop this
577
* hack at some point in the future.
578
*/
579
struct vtn_pointer *copy_prop_sampler;
580
581
/* Access qualifiers. */
582
enum gl_access_qualifier access;
583
};
584
585
const struct glsl_type *
586
vtn_type_get_nir_type(struct vtn_builder *b, struct vtn_type *type,
587
enum vtn_variable_mode mode);
588
589
struct vtn_image_pointer {
590
nir_deref_instr *image;
591
nir_ssa_def *coord;
592
nir_ssa_def *sample;
593
nir_ssa_def *lod;
594
};
595
596
struct vtn_value {
597
enum vtn_value_type value_type;
598
599
/* Workaround for https://gitlab.freedesktop.org/mesa/mesa/-/issues/3406
600
* Only set for OpImage / OpSampledImage. Note that this is in addition
601
* the existence of a NonUniform decoration on this value.*/
602
uint32_t propagated_non_uniform : 1;
603
604
/* Valid for vtn_value_type_constant to indicate the value is OpConstantNull. */
605
bool is_null_constant:1;
606
607
const char *name;
608
struct vtn_decoration *decoration;
609
struct vtn_type *type;
610
union {
611
const char *str;
612
nir_constant *constant;
613
struct vtn_pointer *pointer;
614
struct vtn_image_pointer *image;
615
struct vtn_function *func;
616
struct vtn_block *block;
617
struct vtn_ssa_value *ssa;
618
vtn_instruction_handler ext_handler;
619
};
620
};
621
622
#define VTN_DEC_DECORATION -1
623
#define VTN_DEC_EXECUTION_MODE -2
624
#define VTN_DEC_STRUCT_MEMBER0 0
625
626
struct vtn_decoration {
627
struct vtn_decoration *next;
628
629
/* Specifies how to apply this decoration. Negative values represent a
630
* decoration or execution mode. (See the VTN_DEC_ #defines above.)
631
* Non-negative values specify that it applies to a structure member.
632
*/
633
int scope;
634
635
const uint32_t *operands;
636
struct vtn_value *group;
637
638
union {
639
SpvDecoration decoration;
640
SpvExecutionMode exec_mode;
641
};
642
};
643
644
struct vtn_builder {
645
nir_builder nb;
646
647
/* Used by vtn_fail to jump back to the beginning of SPIR-V compilation */
648
jmp_buf fail_jump;
649
650
const uint32_t *spirv;
651
size_t spirv_word_count;
652
uint32_t version;
653
654
nir_shader *shader;
655
struct spirv_to_nir_options *options;
656
struct vtn_block *block;
657
658
/* Current offset, file, line, and column. Useful for debugging. Set
659
* automatically by vtn_foreach_instruction.
660
*/
661
size_t spirv_offset;
662
const char *file;
663
int line, col;
664
665
/*
666
* In SPIR-V, constants are global, whereas in NIR, the load_const
667
* instruction we use is per-function. So while we parse each function, we
668
* keep a hash table of constants we've resolved to nir_ssa_value's so
669
* far, and we lazily resolve them when we see them used in a function.
670
*/
671
struct hash_table *const_table;
672
673
/*
674
* Map from phi instructions (pointer to the start of the instruction)
675
* to the variable corresponding to it.
676
*/
677
struct hash_table *phi_table;
678
679
/* In Vulkan, when lowering some modes variable access, the derefs of the
680
* variables are replaced with a resource index intrinsics, leaving the
681
* variable hanging. This set keeps track of them so they can be filtered
682
* (and not removed) in nir_remove_dead_variables.
683
*/
684
struct set *vars_used_indirectly;
685
686
unsigned num_specializations;
687
struct nir_spirv_specialization *specializations;
688
689
unsigned value_id_bound;
690
struct vtn_value *values;
691
692
/* Information on the origin of the SPIR-V */
693
enum vtn_generator generator_id;
694
SpvSourceLanguage source_lang;
695
696
/* True if we need to fix up CS OpControlBarrier */
697
bool wa_glslang_cs_barrier;
698
699
/* Workaround discard bugs in HLSL -> SPIR-V compilers */
700
bool uses_demote_to_helper_invocation;
701
bool convert_discard_to_demote;
702
703
gl_shader_stage entry_point_stage;
704
const char *entry_point_name;
705
struct vtn_value *entry_point;
706
struct vtn_value *workgroup_size_builtin;
707
bool variable_pointers;
708
709
uint32_t *interface_ids;
710
size_t interface_ids_count;
711
712
struct vtn_function *func;
713
struct list_head functions;
714
715
/* Current function parameter index */
716
unsigned func_param_idx;
717
718
/* false by default, set to true by the ContractionOff execution mode */
719
bool exact;
720
721
/* when a physical memory model is choosen */
722
bool physical_ptrs;
723
724
/* memory model specified by OpMemoryModel */
725
unsigned mem_model;
726
};
727
728
nir_ssa_def *
729
vtn_pointer_to_ssa(struct vtn_builder *b, struct vtn_pointer *ptr);
730
struct vtn_pointer *
731
vtn_pointer_from_ssa(struct vtn_builder *b, nir_ssa_def *ssa,
732
struct vtn_type *ptr_type);
733
734
static inline struct vtn_value *
735
vtn_untyped_value(struct vtn_builder *b, uint32_t value_id)
736
{
737
vtn_fail_if(value_id >= b->value_id_bound,
738
"SPIR-V id %u is out-of-bounds", value_id);
739
return &b->values[value_id];
740
}
741
742
static inline uint32_t
743
vtn_id_for_value(struct vtn_builder *b, struct vtn_value *value)
744
{
745
vtn_fail_if(value <= b->values, "vtn_value pointer outside the range of valid values");
746
uint32_t value_id = value - b->values;
747
vtn_fail_if(value_id >= b->value_id_bound, "vtn_value pointer outside the range of valid values");
748
return value_id;
749
}
750
751
/* Consider not using this function directly and instead use
752
* vtn_push_ssa/vtn_push_pointer so that appropriate applying of
753
* decorations is handled by common code.
754
*/
755
static inline struct vtn_value *
756
vtn_push_value(struct vtn_builder *b, uint32_t value_id,
757
enum vtn_value_type value_type)
758
{
759
struct vtn_value *val = vtn_untyped_value(b, value_id);
760
761
vtn_fail_if(value_type == vtn_value_type_ssa,
762
"Do not call vtn_push_value for value_type_ssa. Use "
763
"vtn_push_ssa_value instead.");
764
765
vtn_fail_if(val->value_type != vtn_value_type_invalid,
766
"SPIR-V id %u has already been written by another instruction",
767
value_id);
768
769
val->value_type = value_type;
770
771
return &b->values[value_id];
772
}
773
774
static inline struct vtn_value *
775
vtn_value(struct vtn_builder *b, uint32_t value_id,
776
enum vtn_value_type value_type)
777
{
778
struct vtn_value *val = vtn_untyped_value(b, value_id);
779
vtn_fail_if(val->value_type != value_type,
780
"SPIR-V id %u is the wrong kind of value", value_id);
781
return val;
782
}
783
784
bool
785
vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
786
const uint32_t *w, unsigned count);
787
788
static inline uint64_t
789
vtn_constant_uint(struct vtn_builder *b, uint32_t value_id)
790
{
791
struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
792
793
vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
794
!glsl_type_is_integer(val->type->type),
795
"Expected id %u to be an integer constant", value_id);
796
797
switch (glsl_get_bit_size(val->type->type)) {
798
case 8: return val->constant->values[0].u8;
799
case 16: return val->constant->values[0].u16;
800
case 32: return val->constant->values[0].u32;
801
case 64: return val->constant->values[0].u64;
802
default: unreachable("Invalid bit size");
803
}
804
}
805
806
static inline int64_t
807
vtn_constant_int(struct vtn_builder *b, uint32_t value_id)
808
{
809
struct vtn_value *val = vtn_value(b, value_id, vtn_value_type_constant);
810
811
vtn_fail_if(val->type->base_type != vtn_base_type_scalar ||
812
!glsl_type_is_integer(val->type->type),
813
"Expected id %u to be an integer constant", value_id);
814
815
switch (glsl_get_bit_size(val->type->type)) {
816
case 8: return val->constant->values[0].i8;
817
case 16: return val->constant->values[0].i16;
818
case 32: return val->constant->values[0].i32;
819
case 64: return val->constant->values[0].i64;
820
default: unreachable("Invalid bit size");
821
}
822
}
823
824
static inline struct vtn_type *
825
vtn_get_value_type(struct vtn_builder *b, uint32_t value_id)
826
{
827
struct vtn_value *val = vtn_untyped_value(b, value_id);
828
vtn_fail_if(val->type == NULL, "Value %u does not have a type", value_id);
829
return val->type;
830
}
831
832
static inline struct vtn_type *
833
vtn_get_type(struct vtn_builder *b, uint32_t value_id)
834
{
835
return vtn_value(b, value_id, vtn_value_type_type)->type;
836
}
837
838
struct vtn_ssa_value *vtn_ssa_value(struct vtn_builder *b, uint32_t value_id);
839
struct vtn_value *vtn_push_ssa_value(struct vtn_builder *b, uint32_t value_id,
840
struct vtn_ssa_value *ssa);
841
842
nir_ssa_def *vtn_get_nir_ssa(struct vtn_builder *b, uint32_t value_id);
843
struct vtn_value *vtn_push_nir_ssa(struct vtn_builder *b, uint32_t value_id,
844
nir_ssa_def *def);
845
846
struct vtn_value *vtn_push_pointer(struct vtn_builder *b,
847
uint32_t value_id,
848
struct vtn_pointer *ptr);
849
850
struct vtn_sampled_image {
851
nir_deref_instr *image;
852
nir_deref_instr *sampler;
853
};
854
855
nir_ssa_def *vtn_sampled_image_to_nir_ssa(struct vtn_builder *b,
856
struct vtn_sampled_image si);
857
858
void
859
vtn_copy_value(struct vtn_builder *b, uint32_t src_value_id,
860
uint32_t dst_value_id);
861
862
struct vtn_ssa_value *vtn_create_ssa_value(struct vtn_builder *b,
863
const struct glsl_type *type);
864
865
struct vtn_ssa_value *vtn_ssa_transpose(struct vtn_builder *b,
866
struct vtn_ssa_value *src);
867
868
nir_deref_instr *vtn_nir_deref(struct vtn_builder *b, uint32_t id);
869
870
nir_deref_instr *vtn_pointer_to_deref(struct vtn_builder *b,
871
struct vtn_pointer *ptr);
872
nir_ssa_def *
873
vtn_pointer_to_offset(struct vtn_builder *b, struct vtn_pointer *ptr,
874
nir_ssa_def **index_out);
875
876
nir_deref_instr *
877
vtn_get_call_payload_for_location(struct vtn_builder *b, uint32_t location_id);
878
879
struct vtn_ssa_value *
880
vtn_local_load(struct vtn_builder *b, nir_deref_instr *src,
881
enum gl_access_qualifier access);
882
883
void vtn_local_store(struct vtn_builder *b, struct vtn_ssa_value *src,
884
nir_deref_instr *dest,
885
enum gl_access_qualifier access);
886
887
struct vtn_ssa_value *
888
vtn_variable_load(struct vtn_builder *b, struct vtn_pointer *src,
889
enum gl_access_qualifier access);
890
891
void vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
892
struct vtn_pointer *dest, enum gl_access_qualifier access);
893
894
void vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
895
const uint32_t *w, unsigned count);
896
897
898
typedef void (*vtn_decoration_foreach_cb)(struct vtn_builder *,
899
struct vtn_value *,
900
int member,
901
const struct vtn_decoration *,
902
void *);
903
904
void vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
905
vtn_decoration_foreach_cb cb, void *data);
906
907
typedef void (*vtn_execution_mode_foreach_cb)(struct vtn_builder *,
908
struct vtn_value *,
909
const struct vtn_decoration *,
910
void *);
911
912
void vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
913
vtn_execution_mode_foreach_cb cb, void *data);
914
915
nir_op vtn_nir_alu_op_for_spirv_opcode(struct vtn_builder *b,
916
SpvOp opcode, bool *swap, bool *exact,
917
unsigned src_bit_size, unsigned dst_bit_size);
918
919
void vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
920
const uint32_t *w, unsigned count);
921
922
void vtn_handle_bitcast(struct vtn_builder *b, const uint32_t *w,
923
unsigned count);
924
925
void vtn_handle_no_contraction(struct vtn_builder *b, struct vtn_value *val);
926
927
void vtn_handle_subgroup(struct vtn_builder *b, SpvOp opcode,
928
const uint32_t *w, unsigned count);
929
930
bool vtn_handle_glsl450_instruction(struct vtn_builder *b, SpvOp ext_opcode,
931
const uint32_t *words, unsigned count);
932
933
bool vtn_handle_opencl_instruction(struct vtn_builder *b, SpvOp ext_opcode,
934
const uint32_t *words, unsigned count);
935
bool vtn_handle_opencl_core_instruction(struct vtn_builder *b, SpvOp opcode,
936
const uint32_t *w, unsigned count);
937
938
struct vtn_builder* vtn_create_builder(const uint32_t *words, size_t word_count,
939
gl_shader_stage stage, const char *entry_point_name,
940
const struct spirv_to_nir_options *options);
941
942
void vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
943
unsigned count);
944
945
void vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
946
const uint32_t *w, unsigned count);
947
948
enum vtn_variable_mode vtn_storage_class_to_mode(struct vtn_builder *b,
949
SpvStorageClass class,
950
struct vtn_type *interface_type,
951
nir_variable_mode *nir_mode_out);
952
953
nir_address_format vtn_mode_to_address_format(struct vtn_builder *b,
954
enum vtn_variable_mode);
955
956
nir_rounding_mode vtn_rounding_mode_to_nir(struct vtn_builder *b,
957
SpvFPRoundingMode mode);
958
959
static inline uint32_t
960
vtn_align_u32(uint32_t v, uint32_t a)
961
{
962
assert(a != 0 && a == (a & -((int32_t) a)));
963
return (v + a - 1) & ~(a - 1);
964
}
965
966
static inline uint64_t
967
vtn_u64_literal(const uint32_t *w)
968
{
969
return (uint64_t)w[1] << 32 | w[0];
970
}
971
972
bool vtn_handle_amd_gcn_shader_instruction(struct vtn_builder *b, SpvOp ext_opcode,
973
const uint32_t *words, unsigned count);
974
975
bool vtn_handle_amd_shader_ballot_instruction(struct vtn_builder *b, SpvOp ext_opcode,
976
const uint32_t *w, unsigned count);
977
978
bool vtn_handle_amd_shader_trinary_minmax_instruction(struct vtn_builder *b, SpvOp ext_opcode,
979
const uint32_t *words, unsigned count);
980
981
bool vtn_handle_amd_shader_explicit_vertex_parameter_instruction(struct vtn_builder *b,
982
SpvOp ext_opcode,
983
const uint32_t *words,
984
unsigned count);
985
986
SpvMemorySemanticsMask vtn_mode_to_memory_semantics(enum vtn_variable_mode mode);
987
988
void vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
989
SpvMemorySemanticsMask semantics);
990
991
static inline int
992
cmp_uint32_t(const void *pa, const void *pb)
993
{
994
uint32_t a = *((const uint32_t *)pa);
995
uint32_t b = *((const uint32_t *)pb);
996
if (a < b)
997
return -1;
998
if (a > b)
999
return 1;
1000
return 0;
1001
}
1002
1003
#endif /* _VTN_PRIVATE_H_ */
1004
1005