aco: ensure predecessors' p_logical_end is in WQM when a p_phi is in WQM
[mesa.git] / src / amd / compiler / aco_insert_exec_mask.cpp
1 /*
2 * Copyright © 2019 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include "aco_ir.h"
26 #include "aco_builder.h"
27 #include "util/u_math.h"
28
29 namespace aco {
30
31 namespace {
32
33 enum WQMState : uint8_t {
34 Unspecified = 0,
35 Exact = 1 << 0,
36 WQM = 1 << 1, /* with control flow applied */
37 Preserve_WQM = 1 << 2,
38 Exact_Branch = 1 << 3,
39 };
40
41 enum mask_type : uint8_t {
42 mask_type_global = 1 << 0,
43 mask_type_exact = 1 << 1,
44 mask_type_wqm = 1 << 2,
45 mask_type_loop = 1 << 3, /* active lanes of a loop */
46 mask_type_initial = 1 << 4, /* initially active lanes */
47 };
48
49 struct wqm_ctx {
50 Program* program;
51 /* state for WQM propagation */
52 std::set<unsigned> worklist;
53 std::vector<uint16_t> defined_in;
54 std::vector<bool> needs_wqm;
55 std::vector<bool> branch_wqm; /* true if the branch condition in this block should be in wqm */
56 bool loop;
57 bool wqm;
58 wqm_ctx(Program* program) : program(program),
59 defined_in(program->peekAllocationId(), 0xFFFF),
60 needs_wqm(program->peekAllocationId()),
61 branch_wqm(program->blocks.size()),
62 loop(false),
63 wqm(false)
64 {
65 for (unsigned i = 0; i < program->blocks.size(); i++)
66 worklist.insert(i);
67 }
68 };
69
70 struct loop_info {
71 Block* loop_header;
72 uint16_t num_exec_masks;
73 uint8_t needs;
74 bool has_divergent_break;
75 bool has_divergent_continue;
76 bool has_discard; /* has a discard or demote */
77 loop_info(Block* b, uint16_t num, uint8_t needs, bool breaks, bool cont, bool discard) :
78 loop_header(b), num_exec_masks(num), needs(needs), has_divergent_break(breaks),
79 has_divergent_continue(cont), has_discard(discard) {}
80 };
81
82 struct block_info {
83 std::vector<std::pair<Temp, uint8_t>> exec;
84 std::vector<WQMState> instr_needs;
85 uint8_t block_needs;
86 uint8_t ever_again_needs;
87 bool logical_end_wqm;
88 /* more... */
89 };
90
91 struct exec_ctx {
92 Program *program;
93 std::vector<block_info> info;
94 std::vector<loop_info> loop;
95 bool handle_wqm = false;
96 exec_ctx(Program *program) : program(program), info(program->blocks.size()) {}
97 };
98
99 bool pred_by_exec_mask(aco_ptr<Instruction>& instr) {
100 if (instr->isSALU())
101 return instr->reads_exec();
102 if (instr->format == Format::SMEM || instr->isSALU())
103 return false;
104 if (instr->format == Format::PSEUDO_BARRIER)
105 return false;
106
107 if (instr->format == Format::PSEUDO) {
108 switch (instr->opcode) {
109 case aco_opcode::p_create_vector:
110 return instr->definitions[0].getTemp().type() == RegType::vgpr;
111 case aco_opcode::p_extract_vector:
112 case aco_opcode::p_split_vector:
113 return instr->operands[0].getTemp().type() == RegType::vgpr;
114 case aco_opcode::p_spill:
115 case aco_opcode::p_reload:
116 return false;
117 default:
118 break;
119 }
120 }
121
122 if (instr->opcode == aco_opcode::v_readlane_b32 ||
123 instr->opcode == aco_opcode::v_readlane_b32_e64 ||
124 instr->opcode == aco_opcode::v_writelane_b32 ||
125 instr->opcode == aco_opcode::v_writelane_b32_e64)
126 return false;
127
128 return true;
129 }
130
131 bool needs_exact(aco_ptr<Instruction>& instr) {
132 if (instr->format == Format::MUBUF) {
133 MUBUF_instruction *mubuf = static_cast<MUBUF_instruction *>(instr.get());
134 return mubuf->disable_wqm;
135 } else if (instr->format == Format::MTBUF) {
136 MTBUF_instruction *mtbuf = static_cast<MTBUF_instruction *>(instr.get());
137 return mtbuf->disable_wqm;
138 } else if (instr->format == Format::MIMG) {
139 MIMG_instruction *mimg = static_cast<MIMG_instruction *>(instr.get());
140 return mimg->disable_wqm;
141 } else if (instr->format == Format::FLAT || instr->format == Format::GLOBAL) {
142 FLAT_instruction *flat = static_cast<FLAT_instruction *>(instr.get());
143 return flat->disable_wqm;
144 } else {
145 return instr->format == Format::EXP || instr->opcode == aco_opcode::p_fs_buffer_store_smem;
146 }
147 }
148
149 void set_needs_wqm(wqm_ctx &ctx, Temp tmp)
150 {
151 if (!ctx.needs_wqm[tmp.id()]) {
152 ctx.needs_wqm[tmp.id()] = true;
153 if (ctx.defined_in[tmp.id()] != 0xFFFF)
154 ctx.worklist.insert(ctx.defined_in[tmp.id()]);
155 }
156 }
157
158 void mark_block_wqm(wqm_ctx &ctx, unsigned block_idx)
159 {
160 if (ctx.branch_wqm[block_idx])
161 return;
162
163 ctx.branch_wqm[block_idx] = true;
164 Block& block = ctx.program->blocks[block_idx];
165 aco_ptr<Instruction>& branch = block.instructions.back();
166
167 if (branch->opcode != aco_opcode::p_branch) {
168 assert(!branch->operands.empty() && branch->operands[0].isTemp());
169 set_needs_wqm(ctx, branch->operands[0].getTemp());
170 }
171
172 /* TODO: this sets more branch conditions to WQM than it needs to
173 * it should be enough to stop at the "exec mask top level" */
174 if (block.kind & block_kind_top_level)
175 return;
176
177 for (unsigned pred_idx : block.logical_preds)
178 mark_block_wqm(ctx, pred_idx);
179 }
180
181 void get_block_needs(wqm_ctx &ctx, exec_ctx &exec_ctx, Block* block)
182 {
183 block_info& info = exec_ctx.info[block->index];
184
185 std::vector<WQMState> instr_needs(block->instructions.size());
186
187 if (block->kind & block_kind_top_level) {
188 if (ctx.loop && ctx.wqm) {
189 /* mark all break conditions as WQM */
190 unsigned block_idx = block->index + 1;
191 while (!(ctx.program->blocks[block_idx].kind & block_kind_top_level)) {
192 if (ctx.program->blocks[block_idx].kind & block_kind_break)
193 mark_block_wqm(ctx, block_idx);
194 block_idx++;
195 }
196 } else if (ctx.loop && !ctx.wqm) {
197 /* Ensure a branch never results in an exec mask with only helper
198 * invocations (which can cause a loop to repeat infinitively if it's
199 * break branches are done in exact). */
200 unsigned block_idx = block->index;
201 do {
202 if ((ctx.program->blocks[block_idx].kind & block_kind_branch))
203 exec_ctx.info[block_idx].block_needs |= Exact_Branch;
204 block_idx++;
205 } while (!(ctx.program->blocks[block_idx].kind & block_kind_top_level));
206 }
207
208 ctx.loop = false;
209 ctx.wqm = false;
210 }
211
212 for (int i = block->instructions.size() - 1; i >= 0; --i) {
213 aco_ptr<Instruction>& instr = block->instructions[i];
214
215 WQMState needs = needs_exact(instr) ? Exact : Unspecified;
216 bool propagate_wqm = instr->opcode == aco_opcode::p_wqm;
217 bool preserve_wqm = instr->opcode == aco_opcode::p_discard_if;
218 bool pred_by_exec = pred_by_exec_mask(instr);
219 for (const Definition& definition : instr->definitions) {
220 if (!definition.isTemp())
221 continue;
222 const unsigned def = definition.tempId();
223 ctx.defined_in[def] = block->index;
224 if (needs == Unspecified && ctx.needs_wqm[def]) {
225 needs = pred_by_exec ? WQM : Unspecified;
226 propagate_wqm = true;
227 }
228 }
229
230 if (propagate_wqm) {
231 for (const Operand& op : instr->operands) {
232 if (op.isTemp()) {
233 set_needs_wqm(ctx, op.getTemp());
234 }
235 }
236 } else if (preserve_wqm && info.block_needs & WQM) {
237 needs = Preserve_WQM;
238 }
239
240 /* ensure the condition controlling the control flow for this phi is in WQM */
241 if (needs == WQM && instr->opcode == aco_opcode::p_phi) {
242 for (unsigned pred_idx : block->logical_preds) {
243 mark_block_wqm(ctx, pred_idx);
244 exec_ctx.info[pred_idx].logical_end_wqm = true;
245 ctx.worklist.insert(pred_idx);
246 }
247 }
248
249 if (instr->opcode == aco_opcode::p_logical_end && info.logical_end_wqm) {
250 assert(needs == Unspecified);
251 needs = WQM;
252 }
253
254 instr_needs[i] = needs;
255 info.block_needs |= needs;
256 }
257
258 info.instr_needs = instr_needs;
259
260 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
261 * <cond> should be computed in WQM */
262 if (info.block_needs & WQM && !(block->kind & block_kind_top_level)) {
263 for (unsigned pred_idx : block->logical_preds)
264 mark_block_wqm(ctx, pred_idx);
265 ctx.wqm = true;
266 }
267 if (block->kind & block_kind_loop_header)
268 ctx.loop = true;
269 }
270
271 void calculate_wqm_needs(exec_ctx& exec_ctx)
272 {
273 wqm_ctx ctx(exec_ctx.program);
274
275 while (!ctx.worklist.empty()) {
276 unsigned block_index = *std::prev(ctx.worklist.end());
277 ctx.worklist.erase(std::prev(ctx.worklist.end()));
278
279 get_block_needs(ctx, exec_ctx, &exec_ctx.program->blocks[block_index]);
280 }
281
282 uint8_t ever_again_needs = 0;
283 for (int i = exec_ctx.program->blocks.size() - 1; i >= 0; i--) {
284 exec_ctx.info[i].ever_again_needs = ever_again_needs;
285 Block& block = exec_ctx.program->blocks[i];
286
287 if (block.kind & block_kind_needs_lowering)
288 exec_ctx.info[i].block_needs |= Exact;
289
290 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
291 if ((block.kind & block_kind_discard ||
292 block.kind & block_kind_uses_discard_if) &&
293 ever_again_needs & WQM)
294 exec_ctx.info[i].block_needs |= Preserve_WQM;
295
296 ever_again_needs |= exec_ctx.info[i].block_needs & ~Exact_Branch;
297 if (block.kind & block_kind_discard ||
298 block.kind & block_kind_uses_discard_if ||
299 block.kind & block_kind_uses_demote)
300 ever_again_needs |= Exact;
301
302 /* don't propagate WQM preservation further than the next top_level block */
303 if (block.kind & block_kind_top_level)
304 ever_again_needs &= ~Preserve_WQM;
305 else
306 exec_ctx.info[i].block_needs &= ~Preserve_WQM;
307 }
308 exec_ctx.handle_wqm = true;
309 }
310
311 void transition_to_WQM(exec_ctx& ctx, Builder bld, unsigned idx)
312 {
313 if (ctx.info[idx].exec.back().second & mask_type_wqm)
314 return;
315 if (ctx.info[idx].exec.back().second & mask_type_global) {
316 Temp exec_mask = ctx.info[idx].exec.back().first;
317 exec_mask = bld.sop1(Builder::s_wqm, bld.def(bld.lm, exec), bld.def(s1, scc), exec_mask);
318 ctx.info[idx].exec.emplace_back(exec_mask, mask_type_global | mask_type_wqm);
319 return;
320 }
321 /* otherwise, the WQM mask should be one below the current mask */
322 ctx.info[idx].exec.pop_back();
323 assert(ctx.info[idx].exec.back().second & mask_type_wqm);
324 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
325 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
326 ctx.info[idx].exec.back().first);
327 }
328
329 void transition_to_Exact(exec_ctx& ctx, Builder bld, unsigned idx)
330 {
331 if (ctx.info[idx].exec.back().second & mask_type_exact)
332 return;
333 /* We can't remove the loop exec mask, because that can cause exec.size() to
334 * be less than num_exec_masks. The loop exec mask also needs to be kept
335 * around for various uses. */
336 if ((ctx.info[idx].exec.back().second & mask_type_global) &&
337 !(ctx.info[idx].exec.back().second & mask_type_loop)) {
338 ctx.info[idx].exec.pop_back();
339 assert(ctx.info[idx].exec.back().second & mask_type_exact);
340 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
341 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
342 ctx.info[idx].exec.back().first);
343 return;
344 }
345 /* otherwise, we create an exact mask and push to the stack */
346 Temp wqm = ctx.info[idx].exec.back().first;
347 Temp exact = bld.tmp(bld.lm);
348 wqm = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
349 bld.exec(Definition(exact)), ctx.info[idx].exec[0].first, bld.exec(wqm));
350 ctx.info[idx].exec.back().first = wqm;
351 ctx.info[idx].exec.emplace_back(exact, mask_type_exact);
352 }
353
354 unsigned add_coupling_code(exec_ctx& ctx, Block* block,
355 std::vector<aco_ptr<Instruction>>& instructions)
356 {
357 unsigned idx = block->index;
358 Builder bld(ctx.program, &instructions);
359 std::vector<unsigned>& preds = block->linear_preds;
360
361 /* start block */
362 if (idx == 0) {
363 aco_ptr<Instruction>& startpgm = block->instructions[0];
364 assert(startpgm->opcode == aco_opcode::p_startpgm);
365 Temp exec_mask = startpgm->definitions.back().getTemp();
366 bld.insert(std::move(startpgm));
367
368 /* exec seems to need to be manually initialized with combined shaders */
369 if (util_bitcount(ctx.program->stage & sw_mask) > 1) {
370 bld.sop1(Builder::s_mov, bld.exec(Definition(exec_mask)), bld.lm == s2 ? Operand(UINT64_MAX) : Operand(UINT32_MAX));
371 instructions[0]->definitions.pop_back();
372 }
373
374 if (ctx.handle_wqm) {
375 ctx.info[0].exec.emplace_back(exec_mask, mask_type_global | mask_type_exact | mask_type_initial);
376 /* if this block only needs WQM, initialize already */
377 if (ctx.info[0].block_needs == WQM)
378 transition_to_WQM(ctx, bld, 0);
379 } else {
380 uint8_t mask = mask_type_global;
381 if (ctx.program->needs_wqm) {
382 exec_mask = bld.sop1(Builder::s_wqm, bld.def(bld.lm, exec), bld.def(s1, scc), bld.exec(exec_mask));
383 mask |= mask_type_wqm;
384 } else {
385 mask |= mask_type_exact;
386 }
387 ctx.info[0].exec.emplace_back(exec_mask, mask);
388 }
389
390 return 1;
391 }
392
393 /* loop entry block */
394 if (block->kind & block_kind_loop_header) {
395 assert(preds[0] == idx - 1);
396 ctx.info[idx].exec = ctx.info[idx - 1].exec;
397 loop_info& info = ctx.loop.back();
398 while (ctx.info[idx].exec.size() > info.num_exec_masks)
399 ctx.info[idx].exec.pop_back();
400
401 /* create ssa names for outer exec masks */
402 if (info.has_discard) {
403 aco_ptr<Pseudo_instruction> phi;
404 for (int i = 0; i < info.num_exec_masks - 1; i++) {
405 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1));
406 phi->definitions[0] = bld.def(bld.lm);
407 phi->operands[0] = Operand(ctx.info[preds[0]].exec[i].first);
408 ctx.info[idx].exec[i].first = bld.insert(std::move(phi));
409 }
410 }
411
412 /* create ssa name for restore mask */
413 if (info.has_divergent_break) {
414 /* this phi might be trivial but ensures a parallelcopy on the loop header */
415 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
416 phi->definitions[0] = bld.def(bld.lm);
417 phi->operands[0] = Operand(ctx.info[preds[0]].exec[info.num_exec_masks - 1].first);
418 ctx.info[idx].exec.back().first = bld.insert(std::move(phi));
419 }
420
421 /* create ssa name for loop active mask */
422 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
423 if (info.has_divergent_continue)
424 phi->definitions[0] = bld.def(bld.lm);
425 else
426 phi->definitions[0] = bld.def(bld.lm, exec);
427 phi->operands[0] = Operand(ctx.info[preds[0]].exec.back().first);
428 Temp loop_active = bld.insert(std::move(phi));
429
430 if (info.has_divergent_break) {
431 uint8_t mask_type = (ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact)) | mask_type_loop;
432 ctx.info[idx].exec.emplace_back(loop_active, mask_type);
433 } else {
434 ctx.info[idx].exec.back().first = loop_active;
435 ctx.info[idx].exec.back().second |= mask_type_loop;
436 }
437
438 /* create a parallelcopy to move the active mask to exec */
439 unsigned i = 0;
440 if (info.has_divergent_continue) {
441 while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {
442 bld.insert(std::move(block->instructions[i]));
443 i++;
444 }
445 uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
446 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
447 ctx.info[idx].exec.emplace_back(bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
448 ctx.info[idx].exec.back().first), mask_type);
449 }
450
451 return i;
452 }
453
454 /* loop exit block */
455 if (block->kind & block_kind_loop_exit) {
456 Block* header = ctx.loop.back().loop_header;
457 loop_info& info = ctx.loop.back();
458
459 for (ASSERTED unsigned pred : preds)
460 assert(ctx.info[pred].exec.size() >= info.num_exec_masks);
461
462 /* fill the loop header phis */
463 std::vector<unsigned>& header_preds = header->linear_preds;
464 int k = 0;
465 if (info.has_discard) {
466 while (k < info.num_exec_masks - 1) {
467 aco_ptr<Instruction>& phi = header->instructions[k];
468 assert(phi->opcode == aco_opcode::p_linear_phi);
469 for (unsigned i = 1; i < phi->operands.size(); i++)
470 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[k].first);
471 k++;
472 }
473 }
474 aco_ptr<Instruction>& phi = header->instructions[k++];
475 assert(phi->opcode == aco_opcode::p_linear_phi);
476 for (unsigned i = 1; i < phi->operands.size(); i++)
477 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[info.num_exec_masks - 1].first);
478
479 if (info.has_divergent_break) {
480 aco_ptr<Instruction>& phi = header->instructions[k];
481 assert(phi->opcode == aco_opcode::p_linear_phi);
482 for (unsigned i = 1; i < phi->operands.size(); i++)
483 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[info.num_exec_masks].first);
484 }
485
486 assert(!(block->kind & block_kind_top_level) || info.num_exec_masks <= 2);
487
488 /* create the loop exit phis if not trivial */
489 for (unsigned k = 0; k < info.num_exec_masks; k++) {
490 Temp same = ctx.info[preds[0]].exec[k].first;
491 uint8_t type = ctx.info[header_preds[0]].exec[k].second;
492 bool trivial = true;
493
494 for (unsigned i = 1; i < preds.size() && trivial; i++) {
495 if (ctx.info[preds[i]].exec[k].first != same)
496 trivial = false;
497 }
498
499 if (trivial) {
500 ctx.info[idx].exec.emplace_back(same, type);
501 } else {
502 /* create phi for loop footer */
503 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
504 phi->definitions[0] = bld.def(bld.lm);
505 for (unsigned i = 0; i < phi->operands.size(); i++)
506 phi->operands[i] = Operand(ctx.info[preds[i]].exec[k].first);
507 ctx.info[idx].exec.emplace_back(bld.insert(std::move(phi)), type);
508 }
509 }
510 assert(ctx.info[idx].exec.size() == info.num_exec_masks);
511
512 /* create a parallelcopy to move the live mask to exec */
513 unsigned i = 0;
514 while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {
515 bld.insert(std::move(block->instructions[i]));
516 i++;
517 }
518
519 if (ctx.handle_wqm) {
520 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {
521 if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||
522 (ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {
523 ctx.info[idx].exec.back().second |= mask_type_global;
524 transition_to_Exact(ctx, bld, idx);
525 ctx.handle_wqm = false;
526 }
527 }
528 if (ctx.info[idx].block_needs == WQM)
529 transition_to_WQM(ctx, bld, idx);
530 else if (ctx.info[idx].block_needs == Exact)
531 transition_to_Exact(ctx, bld, idx);
532 }
533
534 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
535 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
536 ctx.info[idx].exec.back().first);
537
538 ctx.loop.pop_back();
539 return i;
540 }
541
542 if (preds.size() == 1) {
543 ctx.info[idx].exec = ctx.info[preds[0]].exec;
544 } else {
545 assert(preds.size() == 2);
546 /* if one of the predecessors ends in exact mask, we pop it from stack */
547 unsigned num_exec_masks = std::min(ctx.info[preds[0]].exec.size(),
548 ctx.info[preds[1]].exec.size());
549 if (block->kind & block_kind_top_level && !(block->kind & block_kind_merge))
550 num_exec_masks = std::min(num_exec_masks, 2u);
551
552 /* create phis for diverged exec masks */
553 for (unsigned i = 0; i < num_exec_masks; i++) {
554 bool in_exec = i == num_exec_masks - 1 && !(block->kind & block_kind_merge);
555 if (!in_exec && ctx.info[preds[0]].exec[i].first == ctx.info[preds[1]].exec[i].first) {
556 assert(ctx.info[preds[0]].exec[i].second == ctx.info[preds[1]].exec[i].second);
557 ctx.info[idx].exec.emplace_back(ctx.info[preds[0]].exec[i]);
558 continue;
559 }
560
561 Temp phi = bld.pseudo(aco_opcode::p_linear_phi, in_exec ? bld.def(bld.lm, exec) : bld.def(bld.lm),
562 ctx.info[preds[0]].exec[i].first,
563 ctx.info[preds[1]].exec[i].first);
564 uint8_t mask_type = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;
565 ctx.info[idx].exec.emplace_back(phi, mask_type);
566 }
567 }
568
569 unsigned i = 0;
570 while (block->instructions[i]->opcode == aco_opcode::p_phi ||
571 block->instructions[i]->opcode == aco_opcode::p_linear_phi) {
572 bld.insert(std::move(block->instructions[i]));
573 i++;
574 }
575
576 if (block->kind & block_kind_merge)
577 ctx.info[idx].exec.pop_back();
578
579 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 3) {
580 assert(ctx.info[idx].exec.back().second == mask_type_exact);
581 assert(block->kind & block_kind_merge);
582 ctx.info[idx].exec.pop_back();
583 }
584
585 /* try to satisfy the block's needs */
586 if (ctx.handle_wqm) {
587 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {
588 if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||
589 (ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {
590 ctx.info[idx].exec.back().second |= mask_type_global;
591 transition_to_Exact(ctx, bld, idx);
592 ctx.handle_wqm = false;
593 }
594 }
595 if (ctx.info[idx].block_needs == WQM)
596 transition_to_WQM(ctx, bld, idx);
597 else if (ctx.info[idx].block_needs == Exact)
598 transition_to_Exact(ctx, bld, idx);
599 }
600
601 if (block->kind & block_kind_merge) {
602 Temp restore = ctx.info[idx].exec.back().first;
603 assert(restore.size() == bld.lm.size());
604 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec), restore);
605 }
606
607 return i;
608 }
609
610 void lower_fs_buffer_store_smem(Builder& bld, bool need_check, aco_ptr<Instruction>& instr, Temp cur_exec)
611 {
612 Operand offset = instr->operands[1];
613 if (need_check) {
614 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
615 Temp nonempty = bld.sopc(Builder::s_cmp_lg, bld.def(s1, scc), cur_exec, Operand(0u));
616
617 if (offset.isLiteral())
618 offset = bld.sop1(aco_opcode::s_mov_b32, bld.def(s1), offset);
619
620 offset = bld.sop2(aco_opcode::s_cselect_b32, bld.hint_m0(bld.def(s1)),
621 offset, Operand(UINT32_MAX), bld.scc(nonempty));
622 } else if (offset.isConstant() && offset.constantValue() > 0xFFFFF) {
623 offset = bld.sop1(aco_opcode::s_mov_b32, bld.hint_m0(bld.def(s1)), offset);
624 }
625 if (!offset.isConstant())
626 offset.setFixed(m0);
627
628 switch (instr->operands[2].size()) {
629 case 1:
630 instr->opcode = aco_opcode::s_buffer_store_dword;
631 break;
632 case 2:
633 instr->opcode = aco_opcode::s_buffer_store_dwordx2;
634 break;
635 case 4:
636 instr->opcode = aco_opcode::s_buffer_store_dwordx4;
637 break;
638 default:
639 unreachable("Invalid SMEM buffer store size");
640 }
641 instr->operands[1] = offset;
642 /* as_uniform() needs to be done here so it's done in exact mode and helper
643 * lanes don't contribute. */
644 instr->operands[2] = Operand(bld.as_uniform(instr->operands[2]));
645 }
646
647 void process_instructions(exec_ctx& ctx, Block* block,
648 std::vector<aco_ptr<Instruction>>& instructions,
649 unsigned idx)
650 {
651 WQMState state;
652 if (ctx.info[block->index].exec.back().second & mask_type_wqm)
653 state = WQM;
654 else {
655 assert(!ctx.handle_wqm || ctx.info[block->index].exec.back().second & mask_type_exact);
656 state = Exact;
657 }
658
659 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
660 bool process = (ctx.handle_wqm &&
661 (ctx.info[block->index].block_needs & state) !=
662 (ctx.info[block->index].block_needs & (WQM | Exact))) ||
663 block->kind & block_kind_uses_discard_if ||
664 block->kind & block_kind_uses_demote ||
665 block->kind & block_kind_needs_lowering;
666 if (!process) {
667 std::vector<aco_ptr<Instruction>>::iterator it = std::next(block->instructions.begin(), idx);
668 instructions.insert(instructions.end(),
669 std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(it),
670 std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(block->instructions.end()));
671 return;
672 }
673
674 Builder bld(ctx.program, &instructions);
675
676 for (; idx < block->instructions.size(); idx++) {
677 aco_ptr<Instruction> instr = std::move(block->instructions[idx]);
678
679 WQMState needs = ctx.handle_wqm ? ctx.info[block->index].instr_needs[idx] : Unspecified;
680
681 if (instr->opcode == aco_opcode::p_discard_if) {
682 if (ctx.info[block->index].block_needs & Preserve_WQM) {
683 assert(block->kind & block_kind_top_level);
684 transition_to_WQM(ctx, bld, block->index);
685 ctx.info[block->index].exec.back().second &= ~mask_type_global;
686 }
687 int num = ctx.info[block->index].exec.size();
688 assert(num);
689 Operand cond = instr->operands[0];
690 for (int i = num - 1; i >= 0; i--) {
691 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
692 ctx.info[block->index].exec[i].first, cond);
693 if (i == num - 1) {
694 andn2->operands[0].setFixed(exec);
695 andn2->definitions[0].setFixed(exec);
696 }
697 if (i == 0) {
698 instr->opcode = aco_opcode::p_exit_early_if;
699 instr->operands[0] = bld.scc(andn2->definitions[1].getTemp());
700 }
701 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
702 }
703 assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);
704
705 } else if (needs == WQM && state != WQM) {
706 transition_to_WQM(ctx, bld, block->index);
707 state = WQM;
708 } else if (needs == Exact && state != Exact) {
709 transition_to_Exact(ctx, bld, block->index);
710 state = Exact;
711 }
712
713 if (instr->opcode == aco_opcode::p_is_helper || instr->opcode == aco_opcode::p_load_helper) {
714 Definition dst = instr->definitions[0];
715 assert(dst.size() == bld.lm.size());
716 if (state == Exact) {
717 instr.reset(create_instruction<SOP1_instruction>(bld.w64or32(Builder::s_mov), Format::SOP1, 1, 1));
718 instr->operands[0] = Operand(0u);
719 instr->definitions[0] = dst;
720 } else {
721 std::pair<Temp, uint8_t>& exact_mask = ctx.info[block->index].exec[0];
722 if (instr->opcode == aco_opcode::p_load_helper &&
723 !(ctx.info[block->index].exec[0].second & mask_type_initial)) {
724 /* find last initial exact mask */
725 for (int i = block->index; i >= 0; i--) {
726 if (ctx.program->blocks[i].kind & block_kind_top_level &&
727 ctx.info[i].exec[0].second & mask_type_initial) {
728 exact_mask = ctx.info[i].exec[0];
729 break;
730 }
731 }
732 }
733
734 assert(instr->opcode == aco_opcode::p_is_helper || exact_mask.second & mask_type_initial);
735 assert(exact_mask.second & mask_type_exact);
736
737 instr.reset(create_instruction<SOP2_instruction>(bld.w64or32(Builder::s_andn2), Format::SOP2, 2, 2));
738 instr->operands[0] = Operand(ctx.info[block->index].exec.back().first); /* current exec */
739 instr->operands[1] = Operand(exact_mask.first);
740 instr->definitions[0] = dst;
741 instr->definitions[1] = bld.def(s1, scc);
742 }
743 } else if (instr->opcode == aco_opcode::p_demote_to_helper) {
744 /* turn demote into discard_if with only exact masks */
745 assert((ctx.info[block->index].exec[0].second & (mask_type_exact | mask_type_global)) == (mask_type_exact | mask_type_global));
746 ctx.info[block->index].exec[0].second &= ~mask_type_initial;
747
748 int num;
749 Temp cond, exit_cond;
750 if (instr->operands[0].isConstant()) {
751 assert(instr->operands[0].constantValue() == -1u);
752 /* transition to exact and set exec to zero */
753 Temp old_exec = ctx.info[block->index].exec.back().first;
754 Temp new_exec = bld.tmp(bld.lm);
755 exit_cond = bld.tmp(s1);
756 cond = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.scc(Definition(exit_cond)),
757 bld.exec(Definition(new_exec)), Operand(0u), bld.exec(old_exec));
758
759 num = ctx.info[block->index].exec.size() - 2;
760 if (ctx.info[block->index].exec.back().second & mask_type_exact) {
761 ctx.info[block->index].exec.back().first = new_exec;
762 } else {
763 ctx.info[block->index].exec.back().first = cond;
764 ctx.info[block->index].exec.emplace_back(new_exec, mask_type_exact);
765 }
766 } else {
767 /* demote_if: transition to exact */
768 transition_to_Exact(ctx, bld, block->index);
769 assert(instr->operands[0].isTemp());
770 cond = instr->operands[0].getTemp();
771 num = ctx.info[block->index].exec.size() - 1;
772 }
773
774 for (int i = num; i >= 0; i--) {
775 if (ctx.info[block->index].exec[i].second & mask_type_exact) {
776 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
777 ctx.info[block->index].exec[i].first, cond);
778 if (i == (int)ctx.info[block->index].exec.size() - 1) {
779 andn2->operands[0].setFixed(exec);
780 andn2->definitions[0].setFixed(exec);
781 }
782
783 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
784 exit_cond = andn2->definitions[1].getTemp();
785 } else {
786 assert(i != 0);
787 }
788 }
789 instr->opcode = aco_opcode::p_exit_early_if;
790 instr->operands[0] = bld.scc(exit_cond);
791 state = Exact;
792
793 } else if (instr->opcode == aco_opcode::p_fs_buffer_store_smem) {
794 bool need_check = ctx.info[block->index].exec.size() != 1 &&
795 !(ctx.info[block->index].exec[ctx.info[block->index].exec.size() - 2].second & Exact);
796 lower_fs_buffer_store_smem(bld, need_check, instr, ctx.info[block->index].exec.back().first);
797 }
798
799 bld.insert(std::move(instr));
800 }
801 }
802
803 void add_branch_code(exec_ctx& ctx, Block* block)
804 {
805 unsigned idx = block->index;
806 Builder bld(ctx.program, block);
807
808 if (idx == ctx.program->blocks.size() - 1)
809 return;
810
811 /* try to disable wqm handling */
812 if (ctx.handle_wqm && block->kind & block_kind_top_level) {
813 if (ctx.info[idx].exec.size() == 3) {
814 assert(ctx.info[idx].exec[1].second == mask_type_wqm);
815 ctx.info[idx].exec.pop_back();
816 }
817 assert(ctx.info[idx].exec.size() <= 2);
818
819 if (ctx.info[idx].ever_again_needs == 0 ||
820 ctx.info[idx].ever_again_needs == Exact) {
821 /* transition to Exact */
822 aco_ptr<Instruction> branch = std::move(block->instructions.back());
823 block->instructions.pop_back();
824 ctx.info[idx].exec.back().second |= mask_type_global;
825 transition_to_Exact(ctx, bld, idx);
826 bld.insert(std::move(branch));
827 ctx.handle_wqm = false;
828
829 } else if (ctx.info[idx].block_needs & Preserve_WQM) {
830 /* transition to WQM and remove global flag */
831 aco_ptr<Instruction> branch = std::move(block->instructions.back());
832 block->instructions.pop_back();
833 transition_to_WQM(ctx, bld, idx);
834 ctx.info[idx].exec.back().second &= ~mask_type_global;
835 bld.insert(std::move(branch));
836 }
837 }
838
839 if (block->kind & block_kind_loop_preheader) {
840 /* collect information about the succeeding loop */
841 bool has_divergent_break = false;
842 bool has_divergent_continue = false;
843 bool has_discard = false;
844 uint8_t needs = 0;
845 unsigned loop_nest_depth = ctx.program->blocks[idx + 1].loop_nest_depth;
846
847 for (unsigned i = idx + 1; ctx.program->blocks[i].loop_nest_depth >= loop_nest_depth; i++) {
848 Block& loop_block = ctx.program->blocks[i];
849 needs |= ctx.info[i].block_needs;
850
851 if (loop_block.kind & block_kind_uses_discard_if ||
852 loop_block.kind & block_kind_discard ||
853 loop_block.kind & block_kind_uses_demote)
854 has_discard = true;
855 if (loop_block.loop_nest_depth != loop_nest_depth)
856 continue;
857
858 if (loop_block.kind & block_kind_uniform)
859 continue;
860 else if (loop_block.kind & block_kind_break)
861 has_divergent_break = true;
862 else if (loop_block.kind & block_kind_continue)
863 has_divergent_continue = true;
864 }
865
866 if (ctx.handle_wqm) {
867 if (needs & WQM) {
868 aco_ptr<Instruction> branch = std::move(block->instructions.back());
869 block->instructions.pop_back();
870 transition_to_WQM(ctx, bld, idx);
871 bld.insert(std::move(branch));
872 } else {
873 aco_ptr<Instruction> branch = std::move(block->instructions.back());
874 block->instructions.pop_back();
875 transition_to_Exact(ctx, bld, idx);
876 bld.insert(std::move(branch));
877 }
878 }
879
880 unsigned num_exec_masks = ctx.info[idx].exec.size();
881 if (block->kind & block_kind_top_level)
882 num_exec_masks = std::min(num_exec_masks, 2u);
883
884 ctx.loop.emplace_back(&ctx.program->blocks[block->linear_succs[0]],
885 num_exec_masks,
886 needs,
887 has_divergent_break,
888 has_divergent_continue,
889 has_discard);
890 }
891
892 if (block->kind & block_kind_discard) {
893
894 assert(block->instructions.back()->format == Format::PSEUDO_BRANCH);
895 aco_ptr<Instruction> branch = std::move(block->instructions.back());
896 block->instructions.pop_back();
897
898 /* create a discard_if() instruction with the exec mask as condition */
899 unsigned num = 0;
900 if (ctx.loop.size()) {
901 /* if we're in a loop, only discard from the outer exec masks */
902 num = ctx.loop.back().num_exec_masks;
903 } else {
904 num = ctx.info[idx].exec.size() - 1;
905 }
906
907 Temp old_exec = ctx.info[idx].exec.back().first;
908 Temp new_exec = bld.tmp(bld.lm);
909 Temp cond = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
910 bld.exec(Definition(new_exec)), Operand(0u), bld.exec(old_exec));
911 ctx.info[idx].exec.back().first = new_exec;
912
913 for (int i = num - 1; i >= 0; i--) {
914 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
915 ctx.info[block->index].exec[i].first, cond);
916 if (i == (int)ctx.info[idx].exec.size() - 1)
917 andn2->definitions[0].setFixed(exec);
918 if (i == 0)
919 bld.pseudo(aco_opcode::p_exit_early_if, bld.scc(andn2->definitions[1].getTemp()));
920 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
921 }
922 assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);
923
924 if ((block->kind & (block_kind_break | block_kind_uniform)) == block_kind_break)
925 ctx.info[idx].exec.back().first = cond;
926 bld.insert(std::move(branch));
927 /* no return here as it can be followed by a divergent break */
928 }
929
930 if (block->kind & block_kind_continue_or_break) {
931 assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[1]].linear_succs[0]].kind & block_kind_loop_header);
932 assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[0]].linear_succs[0]].kind & block_kind_loop_exit);
933 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
934 block->instructions.pop_back();
935
936 while (!(ctx.info[idx].exec.back().second & mask_type_loop))
937 ctx.info[idx].exec.pop_back();
938
939 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec), ctx.info[idx].exec.back().first);
940 bld.branch(aco_opcode::p_cbranch_nz, bld.exec(ctx.info[idx].exec.back().first), block->linear_succs[1], block->linear_succs[0]);
941 return;
942 }
943
944 if (block->kind & block_kind_uniform) {
945 Pseudo_branch_instruction* branch = static_cast<Pseudo_branch_instruction*>(block->instructions.back().get());
946 if (branch->opcode == aco_opcode::p_branch) {
947 branch->target[0] = block->linear_succs[0];
948 } else {
949 branch->target[0] = block->linear_succs[1];
950 branch->target[1] = block->linear_succs[0];
951 }
952 return;
953 }
954
955 if (block->kind & block_kind_branch) {
956
957 if (ctx.handle_wqm &&
958 ctx.info[idx].exec.size() >= 2 &&
959 ctx.info[idx].exec.back().second == mask_type_exact &&
960 !(ctx.info[idx].block_needs & Exact_Branch) &&
961 ctx.info[idx].exec[ctx.info[idx].exec.size() - 2].second & mask_type_wqm) {
962 /* return to wqm before branching */
963 ctx.info[idx].exec.pop_back();
964 }
965
966 // orig = s_and_saveexec_b64
967 assert(block->linear_succs.size() == 2);
968 assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_z);
969 Temp cond = block->instructions.back()->operands[0].getTemp();
970 block->instructions.pop_back();
971
972 if (ctx.info[idx].block_needs & Exact_Branch)
973 transition_to_Exact(ctx, bld, idx);
974
975 Temp current_exec = ctx.info[idx].exec.back().first;
976 uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
977
978 Temp then_mask = bld.tmp(bld.lm);
979 Temp old_exec = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
980 bld.exec(Definition(then_mask)), cond, bld.exec(current_exec));
981
982 ctx.info[idx].exec.back().first = old_exec;
983
984 /* add next current exec to the stack */
985 ctx.info[idx].exec.emplace_back(then_mask, mask_type);
986
987 bld.branch(aco_opcode::p_cbranch_z, bld.exec(then_mask), block->linear_succs[1], block->linear_succs[0]);
988 return;
989 }
990
991 if (block->kind & block_kind_invert) {
992 // exec = s_andn2_b64 (original_exec, exec)
993 assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_nz);
994 block->instructions.pop_back();
995 Temp then_mask = ctx.info[idx].exec.back().first;
996 uint8_t mask_type = ctx.info[idx].exec.back().second;
997 ctx.info[idx].exec.pop_back();
998 Temp orig_exec = ctx.info[idx].exec.back().first;
999 Temp else_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm, exec),
1000 bld.def(s1, scc), orig_exec, bld.exec(then_mask));
1001
1002 /* add next current exec to the stack */
1003 ctx.info[idx].exec.emplace_back(else_mask, mask_type);
1004
1005 bld.branch(aco_opcode::p_cbranch_z, bld.exec(else_mask), block->linear_succs[1], block->linear_succs[0]);
1006 return;
1007 }
1008
1009 if (block->kind & block_kind_break) {
1010 // loop_mask = s_andn2_b64 (loop_mask, exec)
1011 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
1012 block->instructions.pop_back();
1013
1014 Temp current_exec = ctx.info[idx].exec.back().first;
1015 Temp cond = Temp();
1016 for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
1017 cond = bld.tmp(s1);
1018 Temp exec_mask = ctx.info[idx].exec[exec_idx].first;
1019 exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
1020 exec_mask, current_exec);
1021 ctx.info[idx].exec[exec_idx].first = exec_mask;
1022 if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
1023 break;
1024 }
1025
1026 /* check if the successor is the merge block, otherwise set exec to 0 */
1027 // TODO: this could be done better by directly branching to the merge block
1028 unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
1029 Block& succ = ctx.program->blocks[succ_idx];
1030 if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
1031 ctx.info[idx].exec.back().first = bld.sop1(Builder::s_mov, bld.def(bld.lm, exec), Operand(0u));
1032 }
1033
1034 bld.branch(aco_opcode::p_cbranch_nz, bld.scc(cond), block->linear_succs[1], block->linear_succs[0]);
1035 return;
1036 }
1037
1038 if (block->kind & block_kind_continue) {
1039 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
1040 block->instructions.pop_back();
1041
1042 Temp current_exec = ctx.info[idx].exec.back().first;
1043 Temp cond = Temp();
1044 for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
1045 if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
1046 break;
1047 cond = bld.tmp(s1);
1048 Temp exec_mask = ctx.info[idx].exec[exec_idx].first;
1049 exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
1050 exec_mask, bld.exec(current_exec));
1051 ctx.info[idx].exec[exec_idx].first = exec_mask;
1052 }
1053 assert(cond != Temp());
1054
1055 /* check if the successor is the merge block, otherwise set exec to 0 */
1056 // TODO: this could be done better by directly branching to the merge block
1057 unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
1058 Block& succ = ctx.program->blocks[succ_idx];
1059 if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
1060 ctx.info[idx].exec.back().first = bld.sop1(Builder::s_mov, bld.def(bld.lm, exec), Operand(0u));
1061 }
1062
1063 bld.branch(aco_opcode::p_cbranch_nz, bld.scc(cond), block->linear_succs[1], block->linear_succs[0]);
1064 return;
1065 }
1066 }
1067
1068 void process_block(exec_ctx& ctx, Block* block)
1069 {
1070 std::vector<aco_ptr<Instruction>> instructions;
1071 instructions.reserve(block->instructions.size());
1072
1073 unsigned idx = add_coupling_code(ctx, block, instructions);
1074
1075 assert(block->index != ctx.program->blocks.size() - 1 ||
1076 ctx.info[block->index].exec.size() <= 2);
1077
1078 process_instructions(ctx, block, instructions, idx);
1079
1080 block->instructions = std::move(instructions);
1081
1082 add_branch_code(ctx, block);
1083
1084 block->live_out_exec = ctx.info[block->index].exec.back().first;
1085 }
1086
1087 } /* end namespace */
1088
1089
1090 void insert_exec_mask(Program *program)
1091 {
1092 exec_ctx ctx(program);
1093
1094 if (program->needs_wqm && program->needs_exact)
1095 calculate_wqm_needs(ctx);
1096
1097 for (Block& block : program->blocks)
1098 process_block(ctx, &block);
1099
1100 }
1101
1102 }
1103