Module: Mesa Branch: master Commit: 7050896be077ef74e1dbdb2ed66395173907e9c8 URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=7050896be077ef74e1dbdb2ed66395173907e9c8
Author: Rhys Perry <[email protected]> Date: Thu Apr 8 16:26:38 2021 +0100 nir: add nir_block_get_predecessors_sorted() helper Signed-off-by: Rhys Perry <[email protected]> Reviewed-by: Jason Ekstrand <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3364> --- src/compiler/nir/nir.c | 26 ++++++++++++++++++++++++++ src/compiler/nir/nir.h | 2 ++ src/compiler/nir/nir_phi_builder.c | 27 +++++---------------------- src/compiler/nir/nir_print.c | 24 ++---------------------- 4 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index 12ae5cf818f..5e81447c039 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -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) { diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 260d0ca2700..345bb25a1c9 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -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); diff --git a/src/compiler/nir/nir_phi_builder.c b/src/compiler/nir/nir_phi_builder.c index f8de1d78fc9..5525a71be0a 100644 --- a/src/compiler/nir/nir_phi_builder.c +++ b/src/compiler/nir/nir_phi_builder.c @@ -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); } } diff --git a/src/compiler/nir/nir_print.c b/src/compiler/nir/nir_print.c index 669e669cb11..af740daae5b 100644 --- a/src/compiler/nir/nir_print.c +++ b/src/compiler/nir/nir_print.c @@ -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); _______________________________________________ mesa-commit mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/mesa-commit
