nir/validate: Print when the validation failed

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
diff --git a/src/amd/vulkan/radv_shader.c b/src/amd/vulkan/radv_shader.c
index 15c9de1..edeaefb 100644
--- a/src/amd/vulkan/radv_shader.c
+++ b/src/amd/vulkan/radv_shader.c
@@ -189,7 +189,7 @@
 		 * and just use the NIR shader */
 		nir = module->nir;
 		nir->options = &nir_options;
-		nir_validate_shader(nir);
+		nir_validate_shader(nir, "in internal shader");
 
 		assert(exec_list_length(&nir->functions) == 1);
 		struct exec_node *node = exec_list_get_head(&nir->functions);
@@ -251,7 +251,7 @@
 					   &spirv_options, &nir_options);
 		nir = entry_point->shader;
 		assert(nir->info.stage == stage);
-		nir_validate_shader(nir);
+		nir_validate_shader(nir, "after spirv_to_nir");
 
 		free(spec_entries);
 
diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
index 60ea4fb..a0ae9a4 100644
--- a/src/compiler/nir/nir.h
+++ b/src/compiler/nir/nir.h
@@ -2664,7 +2664,7 @@
 nir_shader *nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s);
 
 #ifndef NDEBUG
-void nir_validate_shader(nir_shader *shader);
+void nir_validate_shader(nir_shader *shader, const char *when);
 void nir_metadata_set_validation_flag(nir_shader *shader);
 void nir_metadata_check_validation_flag(nir_shader *shader);
 
@@ -2698,7 +2698,7 @@
    return should_print;
 }
 #else
-static inline void nir_validate_shader(nir_shader *shader) { (void) shader; }
+static inline void nir_validate_shader(nir_shader *shader, const char *when) { (void) shader; (void)when; }
 static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
 static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
 static inline bool should_clone_nir(void) { return false; }
@@ -2706,9 +2706,9 @@
 static inline bool should_print_nir(void) { return false; }
 #endif /* NDEBUG */
 
-#define _PASS(nir, do_pass) do {                                     \
+#define _PASS(pass, nir, do_pass) do {                               \
    do_pass                                                           \
-   nir_validate_shader(nir);                                         \
+   nir_validate_shader(nir, "after " #pass);                         \
    if (should_clone_nir()) {                                         \
       nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
       ralloc_free(nir);                                              \
@@ -2720,7 +2720,7 @@
    }                                                                 \
 } while (0)
 
-#define NIR_PASS(progress, nir, pass, ...) _PASS(nir,                \
+#define NIR_PASS(progress, nir, pass, ...) _PASS(pass, nir,          \
    nir_metadata_set_validation_flag(nir);                            \
    if (should_print_nir())                                           \
       printf("%s\n", #pass);                                         \
@@ -2732,7 +2732,7 @@
    }                                                                 \
 )
 
-#define NIR_PASS_V(nir, pass, ...) _PASS(nir,                        \
+#define NIR_PASS_V(nir, pass, ...) _PASS(pass, nir,                  \
    if (should_print_nir())                                           \
       printf("%s\n", #pass);                                         \
    pass(nir, ##__VA_ARGS__);                                         \
diff --git a/src/compiler/nir/nir_validate.c b/src/compiler/nir/nir_validate.c
index cf7ef91..62893ca 100644
--- a/src/compiler/nir/nir_validate.c
+++ b/src/compiler/nir/nir_validate.c
@@ -1140,11 +1140,17 @@
 }
 
 static void
-dump_errors(validate_state *state)
+dump_errors(validate_state *state, const char *when)
 {
    struct hash_table *errors = state->errors;
 
-   fprintf(stderr, "%d errors:\n", _mesa_hash_table_num_entries(errors));
+   if (when) {
+      fprintf(stderr, "NIR validation failed %s\n", when);
+      fprintf(stderr, "%d errors:\n", _mesa_hash_table_num_entries(errors));
+   } else {
+      fprintf(stderr, "NIR validation failed with %d errors:\n",
+              _mesa_hash_table_num_entries(errors));
+   }
 
    nir_print_shader_annotated(state->shader, stderr, errors);
 
@@ -1160,7 +1166,7 @@
 }
 
 void
-nir_validate_shader(nir_shader *shader)
+nir_validate_shader(nir_shader *shader, const char *when)
 {
    static int should_validate = -1;
    if (should_validate < 0)
@@ -1223,7 +1229,7 @@
    }
 
    if (_mesa_hash_table_num_entries(state.errors) > 0)
-      dump_errors(&state);
+      dump_errors(&state, when);
 
    destroy_validate_state(&state);
 }
