About Social Code
aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/nir/nir_opt_dead_write_vars.c
blob: b7c72eab98145ef8de5087d91b8d9e6d86538e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright © 2018 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "nir.h"
#include "nir_builder.h"
#include "nir_deref.h"

#include "util/u_dynarray.h"

/**
 * Elimination of dead writes based on derefs.
 *
 * Dead writes are stores and copies that write to a deref, which then gets
 * another write before it was used (read or sourced for a copy).  Those
 * writes can be removed since they don't affect anything.
 *
 * For derefs that refer to a memory area that can be read after the program,
 * the last write is considered used.  The presence of certain instructions
 * may also cause writes to be considered used, e.g. memory barrier (in this case
 * the value must be written as other thread might use it).
 *
 * The write mask for store instructions is considered, so it is possible that
 * a store is removed because of the combination of other stores overwritten
 * its value.
 */

/* Entry for unused_writes arrays. */
struct write_entry {
   /* If NULL indicates the entry is free to be reused. */
   nir_intrinsic_instr *intrin;
   nir_component_mask_t mask;
   nir_deref_instr *dst;
};

static void
clear_unused_for_modes(struct util_dynarray *unused_writes, nir_variable_mode modes)
{
   util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
      if (nir_deref_mode_may_be(entry->dst, modes))
         *entry = util_dynarray_pop(unused_writes, struct write_entry);
   }
}

static void
clear_unused_for_read(struct util_dynarray *unused_writes, nir_deref_instr *src)
{
   util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
      if (nir_compare_derefs(src, entry->dst) & nir_derefs_may_alias_bit)
         *entry = util_dynarray_pop(unused_writes, struct write_entry);
   }
}

static bool
update_unused_writes(struct util_dynarray *unused_writes,
                     nir_intrinsic_instr *intrin,
                     nir_deref_instr *dst, nir_component_mask_t mask)
{
   bool progress = false;

   /* This pass assumes that destination of copies and stores are derefs that
    * end in a vector or scalar (it is OK to have wildcards or indirects for
    * arrays).
    */
   assert(glsl_type_is_vector_or_scalar(dst->type));

   /* Find writes that are unused and can be removed. */
   util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
      nir_deref_compare_result comp = nir_compare_derefs(dst, entry->dst);
      if (comp & nir_derefs_a_contains_b_bit) {
         entry->mask &= ~mask;
         if (entry->mask == 0) {
            nir_instr_remove(&entry->intrin->instr);
            *entry = util_dynarray_pop(unused_writes, struct write_entry);
            progress = true;
         }
      }
   }

   /* Add the new write to the unused array. */
   struct write_entry new_entry = {
      .intrin = intrin,
      .mask = mask,
      .dst = dst,
   };

   util_dynarray_append(unused_writes, new_entry);

   return progress;
}

static bool
ends_program(nir_block *block)
{
   /* Avoid back-edges */
   if (block->cf_node.parent->type == nir_cf_node_loop)
      return false;

   /* Avoid called functions */
   if (!nir_cf_node_get_function(&block->cf_node)->function->is_entrypoint)
      return false;

   if (block->successors[0] == NULL) {
      /* This is the end block */
      assert(block->successors[1] == NULL);
      return true;
   }

   if (block->successors[1] != NULL)
      return false;

   return exec_list_is_empty(&block->successors[0]->instr_list) &&
          ends_program(block->successors[0]);
}

static bool
remove_dead_write_vars_local(nir_shader *shader, nir_block *block,
                             struct util_dynarray *unused_writes)
{
   bool progress = false;

   util_dynarray_clear(unused_writes);

