lima/ppir: duplicate consts in nir
[mesa.git] / src / gallium / drivers / lima / ir / lima_nir_duplicate_consts.c
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
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 != use_src->parent_instr) {
38 /* if ssa use, clone for the target block */
39 b->cursor = nir_before_instr(use_src->parent_instr);
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_instr_rewrite_src(use_src->parent_instr, use_src, nir_src_for_ssa(&dupl->def));
53 last_parent_instr = use_src->parent_instr;
54 last_dupl = dupl;
55 }
56
57 last_dupl = NULL;
58 last_parent_instr = NULL;
59
60 nir_foreach_if_use_safe(use_src, &load->def) {
61 nir_load_const_instr *dupl;
62
63 if (last_parent_instr != use_src->parent_instr) {
64 /* if 'if use', clone where it is */
65 b->cursor = nir_before_instr(&load->instr);
66
67 dupl = nir_load_const_instr_create(b->shader, load->def.num_components,
68 load->def.bit_size);
69 memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);
70
71 dupl->instr.pass_flags = 1;
72 nir_builder_instr_insert(b, &dupl->instr);
73 }
74 else {
75 dupl = last_dupl;
76 }
77
78 nir_if_rewrite_condition(use_src->parent_if, nir_src_for_ssa(&dupl->def));
79 last_parent_instr = use_src->parent_instr;
80 last_dupl = dupl;
81 }
82
83 nir_instr_remove(&load->instr);
84 return true;
85 }
86
87 static void
88 lima_nir_duplicate_load_consts_impl(nir_shader *shader, nir_function_impl *impl)
89 {
90 nir_builder builder;
91 nir_builder_init(&builder, 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_block_index |
112 nir_metadata_dominance);
113 }
114
115 /* Duplicate load consts for every user.
116 * Helps by utilizing the load const instruction slots that would
117 * otherwise stay empty, and reduces register pressure. */
118 void
119 lima_nir_duplicate_load_consts(nir_shader *shader)
120 {
121 nir_foreach_function(function, shader) {
122 if (function->impl) {
123 lima_nir_duplicate_load_consts_impl(shader, function->impl);
124 }
125 }
126 }