From b192f3c458010edaecfea4ec303d79873f07388b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sat, 2 Sep 2023 15:15:54 -0400 Subject: [PATCH] nir/builder: Add nir_call helper This adds an idiomatic way to insert NIR function calls with the builder. Since functions have variable numbers of arguments, this is a variadic function. v2: Define with a variadic macro instead, for safety with the argument count. Signed-off-by: Alyssa Rosenzweig Reviewed-by: Karol Herbst Reviewed-by: Faith Ekstrand Part-of: --- src/compiler/nir/nir_builder.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index d2b5702d3c9..6fe3c84e0c7 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -2000,6 +2000,32 @@ nir_goto_if(nir_builder *build, struct nir_block *target, nir_src cond, nir_builder_instr_insert(build, &jump->instr); } +static inline void +nir_build_call(nir_builder *build, nir_function *func, size_t count, + nir_def **args) +{ + assert(count == func->num_params && "parameter count must match"); + nir_call_instr *call = nir_call_instr_create(build->shader, func); + + for (unsigned i = 0; i < count; ++i) { + call->params[i] = nir_src_for_ssa(args[i]); + } + + nir_builder_instr_insert(build, &call->instr); +} + +/* + * Call a given nir_function * with a variadic number of nir_def * arguments. + * + * Defined with __VA_ARGS__ instead of va_list so we can assert the correct + * number of parameters are passed in. + */ +#define nir_call(build, func, ...) \ + do { \ + nir_def *args[] = { __VA_ARGS__ }; \ + nir_build_call(build, func, ARRAY_SIZE(args), args); \ + } while (0) + nir_def * nir_compare_func(nir_builder *b, enum compare_func func, nir_def *src0, nir_def *src1);