nir: Remove old-school deref chain support
[mesa.git] / src / compiler / nir / nir_lower_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 #include "nir_deref.h"
31 #include "compiler/nir_types.h"
32
33 /*
34 * Lowers all copy intrinsics to sequences of load/store intrinsics.
35 */
36
37 static nir_deref_instr *
38 build_deref_to_next_wildcard(nir_builder *b,
39 nir_deref_instr *parent,
40 nir_deref_instr ***deref_arr)
41 {
42 for (; **deref_arr; (*deref_arr)++) {
43 if ((**deref_arr)->deref_type == nir_deref_type_array_wildcard)
44 return parent;
45
46 parent = nir_build_deref_follower(b, parent, **deref_arr);
47 }
48
49 assert(**deref_arr == NULL);
50 *deref_arr = NULL;
51 return parent;
52 }
53
54 static void
55 emit_deref_copy_load_store(nir_builder *b,
56 nir_deref_instr *dst_deref,
57 nir_deref_instr **dst_deref_arr,
58 nir_deref_instr *src_deref,
59 nir_deref_instr **src_deref_arr)
60 {
61 if (dst_deref_arr || src_deref_arr) {
62 assert(dst_deref_arr && src_deref_arr);
63 dst_deref = build_deref_to_next_wildcard(b, dst_deref, &dst_deref_arr);
64 src_deref = build_deref_to_next_wildcard(b, src_deref, &src_deref_arr);
65 }
66
67 if (dst_deref_arr || src_deref_arr) {
68 assert(dst_deref_arr && src_deref_arr);
69 assert((*dst_deref_arr)->deref_type == nir_deref_type_array_wildcard);
70 assert((*src_deref_arr)->deref_type == nir_deref_type_array_wildcard);
71
72 unsigned length = glsl_get_length(src_deref->type);
73 /* The wildcards should represent the same number of elements */
74 assert(length == glsl_get_length(dst_deref->type));
75 assert(length > 0);
76
77 for (unsigned i = 0; i < length; i++) {
78 nir_ssa_def *index = nir_imm_int(b, i);
79 emit_deref_copy_load_store(b,
80 nir_build_deref_array(b, dst_deref, index),
81 dst_deref_arr + 1,
82 nir_build_deref_array(b, src_deref, index),
83 src_deref_arr + 1);
84 }
85 } else {
86 assert(dst_deref->type == src_deref->type);
87 assert(glsl_type_is_vector_or_scalar(dst_deref->type));
88
89 nir_store_deref(b, dst_deref, nir_load_deref(b, src_deref), ~0);
90 }
91 }
92
93 void
94 nir_lower_deref_copy_instr(nir_builder *b, nir_intrinsic_instr *copy)
95 {
96 /* Unfortunately, there's just no good way to handle wildcards except to
97 * flip the chain around and walk the list from variable to final pointer.
98 */
99 assert(copy->src[0].is_ssa && copy->src[1].is_ssa);
100 nir_deref_instr *dst = nir_instr_as_deref(copy->src[0].ssa->parent_instr);
101 nir_deref_instr *src = nir_instr_as_deref(copy->src[1].ssa->parent_instr);
102
103 nir_deref_path dst_path, src_path;
104 nir_deref_path_init(&dst_path, dst, NULL);
105 nir_deref_path_init(&src_path, src, NULL);
106
107 b->cursor = nir_before_instr(&copy->instr);
108 emit_deref_copy_load_store(b, dst_path.path[0], &dst_path.path[1],
109 src_path.path[0], &src_path.path[1]);
110
111 nir_deref_path_finish(&dst_path);
112 nir_deref_path_finish(&src_path);
113 }
114
115 static bool
116 lower_var_copies_impl(nir_function_impl *impl)
117 {
118 bool progress = false;
119
120 nir_builder b;
121 nir_builder_init(&b, impl);
122
123 nir_foreach_block(block, impl) {
124 nir_foreach_instr_safe(instr, block) {
125 if (instr->type != nir_instr_type_intrinsic)
126 continue;
127
128 nir_intrinsic_instr *copy = nir_instr_as_intrinsic(instr);
129 if (copy->intrinsic != nir_intrinsic_copy_deref)
130 continue;
131
132 nir_lower_deref_copy_instr(&b, copy);
133
134 nir_instr_remove(&copy->instr);
135 nir_deref_instr_remove_if_unused(nir_src_as_deref(copy->src[0]));
136 nir_deref_instr_remove_if_unused(nir_src_as_deref(copy->src[1]));
137
138 progress = true;
139 ralloc_free(copy);
140 }
141 }
142
143 if (progress)
144 nir_metadata_preserve(impl, nir_metadata_block_index |
145 nir_metadata_dominance);
146
147 return progress;
148 }
149
150 /* Lowers every copy_var instruction in the program to a sequence of
151 * load/store instructions.
152 */
153 bool
154 nir_lower_var_copies(nir_shader *shader)
155 {
156 bool progress = false;
157
158 nir_foreach_function(function, shader) {
159 if (function->impl)
160 progress |= lower_var_copies_impl(function->impl);
161 }
162
163 return progress;
164 }