• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Lima Project
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 
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "lima_ir.h"
27 
28 static bool
lima_nir_duplicate_intrinsic(nir_builder * b,nir_intrinsic_instr * itr,nir_intrinsic_op op)29 lima_nir_duplicate_intrinsic(nir_builder *b, nir_intrinsic_instr *itr,
30                              nir_intrinsic_op op)
31 {
32    nir_intrinsic_instr *last_dupl = NULL;
33    nir_instr *last_parent_instr = NULL;
34 
35    nir_foreach_use_safe(use_src, &itr->dest.ssa) {
36       nir_intrinsic_instr *dupl;
37 
38       if (last_parent_instr != use_src->parent_instr) {
39          /* if ssa use, clone for the target block */
40          b->cursor = nir_before_instr(use_src->parent_instr);
41          dupl = nir_intrinsic_instr_create(b->shader, op);
42          dupl->num_components = itr->num_components;
43          memcpy(dupl->const_index, itr->const_index, sizeof(itr->const_index));
44          dupl->src[0].is_ssa = itr->src[0].is_ssa;
45          if (itr->src[0].is_ssa)
46             dupl->src[0].ssa = itr->src[0].ssa;
47          else
48             dupl->src[0].reg = itr->src[0].reg;
49 
50          nir_ssa_dest_init(&dupl->instr, &dupl->dest,
51                dupl->num_components, itr->dest.ssa.bit_size, NULL);
52 
53          dupl->instr.pass_flags = 1;
54          nir_builder_instr_insert(b, &dupl->instr);
55       }
56       else {
57          dupl = last_dupl;
58       }
59 
60       nir_instr_rewrite_src(use_src->parent_instr, use_src, nir_src_for_ssa(&dupl->dest.ssa));
61       last_parent_instr = use_src->parent_instr;
62       last_dupl = dupl;
63    }
64 
65    last_dupl = NULL;
66    last_parent_instr = NULL;
67 
68    nir_foreach_if_use_safe(use_src, &itr->dest.ssa) {
69       nir_intrinsic_instr *dupl;
70 
71       if (last_parent_instr != use_src->parent_instr) {
72          /* if 'if use', clone where it is */
73          b->cursor = nir_before_instr(&itr->instr);
74          dupl = nir_intrinsic_instr_create(b->shader, op);
75          dupl->num_components = itr->num_components;
76          memcpy(dupl->const_index, itr->const_index, sizeof(itr->const_index));
77          dupl->src[0].is_ssa = itr->src[0].is_ssa;
78          if (itr->src[0].is_ssa)
79             dupl->src[0].ssa = itr->src[0].ssa;
80          else
81             dupl->src[0].reg = itr->src[0].reg;
82 
83          nir_ssa_dest_init(&dupl->instr, &dupl->dest,
84                dupl->num_components, itr->dest.ssa.bit_size, NULL);
85 
86          dupl->instr.pass_flags = 1;
87          nir_builder_instr_insert(b, &dupl->instr);
88       }
89       else {
90          dupl = last_dupl;
91       }
92 
93       nir_if_rewrite_condition(use_src->parent_if, nir_src_for_ssa(&dupl->dest.ssa));
94       last_parent_instr = use_src->parent_instr;
95       last_dupl = dupl;
96    }
97 
98    nir_instr_remove(&itr->instr);
99    return true;
100 }
101 
102 static void
lima_nir_duplicate_intrinsic_impl(nir_shader * shader,nir_function_impl * impl,nir_intrinsic_op op)103 lima_nir_duplicate_intrinsic_impl(nir_shader *shader, nir_function_impl *impl,
104                                   nir_intrinsic_op op)
105 {
106    nir_builder builder;
107    nir_builder_init(&builder, impl);
108 
109    nir_foreach_block(block, impl) {
110       nir_foreach_instr(instr, block) {
111          instr->pass_flags = 0;
112       }
113 
114       nir_foreach_instr_safe(instr, block) {
115          if (instr->type != nir_instr_type_intrinsic)
116             continue;
117 
118          nir_intrinsic_instr *itr = nir_instr_as_intrinsic(instr);
119 
120          if (itr->intrinsic != op)
121             continue;
122 
123          if (itr->instr.pass_flags)
124             continue;
125 
126          if (!itr->dest.is_ssa)
127             continue;
128 
129          lima_nir_duplicate_intrinsic(&builder, itr, op);
130       }
131    }
132 
133    nir_metadata_preserve(impl, nir_metadata_block_index |
134                                nir_metadata_dominance);
135 }
136 
137 /* Duplicate load uniforms for every user.
138  * Helps by utilizing the load uniform instruction slots that would
139  * otherwise stay empty, and reduces register pressure. */
140 void
lima_nir_duplicate_load_uniforms(nir_shader * shader)141 lima_nir_duplicate_load_uniforms(nir_shader *shader)
142 {
143    nir_foreach_function(function, shader) {
144       if (function->impl) {
145          lima_nir_duplicate_intrinsic_impl(shader, function->impl, nir_intrinsic_load_uniform);
146       }
147    }
148 }
149 
150 /* Duplicate load inputs for every user.
151  * Helps by utilizing the load input instruction slots that would
152  * otherwise stay empty, and reduces register pressure. */
153 void
lima_nir_duplicate_load_inputs(nir_shader * shader)154 lima_nir_duplicate_load_inputs(nir_shader *shader)
155 {
156    nir_foreach_function(function, shader) {
157       if (function->impl) {
158          lima_nir_duplicate_intrinsic_impl(shader, function->impl, nir_intrinsic_load_input);
159       }
160    }
161 }
162