xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_discard_if.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "compiler/nir/nir_builder.h"
25 #include "nir.h"
26 
27 static bool
lower_discard_if(nir_builder * b,nir_intrinsic_instr * instr,void * cb_data)28 lower_discard_if(nir_builder *b, nir_intrinsic_instr *instr, void *cb_data)
29 {
30    nir_lower_discard_if_options options = *(nir_lower_discard_if_options *)cb_data;
31 
32    switch (instr->intrinsic) {
33    case nir_intrinsic_demote_if:
34       if (!(options & nir_lower_demote_if_to_cf))
35          return false;
36       break;
37    case nir_intrinsic_terminate_if:
38       if (!(options & nir_lower_terminate_if_to_cf))
39          return false;
40       break;
41    default:
42       return false;
43    }
44 
45    b->cursor = nir_before_instr(&instr->instr);
46 
47    nir_if *if_stmt = nir_push_if(b, instr->src[0].ssa);
48    switch (instr->intrinsic) {
49    case nir_intrinsic_demote_if:
50       nir_demote(b);
51       break;
52    case nir_intrinsic_terminate_if:
53       nir_terminate(b);
54       break;
55    default:
56       unreachable("bad intrinsic");
57    }
58    nir_pop_if(b, if_stmt);
59    nir_instr_remove(&instr->instr);
60    return true;
61 
62    /* a shader like this (shaders@glsl-fs-discard-04):
63 
64       uniform int j, k;
65 
66       void main()
67       {
68        for (int i = 0; i < j; i++) {
69         if (i > k)
70          continue;
71         discard;
72        }
73        gl_FragColor = vec4(0.0, 1.0, 0.0, 0.0);
74       }
75 
76 
77 
78       will generate nir like:
79 
80       loop   {
81          //snip
82          if   ssa_11   {
83             block   block_5:
84             /   preds:   block_4   /
85             vec1   32   ssa_17   =   iadd   ssa_50,   ssa_31
86             /   succs:   block_7   /
87          }   else   {
88             block   block_6:
89             /   preds:   block_4   /
90             intrinsic   terminate   ()   () <-- not last instruction
91             vec1   32   ssa_23   =   iadd   ssa_50,   ssa_31 <-- dead code loop itr increment
92             /   succs:   block_7   /
93          }
94          //snip
95       }
96 
97       which means that we can't assert like this:
98 
99       assert(instr->intrinsic != nir_intrinsic_terminate ||
100              nir_block_last_instr(instr->instr.block) == &instr->instr);
101 
102 
103       and it's unnecessary anyway since later optimizations will dce the
104       instructions following the discard
105     */
106 
107    return false;
108 }
109 
110 bool
nir_lower_discard_if(nir_shader * shader,nir_lower_discard_if_options options)111 nir_lower_discard_if(nir_shader *shader, nir_lower_discard_if_options options)
112 {
113    return nir_shader_intrinsics_pass(shader,
114                                      lower_discard_if,
115                                      nir_metadata_none,
116                                      &options);
117 }
118