nir: add deref lowering sanity checking
authorRob Clark <robdclark@gmail.com>
Wed, 16 May 2018 14:02:55 +0000 (10:02 -0400)
committerJason Ekstrand <jason.ekstrand@intel.com>
Sat, 23 Jun 2018 03:15:54 +0000 (20:15 -0700)
This will be removed at the end of the transition, but add some tracking
plus asserts to help ensure that lowering passes are called at the
correct point (pre or post deref instruction lowering) as passes are
converted and the point where lower_deref_instrs() is called is moved.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Acked-by: Rob Clark <robdclark@gmail.com>
Acked-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Acked-by: Dave Airlie <airlied@redhat.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
36 files changed:
src/compiler/glsl/gl_nir_lower_atomics.c
src/compiler/glsl/gl_nir_lower_samplers.c
src/compiler/glsl/gl_nir_lower_samplers_as_deref.c
src/compiler/nir/nir.c
src/compiler/nir/nir.h
src/compiler/nir/nir_clone.c
src/compiler/nir/nir_deref.c
src/compiler/nir/nir_gather_info.c
src/compiler/nir/nir_linking_helpers.c
src/compiler/nir/nir_loop_analyze.c
src/compiler/nir/nir_lower_alpha_test.c
src/compiler/nir/nir_lower_clamp_color_outputs.c
src/compiler/nir/nir_lower_clip_cull_distance_arrays.c
src/compiler/nir/nir_lower_drawpixels.c
src/compiler/nir/nir_lower_global_vars_to_local.c
src/compiler/nir/nir_lower_indirect_derefs.c
src/compiler/nir/nir_lower_io.c
src/compiler/nir/nir_lower_io_arrays_to_elements.c
src/compiler/nir/nir_lower_io_to_scalar.c
src/compiler/nir/nir_lower_io_types.c
src/compiler/nir/nir_lower_locals_to_regs.c
src/compiler/nir/nir_lower_phis_to_scalar.c
src/compiler/nir/nir_lower_system_values.c
src/compiler/nir/nir_lower_tex.c
src/compiler/nir/nir_lower_var_copies.c
src/compiler/nir/nir_lower_vars_to_ssa.c
src/compiler/nir/nir_lower_wpos_center.c
src/compiler/nir/nir_lower_wpos_ytransform.c
src/compiler/nir/nir_opt_copy_prop_vars.c
src/compiler/nir/nir_opt_peephole_select.c
src/compiler/nir/nir_opt_undef.c
src/compiler/nir/nir_propagate_invariant.c
src/compiler/nir/nir_remove_dead_variables.c
src/compiler/nir/nir_serialize.c
src/compiler/nir/nir_split_var_copies.c
src/mesa/state_tracker/st_nir_lower_builtin.c

