Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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.  *    Jason Ekstrand (jason@jlekstrand.net)
  25.  *
  26.  */
  27.  
  28. /*
  29.  * This lowering pass detects when a global variable is only being used by
  30.  * one function and makes it local to that function
  31.  */
  32.  
  33. #include "nir.h"
  34.  
  35. struct global_to_local_state {
  36.    nir_function_impl *impl;
  37.    /* A hash table keyed on variable pointers that stores the unique
  38.     * nir_function_impl that uses the given variable.  If a variable is
  39.     * used in multiple functions, the data for the given key will be NULL.
  40.     */
  41.    struct hash_table *var_func_table;
  42. };
  43.  
  44. static bool
  45. mark_global_var_uses_block(nir_block *block, void *void_state)
  46. {
  47.    struct global_to_local_state *state = void_state;
  48.  
  49.    nir_foreach_instr(block, instr) {
  50.       if (instr->type != nir_instr_type_intrinsic)
  51.          continue;
  52.  
  53.       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
  54.       unsigned num_vars = nir_intrinsic_infos[intrin->intrinsic].num_variables;
  55.  
  56.       for (unsigned i = 0; i < num_vars; i++) {
  57.          nir_variable *var = intrin->variables[i]->var;
  58.          if (var->data.mode != nir_var_global)
  59.             continue;
  60.  
  61.          struct hash_entry *entry =
  62.             _mesa_hash_table_search(state->var_func_table, var);
  63.  
  64.          if (entry) {
  65.             if (entry->data != state->impl)
  66.                entry->data = NULL;
  67.          } else {
  68.             _mesa_hash_table_insert(state->var_func_table, var, state->impl);
  69.          }
  70.       }
  71.    }
  72.  
  73.    return true;
  74. }
  75.  
  76. void
  77. nir_lower_global_vars_to_local(nir_shader *shader)
  78. {
  79.    struct global_to_local_state state;
  80.  
  81.    state.var_func_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
  82.                                                   _mesa_key_pointer_equal);
  83.  
  84.    nir_foreach_overload(shader, overload) {
  85.       if (overload->impl) {
  86.          state.impl = overload->impl;
  87.          nir_foreach_block(overload->impl, mark_global_var_uses_block, &state);
  88.       }
  89.    }
  90.  
  91.    struct hash_entry *entry;
  92.    hash_table_foreach(state.var_func_table, entry) {
  93.       nir_variable *var = (void *)entry->key;
  94.       nir_function_impl *impl = entry->data;
  95.  
  96.       assert(var->data.mode == nir_var_global);
  97.  
  98.       if (impl != NULL) {
  99.          exec_node_remove(&var->node);
  100.          var->data.mode = nir_var_local;
  101.          exec_list_push_tail(&impl->locals, &var->node);
  102.       }
  103.    }
  104.  
  105.    _mesa_hash_table_destroy(state.var_func_table, NULL);
  106. }
  107.