dd949998cc8c6f5e7a0a9be2ed8fe29bece536bd
[mesa.git] / src / compiler / nir / nir_opt_dead_write_vars.c
1 /*
2 * Copyright © 2018 Intel Corporation
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 "nir_deref.h"
27
28 #include "util/u_dynarray.h"
29
30 /**
31 * Elimination of dead writes based on derefs.
32 *
33 * Dead writes are stores and copies that write to a deref, which then gets
34 * another write before it was used (read or sourced for a copy). Those
35 * writes can be removed since they don't affect anything.
36 *
37 * For derefs that refer to a memory area that can be read after the program,
38 * the last write is considered used. The presence of certain instructions
39 * may also cause writes to be considered used, e.g. memory barrier (in this case
40 * the value must be written as other thread might use it).
41 *
42 * The write mask for store instructions is considered, so it is possible that
43 * a store is removed because of the combination of other stores overwritten
44 * its value.
45 */
46
47 /* Entry for unused_writes arrays. */
48 struct write_entry {
49 /* If NULL indicates the entry is free to be reused. */
50 nir_intrinsic_instr *intrin;
51 nir_component_mask_t mask;
52 nir_deref_instr *dst;
53 };
54
55 static void
56 clear_unused_for_modes(struct util_dynarray *unused_writes, nir_variable_mode modes)
57 {
58 util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
59 nir_variable *var = nir_deref_instr_get_variable(entry->dst);
60 if (var->data.mode & modes)
61 *entry = util_dynarray_pop(unused_writes, struct write_entry);
62 }
63 }
64
65 static void
66 clear_unused_for_read(struct util_dynarray *unused_writes, nir_deref_instr *src)
67 {
68 util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
69 if (nir_compare_derefs(src, entry->dst) & nir_derefs_may_alias_bit)
70 *entry = util_dynarray_pop(unused_writes, struct write_entry);
71 }
72 }
73
74 static bool
75 update_unused_writes(struct util_dynarray *unused_writes,
76 nir_intrinsic_instr *intrin,
77 nir_deref_instr *dst, nir_component_mask_t mask)
78 {
79 bool progress = false;
80
81 /* This pass assumes that destination of copies and stores are derefs that
82 * end in a vector or scalar (it is OK to have wildcards or indirects for
83 * arrays).
84 */
85 assert(glsl_type_is_vector_or_scalar(dst->type));
86
87 /* Find writes that are unused and can be removed. */
88 util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
89 nir_deref_compare_result comp = nir_compare_derefs(dst, entry->dst);
90 if (comp & nir_derefs_a_contains_b_bit) {
91 entry->mask &= ~mask;
92 if (entry->mask == 0) {
93 nir_instr_remove(&entry->intrin->instr);
94 *entry = util_dynarray_pop(unused_writes, struct write_entry);
95 progress = true;
96 }
97 }
98 }
99
100 /* Add the new write to the unused array. */
101 struct write_entry new_entry = {
102 .intrin = intrin,
103 .mask = mask,
104 .dst = dst,
105 };
106
107 util_dynarray_append(unused_writes, struct write_entry, new_entry);
108
109 return progress;
110 }
111
112 static bool
113 remove_dead_write_vars_local(void *mem_ctx, nir_block *block)
114 {
115 bool progress = false;
116
117 struct util_dynarray unused_writes;
118 util_dynarray_init(&unused_writes, mem_ctx);
119
120 nir_foreach_instr_safe(instr, block) {
121 if (instr->type == nir_instr_type_call) {
122 clear_unused_for_modes(&unused_writes, nir_var_shader_out |
123 nir_var_global |
124 nir_var_local |
125 nir_var_shader_storage |
126 nir_var_shared);
127 continue;
128 }
129
130 if (instr->type != nir_instr_type_intrinsic)
131 continue;
132
133 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
134 switch (intrin->intrinsic) {
135 case nir_intrinsic_barrier:
136 case nir_intrinsic_memory_barrier: {
137 clear_unused_for_modes(&unused_writes, nir_var_shader_out |
138 nir_var_shader_storage |
139 nir_var_shared);
140 break;
141 }
142
143 case nir_intrinsic_emit_vertex:
144 case nir_intrinsic_emit_vertex_with_counter: {
145 clear_unused_for_modes(&unused_writes, nir_var_shader_out);
146 break;
147 }
148
149 case nir_intrinsic_load_deref: {
150 nir_deref_instr *src = nir_src_as_deref(intrin->src[0]);
151 clear_unused_for_read(&unused_writes, src);
152 break;
153 }
154
155 case nir_intrinsic_store_deref: {
156 nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
157 nir_component_mask_t mask = nir_intrinsic_write_mask(intrin);
158 progress |= update_unused_writes(&unused_writes, intrin, dst, mask);
159 break;
160 }
161
162 case nir_intrinsic_copy_deref: {
163 nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
164 nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);
165
166 /* Self-copy is removed. */
167 if (nir_compare_derefs(src, dst) & nir_derefs_equal_bit) {
168 nir_instr_remove(instr);
169 progress = true;
170 break;
171 }
172
173 clear_unused_for_read(&unused_writes, src);
174 nir_component_mask_t mask = (1 << glsl_get_vector_elements(dst->type)) - 1;
175 progress |= update_unused_writes(&unused_writes, intrin, dst, mask);
176 break;
177 }
178
179 default:
180 break;
181 }
182 }
183
184 /* All unused writes at the end of the block are kept, since we can't be
185 * sure they'll be overwritten or not with local analysis only.
186 */
187
188 return progress;
189 }
190
191 static bool
192 remove_dead_write_vars_impl(void *mem_ctx, nir_function_impl *impl)
193 {
194 bool progress = false;
195
196 nir_metadata_require(impl, nir_metadata_block_index);
197
198 nir_foreach_block(block, impl)
199 progress |= remove_dead_write_vars_local(mem_ctx, block);
200
201 if (progress) {
202 nir_metadata_preserve(impl, nir_metadata_block_index |
203 nir_metadata_dominance);
204 }
205
206 return progress;
207 }
208
209 bool
210 nir_opt_dead_write_vars(nir_shader *shader)
211 {
212 void *mem_ctx = ralloc_context(NULL);
213 bool progress = false;
214
215 nir_foreach_function(function, shader) {
216 if (!function->impl)
217 continue;
218 progress |= remove_dead_write_vars_impl(mem_ctx, function->impl);
219 }
220
221 ralloc_free(mem_ctx);
222 return progress;
223 }