diff --git a/src/compiler/nir/tests/vars_tests.cpp b/src/compiler/nir/tests/vars_tests.cpp
index a7bbeb0..32763d2 100644
--- a/src/compiler/nir/tests/vars_tests.cpp
+++ b/src/compiler/nir/tests/vars_tests.cpp
@@ -147,14 +147,14 @@
    nir_store_var(b, out[0], nir_load_var(b, in), 1);
    nir_store_var(b, out[1], nir_load_var(b, in), 1);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 2);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 1);
 }
@@ -173,14 +173,14 @@
 
    nir_store_var(b, out[1], nir_load_var(b, in), 1);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 2);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_load_deref), 1);
 }
@@ -210,7 +210,7 @@
    nir_store_var(b, out[1], nir_load_var(b, g[1]), 1);
    nir_store_var(b, out[2], nir_load_var(b, g[2]), 1);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
@@ -265,12 +265,12 @@
    nir_copy_var(b, temp, in);
    nir_copy_var(b, out, temp);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    nir_intrinsic_instr *copy = NULL;
    copy = find_next_intrinsic(nir_intrinsic_copy_deref, copy);
@@ -293,12 +293,12 @@
    nir_ssa_def *read_value = nir_load_var(b, v[0]);
    nir_store_var(b, v[1], read_value, mask);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_store_deref), 2);
 
@@ -324,12 +324,12 @@
    nir_ssa_def *read_value = nir_load_var(b, v[0]);
    nir_store_var(b, v[1], read_value, mask);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    /* Store to v[1] should use second_value directly. */
    nir_intrinsic_instr *store_to_v1 = NULL;
