Debian Codegraph @

src/panfrost/compiler/bifrost/valhall/va_insert_flow.c

raw
1 /*
2 * Copyright (C) 2022 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "panfrost/lib/pan_props.h"
7
8 #include "bi_debug.h"
9 #include "bi_builder.h"
10 #include "va_compiler.h"
11 #include "valhall_enums.h"
12
13 /*
14 * Insert flow control into a scheduled and register allocated shader. This
15 * pass runs after scheduling and register allocation. This pass only
16 * inserts NOPs with the appropriate flow control modifiers. It should be
17 * followed by a cleanup pass to merge flow control modifiers on adjacent
18 * instructions, eliminating the NOPs. This decouples optimization from
19 * correctness, simplifying both passes.
20 *
21 * This pass is responsible for calculating dependencies, according to the
22 * rules:
23 *
24 * 1. An instruction that depends on the results of a previous asyncronous
25 * must first wait for that instruction's slot, unless all
26 * reaching code paths already depended on it.
27 * 2. More generally, any dependencies must be encoded. This includes
28 * Write-After-Write and Write-After-Read hazards with LOAD/STORE to memory.
29 * 3. The shader must wait on slot #6 before running BLEND, ATEST
30 * 4. The shader must wait on slot #7 before running BLEND, ST_TILE
31 * 6. BARRIER must wait on every active slot.
32 *
33 * Unlike Bifrost, it is not necessary to worry about outbound staging
34 * registers, as the hardware stalls reading staging registers when issuing
35 * asynchronous instructions. So we don't track reads in our model of the
36 * hardware scoreboard. This makes things a bit simpler.
37 *
38 * We may reuse slots for multiple asynchronous instructions, though there may
39 * be a performance penalty.
40 */
41
42 #define BI_NUM_REGISTERS 64
43
44 /*
45 * Insert a NOP instruction with given flow control.
46 */
47 static void
48 bi_flow(bi_context *ctx, bi_cursor cursor, enum va_flow flow)
49 {
50 bi_builder b = bi_init_builder(ctx, cursor);
51
52 bi_nop(&b)->flow = flow;
53 }
54
55 static uint64_t
56 bi_read_mask(bi_instr *I)
57 {
58 uint64_t mask = 0;
59
60 bi_foreach_src(I, s) {
61 if (I->src[s].type == BI_INDEX_REGISTER) {
62 unsigned reg = I->src[s].value;
63 unsigned count = bi_count_read_registers(I, s);
64
65 mask |= (BITFIELD64_MASK(count) << reg);
66 }
67 }
68
69 return mask;
70 }
71
72 static uint64_t
73 bi_write_mask(bi_instr *I)
74 {
75 uint64_t mask = 0;
76
77 bi_foreach_dest(I, d) {
78 assert(I->dest[d].type == BI_INDEX_REGISTER);
79
80 unsigned reg = I->dest[d].value;
81 unsigned count = bi_count_write_registers(I, d);
82
83 mask |= (BITFIELD64_MASK(count) << reg);
84 }
85
86 return mask;
87 }
88
89 static bool
90 bi_ld_vary_writes_hidden_register(const bi_instr *I)
91 {
92 /* Only varying loads can write the hidden register */
93 if (bi_get_opcode_props(I)->message != BIFROST_MESSAGE_VARYING)
94 return false;
95
96 /* They only write in some update modes */
97 return (I->update == BI_UPDATE_STORE) || (I->update == BI_UPDATE_CLOBBER);
98 }
99
100 static bool
101 bi_is_memory_access(const bi_instr *I)
102 {
103 /* Some instructions on the attribute unit are functionally
104 a general memory load */
105 switch (I->op) {
106 case BI_OPCODE_LD_ATTR_TEX:
107 case BI_OPCODE_LD_TEX:
108 case BI_OPCODE_LD_TEX_IMM:
109 return true;
110 default:
111 break;
112 }
113
114 /* UBOs are read-only so there are no ordering constriants */
115 if (I->seg == BI_SEG_UBO)
116 return false;
117
118 switch (bi_get_opcode_props(I)->message) {
119 case BIFROST_MESSAGE_LOAD:
120 case BIFROST_MESSAGE_STORE:
121 case BIFROST_MESSAGE_ATOMIC:
122 return true;
123 default:
124 return false;
125 }
126 }
127
128 /* Update the scoreboard model to assign an instruction to a given slot */
129
130 static void
131 bi_push_instr(struct bi_scoreboard_state *st, bi_instr *I)
132 {
133 if (bi_get_opcode_props(I)->sr_write)
134 st->write[I->slot] |= bi_write_mask(I);
135
136 if (bi_is_memory_access(I))
137 st->memory |= BITFIELD_BIT(I->slot);
138
139 if (bi_get_opcode_props(I)->message == BIFROST_MESSAGE_VARYING)
140 st->varying |= BITFIELD_BIT(I->slot);
141 }
142
143 static uint8_t MUST_CHECK
144 bi_pop_slot(struct bi_scoreboard_state *st, unsigned slot)
145 {
146 st->write[slot] = 0;
147 st->varying &= ~BITFIELD_BIT(slot);
148 st->memory &= ~BITFIELD_BIT(slot);
149
150 return BITFIELD_BIT(slot);
151 }
152
153 /* Adds a dependency on each slot writing any specified register */
154
155 static uint8_t MUST_CHECK
156 bi_depend_on_writers(struct bi_scoreboard_state *st, uint64_t regmask)
157 {
158 uint8_t slots = 0;
159
160 for (unsigned slot = 0; slot < ARRAY_SIZE(st->write); ++slot) {
161 if (st->write[slot] & regmask)
162 slots |= bi_pop_slot(st, slot);
163 }
164
165 return slots;
166 }
167
168 /* Sets the dependencies for a given clause, updating the model */
169
170 static void
171 bi_set_dependencies(bi_block *block, bi_instr *I,
172 struct bi_scoreboard_state *st)
173 {
174 /* Depend on writers to handle read-after-write and write-after-write
175 * dependencies. Write-after-read dependencies are handled in the hardware
176 * where necessary, so we don't worry about them.
177 */
178 I->flow |= bi_depend_on_writers(st, bi_read_mask(I) | bi_write_mask(I));
179
180 /* Handle write-after-write and write-after-read dependencies for the varying
181 * hidden registers. Read-after-write dependencies handled in hardware.
182 */
183 if (bi_ld_vary_writes_hidden_register(I)) {
184 u_foreach_bit(slot, st->varying)
185 I->flow |= bi_pop_slot(st, slot);
186 }
187
188 /* For now, serialize all memory access */
189 if (bi_is_memory_access(I)) {
190 u_foreach_bit(slot, st->memory)
191 I->flow |= bi_pop_slot(st, slot);
192 }
193
194 /* We need to wait for all general slots before a barrier. The reason is
195 * unknown. In theory, this is redundant, since the BARRIER instruction will
196 * be followed immediately by .wait which waits for all slots. However, that
197 * doesn't seem to work properly in practice.
198 *
199 * The DDK is observed to use the same workaround, going so far as
200 * introducing a NOP before a BARRIER at the beginning of a basic block when
201 * there are outstanding stores.
202 *
203 * NOP.wait12
204 * BARRIER.slot7.wait
205 *
206 * Luckily, this situation is pretty rare. The wait introduced here can
207 * usually be merged into the preceding instruction.
208 *
209 * We also use the same workaround to serialize all async instructions when
210 * debugging this pass with the BIFROST_MESA_DEBUG=nosb option.
211 */
212 if (I->op == BI_OPCODE_BARRIER || (bifrost_debug & BIFROST_DBG_NOSB)) {
213 for (unsigned i = 0; i < VA_NUM_GENERAL_SLOTS; ++i) {
214 if (st->write[i] || ((st->varying | st->memory) & BITFIELD_BIT(i)))
215 I->flow |= bi_pop_slot(st, i);
216 }
217 }
218 }
219
220 static bool
221 scoreboard_block_update(bi_context *ctx, bi_block *blk)
222 {
223 bool progress = false;
224
225 /* pending_in[s] = sum { p in pred[s] } ( pending_out[p] ) */
226 bi_foreach_predecessor(blk, pred) {
227 for (unsigned i = 0; i < BI_NUM_SLOTS; ++i) {
228 blk->scoreboard_in.read[i] |= (*pred)->scoreboard_out.read[i];
229 blk->scoreboard_in.write[i] |= (*pred)->scoreboard_out.write[i];
230 blk->scoreboard_in.varying |= (*pred)->scoreboard_out.varying;
231 blk->scoreboard_in.memory |= (*pred)->scoreboard_out.memory;
232 }
233 }
234
235 struct bi_scoreboard_state state = blk->scoreboard_in;
236
237 /* Assign locally */
238
239 bi_foreach_instr_in_block(blk, I) {
240 bi_set_dependencies(blk, I, &state);
241 bi_push_instr(&state, I);
242 }
243
244 /* Insert a wait for varyings at the end of the block.
245 *
246 * A varying load with .store has to wait for all other varying loads
247 * in the quad to complete. The bad case looks like:
248 *
249 * if (dynamic) {
250 * x = ld_var()
251 * } else {
252 * x = ld_var()
253 * }
254 *
255 * Logically, a given thread executes only a single ld_var instruction. But
256 * if the quad diverges, the second ld_var has to wait for the first ld_var.
257 * For correct handling, we need to maintain a physical control flow graph
258 * and do the dataflow analysis on that instead of the logical control flow
259 * graph. However, this probably doesn't matter much in practice. This seems
260 * like a decent compromise for now.
261 *
262 * TODO: Consider optimizing this case.
263 */
264 if (state.varying) {
265 uint8_t flow = 0;
266
267 u_foreach_bit(slot, state.varying)
268 flow |= bi_pop_slot(&state, slot);
269
270 bi_flow(ctx, bi_after_block(blk), flow);
271 }
272
273 /* To figure out progress, diff scoreboard_out */
274 progress = !!memcmp(&state, &blk->scoreboard_out, sizeof(state));
275
276 blk->scoreboard_out = state;
277
278 return progress;
279 }
280
281 static void
282 va_assign_scoreboard(bi_context *ctx)
283 {
284 u_worklist worklist;
285 bi_worklist_init(ctx, &worklist);
286
287 bi_foreach_block(ctx, block) {
288 bi_worklist_push_tail(&worklist, block);
289 }
290
291 /* Perform forward data flow analysis to calculate dependencies */
292 while (!u_worklist_is_empty(&worklist)) {
293 /* Pop from the front for forward analysis */
294 bi_block *blk = bi_worklist_pop_head(&worklist);
295
296 if (scoreboard_block_update(ctx, blk)) {
297 bi_foreach_successor(blk, succ)
298 bi_worklist_push_tail(&worklist, succ);
299 }
300 }
301
302 u_worklist_fini(&worklist);
303 }
304
305 /*
306 * Determine if execution should terminate after a given block. Execution cannot
307 * terminate within a basic block.
308 */
309 static bool
310 va_should_end(bi_block *block)
311 {
312 /* Don't return if we're succeeded by instructions */
313 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
314 bi_block *succ = block->successors[i];
315
316 if (succ)
317 return false;
318 }
319
320 return true;
321 }
322
323 /*
324 * We should discard helper invocations as soon as helper invocations die after
325 * their last use. Either they die after an instruction using helper
326 * invocations, or they die along a control flow edge. The former is handled by
327 * discarding appropriately after instructions. The latter is handled by
328 * inserting a discard at the _start_ of some blocks:
329 *
330 * Lemma: If a non-critical edge discards helpers, it is the only edge that
331 * enters its destination.
332 *
333 * Proof: An edge discards helpers if helpers are live at the end of the source
334 * block and dead at the start of the destination block. By definition, helpers
335 * are live at the end of a block iff they are live at the start of some
336 * successor of a block. The source block therefore has a successor with helpers
337 * live at the start and a successor with helpers dead at the start. As the
338 * source block has at least two successors, the edge is NOT the only edge
339 * exiting its source. Hence it is the only edge entering the destination,
340 * otherwise the edge would be critical.
341 *
342 * By corrollary, we may handle discards on control flow edges by discarding at
343 * the start of blocks with a single predecessor.
344 *
345 * This routine tests if a block should discard helper invocations at its start.
346 */
347 static bool
348 va_discard_before_block(bi_block *block)
349 {
350 /* Do not discard if the block requires helpers at the start */
351 if (block->pass_flags)
352 return false;
353
354 /* By the lemma, if we need to discard, there is a unique predecessor */
355 if (bi_num_predecessors(block) != 1)
356 return false;
357
358 bi_block *pred = *util_dynarray_element(&block->predecessors, bi_block *, 0);
359
360 /* Discard if helpers are live at the end of the predecessor, due to helpers
361 * live at the start of some (other) successor.
362 */
363 bi_foreach_successor(pred, succ) {
364 if (succ->pass_flags)
365 return true;
366 }
367
368 return false;
369 }
370
371 /*
372 * Test if a program is empty, in the sense of having zero instructions. Empty
373 * shaders get special handling.
374 */
375 static bool
376 bi_is_empty(bi_context *ctx)
377 {
378 bi_foreach_instr_global(ctx, _)
379 return false;
380
381 return true;
382 }
383
384 /*
385 * Given a program with no flow control modifiers, insert NOPs signaling the
386 * required flow control. Not much optimization happens here.
387 */
388 void
389 va_insert_flow_control_nops(bi_context *ctx)
390 {
391 /* Special case: if a program is empty, leave it empty. In particular, do not
392 * insert NOP.end. There is special handling in the driver for skipping empty
393 * shaders for shader stage. The .end is not necessary and disrupts
394 * optimizations.
395 */
396 if (bi_is_empty(ctx))
397 return;
398
399 /* First do dataflow analysis for the scoreboard. This populates I->flow with
400 * a bitmap of slots to wait on.
401 *
402 * Also do dataflow analysis for helper invocations in fragment shaders. This
403 * populates block->pass_flags with helper invocation information.
404 */
405 va_assign_scoreboard(ctx);
406 bi_analyze_helper_terminate(ctx);
407
408 bi_foreach_block(ctx, block) {
409 /* Handle discards along control flow edges */
410 if (va_discard_before_block(block))
411 bi_flow(ctx, bi_before_block(block), VA_FLOW_DISCARD);
412
413 bi_foreach_instr_in_block_safe(block, I) {
414 switch (I->op) {
415 /* Signal barriers immediately */
416 case BI_OPCODE_BARRIER:
417 bi_flow(ctx, bi_after_instr(I), VA_FLOW_WAIT);
418 break;
419
420 /* Insert waits for tilebuffer and depth/stencil instructions. These
421 * only happen in regular fragment shaders, as the required waits are
422 * assumed to already have happened in blend shaders.
423 *
424 * For discarded thread handling, ATEST must be serialized against all
425 * other asynchronous instructions and should be serialized against all
426 * instructions. Wait for slot 0 immediately after the ATEST.
427 */
428 case BI_OPCODE_BLEND:
429 case BI_OPCODE_ST_TILE:
430 if (!ctx->inputs->is_blend)
431 bi_flow(ctx, bi_before_instr(I), VA_FLOW_WAIT);
432 break;
433
434 case BI_OPCODE_LD_TILE: {
435 if (ctx->inputs->is_blend)
436 break;
437
438 assert(!I->wait_resource || pan_arch(ctx->inputs->gpu_id) >= 10);
439 bi_flow(ctx, bi_before_instr(I),
440 I->wait_resource ? VA_FLOW_WAIT_RESOURCE : VA_FLOW_WAIT);
441 break;
442 }
443
444 case BI_OPCODE_ATEST:
445 bi_flow(ctx, bi_before_instr(I), VA_FLOW_WAIT0126);
446 bi_flow(ctx, bi_after_instr(I), VA_FLOW_WAIT0);
447 break;
448 case BI_OPCODE_ZS_EMIT:
449 if (!ctx->inputs->is_blend)
450 bi_flow(ctx, bi_before_instr(I), VA_FLOW_WAIT0126);
451 break;
452
453 default:
454 break;
455 }
456
457 if (I->flow && I->op != BI_OPCODE_NOP) {
458 /* Wait on the results of asynchronous instructions
459 *
460 * Bitmap of general slots lines up with the encoding of va_flow for
461 * waits on general slots. The dataflow analysis should be ignoring
462 * the special slots #6 and #7, which are handled separately.
463 */
464 assert((I->flow & ~BITFIELD_MASK(VA_NUM_GENERAL_SLOTS)) == 0);
465
466 bi_flow(ctx, bi_before_instr(I), I->flow);
467 I->flow = 0;
468 }
469 }
470
471 /* Terminate helpers after the last use */
472 if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->inputs->is_blend &&
473 block->pass_flags && bi_block_terminates_helpers(block)) {
474
475 bi_foreach_instr_in_block_safe_rev(block, I) {
476 if (bi_instr_uses_helpers(I)) {
477 bi_flow(ctx, bi_after_instr(I), VA_FLOW_DISCARD);
478 break;
479 }
480 }
481 }
482
483 /* End exeuction at the end of the block if needed, or reconverge if we
484 * continue but we don't need to end execution.
485 */
486 if (va_should_end(block) || block->needs_nop) {
487 /* Don't bother adding a NOP into an unreachable block */
488 if (block == bi_start_block(&ctx->blocks) ||
489 bi_num_predecessors(block))
490 bi_flow(ctx, bi_after_block(block), VA_FLOW_END);
491 } else if (bi_reconverge_branches(block)) {
492 /* TODO: Do we have ever need to reconverge from an empty block? */
493 if (!list_is_empty(&block->instructions))
494 bi_flow(ctx, bi_after_block(block), VA_FLOW_RECONVERGE);
495 }
496 }
497
498 /* If helpers are not used anywhere, they are not used at the start, so we
499 * terminate at the start. Else, helpers are used somewhere in the shader and
500 * are terminated after last use.
501 */
502 bi_block *start = bi_start_block(&ctx->blocks);
503 bool frag = (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->inputs->is_blend);
504
505 if (frag && !start->pass_flags)
506 bi_flow(ctx, bi_before_block(start), VA_FLOW_DISCARD);
507 }
508
509 /*
510 * Assign slots to all asynchronous instructions. A few special instructions
511 * require specific slots. For the rest, we assign slots in a round-robin
512 * fashion to reduce false dependencies when encoding waits.
513 *
514 * This should be called before va_insert_flow_control_nops.
515 */
516 void
517 va_assign_slots(bi_context *ctx)
518 {
519 unsigned counter = 0;
520
521 bi_foreach_instr_global(ctx, I) {
522 if (I->op == BI_OPCODE_BARRIER) {
523 I->slot = 7;
524 } else if (I->op == BI_OPCODE_ZS_EMIT || I->op == BI_OPCODE_ATEST) {
525 I->slot = 0;
526 } else if (bi_get_opcode_props(I)->message) {
527 I->slot = counter++;
528
529 if (counter == 3)
530 counter = 0;
531 }
532 }
533 }