[Mesa-dev] [PATCH 2/3] nir: add conditional discard optimisation

Nicolai Hähnle nhaehnle at gmail.com
Wed Nov 2 15:19:06 UTC 2016


On 02.11.2016 02:26, Dave Airlie wrote:
> From: Dave Airlie <airlied at redhat.com>
>
> This is ported from GLSL and converts
>
> if (cond)
> 	discard;
>
> into
> discard_if(cond);
>
> This removes a block, but also is needed by radv
> to workaround a bug in the LLVM backend.

FYI, the GLSL pass also converts

   if (A)
     discard_if(B);

to

   discard_if(A && B);

and there are shaders in the wild where that actually happens.

For the emit_discard_if, be aware that the LLVM intrinsic is a bit 
non-intuitive: it expects a float argument, and kills when the argument 
is negative. I'm not familiar enough with NIR to judge whether patch #1 
does the right thing here.

Cheers,
Nicolai

>
> Signed-off-by: Dave Airlie <airlied at redhat.com>
> ---
>  src/compiler/Makefile.sources                  |  1 +
>  src/compiler/nir/nir.h                         |  1 +
>  src/compiler/nir/nir_opt_conditional_discard.c | 88 ++++++++++++++++++++++++++
>  3 files changed, 90 insertions(+)
>  create mode 100644 src/compiler/nir/nir_opt_conditional_discard.c
>
> diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
> index 669c499..b710cbd 100644
> --- a/src/compiler/Makefile.sources
> +++ b/src/compiler/Makefile.sources
> @@ -228,6 +228,7 @@ NIR_FILES = \
>  	nir/nir_metadata.c \
>  	nir/nir_move_vec_src_uses_to_dest.c \
>  	nir/nir_normalize_cubemap_coords.c \
> +	nir/nir_opt_conditional_discard.c \
>  	nir/nir_opt_constant_folding.c \
>  	nir/nir_opt_copy_propagate.c \
>  	nir/nir_opt_cse.c \
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h
> index 9264763..caff7a6 100644
> --- a/src/compiler/nir/nir.h
> +++ b/src/compiler/nir/nir.h
> @@ -2531,6 +2531,7 @@ bool nir_opt_remove_phis(nir_shader *shader);
>
>  bool nir_opt_undef(nir_shader *shader);
>
> +bool nir_opt_conditional_discard(nir_shader *shader);
>  void nir_sweep(nir_shader *shader);
>
>  nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
> diff --git a/src/compiler/nir/nir_opt_conditional_discard.c b/src/compiler/nir/nir_opt_conditional_discard.c
> new file mode 100644
> index 0000000..dde93df
> --- /dev/null
> +++ b/src/compiler/nir/nir_opt_conditional_discard.c
> @@ -0,0 +1,88 @@
> +/*
> + * Copyright © 2016 Red Hat
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + */
> +
> +#include "nir.h"
> +#include "nir_builder.h"
> +
> +/** @file nir_opt_conditional_discard.c
> + *
> + * Handles optimization of lowering if (cond) discard to discard_if(cond).
> + */
> +
> +static bool
> +nir_opt_conditional_discard_block(nir_block *block, void *mem_ctx)
> +{
> +   if (nir_cf_node_is_first(&block->cf_node))
> +      return false;
> +
> +   nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
> +   if (prev_node->type != nir_cf_node_if)
> +      return false;
> +
> +   nir_if *if_stmt = nir_cf_node_as_if(prev_node);
> +   nir_block *then_block = nir_if_first_then_block(if_stmt);
> +   nir_block *else_block = nir_if_first_else_block(if_stmt);
> +
> +   if (else_block) {
> +      if (!exec_list_is_empty(&else_block->instr_list))
> +	 return false;
> +   }
> +
> +   if (nir_if_last_then_block(if_stmt) != then_block)
> +      return false;
> +
> +   nir_foreach_instr(instr, then_block) {
> +      if (instr->type != nir_instr_type_intrinsic)
> +	 return false;
> +
> +      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
> +      if (intrin->intrinsic != nir_intrinsic_discard)
> +	 return false;
> +
> +      nir_intrinsic_instr *discard_if = nir_intrinsic_instr_create(mem_ctx, nir_intrinsic_discard_if);
> +      nir_src_copy(&discard_if->src[0], &if_stmt->condition, discard_if);
> +
> +      nir_instr_insert_before_cf(prev_node, &discard_if->instr);
> +      nir_instr_remove(&intrin->instr);
> +      nir_cf_node_remove(&if_stmt->cf_node);
> +      break;
> +   }
> +
> +   return false;
> +}
> +
> +bool
> +nir_opt_conditional_discard(nir_shader *shader)
> +{
> +   bool progress = false;
> +
> +   nir_foreach_function(function, shader) {
> +      if (function->impl) {
> +	 void *mem_ctx = ralloc_parent(function->impl);
> +         nir_foreach_block_safe(block, function->impl) {
> +	    progress |= nir_opt_conditional_discard_block(block, mem_ctx);
> +	 }
> +      }
> +   }
> +   return progress;
> +}
>


More information about the mesa-dev mailing list