1/*
2 * Copyright 2018 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include "nir.h"
25#include "compiler/nir/nir_builder.h"
26
27static bool
28lower_discard_if_instr(nir_builder *b, nir_instr *instr_, UNUSED void *cb_data)
29{
30   if (instr_->type != nir_instr_type_intrinsic)
31      return false;
32
33   nir_intrinsic_instr *instr = nir_instr_as_intrinsic(instr_);
34
35   if (instr->intrinsic == nir_intrinsic_discard_if) {
36      b->cursor = nir_before_instr(&instr->instr);
37
38      nir_if *if_stmt = nir_push_if(b, nir_ssa_for_src(b, instr->src[0], 1));
39      nir_discard(b);
40      nir_pop_if(b, if_stmt);
41      nir_instr_remove(&instr->instr);
42      return true;
43   } else if (instr->intrinsic == nir_intrinsic_terminate_if ||
44              instr->intrinsic == nir_intrinsic_demote_if) {
45      unreachable("todo: handle terminates and demotes for Vulkan");
46   }
47
48   /* a shader like this (shaders@glsl-fs-discard-04):
49
50      uniform int j, k;
51
52      void main()
53      {
54       for (int i = 0; i < j; i++) {
55        if (i > k)
56         continue;
57        discard;
58       }
59       gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);
60      }
61
62
63
64      will generate nir like:
65
66      loop   {
67         //snip
68         if   ssa_11   {
69            block   block_5:
70            /   preds:   block_4   /
71            vec1   32   ssa_17   =   iadd   ssa_50,   ssa_31
72            /   succs:   block_7   /
73         }   else   {
74            block   block_6:
75            /   preds:   block_4   /
76            intrinsic   discard   ()   () <-- not last instruction
77            vec1   32   ssa_23   =   iadd   ssa_50,   ssa_31 <-- dead code loop itr increment
78            /   succs:   block_7   /
79         }
80         //snip
81      }
82
83      which means that we can't assert like this:
84
85      assert(instr->intrinsic != nir_intrinsic_discard ||
86             nir_block_last_instr(instr->instr.block) == &instr->instr);
87
88
89      and it's unnecessary anyway since later optimizations will dce the
90      instructions following the discard
91    */
92
93   return false;
94}
95
96bool
97nir_lower_discard_if(nir_shader *shader)
98{
99   return nir_shader_instructions_pass(shader,
100                                       lower_discard_if_instr,
101                                       nir_metadata_none,
102                                       NULL);
103}
104