Skip to content

nir: Add pass to optimize similar discards

Alyssa Rosenzweig requested to merge alyssa/mesa:nir/similar-discards into main

What does this MR do and why?

RFC: nir: Add pass to optimize similar discards

Add a pass that turns code like:

   if ... {
      ...
      discard_if(x)
   } else {
      ...
      discard_if(y)
   }

into

   if ... {
      ...
   } else {
      ....
   }
   cond = phi x, y
   discard_if(cond)

It should be run immediately before peephole_select, as it allows peephole_select to make more progress. I looked into this after seeing an ubershader that had a bunch of nested if's to work out the discard condition. This pass, together with peephole_select, collapses all of the control flow into away leaving only a big pile of 1-bit arithmetic and a single discard_if at the end. Whether this is a good idea is not 100% clear.

If people think this is useful, the pass can easily be generalized for demote_if/terminate_if.

Signed-off-by: Alyssa Rosenzweig alyssa@rosenzweig.io

Edited by Alyssa Rosenzweig

Merge request reports