2 * Copyright © 2015 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
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
29 static bool inline_function_impl(nir_function_impl
*impl
, struct set
*inlined
);
32 inline_functions_block(nir_block
*block
, nir_builder
*b
,
35 bool progress
= false;
36 /* This is tricky. We're iterating over instructions in a block but, as
37 * we go, the block and its instruction list are being split into
38 * pieces. However, this *should* be safe since foreach_safe always
39 * stashes the next thing in the iteration. That next thing will
40 * properly get moved to the next block when it gets split, and we
41 * continue iterating there.
43 nir_foreach_instr_safe(instr
, block
) {
44 if (instr
->type
!= nir_instr_type_call
)
49 nir_call_instr
*call
= nir_instr_as_call(instr
);
50 assert(call
->callee
->impl
);
52 inline_function_impl(call
->callee
->impl
, inlined
);
54 nir_function_impl
*callee_copy
=
55 nir_function_impl_clone(call
->callee
->impl
);
56 callee_copy
->function
= call
->callee
;
58 exec_list_append(&b
->impl
->locals
, &callee_copy
->locals
);
59 exec_list_append(&b
->impl
->registers
, &callee_copy
->registers
);
61 b
->cursor
= nir_before_instr(&call
->instr
);
63 /* Rewrite all of the uses of the callee's parameters to use the call
64 * instructions sources. In order to ensure that the "load" happens
65 * here and not later (for register sources), we make sure to convert it
66 * to an SSA value first.
68 const unsigned num_params
= call
->num_params
;
69 NIR_VLA(nir_ssa_def
*, params
, num_params
);
70 for (unsigned i
= 0; i
< num_params
; i
++) {
71 params
[i
] = nir_ssa_for_src(b
, call
->params
[i
],
72 call
->callee
->params
[i
].num_components
);
75 nir_foreach_block(block
, callee_copy
) {
76 nir_foreach_instr_safe(instr
, block
) {
77 if (instr
->type
!= nir_instr_type_intrinsic
)
80 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
81 if (load
->intrinsic
!= nir_intrinsic_load_param
)
84 unsigned param_idx
= nir_intrinsic_param_idx(load
);
85 assert(param_idx
< num_params
);
86 assert(load
->dest
.is_ssa
);
87 nir_ssa_def_rewrite_uses(&load
->dest
.ssa
,
88 nir_src_for_ssa(params
[param_idx
]));
90 /* Remove any left-over load_param intrinsics because they're soon
91 * to be in another function and therefore no longer valid.
93 nir_instr_remove(&load
->instr
);
97 /* Pluck the body out of the function and place it here */
99 nir_cf_list_extract(&body
, &callee_copy
->body
);
100 nir_cf_reinsert(&body
, b
->cursor
);
102 nir_instr_remove(&call
->instr
);
109 inline_function_impl(nir_function_impl
*impl
, struct set
*inlined
)
111 if (_mesa_set_search(inlined
, impl
))
112 return false; /* Already inlined */
115 nir_builder_init(&b
, impl
);
117 bool progress
= false;
118 nir_foreach_block_safe(block
, impl
) {
119 progress
|= inline_functions_block(block
, &b
, inlined
);
123 /* SSA and register indices are completely messed up now */
124 nir_index_ssa_defs(impl
);
125 nir_index_local_regs(impl
);
127 nir_metadata_preserve(impl
, nir_metadata_none
);
130 _mesa_set_add(inlined
, impl
);
136 nir_inline_functions(nir_shader
*shader
)
138 struct set
*inlined
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
139 _mesa_key_pointer_equal
);
140 bool progress
= false;
142 nir_foreach_function(function
, shader
) {
144 progress
= inline_function_impl(function
->impl
, inlined
) || progress
;
147 _mesa_set_destroy(inlined
, NULL
);