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