Path: blob/21.2-virgl/src/gallium/drivers/lima/ir/lima_nir_duplicate_consts.c
4574 views
/*1* Copyright (c) 2020 Lima Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir.h"24#include "nir_builder.h"25#include "lima_ir.h"2627static bool28lima_nir_duplicate_load_const(nir_builder *b, nir_load_const_instr *load)29{30nir_load_const_instr *last_dupl = NULL;31nir_instr *last_parent_instr = NULL;3233nir_foreach_use_safe(use_src, &load->def) {34nir_load_const_instr *dupl;3536if (last_parent_instr != use_src->parent_instr) {37/* if ssa use, clone for the target block */38b->cursor = nir_before_instr(use_src->parent_instr);3940dupl = nir_load_const_instr_create(b->shader, load->def.num_components,41load->def.bit_size);42memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);4344dupl->instr.pass_flags = 1;45nir_builder_instr_insert(b, &dupl->instr);46}47else {48dupl = last_dupl;49}5051nir_instr_rewrite_src(use_src->parent_instr, use_src, nir_src_for_ssa(&dupl->def));52last_parent_instr = use_src->parent_instr;53last_dupl = dupl;54}5556last_dupl = NULL;57last_parent_instr = NULL;5859nir_foreach_if_use_safe(use_src, &load->def) {60nir_load_const_instr *dupl;6162if (last_parent_instr != use_src->parent_instr) {63/* if 'if use', clone where it is */64b->cursor = nir_before_instr(&load->instr);6566dupl = nir_load_const_instr_create(b->shader, load->def.num_components,67load->def.bit_size);68memcpy(&dupl->value, &load->value, sizeof(*load->value) * load->def.num_components);6970dupl->instr.pass_flags = 1;71nir_builder_instr_insert(b, &dupl->instr);72}73else {74dupl = last_dupl;75}7677nir_if_rewrite_condition(use_src->parent_if, nir_src_for_ssa(&dupl->def));78last_parent_instr = use_src->parent_instr;79last_dupl = dupl;80}8182nir_instr_remove(&load->instr);83return true;84}8586static void87lima_nir_duplicate_load_consts_impl(nir_shader *shader, nir_function_impl *impl)88{89nir_builder builder;90nir_builder_init(&builder, impl);9192nir_foreach_block(block, impl) {93nir_foreach_instr(instr, block) {94instr->pass_flags = 0;95}9697nir_foreach_instr_safe(instr, block) {98if (instr->type != nir_instr_type_load_const)99continue;100101nir_load_const_instr *load = nir_instr_as_load_const(instr);102103if (load->instr.pass_flags)104continue;105106lima_nir_duplicate_load_const(&builder, load);107}108}109110nir_metadata_preserve(impl, nir_metadata_block_index |111nir_metadata_dominance);112}113114/* Duplicate load consts for every user.115* Helps by utilizing the load const instruction slots that would116* otherwise stay empty, and reduces register pressure. */117void118lima_nir_duplicate_load_consts(nir_shader *shader)119{120nir_foreach_function(function, shader) {121if (function->impl) {122lima_nir_duplicate_load_consts_impl(shader, function->impl);123}124}125}126127128