lima: ppir: duplicate fneg and fabs if its source is an intrinsic

fneg and fabs are folded later in ppir, but having them in nir as a
separate instructions prevents duplicate_intrinsics pass from duplicating
load_input and load_uniform. Duplicate fneg and fabs, so subsequent
duplicate_intrinsic pass can duplicate the loads

shader-db:

total instructions in shared programs: 27698 -> 27675 (-0.08%)
instructions in affected programs: 2752 -> 2729 (-0.84%)
helped: 21
HURT: 2
helped stats (abs) min: 1 max: 4 x̄: 1.19 x̃: 1
helped stats (rel) min: 0.38% max: 6.67% x̄: 2.75% x̃: 0.75%
HURT stats (abs)   min: 1 max: 1 x̄: 1.00 x̃: 1
HURT stats (rel)   min: 1.89% max: 1.89% x̄: 1.89% x̃: 1.89%
95% mean confidence interval for instructions value: -1.39 -0.61
95% mean confidence interval for instructions %-change: -3.67% -1.03%
Instructions are helped.

total loops in shared programs: 2 -> 2 (0.00%)
loops in affected programs: 0 -> 0
helped: 0
HURT: 0

total spills in shared programs: 372 -> 368 (-1.08%)
spills in affected programs: 27 -> 23 (-14.81%)
helped: 4
HURT: 0

total fills in shared programs: 1224 -> 1205 (-1.55%)
fills in affected programs: 81 -> 62 (-23.46%)
helped: 4
HURT: 0

LOST:   0
GAINED: 0

Reviewed-by: Erico Nunes <nunes.erico@gmail.com>
Signed-off-by: Vasily Khoruzhick <anarsoul@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33569>
This commit is contained in:
Vasily Khoruzhick
2025-02-15 18:11:42 -08:00
committed by Marge Bot
parent 86e217e7df
commit b6fba15bd7
4 changed files with 132 additions and 0 deletions

View File

@@ -72,6 +72,7 @@ bool lima_nir_split_loads(nir_shader *shader);
void lima_nir_duplicate_load_consts(nir_shader *shader);
void lima_nir_duplicate_load_inputs(nir_shader *shader);
void lima_nir_duplicate_load_uniforms(nir_shader *shader);
void lima_nir_duplicate_modifiers(nir_shader *shader);
bool lima_nir_lower_txp(nir_shader *shader);

View File

@@ -0,0 +1,129 @@
/*
* Copyright (c) 2025 Lima Project
*
* 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"
#include "nir_builder.h"
#include "lima_ir.h"
static bool
lima_nir_duplicate_modifier(nir_builder *b, nir_alu_instr *alu,
nir_op op)
{
nir_alu_instr *last_dupl = NULL;
nir_instr *last_parent_instr = NULL;
nir_foreach_use_safe(use_src, &alu->def) {
nir_alu_instr *dupl;
if (last_parent_instr != nir_src_parent_instr(use_src)) {
/* if ssa use, clone for the target block */
b->cursor = nir_before_instr(nir_src_parent_instr(use_src));
dupl = nir_alu_instr_clone(b->shader, alu);
dupl->instr.pass_flags = 1;
nir_builder_instr_insert(b, &dupl->instr);
}
else {
dupl = last_dupl;
}
nir_src_rewrite(use_src, &dupl->def);
last_parent_instr = nir_src_parent_instr(use_src);
last_dupl = dupl;
}
last_dupl = NULL;
nir_if *last_parent_if = NULL;
nir_foreach_if_use_safe(use_src, &alu->def) {
nir_alu_instr *dupl;
nir_if *nif = nir_src_parent_if(use_src);
if (last_parent_if != nif) {
/* if 'if use', clone where it is */
b->cursor = nir_before_instr(&alu->instr);
dupl = nir_alu_instr_clone(b->shader, alu);
dupl->instr.pass_flags = 1;
nir_builder_instr_insert(b, &dupl->instr);
}
else {
dupl = last_dupl;
}
nir_src_rewrite(&nir_src_parent_if(use_src)->condition, &dupl->def);
last_parent_if = nif;
last_dupl = dupl;
}
nir_instr_remove(&alu->instr);
return true;
}
static void
lima_nir_duplicate_modifier_impl(nir_shader *shader, nir_function_impl *impl,
nir_op op)
{
nir_builder builder = nir_builder_create(impl);
nir_foreach_block(block, impl) {
nir_foreach_instr(instr, block) {
instr->pass_flags = 0;
}
nir_foreach_instr_safe(instr, block) {
if (instr->type != nir_instr_type_alu)
continue;
nir_alu_instr *alu = nir_instr_as_alu(instr);
if (alu->op != op)
continue;
if (alu->instr.pass_flags)
continue;
nir_intrinsic_instr *itr = nir_src_as_intrinsic(alu->src[0].src);
if (!itr)
continue;
if (itr->intrinsic != nir_intrinsic_load_input &&
itr->intrinsic != nir_intrinsic_load_uniform)
continue;
lima_nir_duplicate_modifier(&builder, alu, op);
}
}
nir_metadata_preserve(impl, nir_metadata_control_flow);
}
/* Duplicate load inputs for every user.
* Helps by utilizing the load input instruction slots that would
* otherwise stay empty, and reduces register pressure. */
void
lima_nir_duplicate_modifiers(nir_shader *shader)
{
nir_foreach_function_impl(impl, shader) {
lima_nir_duplicate_modifier_impl(shader, impl, nir_op_fneg);
lima_nir_duplicate_modifier_impl(shader, impl, nir_op_fabs);
}
}

View File

@@ -280,6 +280,7 @@ lima_program_optimize_fs_nir(struct nir_shader *s,
NIR_PASS_V(s, nir_opt_dce); /* clean up any new dead code from vec to movs */
NIR_PASS_V(s, lima_nir_duplicate_modifiers);
NIR_PASS_V(s, lima_nir_duplicate_load_uniforms);
NIR_PASS_V(s, lima_nir_duplicate_load_inputs);
NIR_PASS_V(s, lima_nir_duplicate_load_consts);

View File

@@ -30,6 +30,7 @@ files_lima = files(
'ir/lima_nir_duplicate_consts.c',
'ir/lima_nir_duplicate_intrinsic.c',
'ir/lima_nir_duplicate_modifiers.c',
'ir/lima_nir_lower_uniform_to_scalar.c',
'ir/lima_nir_split_load_input.c',
'ir/lima_nir_split_loads.c',