nir: add nir_lower_alu_vec8_16_srcs pass

This pass is useful for vector based backends as we might end up with alu
instructions referencing vec8/vec16 values even though being vec4 or
smaller themselves.

This new pass intents to clean up any use of vec8/vec16 sources other
passes won't.

Signed-off-by: Karol Herbst <kherbst@redhat.com>
Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25330>
This commit is contained in:
Karol Herbst 2023-09-21 14:03:55 +02:00 committed by Marge Bot
parent 284ab9af99
commit 807ff7ed01
2 changed files with 44 additions and 0 deletions

View file

@ -5317,6 +5317,7 @@ bool nir_scale_fdiv(nir_shader *shader);
bool nir_lower_alu_to_scalar(nir_shader *shader, nir_instr_filter_cb cb, const void *data);
bool nir_lower_alu_width(nir_shader *shader, nir_vectorize_cb cb, const void *data);
bool nir_lower_alu_vec8_16_srcs(nir_shader *shader);
bool nir_lower_bool_to_bitsize(nir_shader *shader);
bool nir_lower_bool_to_float(nir_shader *shader, bool has_fcsel_ne);
bool nir_lower_bool_to_int32(nir_shader *shader);

View file

@ -454,3 +454,46 @@ nir_lower_alu_to_scalar(nir_shader *shader, nir_instr_filter_cb cb, const void *
return nir_lower_alu_width(shader, cb ? scalar_cb : NULL, &data);
}
static bool
lower_alu_vec8_16_src(nir_builder *b, nir_instr *instr, void *_data)
{
if (instr->type != nir_instr_type_alu)
return false;
nir_alu_instr *alu = nir_instr_as_alu(instr);
const nir_op_info *info = &nir_op_infos[alu->op];
bool changed = false;
b->cursor = nir_before_instr(instr);
for (int i = 0; i < info->num_inputs; i++) {
if (alu->src[i].src.ssa->num_components < 8 || info->input_sizes[i])
continue;
changed = true;
nir_def *comps[4];
for (int c = 0; c < alu->def.num_components; c++) {
unsigned swizzle = alu->src[i].swizzle[c];
alu->src[i].swizzle[c] = c;
nir_const_value *const_val = nir_src_as_const_value(alu->src[i].src);
if (const_val) {
comps[c] = nir_build_imm(b, 1, alu->src[i].src.ssa->bit_size, &const_val[swizzle]);
} else {
comps[c] = nir_swizzle(b, alu->src[i].src.ssa, &swizzle, 1);
}
}
nir_def *src = nir_vec(b, comps, alu->def.num_components);
nir_src_rewrite(&alu->src[i].src, src);
}
return changed;
}
bool
nir_lower_alu_vec8_16_srcs(nir_shader *shader)
{
return nir_shader_instructions_pass(shader, lower_alu_vec8_16_src,
nir_metadata_block_index | nir_metadata_dominance,
NULL);
}