   nir_foreach_instr_safe(instr, block) {
      if (instr->type == nir_instr_type_call) {
         clear_unused_for_modes(unused_writes, nir_var_shader_out |
                                                   nir_var_shader_temp |
                                                   nir_var_function_temp |
                                                   nir_var_mem_ssbo |
                                                   nir_var_mem_shared |
                                                   nir_var_mem_global);
         continue;
      }

      if (instr->type != nir_instr_type_intrinsic)
         continue;

      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
      switch (intrin->intrinsic) {
      case nir_intrinsic_barrier: {
         if (nir_intrinsic_memory_semantics(intrin) & NIR_MEMORY_RELEASE) {
            clear_unused_for_modes(unused_writes,
                                   nir_intrinsic_memory_modes(intrin));
         }
         break;
      }

      case nir_intrinsic_emit_vertex:
      case nir_intrinsic_emit_vertex_with_counter: {
         clear_unused_for_modes(unused_writes, nir_var_shader_out);
         break;
      }

      case nir_intrinsic_execute_callable:
      case nir_intrinsic_rt_execute_callable: {
         /* Mark payload as it can be used by the callee */
         nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
         clear_unused_for_read(unused_writes, src);
         break;
      }

      case nir_intrinsic_trace_ray:
      case nir_intrinsic_rt_trace_ray: {
         /* Mark payload as it can be used by the callees */
         nir_deref_instr *src = nir_src_as_deref(intrin->src[10]);
         clear_unused_for_read(unused_writes, src);
         break;
      }

      case nir_intrinsic_load_deref: {
         nir_deref_instr *src = nir_src_as_deref(intrin->src[0]);
         if (nir_deref_mode_must_be(src, nir_var_read_only_modes))
            break;
         clear_unused_for_read(unused_writes, src);
         break;
      }

      case nir_intrinsic_store_deref: {
         nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);

         if (nir_intrinsic_access(intrin) & ACCESS_VOLATILE) {
            /* Consider a volatile write to also be a sort of read.  This
             * prevents us from deleting a non-volatile write just before a
             * volatile write thanks to a non-volatile write afterwards.  It's
             * quite the corner case, but this should be safer and more
             * predictable for the programmer than allowing two non-volatile
             * writes to be combined with a volatile write between them.
             */
            clear_unused_for_read(unused_writes, dst);
            break;
         }

         nir_component_mask_t mask = nir_intrinsic_write_mask(intrin);
         progress |= update_unused_writes(unused_writes, intrin, dst, mask);
         break;
      }

      case nir_intrinsic_copy_deref: {
         nir_deref_instr *src = nir_src_as_deref(intrin->src[1]);
         nir_deref_instr *dst = nir_src_as_deref(intrin->src[0]);

         if (nir_intrinsic_dst_access(intrin) & ACCESS_VOLATILE) {
            clear_unused_for_read(unused_writes, src);
            clear_unused_for_read(unused_writes, dst);
            break;
         }

         /* Self-copy is removed. */
         if (nir_compare_derefs(src, dst) & nir_derefs_equal_bit) {
            nir_instr_remove(instr);
            progress = true;
            break;
         }

         clear_unused_for_read(unused_writes, src);
         nir_component_mask_t mask = (1 << glsl_get_vector_elements(dst->type)) - 1;
         progress |= update_unused_writes(unused_writes, intrin, dst, mask);
         break;
      }

      default:
         break;
      }
   }

   /* All unused writes at the end of the block are kept, since we can't be
    * sure they'll be overwritten or not with local analysis only.
    *
    * However, if the next block is the end of the program, then we can
    * eliminate any shared writes, since no one will ever read it again.
    */
   if (ends_program(block)) {
      util_dynarray_foreach_reverse(unused_writes, struct write_entry, entry) {
         if (nir_deref_mode_may_be(entry->dst, nir_var_mem_shared) &&
             nir_deref_mode_is(entry->dst, nir_var_mem_shared)) {
            nir_instr_remove(&entry->intrin->instr);
            progress = true;
         }
      }
   }

   return progress;
}

static bool
remove_dead_write_vars_impl(nir_shader *shader, nir_function_impl *impl,
                            struct util_dynarray *unused_writes)
{
   bool progress = false;

   nir_metadata_require(impl, nir_metadata_block_index);

   nir_foreach_block(block, impl)
      progress |= remove_dead_write_vars_local(shader, block, unused_writes);

   return nir_progress(progress, impl, nir_metadata_control_flow);
}

bool
nir_opt_dead_write_vars(nir_shader *shader)
{
   bool progress = false;

   struct util_dynarray unused_writes = UTIL_DYNARRAY_INIT;

   nir_foreach_function_impl(impl, shader) {
      progress |= remove_dead_write_vars_impl(shader, impl, &unused_writes);
   }

   util_dynarray_fini(&unused_writes);
   return progress;
}