nir: Properly invalidate metadata in nir_lower_vec_to_movs().
[mesa.git] / src / glsl / nir / nir_lower_outputs_to_temporaries.c
1 /*
2 * Copyright © 2015 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 /*
25 * Implements a pass that lowers output variables to a temporary plus an
26 * output variable with a single copy at each exit point of the shader.
27 * This way the output variable is only ever written.
28 *
29 * Because valid NIR requires that output variables are never read, this
30 * pass is more of a helper for NIR producers and must be run before the
31 * shader is ever validated.
32 */
33
34 #include "nir.h"
35
36 struct lower_outputs_state {
37 nir_shader *shader;
38 struct exec_list old_outputs;
39 };
40
41 static void
42 emit_output_copies(nir_cursor cursor, struct lower_outputs_state *state)
43 {
44 assert(exec_list_length(&state->shader->outputs) ==
45 exec_list_length(&state->old_outputs));
46
47 foreach_two_lists(out_node, &state->shader->outputs,
48 temp_node, &state->old_outputs) {
49 nir_variable *output = exec_node_data(nir_variable, out_node, node);
50 nir_variable *temp = exec_node_data(nir_variable, temp_node, node);
51
52 nir_intrinsic_instr *copy =
53 nir_intrinsic_instr_create(state->shader, nir_intrinsic_copy_var);
54 copy->variables[0] = nir_deref_var_create(copy, output);
55 copy->variables[1] = nir_deref_var_create(copy, temp);
56
57 nir_instr_insert(cursor, &copy->instr);
58 }
59 }
60
61 static bool
62 emit_output_copies_block(nir_block *block, void *state)
63 {
64 nir_foreach_instr(block, instr) {
65 if (instr->type != nir_instr_type_intrinsic)
66 continue;
67
68 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
69 if (intrin->intrinsic == nir_intrinsic_emit_vertex)
70 emit_output_copies(nir_before_instr(&intrin->instr), state);
71 }
72
73 return true;
74 }
75
76 void
77 nir_lower_outputs_to_temporaries(nir_shader *shader)
78 {
79 struct lower_outputs_state state;
80
81 state.shader = shader;
82 exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
83
84 /* Walk over all of the outputs turn each output into a temporary and
85 * make a new variable for the actual output.
86 */
87 nir_foreach_variable(var, &state.old_outputs) {
88 nir_variable *output = ralloc(shader, nir_variable);
89 memcpy(output, var, sizeof *output);
90
91 /* The orignal is now the temporary */
92 nir_variable *temp = var;
93
94 /* Reparent the name to the new variable */
95 ralloc_steal(output, output->name);
96
97 /* Give the output a new name with @out-temp appended */
98 temp->name = ralloc_asprintf(var, "%s@out-temp", output->name);
99 temp->data.mode = nir_var_global;
100 temp->constant_initializer = NULL;
101
102 exec_list_push_tail(&shader->outputs, &output->node);
103 }
104
105 nir_foreach_overload(shader, overload) {
106 if (overload->impl == NULL)
107 continue;
108
109 if (shader->stage == MESA_SHADER_GEOMETRY) {
110 /* For geometry shaders, we have to emit the output copies right
111 * before each EmitVertex call.
112 */
113 nir_foreach_block(overload->impl, emit_output_copies_block, &state);
114 } else if (strcmp(overload->function->name, "main") == 0) {
115 /* For all other shader types, we need to do the copies right before
116 * the jumps to the end block.
117 */
118 struct set_entry *block_entry;
119 set_foreach(overload->impl->end_block->predecessors, block_entry) {
120 struct nir_block *block = (void *)block_entry->key;
121 emit_output_copies(nir_after_block_before_jump(block), &state);
122 }
123 }
124
125 nir_metadata_preserve(overload->impl, nir_metadata_block_index |
126 nir_metadata_dominance);
127 }
128
129 exec_list_append(&shader->globals, &state.old_outputs);
130 }