/* * Copyright © 2014-2015 Broadcom * Copyright © 2021 Google * * 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_builder.h" nir_ssa_def * nir_builder_alu_instr_finish_and_insert(nir_builder *build, nir_alu_instr *instr) { const nir_op_info *op_info = &nir_op_infos[instr->op]; instr->exact = build->exact; /* Guess the number of components the destination temporary should have * based on our input sizes, if it's not fixed for the op. */ unsigned num_components = op_info->output_size; if (num_components == 0) { for (unsigned i = 0; i < op_info->num_inputs; i++) { if (op_info->input_sizes[i] == 0) num_components = MAX2(num_components, instr->src[i].src.ssa->num_components); } } assert(num_components != 0); /* Figure out the bitwidth based on the source bitwidth if the instruction * is variable-width. */ unsigned bit_size = nir_alu_type_get_type_size(op_info->output_type); if (bit_size == 0) { for (unsigned i = 0; i < op_info->num_inputs; i++) { unsigned src_bit_size = instr->src[i].src.ssa->bit_size; if (nir_alu_type_get_type_size(op_info->input_types[i]) == 0) { if (bit_size) assert(src_bit_size == bit_size); else bit_size = src_bit_size; } else { assert(src_bit_size == nir_alu_type_get_type_size(op_info->input_types[i])); } } } /* When in doubt, assume 32. */ if (bit_size == 0) bit_size = 32; /* Make sure we don't swizzle from outside of our source vector (like if a * scalar value was passed into a multiply with a vector). */ for (unsigned i = 0; i < op_info->num_inputs; i++) { for (unsigned j = instr->src[i].src.ssa->num_components; j < NIR_MAX_VEC_COMPONENTS; j++) { instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1; } } nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, bit_size, NULL); instr->dest.write_mask = (1 << num_components) - 1; nir_builder_instr_insert(build, &instr->instr); return &instr->dest.dest.ssa; }