1 /*
2 * Copyright © 2019 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 static bool
nir_lower_array_deref_of_vec_impl(nir_function_impl * impl,nir_variable_mode modes,nir_lower_array_deref_of_vec_options options)28 nir_lower_array_deref_of_vec_impl(nir_function_impl *impl,
29 nir_variable_mode modes,
30 nir_lower_array_deref_of_vec_options options)
31 {
32 bool progress = false;
33
34 nir_builder b = nir_builder_create(impl);
35
36 nir_foreach_block(block, impl) {
37 nir_foreach_instr_safe(instr, block) {
38 if (instr->type != nir_instr_type_intrinsic)
39 continue;
40
41 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
42 assert(intrin->intrinsic != nir_intrinsic_copy_deref);
43
44 if (intrin->intrinsic != nir_intrinsic_load_deref &&
45 intrin->intrinsic != nir_intrinsic_interp_deref_at_centroid &&
46 intrin->intrinsic != nir_intrinsic_interp_deref_at_sample &&
47 intrin->intrinsic != nir_intrinsic_interp_deref_at_offset &&
48 intrin->intrinsic != nir_intrinsic_interp_deref_at_vertex &&
49 intrin->intrinsic != nir_intrinsic_store_deref)
50 continue;
51
52 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
53
54 /* We choose to be conservative here. If the deref contains any
55 * modes which weren't specified, we bail and don't bother lowering.
56 */
57 if (!nir_deref_mode_must_be(deref, modes))
58 continue;
59
60 /* We only care about array derefs that act on vectors */
61 if (deref->deref_type != nir_deref_type_array)
62 continue;
63
64 nir_deref_instr *vec_deref = nir_deref_instr_parent(deref);
65 if (!glsl_type_is_vector(vec_deref->type))
66 continue;
67
68 assert(intrin->num_components == 1);
69 unsigned num_components = glsl_get_components(vec_deref->type);
70 assert(num_components > 1 && num_components <= NIR_MAX_VEC_COMPONENTS);
71
72 b.cursor = nir_after_instr(&intrin->instr);
73
74 if (intrin->intrinsic == nir_intrinsic_store_deref) {
75 nir_def *value = intrin->src[1].ssa;
76
77 if (nir_src_is_const(deref->arr.index)) {
78 if (!(options & nir_lower_direct_array_deref_of_vec_store))
79 continue;
80
81 unsigned index = nir_src_as_uint(deref->arr.index);
82 /* If index is OOB, we throw the old store away and don't
83 * replace it with anything.
84 */
85 if (index < num_components)
86 nir_build_write_masked_store(&b, vec_deref, value, index);
87 } else {
88 if (!(options & nir_lower_indirect_array_deref_of_vec_store))
89 continue;
90
91 nir_def *index = deref->arr.index.ssa;
92 nir_build_write_masked_stores(&b, vec_deref, value, index,
93 0, num_components);
94 }
95 nir_instr_remove(&intrin->instr);
96
97 progress = true;
98 } else {
99 if (nir_src_is_const(deref->arr.index)) {
100 if (!(options & nir_lower_direct_array_deref_of_vec_load))
101 continue;
102 } else {
103 if (!(options & nir_lower_indirect_array_deref_of_vec_load))
104 continue;
105 }
106
107 /* Turn the load into a vector load */
108 nir_src_rewrite(&intrin->src[0], &vec_deref->def);
109 intrin->def.num_components = num_components;
110 intrin->num_components = num_components;
111
112 nir_def *index = deref->arr.index.ssa;
113 nir_def *scalar =
114 nir_vector_extract(&b, &intrin->def, index);
115 if (scalar->parent_instr->type == nir_instr_type_undef) {
116 nir_def_rewrite_uses(&intrin->def,
117 scalar);
118 nir_instr_remove(&intrin->instr);
119 } else {
120 nir_def_rewrite_uses_after(&intrin->def,
121 scalar,
122 scalar->parent_instr);
123 }
124 progress = true;
125 }
126 }
127 }
128
129 if (progress) {
130 nir_metadata_preserve(impl, nir_metadata_block_index |
131 nir_metadata_dominance);
132 } else {
133 nir_metadata_preserve(impl, nir_metadata_all);
134 }
135
136 return progress;
137 }
138
139 /* Lowers away array dereferences on vectors
140 *
141 * These are allowed on certain variable types such as SSBOs and TCS outputs.
142 * However, not everyone can actually handle them everywhere. There are also
143 * cases where we want to lower them for performance reasons.
144 *
145 * This patch assumes that copy_deref instructions have already been lowered.
146 */
147 bool
nir_lower_array_deref_of_vec(nir_shader * shader,nir_variable_mode modes,nir_lower_array_deref_of_vec_options options)148 nir_lower_array_deref_of_vec(nir_shader *shader, nir_variable_mode modes,
149 nir_lower_array_deref_of_vec_options options)
150 {
151 bool progress = false;
152
153 nir_foreach_function_impl(impl, shader) {
154 if (nir_lower_array_deref_of_vec_impl(impl, modes, options))
155 progress = true;
156 }
157
158 return progress;
159 }
160