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_types.h"
  30.  
  31. /*
  32.  * Lowers all copy intrinsics to sequences of load/store intrinsics.
  33.  */
  34.  
  35. /* Walks down the deref chain and returns the next deref in the chain whose
  36.  * child is a wildcard.  In other words, given the chain  a[1].foo[*].bar,
  37.  * this function will return the deref to foo.  Calling it a second time
  38.  * with the [*].bar, it will return NULL.
  39.  */
  40. static nir_deref *
  41. deref_next_wildcard_parent(nir_deref *deref)
  42. {
  43.    for (nir_deref *tail = deref; tail->child; tail = tail->child) {
  44.       if (tail->child->deref_type != nir_deref_type_array)
  45.          continue;
  46.  
  47.       nir_deref_array *arr = nir_deref_as_array(tail->child);
  48.  
  49.       if (arr->deref_array_type == nir_deref_array_type_wildcard)
  50.          return tail;
  51.    }
  52.  
  53.    return NULL;
  54. }
  55.  
  56. /* Returns the last deref in the chain.
  57.  */
  58. static nir_deref *
  59. get_deref_tail(nir_deref *deref)
  60. {
  61.    while (deref->child)
  62.       deref = deref->child;
  63.  
  64.    return deref;
  65. }
  66.  
  67. /* This function recursively walks the given deref chain and replaces the
  68.  * given copy instruction with an equivalent sequence load/store
  69.  * operations.
  70.  *
  71.  * @copy_instr    The copy instruction to replace; new instructions will be
  72.  *                inserted before this one
  73.  *
  74.  * @dest_head     The head of the destination variable deref chain
  75.  *
  76.  * @src_head      The head of the source variable deref chain
  77.  *
  78.  * @dest_tail     The current tail of the destination variable deref chain;
  79.  *                this is used for recursion and external callers of this
  80.  *                function should call it with tail == head
  81.  *
  82.  * @src_tail      The current tail of the source variable deref chain;
  83.  *                this is used for recursion and external callers of this
  84.  *                function should call it with tail == head
  85.  *
  86.  * @state         The current variable lowering state
  87.  */
  88. static void
  89. emit_copy_load_store(nir_intrinsic_instr *copy_instr,
  90.                      nir_deref_var *dest_head, nir_deref_var *src_head,
  91.                      nir_deref *dest_tail, nir_deref *src_tail, void *mem_ctx)
  92. {
  93.    /* Find the next pair of wildcards */
  94.    nir_deref *src_arr_parent = deref_next_wildcard_parent(src_tail);
  95.    nir_deref *dest_arr_parent = deref_next_wildcard_parent(dest_tail);
  96.  
  97.    if (src_arr_parent || dest_arr_parent) {
  98.       /* Wildcards had better come in matched pairs */
  99.       assert(dest_arr_parent && dest_arr_parent);
  100.  
  101.       nir_deref_array *src_arr = nir_deref_as_array(src_arr_parent->child);
  102.       nir_deref_array *dest_arr = nir_deref_as_array(dest_arr_parent->child);
  103.  
  104.       unsigned length = glsl_get_length(src_arr_parent->type);
  105.       /* The wildcards should represent the same number of elements */
  106.       assert(length == glsl_get_length(dest_arr_parent->type));
  107.       assert(length > 0);
  108.  
  109.       /* Walk over all of the elements that this wildcard refers to and
  110.        * call emit_copy_load_store on each one of them */
  111.       src_arr->deref_array_type = nir_deref_array_type_direct;
  112.       dest_arr->deref_array_type = nir_deref_array_type_direct;
  113.       for (unsigned i = 0; i < length; i++) {
  114.          src_arr->base_offset = i;
  115.          dest_arr->base_offset = i;
  116.          emit_copy_load_store(copy_instr, dest_head, src_head,
  117.                               &dest_arr->deref, &src_arr->deref, mem_ctx);
  118.       }
  119.       src_arr->deref_array_type = nir_deref_array_type_wildcard;
  120.       dest_arr->deref_array_type = nir_deref_array_type_wildcard;
  121.    } else {
  122.       /* In this case, we have no wildcards anymore, so all we have to do
  123.        * is just emit the load and store operations. */
  124.       src_tail = get_deref_tail(src_tail);
  125.       dest_tail = get_deref_tail(dest_tail);
  126.  
  127.       assert(src_tail->type == dest_tail->type);
  128.  
  129.       unsigned num_components = glsl_get_vector_elements(src_tail->type);
  130.  
  131.       nir_intrinsic_instr *load =
  132.          nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_load_var);
  133.       load->num_components = num_components;
  134.       load->variables[0] = nir_deref_as_var(nir_copy_deref(load, &src_head->deref));
  135.       nir_ssa_dest_init(&load->instr, &load->dest, num_components, NULL);
  136.  
  137.       nir_instr_insert_before(&copy_instr->instr, &load->instr);
  138.  
  139.       nir_intrinsic_instr *store =
  140.          nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_store_var);
  141.       store->num_components = num_components;
  142.       store->variables[0] = nir_deref_as_var(nir_copy_deref(store, &dest_head->deref));
  143.  
  144.       store->src[0].is_ssa = true;
  145.       store->src[0].ssa = &load->dest.ssa;
  146.  
  147.       nir_instr_insert_before(&copy_instr->instr, &store->instr);
  148.    }
  149. }
  150.  
  151. /* Lowers a copy instruction to a sequence of load/store instructions
  152.  *
  153.  * The new instructions are placed before the copy instruction in the IR.
  154.  */
  155. void
  156. nir_lower_var_copy_instr(nir_intrinsic_instr *copy, void *mem_ctx)
  157. {
  158.    assert(copy->intrinsic == nir_intrinsic_copy_var);
  159.    emit_copy_load_store(copy, copy->variables[0], copy->variables[1],
  160.                         &copy->variables[0]->deref,
  161.                         &copy->variables[1]->deref, mem_ctx);
  162. }
  163.  
  164. static bool
  165. lower_var_copies_block(nir_block *block, void *mem_ctx)
  166. {
  167.    nir_foreach_instr_safe(block, instr) {
  168.       if (instr->type != nir_instr_type_intrinsic)
  169.          continue;
  170.  
  171.       nir_intrinsic_instr *copy = nir_instr_as_intrinsic(instr);
  172.       if (copy->intrinsic != nir_intrinsic_copy_var)
  173.          continue;
  174.  
  175.       nir_lower_var_copy_instr(copy, mem_ctx);
  176.  
  177.       nir_instr_remove(&copy->instr);
  178.       ralloc_free(copy);
  179.    }
  180.  
  181.    return true;
  182. }
  183.  
  184. static void
  185. lower_var_copies_impl(nir_function_impl *impl)
  186. {
  187.    nir_foreach_block(impl, lower_var_copies_block, ralloc_parent(impl));
  188. }
  189.  
  190. /* Lowers every copy_var instruction in the program to a sequence of
  191.  * load/store instructions.
  192.  */
  193. void
  194. nir_lower_var_copies(nir_shader *shader)
  195. {
  196.    nir_foreach_overload(shader, overload) {
  197.       if (overload->impl)
  198.          lower_var_copies_impl(overload->impl);
  199.    }
  200. }
  201.