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