nir: Merge redudant integer clamping.
[mesa.git] / src / compiler / 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, nir_function *entrypoint)
78 {
79 struct lower_outputs_state state;
80
81 if (shader->stage == MESA_SHADER_TESS_CTRL)
82 return;
83
84 state.shader = shader;
85 exec_list_move_nodes_to(&shader->outputs, &state.old_outputs);
86
87 /* Walk over all of the outputs turn each output into a temporary and
88 * make a new variable for the actual output.
89 */
90 nir_foreach_variable(var, &state.old_outputs) {
91 nir_variable *output = ralloc(shader, nir_variable);
92 memcpy(output, var, sizeof *output);
93
94 /* The orignal is now the temporary */
95 nir_variable *temp = var;
96
97 /* Reparent the name to the new variable */
98 ralloc_steal(output, output->name);
99
100 /* Reparent the constant initializer (if any) */
101 ralloc_steal(output, output->constant_initializer);
102
103 /* Give the output a new name with @out-temp appended */
104 temp->name = ralloc_asprintf(var, "%s@out-temp", output->name);
105 temp->data.mode = nir_var_global;
106 temp->constant_initializer = NULL;
107
108 exec_list_push_tail(&shader->outputs, &output->node);
109 }
110
111 nir_foreach_function(shader, function) {
112 if (function->impl == NULL)
113 continue;
114
115 if (shader->stage == MESA_SHADER_GEOMETRY) {
116 /* For geometry shaders, we have to emit the output copies right
117 * before each EmitVertex call.
118 */
119 nir_foreach_block(function->impl, emit_output_copies_block, &state);
120 } else if (function == entrypoint) {
121 /* For all other shader types, we need to do the copies right before
122 * the jumps to the end block.
123 */
124 struct set_entry *block_entry;
125 set_foreach(function->impl->end_block->predecessors, block_entry) {
126 struct nir_block *block = (void *)block_entry->key;
127 emit_output_copies(nir_after_block_before_jump(block), &state);
128 }
129 }
130
131 nir_metadata_preserve(function->impl, nir_metadata_block_index |
132 nir_metadata_dominance);
133 }
134
135 exec_list_append(&shader->globals, &state.old_outputs);
136 }