xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/lima/ir/lima_nir_duplicate_consts.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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_load_const(nir_builder * b,nir_load_const_instr * load)29 lima_nir_duplicate_load_const(nir_builder *b, nir_load_const_instr *load)
30 {
31    nir_load_const_instr *last_dupl = NULL;
32    nir_instr *last_parent_instr = NULL;
33 
34    nir_foreach_use_safe(use_src, &load->def) {
35       nir_load_const_instr *dupl;
36 
37       if (last_parent_instr != nir_src_parent_instr(use_src)) {
38          /* if ssa use, clone for the target block */
39          b->cursor = nir_before_instr(nir_src_parent_instr(use_src));
40 
41          dupl = nir_load_const_instr_create(b->shader, load->def.num_components,
42                                             load->def.bit_size);
43          memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);
44 
45          dupl->instr.pass_flags = 1;
46          nir_builder_instr_insert(b, &dupl->instr);
47       }
48       else {
49          dupl = last_dupl;
50       }
51 
52       nir_src_rewrite(use_src, &dupl->def);
53       last_parent_instr = nir_src_parent_instr(use_src);
54       last_dupl = dupl;
55    }
56 
57    last_dupl = NULL;
58    nir_if *last_parent_if = NULL;
59 
60    nir_foreach_if_use_safe(use_src, &load->def) {
61       nir_load_const_instr *dupl;
62       nir_if *nif = nir_src_parent_if(use_src);
63 
64       if (last_parent_if != nif) {
65          /* if 'if use', clone where it is */
66          b->cursor = nir_before_instr(&load->instr);
67 
68          dupl = nir_load_const_instr_create(b->shader, load->def.num_components,
69                                             load->def.bit_size);
70          memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);
71 
72          dupl->instr.pass_flags = 1;
73          nir_builder_instr_insert(b, &dupl->instr);
74       }
75       else {
76          dupl = last_dupl;
77       }
78 
79       nir_src_rewrite(&nir_src_parent_if(use_src)->condition, &dupl->def);
80       last_parent_if = nif;
81       last_dupl = dupl;
82    }
83 
84    nir_instr_remove(&load->instr);
85    return true;
86 }
87 
88 static void
lima_nir_duplicate_load_consts_impl(nir_shader * shader,nir_function_impl * impl)89 lima_nir_duplicate_load_consts_impl(nir_shader *shader, nir_function_impl *impl)
90 {
91    nir_builder builder = nir_builder_create(impl);
92 
93    nir_foreach_block(block, impl) {
94       nir_foreach_instr(instr, block) {
95          instr->pass_flags = 0;
96       }
97 
98       nir_foreach_instr_safe(instr, block) {
99          if (instr->type != nir_instr_type_load_const)
100             continue;
101 
102          nir_load_const_instr *load = nir_instr_as_load_const(instr);
103 
104          if (load->instr.pass_flags)
105             continue;
106 
107          lima_nir_duplicate_load_const(&builder, load);
108       }
109    }
110 
111    nir_metadata_preserve(impl, nir_metadata_control_flow);
112 }
113 
114 /* Duplicate load consts for every user.
115  * Helps by utilizing the load const instruction slots that would
116  * otherwise stay empty, and reduces register pressure. */
117 void
lima_nir_duplicate_load_consts(nir_shader * shader)118 lima_nir_duplicate_load_consts(nir_shader *shader)
119 {
120    nir_foreach_function_impl(impl, shader) {
121       lima_nir_duplicate_load_consts_impl(shader, impl);
122    }
123 }
124