2016-06-13 12:47:19 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2016 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nir.h"
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_src(nir_src *src, struct set *invariants)
|
|
|
|
|
{
|
2023-08-01 12:33:53 -04:00
|
|
|
_mesa_set_add(invariants, src->ssa);
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
add_src_cb(nir_src *src, void *state)
|
|
|
|
|
{
|
|
|
|
|
add_src(src, state);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2023-08-14 10:16:23 -05:00
|
|
|
def_is_invariant(nir_def *def, struct set *invariants)
|
2016-06-13 12:47:19 -07:00
|
|
|
{
|
2023-08-14 10:16:23 -05:00
|
|
|
return _mesa_set_search(invariants, def);
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_cf_node(nir_cf_node *cf, struct set *invariants)
|
|
|
|
|
{
|
|
|
|
|
if (cf->type == nir_cf_node_if) {
|
|
|
|
|
nir_if *if_stmt = nir_cf_node_as_if(cf);
|
|
|
|
|
add_src(&if_stmt->condition, invariants);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cf->parent)
|
|
|
|
|
add_cf_node(cf->parent, invariants);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
add_var(nir_variable *var, struct set *invariants)
|
|
|
|
|
{
|
2019-06-05 16:54:40 -05:00
|
|
|
/* Because we pass the result of nir_intrinsic_get_var directly to this
|
|
|
|
|
* function, it's possible for var to be NULL if, for instance, there's a
|
|
|
|
|
* cast somewhere in the chain.
|
|
|
|
|
*/
|
|
|
|
|
if (var != NULL)
|
|
|
|
|
_mesa_set_add(invariants, var);
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2023-08-08 12:00:35 -05:00
|
|
|
var_is_invariant(nir_variable *var, struct set *invariants)
|
2016-06-13 12:47:19 -07:00
|
|
|
{
|
2019-06-05 16:54:40 -05:00
|
|
|
/* Because we pass the result of nir_intrinsic_get_var directly to this
|
|
|
|
|
* function, it's possible for var to be NULL if, for instance, there's a
|
|
|
|
|
* cast somewhere in the chain.
|
|
|
|
|
*/
|
2018-12-13 14:06:48 -06:00
|
|
|
return var && (var->data.invariant || _mesa_set_search(invariants, var));
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
propagate_invariant_instr(nir_instr *instr, struct set *invariants)
|
|
|
|
|
{
|
|
|
|
|
switch (instr->type) {
|
|
|
|
|
case nir_instr_type_alu: {
|
|
|
|
|
nir_alu_instr *alu = nir_instr_as_alu(instr);
|
2023-08-14 10:16:23 -05:00
|
|
|
if (!def_is_invariant(&alu->dest.dest.ssa, invariants))
|
2016-06-13 12:47:19 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
alu->exact = true;
|
|
|
|
|
nir_foreach_src(instr, add_src_cb, invariants);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case nir_instr_type_tex: {
|
|
|
|
|
nir_tex_instr *tex = nir_instr_as_tex(instr);
|
2023-08-14 10:16:23 -05:00
|
|
|
if (def_is_invariant(&tex->dest.ssa, invariants))
|
2016-06-13 12:47:19 -07:00
|
|
|
nir_foreach_src(instr, add_src_cb, invariants);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case nir_instr_type_intrinsic: {
|
|
|
|
|
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
|
|
|
|
|
switch (intrin->intrinsic) {
|
2018-03-17 11:22:03 -07:00
|
|
|
case nir_intrinsic_copy_deref:
|
2016-06-13 12:47:19 -07:00
|
|
|
/* If the destination is invariant then so is the source */
|
2018-04-04 20:40:33 -04:00
|
|
|
if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
|
|
|
|
|
add_var(nir_intrinsic_get_var(intrin, 1), invariants);
|
2016-06-13 12:47:19 -07:00
|
|
|
break;
|
|
|
|
|
|
2018-03-17 11:22:03 -07:00
|
|
|
case nir_intrinsic_load_deref:
|
2023-08-14 10:16:23 -05:00
|
|
|
if (def_is_invariant(&intrin->dest.ssa, invariants))
|
2018-04-04 20:40:33 -04:00
|
|
|
add_var(nir_intrinsic_get_var(intrin, 0), invariants);
|
2016-06-13 12:47:19 -07:00
|
|
|
break;
|
|
|
|
|
|
2018-03-17 11:22:03 -07:00
|
|
|
case nir_intrinsic_store_deref:
|
2018-04-04 20:40:33 -04:00
|
|
|
if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
|
2018-03-17 11:22:03 -07:00
|
|
|
add_src(&intrin->src[1], invariants);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-06-13 12:47:19 -07:00
|
|
|
default:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-24 11:02:00 +01:00
|
|
|
FALLTHROUGH;
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-17 11:22:03 -07:00
|
|
|
case nir_instr_type_deref:
|
2016-06-13 12:47:19 -07:00
|
|
|
case nir_instr_type_jump:
|
|
|
|
|
case nir_instr_type_ssa_undef:
|
|
|
|
|
case nir_instr_type_load_const:
|
|
|
|
|
break; /* Nothing to do */
|
|
|
|
|
|
|
|
|
|
case nir_instr_type_phi: {
|
|
|
|
|
nir_phi_instr *phi = nir_instr_as_phi(instr);
|
2023-08-14 10:16:23 -05:00
|
|
|
if (!def_is_invariant(&phi->dest.ssa, invariants))
|
2016-06-13 12:47:19 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
nir_foreach_phi_src(src, phi) {
|
|
|
|
|
add_src(&src->src, invariants);
|
|
|
|
|
add_cf_node(&src->pred->cf_node, invariants);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case nir_instr_type_call:
|
|
|
|
|
unreachable("This pass must be run after function inlining");
|
|
|
|
|
|
|
|
|
|
case nir_instr_type_parallel_copy:
|
|
|
|
|
default:
|
|
|
|
|
unreachable("Cannot have this instruction type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
propagate_invariant_impl(nir_function_impl *impl, struct set *invariants)
|
|
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
uint32_t prev_entries = invariants->entries;
|
|
|
|
|
|
|
|
|
|
nir_foreach_block_reverse(block, impl) {
|
|
|
|
|
nir_foreach_instr_reverse(instr, block)
|
|
|
|
|
propagate_invariant_instr(instr, invariants);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Keep running until we make no more progress. */
|
|
|
|
|
if (invariants->entries > prev_entries) {
|
|
|
|
|
progress = true;
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress) {
|
|
|
|
|
nir_metadata_preserve(impl, nir_metadata_block_index |
|
2023-08-08 12:00:35 -05:00
|
|
|
nir_metadata_dominance |
|
|
|
|
|
nir_metadata_live_ssa_defs);
|
2021-08-10 12:43:05 +02:00
|
|
|
} else {
|
|
|
|
|
nir_metadata_preserve(impl, nir_metadata_all);
|
2016-06-13 12:47:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 15:50:52 +00:00
|
|
|
/* If invariant_prim=true, this pass considers all geometry-affecting
|
|
|
|
|
* outputs as invariant. Doing this works around a common class of application
|
|
|
|
|
* bugs appearing as flickering.
|
|
|
|
|
*/
|
2016-06-13 12:47:19 -07:00
|
|
|
bool
|
2021-01-13 15:50:52 +00:00
|
|
|
nir_propagate_invariant(nir_shader *shader, bool invariant_prim)
|
2016-06-13 12:47:19 -07:00
|
|
|
{
|
|
|
|
|
/* Hash set of invariant things */
|
2019-01-11 11:50:53 -08:00
|
|
|
struct set *invariants = _mesa_pointer_set_create(NULL);
|
2016-06-13 12:47:19 -07:00
|
|
|
|
2021-01-13 15:50:52 +00:00
|
|
|
if (shader->info.stage != MESA_SHADER_FRAGMENT && invariant_prim) {
|
|
|
|
|
nir_foreach_shader_out_variable(var, shader) {
|
|
|
|
|
switch (var->data.location) {
|
|
|
|
|
case VARYING_SLOT_POS:
|
|
|
|
|
case VARYING_SLOT_PSIZ:
|
|
|
|
|
case VARYING_SLOT_CLIP_DIST0:
|
|
|
|
|
case VARYING_SLOT_CLIP_DIST1:
|
|
|
|
|
case VARYING_SLOT_CULL_DIST0:
|
|
|
|
|
case VARYING_SLOT_CULL_DIST1:
|
|
|
|
|
case VARYING_SLOT_TESS_LEVEL_OUTER:
|
|
|
|
|
case VARYING_SLOT_TESS_LEVEL_INNER:
|
|
|
|
|
if (!var->data.invariant)
|
|
|
|
|
_mesa_set_add(invariants, var);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 12:47:19 -07:00
|
|
|
bool progress = false;
|
2023-06-22 13:27:59 -04:00
|
|
|
nir_foreach_function_impl(impl, shader) {
|
|
|
|
|
if (propagate_invariant_impl(impl, invariants))
|
2016-06-13 12:47:19 -07:00
|
|
|
progress = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mesa_set_destroy(invariants, NULL);
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|