Debian Codegraph @

src/panfrost/compiler/midgard/midgard_helper_invocations.c

raw
1 /*
2 * Copyright (C) 2019 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "compiler.h"
7
8 /* Midgard texture/derivative operations have a pair of bits controlling the
9 * behaviour of helper invocations:
10 *
11 * - Should a helper invocation terminate after executing this instruction?
12 * - Should a helper invocation actually execute this instruction?
13 *
14 * The terminate bit should be set on the last instruction requiring helper
15 * invocations. Without control flow, that's literally the last instruction;
16 * with control flow, there may be multiple such instructions (with ifs) or no
17 * such instruction (with loops).
18 *
19 * The execute bit should be set if the value of this instruction is required
20 * by a future instruction requiring helper invocations. Consider:
21 *
22 * 0 = texture ...
23 * 1 = fmul 0, #10
24 * 2 = dfdx 1
25 * store 2
26 *
27 * Since the derivative calculation 2 requires helper invocations, the value 1
28 * must be calculated by helper invocations, and since it depends on 0, 0 must
29 * be calculated by helpers. Hence the texture op has the execute bit set, and
30 * the derivative op has the terminate bit set.
31 *
32 * Calculating the terminate bit occurs by forward dataflow analysis to
33 * determine which blocks require helper invocations. A block requires
34 * invocations in if any of its instructions use helper invocations, or if it
35 * depends on a block that requires invocation. With that analysis, the
36 * terminate bit is set on the last instruction using invocations within any
37 * block that does *not* require invocations out.
38 *
39 * Likewise, calculating the execute bit requires backward dataflow analysis
40 * with union as the join operation and the generating set being the union of
41 * sources of instructions writing executed values.
42 */
43
44 /* Does a block use helpers directly */
45 static bool
46 mir_block_uses_helpers(mesa_shader_stage stage, midgard_block *block)
47 {
48 mir_foreach_instr_in_block(block, ins) {
49 if (ins->type != TAG_TEXTURE_4)
50 continue;
51 if (mir_op_computes_derivatives(stage, ins->op))
52 return true;
53 }
54
55 return false;
56 }
57
58 static bool
59 mir_block_terminates_helpers(midgard_block *block)
60 {
61 /* Can't terminate if there are no helpers */
62 if (!block->helpers_in)
63 return false;
64
65 /* Can't terminate if a successor needs helpers */
66 mir_foreach_successor(block, succ) {
67 if (succ->helpers_in)
68 return false;
69 }
70
71 /* Otherwise we terminate */
72 return true;
73 }
74
75 void
76 mir_analyze_helper_terminate(compiler_context *ctx)
77 {
78 /* Set blocks as directly requiring helpers, and if they do add them to
79 * the worklist to propagate to their predecessors */
80
81 struct set *worklist =
82 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
83
84 struct set *visited =
85 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
86
87 mir_foreach_block(ctx, _block) {
88 midgard_block *block = (midgard_block *)_block;
89 block->helpers_in |= mir_block_uses_helpers(ctx->stage, block);
90
91 if (block->helpers_in)
92 _mesa_set_add(worklist, _block);
93 }
94
95 /* Next, propagate back. Since there are a finite number of blocks, the
96 * worklist (a subset of all the blocks) is finite. Since a block can
97 * only be added to the worklist if it is not on the visited list and
98 * the visited list - also a subset of the blocks - grows every
99 * iteration, the algorithm must terminate. */
100
101 struct set_entry *cur;
102
103 while ((cur = _mesa_set_next_entry(worklist, NULL)) != NULL) {
104 /* Pop off a block requiring helpers */
105 midgard_block *blk = (struct midgard_block *)cur->key;
106 _mesa_set_remove(worklist, cur);
107
108 /* Its predecessors also require helpers */
109 mir_foreach_predecessor(blk, pred) {
110 if (!_mesa_set_search(visited, pred)) {
111 pred->helpers_in = true;
112 _mesa_set_add(worklist, pred);
113 }
114 }
115
116 _mesa_set_add(visited, blk);
117 }
118
119 _mesa_set_destroy(visited, NULL);
120 _mesa_set_destroy(worklist, NULL);
121
122 /* Finally, set helper_terminate on the last derivative-calculating
123 * instruction in a block that terminates helpers */
124 mir_foreach_block(ctx, _block) {
125 midgard_block *block = (midgard_block *)_block;
126
127 if (!mir_block_terminates_helpers(block))
128 continue;
129
130 mir_foreach_instr_in_block_rev(block, ins) {
131 if (ins->type != TAG_TEXTURE_4)
132 continue;
133 if (!mir_op_computes_derivatives(ctx->stage, ins->op))
134 continue;
135
136 ins->helper_terminate = true;
137 break;
138 }
139 }
140 }
141
142 static bool
143 mir_helper_block_update(BITSET_WORD *deps, midgard_block *block,
144 unsigned temp_count)
145 {
146 bool progress = false;
147
148 mir_foreach_instr_in_block_rev(block, ins) {
149 /* Ensure we write to a helper dependency */
150 if (ins->dest >= temp_count || !BITSET_TEST(deps, ins->dest))
151 continue;
152
153 /* Then add all of our dependencies */
154 mir_foreach_src(ins, s) {
155 if (ins->src[s] >= temp_count)
156 continue;
157
158 /* Progress if the dependency set changes */
159 progress |= !BITSET_TEST(deps, ins->src[s]);
160 BITSET_SET(deps, ins->src[s]);
161 }
162 }
163
164 return progress;
165 }
166
167 void
168 mir_analyze_helper_requirements(compiler_context *ctx)
169 {
170 mir_compute_temp_count(ctx);
171 unsigned temp_count = ctx->temp_count;
172 BITSET_WORD *deps = BITSET_CALLOC(temp_count);
173
174 /* Initialize with the sources of instructions consuming
175 * derivatives */
176
177 mir_foreach_instr_global(ctx, ins) {
178 if (ins->type != TAG_TEXTURE_4)
179 continue;
180 if (ins->dest >= ctx->temp_count)
181 continue;
182 if (!mir_op_computes_derivatives(ctx->stage, ins->op))
183 continue;
184
185 mir_foreach_src(ins, s) {
186 if (ins->src[s] < temp_count)
187 BITSET_SET(deps, ins->src[s]);
188 }
189 }
190
191 /* Propagate that up */
192
193 struct set *work_list =
194 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
195
196 struct set *visited =
197 _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
198
199 struct set_entry *cur =
200 _mesa_set_add(work_list, mir_exit_block(&ctx->blocks));
201
202 do {
203 midgard_block *blk = (struct midgard_block *)cur->key;
204 _mesa_set_remove(work_list, cur);
205
206 bool progress = mir_helper_block_update(deps, blk, temp_count);
207
208 if (progress || !_mesa_set_search(visited, blk)) {
209 mir_foreach_predecessor(blk, pred)
210 _mesa_set_add(work_list, pred);
211 }
212
213 _mesa_set_add(visited, blk);
214 } while ((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
215
216 _mesa_set_destroy(visited, NULL);
217 _mesa_set_destroy(work_list, NULL);
218
219 /* Set the execute bits */
220
221 mir_foreach_instr_global(ctx, ins) {
222 if (ins->type != TAG_TEXTURE_4)
223 continue;
224 if (ins->dest >= ctx->temp_count)
225 continue;
226
227 ins->helper_execute = BITSET_TEST(deps, ins->dest);
228 }
229
230 free(deps);
231 }