From 19daa9283c146a8c50fdc0250e73a0bc366c826b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Thu, 22 Jun 2023 12:38:16 -0400 Subject: [PATCH] nir: Add nir_foreach_function_impl helper Most users of nir_foreach_function actually want the nir_function_impl, not the nir_function, and want to skip empty functions (though some graphics-specific passes sometimes fail to do that part). Add a nir_foreach_function_impl macro to make that case more ergonomic. nir_foreach_function_impl(impl, shader) { ... foo(impl) } is equivalent to: nir_foreach_function(func, shader) { if (func->impl) { ... foo(func->impl); } } Signed-off-by: Alyssa Rosenzweig Reviewed-by: Lionel Landwerlin Part-of: --- src/compiler/nir/nir.h | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 10e47314797..4e3a8a43358 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -3995,6 +3995,50 @@ typedef struct nir_shader { #define nir_foreach_function(func, shader) \ foreach_list_typed(nir_function, func, node, &(shader)->functions) +static inline nir_function * +nir_first_function_with_impl(const nir_shader *shader) +{ + foreach_list_typed(nir_function, func, node, &shader->functions) { + if (func->impl != NULL) + return func; + } + + return NULL; +} + +static inline nir_function_impl * +_nir_foreach_function_impl_next(const nir_function **it) +{ + foreach_list_typed_from(nir_function, func, node, _, (*it)->node.next) { + if (func->impl != NULL) { + *it = func; + return func->impl; + } + } + + return NULL; +} + +/* Equivalent to + * + * nir_foreach_function(func, shader) { + * if (func->impl != NULL) { + * ... + * } + * } + * + * Carefully written to ensure break/continue work in the user code. + */ + +#define nir_foreach_function_impl(it, shader) \ + for (const nir_function *_func_##it = nir_first_function_with_impl(shader); \ + _func_##it != NULL; \ + _func_##it = NULL) \ + \ + for (nir_function_impl *it = (_func_##it)->impl; \ + it != NULL; \ + it = _nir_foreach_function_impl_next(&_func_##it)) \ + static inline nir_function_impl * nir_shader_get_entrypoint(const nir_shader *shader) {