nir: add nir_block_get_predecessors_sorted() helper

Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3364>
This commit is contained in:
Rhys Perry
2021-04-08 16:26:38 +01:00
committed by Marge Bot
parent 21582016c5
commit 7050896be0
4 changed files with 35 additions and 44 deletions

View File

@@ -1655,6 +1655,32 @@ nir_block_get_following_loop(nir_block *block)
return nir_cf_node_as_loop(next_node);
}
static int
compare_block_index(const void *p1, const void *p2)
{
const nir_block *block1 = *((const nir_block **) p1);
const nir_block *block2 = *((const nir_block **) p2);
return (int) block1->index - (int) block2->index;
}
nir_block **
nir_block_get_predecessors_sorted(const nir_block *block, void *mem_ctx)
{
nir_block **preds =
ralloc_array(mem_ctx, nir_block *, block->predecessors->entries);
unsigned i = 0;
set_foreach(block->predecessors, entry)
preds[i++] = (nir_block *) entry->key;
assert(i == block->predecessors->entries);
qsort(preds, block->predecessors->entries, sizeof(nir_block *),
compare_block_index);
return preds;
}
void
nir_index_blocks(nir_function_impl *impl)
{

View File

@@ -4045,6 +4045,8 @@ nir_if *nir_block_get_following_if(nir_block *block);
nir_loop *nir_block_get_following_loop(nir_block *block);
nir_block **nir_block_get_predecessors_sorted(const nir_block *block, void *mem_ctx);
void nir_index_local_regs(nir_function_impl *impl);
void nir_index_ssa_defs(nir_function_impl *impl);
unsigned nir_index_instrs(nir_function_impl *impl);

View File

@@ -263,21 +263,9 @@ nir_phi_builder_value_get_block_def(struct nir_phi_builder_value *val,
return def;
}
static int
compare_blocks(const void *_a, const void *_b)
{
const nir_block * const * a = _a;
const nir_block * const * b = _b;
return (*a)->index - (*b)->index;
}
void
nir_phi_builder_finish(struct nir_phi_builder *pb)
{
const unsigned num_blocks = pb->num_blocks;
nir_block **preds = rzalloc_array(pb, nir_block *, num_blocks);
foreach_list_typed(struct nir_phi_builder_value, val, node, &pb->values) {
/* We treat the linked list of phi nodes like a worklist. The list is
* pre-populated by calls to nir_phi_builder_value_get_block_def() that
@@ -295,17 +283,10 @@ nir_phi_builder_finish(struct nir_phi_builder *pb)
exec_node_remove(&phi->instr.node);
/* Construct an array of predecessors. We sort it to ensure
* determinism in the phi insertion algorithm.
*
* XXX: Calling qsort this many times seems expensive.
*/
int num_preds = 0;
set_foreach(phi->instr.block->predecessors, entry)
preds[num_preds++] = (nir_block *)entry->key;
qsort(preds, num_preds, sizeof(*preds), compare_blocks);
/* XXX: Constructing the array this many times seems expensive. */
nir_block **preds = nir_block_get_predecessors_sorted(phi->instr.block, pb);
for (unsigned i = 0; i < num_preds; i++) {
for (unsigned i = 0; i < phi->instr.block->predecessors->entries; i++) {
nir_phi_src *src = ralloc(phi, nir_phi_src);
src->pred = preds[i];
src->src = nir_src_for_ssa(
@@ -313,6 +294,8 @@ nir_phi_builder_finish(struct nir_phi_builder *pb)
exec_list_push_tail(&phi->srcs, &src->node);
}
ralloc_free(preds);
nir_instr_insert(nir_before_block(phi->instr.block), &phi->instr);
}
}

View File

@@ -1410,15 +1410,6 @@ print_instr(const nir_instr *instr, print_state *state, unsigned tabs)
}
}
static int
compare_block_index(const void *p1, const void *p2)
{
const nir_block *block1 = *((const nir_block **) p1);
const nir_block *block2 = *((const nir_block **) p2);
return (int) block1->index - (int) block2->index;
}
static void print_cf_node(nir_cf_node *node, print_state *state,
unsigned tabs);
@@ -1430,18 +1421,7 @@ print_block(nir_block *block, print_state *state, unsigned tabs)
print_tabs(tabs, fp);
fprintf(fp, "block block_%u:\n", block->index);
/* sort the predecessors by index so we consistently print the same thing */
nir_block **preds =
malloc(block->predecessors->entries * sizeof(nir_block *));
unsigned i = 0;
set_foreach(block->predecessors, entry) {
preds[i++] = (nir_block *) entry->key;
}
qsort(preds, block->predecessors->entries, sizeof(nir_block *),
compare_block_index);
nir_block **preds = nir_block_get_predecessors_sorted(block, NULL);
print_tabs(tabs, fp);
fprintf(fp, "/* preds: ");
@@ -1450,7 +1430,7 @@ print_block(nir_block *block, print_state *state, unsigned tabs)
}
fprintf(fp, "*/\n");
free(preds);
ralloc_free(preds);
nir_foreach_instr(instr, block) {
print_instr(instr, state, tabs);