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