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