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->def) {
36 nir_intrinsic_instr *dupl;
37
38 if (last_parent_instr != nir_src_parent_instr(use_src)) {
39 /* if ssa use, clone for the target block */
40 b->cursor = nir_before_instr(nir_src_parent_instr(use_src));
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].ssa = itr->src[0].ssa;
45
46 nir_def_init(&dupl->instr, &dupl->def, dupl->num_components,
47 itr->def.bit_size);
48
49 dupl->instr.pass_flags = 1;
50 nir_builder_instr_insert(b, &dupl->instr);
51 }
52 else {
53 dupl = last_dupl;
54 }
55
56 nir_src_rewrite(use_src, &dupl->def);
57 last_parent_instr = nir_src_parent_instr(use_src);
58 last_dupl = dupl;
59 }
60
61 last_dupl = NULL;
62 nir_if *last_parent_if = NULL;
63
64 nir_foreach_if_use_safe(use_src, &itr->def) {
65 nir_intrinsic_instr *dupl;
66 nir_if *nif = nir_src_parent_if(use_src);
67
68 if (last_parent_if != nif) {
69 /* if 'if use', clone where it is */
70 b->cursor = nir_before_instr(&itr->instr);
71 dupl = nir_intrinsic_instr_create(b->shader, op);
72 dupl->num_components = itr->num_components;
73 memcpy(dupl->const_index, itr->const_index, sizeof(itr->const_index));
74 dupl->src[0].ssa = itr->src[0].ssa;
75
76 nir_def_init(&dupl->instr, &dupl->def, dupl->num_components,
77 itr->def.bit_size);
78
79 dupl->instr.pass_flags = 1;
80 nir_builder_instr_insert(b, &dupl->instr);
81 }
82 else {
83 dupl = last_dupl;
84 }
85
86 nir_src_rewrite(&nir_src_parent_if(use_src)->condition, &dupl->def);
87 last_parent_if = nif;
88 last_dupl = dupl;
89 }
90
91 nir_instr_remove(&itr->instr);
92 return true;
93 }
94
95 static void
lima_nir_duplicate_intrinsic_impl(nir_shader * shader,nir_function_impl * impl,nir_intrinsic_op op)96 lima_nir_duplicate_intrinsic_impl(nir_shader *shader, nir_function_impl *impl,
97 nir_intrinsic_op op)
98 {
99 nir_builder builder = nir_builder_create(impl);
100
101 nir_foreach_block(block, impl) {
102 nir_foreach_instr(instr, block) {
103 instr->pass_flags = 0;
104 }
105
106 nir_foreach_instr_safe(instr, block) {
107 if (instr->type != nir_instr_type_intrinsic)
108 continue;
109
110 nir_intrinsic_instr *itr = nir_instr_as_intrinsic(instr);
111
112 if (itr->intrinsic != op)
113 continue;
114
115 if (itr->instr.pass_flags)
116 continue;
117
118 lima_nir_duplicate_intrinsic(&builder, itr, op);
119 }
120 }
121
122 nir_metadata_preserve(impl, nir_metadata_control_flow);
123 }
124
125 /* Duplicate load uniforms for every user.
126 * Helps by utilizing the load uniform instruction slots that would
127 * otherwise stay empty, and reduces register pressure. */
128 void
lima_nir_duplicate_load_uniforms(nir_shader * shader)129 lima_nir_duplicate_load_uniforms(nir_shader *shader)
130 {
131 nir_foreach_function_impl(impl, shader) {
132 lima_nir_duplicate_intrinsic_impl(shader, impl, nir_intrinsic_load_uniform);
133 }
134 }
135
136 /* Duplicate load inputs for every user.
137 * Helps by utilizing the load input instruction slots that would
138 * otherwise stay empty, and reduces register pressure. */
139 void
lima_nir_duplicate_load_inputs(nir_shader * shader)140 lima_nir_duplicate_load_inputs(nir_shader *shader)
141 {
142 nir_foreach_function_impl(impl, shader) {
143 lima_nir_duplicate_intrinsic_impl(shader, impl, nir_intrinsic_load_input);
144 }
145 }
146