1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cistatic void
27bf215546Sopenharmony_ciadd_src(nir_src *src, struct set *invariants)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   if (src->is_ssa) {
30bf215546Sopenharmony_ci      _mesa_set_add(invariants, src->ssa);
31bf215546Sopenharmony_ci   } else {
32bf215546Sopenharmony_ci      _mesa_set_add(invariants, src->reg.reg);
33bf215546Sopenharmony_ci   }
34bf215546Sopenharmony_ci}
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic bool
37bf215546Sopenharmony_ciadd_src_cb(nir_src *src, void *state)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   add_src(src, state);
40bf215546Sopenharmony_ci   return true;
41bf215546Sopenharmony_ci}
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistatic bool
44bf215546Sopenharmony_cidest_is_invariant(nir_dest *dest, struct set *invariants)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   if (dest->is_ssa) {
47bf215546Sopenharmony_ci      return _mesa_set_search(invariants, &dest->ssa);
48bf215546Sopenharmony_ci   } else {
49bf215546Sopenharmony_ci      return _mesa_set_search(invariants, dest->reg.reg);
50bf215546Sopenharmony_ci   }
51bf215546Sopenharmony_ci}
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void
54bf215546Sopenharmony_ciadd_cf_node(nir_cf_node *cf, struct set *invariants)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   if (cf->type == nir_cf_node_if) {
57bf215546Sopenharmony_ci      nir_if *if_stmt = nir_cf_node_as_if(cf);
58bf215546Sopenharmony_ci      add_src(&if_stmt->condition, invariants);
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   if (cf->parent)
62bf215546Sopenharmony_ci      add_cf_node(cf->parent, invariants);
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic void
66bf215546Sopenharmony_ciadd_var(nir_variable *var, struct set *invariants)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   /* Because we pass the result of nir_intrinsic_get_var directly to this
69bf215546Sopenharmony_ci    * function, it's possible for var to be NULL if, for instance, there's a
70bf215546Sopenharmony_ci    * cast somewhere in the chain.
71bf215546Sopenharmony_ci    */
72bf215546Sopenharmony_ci   if (var != NULL)
73bf215546Sopenharmony_ci      _mesa_set_add(invariants, var);
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic bool
77bf215546Sopenharmony_civar_is_invariant(nir_variable *var, struct set * invariants)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   /* Because we pass the result of nir_intrinsic_get_var directly to this
80bf215546Sopenharmony_ci    * function, it's possible for var to be NULL if, for instance, there's a
81bf215546Sopenharmony_ci    * cast somewhere in the chain.
82bf215546Sopenharmony_ci    */
83bf215546Sopenharmony_ci   return var && (var->data.invariant || _mesa_set_search(invariants, var));
84bf215546Sopenharmony_ci}
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistatic void
87bf215546Sopenharmony_cipropagate_invariant_instr(nir_instr *instr, struct set *invariants)
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci   switch (instr->type) {
90bf215546Sopenharmony_ci   case nir_instr_type_alu: {
91bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(instr);
92bf215546Sopenharmony_ci      if (!dest_is_invariant(&alu->dest.dest, invariants))
93bf215546Sopenharmony_ci         break;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci      alu->exact = true;
96bf215546Sopenharmony_ci      nir_foreach_src(instr, add_src_cb, invariants);
97bf215546Sopenharmony_ci      break;
98bf215546Sopenharmony_ci   }
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   case nir_instr_type_tex: {
101bf215546Sopenharmony_ci      nir_tex_instr *tex = nir_instr_as_tex(instr);
102bf215546Sopenharmony_ci      if (dest_is_invariant(&tex->dest, invariants))
103bf215546Sopenharmony_ci         nir_foreach_src(instr, add_src_cb, invariants);
104bf215546Sopenharmony_ci      break;
105bf215546Sopenharmony_ci   }
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   case nir_instr_type_intrinsic: {
108bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
109bf215546Sopenharmony_ci      switch (intrin->intrinsic) {
110bf215546Sopenharmony_ci      case nir_intrinsic_copy_deref:
111bf215546Sopenharmony_ci         /* If the destination is invariant then so is the source */
112bf215546Sopenharmony_ci         if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
113bf215546Sopenharmony_ci            add_var(nir_intrinsic_get_var(intrin, 1), invariants);
114bf215546Sopenharmony_ci         break;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      case nir_intrinsic_load_deref:
117bf215546Sopenharmony_ci         if (dest_is_invariant(&intrin->dest, invariants))
118bf215546Sopenharmony_ci            add_var(nir_intrinsic_get_var(intrin, 0), invariants);
119bf215546Sopenharmony_ci         break;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci      case nir_intrinsic_store_deref:
122bf215546Sopenharmony_ci         if (var_is_invariant(nir_intrinsic_get_var(intrin, 0), invariants))
123bf215546Sopenharmony_ci            add_src(&intrin->src[1], invariants);
124bf215546Sopenharmony_ci         break;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci      default:
127bf215546Sopenharmony_ci         /* Nothing to do */
128bf215546Sopenharmony_ci         break;
129bf215546Sopenharmony_ci      }
130bf215546Sopenharmony_ci      FALLTHROUGH;
131bf215546Sopenharmony_ci   }
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   case nir_instr_type_deref:
134bf215546Sopenharmony_ci   case nir_instr_type_jump:
135bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
136bf215546Sopenharmony_ci   case nir_instr_type_load_const:
137bf215546Sopenharmony_ci      break; /* Nothing to do */
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   case nir_instr_type_phi: {
140bf215546Sopenharmony_ci      nir_phi_instr *phi = nir_instr_as_phi(instr);
141bf215546Sopenharmony_ci      if (!dest_is_invariant(&phi->dest, invariants))
142bf215546Sopenharmony_ci         break;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci      nir_foreach_phi_src(src, phi) {
145bf215546Sopenharmony_ci         add_src(&src->src, invariants);
146bf215546Sopenharmony_ci         add_cf_node(&src->pred->cf_node, invariants);
147bf215546Sopenharmony_ci      }
148bf215546Sopenharmony_ci      break;
149bf215546Sopenharmony_ci   }
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   case nir_instr_type_call:
152bf215546Sopenharmony_ci      unreachable("This pass must be run after function inlining");
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   case nir_instr_type_parallel_copy:
155bf215546Sopenharmony_ci   default:
156bf215546Sopenharmony_ci      unreachable("Cannot have this instruction type");
157bf215546Sopenharmony_ci   }
158bf215546Sopenharmony_ci}
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_cistatic bool
161bf215546Sopenharmony_cipropagate_invariant_impl(nir_function_impl *impl, struct set *invariants)
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci   bool progress = false;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   while (true) {
166bf215546Sopenharmony_ci      uint32_t prev_entries = invariants->entries;
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci      nir_foreach_block_reverse(block, impl) {
169bf215546Sopenharmony_ci         nir_foreach_instr_reverse(instr, block)
170bf215546Sopenharmony_ci            propagate_invariant_instr(instr, invariants);
171bf215546Sopenharmony_ci      }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci      /* Keep running until we make no more progress. */
174bf215546Sopenharmony_ci      if (invariants->entries > prev_entries) {
175bf215546Sopenharmony_ci         progress = true;
176bf215546Sopenharmony_ci         continue;
177bf215546Sopenharmony_ci      } else {
178bf215546Sopenharmony_ci         break;
179bf215546Sopenharmony_ci      }
180bf215546Sopenharmony_ci   }
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   if (progress) {
183bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
184bf215546Sopenharmony_ci                                  nir_metadata_dominance |
185bf215546Sopenharmony_ci                                  nir_metadata_live_ssa_defs);
186bf215546Sopenharmony_ci   } else {
187bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
188bf215546Sopenharmony_ci   }
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   return progress;
191bf215546Sopenharmony_ci}
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci/* If invariant_prim=true, this pass considers all geometry-affecting
194bf215546Sopenharmony_ci * outputs as invariant. Doing this works around a common class of application
195bf215546Sopenharmony_ci * bugs appearing as flickering.
196bf215546Sopenharmony_ci */
197bf215546Sopenharmony_cibool
198bf215546Sopenharmony_cinir_propagate_invariant(nir_shader *shader, bool invariant_prim)
199bf215546Sopenharmony_ci{
200bf215546Sopenharmony_ci   /* Hash set of invariant things */
201bf215546Sopenharmony_ci   struct set *invariants = _mesa_pointer_set_create(NULL);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   if (shader->info.stage != MESA_SHADER_FRAGMENT && invariant_prim) {
204bf215546Sopenharmony_ci      nir_foreach_shader_out_variable(var, shader) {
205bf215546Sopenharmony_ci         switch (var->data.location) {
206bf215546Sopenharmony_ci         case VARYING_SLOT_POS:
207bf215546Sopenharmony_ci         case VARYING_SLOT_PSIZ:
208bf215546Sopenharmony_ci         case VARYING_SLOT_CLIP_DIST0:
209bf215546Sopenharmony_ci         case VARYING_SLOT_CLIP_DIST1:
210bf215546Sopenharmony_ci         case VARYING_SLOT_CULL_DIST0:
211bf215546Sopenharmony_ci         case VARYING_SLOT_CULL_DIST1:
212bf215546Sopenharmony_ci         case VARYING_SLOT_TESS_LEVEL_OUTER:
213bf215546Sopenharmony_ci         case VARYING_SLOT_TESS_LEVEL_INNER:
214bf215546Sopenharmony_ci            if (!var->data.invariant)
215bf215546Sopenharmony_ci               _mesa_set_add(invariants, var);
216bf215546Sopenharmony_ci            break;
217bf215546Sopenharmony_ci         default:
218bf215546Sopenharmony_ci            break;
219bf215546Sopenharmony_ci         }
220bf215546Sopenharmony_ci      }
221bf215546Sopenharmony_ci   }
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci   bool progress = false;
224bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
225bf215546Sopenharmony_ci      if (function->impl && propagate_invariant_impl(function->impl, invariants))
226bf215546Sopenharmony_ci         progress = true;
227bf215546Sopenharmony_ci   }
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci   _mesa_set_destroy(invariants, NULL);
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   return progress;
232bf215546Sopenharmony_ci}
233