2 * Copyright (c) 2020 Lima Project
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
25 #include "nir_builder.h"
29 lima_nir_duplicate_intrinsic(nir_builder
*b
, nir_intrinsic_instr
*itr
,
32 nir_intrinsic_instr
*last_dupl
= NULL
;
33 nir_instr
*last_parent_instr
= NULL
;
35 nir_foreach_use_safe(use_src
, &itr
->dest
.ssa
) {
36 nir_intrinsic_instr
*dupl
;
38 if (last_parent_instr
!= use_src
->parent_instr
) {
39 /* if ssa use, clone for the target block */
40 b
->cursor
= nir_before_instr(use_src
->parent_instr
);
41 dupl
= nir_intrinsic_instr_create(b
->shader
, op
);
42 dupl
->num_components
= itr
->num_components
;
43 memcpy(dupl
->const_index
, itr
->const_index
, sizeof(itr
->const_index
));
44 dupl
->src
[0].is_ssa
= itr
->src
[0].is_ssa
;
45 if (itr
->src
[0].is_ssa
)
46 dupl
->src
[0].ssa
= itr
->src
[0].ssa
;
48 dupl
->src
[0].reg
= itr
->src
[0].reg
;
50 nir_ssa_dest_init(&dupl
->instr
, &dupl
->dest
,
51 dupl
->num_components
, itr
->dest
.ssa
.bit_size
,
54 dupl
->instr
.pass_flags
= 1;
55 nir_builder_instr_insert(b
, &dupl
->instr
);
61 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, nir_src_for_ssa(&dupl
->dest
.ssa
));
62 last_parent_instr
= use_src
->parent_instr
;
67 last_parent_instr
= NULL
;
69 nir_foreach_if_use_safe(use_src
, &itr
->dest
.ssa
) {
70 nir_intrinsic_instr
*dupl
;
72 if (last_parent_instr
!= use_src
->parent_instr
) {
73 /* if 'if use', clone where it is */
74 b
->cursor
= nir_before_instr(&itr
->instr
);
75 dupl
= nir_intrinsic_instr_create(b
->shader
, op
);
76 dupl
->num_components
= itr
->num_components
;
77 memcpy(dupl
->const_index
, itr
->const_index
, sizeof(itr
->const_index
));
78 dupl
->src
[0].is_ssa
= itr
->src
[0].is_ssa
;
79 if (itr
->src
[0].is_ssa
)
80 dupl
->src
[0].ssa
= itr
->src
[0].ssa
;
82 dupl
->src
[0].reg
= itr
->src
[0].reg
;
84 nir_ssa_dest_init(&dupl
->instr
, &dupl
->dest
,
85 dupl
->num_components
, itr
->dest
.ssa
.bit_size
,
88 dupl
->instr
.pass_flags
= 1;
89 nir_builder_instr_insert(b
, &dupl
->instr
);
95 nir_if_rewrite_condition(use_src
->parent_if
, nir_src_for_ssa(&dupl
->dest
.ssa
));
96 last_parent_instr
= use_src
->parent_instr
;
100 nir_instr_remove(&itr
->instr
);
105 lima_nir_duplicate_intrinsic_impl(nir_shader
*shader
, nir_function_impl
*impl
,
109 nir_builder_init(&builder
, impl
);
111 nir_foreach_block(block
, impl
) {
112 nir_foreach_instr(instr
, block
) {
113 instr
->pass_flags
= 0;
116 nir_foreach_instr_safe(instr
, block
) {
117 if (instr
->type
!= nir_instr_type_intrinsic
)
120 nir_intrinsic_instr
*itr
= nir_instr_as_intrinsic(instr
);
122 if (itr
->intrinsic
!= op
)
125 if (itr
->instr
.pass_flags
)
128 if (!itr
->dest
.is_ssa
)
131 lima_nir_duplicate_intrinsic(&builder
, itr
, op
);
135 nir_metadata_preserve(impl
, nir_metadata_block_index
|
136 nir_metadata_dominance
);
139 /* Duplicate load uniforms for every user.
140 * Helps by utilizing the load uniform instruction slots that would
141 * otherwise stay empty, and reduces register pressure. */
143 lima_nir_duplicate_load_uniforms(nir_shader
*shader
)
145 nir_foreach_function(function
, shader
) {
146 if (function
->impl
) {
147 lima_nir_duplicate_intrinsic_impl(shader
, function
->impl
, nir_intrinsic_load_uniform
);
152 /* Duplicate load inputs for every user.
153 * Helps by utilizing the load input instruction slots that would
154 * otherwise stay empty, and reduces register pressure. */
156 lima_nir_duplicate_load_inputs(nir_shader
*shader
)
158 nir_foreach_function(function
, shader
) {
159 if (function
->impl
) {
160 lima_nir_duplicate_intrinsic_impl(shader
, function
->impl
, nir_intrinsic_load_input
);