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