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_load_const(nir_builder
*b
, nir_load_const_instr
*load
)
31 nir_load_const_instr
*last_dupl
= NULL
;
32 nir_instr
*last_parent_instr
= NULL
;
34 nir_foreach_use_safe(use_src
, &load
->def
) {
35 nir_load_const_instr
*dupl
;
37 if (last_parent_instr
!= use_src
->parent_instr
) {
38 /* if ssa use, clone for the target block */
39 b
->cursor
= nir_before_instr(use_src
->parent_instr
);
41 dupl
= nir_load_const_instr_create(b
->shader
, load
->def
.num_components
,
43 memcpy(&dupl
->value
, &load
->value
, sizeof(*load
->value
) * load
->def
.num_components
);
45 dupl
->instr
.pass_flags
= 1;
46 nir_builder_instr_insert(b
, &dupl
->instr
);
52 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, nir_src_for_ssa(&dupl
->def
));
53 last_parent_instr
= use_src
->parent_instr
;
58 last_parent_instr
= NULL
;
60 nir_foreach_if_use_safe(use_src
, &load
->def
) {
61 nir_load_const_instr
*dupl
;
63 if (last_parent_instr
!= use_src
->parent_instr
) {
64 /* if 'if use', clone where it is */
65 b
->cursor
= nir_before_instr(&load
->instr
);
67 dupl
= nir_load_const_instr_create(b
->shader
, load
->def
.num_components
,
69 memcpy(&dupl
->value
, &load
->value
, sizeof(*load
->value
) * load
->def
.num_components
);
71 dupl
->instr
.pass_flags
= 1;
72 nir_builder_instr_insert(b
, &dupl
->instr
);
78 nir_if_rewrite_condition(use_src
->parent_if
, nir_src_for_ssa(&dupl
->def
));
79 last_parent_instr
= use_src
->parent_instr
;
83 nir_instr_remove(&load
->instr
);
88 lima_nir_duplicate_load_consts_impl(nir_shader
*shader
, nir_function_impl
*impl
)
91 nir_builder_init(&builder
, impl
);
93 nir_foreach_block(block
, impl
) {
94 nir_foreach_instr(instr
, block
) {
95 instr
->pass_flags
= 0;
98 nir_foreach_instr_safe(instr
, block
) {
99 if (instr
->type
!= nir_instr_type_load_const
)
102 nir_load_const_instr
*load
= nir_instr_as_load_const(instr
);
104 if (load
->instr
.pass_flags
)
107 lima_nir_duplicate_load_const(&builder
, load
);
111 nir_metadata_preserve(impl
, nir_metadata_block_index
|
112 nir_metadata_dominance
);
115 /* Duplicate load consts for every user.
116 * Helps by utilizing the load const instruction slots that would
117 * otherwise stay empty, and reduces register pressure. */
119 lima_nir_duplicate_load_consts(nir_shader
*shader
)
121 nir_foreach_function(function
, shader
) {
122 if (function
->impl
) {
123 lima_nir_duplicate_load_consts_impl(shader
, function
->impl
);