nir/validate: Print when the validation failed
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 18 Oct 2018 20:18:30 +0000 (15:18 -0500)
committerJason Ekstrand <jason.ekstrand@intel.com>
Fri, 26 Oct 2018 16:45:29 +0000 (11:45 -0500)
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/amd/vulkan/radv_shader.c
src/compiler/nir/nir.h
src/compiler/nir/nir_validate.c
src/compiler/nir/tests/vars_tests.cpp
src/intel/compiler/brw_nir.c
src/intel/vulkan/anv_pipeline.c
src/mesa/drivers/dri/i965/brw_program.c
src/mesa/main/glspirv.c
src/mesa/state_tracker/st_glsl_to_nir.cpp

index 15c9de1e02005fbee870440183adffd5d4f06fa7..edeaefbc1a276bddbfc0aa58c428d66a6264bbe8 100644 (file)
@@ -189,7 +189,7 @@ radv_shader_compile_to_nir(struct radv_device *device,
                 * 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 @@ radv_shader_compile_to_nir(struct radv_device *device,
                                           &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);
 
index 60ea4fbc7ffb21b7a9253924abf6ec2dce36eadc..a0ae9a4430e86f958fdf92ceebe76539e0512856 100644 (file)
@@ -2664,7 +2664,7 @@ nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
 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 @@ should_print_nir(void)
    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_serialize_deserialize_nir(void) { return false; }
 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 @@ static inline bool should_print_nir(void) { return false; }
    }                                                                 \
 } 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 @@ static inline bool should_print_nir(void) { return false; }
    }                                                                 \
 )
 
-#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__);                                         \
index cf7ef91f08c6c850e28b645bb57b112270d76c2c..62893cad87e9b637395e851591300deef362354a 100644 (file)
@@ -1140,11 +1140,17 @@ destroy_validate_state(validate_state *state)
 }
 
 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 @@ dump_errors(validate_state *state)
 }
 
 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 @@ nir_validate_shader(nir_shader *shader)
    }
 
    if (_mesa_hash_table_num_entries(state.errors) > 0)
-      dump_errors(&state);
+      dump_errors(&state, when);
 
    destroy_validate_state(&state);
 }
index a7bbeb02277992ba3f922791576a9c7acc0f1b81..32763d2db6476a55b021837123dfba1ce2eda8a3 100644 (file)
@@ -147,14 +147,14 @@ TEST_F(nir_redundant_load_vars_test, duplicated_load)
    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 @@ TEST_F(nir_redundant_load_vars_test, duplicated_load_in_two_blocks)
 
    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 @@ TEST_F(nir_redundant_load_vars_test, invalidate_inside_if_block)
    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 @@ TEST_F(nir_copy_prop_vars_test, simple_copies)
    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 @@ TEST_F(nir_copy_prop_vars_test, simple_store_load)
    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 @@ TEST_F(nir_copy_prop_vars_test, store_store_load)
    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 @@ TEST_F(nir_copy_prop_vars_test, store_store_load_different_components)
    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 @@ TEST_F(nir_copy_prop_vars_test, store_store_load_different_components_in_many_bl
    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 @@ TEST_F(nir_copy_prop_vars_test, store_store_load_different_components_in_many_bl
 
    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 @@ TEST_F(nir_copy_prop_vars_test, simple_store_load_in_two_blocks)
    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);
 
index 297845b89b7d093ed631052f618477b69d7ffd32..1cd568615789095da709373306d5f8a2b5d0a91f 100644 (file)
@@ -725,8 +725,8 @@ brw_nir_link_shaders(const struct brw_compiler *compiler,
                      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 @@ brw_nir_apply_sampler_key(nir_shader *nir,
    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 @@ brw_nir_create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compile
       varyings &= ~BITFIELD64_BIT(varying);
    }
 
-   nir_validate_shader(nir);
+   nir_validate_shader(nir, "in brw_nir_create_passthrough_tcs");
 
    nir = brw_preprocess_nir(compiler, nir);
 
index be05c11f45d81af5103b41102ae3654739becfdf..7c9b12301157b94652444fa6d5b4dc20218f9e26 100644 (file)
@@ -165,7 +165,7 @@ anv_shader_compile_to_nir(struct anv_pipeline *pipeline,
                    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);
index f5ebd3c3b059de25dba9b978c7bf46f479580e50..96247f32f0f9c14a1a5b0a80f286865ef2c0e297 100644 (file)
@@ -91,14 +91,15 @@ brw_create_nir(struct brw_context *brw,
 
       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);
 
index 972989055e9caa692ed8888877aee9e78ea467e8..98b7ea773482b2cd72e01741765f7daca9b575dc 100644 (file)
@@ -234,7 +234,7 @@ _mesa_spirv_to_nir(struct gl_context *ctx,
       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;
 
index 911284401e0209d99e2b57d78a4bfcd82c1e5374..fe85eeb458dd642c6fde7e8f3fc3f3e3c764fe4b 100644 (file)
@@ -721,7 +721,7 @@ st_link_nir(struct gl_context *ctx,
                                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);
          }