[Mesa-dev] [PATCH 05/14] glsl: Add IR builder support for function calls.

Ian Romanick idr at freedesktop.org
Tue Oct 22 14:44:33 PDT 2013


On 10/01/2013 07:15 PM, Francisco Jerez wrote:
> ---
>  src/glsl/ir_builder.cpp | 25 +++++++++++++++++++++++++
>  src/glsl/ir_builder.h   |  8 ++++++++
>  2 files changed, 33 insertions(+)
> 
> diff --git a/src/glsl/ir_builder.cpp b/src/glsl/ir_builder.cpp
> index 98b4322..1913c72 100644
> --- a/src/glsl/ir_builder.cpp
> +++ b/src/glsl/ir_builder.cpp
> @@ -21,6 +21,7 @@
>   * IN THE SOFTWARE.
>   */
>  
> +#include <stdarg.h>
>  #include "ir_builder.h"
>  #include "program/prog_instruction.h"
>  
> @@ -535,4 +536,28 @@ if_tree(operand condition,
>     return result;
>  }
>  
> +ir_call *
> +call(ir_function *f, ir_variable *ret, int num_params, ...)

varargs functions that take an explicit count are very susceptible to
copy-and-past bugs.  If you copy-and-paste an invocation, add or remove
a parameter, and forget to change the count, you will be sad.

If possible, I'd much rather see some sort of sentinel to terminate the
list.  I haven't looked to see how ugly that would be in this case.
What do you think?

> +{
> +   void *ctx = ralloc_parent(f);
> +   exec_list actual_parameters;
> +   va_list ap;
> +
> +   va_start(ap, num_params);
> +   for (int i = 0; i < num_params; ++i)
> +      actual_parameters.push_tail(va_arg(ap, operand).val);
> +   va_end(ap);
> +
> +   ir_function_signature *sig =
> +      f->exact_matching_signature(NULL, &actual_parameters);
> +   if (!sig)
> +      return NULL;
> +
> +   ir_dereference_variable *deref =
> +      (sig->return_type->is_void() ? NULL :
> +       new(ctx) ir_dereference_variable(ret));
> +
> +   return new(ctx) ir_call(sig, deref, &actual_parameters);
> +}
> +
>  } /* namespace ir_builder */
> diff --git a/src/glsl/ir_builder.h b/src/glsl/ir_builder.h
> index 6a5f771..80b4b2c 100644
> --- a/src/glsl/ir_builder.h
> +++ b/src/glsl/ir_builder.h
> @@ -210,4 +210,12 @@ ir_if *if_tree(operand condition,
>                 ir_instruction *then_branch,
>                 ir_instruction *else_branch);
>  
> +/**
> + * Call function \param f with \param num_params parameters which are
> + * passed as a variable number of \c operand type arguments.  \param
> + * ret should point to the ir_variable that will hold the function
> + * return value, or be \c NULL if the function has void return type.
> + */
> +ir_call *call(ir_function *f, ir_variable *ret, int num_params, ...);
> +
>  } /* namespace ir_builder */
> 



More information about the mesa-dev mailing list