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"
30 #include "nir_deref.h"
31 #include "compiler/nir_types.h"
34 * Lowers all copy intrinsics to sequences of load/store intrinsics.
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
)
42 for (; **deref_arr
; (*deref_arr
)++) {
43 if ((**deref_arr
)->deref_type
== nir_deref_type_array_wildcard
)
46 parent
= nir_build_deref_follower(b
, parent
, **deref_arr
);
49 assert(**deref_arr
== NULL
);
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
)
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
);
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
);
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
));
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
),
82 nir_build_deref_array(b
, src_deref
, index
),
86 assert(dst_deref
->type
== src_deref
->type
);
87 assert(glsl_type_is_vector_or_scalar(dst_deref
->type
));
89 nir_store_deref(b
, dst_deref
, nir_load_deref(b
, src_deref
), ~0);
94 nir_lower_deref_copy_instr(nir_builder
*b
, nir_intrinsic_instr
*copy
)
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.
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
);
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
);
107 b
->cursor
= nir_before_instr(©
->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]);
111 nir_deref_path_finish(&dst_path
);
112 nir_deref_path_finish(&src_path
);
116 lower_var_copies_impl(nir_function_impl
*impl
)
118 bool progress
= false;
121 nir_builder_init(&b
, impl
);
123 nir_foreach_block(block
, impl
) {
124 nir_foreach_instr_safe(instr
, block
) {
125 if (instr
->type
!= nir_instr_type_intrinsic
)
128 nir_intrinsic_instr
*copy
= nir_instr_as_intrinsic(instr
);
129 if (copy
->intrinsic
!= nir_intrinsic_copy_deref
)
132 nir_lower_deref_copy_instr(&b
, copy
);
134 nir_instr_remove(©
->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]));
144 nir_metadata_preserve(impl
, nir_metadata_block_index
|
145 nir_metadata_dominance
);
150 /* Lowers every copy_var instruction in the program to a sequence of
151 * load/store instructions.
154 nir_lower_var_copies(nir_shader
*shader
)
156 bool progress
= false;
158 nir_assert_unlowered_derefs(shader
, nir_lower_load_store_derefs
);
160 nir_foreach_function(function
, shader
) {
162 progress
|= lower_var_copies_impl(function
->impl
);