Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2022 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "bi_builder.h"
7 #include "va_compiler.h"
8 #include "valhall_enums.h"
9
10 /*
11 * Merge NOPs with flow control with nearby instructions to eliminate the NOPs,
12 * according to the following rules:
13 *
14 * 1. Waits may be combined by waiting on a union of the slots.
15 * 2. Waits may be moved up as far as the first (last) asynchronous instruction
16 * writing to a slot waited on, at a performance cost.
17 * 3. Discard may be moved down, at a performance cost.
18 * 4. Reconverge must be on the last instruction of the block.
19 * 5. End must be on the last instruction of the block.
20 *
21 * For simplicity, this pass only merges within a single basic block. This
22 * should be sufficient. The algorithm works as follows.
23 *
24 * Because reconverge/end must be on the last instruction, there may be only
25 * one of these in a block. First check for a NOP at the end, and if it has
26 * reconverge/end flow control, merge with the penultimate instruction. Now we
27 * only have to worry about waits and discard.
28 *
29 * The cost of moving a wait up is assumed to be greater than the cost of moving
30 * a discard down, so we next move waits while we have more freedom. For each
31 * wait, merge with an instruction above that either is free or has another
32 * wait, and merge the flow control, aborting if we hit a message signaling the
33 * relevant slot. By maintaining the candidate instruction at all steps, this is
34 * implemented in a linear time walk.
35 *
36 * Finally, all that's left are discards. Move discards down to merge with a
37 * free instruction with a similar linear time walk.
38 *
39 * Since discarding helper threads is an optimization (not required for
40 * correctness), we may remove discards if we believe them harmful to the
41 * performance of the shader. Keeping with the local structure of the pass, we
42 * remove discards only if they are at the end of the last block and cannot be
43 * merged with any other instruction. Such a condition means every following
44 * instruction has incompatible flow control (wait and end). In practice, this
45 * means BLEND and ATEST run for helper threads in certain shaders to save a
46 * NOP, but BLEND is a no-op for helper threads anyway.
47 *
48 * One case we don't handle is merging "foo, bar, wait, reconverge" to
49 * "foo.wait, bar.reconverge". This sequence is rarely generated by the dataflow
50 * analysis, so we don't care for the complexity.
51 */
52
53 /*
54 * If the last instruction has reconverge or end, try to merge with the
55 * second to last instruction.
56 *
57 * Precondition: block has at least 2 instructions.
58 */
59 static void
60 merge_end_reconverge(bi_block *block)
61 {
62 bi_instr *last = list_last_entry(&block->instructions, bi_instr, link);
63 bi_instr *penult = bi_prev_op(last);
64
65 if (last->op != BI_OPCODE_NOP)
66 return;
67 if (last->flow != VA_FLOW_RECONVERGE && last->flow != VA_FLOW_END)
68 return;
69
70 /* End implies all other flow control except for waiting on barriers (slot
71 * #7, with VA_FLOW_WAIT), so remove blocking flow control.
72 */
73 if (last->flow == VA_FLOW_END) {
74 while (penult->op == BI_OPCODE_NOP && penult->flow != VA_FLOW_WAIT) {
75 bi_remove_instruction(penult);
76
77 /* There may be nothing left */
78 if (list_is_singular(&block->instructions))
79 return;
80
81 penult = bi_prev_op(last);
82 }
83 }
84
85 /* If there is blocking flow control, we can't merge */
86 if (penult->flow != VA_FLOW_NONE)
87 return;
88
89 /* Else, merge */
90 penult->flow = last->flow;
91 bi_remove_instruction(last);
92 }
93
94 /*
95 * Calculate the union of two waits. We may wait on any combination of slots #0,
96 * #1, #2 or the entirety of 0126 and 01267. If we wait on the entirety, the
97 * union is trivial. If we do not wait on slot #6 (by extension slot #7), we
98 * wait only on slots #0, #1, and #2, for which the waits are encoded as a
99 * bitset and the union is just a bitwise OR.
100 */
101 static enum va_flow
102 union_waits(enum va_flow x, enum va_flow y)
103 {
104 assert(va_flow_is_wait_or_none(x) && va_flow_is_wait_or_none(y));
105
106 if ((x == VA_FLOW_WAIT) || (y == VA_FLOW_WAIT))
107 return VA_FLOW_WAIT;
108 else if ((x == VA_FLOW_WAIT0126) || (y == VA_FLOW_WAIT0126))
109 return VA_FLOW_WAIT0126;
110 else
111 return x | y;
112 }
113
114 static void
115 merge_waits(bi_block *block)
116 {
117 /* Most recent instruction with which we can merge, or NULL if none */
118 bi_instr *last_free = NULL;
119
120 bi_foreach_instr_in_block_safe(block, I) {
121 if (last_free != NULL && I->op == BI_OPCODE_NOP &&
122 va_flow_is_wait_or_none(I->flow)) {
123
124 /* Merge waits with compatible instructions */
125 last_free->flow = union_waits(last_free->flow, I->flow);
126 bi_remove_instruction(I);
127 continue;
128 }
129
130 /* Don't move waits past async instructions, since they might be what
131 * we're waiting for. If we wanted to optimize this case, we could check
132 * the signaled slots.
133 */
134 if (bi_get_opcode_props(I)->message)
135 last_free = NULL;
136
137 /* We can only merge with instructions whose flow control is a wait.
138 * This includes such an instruction after merging in a wait. It also
139 * includes async instructions.
140 */
141 if (va_flow_is_wait_or_none(I->flow))
142 last_free = I;
143 }
144 }
145
146 static void
147 merge_resource_waits(bi_block *block)
148 {
149 /* Most recent instruction with which we can merge, or NULL if none */
150 bi_instr *last_free = NULL;
151
152 bi_foreach_instr_in_block_safe(block, I) {
153 if (last_free != NULL && I->op == BI_OPCODE_NOP &&
154 I->flow == VA_FLOW_WAIT_RESOURCE) {
155
156 /* Merge resource_waits with compatible instructions */
157 last_free->flow = VA_FLOW_WAIT_RESOURCE;
158 bi_remove_instruction(I);
159 continue;
160 }
161
162 /* Don't move waits past async instructions, since they might be what
163 * we're waiting for. If we wanted to optimize this case, we could check
164 * the signaled slots.
165 */
166 if (bi_get_opcode_props(I)->message)
167 last_free = NULL;
168
169 /* We can only merge with instructions whose flow control is a none. */
170 if (I->flow == VA_FLOW_NONE)
171 last_free = I;
172 }
173 }
174
175 static bool
176 bi_is_first_instr(bi_block *block, bi_instr *I)
177 {
178 return block->instructions.next == &I->link;
179 }
180
181 static void
182 merge_discard(bi_block *block)
183 {
184 bi_instr *last_free = NULL;
185
186 bi_foreach_instr_in_block_safe_rev(block, I) {
187 if ((I->op == BI_OPCODE_NOP) && (I->flow == VA_FLOW_DISCARD)) {
188 /* Try to merge with the instruction *preceding* discard, because
189 * because flow control happens at the end of an instruction and
190 * discard is a NOP.
191 */
192 if (!bi_is_first_instr(block, I)) {
193 bi_instr *prev = bi_prev_op(I);
194
195 if (prev->flow == VA_FLOW_NONE) {
196 prev->flow = VA_FLOW_DISCARD;
197 bi_remove_instruction(I);
198 continue;
199 }
200 }
201
202 /* Or try to merge with the next instruction with no flow control */
203 if (last_free != NULL) {
204 last_free->flow = VA_FLOW_DISCARD;
205 bi_remove_instruction(I);
206 continue;
207 }
208
209 /* If there's nowhere to merge and this is the end of the shader, just
210 * remove the discard.
211 */
212 if (bi_num_successors(block) == 0) {
213 bi_remove_instruction(I);
214 continue;
215 }
216 }
217
218 /* Allow merging into free instructions */
219 if (I->flow == VA_FLOW_NONE)
220 last_free = I;
221 }
222 }
223
224 void
225 va_merge_flow(bi_context *ctx)
226 {
227 bi_foreach_block(ctx, block) {
228 /* If there are less than 2 instructions, there's nothing to merge */
229 if (list_is_empty(&block->instructions))
230 continue;
231 if (list_is_singular(&block->instructions))
232 continue;
233
234 merge_end_reconverge(block);
235 merge_resource_waits(block);
236 merge_waits(block);
237
238 if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->inputs->is_blend)
239 merge_discard(block);
240 }
241 }