44da289339b4bd1bad08359bf1e0afa857fc6ebe
[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 instr->opcode == aco_opcode::p_wqm) {
251 assert(needs != Exact);
252 needs = WQM;
253 }
254
255 instr_needs[i] = needs;
256 info.block_needs |= needs;
257 }
258
259 info.instr_needs = instr_needs;
260
261 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
262 * <cond> should be computed in WQM */
263 if (info.block_needs & WQM && !(block->kind & block_kind_top_level)) {
264 for (unsigned pred_idx : block->logical_preds)
265 mark_block_wqm(ctx, pred_idx);
266 ctx.wqm = true;
267 }
268 if (block->kind & block_kind_loop_header)
269 ctx.loop = true;
270 }
271
272 void calculate_wqm_needs(exec_ctx& exec_ctx)
273 {
274 wqm_ctx ctx(exec_ctx.program);
275
276 while (!ctx.worklist.empty()) {
277 unsigned block_index = *std::prev(ctx.worklist.end());
278 ctx.worklist.erase(std::prev(ctx.worklist.end()));
279
280 get_block_needs(ctx, exec_ctx, &exec_ctx.program->blocks[block_index]);
281 }
282
283 uint8_t ever_again_needs = 0;
284 for (int i = exec_ctx.program->blocks.size() - 1; i >= 0; i--) {
285 exec_ctx.info[i].ever_again_needs = ever_again_needs;
286 Block& block = exec_ctx.program->blocks[i];
287
288 if (block.kind & block_kind_needs_lowering)
289 exec_ctx.info[i].block_needs |= Exact;
290
291 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
292 if ((block.kind & block_kind_discard ||
293 block.kind & block_kind_uses_discard_if) &&
294 ever_again_needs & WQM)
295 exec_ctx.info[i].block_needs |= Preserve_WQM;
296
297 ever_again_needs |= exec_ctx.info[i].block_needs & ~Exact_Branch;
298 if (block.kind & block_kind_discard ||
299 block.kind & block_kind_uses_discard_if ||
300 block.kind & block_kind_uses_demote)
301 ever_again_needs |= Exact;
302
303 /* don't propagate WQM preservation further than the next top_level block */
304 if (block.kind & block_kind_top_level)
305 ever_again_needs &= ~Preserve_WQM;
306 else
307 exec_ctx.info[i].block_needs &= ~Preserve_WQM;
308 }
309 exec_ctx.handle_wqm = true;
310 }
311
312 void transition_to_WQM(exec_ctx& ctx, Builder bld, unsigned idx)
313 {
314 if (ctx.info[idx].exec.back().second & mask_type_wqm)
315 return;
316 if (ctx.info[idx].exec.back().second & mask_type_global) {
317 Temp exec_mask = ctx.info[idx].exec.back().first;
318 /* TODO: we might generate better code if we pass the uncopied "exec_mask"
319 * directly to the s_wqm (we still need to keep this parallelcopy for
320 * potential later uses of exec_mask though). We currently can't do this
321 * because of a RA bug. */
322 exec_mask = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm), bld.exec(exec_mask));
323 ctx.info[idx].exec.back().first = exec_mask;
324
325 exec_mask = bld.sop1(Builder::s_wqm, bld.def(bld.lm, exec), bld.def(s1, scc), exec_mask);
326 ctx.info[idx].exec.emplace_back(exec_mask, mask_type_global | mask_type_wqm);
327 return;
328 }
329 /* otherwise, the WQM mask should be one below the current mask */
330 ctx.info[idx].exec.pop_back();
331 assert(ctx.info[idx].exec.back().second & mask_type_wqm);
332 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
333 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
334 ctx.info[idx].exec.back().first);
335 }
336
337 void transition_to_Exact(exec_ctx& ctx, Builder bld, unsigned idx)
338 {
339 if (ctx.info[idx].exec.back().second & mask_type_exact)
340 return;
341 /* We can't remove the loop exec mask, because that can cause exec.size() to
342 * be less than num_exec_masks. The loop exec mask also needs to be kept
343 * around for various uses. */
344 if ((ctx.info[idx].exec.back().second & mask_type_global) &&
345 !(ctx.info[idx].exec.back().second & mask_type_loop)) {
346 ctx.info[idx].exec.pop_back();
347 assert(ctx.info[idx].exec.back().second & mask_type_exact);
348 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
349 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
350 ctx.info[idx].exec.back().first);
351 return;
352 }
353 /* otherwise, we create an exact mask and push to the stack */
354 Temp wqm = ctx.info[idx].exec.back().first;
355 Temp exact = bld.tmp(bld.lm);
356 wqm = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
357 bld.exec(Definition(exact)), ctx.info[idx].exec[0].first, bld.exec(wqm));
358 ctx.info[idx].exec.back().first = wqm;
359 ctx.info[idx].exec.emplace_back(exact, mask_type_exact);
360 }
361
362 unsigned add_coupling_code(exec_ctx& ctx, Block* block,
363 std::vector<aco_ptr<Instruction>>& instructions)
364 {
365 unsigned idx = block->index;
366 Builder bld(ctx.program, &instructions);
367 std::vector<unsigned>& preds = block->linear_preds;
368
369 /* start block */
370 if (idx == 0) {
371 aco_ptr<Instruction>& startpgm = block->instructions[0];
372 assert(startpgm->opcode == aco_opcode::p_startpgm);
373 Temp exec_mask = startpgm->definitions.back().getTemp();
374 bld.insert(std::move(startpgm));
375
376 /* exec seems to need to be manually initialized with combined shaders */
377 if (util_bitcount(ctx.program->stage & sw_mask) > 1) {
378 bld.sop1(Builder::s_mov, bld.exec(Definition(exec_mask)), bld.lm == s2 ? Operand(UINT64_MAX) : Operand(UINT32_MAX));
379 instructions[0]->definitions.pop_back();
380 }
381
382 if (ctx.handle_wqm) {
383 ctx.info[0].exec.emplace_back(exec_mask, mask_type_global | mask_type_exact | mask_type_initial);
384 /* if this block only needs WQM, initialize already */
385 if (ctx.info[0].block_needs == WQM)
386 transition_to_WQM(ctx, bld, 0);
387 } else {
388 uint8_t mask = mask_type_global;
389 if (ctx.program->needs_wqm) {
390 exec_mask = bld.sop1(Builder::s_wqm, bld.def(bld.lm, exec), bld.def(s1, scc), bld.exec(exec_mask));
391 mask |= mask_type_wqm;
392 } else {
393 mask |= mask_type_exact;
394 }
395 ctx.info[0].exec.emplace_back(exec_mask, mask);
396 }
397
398 return 1;
399 }
400
401 /* loop entry block */
402 if (block->kind & block_kind_loop_header) {
403 assert(preds[0] == idx - 1);
404 ctx.info[idx].exec = ctx.info[idx - 1].exec;
405 loop_info& info = ctx.loop.back();
406 while (ctx.info[idx].exec.size() > info.num_exec_masks)
407 ctx.info[idx].exec.pop_back();
408
409 /* create ssa names for outer exec masks */
410 if (info.has_discard) {
411 aco_ptr<Pseudo_instruction> phi;
412 for (int i = 0; i < info.num_exec_masks - 1; i++) {
413 phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1));
414 phi->definitions[0] = bld.def(bld.lm);
415 phi->operands[0] = Operand(ctx.info[preds[0]].exec[i].first);
416 ctx.info[idx].exec[i].first = bld.insert(std::move(phi));
417 }
418 }
419
420 /* create ssa name for restore mask */
421 if (info.has_divergent_break) {
422 /* this phi might be trivial but ensures a parallelcopy on the loop header */
423 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
424 phi->definitions[0] = bld.def(bld.lm);
425 phi->operands[0] = Operand(ctx.info[preds[0]].exec[info.num_exec_masks - 1].first);
426 ctx.info[idx].exec.back().first = bld.insert(std::move(phi));
427 }
428
429 /* create ssa name for loop active mask */
430 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
431 if (info.has_divergent_continue)
432 phi->definitions[0] = bld.def(bld.lm);
433 else
434 phi->definitions[0] = bld.def(bld.lm, exec);
435 phi->operands[0] = Operand(ctx.info[preds[0]].exec.back().first);
436 Temp loop_active = bld.insert(std::move(phi));
437
438 if (info.has_divergent_break) {
439 uint8_t mask_type = (ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact)) | mask_type_loop;
440 ctx.info[idx].exec.emplace_back(loop_active, mask_type);
441 } else {
442 ctx.info[idx].exec.back().first = loop_active;
443 ctx.info[idx].exec.back().second |= mask_type_loop;
444 }
445
446 /* create a parallelcopy to move the active mask to exec */
447 unsigned i = 0;
448 if (info.has_divergent_continue) {
449 while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {
450 bld.insert(std::move(block->instructions[i]));
451 i++;
452 }
453 uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
454 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
455 ctx.info[idx].exec.emplace_back(bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
456 ctx.info[idx].exec.back().first), mask_type);
457 }
458
459 return i;
460 }
461
462 /* loop exit block */
463 if (block->kind & block_kind_loop_exit) {
464 Block* header = ctx.loop.back().loop_header;
465 loop_info& info = ctx.loop.back();
466
467 for (ASSERTED unsigned pred : preds)
468 assert(ctx.info[pred].exec.size() >= info.num_exec_masks);
469
470 /* fill the loop header phis */
471 std::vector<unsigned>& header_preds = header->linear_preds;
472 int k = 0;
473 if (info.has_discard) {
474 while (k < info.num_exec_masks - 1) {
475 aco_ptr<Instruction>& phi = header->instructions[k];
476 assert(phi->opcode == aco_opcode::p_linear_phi);
477 for (unsigned i = 1; i < phi->operands.size(); i++)
478 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[k].first);
479 k++;
480 }
481 }
482 aco_ptr<Instruction>& phi = header->instructions[k++];
483 assert(phi->opcode == aco_opcode::p_linear_phi);
484 for (unsigned i = 1; i < phi->operands.size(); i++)
485 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[info.num_exec_masks - 1].first);
486
487 if (info.has_divergent_break) {
488 aco_ptr<Instruction>& phi = header->instructions[k];
489 assert(phi->opcode == aco_opcode::p_linear_phi);
490 for (unsigned i = 1; i < phi->operands.size(); i++)
491 phi->operands[i] = Operand(ctx.info[header_preds[i]].exec[info.num_exec_masks].first);
492 }
493
494 assert(!(block->kind & block_kind_top_level) || info.num_exec_masks <= 2);
495
496 /* create the loop exit phis if not trivial */
497 bool need_parallelcopy = false;
498 for (unsigned k = 0; k < info.num_exec_masks; k++) {
499 Temp same = ctx.info[preds[0]].exec[k].first;
500 uint8_t type = ctx.info[header_preds[0]].exec[k].second;
501 bool trivial = true;
502
503 for (unsigned i = 1; i < preds.size() && trivial; i++) {
504 if (ctx.info[preds[i]].exec[k].first != same)
505 trivial = false;
506 }
507
508 if (k == info.num_exec_masks - 1u) {
509 bool all_liveout_exec = true;
510 bool all_not_liveout_exec = true;
511 for (unsigned pred : preds) {
512 all_liveout_exec = all_liveout_exec && same == ctx.program->blocks[pred].live_out_exec;
513 all_not_liveout_exec = all_not_liveout_exec && same != ctx.program->blocks[pred].live_out_exec;
514 }
515 if (!all_liveout_exec && !all_not_liveout_exec)
516 trivial = false;
517 else if (all_not_liveout_exec)
518 need_parallelcopy = true;
519
520 need_parallelcopy |= !trivial;
521 }
522
523 if (trivial) {
524 ctx.info[idx].exec.emplace_back(same, type);
525 } else {
526 /* create phi for loop footer */
527 aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
528 phi->definitions[0] = bld.def(bld.lm);
529 if (k == info.num_exec_masks - 1) {
530 phi->definitions[0].setFixed(exec);
531 need_parallelcopy = false;
532 }
533 for (unsigned i = 0; i < phi->operands.size(); i++)
534 phi->operands[i] = Operand(ctx.info[preds[i]].exec[k].first);
535 ctx.info[idx].exec.emplace_back(bld.insert(std::move(phi)), type);
536 }
537 }
538 assert(ctx.info[idx].exec.size() == info.num_exec_masks);
539
540 /* create a parallelcopy to move the live mask to exec */
541 unsigned i = 0;
542 while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {
543 bld.insert(std::move(block->instructions[i]));
544 i++;
545 }
546
547 if (ctx.handle_wqm) {
548 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {
549 if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||
550 (ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {
551 ctx.info[idx].exec.back().second |= mask_type_global;
552 transition_to_Exact(ctx, bld, idx);
553 ctx.handle_wqm = false;
554 }
555 }
556 if (ctx.info[idx].block_needs == WQM)
557 transition_to_WQM(ctx, bld, idx);
558 else if (ctx.info[idx].block_needs == Exact)
559 transition_to_Exact(ctx, bld, idx);
560 }
561
562 assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
563 if (need_parallelcopy) {
564 /* only create this parallelcopy is needed, since the operand isn't
565 * fixed to exec which causes the spiller to miscalculate register demand */
566 /* TODO: Fix register_demand calculation for spilling on loop exits.
567 * The problem is only mitigated because the register demand could be
568 * higher if the exec phi doesn't get assigned to exec. */
569 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec),
570 ctx.info[idx].exec.back().first);
571 }
572
573 ctx.loop.pop_back();
574 return i;
575 }
576
577 if (preds.size() == 1) {
578 ctx.info[idx].exec = ctx.info[preds[0]].exec;
579 } else {
580 assert(preds.size() == 2);
581 /* if one of the predecessors ends in exact mask, we pop it from stack */
582 unsigned num_exec_masks = std::min(ctx.info[preds[0]].exec.size(),
583 ctx.info[preds[1]].exec.size());
584 if (block->kind & block_kind_top_level && !(block->kind & block_kind_merge))
585 num_exec_masks = std::min(num_exec_masks, 2u);
586
587 /* create phis for diverged exec masks */
588 for (unsigned i = 0; i < num_exec_masks; i++) {
589 bool in_exec = i == num_exec_masks - 1 && !(block->kind & block_kind_merge);
590 if (!in_exec && ctx.info[preds[0]].exec[i].first == ctx.info[preds[1]].exec[i].first) {
591 assert(ctx.info[preds[0]].exec[i].second == ctx.info[preds[1]].exec[i].second);
592 ctx.info[idx].exec.emplace_back(ctx.info[preds[0]].exec[i]);
593 continue;
594 }
595
596 Temp phi = bld.pseudo(aco_opcode::p_linear_phi, in_exec ? bld.def(bld.lm, exec) : bld.def(bld.lm),
597 ctx.info[preds[0]].exec[i].first,
598 ctx.info[preds[1]].exec[i].first);
599 uint8_t mask_type = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;
600 ctx.info[idx].exec.emplace_back(phi, mask_type);
601 }
602 }
603
604 unsigned i = 0;
605 while (block->instructions[i]->opcode == aco_opcode::p_phi ||
606 block->instructions[i]->opcode == aco_opcode::p_linear_phi) {
607 bld.insert(std::move(block->instructions[i]));
608 i++;
609 }
610
611 if (block->kind & block_kind_merge)
612 ctx.info[idx].exec.pop_back();
613
614 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 3) {
615 assert(ctx.info[idx].exec.back().second == mask_type_exact);
616 assert(block->kind & block_kind_merge);
617 ctx.info[idx].exec.pop_back();
618 }
619
620 /* try to satisfy the block's needs */
621 if (ctx.handle_wqm) {
622 if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {
623 if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||
624 (ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {
625 ctx.info[idx].exec.back().second |= mask_type_global;
626 transition_to_Exact(ctx, bld, idx);
627 ctx.handle_wqm = false;
628 }
629 }
630 if (ctx.info[idx].block_needs == WQM)
631 transition_to_WQM(ctx, bld, idx);
632 else if (ctx.info[idx].block_needs == Exact)
633 transition_to_Exact(ctx, bld, idx);
634 }
635
636 if (block->kind & block_kind_merge) {
637 Temp restore = ctx.info[idx].exec.back().first;
638 assert(restore.size() == bld.lm.size());
639 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec), restore);
640 }
641
642 return i;
643 }
644
645 void lower_fs_buffer_store_smem(Builder& bld, bool need_check, aco_ptr<Instruction>& instr, Temp cur_exec)
646 {
647 Operand offset = instr->operands[1];
648 if (need_check) {
649 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
650 Temp nonempty = bld.sopc(Builder::s_cmp_lg, bld.def(s1, scc), cur_exec, Operand(0u));
651
652 if (offset.isLiteral())
653 offset = bld.sop1(aco_opcode::s_mov_b32, bld.def(s1), offset);
654
655 offset = bld.sop2(aco_opcode::s_cselect_b32, bld.hint_m0(bld.def(s1)),
656 offset, Operand(UINT32_MAX), bld.scc(nonempty));
657 } else if (offset.isConstant() && offset.constantValue() > 0xFFFFF) {
658 offset = bld.sop1(aco_opcode::s_mov_b32, bld.hint_m0(bld.def(s1)), offset);
659 }
660 if (!offset.isConstant())
661 offset.setFixed(m0);
662
663 switch (instr->operands[2].size()) {
664 case 1:
665 instr->opcode = aco_opcode::s_buffer_store_dword;
666 break;
667 case 2:
668 instr->opcode = aco_opcode::s_buffer_store_dwordx2;
669 break;
670 case 4:
671 instr->opcode = aco_opcode::s_buffer_store_dwordx4;
672 break;
673 default:
674 unreachable("Invalid SMEM buffer store size");
675 }
676 instr->operands[1] = offset;
677 /* as_uniform() needs to be done here so it's done in exact mode and helper
678 * lanes don't contribute. */
679 instr->operands[2] = Operand(bld.as_uniform(instr->operands[2]));
680 }
681
682 void process_instructions(exec_ctx& ctx, Block* block,
683 std::vector<aco_ptr<Instruction>>& instructions,
684 unsigned idx)
685 {
686 WQMState state;
687 if (ctx.info[block->index].exec.back().second & mask_type_wqm)
688 state = WQM;
689 else {
690 assert(!ctx.handle_wqm || ctx.info[block->index].exec.back().second & mask_type_exact);
691 state = Exact;
692 }
693
694 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
695 bool process = (ctx.handle_wqm &&
696 (ctx.info[block->index].block_needs & state) !=
697 (ctx.info[block->index].block_needs & (WQM | Exact))) ||
698 block->kind & block_kind_uses_discard_if ||
699 block->kind & block_kind_uses_demote ||
700 block->kind & block_kind_needs_lowering;
701 if (!process) {
702 std::vector<aco_ptr<Instruction>>::iterator it = std::next(block->instructions.begin(), idx);
703 instructions.insert(instructions.end(),
704 std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(it),
705 std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(block->instructions.end()));
706 return;
707 }
708
709 Builder bld(ctx.program, &instructions);
710
711 for (; idx < block->instructions.size(); idx++) {
712 aco_ptr<Instruction> instr = std::move(block->instructions[idx]);
713
714 WQMState needs = ctx.handle_wqm ? ctx.info[block->index].instr_needs[idx] : Unspecified;
715
716 if (instr->opcode == aco_opcode::p_discard_if) {
717 if (ctx.info[block->index].block_needs & Preserve_WQM) {
718 assert(block->kind & block_kind_top_level);
719 transition_to_WQM(ctx, bld, block->index);
720 ctx.info[block->index].exec.back().second &= ~mask_type_global;
721 }
722 int num = ctx.info[block->index].exec.size();
723 assert(num);
724 Operand cond = instr->operands[0];
725 for (int i = num - 1; i >= 0; i--) {
726 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
727 ctx.info[block->index].exec[i].first, cond);
728 if (i == num - 1) {
729 andn2->operands[0].setFixed(exec);
730 andn2->definitions[0].setFixed(exec);
731 }
732 if (i == 0) {
733 instr->opcode = aco_opcode::p_exit_early_if;
734 instr->operands[0] = bld.scc(andn2->definitions[1].getTemp());
735 }
736 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
737 }
738 assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);
739
740 } else if (needs == WQM && state != WQM) {
741 transition_to_WQM(ctx, bld, block->index);
742 state = WQM;
743 } else if (needs == Exact && state != Exact) {
744 transition_to_Exact(ctx, bld, block->index);
745 state = Exact;
746 }
747
748 if (instr->opcode == aco_opcode::p_is_helper || instr->opcode == aco_opcode::p_load_helper) {
749 Definition dst = instr->definitions[0];
750 assert(dst.size() == bld.lm.size());
751 if (state == Exact) {
752 instr.reset(create_instruction<SOP1_instruction>(bld.w64or32(Builder::s_mov), Format::SOP1, 1, 1));
753 instr->operands[0] = Operand(0u);
754 instr->definitions[0] = dst;
755 } else {
756 std::pair<Temp, uint8_t>& exact_mask = ctx.info[block->index].exec[0];
757 if (instr->opcode == aco_opcode::p_load_helper &&
758 !(ctx.info[block->index].exec[0].second & mask_type_initial)) {
759 /* find last initial exact mask */
760 for (int i = block->index; i >= 0; i--) {
761 if (ctx.program->blocks[i].kind & block_kind_top_level &&
762 ctx.info[i].exec[0].second & mask_type_initial) {
763 exact_mask = ctx.info[i].exec[0];
764 break;
765 }
766 }
767 }
768
769 assert(instr->opcode == aco_opcode::p_is_helper || exact_mask.second & mask_type_initial);
770 assert(exact_mask.second & mask_type_exact);
771
772 instr.reset(create_instruction<SOP2_instruction>(bld.w64or32(Builder::s_andn2), Format::SOP2, 2, 2));
773 instr->operands[0] = Operand(ctx.info[block->index].exec.back().first); /* current exec */
774 instr->operands[1] = Operand(exact_mask.first);
775 instr->definitions[0] = dst;
776 instr->definitions[1] = bld.def(s1, scc);
777 }
778 } else if (instr->opcode == aco_opcode::p_demote_to_helper) {
779 /* turn demote into discard_if with only exact masks */
780 assert((ctx.info[block->index].exec[0].second & (mask_type_exact | mask_type_global)) == (mask_type_exact | mask_type_global));
781 ctx.info[block->index].exec[0].second &= ~mask_type_initial;
782
783 int num;
784 Temp cond, exit_cond;
785 if (instr->operands[0].isConstant()) {
786 assert(instr->operands[0].constantValue() == -1u);
787 /* transition to exact and set exec to zero */
788 Temp old_exec = ctx.info[block->index].exec.back().first;
789 Temp new_exec = bld.tmp(bld.lm);
790 exit_cond = bld.tmp(s1);
791 cond = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.scc(Definition(exit_cond)),
792 bld.exec(Definition(new_exec)), Operand(0u), bld.exec(old_exec));
793
794 num = ctx.info[block->index].exec.size() - 2;
795 if (ctx.info[block->index].exec.back().second & mask_type_exact) {
796 ctx.info[block->index].exec.back().first = new_exec;
797 } else {
798 ctx.info[block->index].exec.back().first = cond;
799 ctx.info[block->index].exec.emplace_back(new_exec, mask_type_exact);
800 }
801 } else {
802 /* demote_if: transition to exact */
803 transition_to_Exact(ctx, bld, block->index);
804 assert(instr->operands[0].isTemp());
805 cond = instr->operands[0].getTemp();
806 num = ctx.info[block->index].exec.size() - 1;
807 }
808
809 for (int i = num; i >= 0; i--) {
810 if (ctx.info[block->index].exec[i].second & mask_type_exact) {
811 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
812 ctx.info[block->index].exec[i].first, cond);
813 if (i == (int)ctx.info[block->index].exec.size() - 1) {
814 andn2->operands[0].setFixed(exec);
815 andn2->definitions[0].setFixed(exec);
816 }
817
818 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
819 exit_cond = andn2->definitions[1].getTemp();
820 } else {
821 assert(i != 0);
822 }
823 }
824 instr->opcode = aco_opcode::p_exit_early_if;
825 instr->operands[0] = bld.scc(exit_cond);
826 state = Exact;
827
828 } else if (instr->opcode == aco_opcode::p_fs_buffer_store_smem) {
829 bool need_check = ctx.info[block->index].exec.size() != 1 &&
830 !(ctx.info[block->index].exec[ctx.info[block->index].exec.size() - 2].second & Exact);
831 lower_fs_buffer_store_smem(bld, need_check, instr, ctx.info[block->index].exec.back().first);
832 }
833
834 bld.insert(std::move(instr));
835 }
836 }
837
838 void add_branch_code(exec_ctx& ctx, Block* block)
839 {
840 unsigned idx = block->index;
841 Builder bld(ctx.program, block);
842
843 if (idx == ctx.program->blocks.size() - 1)
844 return;
845
846 /* try to disable wqm handling */
847 if (ctx.handle_wqm && block->kind & block_kind_top_level) {
848 if (ctx.info[idx].exec.size() == 3) {
849 assert(ctx.info[idx].exec[1].second == mask_type_wqm);
850 ctx.info[idx].exec.pop_back();
851 }
852 assert(ctx.info[idx].exec.size() <= 2);
853
854 if (ctx.info[idx].ever_again_needs == 0 ||
855 ctx.info[idx].ever_again_needs == Exact) {
856 /* transition to Exact */
857 aco_ptr<Instruction> branch = std::move(block->instructions.back());
858 block->instructions.pop_back();
859 ctx.info[idx].exec.back().second |= mask_type_global;
860 transition_to_Exact(ctx, bld, idx);
861 bld.insert(std::move(branch));
862 ctx.handle_wqm = false;
863
864 } else if (ctx.info[idx].block_needs & Preserve_WQM) {
865 /* transition to WQM and remove global flag */
866 aco_ptr<Instruction> branch = std::move(block->instructions.back());
867 block->instructions.pop_back();
868 transition_to_WQM(ctx, bld, idx);
869 ctx.info[idx].exec.back().second &= ~mask_type_global;
870 bld.insert(std::move(branch));
871 }
872 }
873
874 if (block->kind & block_kind_loop_preheader) {
875 /* collect information about the succeeding loop */
876 bool has_divergent_break = false;
877 bool has_divergent_continue = false;
878 bool has_discard = false;
879 uint8_t needs = 0;
880 unsigned loop_nest_depth = ctx.program->blocks[idx + 1].loop_nest_depth;
881
882 for (unsigned i = idx + 1; ctx.program->blocks[i].loop_nest_depth >= loop_nest_depth; i++) {
883 Block& loop_block = ctx.program->blocks[i];
884 needs |= ctx.info[i].block_needs;
885
886 if (loop_block.kind & block_kind_uses_discard_if ||
887 loop_block.kind & block_kind_discard ||
888 loop_block.kind & block_kind_uses_demote)
889 has_discard = true;
890 if (loop_block.loop_nest_depth != loop_nest_depth)
891 continue;
892
893 if (loop_block.kind & block_kind_uniform)
894 continue;
895 else if (loop_block.kind & block_kind_break)
896 has_divergent_break = true;
897 else if (loop_block.kind & block_kind_continue)
898 has_divergent_continue = true;
899 }
900
901 if (ctx.handle_wqm) {
902 if (needs & WQM) {
903 aco_ptr<Instruction> branch = std::move(block->instructions.back());
904 block->instructions.pop_back();
905 transition_to_WQM(ctx, bld, idx);
906 bld.insert(std::move(branch));
907 } else {
908 aco_ptr<Instruction> branch = std::move(block->instructions.back());
909 block->instructions.pop_back();
910 transition_to_Exact(ctx, bld, idx);
911 bld.insert(std::move(branch));
912 }
913 }
914
915 unsigned num_exec_masks = ctx.info[idx].exec.size();
916 if (block->kind & block_kind_top_level)
917 num_exec_masks = std::min(num_exec_masks, 2u);
918
919 ctx.loop.emplace_back(&ctx.program->blocks[block->linear_succs[0]],
920 num_exec_masks,
921 needs,
922 has_divergent_break,
923 has_divergent_continue,
924 has_discard);
925 }
926
927 if (block->kind & block_kind_discard) {
928
929 assert(block->instructions.back()->format == Format::PSEUDO_BRANCH);
930 aco_ptr<Instruction> branch = std::move(block->instructions.back());
931 block->instructions.pop_back();
932
933 /* create a discard_if() instruction with the exec mask as condition */
934 unsigned num = 0;
935 if (ctx.loop.size()) {
936 /* if we're in a loop, only discard from the outer exec masks */
937 num = ctx.loop.back().num_exec_masks;
938 } else {
939 num = ctx.info[idx].exec.size() - 1;
940 }
941
942 Temp old_exec = ctx.info[idx].exec.back().first;
943 Temp new_exec = bld.tmp(bld.lm);
944 Temp cond = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
945 bld.exec(Definition(new_exec)), Operand(0u), bld.exec(old_exec));
946 ctx.info[idx].exec.back().first = new_exec;
947
948 for (int i = num - 1; i >= 0; i--) {
949 Instruction *andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
950 ctx.info[block->index].exec[i].first, cond);
951 if (i == (int)ctx.info[idx].exec.size() - 1)
952 andn2->definitions[0].setFixed(exec);
953 if (i == 0)
954 bld.pseudo(aco_opcode::p_exit_early_if, bld.scc(andn2->definitions[1].getTemp()));
955 ctx.info[block->index].exec[i].first = andn2->definitions[0].getTemp();
956 }
957 assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);
958
959 if ((block->kind & (block_kind_break | block_kind_uniform)) == block_kind_break)
960 ctx.info[idx].exec.back().first = cond;
961 bld.insert(std::move(branch));
962 /* no return here as it can be followed by a divergent break */
963 }
964
965 if (block->kind & block_kind_continue_or_break) {
966 assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[1]].linear_succs[0]].kind & block_kind_loop_header);
967 assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[0]].linear_succs[0]].kind & block_kind_loop_exit);
968 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
969 block->instructions.pop_back();
970
971 while (!(ctx.info[idx].exec.back().second & mask_type_loop))
972 ctx.info[idx].exec.pop_back();
973
974 ctx.info[idx].exec.back().first = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm, exec), ctx.info[idx].exec.back().first);
975 bld.branch(aco_opcode::p_cbranch_nz, bld.exec(ctx.info[idx].exec.back().first), block->linear_succs[1], block->linear_succs[0]);
976 return;
977 }
978
979 if (block->kind & block_kind_uniform) {
980 Pseudo_branch_instruction* branch = static_cast<Pseudo_branch_instruction*>(block->instructions.back().get());
981 if (branch->opcode == aco_opcode::p_branch) {
982 branch->target[0] = block->linear_succs[0];
983 } else {
984 branch->target[0] = block->linear_succs[1];
985 branch->target[1] = block->linear_succs[0];
986 }
987 return;
988 }
989
990 if (block->kind & block_kind_branch) {
991
992 if (ctx.handle_wqm &&
993 ctx.info[idx].exec.size() >= 2 &&
994 ctx.info[idx].exec.back().second == mask_type_exact &&
995 !(ctx.info[idx].block_needs & Exact_Branch) &&
996 ctx.info[idx].exec[ctx.info[idx].exec.size() - 2].second & mask_type_wqm) {
997 /* return to wqm before branching */
998 ctx.info[idx].exec.pop_back();
999 }
1000
1001 // orig = s_and_saveexec_b64
1002 assert(block->linear_succs.size() == 2);
1003 assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_z);
1004 Temp cond = block->instructions.back()->operands[0].getTemp();
1005 block->instructions.pop_back();
1006
1007 if (ctx.info[idx].block_needs & Exact_Branch)
1008 transition_to_Exact(ctx, bld, idx);
1009
1010 Temp current_exec = ctx.info[idx].exec.back().first;
1011 uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
1012
1013 Temp then_mask = bld.tmp(bld.lm);
1014 Temp old_exec = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
1015 bld.exec(Definition(then_mask)), cond, bld.exec(current_exec));
1016
1017 ctx.info[idx].exec.back().first = old_exec;
1018
1019 /* add next current exec to the stack */
1020 ctx.info[idx].exec.emplace_back(then_mask, mask_type);
1021
1022 bld.branch(aco_opcode::p_cbranch_z, bld.exec(then_mask), block->linear_succs[1], block->linear_succs[0]);
1023 return;
1024 }
1025
1026 if (block->kind & block_kind_invert) {
1027 // exec = s_andn2_b64 (original_exec, exec)
1028 assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_nz);
1029 block->instructions.pop_back();
1030 Temp then_mask = ctx.info[idx].exec.back().first;
1031 uint8_t mask_type = ctx.info[idx].exec.back().second;
1032 ctx.info[idx].exec.pop_back();
1033 Temp orig_exec = ctx.info[idx].exec.back().first;
1034 Temp else_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm, exec),
1035 bld.def(s1, scc), orig_exec, bld.exec(then_mask));
1036
1037 /* add next current exec to the stack */
1038 ctx.info[idx].exec.emplace_back(else_mask, mask_type);
1039
1040 bld.branch(aco_opcode::p_cbranch_z, bld.exec(else_mask), block->linear_succs[1], block->linear_succs[0]);
1041 return;
1042 }
1043
1044 if (block->kind & block_kind_break) {
1045 // loop_mask = s_andn2_b64 (loop_mask, exec)
1046 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
1047 block->instructions.pop_back();
1048
1049 Temp current_exec = ctx.info[idx].exec.back().first;
1050 Temp cond = Temp();
1051 for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
1052 cond = bld.tmp(s1);
1053 Temp exec_mask = ctx.info[idx].exec[exec_idx].first;
1054 exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
1055 exec_mask, current_exec);
1056 ctx.info[idx].exec[exec_idx].first = exec_mask;
1057 if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
1058 break;
1059 }
1060
1061 /* check if the successor is the merge block, otherwise set exec to 0 */
1062 // TODO: this could be done better by directly branching to the merge block
1063 unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
1064 Block& succ = ctx.program->blocks[succ_idx];
1065 if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
1066 ctx.info[idx].exec.back().first = bld.sop1(Builder::s_mov, bld.def(bld.lm, exec), Operand(0u));
1067 }
1068
1069 bld.branch(aco_opcode::p_cbranch_nz, bld.scc(cond), block->linear_succs[1], block->linear_succs[0]);
1070 return;
1071 }
1072
1073 if (block->kind & block_kind_continue) {
1074 assert(block->instructions.back()->opcode == aco_opcode::p_branch);
1075 block->instructions.pop_back();
1076
1077 Temp current_exec = ctx.info[idx].exec.back().first;
1078 Temp cond = Temp();
1079 for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
1080 if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
1081 break;
1082 cond = bld.tmp(s1);
1083 Temp exec_mask = ctx.info[idx].exec[exec_idx].first;
1084 exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
1085 exec_mask, bld.exec(current_exec));
1086 ctx.info[idx].exec[exec_idx].first = exec_mask;
1087 }
1088 assert(cond != Temp());
1089
1090 /* check if the successor is the merge block, otherwise set exec to 0 */
1091 // TODO: this could be done better by directly branching to the merge block
1092 unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
1093 Block& succ = ctx.program->blocks[succ_idx];
1094 if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
1095 ctx.info[idx].exec.back().first = bld.sop1(Builder::s_mov, bld.def(bld.lm, exec), Operand(0u));
1096 }
1097
1098 bld.branch(aco_opcode::p_cbranch_nz, bld.scc(cond), block->linear_succs[1], block->linear_succs[0]);
1099 return;
1100 }
1101 }
1102
1103 void process_block(exec_ctx& ctx, Block* block)
1104 {
1105 std::vector<aco_ptr<Instruction>> instructions;
1106 instructions.reserve(block->instructions.size());
1107
1108 unsigned idx = add_coupling_code(ctx, block, instructions);
1109
1110 assert(block->index != ctx.program->blocks.size() - 1 ||
1111 ctx.info[block->index].exec.size() <= 2);
1112
1113 process_instructions(ctx, block, instructions, idx);
1114
1115 block->instructions = std::move(instructions);
1116
1117 add_branch_code(ctx, block);
1118
1119 block->live_out_exec = ctx.info[block->index].exec.back().first;
1120 }
1121
1122 } /* end namespace */
1123
1124
1125 void insert_exec_mask(Program *program)
1126 {
1127 exec_ctx ctx(program);
1128
1129 if (program->needs_wqm && program->needs_exact)
1130 calculate_wqm_needs(ctx);
1131
1132 for (Block& block : program->blocks)
1133 process_block(ctx, &block);
1134
1135 }
1136
1137 }
1138