2 * Copyright © 2015 Intel Corporation
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:
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
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
26 #include "util/hash_table.h"
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.
34 * Returns the bits in the inputs_read, outputs_written, or
35 * system_values_read bitfield corresponding to this variable.
38 get_variable_io_mask(nir_variable
*var
, gl_shader_stage stage
)
40 if (var
->data
.location
< 0)
43 unsigned location
= var
->data
.patch
?
44 var
->data
.location
- VARYING_SLOT_PATCH0
: var
->data
.location
;
46 assert(var
->data
.mode
== nir_var_shader_in
||
47 var
->data
.mode
== nir_var_shader_out
||
48 var
->data
.mode
== nir_var_system_value
);
49 assert(var
->data
.location
>= 0);
51 const struct glsl_type
*type
= var
->type
;
52 if (nir_is_per_vertex_io(var
, stage
)) {
53 assert(glsl_type_is_array(type
));
54 type
= glsl_get_array_element(type
);
57 unsigned slots
= glsl_count_attribute_slots(type
, false);
58 return ((1ull << slots
) - 1) << location
;
62 tcs_add_output_reads(nir_shader
*shader
, uint64_t *read
, uint64_t *patches_read
)
64 nir_foreach_function(function
, shader
) {
66 nir_foreach_block(block
, function
->impl
) {
67 nir_foreach_instr(instr
, block
) {
68 if (instr
->type
!= nir_instr_type_intrinsic
)
71 nir_intrinsic_instr
*intrin_instr
=
72 nir_instr_as_intrinsic(instr
);
73 if (intrin_instr
->intrinsic
== nir_intrinsic_load_var
&&
74 intrin_instr
->variables
[0]->var
->data
.mode
==
77 nir_variable
*var
= intrin_instr
->variables
[0]->var
;
78 if (var
->data
.patch
) {
79 patches_read
[var
->data
.location_frac
] |=
80 get_variable_io_mask(intrin_instr
->variables
[0]->var
,
83 read
[var
->data
.location_frac
] |=
84 get_variable_io_mask(intrin_instr
->variables
[0]->var
,
95 remove_unused_io_vars(nir_shader
*shader
, struct exec_list
*var_list
,
96 uint64_t *used_by_other_stage
,
97 uint64_t *used_by_other_stage_patches
)
99 bool progress
= false;
102 nir_foreach_variable_safe(var
, var_list
) {
104 used
= used_by_other_stage_patches
;
106 used
= used_by_other_stage
;
108 if (var
->data
.location
< VARYING_SLOT_VAR0
&& var
->data
.location
>= 0)
111 if (var
->data
.always_active_io
)
114 uint64_t other_stage
= used
[var
->data
.location_frac
];
116 if (!(other_stage
& get_variable_io_mask(var
, shader
->info
.stage
))) {
117 /* This one is invalid, make it a global variable instead */
118 var
->data
.location
= 0;
119 var
->data
.mode
= nir_var_global
;
121 exec_node_remove(&var
->node
);
122 exec_list_push_tail(&shader
->globals
, &var
->node
);
132 nir_remove_unused_varyings(nir_shader
*producer
, nir_shader
*consumer
)
134 assert(producer
->info
.stage
!= MESA_SHADER_FRAGMENT
);
135 assert(consumer
->info
.stage
!= MESA_SHADER_VERTEX
);
137 uint64_t read
[4] = { 0 }, written
[4] = { 0 };
138 uint64_t patches_read
[4] = { 0 }, patches_written
[4] = { 0 };
140 nir_foreach_variable(var
, &producer
->outputs
) {
141 if (var
->data
.patch
) {
142 patches_written
[var
->data
.location_frac
] |=
143 get_variable_io_mask(var
, producer
->info
.stage
);
145 written
[var
->data
.location_frac
] |=
146 get_variable_io_mask(var
, producer
->info
.stage
);
150 nir_foreach_variable(var
, &consumer
->inputs
) {
151 if (var
->data
.patch
) {
152 patches_read
[var
->data
.location_frac
] |=
153 get_variable_io_mask(var
, consumer
->info
.stage
);
155 read
[var
->data
.location_frac
] |=
156 get_variable_io_mask(var
, consumer
->info
.stage
);
160 /* Each TCS invocation can read data written by other TCS invocations,
161 * so even if the outputs are not used by the TES we must also make
162 * sure they are not read by the TCS before demoting them to globals.
164 if (producer
->info
.stage
== MESA_SHADER_TESS_CTRL
)
165 tcs_add_output_reads(producer
, read
, patches_read
);
167 bool progress
= false;
168 progress
= remove_unused_io_vars(producer
, &producer
->outputs
, read
,
171 progress
= remove_unused_io_vars(consumer
, &consumer
->inputs
, written
,
172 patches_written
) || progress
;