1 /*
2 * Copyright © 2015 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
24 #include "nir.h"
25
26 /**
27 * \file nir_sweep.c
28 *
29 * The nir_sweep() pass performs a mark and sweep pass over a nir_shader's associated
30 * memory - anything still connected to the program will be kept, and any dead memory
31 * we dropped on the floor will be freed.
32 *
33 * The expectation is that drivers should call this when finished compiling the shader
34 * (after any optimization, lowering, and so on). However, it's also fine to call it
35 * earlier, and even many times, trading CPU cycles for memory savings.
36 */
37
38 #define steal_list(mem_ctx, type, list) \
39 foreach_list_typed(type, obj, node, list) { \
40 ralloc_steal(mem_ctx, obj); \
41 }
42
43 static void sweep_cf_node(nir_shader *nir, nir_cf_node *cf_node);
44
45 static void
sweep_block(nir_shader * nir,nir_block * block)46 sweep_block(nir_shader *nir, nir_block *block)
47 {
48 ralloc_steal(nir, block);
49
50 /* sweep_impl will mark all metadata invalid. We can safely release all of
51 * this here.
52 */
53 ralloc_free(block->live_in);
54 block->live_in = NULL;
55
56 ralloc_free(block->live_out);
57 block->live_out = NULL;
58
59 nir_foreach_instr(instr, block) {
60 gc_mark_live(nir->gctx, instr);
61
62 switch (instr->type) {
63 case nir_instr_type_tex:
64 gc_mark_live(nir->gctx, nir_instr_as_tex(instr)->src);
65 break;
66 case nir_instr_type_phi:
67 nir_foreach_phi_src(src, nir_instr_as_phi(instr))
68 gc_mark_live(nir->gctx, src);
69 break;
70 case nir_instr_type_intrinsic:
71 ralloc_steal(nir, (void*)nir_instr_as_intrinsic(instr)->name);
72 break;
73 default:
74 break;
75 }
76 }
77 }
78
79 static void
sweep_if(nir_shader * nir,nir_if * iff)80 sweep_if(nir_shader *nir, nir_if *iff)
81 {
82 ralloc_steal(nir, iff);
83
84 foreach_list_typed(nir_cf_node, cf_node, node, &iff->then_list) {
85 sweep_cf_node(nir, cf_node);
86 }
87
88 foreach_list_typed(nir_cf_node, cf_node, node, &iff->else_list) {
89 sweep_cf_node(nir, cf_node);
90 }
91 }
92
93 static void
sweep_loop(nir_shader * nir,nir_loop * loop)94 sweep_loop(nir_shader *nir, nir_loop *loop)
95 {
96 assert(!nir_loop_has_continue_construct(loop));
97 ralloc_steal(nir, loop);
98
99 foreach_list_typed(nir_cf_node, cf_node, node, &loop->body) {
100 sweep_cf_node(nir, cf_node);
101 }
102 }
103
104 static void
sweep_cf_node(nir_shader * nir,nir_cf_node * cf_node)105 sweep_cf_node(nir_shader *nir, nir_cf_node *cf_node)
106 {
107 switch (cf_node->type) {
108 case nir_cf_node_block:
109 sweep_block(nir, nir_cf_node_as_block(cf_node));
110 break;
111 case nir_cf_node_if:
112 sweep_if(nir, nir_cf_node_as_if(cf_node));
113 break;
114 case nir_cf_node_loop:
115 sweep_loop(nir, nir_cf_node_as_loop(cf_node));
116 break;
117 default:
118 unreachable("Invalid CF node type");
119 }
120 }
121
122 static void
sweep_impl(nir_shader * nir,nir_function_impl * impl)123 sweep_impl(nir_shader *nir, nir_function_impl *impl)
124 {
125 ralloc_steal(nir, impl);
126
127 steal_list(nir, nir_variable, &impl->locals);
128
129 foreach_list_typed(nir_cf_node, cf_node, node, &impl->body) {
130 sweep_cf_node(nir, cf_node);
131 }
132
133 sweep_block(nir, impl->end_block);
134
135 /* Wipe out all the metadata, if any. */
136 nir_metadata_preserve(impl, nir_metadata_none);
137 }
138
139 static void
sweep_function(nir_shader * nir,nir_function * f)140 sweep_function(nir_shader *nir, nir_function *f)
141 {
142 ralloc_steal(nir, f);
143 ralloc_steal(nir, f->params);
144
145 if (f->impl)
146 sweep_impl(nir, f->impl);
147 }
148
149 void
nir_sweep(nir_shader * nir)150 nir_sweep(nir_shader *nir)
151 {
152 void *rubbish = ralloc_context(NULL);
153
154 struct list_head instr_gc_list;
155 list_inithead(&instr_gc_list);
156
157 /* First, move ownership of all the memory to a temporary context; assume dead. */
158 ralloc_adopt(rubbish, nir);
159
160 /* Start sweeping */
161 gc_sweep_start(nir->gctx);
162
163 ralloc_steal(nir, nir->gctx);
164 ralloc_steal(nir, (char *)nir->info.name);
165 if (nir->info.label)
166 ralloc_steal(nir, (char *)nir->info.label);
167
168 /* Variables are not dead. Steal them back. */
169 steal_list(nir, nir_variable, &nir->variables);
170
171 /* Recurse into functions, stealing their contents back. */
172 foreach_list_typed(nir_function, func, node, &nir->functions) {
173 sweep_function(nir, func);
174 }
175
176 ralloc_steal(nir, nir->constant_data);
177 ralloc_steal(nir, nir->xfb_info);
178 ralloc_steal(nir, nir->printf_info);
179 for (int i = 0; i < nir->printf_info_count; i++) {
180 ralloc_steal(nir, nir->printf_info[i].arg_sizes);
181 ralloc_steal(nir, nir->printf_info[i].strings);
182 }
183
184 /* Free everything we didn't steal back. */
185 gc_sweep_end(nir->gctx);
186 ralloc_free(rubbish);
187 }
188