2015-11-14 20:26:47 -05:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2015 Connor Abbott
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-12-18 19:05:47 +01:00
|
|
|
/**
|
|
|
|
|
* nir_opt_vectorize() aims to vectorize ALU instructions.
|
|
|
|
|
*
|
|
|
|
|
* The default vectorization width is 4.
|
|
|
|
|
* If desired, a callback function which returns the max vectorization width
|
|
|
|
|
* per instruction can be provided.
|
|
|
|
|
*
|
|
|
|
|
* The max vectorization width must be a power of 2.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
#include "nir.h"
|
|
|
|
|
#include "nir_vla.h"
|
|
|
|
|
#include "nir_builder.h"
|
|
|
|
|
#include "util/u_dynarray.h"
|
|
|
|
|
|
2020-02-27 15:04:25 +02:00
|
|
|
#define HASH(hash, data) XXH32(&data, sizeof(data), hash)
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
|
hash_src(uint32_t hash, const nir_src *src)
|
|
|
|
|
{
|
|
|
|
|
assert(src->is_ssa);
|
2019-06-20 23:05:13 -04:00
|
|
|
void *hash_data = nir_src_is_const(*src) ? NULL : src->ssa;
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2019-06-20 23:05:13 -04:00
|
|
|
return HASH(hash, hash_data);
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
2020-09-11 11:05:17 +01:00
|
|
|
hash_alu_src(uint32_t hash, const nir_alu_src *src,
|
|
|
|
|
uint32_t num_components, uint32_t max_vec)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
assert(!src->abs && !src->negate);
|
|
|
|
|
|
2020-09-11 11:05:17 +01:00
|
|
|
/* hash whether a swizzle accesses elements beyond the maximum
|
|
|
|
|
* vectorization factor:
|
|
|
|
|
* For example accesses to .x and .y are considered different variables
|
|
|
|
|
* compared to accesses to .z and .w for 16-bit vec2.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t swizzle = (src->swizzle[0] & ~(max_vec - 1));
|
|
|
|
|
hash = HASH(hash, swizzle);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
return hash_src(hash, &src->src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
2020-09-09 17:44:48 +01:00
|
|
|
hash_instr(const void *data)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
2020-09-09 17:44:48 +01:00
|
|
|
const nir_instr *instr = (nir_instr *) data;
|
|
|
|
|
assert(instr->type == nir_instr_type_alu);
|
|
|
|
|
nir_alu_instr *alu = nir_instr_as_alu(instr);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
uint32_t hash = HASH(0, alu->op);
|
|
|
|
|
hash = HASH(hash, alu->dest.dest.ssa.bit_size);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++)
|
2020-09-11 11:05:17 +01:00
|
|
|
hash = hash_alu_src(hash, &alu->src[i],
|
|
|
|
|
alu->dest.dest.ssa.num_components,
|
|
|
|
|
instr->pass_flags);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
srcs_equal(const nir_src *src1, const nir_src *src2)
|
|
|
|
|
{
|
|
|
|
|
assert(src1->is_ssa);
|
|
|
|
|
assert(src2->is_ssa);
|
|
|
|
|
|
2019-06-20 23:05:13 -04:00
|
|
|
return src1->ssa == src2->ssa ||
|
2021-01-08 15:23:07 +00:00
|
|
|
(nir_src_is_const(*src1) && nir_src_is_const(*src2));
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-09-11 11:05:17 +01:00
|
|
|
alu_srcs_equal(const nir_alu_src *src1, const nir_alu_src *src2,
|
|
|
|
|
uint32_t max_vec)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
assert(!src1->abs);
|
|
|
|
|
assert(!src1->negate);
|
|
|
|
|
assert(!src2->abs);
|
|
|
|
|
assert(!src2->negate);
|
|
|
|
|
|
2020-09-11 11:05:17 +01:00
|
|
|
uint32_t mask = ~(max_vec - 1);
|
|
|
|
|
if ((src1->swizzle[0] & mask) != (src2->swizzle[0] & mask))
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
return srcs_equal(&src1->src, &src2->src);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-09-23 15:08:13 +01:00
|
|
|
instrs_equal(const void *data1, const void *data2)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
2020-09-23 15:08:13 +01:00
|
|
|
const nir_instr *instr1 = (nir_instr *) data1;
|
|
|
|
|
const nir_instr *instr2 = (nir_instr *) data2;
|
2020-09-09 17:44:48 +01:00
|
|
|
assert(instr1->type == nir_instr_type_alu);
|
|
|
|
|
assert(instr2->type == nir_instr_type_alu);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
nir_alu_instr *alu1 = nir_instr_as_alu(instr1);
|
|
|
|
|
nir_alu_instr *alu2 = nir_instr_as_alu(instr2);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
if (alu1->op != alu2->op)
|
|
|
|
|
return false;
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
if (alu1->dest.dest.ssa.bit_size != alu2->dest.dest.ssa.bit_size)
|
|
|
|
|
return false;
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {
|
2020-09-11 11:05:17 +01:00
|
|
|
if (!alu_srcs_equal(&alu1->src[i], &alu2->src[i], instr1->pass_flags))
|
2020-09-09 17:44:48 +01:00
|
|
|
return false;
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
return true;
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-12-18 19:05:47 +01:00
|
|
|
instr_can_rewrite(nir_instr *instr)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
switch (instr->type) {
|
|
|
|
|
case nir_instr_type_alu: {
|
|
|
|
|
nir_alu_instr *alu = nir_instr_as_alu(instr);
|
|
|
|
|
|
|
|
|
|
/* Don't try and vectorize mov's. Either they'll be handled by copy
|
|
|
|
|
* prop, or they're actually necessary and trying to vectorize them
|
|
|
|
|
* would result in fighting with copy prop.
|
|
|
|
|
*/
|
|
|
|
|
if (alu->op == nir_op_mov)
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-09-09 17:44:48 +01:00
|
|
|
/* no need to hash instructions which are already vectorized */
|
2020-12-18 19:05:47 +01:00
|
|
|
if (alu->dest.dest.ssa.num_components >= instr->pass_flags)
|
2020-09-09 17:44:48 +01:00
|
|
|
return false;
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
if (nir_op_infos[alu->op].output_size != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
|
|
|
|
|
if (nir_op_infos[alu->op].input_sizes[i] != 0)
|
|
|
|
|
return false;
|
2020-09-11 11:05:17 +01:00
|
|
|
|
|
|
|
|
/* don't hash instructions which are already swizzled
|
|
|
|
|
* outside of max_components: these should better be scalarized */
|
2020-12-18 19:05:47 +01:00
|
|
|
uint32_t mask = ~(instr->pass_flags - 1);
|
|
|
|
|
for (unsigned j = 1; j < alu->dest.dest.ssa.num_components; j++) {
|
2021-01-08 15:13:44 +00:00
|
|
|
if ((alu->src[i].swizzle[0] & mask) != (alu->src[i].swizzle[j] & mask))
|
2020-09-11 11:05:17 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO support phi nodes */
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Tries to combine two instructions whose sources are different components of
|
|
|
|
|
* the same instructions into one vectorized instruction. Note that instr1
|
|
|
|
|
* should dominate instr2.
|
|
|
|
|
*/
|
|
|
|
|
static nir_instr *
|
2020-12-18 19:05:47 +01:00
|
|
|
instr_try_combine(struct set *instr_set, nir_instr *instr1, nir_instr *instr2)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
assert(instr1->type == nir_instr_type_alu);
|
|
|
|
|
assert(instr2->type == nir_instr_type_alu);
|
|
|
|
|
nir_alu_instr *alu1 = nir_instr_as_alu(instr1);
|
|
|
|
|
nir_alu_instr *alu2 = nir_instr_as_alu(instr2);
|
|
|
|
|
|
|
|
|
|
assert(alu1->dest.dest.ssa.bit_size == alu2->dest.dest.ssa.bit_size);
|
|
|
|
|
unsigned alu1_components = alu1->dest.dest.ssa.num_components;
|
|
|
|
|
unsigned alu2_components = alu2->dest.dest.ssa.num_components;
|
|
|
|
|
unsigned total_components = alu1_components + alu2_components;
|
|
|
|
|
|
2020-12-18 19:05:47 +01:00
|
|
|
assert(instr1->pass_flags == instr2->pass_flags);
|
|
|
|
|
if (total_components > instr1->pass_flags)
|
2015-11-14 20:26:47 -05:00
|
|
|
return NULL;
|
|
|
|
|
|
2023-06-26 10:42:29 -04:00
|
|
|
nir_builder b = nir_builder_create(nir_cf_node_get_function(&instr1->block->cf_node));
|
2015-11-14 20:26:47 -05:00
|
|
|
b.cursor = nir_after_instr(instr1);
|
|
|
|
|
|
|
|
|
|
nir_alu_instr *new_alu = nir_alu_instr_create(b.shader, alu1->op);
|
nir: Drop unused name from nir_ssa_dest_init
Since 624e799cc34 ("nir: Drop nir_ssa_def::name and nir_register::name"), SSA
defs don't have names, making the name argument unused. Drop it from the
signature and fix the call sites. This was done with the help of the following
Coccinelle semantic patch:
@@
expression A, B, C, D, E;
@@
-nir_ssa_dest_init(A, B, C, D, E);
+nir_ssa_dest_init(A, B, C, D);
Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Timur Kristóf <timur.kristof@gmail.com>
Reviewed-by: Emma Anholt <emma@anholt.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23078>
2023-05-17 09:08:22 -04:00
|
|
|
nir_ssa_dest_init(&new_alu->instr, &new_alu->dest.dest, total_components,
|
|
|
|
|
alu1->dest.dest.ssa.bit_size);
|
2015-11-14 20:26:47 -05:00
|
|
|
new_alu->dest.write_mask = (1 << total_components) - 1;
|
2020-09-11 11:05:17 +01:00
|
|
|
new_alu->instr.pass_flags = alu1->instr.pass_flags;
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-09-11 14:55:28 -04:00
|
|
|
/* If either channel is exact, we have to preserve it even if it's
|
|
|
|
|
* not optimal for other channels.
|
|
|
|
|
*/
|
|
|
|
|
new_alu->exact = alu1->exact || alu2->exact;
|
|
|
|
|
|
|
|
|
|
/* If all channels don't wrap, we can say that the whole vector doesn't
|
|
|
|
|
* wrap.
|
|
|
|
|
*/
|
|
|
|
|
new_alu->no_signed_wrap = alu1->no_signed_wrap && alu2->no_signed_wrap;
|
|
|
|
|
new_alu->no_unsigned_wrap = alu1->no_unsigned_wrap && alu2->no_unsigned_wrap;
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
for (unsigned i = 0; i < nir_op_infos[alu1->op].num_inputs; i++) {
|
2019-06-20 23:05:13 -04:00
|
|
|
/* handle constant merging case */
|
|
|
|
|
if (alu1->src[i].src.ssa != alu2->src[i].src.ssa) {
|
|
|
|
|
nir_const_value *c1 = nir_src_as_const_value(alu1->src[i].src);
|
|
|
|
|
nir_const_value *c2 = nir_src_as_const_value(alu2->src[i].src);
|
|
|
|
|
assert(c1 && c2);
|
2020-09-08 18:36:47 -05:00
|
|
|
nir_const_value value[NIR_MAX_VEC_COMPONENTS];
|
2019-06-20 23:05:13 -04:00
|
|
|
unsigned bit_size = alu1->src[i].src.ssa->bit_size;
|
|
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < total_components; j++) {
|
|
|
|
|
value[j].u64 = j < alu1_components ?
|
|
|
|
|
c1[alu1->src[i].swizzle[j]].u64 :
|
|
|
|
|
c2[alu2->src[i].swizzle[j - alu1_components]].u64;
|
|
|
|
|
}
|
|
|
|
|
nir_ssa_def *def = nir_build_imm(&b, total_components, bit_size, value);
|
|
|
|
|
|
|
|
|
|
new_alu->src[i].src = nir_src_for_ssa(def);
|
|
|
|
|
for (unsigned j = 0; j < total_components; j++)
|
|
|
|
|
new_alu->src[i].swizzle[j] = j;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
new_alu->src[i].src = alu1->src[i].src;
|
|
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < alu1_components; j++)
|
|
|
|
|
new_alu->src[i].swizzle[j] = alu1->src[i].swizzle[j];
|
|
|
|
|
|
|
|
|
|
for (unsigned j = 0; j < alu2_components; j++) {
|
|
|
|
|
new_alu->src[i].swizzle[j + alu1_components] =
|
|
|
|
|
alu2->src[i].swizzle[j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_builder_instr_insert(&b, &new_alu->instr);
|
|
|
|
|
|
2022-03-29 19:01:59 +02:00
|
|
|
/* update all ALU uses */
|
2015-11-14 20:26:47 -05:00
|
|
|
nir_foreach_use_safe(src, &alu1->dest.dest.ssa) {
|
2020-09-22 18:16:26 +01:00
|
|
|
nir_instr *user_instr = src->parent_instr;
|
|
|
|
|
if (user_instr->type == nir_instr_type_alu) {
|
|
|
|
|
/* Check if user is found in the hashset */
|
|
|
|
|
struct set_entry *entry = _mesa_set_search(instr_set, user_instr);
|
|
|
|
|
|
2015-11-14 20:26:47 -05:00
|
|
|
/* For ALU instructions, rewrite the source directly to avoid a
|
|
|
|
|
* round-trip through copy propagation.
|
|
|
|
|
*/
|
2020-09-22 18:16:26 +01:00
|
|
|
nir_instr_rewrite_src(user_instr, src,
|
2015-11-14 20:26:47 -05:00
|
|
|
nir_src_for_ssa(&new_alu->dest.dest.ssa));
|
2020-09-22 18:16:26 +01:00
|
|
|
|
|
|
|
|
/* Rehash user if it was found in the hashset */
|
|
|
|
|
if (entry && entry->key == user_instr) {
|
|
|
|
|
_mesa_set_remove(instr_set, entry);
|
2022-03-29 19:01:59 +02:00
|
|
|
_mesa_set_add(instr_set, user_instr);
|
2020-09-22 18:16:26 +01:00
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nir_foreach_use_safe(src, &alu2->dest.dest.ssa) {
|
|
|
|
|
if (src->parent_instr->type == nir_instr_type_alu) {
|
|
|
|
|
/* For ALU instructions, rewrite the source directly to avoid a
|
|
|
|
|
* round-trip through copy propagation.
|
|
|
|
|
*/
|
|
|
|
|
nir_instr_rewrite_src(src->parent_instr, src,
|
|
|
|
|
nir_src_for_ssa(&new_alu->dest.dest.ssa));
|
|
|
|
|
|
2022-03-29 19:01:59 +02:00
|
|
|
nir_alu_src *alu_src = container_of(src, nir_alu_src, src);
|
|
|
|
|
nir_alu_instr *use = nir_instr_as_alu(src->parent_instr);
|
|
|
|
|
unsigned components = nir_ssa_alu_instr_src_components(use, alu_src - use->src);
|
|
|
|
|
for (unsigned i = 0; i < components; i++)
|
|
|
|
|
alu_src->swizzle[i] += alu1_components;
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 19:01:59 +02:00
|
|
|
/* update all other uses if there are any */
|
|
|
|
|
unsigned swiz[NIR_MAX_VEC_COMPONENTS];
|
|
|
|
|
|
|
|
|
|
if (!nir_ssa_def_is_unused(&alu1->dest.dest.ssa)) {
|
|
|
|
|
for (unsigned i = 0; i < alu1_components; i++)
|
|
|
|
|
swiz[i] = i;
|
|
|
|
|
nir_ssa_def *new_alu1 = nir_swizzle(&b, &new_alu->dest.dest.ssa, swiz,
|
|
|
|
|
alu1_components);
|
|
|
|
|
nir_ssa_def_rewrite_uses(&alu1->dest.dest.ssa, new_alu1);
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-29 19:01:59 +02:00
|
|
|
if (!nir_ssa_def_is_unused(&alu2->dest.dest.ssa)) {
|
|
|
|
|
for (unsigned i = 0; i < alu2_components; i++)
|
|
|
|
|
swiz[i] = i + alu1_components;
|
|
|
|
|
nir_ssa_def *new_alu2 = nir_swizzle(&b, &new_alu->dest.dest.ssa, swiz,
|
|
|
|
|
alu2_components);
|
|
|
|
|
nir_ssa_def_rewrite_uses(&alu2->dest.dest.ssa, new_alu2);
|
|
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
nir_instr_remove(instr1);
|
|
|
|
|
nir_instr_remove(instr2);
|
|
|
|
|
|
|
|
|
|
return &new_alu->instr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct set *
|
|
|
|
|
vec_instr_set_create(void)
|
|
|
|
|
{
|
2020-09-23 15:08:13 +01:00
|
|
|
return _mesa_set_create(NULL, hash_instr, instrs_equal);
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
vec_instr_set_destroy(struct set *instr_set)
|
|
|
|
|
{
|
|
|
|
|
_mesa_set_destroy(instr_set, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-12-18 19:05:47 +01:00
|
|
|
vec_instr_set_add_or_rewrite(struct set *instr_set, nir_instr *instr,
|
|
|
|
|
nir_vectorize_cb filter, void *data)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
2020-12-18 19:05:47 +01:00
|
|
|
/* set max vector to instr pass flags: this is used to hash swizzles */
|
|
|
|
|
instr->pass_flags = filter ? filter(instr, data) : 4;
|
|
|
|
|
assert(util_is_power_of_two_or_zero(instr->pass_flags));
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2020-12-18 19:05:47 +01:00
|
|
|
if (!instr_can_rewrite(instr))
|
2020-10-09 11:54:46 +02:00
|
|
|
return false;
|
|
|
|
|
|
2020-09-23 15:08:13 +01:00
|
|
|
struct set_entry *entry = _mesa_set_search(instr_set, instr);
|
2015-11-14 20:26:47 -05:00
|
|
|
if (entry) {
|
2020-09-23 15:08:13 +01:00
|
|
|
nir_instr *old_instr = (nir_instr *) entry->key;
|
|
|
|
|
_mesa_set_remove(instr_set, entry);
|
2020-12-18 19:05:47 +01:00
|
|
|
nir_instr *new_instr = instr_try_combine(instr_set, old_instr, instr);
|
2020-09-23 15:08:13 +01:00
|
|
|
if (new_instr) {
|
2020-12-18 19:05:47 +01:00
|
|
|
if (instr_can_rewrite(new_instr))
|
2020-09-09 17:44:48 +01:00
|
|
|
_mesa_set_add(instr_set, new_instr);
|
2020-09-23 15:08:13 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
2020-09-23 15:08:13 +01:00
|
|
|
_mesa_set_add(instr_set, instr);
|
2015-11-14 20:26:47 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-12-18 19:05:47 +01:00
|
|
|
vectorize_block(nir_block *block, struct set *instr_set,
|
|
|
|
|
nir_vectorize_cb filter, void *data)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
|
|
nir_foreach_instr_safe(instr, block) {
|
2020-12-18 19:05:47 +01:00
|
|
|
if (vec_instr_set_add_or_rewrite(instr_set, instr, filter, data))
|
2015-11-14 20:26:47 -05:00
|
|
|
progress = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < block->num_dom_children; i++) {
|
|
|
|
|
nir_block *child = block->dom_children[i];
|
2020-12-18 19:05:47 +01:00
|
|
|
progress |= vectorize_block(child, instr_set, filter, data);
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
2020-09-23 15:08:13 +01:00
|
|
|
nir_foreach_instr_reverse(instr, block) {
|
2020-12-18 19:05:47 +01:00
|
|
|
if (instr_can_rewrite(instr))
|
2020-09-09 17:44:48 +01:00
|
|
|
_mesa_set_remove_key(instr_set, instr);
|
2020-09-23 15:08:13 +01:00
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2020-12-18 19:05:47 +01:00
|
|
|
nir_opt_vectorize_impl(nir_function_impl *impl,
|
|
|
|
|
nir_vectorize_cb filter, void *data)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
struct set *instr_set = vec_instr_set_create();
|
|
|
|
|
|
|
|
|
|
nir_metadata_require(impl, nir_metadata_dominance);
|
|
|
|
|
|
2020-12-18 19:05:47 +01:00
|
|
|
bool progress = vectorize_block(nir_start_block(impl), instr_set,
|
2020-08-27 12:49:13 -07:00
|
|
|
filter, data);
|
2015-11-14 20:26:47 -05:00
|
|
|
|
2021-08-10 12:46:55 +02:00
|
|
|
if (progress) {
|
2015-11-14 20:26:47 -05:00
|
|
|
nir_metadata_preserve(impl, nir_metadata_block_index |
|
|
|
|
|
nir_metadata_dominance);
|
2021-08-10 12:46:55 +02:00
|
|
|
} else {
|
|
|
|
|
nir_metadata_preserve(impl, nir_metadata_all);
|
|
|
|
|
}
|
2015-11-14 20:26:47 -05:00
|
|
|
|
|
|
|
|
vec_instr_set_destroy(instr_set);
|
|
|
|
|
return progress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2020-12-18 19:05:47 +01:00
|
|
|
nir_opt_vectorize(nir_shader *shader, nir_vectorize_cb filter,
|
2020-08-27 12:49:13 -07:00
|
|
|
void *data)
|
2015-11-14 20:26:47 -05:00
|
|
|
{
|
|
|
|
|
bool progress = false;
|
|
|
|
|
|
2023-06-22 13:27:59 -04:00
|
|
|
nir_foreach_function_impl(impl, shader) {
|
|
|
|
|
progress |= nir_opt_vectorize_impl(impl, filter, data);
|
2015-11-14 20:26:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
|
}
|