• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    nir_foreach_instr(instr, block) {
51       gc_mark_live(nir->gctx, instr);
52 
53       switch (instr->type) {
54       case nir_instr_type_tex:
55          gc_mark_live(nir->gctx, nir_instr_as_tex(instr)->src);
56          break;
57       case nir_instr_type_phi:
58          nir_foreach_phi_src(src, nir_instr_as_phi(instr))
59             gc_mark_live(nir->gctx, src);
60          break;
61       case nir_instr_type_intrinsic:
62          ralloc_steal(nir, (void*)nir_instr_as_intrinsic(instr)->name);
63          break;
64       default:
65          break;
66       }
67    }
68 }
69 
70 static void
sweep_if(nir_shader * nir,nir_if * iff)71 sweep_if(nir_shader *nir, nir_if *iff)
72 {
73    ralloc_steal(nir, iff);
74 
75    foreach_list_typed(nir_cf_node, cf_node, node, &iff->then_list) {
76       sweep_cf_node(nir, cf_node);
77    }
78 
79    foreach_list_typed(nir_cf_node, cf_node, node, &iff->else_list) {
80       sweep_cf_node(nir, cf_node);
81    }
82 }
83 
84 static void
sweep_loop(nir_shader * nir,nir_loop * loop)85 sweep_loop(nir_shader *nir, nir_loop *loop)
86 {
87    assert(!nir_loop_has_continue_construct(loop));
88    ralloc_steal(nir, loop);
89 
90    foreach_list_typed(nir_cf_node, cf_node, node, &loop->body) {
91       sweep_cf_node(nir, cf_node);
92    }
93 }
94 
95 static void
sweep_cf_node(nir_shader * nir,nir_cf_node * cf_node)96 sweep_cf_node(nir_shader *nir, nir_cf_node *cf_node)
97 {
98    switch (cf_node->type) {
99    case nir_cf_node_block:
100       sweep_block(nir, nir_cf_node_as_block(cf_node));
101       break;
102    case nir_cf_node_if:
103       sweep_if(nir, nir_cf_node_as_if(cf_node));
104       break;
105    case nir_cf_node_loop:
106       sweep_loop(nir, nir_cf_node_as_loop(cf_node));
107       break;
108    default:
109       unreachable("Invalid CF node type");
110    }
111 }
112 
113 static void
sweep_impl(nir_shader * nir,nir_function_impl * impl)114 sweep_impl(nir_shader *nir, nir_function_impl *impl)
115 {
116    ralloc_steal(nir, impl);
117 
118    steal_list(nir, nir_variable, &impl->locals);
119 
120    foreach_list_typed(nir_cf_node, cf_node, node, &impl->body) {
121       sweep_cf_node(nir, cf_node);
122    }
123 
124    sweep_block(nir, impl->end_block);
125 
126    /* Wipe out all the metadata, if any. */
127    nir_metadata_preserve(impl, nir_metadata_none);
128 }
129 
130 static void
sweep_function(nir_shader * nir,nir_function * f)131 sweep_function(nir_shader *nir, nir_function *f)
132 {
133    ralloc_steal(nir, f);
134    ralloc_steal(nir, f->params);
135 
136    if (f->impl)
137       sweep_impl(nir, f->impl);
138 }
139 
140 void
nir_sweep(nir_shader * nir)141 nir_sweep(nir_shader *nir)
142 {
143    void *rubbish = ralloc_context(NULL);
144 
145    struct list_head instr_gc_list;
146    list_inithead(&instr_gc_list);
147 
148    /* First, move ownership of all the memory to a temporary context; assume dead. */
149    ralloc_adopt(rubbish, nir);
150 
151    /* Start sweeping */
152    gc_sweep_start(nir->gctx);
153 
154    ralloc_steal(nir, nir->gctx);
155    ralloc_steal(nir, (char *)nir->info.name);
156    if (nir->info.label)
157       ralloc_steal(nir, (char *)nir->info.label);
158 
159    /* Variables are not dead.  Steal them back. */
160    steal_list(nir, nir_variable, &nir->variables);
161 
162    /* Recurse into functions, stealing their contents back. */
163    foreach_list_typed(nir_function, func, node, &nir->functions) {
164       sweep_function(nir, func);
165    }
166 
167    ralloc_steal(nir, nir->constant_data);
168    ralloc_steal(nir, nir->xfb_info);
169    ralloc_steal(nir, nir->printf_info);
170    for (int i = 0; i < nir->printf_info_count; i++) {
171       ralloc_steal(nir, nir->printf_info[i].arg_sizes);
172       ralloc_steal(nir, nir->printf_info[i].strings);
173    }
174 
175    /* Free everything we didn't steal back. */
176    gc_sweep_end(nir->gctx);
177    ralloc_free(rubbish);
178 }
179