Remove wrongly repeated words in comments
[mesa.git] / src / compiler / nir / nir_lower_io_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 and/or input variables to a
26 * temporary plus an output variable with a single copy at each exit
27 * point of the shader and/or an input variable with a single copy
28 * at the entrance point of the shader. This way the output variable
29 * is only ever written once and/or input is only read once, and there
30 * are no indirect outut/input accesses.
31 */
32
33 #include "nir.h"
34
35 struct lower_io_state {
36 nir_shader *shader;
37 nir_function *entrypoint;
38 struct exec_list old_outputs;
39 struct exec_list old_inputs;
40 };
41
42 static void
43 emit_copies(nir_cursor cursor, nir_shader *shader, struct exec_list *new_vars,
44 struct exec_list *old_vars)
45 {
46 assert(exec_list_length(new_vars) == exec_list_length(old_vars));
47
48 foreach_two_lists(new_node, new_vars, old_node, old_vars) {
49 nir_variable *newv = exec_node_data(nir_variable, new_node, node);
50 nir_variable *temp = exec_node_data(nir_variable, old_node, node);
51
52 nir_intrinsic_instr *copy =
53 nir_intrinsic_instr_create(shader, nir_intrinsic_copy_var);
54 copy->variables[0] = nir_deref_var_create(copy, newv);
55 copy->variables[1] = nir_deref_var_create(copy, temp);
56
57 nir_instr_insert(cursor, &copy->instr);
58 }
59 }
60
61 static void
62 emit_output_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
63 {
64 if (state->shader->stage == MESA_SHADER_GEOMETRY) {
65 /* For geometry shaders, we have to emit the output copies right
66 * before each EmitVertex call.
67 */
68 nir_foreach_block(block, impl) {
69 nir_foreach_instr(instr, block) {
70 if (instr->type != nir_instr_type_intrinsic)
71 continue;
72
73 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
74 if (intrin->intrinsic == nir_intrinsic_emit_vertex) {
75 nir_cursor cursor = nir_before_instr(&intrin->instr);
76 emit_copies(cursor, state->shader, &state->shader->outputs,
77 &state->old_outputs);
78 }
79 }
80 }
81 } else if (impl->function == state->entrypoint) {
82 /* For all other shader types, we need to do the copies right before
83 * the jumps to the end block.
84 */
85 struct set_entry *block_entry;
86 set_foreach(impl->end_block->predecessors, block_entry) {
87 struct nir_block *block = (void *)block_entry->key;
88 nir_cursor cursor = nir_after_block_before_jump(block);
89 emit_copies(cursor, state->shader, &state->shader->outputs,
90 &state->old_outputs);
91 }
92 }
93 }
94
95 static void
96 emit_input_copies_impl(struct lower_io_state *state, nir_function_impl *impl)
97 {
98 if (impl->function == state->entrypoint) {
99 nir_cursor cursor = nir_before_block(nir_start_block(impl));
100 emit_copies(cursor, state->shader, &state->old_inputs,
101 &state->shader->inputs);
102 }
103 }
104
105 static nir_variable *
106 create_shadow_temp(struct lower_io_state *state, nir_variable *var)
107 {
108 nir_variable *nvar = ralloc(state->shader, nir_variable);
109 memcpy(nvar, var, sizeof *nvar);
110
111 /* The original is now the temporary */
112 nir_variable *temp = var;
113
114 /* Reparent the name to the new variable */
115 ralloc_steal(nvar, nvar->name);
116
117 /* Reparent the constant initializer (if any) */
118 ralloc_steal(nvar, nvar->constant_initializer);
119
120 /* Give the original a new name with @<mode>-temp appended */
121 const char *mode = (temp->data.mode == nir_var_shader_in) ? "in" : "out";
122 temp->name = ralloc_asprintf(var, "%s@%s-temp", mode, nvar->name);
123 temp->data.mode = nir_var_global;
124 temp->constant_initializer = NULL;
125
126 return nvar;
127 }
128
129 void
130 nir_lower_io_to_temporaries(nir_shader *shader, nir_function *entrypoint,
131 bool outputs, bool inputs)
132 {
133 struct lower_io_state state;
134
135 if (shader->stage == MESA_SHADER_TESS_CTRL)
136 return;
137
138 state.shader = shader;
139 state.entrypoint = entrypoint;
140
141 if (inputs)
142 exec_list_move_nodes_to(&shader->inputs, &state.old_inputs);
143 else
144 exec_list_make_empty(&state.old_inputs);
145
146 if (outputs)
147 exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
148 else
149 exec_list_make_empty(&state.old_outputs);
150
151 /* Walk over all of the outputs turn each output into a temporary and
152 * make a new variable for the actual output.
153 */
154 nir_foreach_variable(var, &state.old_outputs) {
155 nir_variable *output = create_shadow_temp(&state, var);
156 exec_list_push_tail(&shader->outputs, &output->node);
157 }
158
159 /* and same for inputs: */
160 nir_foreach_variable(var, &state.old_inputs) {
161 nir_variable *input = create_shadow_temp(&state, var);
162 exec_list_push_tail(&shader->inputs, &input->node);
163 }
164
165 nir_foreach_function(function, shader) {
166 if (function->impl == NULL)
167 continue;
168
169 if (inputs)
170 emit_input_copies_impl(&state, function->impl);
171
172 if (outputs)
173 emit_output_copies_impl(&state, function->impl);
174
175 nir_metadata_preserve(function->impl, nir_metadata_block_index |
176 nir_metadata_dominance);
177 }
178
179 exec_list_append(&shader->globals, &state.old_inputs);
180 exec_list_append(&shader->globals, &state.old_outputs);
181 }