Path: blob/21.2-virgl/src/compiler/nir/nir_lower_array_deref_of_vec.c
4545 views
/*1* Copyright © 2019 Intel Corporation2*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"2526static void27build_write_masked_store(nir_builder *b, nir_deref_instr *vec_deref,28nir_ssa_def *value, unsigned component)29{30assert(value->num_components == 1);31unsigned num_components = glsl_get_components(vec_deref->type);32assert(num_components > 1 && num_components <= NIR_MAX_VEC_COMPONENTS);3334nir_ssa_def *u = nir_ssa_undef(b, 1, value->bit_size);35nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS];36for (unsigned i = 0; i < num_components; i++)37comps[i] = (i == component) ? value : u;3839nir_ssa_def *vec = nir_vec(b, comps, num_components);40nir_store_deref(b, vec_deref, vec, (1u << component));41}4243static void44build_write_masked_stores(nir_builder *b, nir_deref_instr *vec_deref,45nir_ssa_def *value, nir_ssa_def *index,46unsigned start, unsigned end)47{48if (start == end - 1) {49build_write_masked_store(b, vec_deref, value, start);50} else {51unsigned mid = start + (end - start) / 2;52nir_push_if(b, nir_ilt(b, index, nir_imm_int(b, mid)));53build_write_masked_stores(b, vec_deref, value, index, start, mid);54nir_push_else(b, NULL);55build_write_masked_stores(b, vec_deref, value, index, mid, end);56nir_pop_if(b, NULL);57}58}5960static bool61nir_lower_array_deref_of_vec_impl(nir_function_impl *impl,62nir_variable_mode modes,63nir_lower_array_deref_of_vec_options options)64{65bool progress = false;6667nir_builder b;68nir_builder_init(&b, impl);6970nir_foreach_block(block, impl) {71nir_foreach_instr_safe(instr, block) {72if (instr->type != nir_instr_type_intrinsic)73continue;7475nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);76assert(intrin->intrinsic != nir_intrinsic_copy_deref);7778if (intrin->intrinsic != nir_intrinsic_load_deref &&79intrin->intrinsic != nir_intrinsic_interp_deref_at_centroid &&80intrin->intrinsic != nir_intrinsic_interp_deref_at_sample &&81intrin->intrinsic != nir_intrinsic_interp_deref_at_offset &&82intrin->intrinsic != nir_intrinsic_interp_deref_at_vertex &&83intrin->intrinsic != nir_intrinsic_store_deref)84continue;8586nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);8788/* We choose to be conservative here. If the deref contains any89* modes which weren't specified, we bail and don't bother lowering.90*/91if (!nir_deref_mode_must_be(deref, modes))92continue;9394/* We only care about array derefs that act on vectors */95if (deref->deref_type != nir_deref_type_array)96continue;9798nir_deref_instr *vec_deref = nir_deref_instr_parent(deref);99if (!glsl_type_is_vector(vec_deref->type))100continue;101102assert(intrin->num_components == 1);103unsigned num_components = glsl_get_components(vec_deref->type);104assert(num_components > 1 && num_components <= NIR_MAX_VEC_COMPONENTS);105106b.cursor = nir_after_instr(&intrin->instr);107108if (intrin->intrinsic == nir_intrinsic_store_deref) {109assert(intrin->src[1].is_ssa);110nir_ssa_def *value = intrin->src[1].ssa;111112if (nir_src_is_const(deref->arr.index)) {113if (!(options & nir_lower_direct_array_deref_of_vec_store))114continue;115116unsigned index = nir_src_as_uint(deref->arr.index);117/* If index is OOB, we throw the old store away and don't118* replace it with anything.119*/120if (index < num_components)121build_write_masked_store(&b, vec_deref, value, index);122} else {123if (!(options & nir_lower_indirect_array_deref_of_vec_store))124continue;125126nir_ssa_def *index = nir_ssa_for_src(&b, deref->arr.index, 1);127build_write_masked_stores(&b, vec_deref, value, index,1280, num_components);129}130nir_instr_remove(&intrin->instr);131132progress = true;133} else {134if (nir_src_is_const(deref->arr.index)) {135if (!(options & nir_lower_direct_array_deref_of_vec_load))136continue;137} else {138if (!(options & nir_lower_indirect_array_deref_of_vec_load))139continue;140}141142/* Turn the load into a vector load */143nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],144nir_src_for_ssa(&vec_deref->dest.ssa));145intrin->dest.ssa.num_components = num_components;146intrin->num_components = num_components;147148nir_ssa_def *index = nir_ssa_for_src(&b, deref->arr.index, 1);149nir_ssa_def *scalar =150nir_vector_extract(&b, &intrin->dest.ssa, index);151if (scalar->parent_instr->type == nir_instr_type_ssa_undef) {152nir_ssa_def_rewrite_uses(&intrin->dest.ssa,153scalar);154nir_instr_remove(&intrin->instr);155} else {156nir_ssa_def_rewrite_uses_after(&intrin->dest.ssa,157scalar,158scalar->parent_instr);159}160progress = true;161}162}163}164165if (progress) {166nir_metadata_preserve(impl, nir_metadata_block_index |167nir_metadata_dominance);168} else {169nir_metadata_preserve(impl, nir_metadata_all);170}171172return progress;173}174175/* Lowers away array dereferences on vectors176*177* These are allowed on certain variable types such as SSBOs and TCS outputs.178* However, not everyone can actually handle them everywhere. There are also179* cases where we want to lower them for performance reasons.180*181* This patch assumes that copy_deref instructions have already been lowered.182*/183bool184nir_lower_array_deref_of_vec(nir_shader *shader, nir_variable_mode modes,185nir_lower_array_deref_of_vec_options options)186{187bool progress = false;188189nir_foreach_function(function, shader) {190if (function->impl &&191nir_lower_array_deref_of_vec_impl(function->impl, modes, options))192progress = true;193}194195return progress;196}197198199