From 5a0702f351d31e19f71f7dc4b240c74a45feccf2 Mon Sep 17 00:00:00 2001 From: Georg Lehmann Date: Sat, 22 Feb 2025 13:04:51 +0100 Subject: [PATCH] nir/builder: add nir_shader_phi_pass Reviewed-by: Konstantin Seurer Reviewed-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir_builder.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 7b9dce0cdbd..949b371c61b 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -81,6 +81,8 @@ typedef bool (*nir_alu_pass_cb)(struct nir_builder *, nir_alu_instr *, void *); typedef bool (*nir_tex_pass_cb)(struct nir_builder *, nir_tex_instr *, void *); +typedef bool (*nir_phi_pass_cb)(struct nir_builder *, + nir_phi_instr *, void *); /** * Iterates over all the instructions in a NIR function and calls the given pass @@ -268,6 +270,36 @@ nir_shader_tex_pass(nir_shader *shader, nir_tex_pass_cb pass, return progress; } +/* As above, but for phis */ +static inline bool +nir_shader_phi_pass(nir_shader *shader, + nir_phi_pass_cb pass, + nir_metadata preserved, + void *cb_data) +{ + bool progress = false; + + nir_foreach_function_impl(impl, shader) { + bool func_progress = false; + nir_builder b = nir_builder_create(impl); + + nir_foreach_block_safe(block, impl) { + nir_foreach_phi_safe(phi, block) { + func_progress |= pass(&b, phi, cb_data); + } + } + + if (func_progress) { + nir_metadata_preserve(impl, preserved); + progress = true; + } else { + nir_metadata_preserve(impl, nir_metadata_all); + } + } + + return progress; +} + void nir_builder_instr_insert(nir_builder *build, nir_instr *instr); void nir_builder_instr_insert_at_top(nir_builder *build, nir_instr *instr);