<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jul 4, 2018 at 2:22 AM, Grazvydas Ignotas <span dir="ltr"><<a href="mailto:notasas@gmail.com" target="_blank">notasas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Wed, Jul 4, 2018 at 9:13 AM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> Many fragment shaders do a discard using relatively little information<br>
> but still put the discard fairly far down in the shader for no good<br>
> reason.  If the discard is moved higher up, we can possibly avoid doing<br>
> some or almost all of the work in the shader.  When this lets us skip<br>
> texturing operations, it's an especially high win.<br>
><br>
> One of the biggest offenders here is DXVK.  The D3D APIs have different<br>
> rules for discards than OpenGL and Vulkan.  One effective way (which is<br>
> what DXVK uses) to implement DX behavior on top of GL or Vulkan is to<br>
> wait until the very end of the shader to discard.  This ends up in the<br>
> pessimal case where we always do all of the work before discarding.<br>
> This pass helps some DXVK shaders significantly.<br>
> ---<br>
>  src/compiler/Makefile.sources      |   1 +<br>
>  src/compiler/nir/meson.build       |   1 +<br>
>  src/compiler/nir/nir.h             |  10 +<br>
>  src/compiler/nir/nir_opt_<wbr>discard.c | 404 +++++++++++++++++++++++++++++<br>
>  4 files changed, 416 insertions(+)<br>
>  create mode 100644 src/compiler/nir/nir_opt_<wbr>discard.c<br>
><br>
> diff --git a/src/compiler/Makefile.<wbr>sources b/src/compiler/Makefile.<wbr>sources<br>
> index 9e3fbdc2612..8600ce81281 100644<br>
> --- a/src/compiler/Makefile.<wbr>sources<br>
> +++ b/src/compiler/Makefile.<wbr>sources<br>
> @@ -271,6 +271,7 @@ NIR_FILES = \<br>
>         nir/nir_opt_cse.c \<br>
>         nir/nir_opt_dce.c \<br>
>         nir/nir_opt_dead_cf.c \<br>
> +       nir/nir_opt_discard.c \<br>
>         nir/nir_opt_gcm.c \<br>
>         nir/nir_opt_global_to_local.c \<br>
>         nir/nir_opt_if.c \<br>
> diff --git a/src/compiler/nir/meson.build b/src/compiler/nir/meson.build<br>
> index 28aa8de7014..e339258bb94 100644<br>
> --- a/src/compiler/nir/meson.build<br>
> +++ b/src/compiler/nir/meson.build<br>
> @@ -156,6 +156,7 @@ files_libnir = files(<br>
>    'nir_opt_cse.c',<br>
>    'nir_opt_dce.c',<br>
>    'nir_opt_dead_cf.c',<br>
> +  'nir_opt_discard.c',<br>
>    'nir_opt_gcm.c',<br>
>    'nir_opt_global_to_local.c',<br>
>    'nir_opt_if.c',<br>
> diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h<br>
> index 92ab3a699cc..44006dac160 100644<br>
> --- a/src/compiler/nir/nir.h<br>
> +++ b/src/compiler/nir/nir.h<br>
> @@ -2022,6 +2022,13 @@ typedef struct nir_shader_compiler_options {<br>
>      */<br>
>     bool vs_inputs_dual_locations;<br>
><br>
> +   /**<br>
> +    * Whether or not derivatives are still a safe operation after a discard<br>
> +    * has occurred.  Optimization passes may be able to be a bit more<br>
> +    * agressive if this is true.<br>
> +    */<br>
> +   bool derivatives_safe_after_<wbr>discard;<br>
> +<br>
>     unsigned max_unroll_iterations;<br>
>  } nir_shader_compiler_options;<br>
><br>
> @@ -2899,6 +2906,9 @@ bool nir_opt_dce(nir_shader *shader);<br>
><br>
>  bool nir_opt_dead_cf(nir_shader *shader);<br>
><br>
> +bool nir_opt_discard_if(nir_shader *shader);<br>
> +bool nir_opt_move_discards_to_top(<wbr>nir_shader *shader);<br>
> +<br>
>  bool nir_opt_gcm(nir_shader *shader, bool value_number);<br>
><br>
>  bool nir_opt_if(nir_shader *shader);<br>
> diff --git a/src/compiler/nir/nir_opt_<wbr>discard.c b/src/compiler/nir/nir_opt_<wbr>discard.c<br>
> new file mode 100644<br>
> index 00000000000..eb1a8296282<br>
> --- /dev/null<br>
> +++ b/src/compiler/nir/nir_opt_<wbr>discard.c<br>
> @@ -0,0 +1,404 @@<br>
> +/*<br>
> + * Copyright © 2014 Intel Corporation<br>
<br>
</div></div>2018?<br><div><div class="h5"></div></div></blockquote><div><br></div><div>Yeah...<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the next<br>
> + * paragraph) shall be included in all copies or substantial portions of the<br>
> + * Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
> + * IN THE SOFTWARE.<br>
> + *<br>
> + * Authors:<br>
> + *    Jason Ekstrand (<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>)<br>
> + *<br>
> + */<br>
> +<br>
> +#include "nir.h"<br>
> +#include "nir_builder.h"<br>
> +#include "nir_control_flow.h"<br>
> +#include "nir_worklist.h"<br>
> +<br>
> +static bool<br>
> +block_has_only_discard(nir_<wbr>block *block)<br>
> +{<br>
> +   nir_instr *instr = nir_block_first_instr(block);<br>
> +   if (instr == NULL || instr != nir_block_last_instr(block))<br>
> +      return false;<br>
> +<br>
> +   if (instr->type != nir_instr_type_intrinsic)<br>
> +      return false;<br>
> +<br>
> +   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
> +   return intrin->intrinsic == nir_intrinsic_discard;<br>
> +}<br>
> +<br>
> +static bool<br>
> +opt_discard_if_impl(nir_<wbr>function_impl *impl)<br>
> +{<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_builder b;<br>
> +   nir_builder_init(&b, impl);<br>
> +<br>
> +   nir_foreach_block(block, impl) {<br>
> +      nir_if *nif = nir_block_get_following_if(<wbr>block);<br>
> +      if (!nif)<br>
> +         continue;<br>
> +<br>
> +      bool discard_in_then;<br>
> +      if (block_has_only_discard(nir_<wbr>if_first_then_block(nif)))<br>
> +         discard_in_then = true;<br>
> +      else if (block_has_only_discard(nir_<wbr>if_first_else_block(nif)))<br>
> +         discard_in_then = false;<br>
> +      else<br>
> +         continue;<br>
> +<br>
> +      b.cursor = nir_after_block(block);<br>
> +      nir_ssa_def *cond = nir_ssa_for_src(&b, nif->condition, 1);<br>
> +      if (!discard_in_then)<br>
> +         cond = nir_inot(&b, cond);<br>
> +<br>
> +      nir_intrinsic_instr *discard_if =<br>
> +         nir_intrinsic_instr_create(b.<wbr>shader, nir_intrinsic_discard_if);<br>
> +      discard_if->src[0] = nir_src_for_ssa(cond);<br>
> +      nir_builder_instr_insert(&b, &discard_if->instr);<br>
> +<br>
> +      nir_lower_phis_to_regs_block(<wbr>nir_cf_node_as_block(<br>
> +                                   nir_cf_node_next(&nif->cf_<wbr>node)));<br>
> +<br>
> +      nir_cf_list list;<br>
> +      if (discard_in_then)<br>
> +         nir_cf_list_extract(&list, &nif->else_list);<br>
> +      else<br>
> +         nir_cf_list_extract(&list, &nif->then_list);<br>
> +      nir_cf_reinsert(&list, nir_after_instr(&discard_if-><wbr>instr));<br>
> +<br>
> +      nir_cf_node_remove(&nif->cf_<wbr>node);<br>
> +<br>
> +      progress = true;<br>
> +   }<br>
> +<br>
> +   /* If we modified control-flow, metadata is toast.  Also, we may have<br>
> +    * lowered some phis to registers so we need to back into SSA.<br>
> +    */<br>
> +   if (progress) {<br>
> +      nir_metadata_preserve(impl, 0);<br>
> +      nir_lower_regs_to_ssa_impl(<wbr>impl);<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> +bool<br>
> +nir_opt_discard_if(nir_shader *shader)<br>
> +{<br>
> +   assert(shader->info.stage == MESA_SHADER_FRAGMENT);<br>
> +<br>
> +   bool progress = false;<br>
> +<br>
> +   nir_foreach_function(function, shader) {<br>
> +      if (function->impl &&<br>
> +          opt_discard_if_impl(function-><wbr>impl))<br>
> +         progress = true;<br>
> +   }<br>
> +<br>
> +   return progress;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_variable_mode_is_read_<wbr>only(nir_variable_mode mode)<br>
> +{<br>
> +   return mode == nir_var_shader_in ||<br>
> +          mode == nir_var_uniform ||<br>
> +          mode == nir_var_system_value;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_op_is_derivative(nir_op op)<br>
> +{<br>
> +   return op == nir_op_fddx ||<br>
> +          op == nir_op_fddy ||<br>
> +          op == nir_op_fddx_fine ||<br>
> +          op == nir_op_fddy_fine ||<br>
> +          op == nir_op_fddx_coarse ||<br>
> +          op == nir_op_fddy_coarse;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_texop_implies_derivative(<wbr>nir_texop op)<br>
> +{<br>
> +   return op == nir_texop_tex ||<br>
> +          op == nir_texop_txb ||<br>
> +          op == nir_texop_lod;<br>
> +}<br>
> +<br>
> +static bool<br>
> +nir_intrinsic_writes_<wbr>external_memory(nir_intrinsic_<wbr>op intrin)<br>
> +{<br>
> +   switch (intrin) {<br>
> +   case nir_intrinsic_store_deref:<br>
> +   case nir_intrinsic_copy_deref:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>add:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>imin:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>umin:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>imax:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>umax:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>and:<br>
> +   case nir_intrinsic_deref_atomic_or:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>xor:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>exchange:<br>
> +   case nir_intrinsic_deref_atomic_<wbr>comp_swap:<br>
> +      /* If we ever start using variables for SSBO ops, we'll need to do<br>
> +       * something here.  For now, they're safe.<br>
> +       */<br>
> +      return false;<br>
> +<br>
> +   case nir_intrinsic_store_ssbo:<br>
> +   case nir_intrinsic_ssbo_atomic_add:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>imin:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>umin:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>imax:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>umax:<br>
> +   case nir_intrinsic_ssbo_atomic_and:<br>
> +   case nir_intrinsic_ssbo_atomic_or:<br>
> +   case nir_intrinsic_ssbo_atomic_xor:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>exchange:<br>
> +   case nir_intrinsic_ssbo_atomic_<wbr>comp_swap:<br>
> +      return true;<br>
> +<br>
> +   case nir_intrinsic_image_deref_<wbr>store:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_add:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_min:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_max:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_and:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_or:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_xor:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_exchange:<br>
> +   case nir_intrinsic_image_deref_<wbr>atomic_comp_swap:<br>
> +      return true;<br>
> +<br>
> +   default:<br>
> +      return false;<br>
> +   }<br>
> +}<br>
> +<br>
> +static bool<br>
> +add_src_instr_to_worklist(<wbr>nir_src *src, void *wl)<br>
> +{<br>
> +   if (!src->is_ssa)<br>
> +      return false;<br>
> +<br>
> +   nir_instr_worklist_push_tail(<wbr>wl, src->ssa->parent_instr);<br>
> +   return true;<br>
> +}<br>
> +<br>
> +static bool<br>
> +try_move_discard(nir_builder *b, nir_intrinsic_instr *discard)<br>
> +{<br>
> +   /* We require the discard to be in the top level of control flow.  We<br>
> +    * could, in theory, move discards that are inside ifs or loops but that<br>
> +    * would be a lot more work.<br>
> +    */<br>
> +   if (discard->instr.block->cf_<wbr>node.parent->type != nir_cf_node_function)<br>
> +      return false;<br>
> +<br>
> +   /* Build the set of all instructions discard depends on.  While we're at<br>
> +    * it, we watch out for any cases that would prevent us from moving the<br>
> +    * instruction.<br>
> +    */<br>
> +   struct set *dep_instrs = _mesa_set_create(NULL, _mesa_hash_pointer,<br>
> +                                             _mesa_key_pointer_equal);<br>
> +<br>
> +   bool can_move_discard = true;<br>
> +   nir_instr_worklist *work = nir_instr_worklist_create();<br>
> +   add_src_instr_to_worklist(&<wbr>discard->src[0], work);<br>
> +   nir_foreach_instr_in_worklist(<wbr>instr, work) {<br>
> +      /* Don't process an instruction twice */<br>
> +      if (_mesa_set_search(dep_instrs, instr))<br>
> +         continue;<br>
> +<br>
> +      /* Phi instructions can't be moved at all.  Also, if we're dependent on<br>
> +       * a phi then we are dependent on some other bit of control flow and<br>
> +       * it's hard to figure out the proper condition.<br>
> +       */<br>
> +      if (instr->type == nir_instr_type_phi) {<br>
> +         can_move_discard = false;<br>
> +         break;<br>
> +      }<br>
> +<br>
> +      if (instr->type == nir_instr_type_intrinsic) {<br>
> +         nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);<br>
> +         if (intrin->intrinsic == nir_intrinsic_load_deref) {<br>
> +            nir_deref_instr *deref = nir_src_as_deref(intrin->src[<wbr>0]);<br>
> +            if (!nir_variable_mode_is_read_<wbr>only(deref->mode)) {<br>
> +               can_move_discard = false;<br>
> +               break;<br>
> +            }<br>
> +         } else if (!(nir_intrinsic_infos[intrin-<wbr>>intrinsic].flags &<br>
> +                      NIR_INTRINSIC_CAN_REORDER)) {<br>
> +            can_move_discard = false;<br>
> +            break;<br>
> +         }<br>
> +      }<br>
> +<br>
> +      if (!nir_foreach_src(instr, add_src_instr_to_worklist, work)) {<br>
> +         can_move_discard = false;<br>
> +         break;<br>
> +      }<br>
> +<br>
> +      _mesa_set_add(dep_instrs, instr);<br>
> +   }<br>
> +<br>
> +   if (!can_move_discard) {<br>
> +      nir_instr_worklist_destroy(<wbr>work);<br>
> +      _mesa_set_destroy(dep_instrs, NULL);<br>
> +      return false;<br>
> +   }<br>
> +<br>
> +   nir_function_impl *impl =<br>
> +      nir_cf_node_as_function(<wbr>discard->instr.block->cf_node.<wbr>parent);<br>
> +<br>
> +   /* Walk the list of instructions and move the discard and everything it<br>
> +    * depends on to the top.  We walk the instruction list here because it<br>
> +    * ensures that everything stays in its original order.  This provides<br>
> +    * stability for the algorithm and ensures that we don't accidentalyly<br>
<br>
</div></div>accidentally<br></blockquote><div><br></div><div>Thanks!  Both fixed locally.</div><div><br></div><div>--Jason<br></div></div><br></div></div>