Debian Codegraph @

src/asahi/compiler/agx_insert_waits.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "agx_builder.h"
7 #include "agx_compiler.h"
8 #include "agx_debug.h"
9
10 #define AGX_MAX_PENDING (8)
11
12 /*
13 * Returns whether an instruction is asynchronous and needs a scoreboard slot
14 */
15 static bool
16 instr_is_async(agx_instr *I)
17 {
18 return agx_opcodes_info[I->op].immediates & AGX_IMMEDIATE_SCOREBOARD;
19 }
20
21 struct slot {
22 /* Set of registers this slot is currently writing */
23 BITSET_DECLARE(writes, AGX_NUM_REGS);
24
25 /* Number of pending messages on this slot. Must not exceed
26 * AGX_MAX_PENDING for correct results.
27 */
28 uint8_t nr_pending;
29 };
30
31 /*
32 * Insert waits within a block to stall after every async instruction. Useful
33 * for debugging.
34 */
35 static void
36 agx_insert_waits_trivial(agx_context *ctx, agx_block *block)
37 {
38 agx_foreach_instr_in_block_safe(block, I) {
39 if (instr_is_async(I)) {
40 agx_builder b = agx_init_builder(ctx, agx_after_instr(I));
41 agx_wait(&b, I->scoreboard);
42 }
43 }
44 }
45
46 /*
47 * Insert waits within a block, assuming scoreboard slots have already been
48 * assigned. This waits for everything at the end of the block, rather than
49 * doing something more intelligent/global. This should be optimized.
50 *
51 * XXX: Do any instructions read their sources asynchronously?
52 */
53 static void
54 agx_insert_waits_local(agx_context *ctx, agx_block *block)
55 {
56 struct slot slots[2] = {0};
57
58 agx_foreach_instr_in_block_safe(block, I) {
59 uint8_t wait_mask = 0;
60
61 /* Check for read-after-write */
62 agx_foreach_src(I, s) {
63 if (I->src[s].type != AGX_INDEX_REGISTER)
64 continue;
65
66 unsigned nr_read = agx_index_size_16(I->src[s]);
67 for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
68 if (BITSET_TEST_COUNT(slots[slot].writes, I->src[s].value, nr_read))
69 wait_mask |= BITSET_BIT(slot);
70 }
71 }
72
73 /* Check for write-after-write */
74 agx_foreach_dest(I, d) {
75 if (I->dest[d].type != AGX_INDEX_REGISTER)
76 continue;
77
78 unsigned nr_writes = agx_index_size_16(I->dest[d]);
79 for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
80 if (BITSET_TEST_COUNT(slots[slot].writes, I->dest[d].value,
81 nr_writes))
82 wait_mask |= BITSET_BIT(slot);
83 }
84 }
85
86 /* Check for barriers */
87 if (I->op == AGX_OPCODE_THREADGROUP_BARRIER ||
88 I->op == AGX_OPCODE_MEMORY_BARRIER) {
89
90 for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
91 if (slots[slot].nr_pending)
92 wait_mask |= BITSET_BIT(slot);
93 }
94 }
95
96 /* Try to assign a free slot */
97 if (instr_is_async(I)) {
98 for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
99 if (slots[slot].nr_pending == 0) {
100 I->scoreboard = slot;
101 break;
102 }
103 }
104 }
105
106 /* Check for slot overflow */
107 if (instr_is_async(I) &&
108 slots[I->scoreboard].nr_pending >= AGX_MAX_PENDING)
109 wait_mask |= BITSET_BIT(I->scoreboard);
110
111 /* Insert the appropriate waits, clearing the slots */
112 u_foreach_bit(slot, wait_mask) {
113 agx_builder b = agx_init_builder(ctx, agx_before_instr(I));
114 agx_wait(&b, slot);
115
116 BITSET_ZERO(slots[slot].writes);
117 slots[slot].nr_pending = 0;
118 }
119
120 /* Record access */
121 if (instr_is_async(I)) {
122 agx_foreach_dest(I, d) {
123 if (agx_is_null(I->dest[d]))
124 continue;
125
126 assert(I->dest[d].type == AGX_INDEX_REGISTER);
127 BITSET_SET_COUNT(slots[I->scoreboard].writes, I->dest[d].value,
128 agx_index_size_16(I->dest[d]));
129 }
130
131 slots[I->scoreboard].nr_pending++;
132 }
133 }
134
135 /* If there are outstanding messages, wait for them. We don't do this for the
136 * exit block, though, since nothing else will execute in the shader so
137 * waiting is pointless.
138 */
139 if (block != agx_exit_block(ctx)) {
140 agx_builder b = agx_init_builder(ctx, agx_after_block_logical(block));
141
142 for (unsigned slot = 0; slot < ARRAY_SIZE(slots); ++slot) {
143 if (slots[slot].nr_pending)
144 agx_wait(&b, slot);
145 }
146 }
147 }
148
149 /*
150 * Assign scoreboard slots to asynchronous instructions and insert waits for the
151 * appropriate hazard tracking.
152 */
153 void
154 agx_insert_waits(agx_context *ctx)
155 {
156 agx_foreach_block(ctx, block) {
157 if (agx_compiler_debug & AGX_DBG_WAIT)
158 agx_insert_waits_trivial(ctx, block);
159 else
160 agx_insert_waits_local(ctx, block);
161 }
162 }