index e203b390b48b5a88c78f5531d474fd7a57e111d4..35a304a4894549038af29ee18263b670f893519d 100644 (file)
@@ -185,6 +185,8 @@ gl_nir_lower_atomics(nir_shader *shader,
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_atomic_counter_derefs);
+
    nir_foreach_function(function, shader) {
       if (!function->impl)
          continue;
index c54455f91de01ee1555c3ac64efb59585c569696..552cd59af9bb70ffb302b1acaf5952d07f3d1db6 100644 (file)
@@ -155,6 +155,8 @@ gl_nir_lower_samplers(nir_shader *shader,
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= lower_impl(function->impl, shader_program,
index 47115f943fe0d5a5d298dd35a65aac1c3bec73b3..be1baa17993f749ff2399e1f380166e4506b684f 100644 (file)
@@ -233,6 +233,8 @@ gl_nir_lower_samplers_as_deref(nir_shader *shader,
    bool progress = false;
    struct lower_samplers_as_deref_state state;
 
+   nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
+
    state.shader = shader;
    state.shader_program = shader_program;
    state.remap_table = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
index 40bf940ef387405163676f313e4ce7b12ec66b05..d1404e82fb4e4269740f3fc3eb01fd20e2120c31 100644 (file)
@@ -64,6 +64,7 @@ nir_shader_create(void *mem_ctx,
    shader->num_outputs = 0;
    shader->num_uniforms = 0;
    shader->num_shared = 0;
+   shader->lowered_derefs = 0;
 
    return shader;
 }
index d25aa7491eda2b4c7e84fd7eaa47380c40b987c5..0b9508623342ac1dd57d73434e2db4dfeb870a1e 100644 (file)
@@ -2133,8 +2133,19 @@ typedef struct nir_shader {
     * access plus one
     */
    unsigned num_inputs, num_uniforms, num_outputs, num_shared;
+
+   /* temporary, tracking for which derefs instructions have been lowered
+    * to deref chains
+    */
+   unsigned lowered_derefs;
 } nir_shader;
 
+#define nir_assert_lowered_derefs(shader, mask) \
+   assert(((shader)->lowered_derefs & (mask)) == (mask))
+
+#define nir_assert_unlowered_derefs(shader, mask) \
+   assert(!((shader)->lowered_derefs & (mask)))
+
 static inline nir_function_impl *
 nir_shader_get_entrypoint(nir_shader *shader)
 {
@@ -2681,6 +2692,7 @@ enum nir_lower_deref_flags {
    nir_lower_atomic_counter_derefs =   (1 << 3),
    nir_lower_atomic_derefs =           (1 << 4),
    nir_lower_image_derefs =            (1 << 5),
+   nir_lower_all_derefs =              (1 << 6) - 1,
 };
 
 bool nir_lower_deref_instrs(nir_shader *shader,
index 20eaaff3bee16534db813b2b9c1b2ca69e2c3f13..7236f0872a71b73e894a5bf2a033eda587e7646a 100644 (file)
@@ -816,6 +816,7 @@ nir_shader_clone(void *mem_ctx, const nir_shader *s)
    ns->num_uniforms = s->num_uniforms;
    ns->num_outputs = s->num_outputs;
    ns->num_shared = s->num_shared;
+   ns->lowered_derefs = s->lowered_derefs;
 
    free_clone_state(&state);
 
index 0e8699aa6e672b0b3a206eab4edb9710279c6ea2..d7b4bbcdcd683c93471201bd629c2e53356b532c 100644 (file)
@@ -350,5 +350,7 @@ nir_lower_deref_instrs(nir_shader *shader,
       progress |= nir_lower_deref_instrs_impl(function->impl, flags);
    }
 
+   shader->lowered_derefs |= flags;
+
    return progress;
 }
index 3534b6949ebc480fb5779cb98b67729d3a74cd3d..4f4b650ab7ed18512eb8eb0e90c1ca8c82e8f50f 100644 (file)
@@ -397,6 +397,8 @@ glsl_type_get_image_count(const struct glsl_type *type)
 void
 nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint)
 {
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+
    shader->info.num_textures = 0;
    shader->info.num_images = 0;
    nir_foreach_variable(var, &shader->uniforms) {
index 2b0a2668a33390f1f321f3739bcd11448a93fafa..707bce12ef952072daefde7fab0cb809602c0466 100644 (file)
@@ -133,6 +133,8 @@ nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer)
 {
    assert(producer->info.stage != MESA_SHADER_FRAGMENT);
    assert(consumer->info.stage != MESA_SHADER_VERTEX);
+   nir_assert_lowered_derefs(producer, nir_lower_load_store_derefs);
+   nir_assert_lowered_derefs(consumer, nir_lower_load_store_derefs);
 
    uint64_t read[4] = { 0 }, written[4] = { 0 };
    uint64_t patches_read[4] = { 0 }, patches_written[4] = { 0 };
index d5281a5faa4dedbb03df1c3be3dc33f10d697f14..870b4ffa5f09bc001a8b2bfda3799ce7e0247375 100644 (file)
@@ -827,6 +827,7 @@ void
 nir_loop_analyze_impl(nir_function_impl *impl,
                       nir_variable_mode indirect_mask)
 {
+   nir_assert_lowered_derefs(impl->function->shader, nir_lower_load_store_derefs);
    nir_index_ssa_defs(impl);
    foreach_list_typed(nir_cf_node, node, node, &impl->body)
       process_loops(node, indirect_mask);
index 6bf9ff142df7b9fc22d6c9f987f622c679843d9a..4dfd79873b5caa4278bb8f6732c07f2451518ae1 100644 (file)
@@ -41,6 +41,8 @@ nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
 {
    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       nir_function_impl *impl = function->impl;
       nir_builder b;
index 55becbf769e87d5f596b16a0b780e4e0f61731ba..17bda24bf74696b84587d421791bacd5e8fcd219 100644 (file)
@@ -134,6 +134,8 @@ nir_lower_clamp_color_outputs(nir_shader *shader)
       .shader = shader,
    };
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= lower_impl(&state, function->impl);
index 95eda82139bfa9c8e93d011706bb574af0ff8e1f..6a93f1c0a2dca83899d31fcb3dab204e80d4384a 100644 (file)
@@ -194,6 +194,8 @@ nir_lower_clip_cull_distance_arrays(nir_shader *nir)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(nir, nir_lower_load_store_derefs);
+
    if (nir->info.stage <= MESA_SHADER_GEOMETRY)
       progress |= combine_clip_cull(nir, &nir->outputs, true);
 
index 5cc358d869f4f06505c1d8dee91f65287217f994..fcdc0bc4b412baebfe090015a5b6d514401d4944 100644 (file)
@@ -253,6 +253,11 @@ nir_lower_drawpixels(nir_shader *shader,
       .shader = shader,
    };
 
+   /* note that this pass already assumes texture/sampler derefs are already
+    * lowered to index
+    */
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
    nir_foreach_function(function, shader) {
index c8fdfde8cb17976e3a461ea5df835dd4f4fd2c9e..9b3bc4cdd23a1bfde4154acf25d85d86d9c61c33 100644 (file)
@@ -76,6 +76,9 @@ nir_lower_global_vars_to_local(nir_shader *shader)
       _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                               _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs |
+         nir_lower_atomic_counter_derefs | nir_lower_atomic_derefs | nir_lower_image_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
          nir_foreach_block(block, function->impl)
index 02f202dea39e583d5163167644375b8eba290ffb..ed27c8f885174855b87d03b0f569677069eaf08b 100644 (file)
@@ -211,6 +211,8 @@ nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress = lower_indirects_impl(function->impl, modes) || progress;
index df91febd68dd1f5fa7af78e98c7c34ff6959a182..7ec2df1d4419098b742dac47612f9620bf4b6b0a 100644 (file)
@@ -511,6 +511,8 @@ nir_lower_io(nir_shader *shader, nir_variable_mode modes,
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs | nir_lower_atomic_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
          progress |= nir_lower_io_impl(function->impl, modes,
index 9a5eec8f870c8726958a9162b64217a51ea6b7c4..c9c6c95440ec551ba83fe4ec0d2b1808f5b10008 100644 (file)
@@ -356,6 +356,8 @@ nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
       _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                               _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+
    uint64_t indirects[4] = {0}, patch_indirects[4] = {0};
 
    lower_io_arrays_to_elements(shader, nir_var_shader_out, indirects,
@@ -398,6 +400,9 @@ nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer)
       _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                               _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(producer, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+   nir_assert_lowered_derefs(consumer, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+
    uint64_t indirects[4] = {0}, patch_indirects[4] = {0};
    create_indirects_mask(producer, indirects, patch_indirects,
                          nir_var_shader_out);
index 7774c2d245879dc7b65876b79e8003fa048db8ac..0dc135e13b89a73d33e1f91cec7a7ddac09c054a 100644 (file)
@@ -289,6 +289,8 @@ nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask)
       _mesa_hash_table_create(NULL, _mesa_hash_pointer,
                               _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs | nir_lower_interp_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
          nir_builder b;
index 795bbd80d5d88307ff49ab80331670e97897aea3..bbdbcdae00f9bccb83f449b9f656f3ed75e42fa7 100644 (file)
@@ -161,6 +161,8 @@ nir_lower_io_types(nir_shader *shader)
 {
    struct lower_io_types_state state;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    state.shader = shader;
    exec_list_make_empty(&state.new_ins);
    exec_list_make_empty(&state.new_outs);
index d0667bc504006a9f3b5235e65c7ef82631c030a2..822c443762fe69b4a416781da0712a59e4a9ae7c 100644 (file)
@@ -292,6 +292,8 @@ nir_lower_locals_to_regs(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress = nir_lower_locals_to_regs_impl(function->impl) || progress;
index b12718f27327c01abfe3e07726c1ca46554b9fa1..d055e781074773e16b36e625799860f890afde75 100644 (file)
@@ -299,6 +299,8 @@ nir_lower_phis_to_scalar(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress = lower_phis_to_scalar_impl(function->impl) || progress;
index 487da042620aa546dde82b49cded58ad918cc1fa..dc71a8c8d8770abddf88882bb8c5ab06aee0cb5e 100644 (file)
@@ -203,6 +203,8 @@ nir_lower_system_values(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress = convert_impl(function->impl) || progress;
index 1062afd97f012817c1103f034e143da1b8d5e129..f4c3c9c743d3b14f0470597f6bd510b35fd4b787 100644 (file)
@@ -865,6 +865,8 @@ nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= nir_lower_tex_impl(function->impl, options);
index 6288bdc465b14cd503fd5c9c44990e898621285c..e7b2bd5a236ac47ec1ec022d4d3e76fda092f45c 100644 (file)
@@ -192,6 +192,8 @@ nir_lower_var_copies(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= lower_var_copies_impl(function->impl);
index 8bc847fd41e3475385704bb68d0583029c2cb1e5..27f0ccb9572a3678bce679c327a97b06928c4e6b 100644 (file)
@@ -737,6 +737,8 @@ nir_lower_vars_to_ssa(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= nir_lower_vars_to_ssa_impl(function->impl);
index dca810d735e793403272a363c15200cde85d485a..fa225992e3e4d508a1545b17c6ff8da76bac36d4 100644 (file)
@@ -107,6 +107,8 @@ nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading)
 
    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
          nir_builder_init(&b, function->impl);
index 62166e787461eb9ab79088ef7d9725eae145ac06..9cb5c71b5600bf710ca4ce1a45a8ddf52158a0c6 100644 (file)
@@ -352,6 +352,8 @@ nir_lower_wpos_ytransform(nir_shader *shader,
       .shader = shader,
    };
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    assert(shader->info.stage == MESA_SHADER_FRAGMENT);
 
    nir_foreach_function(function, shader) {
index cc8f00f9d37303a9a0960e90a9f800d867b0719c..2e1a2e0cd68b3931af5a2d8478b0f13de12910e8 100644 (file)
@@ -768,6 +768,8 @@ nir_opt_copy_prop_vars(nir_shader *shader)
 {
    struct copy_prop_var_state state;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    state.shader = shader;
    state.mem_ctx = ralloc_context(NULL);
    list_inithead(&state.copies);
index 4ca4f80d788e156c965f02c9b23de17dbc22f0e7..24a232ece89b92735ea8e99cbad3dbc2455f2e21 100644 (file)
@@ -255,6 +255,8 @@ nir_opt_peephole_select(nir_shader *shader, unsigned limit)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= nir_opt_peephole_select_impl(function->impl, limit);
index 8d3210c637e0232b4125fdba5d37e620a33c4dd1..b34437746f60d1473588bb9ad5f7c797dd2bc4e6 100644 (file)
@@ -133,6 +133,8 @@ nir_opt_undef(nir_shader *shader)
    nir_builder b;
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl) {
          nir_builder_init(&b, function->impl);
index 7b5bd6cce6b47a049a75227ee1cf4a38a23aa923..d8dc6a03689912c98bb4d0980eff7b409dcd37bd 100644 (file)
@@ -184,6 +184,8 @@ nir_propagate_invariant(nir_shader *shader)
    struct set *invariants = _mesa_set_create(NULL, _mesa_hash_pointer,
                                              _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    bool progress = false;
    nir_foreach_function(function, shader) {
       if (function->impl && propagate_invariant_impl(function->impl, invariants))
index eff66f92d4389cdc7c66c3777ae1f8b26e429961..4a36ef9ba528c953e0285c5ecfd1ea62341c15b3 100644 (file)
@@ -172,6 +172,8 @@ nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes)
    struct set *live =
       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
 
+   nir_assert_lowered_derefs(shader, nir_lower_all_derefs);
+
    add_var_use_shader(shader, live, modes);
 
    if (modes & nir_var_uniform)
index 834a65b07e836e567580c8a48080ccd59063c76e..155205d219896cc0fff52bb4e2eb83d5a5bf805e 100644 (file)
@@ -1215,6 +1215,7 @@ nir_serialize(struct blob *blob, const nir_shader *nir)
    blob_write_uint32(blob, nir->num_uniforms);
    blob_write_uint32(blob, nir->num_outputs);
    blob_write_uint32(blob, nir->num_shared);
+   blob_write_uint32(blob, nir->lowered_derefs);
 
    blob_write_uint32(blob, exec_list_length(&nir->functions));
    nir_foreach_function(fxn, nir) {
@@ -1270,6 +1271,7 @@ nir_deserialize(void *mem_ctx,
    ctx.nir->num_uniforms = blob_read_uint32(blob);
    ctx.nir->num_outputs = blob_read_uint32(blob);
    ctx.nir->num_shared = blob_read_uint32(blob);
+   ctx.nir->lowered_derefs = blob_read_uint32(blob);
 
    unsigned num_functions = blob_read_uint32(blob);
    for (unsigned i = 0; i < num_functions; i++)
index bc3ceedbdb8b7481770f85442355457750167aa8..c18d77d745dec550eeb961660519d622b26fe021 100644 (file)
@@ -287,6 +287,8 @@ nir_split_var_copies(nir_shader *shader)
 {
    bool progress = false;
 
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress = split_var_copies_impl(function->impl) || progress;
index 660fdf3f910b2785413cc69f712bdf7ddf8b4143..8221502b811850fad099bdb85cb6cd2cd6a86aa2 100644 (file)
@@ -239,6 +239,7 @@ lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
 void
 st_nir_lower_builtin(nir_shader *shader)
 {
+   nir_assert_lowered_derefs(shader, nir_lower_load_store_derefs);
    lower_builtin_state state;
    state.shader = shader;
    nir_foreach_function(function, shader) {