From d95f9d189a46ecd6fecb54cd7c733fff89f3d85a Mon Sep 17 00:00:00 2001 From: Emma Anholt Date: Thu, 3 Feb 2022 11:11:00 -0800 Subject: [PATCH] nir: Introduce a nir_vec_scalars() helper using nir_ssa_scalar. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Many users of nir_vec() do so by nir_channel()-ing a new ssa defs as movs from other vectors to put the new vector together, which then just have to get copy-propagated into the ALU srcs and DCEed away the temporary movs. If they instead take nir_ssa_scalar, we can avoid that extra work. Reviewed-by: Ian Romanick Reviewed-by: Daniel Schürmann Part-of: --- src/compiler/nir/nir_builder.c | 26 ++++++++++++++++++++++++++ src/compiler/nir/nir_builder.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/compiler/nir/nir_builder.c b/src/compiler/nir/nir_builder.c index 922807bc3ff..d208cdd11fd 100644 --- a/src/compiler/nir/nir_builder.c +++ b/src/compiler/nir/nir_builder.c @@ -215,6 +215,32 @@ nir_build_alu_src_arr(nir_builder *build, nir_op op, nir_ssa_def **srcs) return nir_builder_alu_instr_finish_and_insert(build, instr); } +nir_ssa_def * +nir_vec_scalars(nir_builder *build, nir_ssa_scalar *comp, unsigned num_components) +{ + nir_op op = nir_op_vec(num_components); + nir_alu_instr *instr = nir_alu_instr_create(build->shader, op); + if (!instr) + return NULL; + + for (unsigned i = 0; i < num_components; i++) { + instr->src[i].src = nir_src_for_ssa(comp[i].def); + instr->src[i].swizzle[0] = comp[i].comp; + } + instr->exact = build->exact; + + /* Note: not reusing nir_builder_alu_instr_finish_and_insert() because it + * can't re-guess the num_components when num_components == 1 (nir_op_mov). + */ + nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, + comp[0].def->bit_size, NULL); + instr->dest.write_mask = (1 << num_components) - 1; + + nir_builder_instr_insert(build, &instr->instr); + + return &instr->dest.dest.ssa; +} + /** * Turns a nir_src into a nir_ssa_def * so it can be passed to * nir_build_alu()-based builder calls. diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 7f5ffb52a65..02245a64267 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -371,6 +371,9 @@ nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components) return nir_build_alu_src_arr(build, nir_op_vec(num_components), comp); } +nir_ssa_def * +nir_vec_scalars(nir_builder *build, nir_ssa_scalar *comp, unsigned num_components); + static inline nir_ssa_def * nir_mov_alu(nir_builder *build, nir_alu_src src, unsigned num_components) {