2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
29 #include "nir_builder.h"
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
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.
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.
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.
66 split_deref_copy_instr(nir_builder
*b
,
67 nir_deref_instr
*dst
, nir_deref_instr
*src
)
69 assert(dst
->type
== src
->type
);
70 if (glsl_type_is_vector_or_scalar(src
->type
)) {
71 nir_copy_deref(b
, dst
, src
);
72 } else if (glsl_type_is_struct(src
->type
)) {
73 for (unsigned i
= 0; i
< glsl_get_length(src
->type
); i
++) {
74 split_deref_copy_instr(b
, nir_build_deref_struct(b
, dst
, i
),
75 nir_build_deref_struct(b
, src
, i
));
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
));
85 split_var_copies_impl(nir_function_impl
*impl
)
87 bool progress
= false;
90 nir_builder_init(&b
, impl
);
92 nir_foreach_block(block
, impl
) {
93 nir_foreach_instr_safe(instr
, block
) {
94 if (instr
->type
!= nir_instr_type_intrinsic
)
97 nir_intrinsic_instr
*copy
= nir_instr_as_intrinsic(instr
);
98 if (copy
->intrinsic
!= nir_intrinsic_copy_deref
)
101 b
.cursor
= nir_instr_remove(©
->instr
);
103 nir_deref_instr
*dst
=
104 nir_instr_as_deref(copy
->src
[0].ssa
->parent_instr
);
105 nir_deref_instr
*src
=
106 nir_instr_as_deref(copy
->src
[1].ssa
->parent_instr
);
107 split_deref_copy_instr(&b
, dst
, src
);
114 nir_metadata_preserve(impl
, nir_metadata_block_index
|
115 nir_metadata_dominance
);
122 nir_split_var_copies(nir_shader
*shader
)
124 bool progress
= false;
126 nir_foreach_function(function
, shader
) {
128 progress
= split_var_copies_impl(function
->impl
) || progress
;