• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Intel Corporation
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Jason Ekstrand (jason@jlekstrand.net)
25  *
26  */
27 
28 #include "nir.h"
29 
30 /*
31  * This pass lowers the neg, abs, and sat operations to source modifiers on
32  * ALU operations to make things nicer for the backend.  It's just much
33  * easier to not have them when we're doing optimizations.
34  */
35 
36 static void
alu_src_consume_abs(nir_alu_src * src)37 alu_src_consume_abs(nir_alu_src *src)
38 {
39    src->abs = true;
40 }
41 
42 static void
alu_src_consume_negate(nir_alu_src * src)43 alu_src_consume_negate(nir_alu_src *src)
44 {
45    /* If abs is set on the source, the negate goes away */
46    if (!src->abs)
47       src->negate = !src->negate;
48 }
49 
50 static bool
nir_lower_to_source_mods_block(nir_block * block,nir_lower_to_source_mods_flags options)51 nir_lower_to_source_mods_block(nir_block *block,
52                                nir_lower_to_source_mods_flags options)
53 {
54    bool progress = false;
55 
56    nir_foreach_instr(instr, block) {
57       if (instr->type != nir_instr_type_alu)
58          continue;
59 
60       nir_alu_instr *alu = nir_instr_as_alu(instr);
61 
62       bool lower_abs = (nir_op_infos[alu->op].num_inputs < 3) ||
63             (options & nir_lower_triop_abs);
64 
65       for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
66          if (!alu->src[i].src.is_ssa)
67             continue;
68 
69          if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
70             continue;
71 
72          nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
73 
74          if (parent->dest.saturate)
75             continue;
76 
77          switch (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i])) {
78          case nir_type_float:
79             if (!(options & nir_lower_float_source_mods))
80                continue;
81             if (!(parent->op == nir_op_fabs && (options & nir_lower_fabs_source_mods)) &&
82                 !(parent->op == nir_op_fneg && (options & nir_lower_fneg_source_mods))) {
83                continue;
84             }
85             break;
86          case nir_type_int:
87             if (!(options & nir_lower_int_source_mods))
88                continue;
89             if (parent->op != nir_op_iabs && parent->op != nir_op_ineg)
90                continue;
91             break;
92          default:
93             continue;
94          }
95 
96          if (nir_src_bit_size(alu->src[i].src) == 64 &&
97              !(options & nir_lower_64bit_source_mods)) {
98             continue;
99          }
100 
101          /* We can only do a rewrite if the source we are copying is SSA.
102           * Otherwise, moving the read might invalidly reorder reads/writes
103           * on a register.
104           */
105          if (!parent->src[0].src.is_ssa)
106             continue;
107 
108          if (!lower_abs && (parent->op == nir_op_fabs ||
109                             parent->op == nir_op_iabs ||
110                             parent->src[0].abs))
111             continue;
112 
113          nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
114 
115          /* Apply any modifiers that come from the parent opcode */
116          if (parent->op == nir_op_fneg || parent->op == nir_op_ineg)
117             alu_src_consume_negate(&alu->src[i]);
118          if (parent->op == nir_op_fabs || parent->op == nir_op_iabs)
119             alu_src_consume_abs(&alu->src[i]);
120 
121          /* Apply modifiers from the parent source */
122          if (parent->src[0].negate)
123             alu_src_consume_negate(&alu->src[i]);
124          if (parent->src[0].abs)
125             alu_src_consume_abs(&alu->src[i]);
126 
127          for (int j = 0; j < 4; ++j) {
128             if (!nir_alu_instr_channel_used(alu, i, j))
129                continue;
130             alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
131          }
132 
133          if (nir_ssa_def_is_unused(&parent->dest.dest.ssa))
134             nir_instr_remove(&parent->instr);
135 
136          progress = true;
137       }
138 
139       /* We've covered sources.  Now we're going to try and saturate the
140        * destination if we can.
141        */
142 
143       if (!alu->dest.dest.is_ssa)
144          continue;
145 
146       if (nir_dest_bit_size(alu->dest.dest) == 64 &&
147           !(options & nir_lower_64bit_source_mods)) {
148          continue;
149       }
150 
151       /* We can only saturate float destinations */
152       if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
153           nir_type_float)
154          continue;
155 
156       if (!(options & nir_lower_float_source_mods))
157          continue;
158 
159       if (!list_is_empty(&alu->dest.dest.ssa.if_uses))
160          continue;
161 
162       bool all_children_are_sat = true;
163       nir_foreach_use(child_src, &alu->dest.dest.ssa) {
164          assert(child_src->is_ssa);
165          nir_instr *child = child_src->parent_instr;
166          if (child->type != nir_instr_type_alu) {
167             all_children_are_sat = false;
168             continue;
169          }
170 
171          nir_alu_instr *child_alu = nir_instr_as_alu(child);
172          if (child_alu->src[0].negate || child_alu->src[0].abs) {
173             all_children_are_sat = false;
174             continue;
175          }
176 
177          if (child_alu->op != nir_op_fsat) {
178             all_children_are_sat = false;
179             continue;
180          }
181       }
182 
183       if (!all_children_are_sat)
184          continue;
185 
186       alu->dest.saturate = true;
187       progress = true;
188 
189       nir_foreach_use(child_src, &alu->dest.dest.ssa) {
190          assert(child_src->is_ssa);
191          nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
192 
193          child_alu->op = nir_op_mov;
194          child_alu->dest.saturate = false;
195          /* We could propagate the dest of our instruction to the
196           * destinations of the uses here.  However, one quick round of
197           * copy propagation will clean that all up and then we don't have
198           * the complexity.
199           */
200       }
201    }
202 
203    return progress;
204 }
205 
206 static bool
nir_lower_to_source_mods_impl(nir_function_impl * impl,nir_lower_to_source_mods_flags options)207 nir_lower_to_source_mods_impl(nir_function_impl *impl,
208                               nir_lower_to_source_mods_flags options)
209 {
210    bool progress = false;
211 
212    nir_foreach_block(block, impl) {
213       progress |= nir_lower_to_source_mods_block(block, options);
214    }
215 
216    if (progress)
217       nir_metadata_preserve(impl, nir_metadata_block_index |
218                                   nir_metadata_dominance);
219 
220    return progress;
221 }
222 
223 bool
nir_lower_to_source_mods(nir_shader * shader,nir_lower_to_source_mods_flags options)224 nir_lower_to_source_mods(nir_shader *shader,
225                          nir_lower_to_source_mods_flags options)
226 {
227    bool progress = false;
228 
229    nir_foreach_function(function, shader) {
230       if (function->impl) {
231          progress |= nir_lower_to_source_mods_impl(function->impl, options);
232       }
233    }
234 
235    return progress;
236 }
237