nir: Optimize mask+downcast to just downcast
[mesa.git] / src / compiler / nir / nir_linking_helpers.c
index 8047d122ffe57b7194f4e078c12eb04bc06ca7b1..e5cd450a014bd169701ca61cec04f0ee9d2da18f 100644 (file)
@@ -128,9 +128,8 @@ nir_remove_unused_io_vars(nir_shader *shader,
    uint64_t *used;
 
    assert(mode == nir_var_shader_in || mode == nir_var_shader_out);
-   struct exec_list *var_list = nir_variable_list_for_mode(shader, mode);
 
-   nir_foreach_variable_safe(var, var_list) {
+   nir_foreach_variable_with_modes_safe(var, shader, mode) {
       if (var->data.patch)
          used = used_by_other_stage_patches;
       else
@@ -152,9 +151,6 @@ nir_remove_unused_io_vars(nir_shader *shader,
          var->data.location = 0;
          var->data.mode = nir_var_shader_temp;
 
-         exec_node_remove(&var->node);
-         exec_list_push_tail(&shader->globals, &var->node);
-
          progress = true;
       }
    }
@@ -269,12 +265,13 @@ struct assigned_comps
  * TODO: allow better packing of complex types.
  */
 static void
-get_unmoveable_components_masks(struct exec_list *var_list,
+get_unmoveable_components_masks(nir_shader *shader,
+                                nir_variable_mode mode,
                                 struct assigned_comps *comps,
                                 gl_shader_stage stage,
                                 bool default_to_smooth_interp)
 {
-   nir_foreach_variable_safe(var, var_list) {
+   nir_foreach_variable_with_modes_safe(var, shader, mode) {
       assert(var->data.location >= 0);
 
       /* Only remap things that aren't built-ins. */
@@ -360,18 +357,19 @@ mark_used_slot(nir_variable *var, uint64_t *slots_used, unsigned offset)
 }
 
 static void
-remap_slots_and_components(struct exec_list *var_list, gl_shader_stage stage,
+remap_slots_and_components(nir_shader *shader, nir_variable_mode mode,
                            struct varying_loc (*remap)[4],
                            uint64_t *slots_used, uint64_t *out_slots_read,
                            uint32_t *p_slots_used, uint32_t *p_out_slots_read)
  {
+   const gl_shader_stage stage = shader->info.stage;
    uint64_t out_slots_read_tmp[2] = {0};
    uint64_t slots_used_tmp[2] = {0};
 
    /* We don't touch builtins so just copy the bitmask */
    slots_used_tmp[0] = *slots_used & BITFIELD64_RANGE(0, VARYING_SLOT_VAR0);
 
-   nir_foreach_variable(var, var_list) {
+   nir_foreach_variable_with_modes(var, shader, mode) {
       assert(var->data.location >= 0);
 
       /* Only remap things that aren't built-ins */
@@ -729,8 +727,6 @@ compact_components(nir_shader *producer, nir_shader *consumer,
                    struct assigned_comps *assigned_comps,
                    bool default_to_smooth_interp)
 {
-   struct exec_list *input_list = &consumer->inputs;
-   struct exec_list *output_list = &producer->outputs;
    struct varying_loc remap[MAX_VARYINGS_INCL_PATCH][4] = {{{0}, {0}}};
    struct varying_component *varying_comp_info;
    unsigned varying_comp_info_size;
@@ -788,10 +784,10 @@ compact_components(nir_shader *producer, nir_shader *consumer,
 
    uint64_t zero = 0;
    uint32_t zero32 = 0;
-   remap_slots_and_components(input_list, consumer->info.stage, remap,
+   remap_slots_and_components(consumer, nir_var_shader_in, remap,
                               &consumer->info.inputs_read, &zero,
                               &consumer->info.patch_inputs_read, &zero32);
-   remap_slots_and_components(output_list, producer->info.stage, remap,
+   remap_slots_and_components(producer, nir_var_shader_out, remap,
                               &producer->info.outputs_written,
                               &producer->info.outputs_read,
                               &producer->info.patch_outputs_written,
@@ -817,10 +813,12 @@ nir_compact_varyings(nir_shader *producer, nir_shader *consumer,
 
    struct assigned_comps assigned_comps[MAX_VARYINGS_INCL_PATCH] = {{0}};
 
-   get_unmoveable_components_masks(&producer->outputs, assigned_comps,
+   get_unmoveable_components_masks(producer, nir_var_shader_out,
+                                   assigned_comps,
                                    producer->info.stage,
                                    default_to_smooth_interp);
-   get_unmoveable_components_masks(&consumer->inputs, assigned_comps,
+   get_unmoveable_components_masks(consumer, nir_var_shader_in,
+                                   assigned_comps,
                                    consumer->info.stage,
                                    default_to_smooth_interp);
 
@@ -1065,7 +1063,7 @@ nir_link_opt_varyings(nir_shader *producer, nir_shader *consumer)
 static void
 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
 {
-   nir_foreach_variable(var, var_list) {
+   nir_foreach_variable_in_list(var, var_list) {
       if (var->data.location > new_var->data.location) {
          exec_node_insert_node_before(&var->node, &new_var->node);
          return;
@@ -1075,30 +1073,30 @@ insert_sorted(struct exec_list *var_list, nir_variable *new_var)
 }
 
 static void
-sort_varyings(struct exec_list *var_list)
+sort_varyings(nir_shader *shader, nir_variable_mode mode,
+              struct exec_list *sorted_list)
 {
-   struct exec_list new_list;
-   exec_list_make_empty(&new_list);
-   nir_foreach_variable_safe(var, var_list) {
+   exec_list_make_empty(sorted_list);
+   nir_foreach_variable_with_modes_safe(var, shader, mode) {
       exec_node_remove(&var->node);
-      insert_sorted(&new_list, var);
+      insert_sorted(sorted_list, var);
    }
-   exec_list_move_nodes_to(&new_list, var_list);
 }
 
 void
-nir_assign_io_var_locations(struct exec_list *var_list, unsigned *size,
-                            gl_shader_stage stage)
+nir_assign_io_var_locations(nir_shader *shader, nir_variable_mode mode,
+                            unsigned *size, gl_shader_stage stage)
 {
    unsigned location = 0;
    unsigned assigned_locations[VARYING_SLOT_TESS_MAX];
    uint64_t processed_locs[2] = {0};
 
-   sort_varyings(var_list);
+   struct exec_list io_vars;
+   sort_varyings(shader, mode, &io_vars);
 
    int UNUSED last_loc = 0;
    bool last_partial = false;
-   nir_foreach_variable(var, var_list) {
+   nir_foreach_variable_in_list(var, &io_vars) {
       const struct glsl_type *type = var->type;
       if (nir_is_per_vertex_io(var, stage) || var->data.per_view) {
          assert(glsl_type_is_array(type));
@@ -1201,6 +1199,7 @@ nir_assign_io_var_locations(struct exec_list *var_list, unsigned *size,
    if (last_partial)
       location++;
 
+   exec_list_append(&shader->variables, &io_vars);
    *size = location;
 }