[Mesa-dev] [PATCH 3/5] compiler/list: add and use for_range_list macro

Ian Romanick idr at freedesktop.org
Tue May 10 23:34:43 UTC 2016


On 05/07/2016 03:05 PM, Nicolai Hähnle wrote:
> From: Nicolai Hähnle <nicolai.haehnle at amd.com>
> 
> This macro avoids undefined downcasting of list sentinels that crashes gcc's
> ubsan.
> ---
>  src/compiler/glsl/list.h                | 8 ++++++++
>  src/compiler/glsl/opt_tree_grafting.cpp | 5 +----
>  2 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/src/compiler/glsl/list.h b/src/compiler/glsl/list.h
> index 12389aa..f05d437 100644
> --- a/src/compiler/glsl/list.h
> +++ b/src/compiler/glsl/list.h
> @@ -719,6 +719,14 @@ inline void exec_node::insert_before(exec_list *before)
>  /**
>   * Iterate over a range [begin, end) of nodes.
>   */
> +#define for_range_list(__type, __node, __begin, __end) \
> +   for (__type *(__node), **__flag = &(__node); __flag; __flag = NULL) \
> +      for (exec_node *__cur = (__begin),                               \
> +                     *__end_stored = (__end);                          \
> +           __cur != __end_stored &&                                    \
> +           (((__node) = (__type *) __cur) || true);                    \
> +           __cur = __cur->next)
> +
>  #define for_range_list_safe(__type, __node, __begin, __end) \
>     for (__type *(__node), **__flag = &(__node); __flag; __flag = NULL) \
>        for (struct exec_node *__cur = (__begin),                        \
> diff --git a/src/compiler/glsl/opt_tree_grafting.cpp b/src/compiler/glsl/opt_tree_grafting.cpp
> index 47fca7d..539ed57 100644
> --- a/src/compiler/glsl/opt_tree_grafting.cpp
> +++ b/src/compiler/glsl/opt_tree_grafting.cpp
> @@ -323,10 +323,7 @@ try_tree_grafting(ir_assignment *start,
>        fprintf(stderr, "\n");
>     }
>  
> -   for (ir_instruction *ir = (ir_instruction *)start->next;
> -	ir != bb_last->next;
> -	ir = (ir_instruction *)ir->next) {
> -

Does this also work?

      for (exec_node *node = start->next;
           node != bb_last->next;
           node = node->next) {
         ir_insruction *const ir = (ir_instruction *) node;

> +   for_range_list(ir_instruction, ir, start->next, bb_last->next) {
>        if (debug) {
>  	 fprintf(stderr, "- ");
>  	 ir->fprint(stderr);
> 



More information about the mesa-dev mailing list