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