@@ -356,15 +356,15 @@
    nir_ssa_def *read_value = nir_load_var(b, v[0]);
    nir_store_var(b, v[1], read_value, 1 << 1);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    nir_opt_constant_folding(b->shader);
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    /* Store to v[1] should use first_value directly.  The write of
     * second_value did not overwrite the component it uses.
@@ -401,7 +401,7 @@
    nir_ssa_def *read_value = nir_load_var(b, v[0]);
    nir_store_var(b, v[1], read_value, 1 << 1);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    nir_print_shader(b->shader, stdout);
 
@@ -410,10 +410,10 @@
 
    nir_print_shader(b->shader, stdout);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    nir_opt_constant_folding(b->shader);
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    /* Store to v[1] should use first_value directly.  The write of
     * second_value did not overwrite the component it uses.
@@ -471,12 +471,12 @@
    nir_ssa_def *read_value = nir_load_var(b, v[0]);
    nir_store_var(b, v[1], read_value, mask);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    bool progress = nir_opt_copy_prop_vars(b->shader);
    EXPECT_TRUE(progress);
 
-   nir_validate_shader(b->shader);
+   nir_validate_shader(b->shader, NULL);
 
    ASSERT_EQ(count_intrinsics(nir_intrinsic_store_deref), 2);
 
diff --git a/src/intel/compiler/brw_nir.c b/src/intel/compiler/brw_nir.c
index 297845b..1cd5686 100644
--- a/src/intel/compiler/brw_nir.c
+++ b/src/intel/compiler/brw_nir.c
@@ -725,8 +725,8 @@
                      nir_shader **producer, nir_shader **consumer)
 {
    nir_lower_io_arrays_to_elements(*producer, *consumer);
-   nir_validate_shader(*producer);
-   nir_validate_shader(*consumer);
+   nir_validate_shader(*producer, "after nir_lower_io_arrays_to_elements");
+   nir_validate_shader(*consumer, "after nir_lower_io_arrays_to_elements");
 
    const bool p_is_scalar =
       compiler->scalar_stage[(*producer)->info.stage];
@@ -879,7 +879,7 @@
    tex_options.lower_xy_uxvx_external = key_tex->xy_uxvx_image_mask;
 
    if (nir_lower_tex(nir, &tex_options)) {
-      nir_validate_shader(nir);
+      nir_validate_shader(nir, "after nir_lower_tex");
       nir = brw_nir_optimize(nir, compiler, is_scalar, false);
    }
 
@@ -1034,7 +1034,7 @@
       varyings &= ~BITFIELD64_BIT(varying);
    }
 
-   nir_validate_shader(nir);
+   nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
 
    nir = brw_preprocess_nir(compiler, nir);
 
diff --git a/src/intel/vulkan/anv_pipeline.c b/src/intel/vulkan/anv_pipeline.c
index be05c11..7c9b123 100644
--- a/src/intel/vulkan/anv_pipeline.c
+++ b/src/intel/vulkan/anv_pipeline.c
@@ -165,7 +165,7 @@
                    stage, entrypoint_name, &spirv_options, nir_options);
    nir_shader *nir = entry_point->shader;
    assert(nir->info.stage == stage);
-   nir_validate_shader(nir);
+   nir_validate_shader(nir, "after spirv_to_nir");
    ralloc_steal(mem_ctx, nir);
 
    free(spec_entries);
diff --git a/src/mesa/drivers/dri/i965/brw_program.c b/src/mesa/drivers/dri/i965/brw_program.c
index f5ebd3c..96247f3 100644
--- a/src/mesa/drivers/dri/i965/brw_program.c
+++ b/src/mesa/drivers/dri/i965/brw_program.c
@@ -91,14 +91,15 @@
 
       nir_remove_dead_variables(nir, nir_var_shader_in | nir_var_shader_out);
       nir_lower_returns(nir);
-      nir_validate_shader(nir);
+      nir_validate_shader(nir, "after glsl_to_nir or spirv_to_nir and "
+                               "return lowering");
       NIR_PASS_V(nir, nir_lower_io_to_temporaries,
                  nir_shader_get_entrypoint(nir), true, false);
    } else {
       nir = prog_to_nir(prog, options);
       NIR_PASS_V(nir, nir_lower_regs_to_ssa); /* turn registers into SSA */
    }
-   nir_validate_shader(nir);
+   nir_validate_shader(nir, "before brw_preprocess_nir");
 
    nir = brw_preprocess_nir(brw->screen->compiler, nir);
 
diff --git a/src/mesa/main/glspirv.c b/src/mesa/main/glspirv.c
index 9729890..98b7ea7 100644
--- a/src/mesa/main/glspirv.c
+++ b/src/mesa/main/glspirv.c
@@ -234,7 +234,7 @@
       ralloc_asprintf(nir, "SPIRV:%s:%d",
                       _mesa_shader_stage_to_abbrev(nir->info.stage),
                       prog->Name);
-   nir_validate_shader(nir);
+   nir_validate_shader(nir, "after spirv_to_nir");
 
    nir->info.separate_shader = linked_shader->Program->info.separate_shader;
 
diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index 9112844..fe85eeb 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -721,7 +721,7 @@
                                PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER);
 
          if (nir_lower_wpos_ytransform(nir, &wpos_options)) {
-            nir_validate_shader(nir);
+            nir_validate_shader(nir, "after nir_lower_wpos_ytransform");
             _mesa_add_state_reference(shader->Program->Parameters,
                                       wposTransformState);
          }