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"
27 #include "util/u_math.h"
33 enum WQMState
: uint8_t {
36 WQM
= 1 << 1, /* with control flow applied */
37 Preserve_WQM
= 1 << 2,
38 Exact_Branch
= 1 << 3,
41 enum mask_type
: uint8_t {
42 mask_type_global
= 1 << 0,
43 mask_type_exact
= 1 << 1,
44 mask_type_wqm
= 1 << 2,
45 mask_type_loop
= 1 << 3, /* active lanes of a loop */
46 mask_type_initial
= 1 << 4, /* initially active lanes */
51 /* state for WQM propagation */
52 std::set
<unsigned> worklist
;
53 std::vector
<uint16_t> defined_in
;
54 std::vector
<bool> needs_wqm
;
55 std::vector
<bool> branch_wqm
; /* true if the branch condition in this block should be in wqm */
58 wqm_ctx(Program
* program
) : program(program
),
59 defined_in(program
->peekAllocationId(), 0xFFFF),
60 needs_wqm(program
->peekAllocationId()),
61 branch_wqm(program
->blocks
.size()),
65 for (unsigned i
= 0; i
< program
->blocks
.size(); i
++)
72 uint16_t num_exec_masks
;
74 bool has_divergent_break
;
75 bool has_divergent_continue
;
76 bool has_discard
; /* has a discard or demote */
77 loop_info(Block
* b
, uint16_t num
, uint8_t needs
, bool breaks
, bool cont
, bool discard
) :
78 loop_header(b
), num_exec_masks(num
), needs(needs
), has_divergent_break(breaks
),
79 has_divergent_continue(cont
), has_discard(discard
) {}
83 std::vector
<std::pair
<Temp
, uint8_t>> exec
;
84 std::vector
<WQMState
> instr_needs
;
86 uint8_t ever_again_needs
;
92 std::vector
<block_info
> info
;
93 std::vector
<loop_info
> loop
;
94 bool handle_wqm
= false;
95 exec_ctx(Program
*program
) : program(program
), info(program
->blocks
.size()) {}
98 bool pred_by_exec_mask(aco_ptr
<Instruction
>& instr
) {
100 return instr
->reads_exec();
101 if (instr
->format
== Format::SMEM
|| instr
->isSALU())
103 if (instr
->format
== Format::PSEUDO_BARRIER
)
106 if (instr
->format
== Format::PSEUDO
) {
107 switch (instr
->opcode
) {
108 case aco_opcode::p_create_vector
:
109 return instr
->definitions
[0].getTemp().type() == RegType::vgpr
;
110 case aco_opcode::p_extract_vector
:
111 case aco_opcode::p_split_vector
:
112 return instr
->operands
[0].getTemp().type() == RegType::vgpr
;
113 case aco_opcode::p_spill
:
114 case aco_opcode::p_reload
:
121 if (instr
->opcode
== aco_opcode::v_readlane_b32
||
122 instr
->opcode
== aco_opcode::v_readlane_b32_e64
||
123 instr
->opcode
== aco_opcode::v_writelane_b32
||
124 instr
->opcode
== aco_opcode::v_writelane_b32_e64
)
130 bool needs_exact(aco_ptr
<Instruction
>& instr
) {
131 if (instr
->format
== Format::MUBUF
) {
132 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
133 return mubuf
->disable_wqm
;
134 } else if (instr
->format
== Format::MTBUF
) {
135 MTBUF_instruction
*mtbuf
= static_cast<MTBUF_instruction
*>(instr
.get());
136 return mtbuf
->disable_wqm
;
137 } else if (instr
->format
== Format::MIMG
) {
138 MIMG_instruction
*mimg
= static_cast<MIMG_instruction
*>(instr
.get());
139 return mimg
->disable_wqm
;
140 } else if (instr
->format
== Format::FLAT
|| instr
->format
== Format::GLOBAL
) {
141 FLAT_instruction
*flat
= static_cast<FLAT_instruction
*>(instr
.get());
142 return flat
->disable_wqm
;
144 return instr
->format
== Format::EXP
|| instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
;
148 void set_needs_wqm(wqm_ctx
&ctx
, Temp tmp
)
150 if (!ctx
.needs_wqm
[tmp
.id()]) {
151 ctx
.needs_wqm
[tmp
.id()] = true;
152 if (ctx
.defined_in
[tmp
.id()] != 0xFFFF)
153 ctx
.worklist
.insert(ctx
.defined_in
[tmp
.id()]);
157 void mark_block_wqm(wqm_ctx
&ctx
, unsigned block_idx
)
159 if (ctx
.branch_wqm
[block_idx
])
162 ctx
.branch_wqm
[block_idx
] = true;
163 Block
& block
= ctx
.program
->blocks
[block_idx
];
164 aco_ptr
<Instruction
>& branch
= block
.instructions
.back();
166 if (branch
->opcode
!= aco_opcode::p_branch
) {
167 assert(!branch
->operands
.empty() && branch
->operands
[0].isTemp());
168 set_needs_wqm(ctx
, branch
->operands
[0].getTemp());
171 /* TODO: this sets more branch conditions to WQM than it needs to
172 * it should be enough to stop at the "exec mask top level" */
173 if (block
.kind
& block_kind_top_level
)
176 for (unsigned pred_idx
: block
.logical_preds
)
177 mark_block_wqm(ctx
, pred_idx
);
180 void get_block_needs(wqm_ctx
&ctx
, exec_ctx
&exec_ctx
, Block
* block
)
182 block_info
& info
= exec_ctx
.info
[block
->index
];
184 std::vector
<WQMState
> instr_needs(block
->instructions
.size());
186 if (block
->kind
& block_kind_top_level
) {
187 if (ctx
.loop
&& ctx
.wqm
) {
188 /* mark all break conditions as WQM */
189 unsigned block_idx
= block
->index
+ 1;
190 while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
)) {
191 if (ctx
.program
->blocks
[block_idx
].kind
& block_kind_break
)
192 mark_block_wqm(ctx
, block_idx
);
195 } else if (ctx
.loop
&& !ctx
.wqm
) {
196 /* Ensure a branch never results in an exec mask with only helper
197 * invocations (which can cause a loop to repeat infinitively if it's
198 * break branches are done in exact). */
199 unsigned block_idx
= block
->index
;
201 if ((ctx
.program
->blocks
[block_idx
].kind
& block_kind_branch
))
202 exec_ctx
.info
[block_idx
].block_needs
|= Exact_Branch
;
204 } while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
));
211 for (int i
= block
->instructions
.size() - 1; i
>= 0; --i
) {
212 aco_ptr
<Instruction
>& instr
= block
->instructions
[i
];
214 WQMState needs
= needs_exact(instr
) ? Exact
: Unspecified
;
215 bool propagate_wqm
= instr
->opcode
== aco_opcode::p_wqm
;
216 bool preserve_wqm
= instr
->opcode
== aco_opcode::p_discard_if
;
217 bool pred_by_exec
= pred_by_exec_mask(instr
);
218 for (const Definition
& definition
: instr
->definitions
) {
219 if (!definition
.isTemp())
221 const unsigned def
= definition
.tempId();
222 ctx
.defined_in
[def
] = block
->index
;
223 if (needs
== Unspecified
&& ctx
.needs_wqm
[def
]) {
224 needs
= pred_by_exec
? WQM
: Unspecified
;
225 propagate_wqm
= true;
230 for (const Operand
& op
: instr
->operands
) {
232 set_needs_wqm(ctx
, op
.getTemp());
235 } else if (preserve_wqm
&& info
.block_needs
& WQM
) {
236 needs
= Preserve_WQM
;
239 /* ensure the condition controlling the control flow for this phi is in WQM */
240 if (needs
== WQM
&& instr
->opcode
== aco_opcode::p_phi
) {
241 for (unsigned pred_idx
: block
->logical_preds
)
242 mark_block_wqm(ctx
, pred_idx
);
245 instr_needs
[i
] = needs
;
246 info
.block_needs
|= needs
;
249 info
.instr_needs
= instr_needs
;
251 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
252 * <cond> should be computed in WQM */
253 if (info
.block_needs
& WQM
&& !(block
->kind
& block_kind_top_level
)) {
254 for (unsigned pred_idx
: block
->logical_preds
)
255 mark_block_wqm(ctx
, pred_idx
);
258 if (block
->kind
& block_kind_loop_header
)
262 void calculate_wqm_needs(exec_ctx
& exec_ctx
)
264 wqm_ctx
ctx(exec_ctx
.program
);
266 while (!ctx
.worklist
.empty()) {
267 unsigned block_index
= *std::prev(ctx
.worklist
.end());
268 ctx
.worklist
.erase(std::prev(ctx
.worklist
.end()));
270 get_block_needs(ctx
, exec_ctx
, &exec_ctx
.program
->blocks
[block_index
]);
273 uint8_t ever_again_needs
= 0;
274 for (int i
= exec_ctx
.program
->blocks
.size() - 1; i
>= 0; i
--) {
275 exec_ctx
.info
[i
].ever_again_needs
= ever_again_needs
;
276 Block
& block
= exec_ctx
.program
->blocks
[i
];
278 if (block
.kind
& block_kind_needs_lowering
)
279 exec_ctx
.info
[i
].block_needs
|= Exact
;
281 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
282 if ((block
.kind
& block_kind_discard
||
283 block
.kind
& block_kind_uses_discard_if
) &&
284 ever_again_needs
& WQM
)
285 exec_ctx
.info
[i
].block_needs
|= Preserve_WQM
;
287 ever_again_needs
|= exec_ctx
.info
[i
].block_needs
& ~Exact_Branch
;
288 if (block
.kind
& block_kind_discard
||
289 block
.kind
& block_kind_uses_discard_if
||
290 block
.kind
& block_kind_uses_demote
)
291 ever_again_needs
|= Exact
;
293 /* don't propagate WQM preservation further than the next top_level block */
294 if (block
.kind
& block_kind_top_level
)
295 ever_again_needs
&= ~Preserve_WQM
;
297 exec_ctx
.info
[i
].block_needs
&= ~Preserve_WQM
;
299 exec_ctx
.handle_wqm
= true;
302 void transition_to_WQM(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
304 if (ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
)
306 if (ctx
.info
[idx
].exec
.back().second
& mask_type_global
) {
307 Temp exec_mask
= ctx
.info
[idx
].exec
.back().first
;
308 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), exec_mask
);
309 ctx
.info
[idx
].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_wqm
);
312 /* otherwise, the WQM mask should be one below the current mask */
313 ctx
.info
[idx
].exec
.pop_back();
314 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
);
315 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
316 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
317 ctx
.info
[idx
].exec
.back().first
);
320 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
322 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
324 /* We can't remove the loop exec mask, because that can cause exec.size() to
325 * be less than num_exec_masks. The loop exec mask also needs to be kept
326 * around for various uses. */
327 if ((ctx
.info
[idx
].exec
.back().second
& mask_type_global
) &&
328 !(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
329 ctx
.info
[idx
].exec
.pop_back();
330 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_exact
);
331 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
332 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
333 ctx
.info
[idx
].exec
.back().first
);
336 /* otherwise, we create an exact mask and push to the stack */
337 Temp wqm
= ctx
.info
[idx
].exec
.back().first
;
338 Temp exact
= bld
.tmp(bld
.lm
);
339 wqm
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
340 bld
.exec(Definition(exact
)), ctx
.info
[idx
].exec
[0].first
, bld
.exec(wqm
));
341 ctx
.info
[idx
].exec
.back().first
= wqm
;
342 ctx
.info
[idx
].exec
.emplace_back(exact
, mask_type_exact
);
345 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
346 std::vector
<aco_ptr
<Instruction
>>& instructions
)
348 unsigned idx
= block
->index
;
349 Builder
bld(ctx
.program
, &instructions
);
350 std::vector
<unsigned>& preds
= block
->linear_preds
;
354 aco_ptr
<Instruction
>& startpgm
= block
->instructions
[0];
355 assert(startpgm
->opcode
== aco_opcode::p_startpgm
);
356 Temp exec_mask
= startpgm
->definitions
.back().getTemp();
357 bld
.insert(std::move(startpgm
));
359 /* exec seems to need to be manually initialized with combined shaders */
360 if (util_bitcount(ctx
.program
->stage
& sw_mask
) > 1) {
361 bld
.sop1(Builder::s_mov
, bld
.exec(Definition(exec_mask
)), bld
.lm
== s2
? Operand(UINT64_MAX
) : Operand(UINT32_MAX
));
362 instructions
[0]->definitions
.pop_back();
365 if (ctx
.handle_wqm
) {
366 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_exact
| mask_type_initial
);
367 /* if this block only needs WQM, initialize already */
368 if (ctx
.info
[0].block_needs
== WQM
)
369 transition_to_WQM(ctx
, bld
, 0);
371 uint8_t mask
= mask_type_global
;
372 if (ctx
.program
->needs_wqm
) {
373 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), bld
.exec(exec_mask
));
374 mask
|= mask_type_wqm
;
376 mask
|= mask_type_exact
;
378 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
384 /* loop entry block */
385 if (block
->kind
& block_kind_loop_header
) {
386 assert(preds
[0] == idx
- 1);
387 ctx
.info
[idx
].exec
= ctx
.info
[idx
- 1].exec
;
388 loop_info
& info
= ctx
.loop
.back();
389 while (ctx
.info
[idx
].exec
.size() > info
.num_exec_masks
)
390 ctx
.info
[idx
].exec
.pop_back();
392 /* create ssa names for outer exec masks */
393 if (info
.has_discard
) {
394 aco_ptr
<Pseudo_instruction
> phi
;
395 for (int i
= 0; i
< info
.num_exec_masks
- 1; i
++) {
396 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1));
397 phi
->definitions
[0] = bld
.def(bld
.lm
);
398 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[i
].first
);
399 ctx
.info
[idx
].exec
[i
].first
= bld
.insert(std::move(phi
));
403 /* create ssa name for restore mask */
404 if (info
.has_divergent_break
) {
405 /* this phi might be trivial but ensures a parallelcopy on the loop header */
406 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
407 phi
->definitions
[0] = bld
.def(bld
.lm
);
408 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[info
.num_exec_masks
- 1].first
);
409 ctx
.info
[idx
].exec
.back().first
= bld
.insert(std::move(phi
));
412 /* create ssa name for loop active mask */
413 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
414 if (info
.has_divergent_continue
)
415 phi
->definitions
[0] = bld
.def(bld
.lm
);
417 phi
->definitions
[0] = bld
.def(bld
.lm
, exec
);
418 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
.back().first
);
419 Temp loop_active
= bld
.insert(std::move(phi
));
421 if (info
.has_divergent_break
) {
422 uint8_t mask_type
= (ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
)) | mask_type_loop
;
423 ctx
.info
[idx
].exec
.emplace_back(loop_active
, mask_type
);
425 ctx
.info
[idx
].exec
.back().first
= loop_active
;
426 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
429 /* create a parallelcopy to move the active mask to exec */
431 if (info
.has_divergent_continue
) {
432 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
433 bld
.insert(std::move(block
->instructions
[i
]));
436 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
437 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
438 ctx
.info
[idx
].exec
.emplace_back(bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
439 ctx
.info
[idx
].exec
.back().first
), mask_type
);
445 /* loop exit block */
446 if (block
->kind
& block_kind_loop_exit
) {
447 Block
* header
= ctx
.loop
.back().loop_header
;
448 loop_info
& info
= ctx
.loop
.back();
450 for (ASSERTED
unsigned pred
: preds
)
451 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
453 /* fill the loop header phis */
454 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
456 if (info
.has_discard
) {
457 while (k
< info
.num_exec_masks
- 1) {
458 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
459 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
460 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
461 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[k
].first
);
465 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
++];
466 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
467 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
468 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
- 1].first
);
470 if (info
.has_divergent_break
) {
471 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
472 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
473 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
474 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
].first
);
477 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
479 /* create the loop exit phis if not trivial */
480 for (unsigned k
= 0; k
< info
.num_exec_masks
; k
++) {
481 Temp same
= ctx
.info
[preds
[0]].exec
[k
].first
;
482 uint8_t type
= ctx
.info
[header_preds
[0]].exec
[k
].second
;
485 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
486 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
491 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
493 /* create phi for loop footer */
494 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
495 phi
->definitions
[0] = bld
.def(bld
.lm
);
496 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++)
497 phi
->operands
[i
] = Operand(ctx
.info
[preds
[i
]].exec
[k
].first
);
498 ctx
.info
[idx
].exec
.emplace_back(bld
.insert(std::move(phi
)), type
);
501 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
503 /* create a parallelcopy to move the live mask to exec */
505 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
506 bld
.insert(std::move(block
->instructions
[i
]));
510 if (ctx
.handle_wqm
) {
511 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
512 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
513 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
514 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
515 transition_to_Exact(ctx
, bld
, idx
);
516 ctx
.handle_wqm
= false;
519 if (ctx
.info
[idx
].block_needs
== WQM
)
520 transition_to_WQM(ctx
, bld
, idx
);
521 else if (ctx
.info
[idx
].block_needs
== Exact
)
522 transition_to_Exact(ctx
, bld
, idx
);
525 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
526 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
527 ctx
.info
[idx
].exec
.back().first
);
533 if (preds
.size() == 1) {
534 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
536 assert(preds
.size() == 2);
537 /* if one of the predecessors ends in exact mask, we pop it from stack */
538 unsigned num_exec_masks
= std::min(ctx
.info
[preds
[0]].exec
.size(),
539 ctx
.info
[preds
[1]].exec
.size());
540 if (block
->kind
& block_kind_top_level
&& !(block
->kind
& block_kind_merge
))
541 num_exec_masks
= std::min(num_exec_masks
, 2u);
543 /* create phis for diverged exec masks */
544 for (unsigned i
= 0; i
< num_exec_masks
; i
++) {
545 bool in_exec
= i
== num_exec_masks
- 1 && !(block
->kind
& block_kind_merge
);
546 if (!in_exec
&& ctx
.info
[preds
[0]].exec
[i
].first
== ctx
.info
[preds
[1]].exec
[i
].first
) {
547 assert(ctx
.info
[preds
[0]].exec
[i
].second
== ctx
.info
[preds
[1]].exec
[i
].second
);
548 ctx
.info
[idx
].exec
.emplace_back(ctx
.info
[preds
[0]].exec
[i
]);
552 Temp phi
= bld
.pseudo(aco_opcode::p_linear_phi
, in_exec
? bld
.def(bld
.lm
, exec
) : bld
.def(bld
.lm
),
553 ctx
.info
[preds
[0]].exec
[i
].first
,
554 ctx
.info
[preds
[1]].exec
[i
].first
);
555 uint8_t mask_type
= ctx
.info
[preds
[0]].exec
[i
].second
& ctx
.info
[preds
[1]].exec
[i
].second
;
556 ctx
.info
[idx
].exec
.emplace_back(phi
, mask_type
);
561 while (block
->instructions
[i
]->opcode
== aco_opcode::p_phi
||
562 block
->instructions
[i
]->opcode
== aco_opcode::p_linear_phi
) {
563 bld
.insert(std::move(block
->instructions
[i
]));
567 if (block
->kind
& block_kind_merge
)
568 ctx
.info
[idx
].exec
.pop_back();
570 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 3) {
571 assert(ctx
.info
[idx
].exec
.back().second
== mask_type_exact
);
572 assert(block
->kind
& block_kind_merge
);
573 ctx
.info
[idx
].exec
.pop_back();
576 /* try to satisfy the block's needs */
577 if (ctx
.handle_wqm
) {
578 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
579 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
580 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
581 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
582 transition_to_Exact(ctx
, bld
, idx
);
583 ctx
.handle_wqm
= false;
586 if (ctx
.info
[idx
].block_needs
== WQM
)
587 transition_to_WQM(ctx
, bld
, idx
);
588 else if (ctx
.info
[idx
].block_needs
== Exact
)
589 transition_to_Exact(ctx
, bld
, idx
);
592 if (block
->kind
& block_kind_merge
) {
593 Temp restore
= ctx
.info
[idx
].exec
.back().first
;
594 assert(restore
.size() == bld
.lm
.size());
595 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), restore
);
601 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
603 Operand offset
= instr
->operands
[1];
605 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
606 Temp nonempty
= bld
.sopc(Builder::s_cmp_lg
, bld
.def(s1
, scc
), cur_exec
, Operand(0u));
608 if (offset
.isLiteral())
609 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
611 offset
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.hint_m0(bld
.def(s1
)),
612 offset
, Operand(UINT32_MAX
), bld
.scc(nonempty
));
613 } else if (offset
.isConstant() && offset
.constantValue() > 0xFFFFF) {
614 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.hint_m0(bld
.def(s1
)), offset
);
616 if (!offset
.isConstant())
619 switch (instr
->operands
[2].size()) {
621 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
624 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
627 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
630 unreachable("Invalid SMEM buffer store size");
632 instr
->operands
[1] = offset
;
633 /* as_uniform() needs to be done here so it's done in exact mode and helper
634 * lanes don't contribute. */
635 instr
->operands
[2] = Operand(bld
.as_uniform(instr
->operands
[2]));
638 void process_instructions(exec_ctx
& ctx
, Block
* block
,
639 std::vector
<aco_ptr
<Instruction
>>& instructions
,
643 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
646 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
650 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
651 bool process
= (ctx
.handle_wqm
&&
652 (ctx
.info
[block
->index
].block_needs
& state
) !=
653 (ctx
.info
[block
->index
].block_needs
& (WQM
| Exact
))) ||
654 block
->kind
& block_kind_uses_discard_if
||
655 block
->kind
& block_kind_uses_demote
||
656 block
->kind
& block_kind_needs_lowering
;
658 std::vector
<aco_ptr
<Instruction
>>::iterator it
= std::next(block
->instructions
.begin(), idx
);
659 instructions
.insert(instructions
.end(),
660 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(it
),
661 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(block
->instructions
.end()));
665 Builder
bld(ctx
.program
, &instructions
);
667 for (; idx
< block
->instructions
.size(); idx
++) {
668 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
670 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
672 if (instr
->opcode
== aco_opcode::p_discard_if
) {
673 if (ctx
.info
[block
->index
].block_needs
& Preserve_WQM
) {
674 assert(block
->kind
& block_kind_top_level
);
675 transition_to_WQM(ctx
, bld
, block
->index
);
676 ctx
.info
[block
->index
].exec
.back().second
&= ~mask_type_global
;
678 int num
= ctx
.info
[block
->index
].exec
.size();
680 Operand cond
= instr
->operands
[0];
681 for (int i
= num
- 1; i
>= 0; i
--) {
682 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
683 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
685 andn2
->operands
[0].setFixed(exec
);
686 andn2
->definitions
[0].setFixed(exec
);
689 instr
->opcode
= aco_opcode::p_exit_early_if
;
690 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
692 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
694 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
696 } else if (needs
== WQM
&& state
!= WQM
) {
697 transition_to_WQM(ctx
, bld
, block
->index
);
699 } else if (needs
== Exact
&& state
!= Exact
) {
700 transition_to_Exact(ctx
, bld
, block
->index
);
704 if (instr
->opcode
== aco_opcode::p_is_helper
|| instr
->opcode
== aco_opcode::p_load_helper
) {
705 Definition dst
= instr
->definitions
[0];
706 assert(dst
.size() == bld
.lm
.size());
707 if (state
== Exact
) {
708 instr
.reset(create_instruction
<SOP1_instruction
>(bld
.w64or32(Builder::s_mov
), Format::SOP1
, 1, 1));
709 instr
->operands
[0] = Operand(0u);
710 instr
->definitions
[0] = dst
;
712 std::pair
<Temp
, uint8_t>& exact_mask
= ctx
.info
[block
->index
].exec
[0];
713 if (instr
->opcode
== aco_opcode::p_load_helper
&&
714 !(ctx
.info
[block
->index
].exec
[0].second
& mask_type_initial
)) {
715 /* find last initial exact mask */
716 for (int i
= block
->index
; i
>= 0; i
--) {
717 if (ctx
.program
->blocks
[i
].kind
& block_kind_top_level
&&
718 ctx
.info
[i
].exec
[0].second
& mask_type_initial
) {
719 exact_mask
= ctx
.info
[i
].exec
[0];
725 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
726 assert(exact_mask
.second
& mask_type_exact
);
728 instr
.reset(create_instruction
<SOP2_instruction
>(bld
.w64or32(Builder::s_andn2
), Format::SOP2
, 2, 2));
729 instr
->operands
[0] = Operand(ctx
.info
[block
->index
].exec
.back().first
); /* current exec */
730 instr
->operands
[1] = Operand(exact_mask
.first
);
731 instr
->definitions
[0] = dst
;
732 instr
->definitions
[1] = bld
.def(s1
, scc
);
734 } else if (instr
->opcode
== aco_opcode::p_demote_to_helper
) {
735 /* turn demote into discard_if with only exact masks */
736 assert((ctx
.info
[block
->index
].exec
[0].second
& (mask_type_exact
| mask_type_global
)) == (mask_type_exact
| mask_type_global
));
737 ctx
.info
[block
->index
].exec
[0].second
&= ~mask_type_initial
;
740 Temp cond
, exit_cond
;
741 if (instr
->operands
[0].isConstant()) {
742 assert(instr
->operands
[0].constantValue() == -1u);
743 /* transition to exact and set exec to zero */
744 Temp old_exec
= ctx
.info
[block
->index
].exec
.back().first
;
745 Temp new_exec
= bld
.tmp(bld
.lm
);
746 exit_cond
= bld
.tmp(s1
);
747 cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.scc(Definition(exit_cond
)),
748 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
750 num
= ctx
.info
[block
->index
].exec
.size() - 2;
751 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
) {
752 ctx
.info
[block
->index
].exec
.back().first
= new_exec
;
754 ctx
.info
[block
->index
].exec
.back().first
= cond
;
755 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
758 /* demote_if: transition to exact */
759 transition_to_Exact(ctx
, bld
, block
->index
);
760 assert(instr
->operands
[0].isTemp());
761 cond
= instr
->operands
[0].getTemp();
762 num
= ctx
.info
[block
->index
].exec
.size() - 1;
765 for (int i
= num
; i
>= 0; i
--) {
766 if (ctx
.info
[block
->index
].exec
[i
].second
& mask_type_exact
) {
767 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
768 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
769 if (i
== (int)ctx
.info
[block
->index
].exec
.size() - 1) {
770 andn2
->operands
[0].setFixed(exec
);
771 andn2
->definitions
[0].setFixed(exec
);
774 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
775 exit_cond
= andn2
->definitions
[1].getTemp();
780 instr
->opcode
= aco_opcode::p_exit_early_if
;
781 instr
->operands
[0] = bld
.scc(exit_cond
);
784 } else if (instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
) {
785 bool need_check
= ctx
.info
[block
->index
].exec
.size() != 1 &&
786 !(ctx
.info
[block
->index
].exec
[ctx
.info
[block
->index
].exec
.size() - 2].second
& Exact
);
787 lower_fs_buffer_store_smem(bld
, need_check
, instr
, ctx
.info
[block
->index
].exec
.back().first
);
790 bld
.insert(std::move(instr
));
794 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
796 unsigned idx
= block
->index
;
797 Builder
bld(ctx
.program
, block
);
799 if (idx
== ctx
.program
->blocks
.size() - 1)
802 /* try to disable wqm handling */
803 if (ctx
.handle_wqm
&& block
->kind
& block_kind_top_level
) {
804 if (ctx
.info
[idx
].exec
.size() == 3) {
805 assert(ctx
.info
[idx
].exec
[1].second
== mask_type_wqm
);
806 ctx
.info
[idx
].exec
.pop_back();
808 assert(ctx
.info
[idx
].exec
.size() <= 2);
810 if (ctx
.info
[idx
].ever_again_needs
== 0 ||
811 ctx
.info
[idx
].ever_again_needs
== Exact
) {
812 /* transition to Exact */
813 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
814 block
->instructions
.pop_back();
815 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
816 transition_to_Exact(ctx
, bld
, idx
);
817 bld
.insert(std::move(branch
));
818 ctx
.handle_wqm
= false;
820 } else if (ctx
.info
[idx
].block_needs
& Preserve_WQM
) {
821 /* transition to WQM and remove global flag */
822 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
823 block
->instructions
.pop_back();
824 transition_to_WQM(ctx
, bld
, idx
);
825 ctx
.info
[idx
].exec
.back().second
&= ~mask_type_global
;
826 bld
.insert(std::move(branch
));
830 if (block
->kind
& block_kind_loop_preheader
) {
831 /* collect information about the succeeding loop */
832 bool has_divergent_break
= false;
833 bool has_divergent_continue
= false;
834 bool has_discard
= false;
836 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
838 for (unsigned i
= idx
+ 1; ctx
.program
->blocks
[i
].loop_nest_depth
>= loop_nest_depth
; i
++) {
839 Block
& loop_block
= ctx
.program
->blocks
[i
];
840 needs
|= ctx
.info
[i
].block_needs
;
842 if (loop_block
.kind
& block_kind_uses_discard_if
||
843 loop_block
.kind
& block_kind_discard
||
844 loop_block
.kind
& block_kind_uses_demote
)
846 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
849 if (loop_block
.kind
& block_kind_uniform
)
851 else if (loop_block
.kind
& block_kind_break
)
852 has_divergent_break
= true;
853 else if (loop_block
.kind
& block_kind_continue
)
854 has_divergent_continue
= true;
857 if (ctx
.handle_wqm
) {
859 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
860 block
->instructions
.pop_back();
861 transition_to_WQM(ctx
, bld
, idx
);
862 bld
.insert(std::move(branch
));
864 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
865 block
->instructions
.pop_back();
866 transition_to_Exact(ctx
, bld
, idx
);
867 bld
.insert(std::move(branch
));
871 unsigned num_exec_masks
= ctx
.info
[idx
].exec
.size();
872 if (block
->kind
& block_kind_top_level
)
873 num_exec_masks
= std::min(num_exec_masks
, 2u);
875 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
879 has_divergent_continue
,
883 if (block
->kind
& block_kind_discard
) {
885 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
886 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
887 block
->instructions
.pop_back();
889 /* create a discard_if() instruction with the exec mask as condition */
891 if (ctx
.loop
.size()) {
892 /* if we're in a loop, only discard from the outer exec masks */
893 num
= ctx
.loop
.back().num_exec_masks
;
895 num
= ctx
.info
[idx
].exec
.size() - 1;
898 Temp old_exec
= ctx
.info
[idx
].exec
.back().first
;
899 Temp new_exec
= bld
.tmp(bld
.lm
);
900 Temp cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
901 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
902 ctx
.info
[idx
].exec
.back().first
= new_exec
;
904 for (int i
= num
- 1; i
>= 0; i
--) {
905 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
906 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
907 if (i
== (int)ctx
.info
[idx
].exec
.size() - 1)
908 andn2
->definitions
[0].setFixed(exec
);
910 bld
.pseudo(aco_opcode::p_exit_early_if
, bld
.scc(andn2
->definitions
[1].getTemp()));
911 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
913 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
915 if ((block
->kind
& (block_kind_break
| block_kind_uniform
)) == block_kind_break
)
916 ctx
.info
[idx
].exec
.back().first
= cond
;
917 bld
.insert(std::move(branch
));
918 /* no return here as it can be followed by a divergent break */
921 if (block
->kind
& block_kind_continue_or_break
) {
922 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0]].kind
& block_kind_loop_header
);
923 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[0]].linear_succs
[0]].kind
& block_kind_loop_exit
);
924 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
925 block
->instructions
.pop_back();
927 while (!(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
))
928 ctx
.info
[idx
].exec
.pop_back();
930 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), ctx
.info
[idx
].exec
.back().first
);
931 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.exec(ctx
.info
[idx
].exec
.back().first
), block
->linear_succs
[1], block
->linear_succs
[0]);
935 if (block
->kind
& block_kind_uniform
) {
936 Pseudo_branch_instruction
* branch
= static_cast<Pseudo_branch_instruction
*>(block
->instructions
.back().get());
937 if (branch
->opcode
== aco_opcode::p_branch
) {
938 branch
->target
[0] = block
->linear_succs
[0];
940 branch
->target
[0] = block
->linear_succs
[1];
941 branch
->target
[1] = block
->linear_succs
[0];
946 if (block
->kind
& block_kind_branch
) {
948 if (ctx
.handle_wqm
&&
949 ctx
.info
[idx
].exec
.size() >= 2 &&
950 ctx
.info
[idx
].exec
.back().second
== mask_type_exact
&&
951 !(ctx
.info
[idx
].block_needs
& Exact_Branch
) &&
952 ctx
.info
[idx
].exec
[ctx
.info
[idx
].exec
.size() - 2].second
& mask_type_wqm
) {
953 /* return to wqm before branching */
954 ctx
.info
[idx
].exec
.pop_back();
957 // orig = s_and_saveexec_b64
958 assert(block
->linear_succs
.size() == 2);
959 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_z
);
960 Temp cond
= block
->instructions
.back()->operands
[0].getTemp();
961 block
->instructions
.pop_back();
963 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
964 transition_to_Exact(ctx
, bld
, idx
);
966 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
967 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
969 Temp then_mask
= bld
.tmp(bld
.lm
);
970 Temp old_exec
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
971 bld
.exec(Definition(then_mask
)), cond
, bld
.exec(current_exec
));
973 ctx
.info
[idx
].exec
.back().first
= old_exec
;
975 /* add next current exec to the stack */
976 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
978 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
982 if (block
->kind
& block_kind_invert
) {
983 // exec = s_andn2_b64 (original_exec, exec)
984 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_nz
);
985 block
->instructions
.pop_back();
986 Temp then_mask
= ctx
.info
[idx
].exec
.back().first
;
987 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
;
988 ctx
.info
[idx
].exec
.pop_back();
989 Temp orig_exec
= ctx
.info
[idx
].exec
.back().first
;
990 Temp else_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
, exec
),
991 bld
.def(s1
, scc
), orig_exec
, bld
.exec(then_mask
));
993 /* add next current exec to the stack */
994 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
996 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1000 if (block
->kind
& block_kind_break
) {
1001 // loop_mask = s_andn2_b64 (loop_mask, exec)
1002 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1003 block
->instructions
.pop_back();
1005 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1007 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1009 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1010 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1011 exec_mask
, current_exec
);
1012 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1013 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1017 /* check if the successor is the merge block, otherwise set exec to 0 */
1018 // TODO: this could be done better by directly branching to the merge block
1019 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1020 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1021 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1022 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1025 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1029 if (block
->kind
& block_kind_continue
) {
1030 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1031 block
->instructions
.pop_back();
1033 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1035 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1036 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1039 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1040 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1041 exec_mask
, bld
.exec(current_exec
));
1042 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1044 assert(cond
!= Temp());
1046 /* check if the successor is the merge block, otherwise set exec to 0 */
1047 // TODO: this could be done better by directly branching to the merge block
1048 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1049 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1050 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1051 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1054 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1059 void process_block(exec_ctx
& ctx
, Block
* block
)
1061 std::vector
<aco_ptr
<Instruction
>> instructions
;
1062 instructions
.reserve(block
->instructions
.size());
1064 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1066 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1067 ctx
.info
[block
->index
].exec
.size() <= 2);
1069 process_instructions(ctx
, block
, instructions
, idx
);
1071 block
->instructions
= std::move(instructions
);
1073 add_branch_code(ctx
, block
);
1075 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1078 } /* end namespace */
1081 void insert_exec_mask(Program
*program
)
1083 exec_ctx
ctx(program
);
1085 if (program
->needs_wqm
&& program
->needs_exact
)
1086 calculate_wqm_needs(ctx
);
1088 for (Block
& block
: program
->blocks
)
1089 process_block(ctx
, &block
);