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. #include "nir.h"
  29. #include "nir_array.h"
  30.  
  31. struct locals_to_regs_state {
  32.    nir_shader *shader;
  33.    nir_function_impl *impl;
  34.  
  35.    /* A hash table mapping derefs to registers */
  36.    struct hash_table *regs_table;
  37.  
  38.    /* A growing array of derefs that we have encountered.  There is exactly
  39.     * one element of this array per element in the hash table.  This is
  40.     * used to make adding register initialization code deterministic.
  41.     */
  42.    nir_array derefs_array;
  43. };
  44.  
  45. /* The following two functions implement a hash and equality check for
  46.  * variable dreferences.  When the hash or equality function encounters an
  47.  * array, it ignores the offset and whether it is direct or indirect
  48.  * entirely.
  49.  */
  50. static uint32_t
  51. hash_deref(const void *void_deref)
  52. {
  53.    uint32_t hash = _mesa_fnv32_1a_offset_bias;
  54.  
  55.    const nir_deref_var *deref_var = void_deref;
  56.    hash = _mesa_fnv32_1a_accumulate(hash, deref_var->var);
  57.  
  58.    for (const nir_deref *deref = deref_var->deref.child;
  59.         deref; deref = deref->child) {
  60.       if (deref->deref_type == nir_deref_type_struct) {
  61.          const nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
  62.          hash = _mesa_fnv32_1a_accumulate(hash, deref_struct->index);
  63.       }
  64.    }
  65.  
  66.    return hash;
  67. }
  68.  
  69. static bool
  70. derefs_equal(const void *void_a, const void *void_b)
  71. {
  72.    const nir_deref_var *a_var = void_a;
  73.    const nir_deref_var *b_var = void_b;
  74.  
  75.    if (a_var->var != b_var->var)
  76.       return false;
  77.  
  78.    for (const nir_deref *a = a_var->deref.child, *b = b_var->deref.child;
  79.         a != NULL; a = a->child, b = b->child) {
  80.       if (a->deref_type != b->deref_type)
  81.          return false;
  82.  
  83.       if (a->deref_type == nir_deref_type_struct) {
  84.          if (nir_deref_as_struct(a)->index != nir_deref_as_struct(b)->index)
  85.             return false;
  86.       }
  87.       /* Do nothing for arrays.  They're all the same. */
  88.  
  89.       assert((a->child == NULL) == (b->child == NULL));
  90.       if((a->child == NULL) != (b->child == NULL))
  91.          return false;
  92.    }
  93.  
  94.    return true;
  95. }
  96.  
  97. static nir_register *
  98. get_reg_for_deref(nir_deref_var *deref, struct locals_to_regs_state *state)
  99. {
  100.    uint32_t hash = hash_deref(deref);
  101.  
  102.    struct hash_entry *entry =
  103.       _mesa_hash_table_search_pre_hashed(state->regs_table, hash, deref);
  104.    if (entry)
  105.       return entry->data;
  106.  
  107.    unsigned array_size = 1;
  108.    nir_deref *tail = &deref->deref;
  109.    while (tail->child) {
  110.       if (tail->child->deref_type == nir_deref_type_array)
  111.          array_size *= glsl_get_length(tail->type);
  112.       tail = tail->child;
  113.    }
  114.  
  115.    assert(glsl_type_is_vector(tail->type) || glsl_type_is_scalar(tail->type));
  116.  
  117.    nir_register *reg = nir_local_reg_create(state->impl);
  118.    reg->num_components = glsl_get_vector_elements(tail->type);
  119.    reg->num_array_elems = array_size > 1 ? array_size : 0;
  120.  
  121.    _mesa_hash_table_insert_pre_hashed(state->regs_table, hash, deref, reg);
  122.    nir_array_add(&state->derefs_array, nir_deref_var *, deref);
  123.  
  124.    return reg;
  125. }
  126.  
  127. static nir_src
  128. get_deref_reg_src(nir_deref_var *deref, nir_instr *instr,
  129.                   struct locals_to_regs_state *state)
  130. {
  131.    nir_src src;
  132.  
  133.    src.is_ssa = false;
  134.    src.reg.reg = get_reg_for_deref(deref, state);
  135.    src.reg.base_offset = 0;
  136.    src.reg.indirect = NULL;
  137.  
  138.    /* It is possible for a user to create a shader that has an array with a
  139.     * single element and then proceed to access it indirectly.  Indirectly
  140.     * accessing a non-array register is not allowed in NIR.  In order to
  141.     * handle this case we just convert it to a direct reference.
  142.     */
  143.    if (src.reg.reg->num_array_elems == 0)
  144.       return src;
  145.  
  146.    nir_deref *tail = &deref->deref;
  147.    while (tail->child != NULL) {
  148.       const struct glsl_type *parent_type = tail->type;
  149.       tail = tail->child;
  150.  
  151.       if (tail->deref_type != nir_deref_type_array)
  152.          continue;
  153.  
  154.       nir_deref_array *deref_array = nir_deref_as_array(tail);
  155.  
  156.       src.reg.base_offset *= glsl_get_length(parent_type);
  157.       src.reg.base_offset += deref_array->base_offset;
  158.  
  159.       if (src.reg.indirect) {
  160.          nir_load_const_instr *load_const =
  161.             nir_load_const_instr_create(state->shader, 1);
  162.          load_const->value.u[0] = glsl_get_length(parent_type);
  163.          nir_instr_insert_before(instr, &load_const->instr);
  164.  
  165.          nir_alu_instr *mul = nir_alu_instr_create(state->shader, nir_op_imul);
  166.          mul->src[0].src = *src.reg.indirect;
  167.          mul->src[1].src.is_ssa = true;
  168.          mul->src[1].src.ssa = &load_const->def;
  169.          mul->dest.write_mask = 1;
  170.          nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
  171.          nir_instr_insert_before(instr, &mul->instr);
  172.  
  173.          src.reg.indirect->is_ssa = true;
  174.          src.reg.indirect->ssa = &mul->dest.dest.ssa;
  175.       }
  176.  
  177.       if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
  178.          if (src.reg.indirect == NULL) {
  179.             src.reg.indirect = ralloc(state->shader, nir_src);
  180.             nir_src_copy(src.reg.indirect, &deref_array->indirect,
  181.                          state->shader);
  182.          } else {
  183.             nir_alu_instr *add = nir_alu_instr_create(state->shader,
  184.                                                       nir_op_iadd);
  185.             add->src[0].src = *src.reg.indirect;
  186.             nir_src_copy(&add->src[1].src, &deref_array->indirect,
  187.                          state->shader);
  188.             add->dest.write_mask = 1;
  189.             nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
  190.             nir_instr_insert_before(instr, &add->instr);
  191.  
  192.             src.reg.indirect->is_ssa = true;
  193.             src.reg.indirect->ssa = &add->dest.dest.ssa;
  194.          }
  195.       }
  196.    }
  197.  
  198.    return src;
  199. }
  200.  
  201. static bool
  202. lower_locals_to_regs_block(nir_block *block, void *void_state)
  203. {
  204.    struct locals_to_regs_state *state = void_state;
  205.  
  206.    nir_foreach_instr_safe(block, instr) {
  207.       if (instr->type != nir_instr_type_intrinsic)
  208.          continue;
  209.  
  210.       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
  211.  
  212.       switch (intrin->intrinsic) {
  213.       case nir_intrinsic_load_var: {
  214.          if (intrin->variables[0]->var->data.mode != nir_var_local)
  215.             continue;
  216.  
  217.          nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
  218.          mov->src[0].src = get_deref_reg_src(intrin->variables[0],
  219.                                              &intrin->instr, state);
  220.          mov->dest.write_mask = (1 << intrin->num_components) - 1;
  221.          if (intrin->dest.is_ssa) {
  222.             nir_ssa_dest_init(&mov->instr, &mov->dest.dest,
  223.                               intrin->num_components, NULL);
  224.             nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
  225.                                      nir_src_for_ssa(&mov->dest.dest.ssa),
  226.                                      state->shader);
  227.          } else {
  228.             nir_dest_copy(&mov->dest.dest, &intrin->dest, state->shader);
  229.          }
  230.          nir_instr_insert_before(&intrin->instr, &mov->instr);
  231.  
  232.          nir_instr_remove(&intrin->instr);
  233.          break;
  234.       }
  235.  
  236.       case nir_intrinsic_store_var: {
  237.          if (intrin->variables[0]->var->data.mode != nir_var_local)
  238.             continue;
  239.  
  240.          nir_src reg_src = get_deref_reg_src(intrin->variables[0],
  241.                                              &intrin->instr, state);
  242.  
  243.          nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
  244.          nir_src_copy(&mov->src[0].src, &intrin->src[0], state->shader);
  245.          mov->dest.write_mask = (1 << intrin->num_components) - 1;
  246.          mov->dest.dest.is_ssa = false;
  247.          mov->dest.dest.reg.reg = reg_src.reg.reg;
  248.          mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
  249.          mov->dest.dest.reg.indirect = reg_src.reg.indirect;
  250.  
  251.          nir_instr_insert_before(&intrin->instr, &mov->instr);
  252.  
  253.          nir_instr_remove(&intrin->instr);
  254.          break;
  255.       }
  256.  
  257.       case nir_intrinsic_copy_var:
  258.          unreachable("There should be no copies whatsoever at this point");
  259.          break;
  260.  
  261.       default:
  262.          continue;
  263.       }
  264.    }
  265.  
  266.    return true;
  267. }
  268.  
  269. static nir_block *
  270. compute_reg_usedef_lca(nir_register *reg)
  271. {
  272.    nir_block *lca = NULL;
  273.  
  274.    list_for_each_entry(nir_dest, def_dest, &reg->defs, reg.def_link)
  275.       lca = nir_dominance_lca(lca, def_dest->reg.parent_instr->block);
  276.  
  277.    list_for_each_entry(nir_src, use_src, &reg->uses, use_link)
  278.       lca = nir_dominance_lca(lca, use_src->parent_instr->block);
  279.  
  280.    list_for_each_entry(nir_src, use_src, &reg->if_uses, use_link) {
  281.       nir_cf_node *prev_node = nir_cf_node_prev(&use_src->parent_if->cf_node);
  282.       assert(prev_node->type == nir_cf_node_block);
  283.       lca = nir_dominance_lca(lca, nir_cf_node_as_block(prev_node));
  284.    }
  285.  
  286.    return lca;
  287. }
  288.  
  289. static void
  290. insert_constant_initializer(nir_deref_var *deref_head, nir_deref *deref_tail,
  291.                             nir_block *block,
  292.                             struct locals_to_regs_state *state)
  293. {
  294.    if (deref_tail->child) {
  295.       switch (deref_tail->child->deref_type) {
  296.       case nir_deref_type_array: {
  297.          unsigned array_elems = glsl_get_length(deref_tail->type);
  298.  
  299.          nir_deref_array arr_deref;
  300.          arr_deref.deref = *deref_tail->child;
  301.          arr_deref.deref_array_type = nir_deref_array_type_direct;
  302.  
  303.          nir_deref *old_child = deref_tail->child;
  304.          deref_tail->child = &arr_deref.deref;
  305.          for (unsigned i = 0; i < array_elems; i++) {
  306.             arr_deref.base_offset = i;
  307.             insert_constant_initializer(deref_head, &arr_deref.deref,
  308.                                         block, state);
  309.          }
  310.          deref_tail->child = old_child;
  311.          return;
  312.       }
  313.  
  314.       case nir_deref_type_struct:
  315.          insert_constant_initializer(deref_head, deref_tail->child,
  316.                                      block, state);
  317.          return;
  318.  
  319.       default:
  320.          unreachable("Invalid deref child type");
  321.       }
  322.    }
  323.  
  324.    assert(deref_tail->child == NULL);
  325.  
  326.    nir_load_const_instr *load =
  327.       nir_deref_get_const_initializer_load(state->shader, deref_head);
  328.    nir_instr_insert_before_block(block, &load->instr);
  329.  
  330.    nir_src reg_src = get_deref_reg_src(deref_head, &load->instr, state);
  331.  
  332.    nir_alu_instr *mov = nir_alu_instr_create(state->shader, nir_op_imov);
  333.    mov->src[0].src = nir_src_for_ssa(&load->def);
  334.    mov->dest.write_mask = (1 << load->def.num_components) - 1;
  335.    mov->dest.dest.is_ssa = false;
  336.    mov->dest.dest.reg.reg = reg_src.reg.reg;
  337.    mov->dest.dest.reg.base_offset = reg_src.reg.base_offset;
  338.    mov->dest.dest.reg.indirect = reg_src.reg.indirect;
  339.  
  340.    nir_instr_insert_after(&load->instr, &mov->instr);
  341. }
  342.  
  343. static void
  344. nir_lower_locals_to_regs_impl(nir_function_impl *impl)
  345. {
  346.    struct locals_to_regs_state state;
  347.  
  348.    state.shader = impl->overload->function->shader;
  349.    state.impl = impl;
  350.    state.regs_table = _mesa_hash_table_create(NULL, hash_deref, derefs_equal);
  351.    nir_array_init(&state.derefs_array, NULL);
  352.  
  353.    nir_metadata_require(impl, nir_metadata_dominance);
  354.  
  355.    nir_foreach_block(impl, lower_locals_to_regs_block, &state);
  356.  
  357.    nir_array_foreach(&state.derefs_array, nir_deref_var *, deref_ptr) {
  358.       nir_deref_var *deref = *deref_ptr;
  359.       struct hash_entry *deref_entry =
  360.          _mesa_hash_table_search(state.regs_table, deref);
  361.       assert(deref_entry && deref_entry->key == deref);
  362.       nir_register *reg = (nir_register *)deref_entry->data;
  363.  
  364.       if (deref->var->constant_initializer == NULL)
  365.          continue;
  366.  
  367.       nir_block *usedef_lca = compute_reg_usedef_lca(reg);
  368.  
  369.       insert_constant_initializer(deref, &deref->deref, usedef_lca, &state);
  370.    }
  371.  
  372.    nir_metadata_preserve(impl, nir_metadata_block_index |
  373.                                nir_metadata_dominance);
  374.  
  375.    nir_array_fini(&state.derefs_array);
  376.    _mesa_hash_table_destroy(state.regs_table, NULL);
  377. }
  378.  
  379. void
  380. nir_lower_locals_to_regs(nir_shader *shader)
  381. {
  382.    nir_foreach_overload(shader, overload) {
  383.       if (overload->impl)
  384.          nir_lower_locals_to_regs_impl(overload->impl);
  385.    }
  386. }
  387.