1 /*
2  * Copyright © 2014 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  *    Connor Abbott (cwabbott0@gmail.com)
25  *
26  */
27 
28 #include "nir.h"
29 #include "c11/threads.h"
30 #include <assert.h>
31 
32 /*
33  * This file checks for invalid IR indicating a bug somewhere in the compiler.
34  */
35 
36 /* Since this file is just a pile of asserts, don't bother compiling it if
37  * we're not building a debug build.
38  */
39 #ifndef NDEBUG
40 
41 /*
42  * Per-register validation state.
43  */
44 
45 typedef struct {
46    /*
47     * equivalent to the uses and defs in nir_register, but built up by the
48     * validator. At the end, we verify that the sets have the same entries.
49     */
50    struct set *uses, *if_uses, *defs;
51    nir_function_impl *where_defined; /* NULL for global registers */
52 } reg_validate_state;
53 
54 typedef struct {
55    void *mem_ctx;
56 
57    /* map of register -> validation state (struct above) */
58    struct hash_table *regs;
59 
60    /* the current shader being validated */
61    nir_shader *shader;
62 
63    /* the current instruction being validated */
64    nir_instr *instr;
65 
66    /* the current variable being validated */
67    nir_variable *var;
68 
69    /* the current basic block being validated */
70    nir_block *block;
71 
72    /* the current if statement being validated */
73    nir_if *if_stmt;
74 
75    /* the current loop being visited */
76    nir_loop *loop;
77 
78    /* the parent of the current cf node being visited */
79    nir_cf_node *parent_node;
80 
81    /* the current function implementation being validated */
82    nir_function_impl *impl;
83 
84    /* Set of all blocks in the list */
85    struct set *blocks;
86 
87    /* Set of seen SSA sources */
88    struct set *ssa_srcs;
89 
90    /* bitset of ssa definitions we have found; used to check uniqueness */
91    BITSET_WORD *ssa_defs_found;
92 
93    /* bitset of registers we have currently found; used to check uniqueness */
94    BITSET_WORD *regs_found;
95 
96    /* map of variable -> function implementation where it is defined or NULL
97     * if it is a global variable
98     */
99    struct hash_table *var_defs;
100 
101    /* map of instruction/var/etc to failed assert string */
102    struct hash_table *errors;
103 
104    struct set *shader_gc_list;
105 } validate_state;
106 
107 static void
log_error(validate_state * state,const char * cond,const char * file,int line)108 log_error(validate_state *state, const char *cond, const char *file, int line)
109 {
110    const void *obj;
111 
112    if (state->instr)
113       obj = state->instr;
114    else if (state->var)
115       obj = state->var;
116    else
117       obj = cond;
118 
119    char *msg = ralloc_asprintf(state->errors, "error: %s (%s:%d)",
120                                cond, file, line);
121 
122    _mesa_hash_table_insert(state->errors, obj, msg);
123 }
124 
125 static bool
validate_assert_impl(validate_state * state,bool cond,const char * str,const char * file,unsigned line)126 validate_assert_impl(validate_state *state, bool cond, const char *str,
127                      const char *file, unsigned line)
128 {
129    if (!cond)
130       log_error(state, str, file, line);
131    return cond;
132 }
133 
134 #define validate_assert(state, cond) \
135    validate_assert_impl(state, (cond), #cond, __FILE__, __LINE__)
136 
137 
138 static void validate_src(nir_src *src, validate_state *state,
139                          unsigned bit_sizes, unsigned num_components);
140 
141 static void
validate_num_components(validate_state * state,unsigned num_components)142 validate_num_components(validate_state *state, unsigned num_components)
143 {
144    validate_assert(state, nir_num_components_valid(num_components));
145 }
146 
147 static void
validate_reg_src(nir_src * src,validate_state * state,unsigned bit_sizes,unsigned num_components)148 validate_reg_src(nir_src *src, validate_state *state,
149                  unsigned bit_sizes, unsigned num_components)
150 {
151    validate_assert(state, src->reg.reg != NULL);
152 
153    struct hash_entry *entry;
154    entry = _mesa_hash_table_search(state->regs, src->reg.reg);
155    validate_assert(state, entry);
156 
157    reg_validate_state *reg_state = (reg_validate_state *) entry->data;
158 
159    if (state->instr) {
160       _mesa_set_add(reg_state->uses, src);
161    } else {
162       validate_assert(state, state->if_stmt);
163       _mesa_set_add(reg_state->if_uses, src);
164    }
165 
166    validate_assert(state, reg_state->where_defined == state->impl &&
167           "using a register declared in a different function");
168 
169    if (bit_sizes)
170       validate_assert(state, src->reg.reg->bit_size & bit_sizes);
171    if (num_components)
172       validate_assert(state, src->reg.reg->num_components == num_components);
173 
174    validate_assert(state, (src->reg.reg->num_array_elems == 0 ||
175           src->reg.base_offset < src->reg.reg->num_array_elems) &&
176           "definitely out-of-bounds array access");
177 
178    if (src->reg.indirect) {
179       validate_assert(state, src->reg.reg->num_array_elems != 0);
180       validate_assert(state, (src->reg.indirect->is_ssa ||
181               src->reg.indirect->reg.indirect == NULL) &&
182              "only one level of indirection allowed");
183       validate_src(src->reg.indirect, state, 32, 1);
184    }
185 }
186 
187 #define SET_PTR_BIT(ptr, bit) \
188    (void *)(((uintptr_t)(ptr)) | (((uintptr_t)1) << bit))
189 
190 static void
validate_ssa_src(nir_src * src,validate_state * state,unsigned bit_sizes,unsigned num_components)191 validate_ssa_src(nir_src *src, validate_state *state,
192                  unsigned bit_sizes, unsigned num_components)
193 {
194    validate_assert(state, src->ssa != NULL);
195 
196    /* As we walk SSA defs, we add every use to this set.  We need to make sure
197     * our use is seen in a use list.
198     */
199    struct set_entry *entry;
200    if (state->instr) {
201       entry = _mesa_set_search(state->ssa_srcs, src);
202    } else {
203       entry = _mesa_set_search(state->ssa_srcs, SET_PTR_BIT(src, 0));
204    }
205    validate_assert(state, entry);
206 
207    /* This will let us prove that we've seen all the sources */
208    if (entry)
209       _mesa_set_remove(state->ssa_srcs, entry);
210 
211    if (bit_sizes)
212       validate_assert(state, src->ssa->bit_size & bit_sizes);
213    if (num_components)
214       validate_assert(state, src->ssa->num_components == num_components);
215 
216    /* TODO validate that the use is dominated by the definition */
217 }
218 
219 static void
validate_src(nir_src * src,validate_state * state,unsigned bit_sizes,unsigned num_components)220 validate_src(nir_src *src, validate_state *state,
221              unsigned bit_sizes, unsigned num_components)
222 {
223    if (state->instr)
224       validate_assert(state, src->parent_instr == state->instr);
225    else
226       validate_assert(state, src->parent_if == state->if_stmt);
227 
228    if (src->is_ssa)
229       validate_ssa_src(src, state, bit_sizes, num_components);
230    else
231       validate_reg_src(src, state, bit_sizes, num_components);
232 }
233 
234 static void
validate_alu_src(nir_alu_instr * instr,unsigned index,validate_state * state)235 validate_alu_src(nir_alu_instr *instr, unsigned index, validate_state *state)
236 {
237    nir_alu_src *src = &instr->src[index];
238 
239    if (instr->op == nir_op_mov)
240       assert(!src->abs && !src->negate);
241 
242    unsigned num_components = nir_src_num_components(src->src);
243    for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
244       validate_assert(state, src->swizzle[i] < NIR_MAX_VEC_COMPONENTS);
245 
246       if (nir_alu_instr_channel_used(instr, index, i))
247          validate_assert(state, src->swizzle[i] < num_components);
248    }
249 
250    validate_src(&src->src, state, 0, 0);
251 }
252 
253 static void
validate_reg_dest(nir_reg_dest * dest,validate_state * state,unsigned bit_sizes,unsigned num_components)254 validate_reg_dest(nir_reg_dest *dest, validate_state *state,
255                   unsigned bit_sizes, unsigned num_components)
256 {
257    validate_assert(state, dest->reg != NULL);
258 
259    validate_assert(state, dest->parent_instr == state->instr);
260 
261    struct hash_entry *entry2;
262    entry2 = _mesa_hash_table_search(state->regs, dest->reg);
263 
264    validate_assert(state, entry2);
265 
266    reg_validate_state *reg_state = (reg_validate_state *) entry2->data;
267    _mesa_set_add(reg_state->defs, dest);
268 
269    validate_assert(state, reg_state->where_defined == state->impl &&
270           "writing to a register declared in a different function");
271 
272    if (bit_sizes)
273       validate_assert(state, dest->reg->bit_size & bit_sizes);
274    if (num_components)
275       validate_assert(state, dest->reg->num_components == num_components);
276 
277    validate_assert(state, (dest->reg->num_array_elems == 0 ||
278           dest->base_offset < dest->reg->num_array_elems) &&
279           "definitely out-of-bounds array access");
280 
281    if (dest->indirect) {
282       validate_assert(state, dest->reg->num_array_elems != 0);
283       validate_assert(state, (dest->indirect->is_ssa || dest->indirect->reg.indirect == NULL) &&
284              "only one level of indirection allowed");
285       validate_src(dest->indirect, state, 32, 1);
286    }
287 }
288 
289 static void
validate_ssa_def(nir_ssa_def * def,validate_state * state)290 validate_ssa_def(nir_ssa_def *def, validate_state *state)
291 {
292    validate_assert(state, def->index < state->impl->ssa_alloc);
293    validate_assert(state, !BITSET_TEST(state->ssa_defs_found, def->index));
294    BITSET_SET(state->ssa_defs_found, def->index);
295 
296    validate_assert(state, def->parent_instr == state->instr);
297    validate_num_components(state, def->num_components);
298 
299    list_validate(&def->uses);
300    nir_foreach_use(src, def) {
301       validate_assert(state, src->is_ssa);
302       validate_assert(state, src->ssa == def);
303       bool already_seen = false;
304       _mesa_set_search_and_add(state->ssa_srcs, src, &already_seen);
305       /* A nir_src should only appear once and only in one SSA def use list */
306       validate_assert(state, !already_seen);
307    }
308 
309    list_validate(&def->if_uses);
310    nir_foreach_if_use(src, def) {
311       validate_assert(state, src->is_ssa);
312       validate_assert(state, src->ssa == def);
313       bool already_seen = false;
314       _mesa_set_search_and_add(state->ssa_srcs, SET_PTR_BIT(src, 0),
315                                &already_seen);
316       /* A nir_src should only appear once and only in one SSA def use list */
317       validate_assert(state, !already_seen);
318    }
319 }
320 
321 static void
validate_dest(nir_dest * dest,validate_state * state,unsigned bit_sizes,unsigned num_components)322 validate_dest(nir_dest *dest, validate_state *state,
323               unsigned bit_sizes, unsigned num_components)
324 {
325    if (dest->is_ssa) {
326       if (bit_sizes)
327          validate_assert(state, dest->ssa.bit_size & bit_sizes);
328       if (num_components)
329          validate_assert(state, dest->ssa.num_components == num_components);
330       validate_ssa_def(&dest->ssa, state);
331    } else {
332       validate_reg_dest(&dest->reg, state, bit_sizes, num_components);
333    }
334 }
335 
336 static void
validate_alu_dest(nir_alu_instr * instr,validate_state * state)337 validate_alu_dest(nir_alu_instr *instr, validate_state *state)
338 {
339    nir_alu_dest *dest = &instr->dest;
340 
341    if (instr->op == nir_op_mov)
342       assert(!dest->saturate);
343 
344    unsigned dest_size = nir_dest_num_components(dest->dest);
345    /*
346     * validate that the instruction doesn't write to components not in the
347     * register/SSA value
348     */
349    validate_assert(state, !(dest->write_mask & ~((1 << dest_size) - 1)));
350 
351    /* validate that saturate is only ever used on instructions with
352     * destinations of type float
353     */
354    nir_alu_instr *alu = nir_instr_as_alu(state->instr);
355    validate_assert(state,
356           (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) ==
357            nir_type_float) ||
358           !dest->saturate);
359 
360    validate_dest(&dest->dest, state, 0, 0);
361 }
362 
363 static void
validate_alu_instr(nir_alu_instr * instr,validate_state * state)364 validate_alu_instr(nir_alu_instr *instr, validate_state *state)
365 {
366    validate_assert(state, instr->op < nir_num_opcodes);
367 
368    unsigned instr_bit_size = 0;
369    for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
370       nir_alu_type src_type = nir_op_infos[instr->op].input_types[i];
371       unsigned src_bit_size = nir_src_bit_size(instr->src[i].src);
372       if (nir_alu_type_get_type_size(src_type)) {
373          validate_assert(state, src_bit_size == nir_alu_type_get_type_size(src_type));
374       } else if (instr_bit_size) {
375          validate_assert(state, src_bit_size == instr_bit_size);
376       } else {
377          instr_bit_size = src_bit_size;
378       }
379 
380       if (nir_alu_type_get_base_type(src_type) == nir_type_float) {
381          /* 8-bit float isn't a thing */
382          validate_assert(state, src_bit_size == 16 || src_bit_size == 32 ||
383                                 src_bit_size == 64);
384       }
385 
386       validate_alu_src(instr, i, state);
387    }
388 
389    nir_alu_type dest_type = nir_op_infos[instr->op].output_type;
390    unsigned dest_bit_size = nir_dest_bit_size(instr->dest.dest);
391    if (nir_alu_type_get_type_size(dest_type)) {
392       validate_assert(state, dest_bit_size == nir_alu_type_get_type_size(dest_type));
393    } else if (instr_bit_size) {
394       validate_assert(state, dest_bit_size == instr_bit_size);
395    } else {
396       /* The only unsized thing is the destination so it's vacuously valid */
397    }
398 
399    if (nir_alu_type_get_base_type(dest_type) == nir_type_float) {
400       /* 8-bit float isn't a thing */
401       validate_assert(state, dest_bit_size == 16 || dest_bit_size == 32 ||
402                              dest_bit_size == 64);
403    }
404 
405    validate_alu_dest(instr, state);
406 }
407 
408 static void
validate_var_use(nir_variable * var,validate_state * state)409 validate_var_use(nir_variable *var, validate_state *state)
410 {
411    struct hash_entry *entry = _mesa_hash_table_search(state->var_defs, var);
412    validate_assert(state, entry);
413    if (entry && var->data.mode == nir_var_function_temp)
414       validate_assert(state, (nir_function_impl *) entry->data == state->impl);
415 }
416 
417 static void
validate_deref_instr(nir_deref_instr * instr,validate_state * state)418 validate_deref_instr(nir_deref_instr *instr, validate_state *state)
419 {
420    if (instr->deref_type == nir_deref_type_var) {
421       /* Variable dereferences are stupid simple. */
422       validate_assert(state, instr->modes == instr->var->data.mode);
423       validate_assert(state, instr->type == instr->var->type);
424       validate_var_use(instr->var, state);
425    } else if (instr->deref_type == nir_deref_type_cast) {
426       /* For cast, we simply have to trust the instruction.  It's up to
427        * lowering passes and front/back-ends to make them sane.
428        */
429       validate_src(&instr->parent, state, 0, 0);
430 
431       /* Most variable modes in NIR can only exist by themselves. */
432       if (instr->modes & ~nir_var_mem_generic)
433          validate_assert(state, util_bitcount(instr->modes) == 1);
434 
435       nir_deref_instr *parent = nir_src_as_deref(instr->parent);
436       if (parent) {
437          /* Casts can change the mode but it can't change completely.  The new
438           * mode must have some bits in common with the old.
439           */
440          validate_assert(state, instr->modes & parent->modes);
441       } else {
442          /* If our parent isn't a deref, just assert the mode is there */
443          validate_assert(state, instr->modes != 0);
444       }
445 
446       /* We just validate that the type is there */
447       validate_assert(state, instr->type);
448       if (instr->cast.align_mul > 0) {
449          validate_assert(state, util_is_power_of_two_nonzero(instr->cast.align_mul));
450          validate_assert(state, instr->cast.align_offset < instr->cast.align_mul);
451       } else {
452          validate_assert(state, instr->cast.align_offset == 0);
453       }
454    } else {
455       /* We require the parent to be SSA.  This may be lifted in the future */
456       validate_assert(state, instr->parent.is_ssa);
457 
458       /* The parent pointer value must have the same number of components
459        * as the destination.
460        */
461       validate_src(&instr->parent, state, nir_dest_bit_size(instr->dest),
462                    nir_dest_num_components(instr->dest));
463 
464       nir_instr *parent_instr = instr->parent.ssa->parent_instr;
465 
466       /* The parent must come from another deref instruction */
467       validate_assert(state, parent_instr->type == nir_instr_type_deref);
468 
469       nir_deref_instr *parent = nir_instr_as_deref(parent_instr);
470 
471       validate_assert(state, instr->modes == parent->modes);
472 
473       switch (instr->deref_type) {
474       case nir_deref_type_struct:
475          validate_assert(state, glsl_type_is_struct_or_ifc(parent->type));
476          validate_assert(state,
477             instr->strct.index < glsl_get_length(parent->type));
478          validate_assert(state, instr->type ==
479             glsl_get_struct_field(parent->type, instr->strct.index));
480          break;
481 
482       case nir_deref_type_array:
483       case nir_deref_type_array_wildcard:
484          if (instr->modes & nir_var_vec_indexable_modes) {
485             /* Shared variables and UBO/SSBOs have a bit more relaxed rules
486              * because we need to be able to handle array derefs on vectors.
487              * Fortunately, nir_lower_io handles these just fine.
488              */
489             validate_assert(state, glsl_type_is_array(parent->type) ||
490                                    glsl_type_is_matrix(parent->type) ||
491                                    glsl_type_is_vector(parent->type));
492          } else {
493             /* Most of NIR cannot handle array derefs on vectors */
494             validate_assert(state, glsl_type_is_array(parent->type) ||
495                                    glsl_type_is_matrix(parent->type));
496          }
497          validate_assert(state,
498             instr->type == glsl_get_array_element(parent->type));
499 
500          if (instr->deref_type == nir_deref_type_array) {
501             validate_src(&instr->arr.index, state,
502                          nir_dest_bit_size(instr->dest), 1);
503          }
504          break;
505 
506       case nir_deref_type_ptr_as_array:
507          /* ptr_as_array derefs must have a parent that is either an array,
508           * ptr_as_array, or cast.  If the parent is a cast, we get the stride
509           * information (if any) from the cast deref.
510           */
511          validate_assert(state,
512                          parent->deref_type == nir_deref_type_array ||
513                          parent->deref_type == nir_deref_type_ptr_as_array ||
514                          parent->deref_type == nir_deref_type_cast);
515          validate_src(&instr->arr.index, state,
516                       nir_dest_bit_size(instr->dest), 1);
517          break;
518 
519       default:
520          unreachable("Invalid deref instruction type");
521       }
522    }
523 
524    /* We intentionally don't validate the size of the destination because we
525     * want to let other compiler components such as SPIR-V decide how big
526     * pointers should be.
527     */
528    validate_dest(&instr->dest, state, 0, 0);
529 
530    /* Deref instructions as if conditions don't make sense because if
531     * conditions expect well-formed Booleans.  If you want to compare with
532     * NULL, an explicit comparison operation should be used.
533     */
534    validate_assert(state, list_is_empty(&instr->dest.ssa.if_uses));
535 
536    /* Certain modes cannot be used as sources for phi instructions because
537     * way too many passes assume that they can always chase deref chains.
538     */
539    nir_foreach_use(use, &instr->dest.ssa) {
540       if (use->parent_instr->type == nir_instr_type_phi) {
541          validate_assert(state, !(instr->modes & (nir_var_shader_in |
542                                                   nir_var_shader_out |
543                                                   nir_var_shader_out |
544                                                   nir_var_uniform)));
545       }
546    }
547 }
548 
549 static bool
vectorized_intrinsic(nir_intrinsic_instr * intr)550 vectorized_intrinsic(nir_intrinsic_instr *intr)
551 {
552    const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
553 
554    if (info->dest_components == 0)
555       return true;
556 
557    for (unsigned i = 0; i < info->num_srcs; i++)
558       if (info->src_components[i] == 0)
559          return true;
560 
561    return false;
562 }
563 
564 /** Returns the image format or PIPE_FORMAT_COUNT for incomplete derefs
565  *
566  * We use PIPE_FORMAT_COUNT for incomplete derefs because PIPE_FORMAT_NONE
567  * indicates that we found the variable but it has no format specified.
568  */
569 static enum pipe_format
image_intrin_format(nir_intrinsic_instr * instr)570 image_intrin_format(nir_intrinsic_instr *instr)
571 {
572    if (nir_intrinsic_format(instr) != PIPE_FORMAT_NONE)
573       return nir_intrinsic_format(instr);
574 
575    /* If this not a deref intrinsic, PIPE_FORMAT_NONE is the best we can do */
576    if (nir_intrinsic_infos[instr->intrinsic].src_components[0] != -1)
577       return PIPE_FORMAT_NONE;
578 
579    nir_variable *var = nir_intrinsic_get_var(instr, 0);
580    if (var == NULL)
581       return PIPE_FORMAT_COUNT;
582 
583    return var->data.image.format;
584 }
585 
586 static void
validate_intrinsic_instr(nir_intrinsic_instr * instr,validate_state * state)587 validate_intrinsic_instr(nir_intrinsic_instr *instr, validate_state *state)
588 {
589    unsigned dest_bit_size = 0;
590    unsigned src_bit_sizes[NIR_INTRINSIC_MAX_INPUTS] = { 0, };
591    switch (instr->intrinsic) {
592    case nir_intrinsic_convert_alu_types: {
593       nir_alu_type src_type = nir_intrinsic_src_type(instr);
594       nir_alu_type dest_type = nir_intrinsic_dest_type(instr);
595       dest_bit_size = nir_alu_type_get_type_size(dest_type);
596       src_bit_sizes[0] = nir_alu_type_get_type_size(src_type);
597       validate_assert(state, dest_bit_size != 0);
598       validate_assert(state, src_bit_sizes[0] != 0);
599       break;
600    }
601 
602    case nir_intrinsic_load_param: {
603       unsigned param_idx = nir_intrinsic_param_idx(instr);
604       validate_assert(state, param_idx < state->impl->function->num_params);
605       nir_parameter *param = &state->impl->function->params[param_idx];
606       validate_assert(state, instr->num_components == param->num_components);
607       dest_bit_size = param->bit_size;
608       break;
609    }
610 
611    case nir_intrinsic_load_deref: {
612       nir_deref_instr *src = nir_src_as_deref(instr->src[0]);
613       assert(src);
614       validate_assert(state, glsl_type_is_vector_or_scalar(src->type) ||
615                       (src->modes == nir_var_uniform &&
616                        glsl_get_base_type(src->type) == GLSL_TYPE_SUBROUTINE));
617       validate_assert(state, instr->num_components ==
618                              glsl_get_vector_elements(src->type));
619       dest_bit_size = glsl_get_bit_size(src->type);
620       /* Also allow 32-bit boolean load operations */
621       if (glsl_type_is_boolean(src->type))
622          dest_bit_size |= 32;
623       break;
624    }
625 
626    case nir_intrinsic_store_deref: {
627       nir_deref_instr *dst = nir_src_as_deref(instr->src[0]);
628       assert(dst);
629       validate_assert(state, glsl_type_is_vector_or_scalar(dst->type));
630       validate_assert(state, instr->num_components ==
631                              glsl_get_vector_elements(dst->type));
632       src_bit_sizes[1] = glsl_get_bit_size(dst->type);
633       /* Also allow 32-bit boolean store operations */
634       if (glsl_type_is_boolean(dst->type))
635          src_bit_sizes[1] |= 32;
636       validate_assert(state, !nir_deref_mode_may_be(dst, nir_var_read_only_modes));
637       validate_assert(state, (nir_intrinsic_write_mask(instr) & ~((1 << instr->num_components) - 1)) == 0);
638       break;
639    }
640 
641    case nir_intrinsic_copy_deref: {
642       nir_deref_instr *dst = nir_src_as_deref(instr->src[0]);
643       nir_deref_instr *src = nir_src_as_deref(instr->src[1]);
644       validate_assert(state, glsl_get_bare_type(dst->type) ==
645                              glsl_get_bare_type(src->type));
646       validate_assert(state, !nir_deref_mode_may_be(dst, nir_var_read_only_modes));
647       break;
648    }
649 
650    case nir_intrinsic_load_ubo_vec4: {
651       int bit_size = nir_dest_bit_size(instr->dest);
652       validate_assert(state, bit_size >= 8);
653       validate_assert(state, (nir_intrinsic_component(instr) +
654                               instr->num_components) * (bit_size / 8) <= 16);
655       break;
656    }
657 
658    case nir_intrinsic_load_ubo:
659       /* Make sure that the creator didn't forget to set the range_base+range. */
660       validate_assert(state, nir_intrinsic_range(instr) != 0);
661       FALLTHROUGH;
662    case nir_intrinsic_load_ssbo:
663    case nir_intrinsic_load_shared:
664    case nir_intrinsic_load_global:
665    case nir_intrinsic_load_global_constant:
666    case nir_intrinsic_load_scratch:
667    case nir_intrinsic_load_constant:
668       /* These memory load operations must have alignments */
669       validate_assert(state,
670          util_is_power_of_two_nonzero(nir_intrinsic_align_mul(instr)));
671       validate_assert(state, nir_intrinsic_align_offset(instr) <
672                              nir_intrinsic_align_mul(instr));
673       FALLTHROUGH;
674 
675    case nir_intrinsic_load_uniform:
676    case nir_intrinsic_load_input:
677    case nir_intrinsic_load_per_vertex_input:
678    case nir_intrinsic_load_interpolated_input:
679    case nir_intrinsic_load_output:
680    case nir_intrinsic_load_per_vertex_output:
681    case nir_intrinsic_load_per_primitive_output:
682    case nir_intrinsic_load_push_constant:
683       /* All memory load operations must load at least a byte */
684       validate_assert(state, nir_dest_bit_size(instr->dest) >= 8);
685       break;
686 
687    case nir_intrinsic_store_ssbo:
688    case nir_intrinsic_store_shared:
689    case nir_intrinsic_store_global:
690    case nir_intrinsic_store_scratch:
691       /* These memory store operations must also have alignments */
692       validate_assert(state,
693          util_is_power_of_two_nonzero(nir_intrinsic_align_mul(instr)));
694       validate_assert(state, nir_intrinsic_align_offset(instr) <
695                              nir_intrinsic_align_mul(instr));
696       FALLTHROUGH;
697 
698    case nir_intrinsic_store_output:
699    case nir_intrinsic_store_per_vertex_output:
700       /* All memory store operations must store at least a byte */
701       validate_assert(state, nir_src_bit_size(instr->src[0]) >= 8);
702       break;
703 
704    case nir_intrinsic_deref_mode_is:
705    case nir_intrinsic_addr_mode_is:
706       validate_assert(state,
707          util_bitcount(nir_intrinsic_memory_modes(instr)) == 1);
708       break;
709 
710    case nir_intrinsic_image_deref_atomic_add:
711    case nir_intrinsic_image_deref_atomic_imin:
712    case nir_intrinsic_image_deref_atomic_umin:
713    case nir_intrinsic_image_deref_atomic_imax:
714    case nir_intrinsic_image_deref_atomic_umax:
715    case nir_intrinsic_image_deref_atomic_and:
716    case nir_intrinsic_image_deref_atomic_or:
717    case nir_intrinsic_image_deref_atomic_xor:
718    case nir_intrinsic_image_deref_atomic_comp_swap:
719    case nir_intrinsic_image_atomic_add:
720    case nir_intrinsic_image_atomic_imin:
721    case nir_intrinsic_image_atomic_umin:
722    case nir_intrinsic_image_atomic_imax:
723    case nir_intrinsic_image_atomic_umax:
724    case nir_intrinsic_image_atomic_and:
725    case nir_intrinsic_image_atomic_or:
726    case nir_intrinsic_image_atomic_xor:
727    case nir_intrinsic_image_atomic_comp_swap:
728    case nir_intrinsic_bindless_image_atomic_add:
729    case nir_intrinsic_bindless_image_atomic_imin:
730    case nir_intrinsic_bindless_image_atomic_umin:
731    case nir_intrinsic_bindless_image_atomic_imax:
732    case nir_intrinsic_bindless_image_atomic_umax:
733    case nir_intrinsic_bindless_image_atomic_and:
734    case nir_intrinsic_bindless_image_atomic_or:
735    case nir_intrinsic_bindless_image_atomic_xor:
736    case nir_intrinsic_bindless_image_atomic_comp_swap: {
737       enum pipe_format format = image_intrin_format(instr);
738       if (format != PIPE_FORMAT_COUNT) {
739          validate_assert(state, format == PIPE_FORMAT_R32_UINT ||
740                                 format == PIPE_FORMAT_R32_SINT ||
741                                 format == PIPE_FORMAT_R64_UINT ||
742                                 format == PIPE_FORMAT_R64_SINT);
743          validate_assert(state, nir_dest_bit_size(instr->dest) ==
744                                 util_format_get_blocksizebits(format));
745       }
746       break;
747    }
748 
749    case nir_intrinsic_image_deref_atomic_exchange:
750    case nir_intrinsic_image_atomic_exchange:
751    case nir_intrinsic_bindless_image_atomic_exchange: {
752       enum pipe_format format = image_intrin_format(instr);
753       if (format != PIPE_FORMAT_COUNT) {
754          validate_assert(state, format == PIPE_FORMAT_R32_UINT ||
755                                 format == PIPE_FORMAT_R32_SINT ||
756                                 format == PIPE_FORMAT_R32_FLOAT ||
757                                 format == PIPE_FORMAT_R64_UINT ||
758                                 format == PIPE_FORMAT_R64_SINT);
759          validate_assert(state, nir_dest_bit_size(instr->dest) ==
760                                 util_format_get_blocksizebits(format));
761       }
762       break;
763    }
764 
765    case nir_intrinsic_image_deref_atomic_fadd:
766    case nir_intrinsic_image_atomic_fadd:
767    case nir_intrinsic_bindless_image_atomic_fadd: {
768       enum pipe_format format = image_intrin_format(instr);
769       validate_assert(state, format == PIPE_FORMAT_COUNT ||
770                              format == PIPE_FORMAT_R32_FLOAT);
771       validate_assert(state, nir_dest_bit_size(instr->dest) == 32);
772       break;
773    }
774 
775    case nir_intrinsic_image_deref_atomic_fmin:
776    case nir_intrinsic_image_deref_atomic_fmax:
777    case nir_intrinsic_image_atomic_fmin:
778    case nir_intrinsic_image_atomic_fmax:
779    case nir_intrinsic_bindless_image_atomic_fmin:
780    case nir_intrinsic_bindless_image_atomic_fmax: {
781       enum pipe_format format = image_intrin_format(instr);
782       validate_assert(state, format == PIPE_FORMAT_COUNT ||
783                              format == PIPE_FORMAT_R16_FLOAT ||
784                              format == PIPE_FORMAT_R32_FLOAT ||
785                              format == PIPE_FORMAT_R64_FLOAT);
786       validate_assert(state, nir_dest_bit_size(instr->dest) ==
787                              util_format_get_blocksizebits(format));
788       break;
789    }
790 
791    default:
792       break;
793    }
794 
795    if (instr->num_components > 0)
796       validate_num_components(state, instr->num_components);
797 
798    const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
799    unsigned num_srcs = info->num_srcs;
800    for (unsigned i = 0; i < num_srcs; i++) {
801       unsigned components_read = nir_intrinsic_src_components(instr, i);
802 
803       validate_num_components(state, components_read);
804 
805       validate_src(&instr->src[i], state, src_bit_sizes[i], components_read);
806    }
807 
808    if (nir_intrinsic_infos[instr->intrinsic].has_dest) {
809       unsigned components_written = nir_intrinsic_dest_components(instr);
810       unsigned bit_sizes = info->dest_bit_sizes;
811       if (!bit_sizes && info->bit_size_src >= 0)
812          bit_sizes = nir_src_bit_size(instr->src[info->bit_size_src]);
813 
814       validate_num_components(state, components_written);
815       if (dest_bit_size && bit_sizes)
816          validate_assert(state, dest_bit_size & bit_sizes);
817       else
818          dest_bit_size = dest_bit_size ? dest_bit_size : bit_sizes;
819 
820       validate_dest(&instr->dest, state, dest_bit_size, components_written);
821    }
822 
823    if (!vectorized_intrinsic(instr))
824       validate_assert(state, instr->num_components == 0);
825 }
826 
827 static void
validate_tex_instr(nir_tex_instr * instr,validate_state * state)828 validate_tex_instr(nir_tex_instr *instr, validate_state *state)
829 {
830    bool src_type_seen[nir_num_tex_src_types];
831    for (unsigned i = 0; i < nir_num_tex_src_types; i++)
832       src_type_seen[i] = false;
833 
834    for (unsigned i = 0; i < instr->num_srcs; i++) {
835       validate_assert(state, !src_type_seen[instr->src[i].src_type]);
836       src_type_seen[instr->src[i].src_type] = true;
837       validate_src(&instr->src[i].src, state,
838                    0, nir_tex_instr_src_size(instr, i));
839 
840       switch (instr->src[i].src_type) {
841       case nir_tex_src_coord:
842          validate_assert(state, nir_src_num_components(instr->src[i].src) ==
843                                 instr->coord_components);
844          break;
845 
846       case nir_tex_src_projector:
847          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
848          break;
849 
850       case nir_tex_src_comparator:
851          validate_assert(state, instr->is_shadow);
852          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
853          break;
854 
855       case nir_tex_src_offset:
856          validate_assert(state, nir_src_num_components(instr->src[i].src) ==
857                                 instr->coord_components - instr->is_array);
858          break;
859 
860       case nir_tex_src_bias:
861          validate_assert(state, instr->op == nir_texop_txb ||
862                                 instr->op == nir_texop_tg4);
863          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
864          break;
865 
866       case nir_tex_src_lod:
867          validate_assert(state, instr->op != nir_texop_tex &&
868                                 instr->op != nir_texop_txb &&
869                                 instr->op != nir_texop_txd &&
870                                 instr->op != nir_texop_lod);
871          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
872          break;
873 
874       case nir_tex_src_min_lod:
875       case nir_tex_src_ms_index:
876          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
877          break;
878 
879       case nir_tex_src_ddx:
880       case nir_tex_src_ddy:
881          validate_assert(state, instr->op == nir_texop_txd);
882          validate_assert(state, nir_src_num_components(instr->src[i].src) ==
883                                 instr->coord_components - instr->is_array);
884          break;
885 
886       case nir_tex_src_texture_deref: {
887          nir_deref_instr *deref = nir_src_as_deref(instr->src[i].src);
888          if (!validate_assert(state, deref))
889             break;
890 
891          validate_assert(state, glsl_type_is_image(deref->type) ||
892                                 glsl_type_is_sampler(deref->type));
893          break;
894       }
895 
896       case nir_tex_src_sampler_deref: {
897          nir_deref_instr *deref = nir_src_as_deref(instr->src[i].src);
898          if (!validate_assert(state, deref))
899             break;
900 
901          validate_assert(state, glsl_type_is_sampler(deref->type));
902          break;
903       }
904 
905       case nir_tex_src_texture_offset:
906       case nir_tex_src_sampler_offset:
907       case nir_tex_src_plane:
908          validate_assert(state, nir_src_num_components(instr->src[i].src) == 1);
909          break;
910 
911       case nir_tex_src_texture_handle:
912       case nir_tex_src_sampler_handle:
913          break;
914 
915       default:
916          break;
917       }
918    }
919 
920    if (instr->op != nir_texop_tg4)
921       validate_assert(state, instr->component == 0);
922 
923    if (nir_tex_instr_has_explicit_tg4_offsets(instr)) {
924       validate_assert(state, instr->op == nir_texop_tg4);
925       validate_assert(state, !src_type_seen[nir_tex_src_offset]);
926    }
927 
928    validate_dest(&instr->dest, state, 0, nir_tex_instr_dest_size(instr));
929 
930    validate_assert(state,
931                    nir_alu_type_get_type_size(instr->dest_type) ==
932                    nir_dest_bit_size(instr->dest));
933 }
934 
935 static void
validate_call_instr(nir_call_instr * instr,validate_state * state)936 validate_call_instr(nir_call_instr *instr, validate_state *state)
937 {
938    validate_assert(state, instr->num_params == instr->callee->num_params);
939 
940    for (unsigned i = 0; i < instr->num_params; i++) {
941       validate_src(&instr->params[i], state,
942                    instr->callee->params[i].bit_size,
943                    instr->callee->params[i].num_components);
944    }
945 }
946 
947 static void
validate_const_value(nir_const_value * val,unsigned bit_size,validate_state * state)948 validate_const_value(nir_const_value *val, unsigned bit_size,
949                      validate_state *state)
950 {
951    /* In order for block copies to work properly for things like instruction
952     * comparisons and [de]serialization, we require the unused bits of the
953     * nir_const_value to be zero.
954     */
955    nir_const_value cmp_val;
956    memset(&cmp_val, 0, sizeof(cmp_val));
957    switch (bit_size) {
958    case 1:
959       cmp_val.b = val->b;
960       break;
961    case 8:
962       cmp_val.u8 = val->u8;
963       break;
964    case 16:
965       cmp_val.u16 = val->u16;
966       break;
967    case 32:
968       cmp_val.u32 = val->u32;
969       break;
970    case 64:
971       cmp_val.u64 = val->u64;
972       break;
973    default:
974       validate_assert(state, !"Invalid load_const bit size");
975    }
976    validate_assert(state, memcmp(val, &cmp_val, sizeof(cmp_val)) == 0);
977 }
978 
979 static void
validate_load_const_instr(nir_load_const_instr * instr,validate_state * state)980 validate_load_const_instr(nir_load_const_instr *instr, validate_state *state)
981 {
982    validate_ssa_def(&instr->def, state);
983 
984    for (unsigned i = 0; i < instr->def.num_components; i++)
985       validate_const_value(&instr->value[i], instr->def.bit_size, state);
986 }
987 
988 static void
validate_ssa_undef_instr(nir_ssa_undef_instr * instr,validate_state * state)989 validate_ssa_undef_instr(nir_ssa_undef_instr *instr, validate_state *state)
990 {
991    validate_ssa_def(&instr->def, state);
992 }
993 
994 static void
validate_phi_instr(nir_phi_instr * instr,validate_state * state)995 validate_phi_instr(nir_phi_instr *instr, validate_state *state)
996 {
997    /*
998     * don't validate the sources until we get to them from their predecessor
999     * basic blocks, to avoid validating an SSA use before its definition.
1000     */
1001 
1002    validate_dest(&instr->dest, state, 0, 0);
1003 
1004    exec_list_validate(&instr->srcs);
1005    validate_assert(state, exec_list_length(&instr->srcs) ==
1006           state->block->predecessors->entries);
1007 }
1008 
1009 static void
validate_jump_instr(nir_jump_instr * instr,validate_state * state)1010 validate_jump_instr(nir_jump_instr *instr, validate_state *state)
1011 {
1012    nir_block *block = state->block;
1013    validate_assert(state, &instr->instr == nir_block_last_instr(block));
1014 
1015    switch (instr->type) {
1016    case nir_jump_return:
1017    case nir_jump_halt:
1018       validate_assert(state, block->successors[0] == state->impl->end_block);
1019       validate_assert(state, block->successors[1] == NULL);
1020       validate_assert(state, instr->target == NULL);
1021       validate_assert(state, instr->else_target == NULL);
1022       break;
1023 
1024    case nir_jump_break:
1025       validate_assert(state, state->impl->structured);
1026       validate_assert(state, state->loop != NULL);
1027       if (state->loop) {
1028          nir_block *after =
1029             nir_cf_node_as_block(nir_cf_node_next(&state->loop->cf_node));
1030          validate_assert(state, block->successors[0] == after);
1031       }
1032       validate_assert(state, block->successors[1] == NULL);
1033       validate_assert(state, instr->target == NULL);
1034       validate_assert(state, instr->else_target == NULL);
1035       break;
1036 
1037    case nir_jump_continue:
1038       validate_assert(state, state->impl->structured);
1039       validate_assert(state, state->loop != NULL);
1040       if (state->loop) {
1041          nir_block *first = nir_loop_first_block(state->loop);
1042          validate_assert(state, block->successors[0] == first);
1043       }
1044       validate_assert(state, block->successors[1] == NULL);
1045       validate_assert(state, instr->target == NULL);
1046       validate_assert(state, instr->else_target == NULL);
1047       break;
1048 
1049    case nir_jump_goto:
1050       validate_assert(state, !state->impl->structured);
1051       validate_assert(state, instr->target == block->successors[0]);
1052       validate_assert(state, instr->target != NULL);
1053       validate_assert(state, instr->else_target == NULL);
1054       break;
1055 
1056    case nir_jump_goto_if:
1057       validate_assert(state, !state->impl->structured);
1058       validate_assert(state, instr->target == block->successors[1]);
1059       validate_assert(state, instr->else_target == block->successors[0]);
1060       validate_src(&instr->condition, state, 0, 1);
1061       validate_assert(state, instr->target != NULL);
1062       validate_assert(state, instr->else_target != NULL);
1063       break;
1064 
1065    default:
1066       validate_assert(state, !"Invalid jump instruction type");
1067       break;
1068    }
1069 }
1070 
1071 static void
validate_instr(nir_instr * instr,validate_state * state)1072 validate_instr(nir_instr *instr, validate_state *state)
1073 {
1074    validate_assert(state, instr->block == state->block);
1075 
1076    state->instr = instr;
1077 
1078    validate_assert(state, _mesa_set_search(state->shader_gc_list, instr));
1079 
1080    switch (instr->type) {
1081    case nir_instr_type_alu:
1082       validate_alu_instr(nir_instr_as_alu(instr), state);
1083       break;
1084 
1085    case nir_instr_type_deref:
1086       validate_deref_instr(nir_instr_as_deref(instr), state);
1087       break;
1088 
1089    case nir_instr_type_call:
1090       validate_call_instr(nir_instr_as_call(instr), state);
1091       break;
1092 
1093    case nir_instr_type_intrinsic:
1094       validate_intrinsic_instr(nir_instr_as_intrinsic(instr), state);
1095       break;
1096 
1097    case nir_instr_type_tex:
1098       validate_tex_instr(nir_instr_as_tex(instr), state);
1099       break;
1100 
1101    case nir_instr_type_load_const:
1102       validate_load_const_instr(nir_instr_as_load_const(instr), state);
1103       break;
1104 
1105    case nir_instr_type_phi:
1106       validate_phi_instr(nir_instr_as_phi(instr), state);
1107       break;
1108 
1109    case nir_instr_type_ssa_undef:
1110       validate_ssa_undef_instr(nir_instr_as_ssa_undef(instr), state);
1111       break;
1112 
1113    case nir_instr_type_jump:
1114       validate_jump_instr(nir_instr_as_jump(instr), state);
1115       break;
1116 
1117    default:
1118       validate_assert(state, !"Invalid ALU instruction type");
1119       break;
1120    }
1121 
1122    state->instr = NULL;
1123 }
1124 
1125 static void
validate_phi_src(nir_phi_instr * instr,nir_block * pred,validate_state * state)1126 validate_phi_src(nir_phi_instr *instr, nir_block *pred, validate_state *state)
1127 {
1128    state->instr = &instr->instr;
1129 
1130    validate_assert(state, instr->dest.is_ssa);
1131 
1132    exec_list_validate(&instr->srcs);
1133    nir_foreach_phi_src(src, instr) {
1134       if (src->pred == pred) {
1135          validate_assert(state, src->src.is_ssa);
1136          validate_src(&src->src, state, instr->dest.ssa.bit_size,
1137                       instr->dest.ssa.num_components);
1138          state->instr = NULL;
1139          return;
1140       }
1141    }
1142    validate_assert(state, !"Phi does not have a source corresponding to one "
1143                            "of its predecessor blocks");
1144 }
1145 
1146 static void
validate_phi_srcs(nir_block * block,nir_block * succ,validate_state * state)1147 validate_phi_srcs(nir_block *block, nir_block *succ, validate_state *state)
1148 {
1149    nir_foreach_instr(instr, succ) {
1150       if (instr->type != nir_instr_type_phi)
1151          break;
1152 
1153       validate_phi_src(nir_instr_as_phi(instr), block, state);
1154    }
1155 }
1156 
1157 static void
collect_blocks(struct exec_list * cf_list,validate_state * state)1158 collect_blocks(struct exec_list *cf_list, validate_state *state)
1159 {
1160    /* We walk the blocks manually here rather than using nir_foreach_block for
1161     * a few reasons:
1162     *
1163     *  1. nir_foreach_block() doesn't work properly for unstructured NIR and
1164     *     we need to be able to handle all forms of NIR here.
1165     *
1166     *  2. We want to call exec_list_validate() on every linked list in the IR
1167     *     which means we need to touch every linked and just walking blocks
1168     *     with nir_foreach_block() would make that difficult.  In particular,
1169     *     we want to validate each list before the first time we walk it so
1170     *     that we catch broken lists in exec_list_validate() instead of
1171     *     getting stuck in a hard-to-debug infinite loop in the validator.
1172     *
1173     *  3. nir_foreach_block() depends on several invariants of the CF node
1174     *     hierarchy which nir_validate_shader() is responsible for verifying.
1175     *     If we used nir_foreach_block() in nir_validate_shader(), we could
1176     *     end up blowing up on a bad list walk instead of throwing the much
1177     *     easier to debug validation error.
1178     */
1179    exec_list_validate(cf_list);
1180    foreach_list_typed(nir_cf_node, node, node, cf_list) {
1181       switch (node->type) {
1182       case nir_cf_node_block:
1183          _mesa_set_add(state->blocks, nir_cf_node_as_block(node));
1184          break;
1185 
1186       case nir_cf_node_if:
1187          collect_blocks(&nir_cf_node_as_if(node)->then_list, state);
1188          collect_blocks(&nir_cf_node_as_if(node)->else_list, state);
1189          break;
1190 
1191       case nir_cf_node_loop:
1192          collect_blocks(&nir_cf_node_as_loop(node)->body, state);
1193          break;
1194 
1195       default:
1196          unreachable("Invalid CF node type");
1197       }
1198    }
1199 }
1200 
1201 static void validate_cf_node(nir_cf_node *node, validate_state *state);
1202 
1203 static void
validate_block_predecessors(nir_block * block,validate_state * state)1204 validate_block_predecessors(nir_block *block, validate_state *state)
1205 {
1206    for (unsigned i = 0; i < 2; i++) {
1207       if (block->successors[i] == NULL)
1208          continue;
1209 
1210       /* The block has to exist in the nir_function_impl */
1211       validate_assert(state, _mesa_set_search(state->blocks,
1212                                               block->successors[i]));
1213 
1214       /* And we have to be in our successor's predecessors set */
1215       validate_assert(state,
1216          _mesa_set_search(block->successors[i]->predecessors, block));
1217 
1218       validate_phi_srcs(block, block->successors[i], state);
1219    }
1220 
1221    /* The start block cannot have any predecessors */
1222    if (block == nir_start_block(state->impl))
1223       validate_assert(state, block->predecessors->entries == 0);
1224 
1225    set_foreach(block->predecessors, entry) {
1226       const nir_block *pred = entry->key;
1227       validate_assert(state, _mesa_set_search(state->blocks, pred));
1228       validate_assert(state, pred->successors[0] == block ||
1229                              pred->successors[1] == block);
1230    }
1231 }
1232 
1233 static void
validate_block(nir_block * block,validate_state * state)1234 validate_block(nir_block *block, validate_state *state)
1235 {
1236    validate_assert(state, block->cf_node.parent == state->parent_node);
1237 
1238    state->block = block;
1239 
1240    exec_list_validate(&block->instr_list);
1241    nir_foreach_instr(instr, block) {
1242       if (instr->type == nir_instr_type_phi) {
1243          validate_assert(state, instr == nir_block_first_instr(block) ||
1244                 nir_instr_prev(instr)->type == nir_instr_type_phi);
1245       }
1246 
1247       validate_instr(instr, state);
1248    }
1249 
1250    validate_assert(state, block->successors[0] != NULL);
1251    validate_assert(state, block->successors[0] != block->successors[1]);
1252    validate_block_predecessors(block, state);
1253 
1254    if (!state->impl->structured) {
1255       validate_assert(state, nir_block_ends_in_jump(block));
1256    } else if (!nir_block_ends_in_jump(block)) {
1257       nir_cf_node *next = nir_cf_node_next(&block->cf_node);
1258       if (next == NULL) {
1259          switch (state->parent_node->type) {
1260          case nir_cf_node_loop: {
1261             nir_block *first = nir_loop_first_block(state->loop);
1262             validate_assert(state, block->successors[0] == first);
1263             /* due to the hack for infinite loops, block->successors[1] may
1264              * point to the block after the loop.
1265              */
1266             break;
1267          }
1268 
1269          case nir_cf_node_if: {
1270             nir_block *after =
1271                nir_cf_node_as_block(nir_cf_node_next(state->parent_node));
1272             validate_assert(state, block->successors[0] == after);
1273             validate_assert(state, block->successors[1] == NULL);
1274             break;
1275          }
1276 
1277          case nir_cf_node_function:
1278             validate_assert(state, block->successors[0] == state->impl->end_block);
1279             validate_assert(state, block->successors[1] == NULL);
1280             break;
1281 
1282          default:
1283             unreachable("unknown control flow node type");
1284          }
1285       } else {
1286          if (next->type == nir_cf_node_if) {
1287             nir_if *if_stmt = nir_cf_node_as_if(next);
1288             validate_assert(state, block->successors[0] ==
1289                    nir_if_first_then_block(if_stmt));
1290             validate_assert(state, block->successors[1] ==
1291                    nir_if_first_else_block(if_stmt));
1292          } else if (next->type == nir_cf_node_loop) {
1293             nir_loop *loop = nir_cf_node_as_loop(next);
1294             validate_assert(state, block->successors[0] ==
1295                    nir_loop_first_block(loop));
1296             validate_assert(state, block->successors[1] == NULL);
1297          } else {
1298             validate_assert(state,
1299                !"Structured NIR cannot have consecutive blocks");
1300          }
1301       }
1302    }
1303 }
1304 
1305 
1306 static void
validate_end_block(nir_block * block,validate_state * state)1307 validate_end_block(nir_block *block, validate_state *state)
1308 {
1309    validate_assert(state, block->cf_node.parent == &state->impl->cf_node);
1310 
1311    exec_list_validate(&block->instr_list);
1312    validate_assert(state, exec_list_is_empty(&block->instr_list));
1313 
1314    validate_assert(state, block->successors[0] == NULL);
1315    validate_assert(state, block->successors[1] == NULL);
1316    validate_block_predecessors(block, state);
1317 }
1318 
1319 static void
validate_if(nir_if * if_stmt,validate_state * state)1320 validate_if(nir_if *if_stmt, validate_state *state)
1321 {
1322    validate_assert(state, state->impl->structured);
1323 
1324    state->if_stmt = if_stmt;
1325 
1326    validate_assert(state, !exec_node_is_head_sentinel(if_stmt->cf_node.node.prev));
1327    nir_cf_node *prev_node = nir_cf_node_prev(&if_stmt->cf_node);
1328    validate_assert(state, prev_node->type == nir_cf_node_block);
1329 
1330    validate_assert(state, !exec_node_is_tail_sentinel(if_stmt->cf_node.node.next));
1331    nir_cf_node *next_node = nir_cf_node_next(&if_stmt->cf_node);
1332    validate_assert(state, next_node->type == nir_cf_node_block);
1333 
1334    validate_src(&if_stmt->condition, state, 0, 1);
1335 
1336    validate_assert(state, !exec_list_is_empty(&if_stmt->then_list));
1337    validate_assert(state, !exec_list_is_empty(&if_stmt->else_list));
1338 
1339    nir_cf_node *old_parent = state->parent_node;
1340    state->parent_node = &if_stmt->cf_node;
1341 
1342    foreach_list_typed(nir_cf_node, cf_node, node, &if_stmt->then_list) {
1343       validate_cf_node(cf_node, state);
1344    }
1345 
1346    foreach_list_typed(nir_cf_node, cf_node, node, &if_stmt->else_list) {
1347       validate_cf_node(cf_node, state);
1348    }
1349 
1350    state->parent_node = old_parent;
1351    state->if_stmt = NULL;
1352 }
1353 
1354 static void
validate_loop(nir_loop * loop,validate_state * state)1355 validate_loop(nir_loop *loop, validate_state *state)
1356 {
1357    validate_assert(state, state->impl->structured);
1358 
1359    validate_assert(state, !exec_node_is_head_sentinel(loop->cf_node.node.prev));
1360    nir_cf_node *prev_node = nir_cf_node_prev(&loop->cf_node);
1361    validate_assert(state, prev_node->type == nir_cf_node_block);
1362 
1363    validate_assert(state, !exec_node_is_tail_sentinel(loop->cf_node.node.next));
1364    nir_cf_node *next_node = nir_cf_node_next(&loop->cf_node);
1365    validate_assert(state, next_node->type == nir_cf_node_block);
1366 
1367    validate_assert(state, !exec_list_is_empty(&loop->body));
1368 
1369    nir_cf_node *old_parent = state->parent_node;
1370    state->parent_node = &loop->cf_node;
1371    nir_loop *old_loop = state->loop;
1372    state->loop = loop;
1373 
1374    foreach_list_typed(nir_cf_node, cf_node, node, &loop->body) {
1375       validate_cf_node(cf_node, state);
1376    }
1377 
1378    state->parent_node = old_parent;
1379    state->loop = old_loop;
1380 }
1381 
1382 static void
validate_cf_node(nir_cf_node * node,validate_state * state)1383 validate_cf_node(nir_cf_node *node, validate_state *state)
1384 {
1385    validate_assert(state, node->parent == state->parent_node);
1386 
1387    switch (node->type) {
1388    case nir_cf_node_block:
1389       validate_block(nir_cf_node_as_block(node), state);
1390       break;
1391 
1392    case nir_cf_node_if:
1393       validate_if(nir_cf_node_as_if(node), state);
1394       break;
1395 
1396    case nir_cf_node_loop:
1397       validate_loop(nir_cf_node_as_loop(node), state);
1398       break;
1399 
1400    default:
1401       unreachable("Invalid CF node type");
1402    }
1403 }
1404 
1405 static void
prevalidate_reg_decl(nir_register * reg,validate_state * state)1406 prevalidate_reg_decl(nir_register *reg, validate_state *state)
1407 {
1408    validate_assert(state, reg->index < state->impl->reg_alloc);
1409    validate_assert(state, !BITSET_TEST(state->regs_found, reg->index));
1410    validate_num_components(state, reg->num_components);
1411    BITSET_SET(state->regs_found, reg->index);
1412 
1413    list_validate(&reg->uses);
1414    list_validate(&reg->defs);
1415    list_validate(&reg->if_uses);
1416 
1417    reg_validate_state *reg_state = ralloc(state->regs, reg_validate_state);
1418    reg_state->uses = _mesa_pointer_set_create(reg_state);
1419    reg_state->if_uses = _mesa_pointer_set_create(reg_state);
1420    reg_state->defs = _mesa_pointer_set_create(reg_state);
1421 
1422    reg_state->where_defined = state->impl;
1423 
1424    _mesa_hash_table_insert(state->regs, reg, reg_state);
1425 }
1426 
1427 static void
postvalidate_reg_decl(nir_register * reg,validate_state * state)1428 postvalidate_reg_decl(nir_register *reg, validate_state *state)
1429 {
1430    struct hash_entry *entry = _mesa_hash_table_search(state->regs, reg);
1431 
1432    assume(entry);
1433    reg_validate_state *reg_state = (reg_validate_state *) entry->data;
1434 
1435    nir_foreach_use(src, reg) {
1436       struct set_entry *entry = _mesa_set_search(reg_state->uses, src);
1437       validate_assert(state, entry);
1438       _mesa_set_remove(reg_state->uses, entry);
1439    }
1440    validate_assert(state, reg_state->uses->entries == 0);
1441 
1442    nir_foreach_if_use(src, reg) {
1443       struct set_entry *entry = _mesa_set_search(reg_state->if_uses, src);
1444       validate_assert(state, entry);
1445       _mesa_set_remove(reg_state->if_uses, entry);
1446    }
1447    validate_assert(state, reg_state->if_uses->entries == 0);
1448 
1449    nir_foreach_def(src, reg) {
1450       struct set_entry *entry = _mesa_set_search(reg_state->defs, src);
1451       validate_assert(state, entry);
1452       _mesa_set_remove(reg_state->defs, entry);
1453    }
1454    validate_assert(state, reg_state->defs->entries == 0);
1455 }
1456 
1457 static void
validate_constant(nir_constant * c,const struct glsl_type * type,validate_state * state)1458 validate_constant(nir_constant *c, const struct glsl_type *type,
1459                   validate_state *state)
1460 {
1461    if (glsl_type_is_vector_or_scalar(type)) {
1462       unsigned num_components = glsl_get_vector_elements(type);
1463       unsigned bit_size = glsl_get_bit_size(type);
1464       for (unsigned i = 0; i < num_components; i++)
1465          validate_const_value(&c->values[i], bit_size, state);
1466       for (unsigned i = num_components; i < NIR_MAX_VEC_COMPONENTS; i++)
1467          validate_assert(state, c->values[i].u64 == 0);
1468    } else {
1469       validate_assert(state, c->num_elements == glsl_get_length(type));
1470       if (glsl_type_is_struct_or_ifc(type)) {
1471          for (unsigned i = 0; i < c->num_elements; i++) {
1472             const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
1473             validate_constant(c->elements[i], elem_type, state);
1474          }
1475       } else if (glsl_type_is_array_or_matrix(type)) {
1476          const struct glsl_type *elem_type = glsl_get_array_element(type);
1477          for (unsigned i = 0; i < c->num_elements; i++)
1478             validate_constant(c->elements[i], elem_type, state);
1479       } else {
1480          validate_assert(state, !"Invalid type for nir_constant");
1481       }
1482    }
1483 }
1484 
1485 static void
validate_var_decl(nir_variable * var,nir_variable_mode valid_modes,validate_state * state)1486 validate_var_decl(nir_variable *var, nir_variable_mode valid_modes,
1487                   validate_state *state)
1488 {
1489    state->var = var;
1490 
1491    /* Must have exactly one mode set */
1492    validate_assert(state, util_is_power_of_two_nonzero(var->data.mode));
1493    validate_assert(state, var->data.mode & valid_modes);
1494 
1495    if (var->data.compact) {
1496       /* The "compact" flag is only valid on arrays of scalars. */
1497       assert(glsl_type_is_array(var->type));
1498 
1499       const struct glsl_type *type = glsl_get_array_element(var->type);
1500       if (nir_is_arrayed_io(var, state->shader->info.stage)) {
1501          assert(glsl_type_is_array(type));
1502          assert(glsl_type_is_scalar(glsl_get_array_element(type)));
1503       } else {
1504          assert(glsl_type_is_scalar(type));
1505       }
1506    }
1507 
1508    if (var->num_members > 0) {
1509       const struct glsl_type *without_array = glsl_without_array(var->type);
1510       validate_assert(state, glsl_type_is_struct_or_ifc(without_array));
1511       validate_assert(state, var->num_members == glsl_get_length(without_array));
1512       validate_assert(state, var->members != NULL);
1513    }
1514 
1515    if (var->data.per_view)
1516       validate_assert(state, glsl_type_is_array(var->type));
1517 
1518    if (var->constant_initializer)
1519       validate_constant(var->constant_initializer, var->type, state);
1520 
1521    /*
1522     * TODO validate some things ir_validate.cpp does (requires more GLSL type
1523     * support)
1524     */
1525 
1526    _mesa_hash_table_insert(state->var_defs, var,
1527                            valid_modes == nir_var_function_temp ?
1528                            state->impl : NULL);
1529 
1530    state->var = NULL;
1531 }
1532 
1533 static bool
validate_ssa_def_dominance(nir_ssa_def * def,void * _state)1534 validate_ssa_def_dominance(nir_ssa_def *def, void *_state)
1535 {
1536    validate_state *state = _state;
1537 
1538    validate_assert(state, def->index < state->impl->ssa_alloc);
1539    validate_assert(state, !BITSET_TEST(state->ssa_defs_found, def->index));
1540    BITSET_SET(state->ssa_defs_found, def->index);
1541 
1542    return true;
1543 }
1544 
1545 static bool
validate_src_dominance(nir_src * src,void * _state)1546 validate_src_dominance(nir_src *src, void *_state)
1547 {
1548    validate_state *state = _state;
1549    if (!src->is_ssa)
1550       return true;
1551 
1552    if (src->ssa->parent_instr->block == src->parent_instr->block) {
1553       validate_assert(state, src->ssa->index < state->impl->ssa_alloc);
1554       validate_assert(state, BITSET_TEST(state->ssa_defs_found,
1555                                          src->ssa->index));
1556    } else {
1557       validate_assert(state, nir_block_dominates(src->ssa->parent_instr->block,
1558                                                  src->parent_instr->block));
1559    }
1560    return true;
1561 }
1562 
1563 static void
validate_ssa_dominance(nir_function_impl * impl,validate_state * state)1564 validate_ssa_dominance(nir_function_impl *impl, validate_state *state)
1565 {
1566    nir_metadata_require(impl, nir_metadata_dominance);
1567 
1568    nir_foreach_block(block, impl) {
1569       state->block = block;
1570       nir_foreach_instr(instr, block) {
1571          state->instr = instr;
1572          if (instr->type == nir_instr_type_phi) {
1573             nir_phi_instr *phi = nir_instr_as_phi(instr);
1574             nir_foreach_phi_src(src, phi) {
1575                validate_assert(state,
1576                   nir_block_dominates(src->src.ssa->parent_instr->block,
1577                                       src->pred));
1578             }
1579          } else {
1580             nir_foreach_src(instr, validate_src_dominance, state);
1581          }
1582          nir_foreach_ssa_def(instr, validate_ssa_def_dominance, state);
1583       }
1584    }
1585 }
1586 
1587 static void
validate_function_impl(nir_function_impl * impl,validate_state * state)1588 validate_function_impl(nir_function_impl *impl, validate_state *state)
1589 {
1590    /* Resize the ssa_srcs set.  It's likely that the size of this set will
1591     * never actually hit the number of SSA defs because we remove sources from
1592     * the set as we visit them.  (It could actually be much larger because
1593     * each SSA def can be used more than once.)  However, growing it now costs
1594     * us very little (the extra memory is already dwarfed by the SSA defs
1595     * themselves) and makes collisions much less likely.
1596     */
1597    _mesa_set_resize(state->ssa_srcs, impl->ssa_alloc);
1598 
1599    validate_assert(state, impl->function->impl == impl);
1600    validate_assert(state, impl->cf_node.parent == NULL);
1601 
1602    validate_assert(state, exec_list_is_empty(&impl->end_block->instr_list));
1603    validate_assert(state, impl->end_block->successors[0] == NULL);
1604    validate_assert(state, impl->end_block->successors[1] == NULL);
1605 
1606    state->impl = impl;
1607    state->parent_node = &impl->cf_node;
1608 
1609    exec_list_validate(&impl->locals);
1610    nir_foreach_function_temp_variable(var, impl) {
1611       validate_var_decl(var, nir_var_function_temp, state);
1612    }
1613 
1614    state->regs_found = reralloc(state->mem_ctx, state->regs_found,
1615                                 BITSET_WORD, BITSET_WORDS(impl->reg_alloc));
1616    memset(state->regs_found, 0, BITSET_WORDS(impl->reg_alloc) *
1617                                 sizeof(BITSET_WORD));
1618    exec_list_validate(&impl->registers);
1619    foreach_list_typed(nir_register, reg, node, &impl->registers) {
1620       prevalidate_reg_decl(reg, state);
1621    }
1622 
1623    state->ssa_defs_found = reralloc(state->mem_ctx, state->ssa_defs_found,
1624                                     BITSET_WORD, BITSET_WORDS(impl->ssa_alloc));
1625    memset(state->ssa_defs_found, 0, BITSET_WORDS(impl->ssa_alloc) *
1626                                     sizeof(BITSET_WORD));
1627 
1628    _mesa_set_clear(state->blocks, NULL);
1629    _mesa_set_resize(state->blocks, impl->num_blocks);
1630    collect_blocks(&impl->body, state);
1631    _mesa_set_add(state->blocks, impl->end_block);
1632    validate_assert(state, !exec_list_is_empty(&impl->body));
1633    foreach_list_typed(nir_cf_node, node, node, &impl->body) {
1634       validate_cf_node(node, state);
1635    }
1636    validate_end_block(impl->end_block, state);
1637 
1638    foreach_list_typed(nir_register, reg, node, &impl->registers) {
1639       postvalidate_reg_decl(reg, state);
1640    }
1641 
1642    validate_assert(state, state->ssa_srcs->entries == 0);
1643    _mesa_set_clear(state->ssa_srcs, NULL);
1644 
1645    static int validate_dominance = -1;
1646    if (validate_dominance < 0) {
1647       validate_dominance =
1648          env_var_as_boolean("NIR_VALIDATE_SSA_DOMINANCE", false);
1649    }
1650    if (validate_dominance)
1651       validate_ssa_dominance(impl, state);
1652 }
1653 
1654 static void
validate_function(nir_function * func,validate_state * state)1655 validate_function(nir_function *func, validate_state *state)
1656 {
1657    if (func->impl != NULL) {
1658       validate_assert(state, func->impl->function == func);
1659       validate_function_impl(func->impl, state);
1660    }
1661 }
1662 
1663 static void
init_validate_state(validate_state * state)1664 init_validate_state(validate_state *state)
1665 {
1666    state->mem_ctx = ralloc_context(NULL);
1667    state->regs = _mesa_pointer_hash_table_create(state->mem_ctx);
1668    state->ssa_srcs = _mesa_pointer_set_create(state->mem_ctx);
1669    state->ssa_defs_found = NULL;
1670    state->regs_found = NULL;
1671    state->blocks = _mesa_pointer_set_create(state->mem_ctx);
1672    state->var_defs = _mesa_pointer_hash_table_create(state->mem_ctx);
1673    state->errors = _mesa_pointer_hash_table_create(state->mem_ctx);
1674    state->shader_gc_list = _mesa_pointer_set_create(state->mem_ctx);
1675 
1676    state->loop = NULL;
1677    state->instr = NULL;
1678    state->var = NULL;
1679 }
1680 
1681 static void
destroy_validate_state(validate_state * state)1682 destroy_validate_state(validate_state *state)
1683 {
1684    ralloc_free(state->mem_ctx);
1685 }
1686 
1687 mtx_t fail_dump_mutex = _MTX_INITIALIZER_NP;
1688 
1689 static void
dump_errors(validate_state * state,const char * when)1690 dump_errors(validate_state *state, const char *when)
1691 {
1692    struct hash_table *errors = state->errors;
1693 
1694    /* Lock around dumping so that we get clean dumps in a multi-threaded
1695     * scenario
1696     */
1697    mtx_lock(&fail_dump_mutex);
1698 
1699    if (when) {
1700       fprintf(stderr, "NIR validation failed %s\n", when);
1701       fprintf(stderr, "%d errors:\n", _mesa_hash_table_num_entries(errors));
1702    } else {
1703       fprintf(stderr, "NIR validation failed with %d errors:\n",
1704               _mesa_hash_table_num_entries(errors));
1705    }
1706 
1707    nir_print_shader_annotated(state->shader, stderr, errors);
1708 
1709    if (_mesa_hash_table_num_entries(errors) > 0) {
1710       fprintf(stderr, "%d additional errors:\n",
1711               _mesa_hash_table_num_entries(errors));
1712       hash_table_foreach(errors, entry) {
1713          fprintf(stderr, "%s\n", (char *)entry->data);
1714       }
1715    }
1716 
1717    mtx_unlock(&fail_dump_mutex);
1718 
1719    abort();
1720 }
1721 
1722 void
nir_validate_shader(nir_shader * shader,const char * when)1723 nir_validate_shader(nir_shader *shader, const char *when)
1724 {
1725    static int should_validate = -1;
1726    if (should_validate < 0)
1727       should_validate = env_var_as_boolean("NIR_VALIDATE", true);
1728    if (!should_validate)
1729       return;
1730 
1731    validate_state state;
1732    init_validate_state(&state);
1733 
1734    list_for_each_entry(nir_instr, instr, &shader->gc_list, gc_node) {
1735       _mesa_set_add(state.shader_gc_list, instr);
1736    }
1737 
1738    state.shader = shader;
1739 
1740    nir_variable_mode valid_modes =
1741       nir_var_shader_in |
1742       nir_var_shader_out |
1743       nir_var_shader_temp |
1744       nir_var_uniform |
1745       nir_var_mem_ubo |
1746       nir_var_system_value |
1747       nir_var_mem_ssbo |
1748       nir_var_mem_shared |
1749       nir_var_mem_push_const |
1750       nir_var_mem_constant;
1751 
1752    if (gl_shader_stage_is_callable(shader->info.stage))
1753       valid_modes |= nir_var_shader_call_data;
1754 
1755    if (shader->info.stage == MESA_SHADER_ANY_HIT ||
1756        shader->info.stage == MESA_SHADER_CLOSEST_HIT ||
1757        shader->info.stage == MESA_SHADER_INTERSECTION)
1758       valid_modes |= nir_var_ray_hit_attrib;
1759 
1760    exec_list_validate(&shader->variables);
1761    nir_foreach_variable_in_shader(var, shader)
1762      validate_var_decl(var, valid_modes, &state);
1763 
1764    exec_list_validate(&shader->functions);
1765    foreach_list_typed(nir_function, func, node, &shader->functions) {
1766       validate_function(func, &state);
1767    }
1768 
1769    if (_mesa_hash_table_num_entries(state.errors) > 0)
1770       dump_errors(&state, when);
1771 
1772    destroy_validate_state(&state);
1773 }
1774 
1775 void
nir_validate_ssa_dominance(nir_shader * shader,const char * when)1776 nir_validate_ssa_dominance(nir_shader *shader, const char *when)
1777 {
1778    static int should_validate = -1;
1779    if (should_validate < 0)
1780       should_validate = env_var_as_boolean("NIR_VALIDATE", true);
1781    if (!should_validate)
1782       return;
1783 
1784    validate_state state;
1785    init_validate_state(&state);
1786 
1787    state.shader = shader;
1788 
1789    nir_foreach_function(func, shader) {
1790       if (func->impl == NULL)
1791          continue;
1792 
1793       state.ssa_defs_found = reralloc(state.mem_ctx, state.ssa_defs_found,
1794                                       BITSET_WORD,
1795                                       BITSET_WORDS(func->impl->ssa_alloc));
1796       memset(state.ssa_defs_found, 0, BITSET_WORDS(func->impl->ssa_alloc) *
1797                                       sizeof(BITSET_WORD));
1798 
1799       state.impl = func->impl;
1800       validate_ssa_dominance(func->impl, &state);
1801    }
1802 
1803    if (_mesa_hash_table_num_entries(state.errors) > 0)
1804       dump_errors(&state, when);
1805 
1806    destroy_validate_state(&state);
1807 }
1808 
1809 #endif /* NDEBUG */
1810