1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2021 Advanced Micro Devices, Inc.
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 DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "si_pipe.h"
25bf215546Sopenharmony_ci#include "nir.h"
26bf215546Sopenharmony_ci#include "nir_builder.h"
27bf215546Sopenharmony_ci#include "nir_worklist.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistatic bool
31bf215546Sopenharmony_ciadd_src_instr_to_worklist(nir_src *src, void *wl)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci   if (!src->is_ssa)
34bf215546Sopenharmony_ci      return false;
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   nir_instr_worklist_push_tail(wl, src->ssa->parent_instr);
37bf215546Sopenharmony_ci   return true;
38bf215546Sopenharmony_ci}
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic int
41bf215546Sopenharmony_ciget_tex_unit(nir_tex_instr *tex)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   int tex_index = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
44bf215546Sopenharmony_ci   if (tex_index >= 0) {
45bf215546Sopenharmony_ci      nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_index].src);
46bf215546Sopenharmony_ci      nir_variable *var = nir_deref_instr_get_variable(deref);
47bf215546Sopenharmony_ci      return var ? var->data.binding : 0;
48bf215546Sopenharmony_ci   }
49bf215546Sopenharmony_ci   return -1;
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic int
53bf215546Sopenharmony_cicheck_instr_depends_on_tex(nir_intrinsic_instr *store)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   int texunit = -1;
56bf215546Sopenharmony_ci   struct set *instrs = _mesa_set_create(NULL, _mesa_hash_pointer,
57bf215546Sopenharmony_ci                                         _mesa_key_pointer_equal);
58bf215546Sopenharmony_ci   nir_instr_worklist *work = nir_instr_worklist_create();
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   _mesa_set_add(instrs, &store->instr);
61bf215546Sopenharmony_ci   add_src_instr_to_worklist(&store->src[0], work);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   nir_foreach_instr_in_worklist(instr, work) {
64bf215546Sopenharmony_ci      /* Don't process an instruction twice */
65bf215546Sopenharmony_ci      if (_mesa_set_search(instrs, instr))
66bf215546Sopenharmony_ci         continue;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci      _mesa_set_add(instrs, instr);
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci      if (instr->type == nir_instr_type_alu ||
71bf215546Sopenharmony_ci          instr->type == nir_instr_type_load_const) {
72bf215546Sopenharmony_ci         /* TODO: ubo, etc */
73bf215546Sopenharmony_ci         if (!nir_foreach_src(instr, add_src_instr_to_worklist, work))
74bf215546Sopenharmony_ci            break;
75bf215546Sopenharmony_ci         continue;
76bf215546Sopenharmony_ci      } else if (instr->type == nir_instr_type_tex) {
77bf215546Sopenharmony_ci         if (texunit != -1) {
78bf215546Sopenharmony_ci            /* We can only depend on a single tex */
79bf215546Sopenharmony_ci            texunit = -1;
80bf215546Sopenharmony_ci            break;
81bf215546Sopenharmony_ci         } else {
82bf215546Sopenharmony_ci            texunit = get_tex_unit(nir_instr_as_tex(instr));
83bf215546Sopenharmony_ci            continue;
84bf215546Sopenharmony_ci         }
85bf215546Sopenharmony_ci      } else {
86bf215546Sopenharmony_ci         break;
87bf215546Sopenharmony_ci      }
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   nir_instr_worklist_destroy(work);
91bf215546Sopenharmony_ci   _mesa_set_destroy(instrs, NULL);
92bf215546Sopenharmony_ci   return texunit;
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistatic bool
96bf215546Sopenharmony_ciget_output_as_const_value(nir_shader *shader, float values[4])
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
99bf215546Sopenharmony_ci      nir_foreach_block_reverse(block, function->impl) {
100bf215546Sopenharmony_ci         nir_foreach_instr_reverse_safe(instr, block) {
101bf215546Sopenharmony_ci            switch (instr->type) {
102bf215546Sopenharmony_ci               case nir_instr_type_intrinsic: {
103bf215546Sopenharmony_ci                  nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
104bf215546Sopenharmony_ci                  if (intrin->intrinsic == nir_intrinsic_store_output) {
105bf215546Sopenharmony_ci                     nir_const_value *c = nir_src_as_const_value(intrin->src[0]);
106bf215546Sopenharmony_ci                     if (c) {
107bf215546Sopenharmony_ci                        nir_const_value_to_array(values, c, 4, f32);
108bf215546Sopenharmony_ci                        return true;
109bf215546Sopenharmony_ci                     }
110bf215546Sopenharmony_ci                     return false;
111bf215546Sopenharmony_ci                  }
112bf215546Sopenharmony_ci                  FALLTHROUGH;
113bf215546Sopenharmony_ci               }
114bf215546Sopenharmony_ci               default:
115bf215546Sopenharmony_ci                  continue;
116bf215546Sopenharmony_ci            }
117bf215546Sopenharmony_ci         }
118bf215546Sopenharmony_ci      }
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci   return false;
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistruct replace_param {
124bf215546Sopenharmony_ci   float value[4];
125bf215546Sopenharmony_ci   int *texunit;
126bf215546Sopenharmony_ci};
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_cistatic bool
129bf215546Sopenharmony_cistore_instr_depends_on_tex(nir_builder *b, nir_instr *instr, void *state)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_intrinsic)
132bf215546Sopenharmony_ci      return false;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
135bf215546Sopenharmony_ci   if (intrin->intrinsic != nir_intrinsic_store_output)
136bf215546Sopenharmony_ci      return false;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   struct replace_param *p = (struct replace_param*) state;
139bf215546Sopenharmony_ci   *(p->texunit) = check_instr_depends_on_tex(intrin);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   return *(p->texunit) != -1;
142bf215546Sopenharmony_ci}
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cistatic bool
146bf215546Sopenharmony_cireplace_tex_by_imm(nir_builder *b, nir_instr *instr, void *state)
147bf215546Sopenharmony_ci{
148bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_tex)
149bf215546Sopenharmony_ci      return false;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   nir_tex_instr *tex = nir_instr_as_tex(instr);
152bf215546Sopenharmony_ci   struct replace_param *p = (struct replace_param*) state;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   if (get_tex_unit(tex) != *(p->texunit))
155bf215546Sopenharmony_ci      return false;
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   b->cursor = nir_instr_remove(&tex->instr);
158bf215546Sopenharmony_ci   nir_ssa_def *imm = nir_imm_vec4(b, p->value[0], p->value[1], p->value[2], p->value[3]);
159bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&tex->dest.ssa, imm);
160bf215546Sopenharmony_ci   return true;
161bf215546Sopenharmony_ci}
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci/* This function returns true if a shader' sole output becomes constant when
165bf215546Sopenharmony_ci * a given texunit is replaced by a constant value.
166bf215546Sopenharmony_ci * The input constant value is passed as 'in' and the determined constant
167bf215546Sopenharmony_ci * value is stored in 'out'. The texunit is also remembered.
168bf215546Sopenharmony_ci */
169bf215546Sopenharmony_cibool
170bf215546Sopenharmony_cisi_nir_is_output_const_if_tex_is_const(nir_shader *shader, float *in, float *out, int *texunit)
171bf215546Sopenharmony_ci{
172bf215546Sopenharmony_ci   assert(shader->info.stage == MESA_SHADER_FRAGMENT);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   if (BITSET_COUNT(shader->info.textures_used) == 0 ||
175bf215546Sopenharmony_ci       util_bitcount64(shader->info.outputs_written) != 1)
176bf215546Sopenharmony_ci      return false;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   struct replace_param p;
179bf215546Sopenharmony_ci   memcpy(p.value, in, 4 * sizeof(float));
180bf215546Sopenharmony_ci   p.texunit = texunit;
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   /* Test if the single store_output only depends on constants and a single texture op */
183bf215546Sopenharmony_ci   if (nir_shader_instructions_pass(shader, store_instr_depends_on_tex, nir_metadata_all, &p)) {
184bf215546Sopenharmony_ci      assert(*p.texunit != -1);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci      /* Replace nir_tex_instr using texunit by vec4(v) */
187bf215546Sopenharmony_ci      nir_shader_instructions_pass(shader, replace_tex_by_imm,
188bf215546Sopenharmony_ci                                   nir_metadata_block_index |
189bf215546Sopenharmony_ci                                   nir_metadata_dominance, &p);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci      /* Optimize the cloned shader */
192bf215546Sopenharmony_ci      bool progress;
193bf215546Sopenharmony_ci      do {
194bf215546Sopenharmony_ci         progress = false;
195bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_copy_prop);
196bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_opt_remove_phis);
197bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_opt_dce);
198bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_opt_dead_cf);
199bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_opt_algebraic);
200bf215546Sopenharmony_ci         NIR_PASS(progress, shader, nir_opt_constant_folding);
201bf215546Sopenharmony_ci      } while (progress);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci      /* Is the output a constant value? */
204bf215546Sopenharmony_ci      if (get_output_as_const_value(shader, out))
205bf215546Sopenharmony_ci         return true;
206bf215546Sopenharmony_ci   }
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   return false;
209bf215546Sopenharmony_ci}
210