2 * Copyright © 2019 Valve Corporation
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:
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
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
26 #include "aco_builder.h"
32 enum WQMState
: uint8_t {
35 WQM
= 1 << 1, /* with control flow applied */
36 Preserve_WQM
= 1 << 2,
37 Exact_Branch
= 1 << 3,
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 */
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 */
57 wqm_ctx(Program
* program
) : program(program
),
58 defined_in(program
->peekAllocationId(), 0xFFFF),
59 needs_wqm(program
->peekAllocationId()),
60 branch_wqm(program
->blocks
.size()),
64 for (unsigned i
= 0; i
< program
->blocks
.size(); i
++)
71 uint16_t num_exec_masks
;
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
) {}
82 std::vector
<std::pair
<Temp
, uint8_t>> exec
;
83 std::vector
<WQMState
> instr_needs
;
85 uint8_t ever_again_needs
;
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()) {}
97 bool pred_by_exec_mask(aco_ptr
<Instruction
>& instr
) {
99 return instr
->reads_exec();
100 if (instr
->format
== Format::SMEM
|| instr
->isSALU())
102 if (instr
->format
== Format::PSEUDO_BARRIER
)
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
:
120 if (instr
->opcode
== aco_opcode::v_readlane_b32
||
121 instr
->opcode
== aco_opcode::v_writelane_b32
)
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
;
141 return instr
->format
== Format::EXP
|| instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
;
145 void set_needs_wqm(wqm_ctx
&ctx
, Temp tmp
)
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()]);
154 void mark_block_wqm(wqm_ctx
&ctx
, unsigned block_idx
)
156 if (ctx
.branch_wqm
[block_idx
])
159 ctx
.branch_wqm
[block_idx
] = true;
160 Block
& block
= ctx
.program
->blocks
[block_idx
];
161 aco_ptr
<Instruction
>& branch
= block
.instructions
.back();
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());
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
)
173 for (unsigned pred_idx
: block
.logical_preds
)
174 mark_block_wqm(ctx
, pred_idx
);
177 void get_block_needs(wqm_ctx
&ctx
, exec_ctx
&exec_ctx
, Block
* block
)
179 block_info
& info
= exec_ctx
.info
[block
->index
];
181 std::vector
<WQMState
> instr_needs(block
->instructions
.size());
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
);
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
;
198 if ((ctx
.program
->blocks
[block_idx
].kind
& block_kind_branch
))
199 exec_ctx
.info
[block_idx
].block_needs
|= Exact_Branch
;
201 } while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
));
208 for (int i
= block
->instructions
.size() - 1; i
>= 0; --i
) {
209 aco_ptr
<Instruction
>& instr
= block
->instructions
[i
];
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())
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;
227 for (const Operand
& op
: instr
->operands
) {
229 set_needs_wqm(ctx
, op
.getTemp());
232 } else if (preserve_wqm
&& info
.block_needs
& WQM
) {
233 needs
= Preserve_WQM
;
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
);
242 instr_needs
[i
] = needs
;
243 info
.block_needs
|= needs
;
246 info
.instr_needs
= instr_needs
;
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
);
255 if (block
->kind
& block_kind_loop_header
)
259 void calculate_wqm_needs(exec_ctx
& exec_ctx
)
261 wqm_ctx
ctx(exec_ctx
.program
);
263 while (!ctx
.worklist
.empty()) {
264 unsigned block_index
= *std::prev(ctx
.worklist
.end());
265 ctx
.worklist
.erase(std::prev(ctx
.worklist
.end()));
267 get_block_needs(ctx
, exec_ctx
, &exec_ctx
.program
->blocks
[block_index
]);
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
];
275 if (block
.kind
& block_kind_needs_lowering
)
276 exec_ctx
.info
[i
].block_needs
|= Exact
;
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
;
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
;
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
;
294 exec_ctx
.info
[i
].block_needs
&= ~Preserve_WQM
;
296 exec_ctx
.handle_wqm
= true;
299 void transition_to_WQM(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
301 if (ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
)
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
);
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
);
317 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
319 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
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
);
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
);
342 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
343 std::vector
<aco_ptr
<Instruction
>>& instructions
)
345 unsigned idx
= block
->index
;
346 Builder
bld(ctx
.program
, &instructions
);
347 std::vector
<unsigned>& preds
= block
->linear_preds
;
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
));
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);
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
;
367 mask
|= mask_type_exact
;
369 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
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();
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
));
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
));
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
);
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
));
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
);
416 ctx
.info
[idx
].exec
.back().first
= loop_active
;
417 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
420 /* create a parallelcopy to move the active mask to exec */
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
]));
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
);
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();
441 for (ASSERTED
unsigned pred
: preds
)
442 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
444 /* fill the loop header phis */
445 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
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
);
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
);
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
);
468 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
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
;
476 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
477 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
482 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
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
);
492 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
494 /* create a parallelcopy to move the live mask to exec */
496 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
497 bld
.insert(std::move(block
->instructions
[i
]));
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;
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
);
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
);
524 if (preds
.size() == 1) {
525 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
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);
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
]);
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
);
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
]));
558 if (block
->kind
& block_kind_merge
)
559 ctx
.info
[idx
].exec
.pop_back();
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();
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;
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
);
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
);
592 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
594 Operand offset
= instr
->operands
[1];
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));
599 if (offset
.isLiteral())
600 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
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
);
607 if (!offset
.isConstant())
610 switch (instr
->operands
[2].size()) {
612 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
615 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
618 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
621 unreachable("Invalid SMEM buffer store size");
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]));
629 void process_instructions(exec_ctx
& ctx
, Block
* block
,
630 std::vector
<aco_ptr
<Instruction
>>& instructions
,
634 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
637 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
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
;
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()));
656 Builder
bld(ctx
.program
, &instructions
);
658 for (; idx
< block
->instructions
.size(); idx
++) {
659 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
661 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
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
;
669 int num
= ctx
.info
[block
->index
].exec
.size();
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
);
676 andn2
->operands
[0].setFixed(exec
);
677 andn2
->definitions
[0].setFixed(exec
);
680 instr
->opcode
= aco_opcode::p_exit_early_if
;
681 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
683 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
685 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
687 } else if (needs
== WQM
&& state
!= WQM
) {
688 transition_to_WQM(ctx
, bld
, block
->index
);
690 } else if (needs
== Exact
&& state
!= Exact
) {
691 transition_to_Exact(ctx
, bld
, block
->index
);
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
;
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];
716 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
717 assert(exact_mask
.second
& mask_type_exact
);
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
);
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
;
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
;
741 ctx
.info
[block
->index
].exec
.back().first
= cond
;
742 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
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();
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
);
758 andn2
->operands
[0].setFixed(exec
);
759 andn2
->definitions
[0].setFixed(exec
);
762 instr
->opcode
= aco_opcode::p_exit_early_if
;
763 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
765 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
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
);
778 bld
.insert(std::move(instr
));
782 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
784 unsigned idx
= block
->index
;
785 Builder
bld(ctx
.program
, block
);
787 if (idx
== ctx
.program
->blocks
.size() - 1)
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();
796 assert(ctx
.info
[idx
].exec
.size() <= 2);
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;
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
));
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;
824 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
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
;
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
)
834 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
837 if (loop_block
.kind
& block_kind_uniform
)
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;
845 if (ctx
.handle_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
));
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
));
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);
863 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
867 has_divergent_continue
,
871 if (block
->kind
& block_kind_discard
) {
873 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
874 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
875 block
->instructions
.pop_back();
877 /* create a discard_if() instruction with the exec mask as condition */
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
;
883 num
= ctx
.info
[idx
].exec
.size() - 1;
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
;
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
);
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();
901 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
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 */
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();
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]);
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));
925 assert(cond
!= Temp());
927 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
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];
937 branch
->target
[0] = block
->linear_succs
[1];
938 branch
->target
[1] = block
->linear_succs
[0];
943 if (block
->kind
& block_kind_branch
) {
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();
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();
960 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
961 transition_to_Exact(ctx
, bld
, idx
);
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
);
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
));
970 ctx
.info
[idx
].exec
.back().first
= old_exec
;
972 /* add next current exec to the stack */
973 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
975 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
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
));
990 /* add next current exec to the stack */
991 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
993 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
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();
1002 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1004 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
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
)
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));
1022 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1026 if (block
->kind
& block_kind_continue
) {
1027 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1028 block
->instructions
.pop_back();
1030 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
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
)
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
;
1041 assert(cond
!= Temp());
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));
1051 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1056 void process_block(exec_ctx
& ctx
, Block
* block
)
1058 std::vector
<aco_ptr
<Instruction
>> instructions
;
1059 instructions
.reserve(block
->instructions
.size());
1061 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1063 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1064 ctx
.info
[block
->index
].exec
.size() <= 2);
1066 process_instructions(ctx
, block
, instructions
, idx
);
1068 block
->instructions
= std::move(instructions
);
1070 add_branch_code(ctx
, block
);
1072 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1075 } /* end namespace */
1078 void insert_exec_mask(Program
*program
)
1080 exec_ctx
ctx(program
);
1082 if (program
->needs_wqm
&& program
->needs_exact
)
1083 calculate_wqm_needs(ctx
);
1085 for (Block
& block
: program
->blocks
)
1086 process_block(ctx
, &block
);