summaryrefslogtreecommitdiff
path: root/src/compiler/nir/nir_lower_io_to_temporaries.c
blob: f92489b9d51cc2eaf502dde8fd12687a421de9ef (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright © 2015 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.
 */

/*
 * Implements a pass that lowers output and/or input variables to a
 * temporary plus an output variable with a single copy at each exit
 * point of the shader and/or an input variable with a single copy
 * at the entrance point of the shader.  This way the output variable
 * is only ever written once and/or input is only read once, and there
 * are no indirect outut/input accesses.
 */

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

struct lower_io_state {
   nir_shader *shader;
   nir_function_impl *entrypoint;
   struct exec_list old_outputs;
   struct exec_list old_inputs;

   /* map from temporary to new input */
   struct hash_table *input_map;
};

static void
emit_copies(nir_builder *b, struct exec_list *dest_vars,
            struct exec_list *src_vars)
{
   assert(exec_list_length(dest_vars) == exec_list_length(src_vars));

   foreach_two_lists(dest_node, dest_vars, src_node, src_vars) {
      nir_variable *dest = exec_node_data(nir_variable, dest_node, node);
      nir_variable *src = exec_node_data(nir_variable, src_node, node);

      /* No need to copy the contents of a non-fb_fetch_output output variable
       * to the temporary allocated for it, since its initial value is
       * undefined.
       */
      if (src->data.mode == nir_var_shader_out &&
          !src->data.fb_fetch_output)
         continue;

      /* Can't copy the contents of the temporary back to a read-only
       * interface variable.  The value of the temporary won't have been
       * modified by the shader anyway.
       */
      if (dest->data.read_only)
         continue;

      nir_copy_var(b, dest, src);
   }
}

static void
emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
{
   nir_builder b;
   nir_builder_init(&b, impl);

   if (state->shader->info.stage == MESA_SHADER_GEOMETRY) {
      /* For geometry shaders, we have to emit the output copies right
       * before each EmitVertex call.
       */
      nir_foreach_block(block, impl) {
         nir_foreach_instr(instr, block) {
            if (instr->type != nir_instr_type_intrinsic)
               continue;

            nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
            if (intrin->intrinsic == nir_intrinsic_emit_vertex ||
                intrin->intrinsic == nir_intrinsic_emit_vertex_with_counter) {
               b.cursor = nir_before_instr(&intrin->instr);
               emit_copies(&b, &state->shader->outputs, &state->old_outputs);
            }
         }
      }
   } else if (impl == state->entrypoint) {
      b.cursor = nir_before_block(nir_start_block(impl));
      emit_copies(&b, &state->old_outputs, &state->shader->outputs);

      /* For all other shader types, we need to do the copies right before
       * the jumps to the end block.
       */
      set_foreach(impl->end_block->predecessors, block_entry) {
         struct nir_block *block = (void *)block_entry->key;
         b.cursor = nir_after_block_before_jump(block);
         emit_copies(&b, &state->shader->outputs, &state->old_outputs);
      }
   }
}

/* For fragment shader inputs, when we lower to temporaries we'll invalidate
 * interpolateAt*() because now they'll be pointing to the temporary instead
 * of the actual variable. Since the caller presumably doesn't support
 * indirect indexing of inputs, we'll need to lower something like:
 *
 * in vec4 foo[3];
 *
 * ... = interpolateAtCentroid(foo[i]);
 *
 * to a sequence of interpolations that store to our temporary, then a
 * load at the end:
 *
 * in vec4 foo[3];
 * vec4 foo_tmp[3];
 *
 * foo_tmp[0] = interpolateAtCentroid(foo[0]);
 * foo_tmp[1] = interpolateAtCentroid(foo[1]);
 * ... = foo_tmp[i];
 */

/*
 * Recursively emit the interpolation instructions. Here old_interp_deref
 * refers to foo[i], temp_deref is foo_tmp[0/1], and new_interp_deref is
 * foo[0/1].
 */

static void
emit_interp(nir_builder *b, nir_deref_instr **old_interp_deref,
            nir_deref_instr *temp_deref, nir_deref_instr *new_interp_deref,
            nir_intrinsic_instr *interp)
{
   while (*old_interp_deref) {
      switch ((*old_interp_deref)->deref_type) {
      case nir_deref_type_struct:
         temp_deref =
            nir_build_deref_struct(b, temp_deref,
                                   (*old_interp_deref)->strct.index);
         new_interp_deref =
            nir_build_deref_struct(b, new_interp_deref,
                                   (*old_interp_deref)->strct.index);
         break;
      case nir_deref_type_array:
         if (nir_src_is_const((*old_interp_deref)->arr.index)) {
            temp_deref =
               nir_build_deref_array(b, temp_deref,
                                     (*old_interp_deref)->arr.index.ssa);
            new_interp_deref =
               nir_build_deref_array(b, new_interp_deref,
                                     (*old_interp_deref)->arr.index.ssa);
            break;
         } else {
            /* We have an indirect deref, so we have to emit interpolations
             * for every index. Recurse in case we have an array of arrays.
             */
            unsigned length = glsl_get_length(temp_deref->type);
            for (unsigned i = 0; i < length; i++) {
               nir_deref_instr *new_temp =
                  nir_build_deref_array_imm(b, temp_deref, i);
               nir_deref_instr *new_interp =
                  nir_build_deref_array_imm(b, new_interp_deref, i);

               emit_interp(b, old_interp_deref + 1, new_temp, new_interp,
                           interp);
            }

            return;
         }

      case nir_deref_type_var:
      case nir_deref_type_array_wildcard:
      case nir_deref_type_ptr_as_array:
      case nir_deref_type_cast:
         unreachable("bad deref type");
      }

      old_interp_deref++;
   }

   /* Now that we've constructed a fully-qualified deref with all the indirect
    * derefs replaced with direct ones, it's time to actually emit the new
    * interpolation instruction.
    */

   nir_intrinsic_instr *new_interp =
      nir_intrinsic_instr_create(b->shader, interp->intrinsic);

   new_interp->src[0] = nir_src_for_ssa(&new_interp_deref->dest.ssa);
   if (interp->intrinsic == nir_intrinsic_interp_deref_at_sample ||
       interp->intrinsic == nir_intrinsic_interp_deref_at_offset) {
      new_interp->src[1] = interp->src[1];
   }

   new_interp->num_components = interp->num_components;
   nir_ssa_dest_init(&new_interp->instr, &new_interp->dest,
                     interp->dest.ssa.num_components,
                     interp->dest.ssa.bit_size, NULL);

   nir_builder_instr_insert(b, &new_interp->instr);
   nir_store_deref(b, temp_deref, &new_interp->dest.ssa,
                   (1 << interp->dest.ssa.num_components) - 1);
}

static void
fixup_interpolation_instr(struct lower_io_state *state,
                          nir_intrinsic_instr *interp, nir_builder *b)
{
   nir_deref_path interp_path;
   nir_deref_path_init(&interp_path, nir_src_as_deref(interp->src[0]), NULL);

   b->cursor = nir_before_instr(&interp->instr);

   /* The original interpolation instruction should contain a deref path
    * starting with the original variable, which is now the temporary.
    */
   nir_deref_instr *temp_root = interp_path.path[0];

   /* Fish out the newly-created input variable. */
   assert(temp_root->deref_type == nir_deref_type_var);
   struct hash_entry *entry = _mesa_hash_table_search(state->input_map,
                                                      temp_root->var);
   assert(entry);
   nir_variable *input = entry->data;
   nir_deref_instr *input_root = nir_build_deref_var(b, input);

   /* Emit the interpolation instructions. */
   emit_interp(b, interp_path.path + 1, temp_root, input_root, interp);

   /* Now the temporary contains the interpolation results, and we can just
    * load from it. We can reuse the original deref, since it points to the
    * correct part of the temporary.
    */
   nir_ssa_def *load = nir_load_deref(b, nir_src_as_deref(interp->src[0]));
   nir_ssa_def_rewrite_uses(&interp->dest.ssa, nir_src_for_ssa(load));
   nir_instr_remove(&interp->instr);

   nir_deref_path_finish(&interp_path);
}

static void
fixup_interpolation(struct lower_io_state *state, nir_function_impl *impl,
                    nir_builder *b)
{
   nir_foreach_block(block, impl) {
      nir_foreach_instr_safe(instr, block) {
         if (instr->type != nir_instr_type_intrinsic)
            continue;

         nir_intrinsic_instr *interp = nir_instr_as_intrinsic(instr);
         
         if (interp->intrinsic == nir_intrinsic_interp_deref_at_centroid ||
             interp->intrinsic == nir_intrinsic_interp_deref_at_sample ||
             interp->intrinsic == nir_intrinsic_interp_deref_at_offset) {
            fixup_interpolation_instr(state, interp, b);
         }
      }
   }
}

static void
emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
{
   if (impl == state->entrypoint) {
      nir_builder b;
      nir_builder_init(&b, impl);
      b.cursor = nir_before_block(nir_start_block(impl));
      emit_copies(&b, &state->old_inputs, &state->shader->inputs);
      if (state->shader->info.stage == MESA_SHADER_FRAGMENT)
         fixup_interpolation(state, impl, &b);
   }
}

static nir_variable *
create_shadow_temp(struct lower_io_state *state, nir_variable *var)
{
   nir_variable *nvar = ralloc(state->shader, nir_variable);
   memcpy(nvar, var, sizeof *nvar);
   nvar->data.cannot_coalesce = true;

   /* The original is now the temporary */
   nir_variable *temp = var;

   /* Reparent the name to the new variable */
   ralloc_steal(nvar, nvar->name);

   assert(nvar->constant_initializer == NULL);

   /* Give the original a new name with @<mode>-temp appended */
   const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
   temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
   temp->data.mode = nir_var_shader_temp;
   temp->data.read_only = false;
   temp->data.fb_fetch_output = false;
   temp->data.compact = false;

   return nvar;
}

void
nir_lower_io_to_temporaries(nir_shader *shader, nir_function_impl *entrypoint,
                            bool outputs, bool inputs)
{
   struct lower_io_state state;

   if (shader->info.stage == MESA_SHADER_TESS_CTRL)
      return;

   state.shader = shader;
   state.entrypoint = entrypoint;
   state.input_map = _mesa_pointer_hash_table_create(NULL);

   if (inputs)
      exec_list_move_nodes_to(&shader->inputs, &state.old_inputs);
   else
      exec_list_make_empty(&state.old_inputs);

   if (outputs)
      exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
   else
      exec_list_make_empty(&state.old_outputs);

   /* Walk over all of the outputs turn each output into a temporary and
    * make a new variable for the actual output.
    */
   nir_foreach_variable(var, &state.old_outputs) {
      nir_variable *output = create_shadow_temp(&state, var);
      exec_list_push_tail(&shader->outputs, &output->node);
   }

   /* and same for inputs: */
   nir_foreach_variable(var, &state.old_inputs) {
      nir_variable *input = create_shadow_temp(&state, var);
      exec_list_push_tail(&shader->inputs, &input->node);
      _mesa_hash_table_insert(state.input_map, var, input);
   }

   nir_foreach_function(function, shader) {
      if (function->impl == NULL)
         continue;

      if (inputs)
         emit_input_copies_impl(&state, function->impl);

      if (outputs)
         emit_output_copies_impl(&state, function->impl);

      nir_metadata_preserve(function->impl, nir_metadata_block_index |
                                            nir_metadata_dominance);
   }

   exec_list_append(&shader->globals, &state.old_inputs);
   exec_list_append(&shader->globals, &state.old_outputs);

   nir_fixup_deref_modes(shader);

   _mesa_hash_table_destroy(state.input_map, NULL);
}