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