xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_split_var_copies.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 
24 #include "nir.h"
25 #include "nir_builder.h"
26 
27 /*
28  * Implements "copy splitting" which is similar to structure splitting only
29  * it works on copy operations rather than the datatypes themselves.  The
30  * GLSL language allows you to copy one variable to another an entire
31  * structure (which may contain arrays or other structures) at a time.
32  * Normally, in a language such as C this would be handled by a "structure
33  * splitting" pass that breaks up the structures.  Unfortunately for us,
34  * structures used in inputs or outputs can't be split.  Therefore,
35  * regardlesss of what we do, we have to be able to copy to/from
36  * structures.
37  *
38  * The primary purpose of structure splitting is to allow you to better
39  * optimize variable access and lower things to registers where you can.
40  * The primary issue here is that, if you lower the copy to a bunch of
41  * loads and stores, you loose a lot of information about the copy
42  * operation that you would like to keep around.  To solve this problem, we
43  * have a "copy splitting" pass that, instead of splitting the structures
44  * or lowering the copy into loads and storres, splits the copy operation
45  * into a bunch of copy operations one for each leaf of the structure tree.
46  * If an intermediate array is encountered, it is referenced with a
47  * wildcard reference to indicate that the entire array is to be copied.
48  *
49  * As things become direct, array copies may be able to be losslessly
50  * lowered to having fewer and fewer wildcards.  However, until that
51  * happens we want to keep the information about the arrays intact.
52  *
53  * Prior to the copy splitting pass, there are no wildcard references but
54  * there may be incomplete references where the tail of the deref chain is
55  * an array or a structure and not a specific element.  After the copy
56  * splitting pass has completed, every variable deref will be a full-length
57  * dereference pointing to a single leaf in the structure type tree with
58  * possibly a few wildcard array dereferences.
59  */
60 
61 static void
split_deref_copy_instr(nir_builder * b,nir_deref_instr * dst,nir_deref_instr * src,enum gl_access_qualifier dst_access,enum gl_access_qualifier src_access)62 split_deref_copy_instr(nir_builder *b,
63                        nir_deref_instr *dst, nir_deref_instr *src,
64                        enum gl_access_qualifier dst_access,
65                        enum gl_access_qualifier src_access)
66 {
67    assert(glsl_get_bare_type(dst->type) ==
68           glsl_get_bare_type(src->type));
69    if (glsl_type_is_vector_or_scalar(src->type)) {
70       nir_copy_deref_with_access(b, dst, src, dst_access, src_access);
71    } else if (glsl_type_is_struct_or_ifc(src->type)) {
72       for (unsigned i = 0; i < glsl_get_length(src->type); i++) {
73          split_deref_copy_instr(b, nir_build_deref_struct(b, dst, i),
74                                 nir_build_deref_struct(b, src, i),
75                                 dst_access, src_access);
76       }
77    } else {
78       assert(glsl_type_is_matrix(src->type) || glsl_type_is_array(src->type));
79       split_deref_copy_instr(b, nir_build_deref_array_wildcard(b, dst),
80                              nir_build_deref_array_wildcard(b, src),
81                              dst_access, src_access);
82    }
83 }
84 
85 static bool
split_var_copies_instr(nir_builder * b,nir_intrinsic_instr * copy,UNUSED void * cb_data)86 split_var_copies_instr(nir_builder *b, nir_intrinsic_instr *copy,
87                        UNUSED void *cb_data)
88 {
89    if (copy->intrinsic != nir_intrinsic_copy_deref)
90       return false;
91 
92    b->cursor = nir_instr_remove(&copy->instr);
93 
94    nir_deref_instr *dst = nir_instr_as_deref(copy->src[0].ssa->parent_instr);
95    nir_deref_instr *src = nir_instr_as_deref(copy->src[1].ssa->parent_instr);
96    split_deref_copy_instr(b, dst, src,
97                           nir_intrinsic_dst_access(copy),
98                           nir_intrinsic_src_access(copy));
99 
100    return true;
101 }
102 
103 bool
nir_split_var_copies(nir_shader * shader)104 nir_split_var_copies(nir_shader *shader)
105 {
106    return nir_shader_intrinsics_pass(shader, split_var_copies_instr,
107                                        nir_metadata_control_flow,
108                                        NULL);
109 }
110