Debian Codegraph @

src/panfrost/compiler/bifrost/bi_repair_ssa.c

raw
1 /*
2 * Copyright 2023 Alyssa Rosenzweig
3 * Copyright 2023 Valve Corporation
4 * Copyright 2022 Collabora Ltd.
5 * SPDX-License-Identifier: MIT
6 */
7
8 /*
9 * Implementation of "Simple and Efficient
10 * Construction of Static Single Assignment Form", also by Braun et al.
11 * https://link.springer.com/content/pdf/10.1007/978-3-642-37051-9_6.pdf
12 */
13
14 #include "util/hash_table.h"
15 #include "util/ralloc.h"
16 #include "util/u_dynarray.h"
17 #include "bi_builder.h"
18 #include "compiler.h"
19 #include "bi_opcodes.h"
20
21 struct repair_block {
22 /* For a loop header, whether phis operands have been added */
23 bool sealed;
24
25 /* Sparse map: variable name -> bi_index.
26 *
27 * Definition of a variable at the end of the block.
28 */
29 struct hash_table_u64 *defs;
30 };
31
32 struct repair_ctx {
33 bi_context *shader;
34
35 /* Number of variables */
36 unsigned n;
37
38 /* Information on blocks indexed in source order */
39 struct repair_block *blocks;
40 };
41
42 static inline struct repair_block *
43 repair_block(struct repair_ctx *ctx, bi_block *block)
44 {
45 return &ctx->blocks[block->index];
46 }
47
48 static void
49 record_write(struct repair_ctx *ctx, bi_block *block, unsigned node,
50 bi_index val)
51 {
52 assert(node < ctx->n);
53
54 /* The value always gets used as a source, where we don't want the
55 * destination "swizzle" and other modifiers that are applied on write.
56 */
57 val.swizzle = BI_SWIZZLE_H01;
58 val.neg = false;
59 val.abs = false;
60
61 struct hash_table_u64 *defs = repair_block(ctx, block)->defs;
62 _mesa_hash_table_u64_insert(defs, node,
63 ralloc_memdup(defs, &val, sizeof(val)));
64 }
65
66 static void add_phi_operands(struct repair_ctx *ctx, bi_block *block,
67 bi_instr *phi, bi_index node);
68
69 static bi_index
70 resolve_read(struct repair_ctx *ctx, bi_block *block, bi_index node)
71 {
72 struct repair_block *rb = repair_block(ctx, block);
73
74 /* Local value numbering */
75 assert(node.type == BI_INDEX_NORMAL);
76 bi_index *local = _mesa_hash_table_u64_search(rb->defs, node.value);
77
78 if (local) {
79 assert(!bi_is_null(*local));
80 return *local;
81 }
82
83 /* Global value numbering. readValueRecursive in the paper */
84 unsigned nr_preds = bi_num_predecessors(block);
85 bi_index val;
86
87 assert(nr_preds > 0);
88
89 /* Loop headers are not in the "sealedBlock" set in the paper. To
90 * handle, we insert an incomplete phi to be filled in after the rest of
91 * the loop is processed.
92 */
93 if (block->loop_header && !rb->sealed) {
94 val = bi_temp_like(ctx->shader, node);
95 bi_builder b = bi_init_builder(ctx->shader, bi_before_block(block));
96 bi_instr *phi = bi_phi_to(&b, val, nr_preds);
97 phi->shadow = true;
98
99 /* Stash the variable in for an intrusive incompletePhis map */
100 phi->index = node.value + 1;
101 } else if (nr_preds == 1) {
102 /* No phi needed */
103 bi_block *pred =
104 *util_dynarray_element(&block->predecessors, bi_block *, 0);
105 val = resolve_read(ctx, pred, node);
106 } else {
107 /* Insert phi first to break cycles */
108 val = bi_temp_like(ctx->shader, node);
109 bi_builder b = bi_init_builder(ctx->shader, bi_before_block(block));
110 bi_instr *phi = bi_phi_to(&b, val, nr_preds);
111 phi->shadow = true;
112 record_write(ctx, block, node.value, val);
113 add_phi_operands(ctx, block, phi, node);
114 }
115
116 assert(!bi_is_null(val));
117 assert(val.swizzle == BI_SWIZZLE_H01);
118 record_write(ctx, block, node.value, val);
119 return val;
120 }
121
122 static void
123 add_phi_operands(struct repair_ctx *ctx, bi_block *block, bi_instr *phi,
124 bi_index node)
125 {
126 /* Add phi operands */
127 bi_foreach_predecessor(block, pred) {
128 unsigned s = bi_predecessor_index(block, *pred);
129 phi->src[s] = resolve_read(ctx, *pred, node);
130 }
131 }
132
133 static void
134 seal_block(struct repair_ctx *ctx, bi_block *block)
135 {
136 bi_foreach_phi_in_block(block, phi) {
137 /* We use phi->index as a sideband to pass the variable name. */
138 if (phi->index) {
139 bi_index var = bi_get_index(phi->index - 1);
140 var.memory = phi->dest[0].memory;
141 add_phi_operands(ctx, block, phi, var);
142 phi->index = 0;
143 }
144 }
145
146 repair_block(ctx, block)->sealed = true;
147 }
148
149 static void
150 seal_loop_headers(struct repair_ctx *ctx, struct bi_block *exit)
151 {
152 bi_foreach_successor(exit, succ) {
153 /* Only loop headers need to be sealed late */
154 if (!succ->loop_header)
155 continue;
156
157 /* Check if all predecessors have been processed */
158 bool any_unprocessed = false;
159
160 bi_foreach_predecessor(succ, P) {
161 if ((*P)->index > exit->index) {
162 any_unprocessed = true;
163 break;
164 }
165 }
166
167 /* Seal once all predecessors are processed */
168 if (!any_unprocessed)
169 seal_block(ctx, succ);
170 }
171 }
172
173 static void
174 bi_opt_trivial_phi(bi_context *ctx)
175 {
176 bi_index *remap = calloc(ctx->ssa_alloc, sizeof(*remap));
177 for (;;) {
178 bool progress = false;
179 memset(remap, 0, ctx->ssa_alloc * sizeof(*remap));
180
181 bi_foreach_block(ctx, block) {
182 bi_foreach_phi_in_block_safe(block, phi) {
183 bi_index same = bi_null();
184 bool all_same = true;
185
186 bi_foreach_src(phi, s) {
187 const bi_index src = phi->src[s];
188 /* TODO: Handle cycles faster */
189 if (bi_is_ssa(src) && !bi_is_null(remap[src.value])) {
190 all_same = false;
191 break;
192 }
193
194 /* Same value or self-reference */
195 if (bi_is_equiv(src, same) ||
196 bi_is_equiv(src, phi->dest[0]))
197 continue;
198
199 if (!bi_is_null(same)) {
200 all_same = false;
201 break;
202 }
203
204 same = src;
205 }
206
207 /* Only optimize trivial phis with normal sources. It is possible
208 * to optimize something like `phi #0, #0` but...
209 *
210 * 1. It would inadvently propagate constants which may be invalid.
211 * Copyprop knows the rules for this, but we don't here.
212 *
213 * 2. These trivial phis should be optimized at the NIR level. This
214 * pass is just to clean up spilling.
215 *
216 * So skip them for correctness in case NIR misses something (which
217 * can happen depending on pass order).
218 */
219 if (all_same && same.type == BI_INDEX_NORMAL) {
220 remap[phi->dest[0].value] = same;
221 bi_remove_instruction(phi);
222 progress = true;
223 }
224 }
225 }
226
227 if (!progress)
228 break;
229
230 bi_foreach_instr_global(ctx, I) {
231 bi_foreach_ssa_src(I, s) {
232 if (!bi_is_null(remap[I->src[s].value])) {
233 bi_replace_src(I, s, remap[I->src[s].value]);
234 }
235 }
236 }
237 }
238
239 free(remap);
240 }
241
242 void
243 bi_repair_ssa(bi_context *ctx)
244 {
245 struct repair_block *blocks =
246 rzalloc_array(NULL, struct repair_block, ctx->num_blocks);
247
248 bi_foreach_block(ctx, block) {
249 struct repair_block *rb = &blocks[block->index];
250 rb->defs = _mesa_hash_table_u64_create(blocks);
251 }
252
253 unsigned n = ctx->ssa_alloc;
254
255 bi_foreach_block(ctx, block) {
256 struct repair_ctx rctx = {
257 .shader = ctx,
258 .n = n,
259 .blocks = blocks,
260 };
261
262 bi_foreach_instr_in_block(block, I) {
263 /* Repair SSA for the instruction */
264 if (I->op != BI_OPCODE_PHI) {
265 bi_foreach_ssa_src(I, s) {
266 assert(I->src[s].value < n);
267 bi_replace_src(I, s, resolve_read(&rctx, block, I->src[s]));
268 }
269 }
270
271 bi_foreach_ssa_dest(I, d) {
272 unsigned handle = I->dest[d].value;
273
274 /* Skip phis that we just created when processing loops */
275 if (handle >= n) {
276 assert(I->op == BI_OPCODE_PHI);
277 continue;
278 }
279
280 I->dest[d] =
281 bi_replace_index(I->dest[d], bi_temp_like(ctx, I->dest[d]));
282
283 record_write(&rctx, block, handle, I->dest[d]);
284 }
285 }
286
287 seal_loop_headers(&rctx, block);
288 }
289
290 bi_foreach_block(ctx, block) {
291 bi_foreach_phi_in_block(block, phi) {
292 /* The kill bit is invalid (and meaningless) for phis. Liveness
293 * analysis does not produce it. However, we're ingesting broken SSA
294 * where we can have random kill bits set on phis. Strip them as part
295 * of the SSA repair.
296 *
297 * The register allocator depends on this for correctness.
298 */
299 phi->dest[0].kill_ssa = false;
300
301 bi_foreach_src(phi, s) {
302 phi->src[s].kill_ssa = false;
303 }
304
305 /* Skip the phis that we just created */
306 if (phi->shadow) {
307 phi->shadow = false;
308 continue;
309 }
310
311 bi_foreach_ssa_src(phi, s) {
312 /* Phis (uniquely) read their sources in their corresponding
313 * predecessors, so chain through for that.
314 */
315 bi_block *read_block =
316 *util_dynarray_element(&block->predecessors, bi_block *, s);
317
318 assert(phi->src[s].value < n);
319
320 struct repair_ctx rctx = {
321 .shader = ctx,
322 .n = n,
323 .blocks = blocks,
324 };
325
326 bi_replace_src(phi, s,
327 resolve_read(&rctx, read_block, phi->src[s]));
328 }
329 }
330 }
331
332 ralloc_free(blocks);
333
334 bi_opt_trivial_phi(ctx);
335 bi_reindex_ssa(ctx);
336 }