nir: copy intrinsic type when lowering load input/uniform and store output
[mesa.git] / src / compiler / nir / nir_split_var_copies.c
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 #include "nir_builder.h"
30
31 /*
32 * Implements "copy splitting" which is similar to structure splitting only
33 * it works on copy operations rather than the datatypes themselves. The
34 * GLSL language allows you to copy one variable to another an entire
35 * structure (which may contain arrays or other structures) at a time.
36 * Normally, in a language such as C this would be handled by a "structure
37 * splitting" pass that breaks up the structures. Unfortunately for us,
38 * structures used in inputs or outputs can't be split. Therefore,
39 * regardlesss of what we do, we have to be able to copy to/from
40 * structures.
41 *
42 * The primary purpose of structure splitting is to allow you to better
43 * optimize variable access and lower things to registers where you can.
44 * The primary issue here is that, if you lower the copy to a bunch of
45 * loads and stores, you loose a lot of information about the copy
46 * operation that you would like to keep around. To solve this problem, we
47 * have a "copy splitting" pass that, instead of splitting the structures
48 * or lowering the copy into loads and storres, splits the copy operation
49 * into a bunch of copy operations one for each leaf of the structure tree.
50 * If an intermediate array is encountered, it is referenced with a
51 * wildcard reference to indicate that the entire array is to be copied.
52 *
53 * As things become direct, array copies may be able to be losslessly
54 * lowered to having fewer and fewer wildcards. However, until that
55 * happens we want to keep the information about the arrays intact.
56 *
57 * Prior to the copy splitting pass, there are no wildcard references but
58 * there may be incomplete references where the tail of the deref chain is
59 * an array or a structure and not a specific element. After the copy
60 * splitting pass has completed, every variable deref will be a full-length
61 * dereference pointing to a single leaf in the structure type tree with
62 * possibly a few wildcard array dereferences.
63 */
64
65 static void
66 split_deref_copy_instr(nir_builder *b,
67 nir_deref_instr *dst, nir_deref_instr *src)
68 {
69 assert(glsl_get_bare_type(dst->type) ==
70 glsl_get_bare_type(src->type));
71 if (glsl_type_is_vector_or_scalar(src->type)) {
72 nir_copy_deref(b, dst, src);
73 } else if (glsl_type_is_struct_or_ifc(src->type)) {
74 for (unsigned i = 0; i < glsl_get_length(src->type); i++) {
75 split_deref_copy_instr(b, nir_build_deref_struct(b, dst, i),
76 nir_build_deref_struct(b, src, i));
77 }
78 } else {
79 assert(glsl_type_is_matrix(src->type) || glsl_type_is_array(src->type));
80 split_deref_copy_instr(b, nir_build_deref_array_wildcard(b, dst),
81 nir_build_deref_array_wildcard(b, src));
82 }
83 }
84
85 static bool
86 split_var_copies_impl(nir_function_impl *impl)
87 {
88 bool progress = false;
89
90 nir_builder b;
91 nir_builder_init(&b, impl);
92
93 nir_foreach_block(block, impl) {
94 nir_foreach_instr_safe(instr, block) {
95 if (instr->type != nir_instr_type_intrinsic)
96 continue;
97
98 nir_intrinsic_instr *copy = nir_instr_as_intrinsic(instr);
99 if (copy->intrinsic != nir_intrinsic_copy_deref)
100 continue;
101
102 b.cursor = nir_instr_remove(&copy->instr);
103
104 nir_deref_instr *dst =
105 nir_instr_as_deref(copy->src[0].ssa->parent_instr);
106 nir_deref_instr *src =
107 nir_instr_as_deref(copy->src[1].ssa->parent_instr);
108 split_deref_copy_instr(&b, dst, src);
109
110 progress = true;
111 }
112 }
113
114 if (progress) {
115 nir_metadata_preserve(impl, nir_metadata_block_index |
116 nir_metadata_dominance);
117 } else {
118 #ifndef NDEBUG
119 impl->valid_metadata &= ~nir_metadata_not_properly_reset;
120 #endif
121 }
122
123 return progress;
124 }
125
126 bool
127 nir_split_var_copies(nir_shader *shader)
128 {
129 bool progress = false;
130
131 nir_foreach_function(function, shader) {
132 if (function->impl)
133 progress = split_var_copies_impl(function->impl) || progress;
134 }
135
136 return progress;
137 }