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