1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 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 DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir_builder.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_cistatic bool
27bf215546Sopenharmony_ciopt_memcpy_deref_cast(nir_intrinsic_instr *cpy, nir_src *deref_src)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   assert(cpy->intrinsic == nir_intrinsic_memcpy_deref);
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci   nir_deref_instr *cast = nir_src_as_deref(*deref_src);
32bf215546Sopenharmony_ci   if (cast == NULL || cast->deref_type != nir_deref_type_cast)
33bf215546Sopenharmony_ci      return false;
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   /* We always have to replace the source with a deref, not a bare uint
36bf215546Sopenharmony_ci    * pointer.  If it's the first deref in the chain, bail.
37bf215546Sopenharmony_ci    */
38bf215546Sopenharmony_ci   nir_deref_instr *parent = nir_src_as_deref(cast->parent);
39bf215546Sopenharmony_ci   if (parent == NULL)
40bf215546Sopenharmony_ci      return false;
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   /* If it has useful alignment information, we want to keep that */
43bf215546Sopenharmony_ci   if (cast->cast.align_mul > 0)
44bf215546Sopenharmony_ci      return false;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   /* Casts to uint8 or int8 never do us any good; get rid of them */
47bf215546Sopenharmony_ci   if (cast->type == glsl_int8_t_type() ||
48bf215546Sopenharmony_ci       cast->type == glsl_uint8_t_type()) {
49bf215546Sopenharmony_ci      nir_instr_rewrite_src(&cpy->instr, deref_src,
50bf215546Sopenharmony_ci                            nir_src_for_ssa(&parent->dest.ssa));
51bf215546Sopenharmony_ci      return true;
52bf215546Sopenharmony_ci   }
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   int64_t parent_type_size = glsl_get_explicit_size(parent->type, false);
55bf215546Sopenharmony_ci   if (parent_type_size < 0)
56bf215546Sopenharmony_ci      return false;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   if (!nir_src_is_const(cpy->src[2]))
59bf215546Sopenharmony_ci      return false;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   /* We don't want to get rid of the cast if the resulting type would be
62bf215546Sopenharmony_ci    * smaller than the amount of data we're copying.
63bf215546Sopenharmony_ci    */
64bf215546Sopenharmony_ci   if (nir_src_as_uint(cpy->src[2]) < (uint64_t)parent_type_size)
65bf215546Sopenharmony_ci      return false;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   nir_instr_rewrite_src(&cpy->instr, deref_src,
68bf215546Sopenharmony_ci                         nir_src_for_ssa(&parent->dest.ssa));
69bf215546Sopenharmony_ci   return true;
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_cistatic bool
73bf215546Sopenharmony_citype_is_tightly_packed(const struct glsl_type *type, unsigned *size_out)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   unsigned size = 0;
76bf215546Sopenharmony_ci   if (glsl_type_is_struct_or_ifc(type)) {
77bf215546Sopenharmony_ci      unsigned num_fields = glsl_get_length(type);
78bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_fields; i++) {
79bf215546Sopenharmony_ci         const struct glsl_struct_field *field =
80bf215546Sopenharmony_ci            glsl_get_struct_field_data(type, i);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci         if (field->offset < 0 || field->offset != size)
83bf215546Sopenharmony_ci            return false;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci         unsigned field_size;
86bf215546Sopenharmony_ci         if (!type_is_tightly_packed(field->type, &field_size))
87bf215546Sopenharmony_ci            return false;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci         size = field->offset + field_size;
90bf215546Sopenharmony_ci      }
91bf215546Sopenharmony_ci   } else if (glsl_type_is_array_or_matrix(type)) {
92bf215546Sopenharmony_ci      if (glsl_type_is_unsized_array(type))
93bf215546Sopenharmony_ci         return false;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci      unsigned stride = glsl_get_explicit_stride(type);
96bf215546Sopenharmony_ci      if (stride == 0)
97bf215546Sopenharmony_ci         return false;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci      const struct glsl_type *elem_type = glsl_get_array_element(type);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci      unsigned elem_size;
102bf215546Sopenharmony_ci      if (!type_is_tightly_packed(elem_type, &elem_size))
103bf215546Sopenharmony_ci         return false;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      if (elem_size != stride)
106bf215546Sopenharmony_ci         return false;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci      size = stride * glsl_get_length(type);
109bf215546Sopenharmony_ci   } else {
110bf215546Sopenharmony_ci      assert(glsl_type_is_vector_or_scalar(type));
111bf215546Sopenharmony_ci      if (glsl_get_explicit_stride(type) > 0)
112bf215546Sopenharmony_ci         return false;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci      if (glsl_type_is_boolean(type))
115bf215546Sopenharmony_ci         return false;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci      size = glsl_get_explicit_size(type, false);
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   if (size_out)
121bf215546Sopenharmony_ci      *size_out = size;
122bf215546Sopenharmony_ci   return true;
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_cistatic bool
126bf215546Sopenharmony_citry_lower_memcpy(nir_builder *b, nir_intrinsic_instr *cpy,
127bf215546Sopenharmony_ci                 struct set *complex_vars)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   nir_deref_instr *dst = nir_src_as_deref(cpy->src[0]);
130bf215546Sopenharmony_ci   nir_deref_instr *src = nir_src_as_deref(cpy->src[1]);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   /* A self-copy can always be eliminated */
133bf215546Sopenharmony_ci   if (dst == src) {
134bf215546Sopenharmony_ci      nir_instr_remove(&cpy->instr);
135bf215546Sopenharmony_ci      return true;
136bf215546Sopenharmony_ci   }
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   if (!nir_src_is_const(cpy->src[2]))
139bf215546Sopenharmony_ci      return false;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   uint64_t size = nir_src_as_uint(cpy->src[2]);
142bf215546Sopenharmony_ci   if (size == 0) {
143bf215546Sopenharmony_ci      nir_instr_remove(&cpy->instr);
144bf215546Sopenharmony_ci      return true;
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   if (glsl_type_is_vector_or_scalar(src->type) &&
148bf215546Sopenharmony_ci       glsl_type_is_vector_or_scalar(dst->type) &&
149bf215546Sopenharmony_ci       glsl_get_explicit_size(dst->type, false) == size &&
150bf215546Sopenharmony_ci       glsl_get_explicit_size(src->type, false) == size) {
151bf215546Sopenharmony_ci      b->cursor = nir_instr_remove(&cpy->instr);
152bf215546Sopenharmony_ci      nir_ssa_def *data =
153bf215546Sopenharmony_ci         nir_load_deref_with_access(b, src, nir_intrinsic_src_access(cpy));
154bf215546Sopenharmony_ci      data = nir_bitcast_vector(b, data, glsl_get_bit_size(dst->type));
155bf215546Sopenharmony_ci      assert(data->num_components == glsl_get_vector_elements(dst->type));
156bf215546Sopenharmony_ci      nir_store_deref_with_access(b, dst, data, ~0 /* write mask */,
157bf215546Sopenharmony_ci                                  nir_intrinsic_dst_access(cpy));
158bf215546Sopenharmony_ci      return true;
159bf215546Sopenharmony_ci   }
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   unsigned type_size;
162bf215546Sopenharmony_ci   if (dst->type == src->type &&
163bf215546Sopenharmony_ci       type_is_tightly_packed(dst->type, &type_size) &&
164bf215546Sopenharmony_ci       type_size == size) {
165bf215546Sopenharmony_ci      b->cursor = nir_instr_remove(&cpy->instr);
166bf215546Sopenharmony_ci      nir_copy_deref_with_access(b, dst, src,
167bf215546Sopenharmony_ci                                 nir_intrinsic_dst_access(cpy),
168bf215546Sopenharmony_ci                                 nir_intrinsic_src_access(cpy));
169bf215546Sopenharmony_ci      return true;
170bf215546Sopenharmony_ci   }
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   /* If one of the two types is tightly packed and happens to equal the
173bf215546Sopenharmony_ci    * memcpy size, then we can get the memcpy by casting to that type and
174bf215546Sopenharmony_ci    * doing a deref copy.
175bf215546Sopenharmony_ci    *
176bf215546Sopenharmony_ci    * However, if we blindly apply this logic, we may end up with extra casts
177bf215546Sopenharmony_ci    * where we don't want them. The whole point of converting memcpy to
178bf215546Sopenharmony_ci    * copy_deref is in the hopes that nir_opt_copy_prop_vars or
179bf215546Sopenharmony_ci    * nir_lower_vars_to_ssa will get rid of the copy and those passes don't
180bf215546Sopenharmony_ci    * handle casts well. Heuristically, only do this optimization if the
181bf215546Sopenharmony_ci    * tightly packed type is on a deref with nir_var_function_temp so we stick
182bf215546Sopenharmony_ci    * the cast on the other mode.
183bf215546Sopenharmony_ci    */
184bf215546Sopenharmony_ci   if (dst->modes == nir_var_function_temp &&
185bf215546Sopenharmony_ci       type_is_tightly_packed(dst->type, &type_size) &&
186bf215546Sopenharmony_ci       type_size == size) {
187bf215546Sopenharmony_ci      b->cursor = nir_instr_remove(&cpy->instr);
188bf215546Sopenharmony_ci      src = nir_build_deref_cast(b, &src->dest.ssa,
189bf215546Sopenharmony_ci                                 src->modes, dst->type, 0);
190bf215546Sopenharmony_ci      nir_copy_deref_with_access(b, dst, src,
191bf215546Sopenharmony_ci                                 nir_intrinsic_dst_access(cpy),
192bf215546Sopenharmony_ci                                 nir_intrinsic_src_access(cpy));
193bf215546Sopenharmony_ci      return true;
194bf215546Sopenharmony_ci   }
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* If we can get at the variable AND the only complex use of that variable
197bf215546Sopenharmony_ci    * is as a memcpy destination, then we don't have to care about any empty
198bf215546Sopenharmony_ci    * space in the variable.  In particular, we know that the variable is never
199bf215546Sopenharmony_ci    * cast to any other type and it's never used as a memcpy source so nothing
200bf215546Sopenharmony_ci    * can see any padding bytes.  This holds even if some other memcpy only
201bf215546Sopenharmony_ci    * writes to part of the variable.
202bf215546Sopenharmony_ci    */
203bf215546Sopenharmony_ci   if (dst->deref_type == nir_deref_type_var &&
204bf215546Sopenharmony_ci       dst->modes == nir_var_function_temp &&
205bf215546Sopenharmony_ci       _mesa_set_search(complex_vars, dst->var) == NULL &&
206bf215546Sopenharmony_ci       glsl_get_explicit_size(dst->type, false) <= size) {
207bf215546Sopenharmony_ci      b->cursor = nir_instr_remove(&cpy->instr);
208bf215546Sopenharmony_ci      src = nir_build_deref_cast(b, &src->dest.ssa,
209bf215546Sopenharmony_ci                                 src->modes, dst->type, 0);
210bf215546Sopenharmony_ci      nir_copy_deref_with_access(b, dst, src,
211bf215546Sopenharmony_ci                                 nir_intrinsic_dst_access(cpy),
212bf215546Sopenharmony_ci                                 nir_intrinsic_src_access(cpy));
213bf215546Sopenharmony_ci      return true;
214bf215546Sopenharmony_ci   }
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   if (src->modes == nir_var_function_temp &&
217bf215546Sopenharmony_ci       type_is_tightly_packed(src->type, &type_size) &&
218bf215546Sopenharmony_ci       type_size == size) {
219bf215546Sopenharmony_ci      b->cursor = nir_instr_remove(&cpy->instr);
220bf215546Sopenharmony_ci      dst = nir_build_deref_cast(b, &dst->dest.ssa,
221bf215546Sopenharmony_ci                                 dst->modes, src->type, 0);
222bf215546Sopenharmony_ci      nir_copy_deref_with_access(b, dst, src,
223bf215546Sopenharmony_ci                                 nir_intrinsic_dst_access(cpy),
224bf215546Sopenharmony_ci                                 nir_intrinsic_src_access(cpy));
225bf215546Sopenharmony_ci      return true;
226bf215546Sopenharmony_ci   }
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci   return false;
229bf215546Sopenharmony_ci}
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_cistatic bool
232bf215546Sopenharmony_ciopt_memcpy_impl(nir_function_impl *impl)
233bf215546Sopenharmony_ci{
234bf215546Sopenharmony_ci   bool progress = false;
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci   nir_builder b;
237bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   struct set *complex_vars = _mesa_pointer_set_create(NULL);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
242bf215546Sopenharmony_ci      nir_foreach_instr(instr, block) {
243bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_deref)
244bf215546Sopenharmony_ci            continue;
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci         nir_deref_instr *deref = nir_instr_as_deref(instr);
247bf215546Sopenharmony_ci         if (deref->deref_type != nir_deref_type_var)
248bf215546Sopenharmony_ci            continue;
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci         nir_deref_instr_has_complex_use_options opts =
251bf215546Sopenharmony_ci            nir_deref_instr_has_complex_use_allow_memcpy_dst;
252bf215546Sopenharmony_ci         if (nir_deref_instr_has_complex_use(deref, opts))
253bf215546Sopenharmony_ci            _mesa_set_add(complex_vars, deref->var);
254bf215546Sopenharmony_ci      }
255bf215546Sopenharmony_ci   }
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
258bf215546Sopenharmony_ci      nir_foreach_instr_safe(instr, block) {
259bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_intrinsic)
260bf215546Sopenharmony_ci            continue;
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci         nir_intrinsic_instr *cpy = nir_instr_as_intrinsic(instr);
263bf215546Sopenharmony_ci         if (cpy->intrinsic != nir_intrinsic_memcpy_deref)
264bf215546Sopenharmony_ci            continue;
265bf215546Sopenharmony_ci
266bf215546Sopenharmony_ci         while (opt_memcpy_deref_cast(cpy, &cpy->src[0]))
267bf215546Sopenharmony_ci            progress = true;
268bf215546Sopenharmony_ci         while (opt_memcpy_deref_cast(cpy, &cpy->src[1]))
269bf215546Sopenharmony_ci            progress = true;
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci         if (try_lower_memcpy(&b, cpy, complex_vars)) {
272bf215546Sopenharmony_ci            progress = true;
273bf215546Sopenharmony_ci            continue;
274bf215546Sopenharmony_ci         }
275bf215546Sopenharmony_ci      }
276bf215546Sopenharmony_ci   }
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   _mesa_set_destroy(complex_vars, NULL);
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   if (progress) {
281bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
282bf215546Sopenharmony_ci                                  nir_metadata_dominance);
283bf215546Sopenharmony_ci   } else {
284bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
285bf215546Sopenharmony_ci   }
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci   return progress;
288bf215546Sopenharmony_ci}
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_cibool
291bf215546Sopenharmony_cinir_opt_memcpy(nir_shader *shader)
292bf215546Sopenharmony_ci{
293bf215546Sopenharmony_ci   bool progress = false;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
296bf215546Sopenharmony_ci      if (function->impl && opt_memcpy_impl(function->impl))
297bf215546Sopenharmony_ci         progress = true;
298bf215546Sopenharmony_ci   }
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   return progress;
301bf215546Sopenharmony_ci}
302