5591f9be8200b306085020da712e1bc066f11c6a
[mesa.git] / src / compiler / nir / nir_linking_helpers.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 #include "nir.h"
25 #include "util/set.h"
26 #include "util/hash_table.h"
27
28 /* This file contains various little helpers for doing simple linking in
29 * NIR. Eventually, we'll probably want a full-blown varying packing
30 * implementation in here. Right now, it just deletes unused things.
31 */
32
33 /**
34 * Returns the bits in the inputs_read, outputs_written, or
35 * system_values_read bitfield corresponding to this variable.
36 */
37 static uint64_t
38 get_variable_io_mask(nir_variable *var, gl_shader_stage stage)
39 {
40 /* TODO: add support for tess patches */
41 if (var->data.patch || var->data.location < 0)
42 return 0;
43
44 assert(var->data.mode == nir_var_shader_in ||
45 var->data.mode == nir_var_shader_out ||
46 var->data.mode == nir_var_system_value);
47 assert(var->data.location >= 0);
48
49 const struct glsl_type *type = var->type;
50 if (nir_is_per_vertex_io(var, stage)) {
51 assert(glsl_type_is_array(type));
52 type = glsl_get_array_element(type);
53 }
54
55 unsigned slots = glsl_count_attribute_slots(type, false);
56 return ((1ull << slots) - 1) << var->data.location;
57 }
58
59 static void
60 tcs_add_output_reads(nir_shader *shader, uint64_t *read)
61 {
62 nir_foreach_function(function, shader) {
63 if (function->impl) {
64 nir_foreach_block(block, function->impl) {
65 nir_foreach_instr(instr, block) {
66 if (instr->type != nir_instr_type_intrinsic)
67 continue;
68
69 nir_intrinsic_instr *intrin_instr =
70 nir_instr_as_intrinsic(instr);
71 if (intrin_instr->intrinsic == nir_intrinsic_load_var &&
72 intrin_instr->variables[0]->var->data.mode ==
73 nir_var_shader_out) {
74
75 nir_variable *var = intrin_instr->variables[0]->var;
76 read[var->data.location_frac] |=
77 get_variable_io_mask(intrin_instr->variables[0]->var,
78 shader->stage);
79 }
80 }
81 }
82 }
83 }
84 }
85
86 static bool
87 remove_unused_io_vars(nir_shader *shader, struct exec_list *var_list,
88 uint64_t *used_by_other_stage)
89 {
90 bool progress = false;
91
92 nir_foreach_variable_safe(var, var_list) {
93 /* TODO: add patch support */
94 if (var->data.patch)
95 continue;
96
97 if (var->data.location < VARYING_SLOT_VAR0 && var->data.location >= 0)
98 continue;
99
100 if (var->data.always_active_io)
101 continue;
102
103 uint64_t other_stage = used_by_other_stage[var->data.location_frac];
104
105 if (!(other_stage & get_variable_io_mask(var, shader->stage))) {
106 /* This one is invalid, make it a global variable instead */
107 var->data.location = 0;
108 var->data.mode = nir_var_global;
109
110 exec_node_remove(&var->node);
111 exec_list_push_tail(&shader->globals, &var->node);
112
113 progress = true;
114 }
115 }
116
117 return progress;
118 }
119
120 bool
121 nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer)
122 {
123 assert(producer->stage != MESA_SHADER_FRAGMENT);
124 assert(consumer->stage != MESA_SHADER_VERTEX);
125
126 uint64_t read[4] = { 0 }, written[4] = { 0 };
127
128 nir_foreach_variable(var, &producer->outputs) {
129 written[var->data.location_frac] |=
130 get_variable_io_mask(var, producer->stage);
131 }
132
133 nir_foreach_variable(var, &consumer->inputs) {
134 read[var->data.location_frac] |=
135 get_variable_io_mask(var, consumer->stage);
136 }
137
138 /* Each TCS invocation can read data written by other TCS invocations,
139 * so even if the outputs are not used by the TES we must also make
140 * sure they are not read by the TCS before demoting them to globals.
141 */
142 if (producer->stage == MESA_SHADER_TESS_CTRL)
143 tcs_add_output_reads(producer, read);
144
145 bool progress = false;
146 progress = remove_unused_io_vars(producer, &producer->outputs, read);
147
148 progress =
149 remove_unused_io_vars(consumer, &consumer->inputs, written) || progress;
150
151 return progress;
152 }