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
;
93 std::vector
<block_info
> info
;
94 std::vector
<loop_info
> loop
;
95 bool handle_wqm
= false;
96 exec_ctx(Program
*program
) : program(program
), info(program
->blocks
.size()) {}
99 bool pred_by_exec_mask(aco_ptr
<Instruction
>& instr
) {
101 return instr
->reads_exec();
102 if (instr
->format
== Format::SMEM
|| instr
->isSALU())
104 if (instr
->format
== Format::PSEUDO_BARRIER
)
107 if (instr
->format
== Format::PSEUDO
) {
108 switch (instr
->opcode
) {
109 case aco_opcode::p_create_vector
:
110 case aco_opcode::p_extract_vector
:
111 case aco_opcode::p_split_vector
:
112 for (Definition def
: instr
->definitions
) {
113 if (def
.getTemp().type() == RegType::vgpr
)
117 case aco_opcode::p_spill
:
118 case aco_opcode::p_reload
:
125 if (instr
->opcode
== aco_opcode::v_readlane_b32
||
126 instr
->opcode
== aco_opcode::v_readlane_b32_e64
||
127 instr
->opcode
== aco_opcode::v_writelane_b32
||
128 instr
->opcode
== aco_opcode::v_writelane_b32_e64
)
134 bool needs_exact(aco_ptr
<Instruction
>& instr
) {
135 if (instr
->format
== Format::MUBUF
) {
136 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
137 return mubuf
->disable_wqm
;
138 } else if (instr
->format
== Format::MTBUF
) {
139 MTBUF_instruction
*mtbuf
= static_cast<MTBUF_instruction
*>(instr
.get());
140 return mtbuf
->disable_wqm
;
141 } else if (instr
->format
== Format::MIMG
) {
142 MIMG_instruction
*mimg
= static_cast<MIMG_instruction
*>(instr
.get());
143 return mimg
->disable_wqm
;
144 } else if (instr
->format
== Format::FLAT
|| instr
->format
== Format::GLOBAL
) {
145 FLAT_instruction
*flat
= static_cast<FLAT_instruction
*>(instr
.get());
146 return flat
->disable_wqm
;
148 return instr
->format
== Format::EXP
|| instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
;
152 void set_needs_wqm(wqm_ctx
&ctx
, Temp tmp
)
154 if (!ctx
.needs_wqm
[tmp
.id()]) {
155 ctx
.needs_wqm
[tmp
.id()] = true;
156 if (ctx
.defined_in
[tmp
.id()] != 0xFFFF)
157 ctx
.worklist
.insert(ctx
.defined_in
[tmp
.id()]);
161 void mark_block_wqm(wqm_ctx
&ctx
, unsigned block_idx
)
163 if (ctx
.branch_wqm
[block_idx
])
166 ctx
.branch_wqm
[block_idx
] = true;
167 Block
& block
= ctx
.program
->blocks
[block_idx
];
168 aco_ptr
<Instruction
>& branch
= block
.instructions
.back();
170 if (branch
->opcode
!= aco_opcode::p_branch
) {
171 assert(!branch
->operands
.empty() && branch
->operands
[0].isTemp());
172 set_needs_wqm(ctx
, branch
->operands
[0].getTemp());
175 /* TODO: this sets more branch conditions to WQM than it needs to
176 * it should be enough to stop at the "exec mask top level" */
177 if (block
.kind
& block_kind_top_level
)
180 for (unsigned pred_idx
: block
.logical_preds
)
181 mark_block_wqm(ctx
, pred_idx
);
184 void get_block_needs(wqm_ctx
&ctx
, exec_ctx
&exec_ctx
, Block
* block
)
186 block_info
& info
= exec_ctx
.info
[block
->index
];
188 std::vector
<WQMState
> instr_needs(block
->instructions
.size());
190 if (block
->kind
& block_kind_top_level
) {
191 if (ctx
.loop
&& ctx
.wqm
) {
192 /* mark all break conditions as WQM */
193 unsigned block_idx
= block
->index
+ 1;
194 while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
)) {
195 if (ctx
.program
->blocks
[block_idx
].kind
& block_kind_break
)
196 mark_block_wqm(ctx
, block_idx
);
199 } else if (ctx
.loop
&& !ctx
.wqm
) {
200 /* Ensure a branch never results in an exec mask with only helper
201 * invocations (which can cause a loop to repeat infinitively if it's
202 * break branches are done in exact). */
203 unsigned block_idx
= block
->index
;
205 if ((ctx
.program
->blocks
[block_idx
].kind
& block_kind_branch
))
206 exec_ctx
.info
[block_idx
].block_needs
|= Exact_Branch
;
208 } while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
));
215 for (int i
= block
->instructions
.size() - 1; i
>= 0; --i
) {
216 aco_ptr
<Instruction
>& instr
= block
->instructions
[i
];
218 WQMState needs
= needs_exact(instr
) ? Exact
: Unspecified
;
219 bool propagate_wqm
= instr
->opcode
== aco_opcode::p_wqm
;
220 bool preserve_wqm
= instr
->opcode
== aco_opcode::p_discard_if
;
221 bool pred_by_exec
= pred_by_exec_mask(instr
);
222 for (const Definition
& definition
: instr
->definitions
) {
223 if (!definition
.isTemp())
225 const unsigned def
= definition
.tempId();
226 ctx
.defined_in
[def
] = block
->index
;
227 if (needs
== Unspecified
&& ctx
.needs_wqm
[def
]) {
228 needs
= pred_by_exec
? WQM
: Unspecified
;
229 propagate_wqm
= true;
234 for (const Operand
& op
: instr
->operands
) {
236 set_needs_wqm(ctx
, op
.getTemp());
239 } else if (preserve_wqm
&& info
.block_needs
& WQM
) {
240 needs
= Preserve_WQM
;
243 /* ensure the condition controlling the control flow for this phi is in WQM */
244 if (needs
== WQM
&& instr
->opcode
== aco_opcode::p_phi
) {
245 for (unsigned pred_idx
: block
->logical_preds
) {
246 mark_block_wqm(ctx
, pred_idx
);
247 exec_ctx
.info
[pred_idx
].logical_end_wqm
= true;
248 ctx
.worklist
.insert(pred_idx
);
252 if ((instr
->opcode
== aco_opcode::p_logical_end
&& info
.logical_end_wqm
) ||
253 instr
->opcode
== aco_opcode::p_wqm
) {
254 assert(needs
!= Exact
);
258 instr_needs
[i
] = needs
;
259 info
.block_needs
|= needs
;
262 info
.instr_needs
= instr_needs
;
264 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
265 * <cond> should be computed in WQM */
266 if (info
.block_needs
& WQM
&& !(block
->kind
& block_kind_top_level
)) {
267 for (unsigned pred_idx
: block
->logical_preds
)
268 mark_block_wqm(ctx
, pred_idx
);
271 if (block
->kind
& block_kind_loop_header
)
275 void calculate_wqm_needs(exec_ctx
& exec_ctx
)
277 wqm_ctx
ctx(exec_ctx
.program
);
279 while (!ctx
.worklist
.empty()) {
280 unsigned block_index
= *std::prev(ctx
.worklist
.end());
281 ctx
.worklist
.erase(std::prev(ctx
.worklist
.end()));
283 get_block_needs(ctx
, exec_ctx
, &exec_ctx
.program
->blocks
[block_index
]);
286 uint8_t ever_again_needs
= 0;
287 for (int i
= exec_ctx
.program
->blocks
.size() - 1; i
>= 0; i
--) {
288 exec_ctx
.info
[i
].ever_again_needs
= ever_again_needs
;
289 Block
& block
= exec_ctx
.program
->blocks
[i
];
291 if (block
.kind
& block_kind_needs_lowering
)
292 exec_ctx
.info
[i
].block_needs
|= Exact
;
294 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
295 if ((block
.kind
& block_kind_discard
||
296 block
.kind
& block_kind_uses_discard_if
) &&
297 ever_again_needs
& WQM
)
298 exec_ctx
.info
[i
].block_needs
|= Preserve_WQM
;
300 ever_again_needs
|= exec_ctx
.info
[i
].block_needs
& ~Exact_Branch
;
301 if (block
.kind
& block_kind_discard
||
302 block
.kind
& block_kind_uses_discard_if
||
303 block
.kind
& block_kind_uses_demote
)
304 ever_again_needs
|= Exact
;
306 /* don't propagate WQM preservation further than the next top_level block */
307 if (block
.kind
& block_kind_top_level
)
308 ever_again_needs
&= ~Preserve_WQM
;
310 exec_ctx
.info
[i
].block_needs
&= ~Preserve_WQM
;
312 exec_ctx
.handle_wqm
= true;
315 void transition_to_WQM(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
317 if (ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
)
319 if (ctx
.info
[idx
].exec
.back().second
& mask_type_global
) {
320 Temp exec_mask
= ctx
.info
[idx
].exec
.back().first
;
321 /* TODO: we might generate better code if we pass the uncopied "exec_mask"
322 * directly to the s_wqm (we still need to keep this parallelcopy for
323 * potential later uses of exec_mask though). We currently can't do this
324 * because of a RA bug. */
325 exec_mask
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
), bld
.exec(exec_mask
));
326 ctx
.info
[idx
].exec
.back().first
= exec_mask
;
328 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), exec_mask
);
329 ctx
.info
[idx
].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_wqm
);
332 /* otherwise, the WQM mask should be one below the current mask */
333 ctx
.info
[idx
].exec
.pop_back();
334 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
);
335 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
336 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
337 ctx
.info
[idx
].exec
.back().first
);
340 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
342 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
344 /* We can't remove the loop exec mask, because that can cause exec.size() to
345 * be less than num_exec_masks. The loop exec mask also needs to be kept
346 * around for various uses. */
347 if ((ctx
.info
[idx
].exec
.back().second
& mask_type_global
) &&
348 !(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
349 ctx
.info
[idx
].exec
.pop_back();
350 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_exact
);
351 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
352 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
353 ctx
.info
[idx
].exec
.back().first
);
356 /* otherwise, we create an exact mask and push to the stack */
357 Temp wqm
= ctx
.info
[idx
].exec
.back().first
;
358 Temp exact
= bld
.tmp(bld
.lm
);
359 wqm
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
360 bld
.exec(Definition(exact
)), ctx
.info
[idx
].exec
[0].first
, bld
.exec(wqm
));
361 ctx
.info
[idx
].exec
.back().first
= wqm
;
362 ctx
.info
[idx
].exec
.emplace_back(exact
, mask_type_exact
);
365 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
366 std::vector
<aco_ptr
<Instruction
>>& instructions
)
368 unsigned idx
= block
->index
;
369 Builder
bld(ctx
.program
, &instructions
);
370 std::vector
<unsigned>& preds
= block
->linear_preds
;
374 aco_ptr
<Instruction
>& startpgm
= block
->instructions
[0];
375 assert(startpgm
->opcode
== aco_opcode::p_startpgm
);
376 Temp exec_mask
= startpgm
->definitions
.back().getTemp();
377 bld
.insert(std::move(startpgm
));
379 /* exec seems to need to be manually initialized with combined shaders */
380 if (util_bitcount(ctx
.program
->stage
& sw_mask
) > 1 || (ctx
.program
->stage
& hw_ngg_gs
)) {
381 bld
.sop1(Builder::s_mov
, bld
.exec(Definition(exec_mask
)), bld
.lm
== s2
? Operand(UINT64_MAX
) : Operand(UINT32_MAX
));
382 instructions
[0]->definitions
.pop_back();
385 if (ctx
.handle_wqm
) {
386 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_exact
| mask_type_initial
);
387 /* if this block only needs WQM, initialize already */
388 if (ctx
.info
[0].block_needs
== WQM
)
389 transition_to_WQM(ctx
, bld
, 0);
391 uint8_t mask
= mask_type_global
;
392 if (ctx
.program
->needs_wqm
) {
393 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), bld
.exec(exec_mask
));
394 mask
|= mask_type_wqm
;
396 mask
|= mask_type_exact
;
398 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
404 /* loop entry block */
405 if (block
->kind
& block_kind_loop_header
) {
406 assert(preds
[0] == idx
- 1);
407 ctx
.info
[idx
].exec
= ctx
.info
[idx
- 1].exec
;
408 loop_info
& info
= ctx
.loop
.back();
409 while (ctx
.info
[idx
].exec
.size() > info
.num_exec_masks
)
410 ctx
.info
[idx
].exec
.pop_back();
412 /* create ssa names for outer exec masks */
413 if (info
.has_discard
) {
414 aco_ptr
<Pseudo_instruction
> phi
;
415 for (int i
= 0; i
< info
.num_exec_masks
- 1; i
++) {
416 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1));
417 phi
->definitions
[0] = bld
.def(bld
.lm
);
418 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[i
].first
);
419 ctx
.info
[idx
].exec
[i
].first
= bld
.insert(std::move(phi
));
423 /* create ssa name for restore mask */
424 if (info
.has_divergent_break
) {
425 /* this phi might be trivial but ensures a parallelcopy on the loop header */
426 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
427 phi
->definitions
[0] = bld
.def(bld
.lm
);
428 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[info
.num_exec_masks
- 1].first
);
429 ctx
.info
[idx
].exec
.back().first
= bld
.insert(std::move(phi
));
432 /* create ssa name for loop active mask */
433 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
434 if (info
.has_divergent_continue
)
435 phi
->definitions
[0] = bld
.def(bld
.lm
);
437 phi
->definitions
[0] = bld
.def(bld
.lm
, exec
);
438 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
.back().first
);
439 Temp loop_active
= bld
.insert(std::move(phi
));
441 if (info
.has_divergent_break
) {
442 uint8_t mask_type
= (ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
)) | mask_type_loop
;
443 ctx
.info
[idx
].exec
.emplace_back(loop_active
, mask_type
);
445 ctx
.info
[idx
].exec
.back().first
= loop_active
;
446 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
449 /* create a parallelcopy to move the active mask to exec */
451 if (info
.has_divergent_continue
) {
452 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
453 bld
.insert(std::move(block
->instructions
[i
]));
456 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
457 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
458 ctx
.info
[idx
].exec
.emplace_back(bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
459 ctx
.info
[idx
].exec
.back().first
), mask_type
);
465 /* loop exit block */
466 if (block
->kind
& block_kind_loop_exit
) {
467 Block
* header
= ctx
.loop
.back().loop_header
;
468 loop_info
& info
= ctx
.loop
.back();
470 for (ASSERTED
unsigned pred
: preds
)
471 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
473 /* fill the loop header phis */
474 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
476 if (info
.has_discard
) {
477 while (k
< info
.num_exec_masks
- 1) {
478 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
479 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
480 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
481 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[k
].first
);
485 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
++];
486 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
487 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
488 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
- 1].first
);
490 if (info
.has_divergent_break
) {
491 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
492 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
493 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
494 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
].first
);
497 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
499 /* create the loop exit phis if not trivial */
500 bool need_parallelcopy
= false;
501 for (unsigned k
= 0; k
< info
.num_exec_masks
; k
++) {
502 Temp same
= ctx
.info
[preds
[0]].exec
[k
].first
;
503 uint8_t type
= ctx
.info
[header_preds
[0]].exec
[k
].second
;
506 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
507 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
511 if (k
== info
.num_exec_masks
- 1u) {
512 bool all_liveout_exec
= true;
513 bool all_not_liveout_exec
= true;
514 for (unsigned pred
: preds
) {
515 all_liveout_exec
= all_liveout_exec
&& same
== ctx
.program
->blocks
[pred
].live_out_exec
;
516 all_not_liveout_exec
= all_not_liveout_exec
&& same
!= ctx
.program
->blocks
[pred
].live_out_exec
;
518 if (!all_liveout_exec
&& !all_not_liveout_exec
)
520 else if (all_not_liveout_exec
)
521 need_parallelcopy
= true;
523 need_parallelcopy
|= !trivial
;
527 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
529 /* create phi for loop footer */
530 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
531 phi
->definitions
[0] = bld
.def(bld
.lm
);
532 if (k
== info
.num_exec_masks
- 1u) {
533 phi
->definitions
[0].setFixed(exec
);
534 need_parallelcopy
= false;
536 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++)
537 phi
->operands
[i
] = Operand(ctx
.info
[preds
[i
]].exec
[k
].first
);
538 ctx
.info
[idx
].exec
.emplace_back(bld
.insert(std::move(phi
)), type
);
541 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
543 /* create a parallelcopy to move the live mask to exec */
545 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
546 bld
.insert(std::move(block
->instructions
[i
]));
550 if (ctx
.handle_wqm
) {
551 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
552 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
553 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
554 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
555 transition_to_Exact(ctx
, bld
, idx
);
556 ctx
.handle_wqm
= false;
559 if (ctx
.info
[idx
].block_needs
== WQM
)
560 transition_to_WQM(ctx
, bld
, idx
);
561 else if (ctx
.info
[idx
].block_needs
== Exact
)
562 transition_to_Exact(ctx
, bld
, idx
);
565 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
566 if (need_parallelcopy
) {
567 /* only create this parallelcopy is needed, since the operand isn't
568 * fixed to exec which causes the spiller to miscalculate register demand */
569 /* TODO: Fix register_demand calculation for spilling on loop exits.
570 * The problem is only mitigated because the register demand could be
571 * higher if the exec phi doesn't get assigned to exec. */
572 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
573 ctx
.info
[idx
].exec
.back().first
);
580 if (preds
.size() == 1) {
581 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
583 assert(preds
.size() == 2);
584 /* if one of the predecessors ends in exact mask, we pop it from stack */
585 unsigned num_exec_masks
= std::min(ctx
.info
[preds
[0]].exec
.size(),
586 ctx
.info
[preds
[1]].exec
.size());
587 if (block
->kind
& block_kind_top_level
&& !(block
->kind
& block_kind_merge
))
588 num_exec_masks
= std::min(num_exec_masks
, 2u);
590 /* create phis for diverged exec masks */
591 for (unsigned i
= 0; i
< num_exec_masks
; i
++) {
592 bool in_exec
= i
== num_exec_masks
- 1 && !(block
->kind
& block_kind_merge
);
593 if (!in_exec
&& ctx
.info
[preds
[0]].exec
[i
].first
== ctx
.info
[preds
[1]].exec
[i
].first
) {
594 assert(ctx
.info
[preds
[0]].exec
[i
].second
== ctx
.info
[preds
[1]].exec
[i
].second
);
595 ctx
.info
[idx
].exec
.emplace_back(ctx
.info
[preds
[0]].exec
[i
]);
599 Temp phi
= bld
.pseudo(aco_opcode::p_linear_phi
, in_exec
? bld
.def(bld
.lm
, exec
) : bld
.def(bld
.lm
),
600 ctx
.info
[preds
[0]].exec
[i
].first
,
601 ctx
.info
[preds
[1]].exec
[i
].first
);
602 uint8_t mask_type
= ctx
.info
[preds
[0]].exec
[i
].second
& ctx
.info
[preds
[1]].exec
[i
].second
;
603 ctx
.info
[idx
].exec
.emplace_back(phi
, mask_type
);
608 while (block
->instructions
[i
]->opcode
== aco_opcode::p_phi
||
609 block
->instructions
[i
]->opcode
== aco_opcode::p_linear_phi
) {
610 bld
.insert(std::move(block
->instructions
[i
]));
614 if (block
->kind
& block_kind_merge
)
615 ctx
.info
[idx
].exec
.pop_back();
617 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 3) {
618 assert(ctx
.info
[idx
].exec
.back().second
== mask_type_exact
);
619 assert(block
->kind
& block_kind_merge
);
620 ctx
.info
[idx
].exec
.pop_back();
623 /* try to satisfy the block's needs */
624 if (ctx
.handle_wqm
) {
625 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
626 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
627 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
628 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
629 transition_to_Exact(ctx
, bld
, idx
);
630 ctx
.handle_wqm
= false;
633 if (ctx
.info
[idx
].block_needs
== WQM
)
634 transition_to_WQM(ctx
, bld
, idx
);
635 else if (ctx
.info
[idx
].block_needs
== Exact
)
636 transition_to_Exact(ctx
, bld
, idx
);
639 if (block
->kind
& block_kind_merge
) {
640 Temp restore
= ctx
.info
[idx
].exec
.back().first
;
641 assert(restore
.size() == bld
.lm
.size());
642 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), restore
);
648 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
650 Operand offset
= instr
->operands
[1];
652 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
653 Temp nonempty
= bld
.sopc(Builder::s_cmp_lg
, bld
.def(s1
, scc
), cur_exec
, Operand(0u));
655 if (offset
.isLiteral())
656 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
658 offset
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.hint_m0(bld
.def(s1
)),
659 offset
, Operand(UINT32_MAX
), bld
.scc(nonempty
));
660 } else if (offset
.isConstant() && offset
.constantValue() > 0xFFFFF) {
661 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.hint_m0(bld
.def(s1
)), offset
);
663 if (!offset
.isConstant())
666 switch (instr
->operands
[2].size()) {
668 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
671 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
674 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
677 unreachable("Invalid SMEM buffer store size");
679 instr
->operands
[1] = offset
;
680 /* as_uniform() needs to be done here so it's done in exact mode and helper
681 * lanes don't contribute. */
682 instr
->operands
[2] = Operand(bld
.as_uniform(instr
->operands
[2]));
685 void process_instructions(exec_ctx
& ctx
, Block
* block
,
686 std::vector
<aco_ptr
<Instruction
>>& instructions
,
690 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
693 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
697 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
698 bool process
= (ctx
.handle_wqm
&&
699 (ctx
.info
[block
->index
].block_needs
& state
) !=
700 (ctx
.info
[block
->index
].block_needs
& (WQM
| Exact
))) ||
701 block
->kind
& block_kind_uses_discard_if
||
702 block
->kind
& block_kind_uses_demote
||
703 block
->kind
& block_kind_needs_lowering
;
705 std::vector
<aco_ptr
<Instruction
>>::iterator it
= std::next(block
->instructions
.begin(), idx
);
706 instructions
.insert(instructions
.end(),
707 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(it
),
708 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(block
->instructions
.end()));
712 Builder
bld(ctx
.program
, &instructions
);
714 for (; idx
< block
->instructions
.size(); idx
++) {
715 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
717 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
719 if (instr
->opcode
== aco_opcode::p_discard_if
) {
720 if (ctx
.info
[block
->index
].block_needs
& Preserve_WQM
) {
721 assert(block
->kind
& block_kind_top_level
);
722 transition_to_WQM(ctx
, bld
, block
->index
);
723 ctx
.info
[block
->index
].exec
.back().second
&= ~mask_type_global
;
725 int num
= ctx
.info
[block
->index
].exec
.size();
727 Operand cond
= instr
->operands
[0];
728 for (int i
= num
- 1; i
>= 0; i
--) {
729 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
730 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
732 andn2
->operands
[0].setFixed(exec
);
733 andn2
->definitions
[0].setFixed(exec
);
736 instr
->opcode
= aco_opcode::p_exit_early_if
;
737 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
739 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
741 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
743 } else if (needs
== WQM
&& state
!= WQM
) {
744 transition_to_WQM(ctx
, bld
, block
->index
);
746 } else if (needs
== Exact
&& state
!= Exact
) {
747 transition_to_Exact(ctx
, bld
, block
->index
);
751 if (instr
->opcode
== aco_opcode::p_is_helper
|| instr
->opcode
== aco_opcode::p_load_helper
) {
752 Definition dst
= instr
->definitions
[0];
753 assert(dst
.size() == bld
.lm
.size());
754 if (state
== Exact
) {
755 instr
.reset(create_instruction
<SOP1_instruction
>(bld
.w64or32(Builder::s_mov
), Format::SOP1
, 1, 1));
756 instr
->operands
[0] = Operand(0u);
757 instr
->definitions
[0] = dst
;
759 std::pair
<Temp
, uint8_t>& exact_mask
= ctx
.info
[block
->index
].exec
[0];
760 if (instr
->opcode
== aco_opcode::p_load_helper
&&
761 !(ctx
.info
[block
->index
].exec
[0].second
& mask_type_initial
)) {
762 /* find last initial exact mask */
763 for (int i
= block
->index
; i
>= 0; i
--) {
764 if (ctx
.program
->blocks
[i
].kind
& block_kind_top_level
&&
765 ctx
.info
[i
].exec
[0].second
& mask_type_initial
) {
766 exact_mask
= ctx
.info
[i
].exec
[0];
772 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
773 assert(exact_mask
.second
& mask_type_exact
);
775 instr
.reset(create_instruction
<SOP2_instruction
>(bld
.w64or32(Builder::s_andn2
), Format::SOP2
, 2, 2));
776 instr
->operands
[0] = Operand(ctx
.info
[block
->index
].exec
.back().first
); /* current exec */
777 instr
->operands
[1] = Operand(exact_mask
.first
);
778 instr
->definitions
[0] = dst
;
779 instr
->definitions
[1] = bld
.def(s1
, scc
);
781 } else if (instr
->opcode
== aco_opcode::p_demote_to_helper
) {
782 /* turn demote into discard_if with only exact masks */
783 assert((ctx
.info
[block
->index
].exec
[0].second
& (mask_type_exact
| mask_type_global
)) == (mask_type_exact
| mask_type_global
));
784 ctx
.info
[block
->index
].exec
[0].second
&= ~mask_type_initial
;
787 Temp cond
, exit_cond
;
788 if (instr
->operands
[0].isConstant()) {
789 assert(instr
->operands
[0].constantValue() == -1u);
790 /* transition to exact and set exec to zero */
791 Temp old_exec
= ctx
.info
[block
->index
].exec
.back().first
;
792 Temp new_exec
= bld
.tmp(bld
.lm
);
793 exit_cond
= bld
.tmp(s1
);
794 cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.scc(Definition(exit_cond
)),
795 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
797 num
= ctx
.info
[block
->index
].exec
.size() - 2;
798 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
) {
799 ctx
.info
[block
->index
].exec
.back().first
= new_exec
;
801 ctx
.info
[block
->index
].exec
.back().first
= cond
;
802 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
805 /* demote_if: transition to exact */
806 transition_to_Exact(ctx
, bld
, block
->index
);
807 assert(instr
->operands
[0].isTemp());
808 cond
= instr
->operands
[0].getTemp();
809 num
= ctx
.info
[block
->index
].exec
.size() - 1;
812 for (int i
= num
; i
>= 0; i
--) {
813 if (ctx
.info
[block
->index
].exec
[i
].second
& mask_type_exact
) {
814 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
815 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
816 if (i
== (int)ctx
.info
[block
->index
].exec
.size() - 1) {
817 andn2
->operands
[0].setFixed(exec
);
818 andn2
->definitions
[0].setFixed(exec
);
821 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
822 exit_cond
= andn2
->definitions
[1].getTemp();
827 instr
->opcode
= aco_opcode::p_exit_early_if
;
828 instr
->operands
[0] = bld
.scc(exit_cond
);
831 } else if (instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
) {
832 bool need_check
= ctx
.info
[block
->index
].exec
.size() != 1 &&
833 !(ctx
.info
[block
->index
].exec
[ctx
.info
[block
->index
].exec
.size() - 2].second
& Exact
);
834 lower_fs_buffer_store_smem(bld
, need_check
, instr
, ctx
.info
[block
->index
].exec
.back().first
);
837 bld
.insert(std::move(instr
));
841 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
843 unsigned idx
= block
->index
;
844 Builder
bld(ctx
.program
, block
);
846 if (idx
== ctx
.program
->blocks
.size() - 1)
849 /* try to disable wqm handling */
850 if (ctx
.handle_wqm
&& block
->kind
& block_kind_top_level
) {
851 if (ctx
.info
[idx
].exec
.size() == 3) {
852 assert(ctx
.info
[idx
].exec
[1].second
== mask_type_wqm
);
853 ctx
.info
[idx
].exec
.pop_back();
855 assert(ctx
.info
[idx
].exec
.size() <= 2);
857 if (ctx
.info
[idx
].ever_again_needs
== 0 ||
858 ctx
.info
[idx
].ever_again_needs
== Exact
) {
859 /* transition to Exact */
860 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
861 block
->instructions
.pop_back();
862 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
863 transition_to_Exact(ctx
, bld
, idx
);
864 bld
.insert(std::move(branch
));
865 ctx
.handle_wqm
= false;
867 } else if (ctx
.info
[idx
].block_needs
& Preserve_WQM
) {
868 /* transition to WQM and remove global flag */
869 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
870 block
->instructions
.pop_back();
871 transition_to_WQM(ctx
, bld
, idx
);
872 ctx
.info
[idx
].exec
.back().second
&= ~mask_type_global
;
873 bld
.insert(std::move(branch
));
877 if (block
->kind
& block_kind_loop_preheader
) {
878 /* collect information about the succeeding loop */
879 bool has_divergent_break
= false;
880 bool has_divergent_continue
= false;
881 bool has_discard
= false;
883 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
885 for (unsigned i
= idx
+ 1; ctx
.program
->blocks
[i
].loop_nest_depth
>= loop_nest_depth
; i
++) {
886 Block
& loop_block
= ctx
.program
->blocks
[i
];
887 needs
|= ctx
.info
[i
].block_needs
;
889 if (loop_block
.kind
& block_kind_uses_discard_if
||
890 loop_block
.kind
& block_kind_discard
||
891 loop_block
.kind
& block_kind_uses_demote
)
893 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
896 if (loop_block
.kind
& block_kind_uniform
)
898 else if (loop_block
.kind
& block_kind_break
)
899 has_divergent_break
= true;
900 else if (loop_block
.kind
& block_kind_continue
)
901 has_divergent_continue
= true;
904 if (ctx
.handle_wqm
) {
906 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
907 block
->instructions
.pop_back();
908 transition_to_WQM(ctx
, bld
, idx
);
909 bld
.insert(std::move(branch
));
911 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
912 block
->instructions
.pop_back();
913 transition_to_Exact(ctx
, bld
, idx
);
914 bld
.insert(std::move(branch
));
918 unsigned num_exec_masks
= ctx
.info
[idx
].exec
.size();
919 if (block
->kind
& block_kind_top_level
)
920 num_exec_masks
= std::min(num_exec_masks
, 2u);
922 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
926 has_divergent_continue
,
930 if (block
->kind
& block_kind_discard
) {
932 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
933 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
934 block
->instructions
.pop_back();
936 /* create a discard_if() instruction with the exec mask as condition */
938 if (ctx
.loop
.size()) {
939 /* if we're in a loop, only discard from the outer exec masks */
940 num
= ctx
.loop
.back().num_exec_masks
;
942 num
= ctx
.info
[idx
].exec
.size() - 1;
945 Temp old_exec
= ctx
.info
[idx
].exec
.back().first
;
946 Temp new_exec
= bld
.tmp(bld
.lm
);
947 Temp cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
948 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
949 ctx
.info
[idx
].exec
.back().first
= new_exec
;
951 for (int i
= num
- 1; i
>= 0; i
--) {
952 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
953 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
954 if (i
== (int)ctx
.info
[idx
].exec
.size() - 1)
955 andn2
->definitions
[0].setFixed(exec
);
957 bld
.pseudo(aco_opcode::p_exit_early_if
, bld
.scc(andn2
->definitions
[1].getTemp()));
958 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
960 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
962 if ((block
->kind
& (block_kind_break
| block_kind_uniform
)) == block_kind_break
)
963 ctx
.info
[idx
].exec
.back().first
= cond
;
964 bld
.insert(std::move(branch
));
965 /* no return here as it can be followed by a divergent break */
968 if (block
->kind
& block_kind_continue_or_break
) {
969 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0]].kind
& block_kind_loop_header
);
970 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[0]].linear_succs
[0]].kind
& block_kind_loop_exit
);
971 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
972 block
->instructions
.pop_back();
974 bool need_parallelcopy
= false;
975 while (!(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
976 ctx
.info
[idx
].exec
.pop_back();
977 need_parallelcopy
= true;
980 if (need_parallelcopy
)
981 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), ctx
.info
[idx
].exec
.back().first
);
982 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.exec(ctx
.info
[idx
].exec
.back().first
), block
->linear_succs
[1], block
->linear_succs
[0]);
986 if (block
->kind
& block_kind_uniform
) {
987 Pseudo_branch_instruction
* branch
= static_cast<Pseudo_branch_instruction
*>(block
->instructions
.back().get());
988 if (branch
->opcode
== aco_opcode::p_branch
) {
989 branch
->target
[0] = block
->linear_succs
[0];
991 branch
->target
[0] = block
->linear_succs
[1];
992 branch
->target
[1] = block
->linear_succs
[0];
997 if (block
->kind
& block_kind_branch
) {
999 if (ctx
.handle_wqm
&&
1000 ctx
.info
[idx
].exec
.size() >= 2 &&
1001 ctx
.info
[idx
].exec
.back().second
== mask_type_exact
&&
1002 !(ctx
.info
[idx
].block_needs
& Exact_Branch
) &&
1003 ctx
.info
[idx
].exec
[ctx
.info
[idx
].exec
.size() - 2].second
& mask_type_wqm
) {
1004 /* return to wqm before branching */
1005 ctx
.info
[idx
].exec
.pop_back();
1008 // orig = s_and_saveexec_b64
1009 assert(block
->linear_succs
.size() == 2);
1010 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_z
);
1011 Temp cond
= block
->instructions
.back()->operands
[0].getTemp();
1012 block
->instructions
.pop_back();
1014 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
1015 transition_to_Exact(ctx
, bld
, idx
);
1017 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1018 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
1020 Temp then_mask
= bld
.tmp(bld
.lm
);
1021 Temp old_exec
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
1022 bld
.exec(Definition(then_mask
)), cond
, bld
.exec(current_exec
));
1024 ctx
.info
[idx
].exec
.back().first
= old_exec
;
1026 /* add next current exec to the stack */
1027 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
1029 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1033 if (block
->kind
& block_kind_invert
) {
1034 // exec = s_andn2_b64 (original_exec, exec)
1035 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_nz
);
1036 block
->instructions
.pop_back();
1037 Temp then_mask
= ctx
.info
[idx
].exec
.back().first
;
1038 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
;
1039 ctx
.info
[idx
].exec
.pop_back();
1040 Temp orig_exec
= ctx
.info
[idx
].exec
.back().first
;
1041 Temp else_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
, exec
),
1042 bld
.def(s1
, scc
), orig_exec
, bld
.exec(then_mask
));
1044 /* add next current exec to the stack */
1045 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
1047 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1051 if (block
->kind
& block_kind_break
) {
1052 // loop_mask = s_andn2_b64 (loop_mask, exec)
1053 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1054 block
->instructions
.pop_back();
1056 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1058 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1060 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1061 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1062 exec_mask
, bld
.exec(current_exec
));
1063 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1064 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1068 /* check if the successor is the merge block, otherwise set exec to 0 */
1069 // TODO: this could be done better by directly branching to the merge block
1070 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1071 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1072 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1073 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1076 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1080 if (block
->kind
& block_kind_continue
) {
1081 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1082 block
->instructions
.pop_back();
1084 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1086 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1087 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1090 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1091 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1092 exec_mask
, bld
.exec(current_exec
));
1093 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1095 assert(cond
!= Temp());
1097 /* check if the successor is the merge block, otherwise set exec to 0 */
1098 // TODO: this could be done better by directly branching to the merge block
1099 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1100 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1101 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1102 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1105 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1110 void process_block(exec_ctx
& ctx
, Block
* block
)
1112 std::vector
<aco_ptr
<Instruction
>> instructions
;
1113 instructions
.reserve(block
->instructions
.size());
1115 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1117 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1118 ctx
.info
[block
->index
].exec
.size() <= 2);
1120 process_instructions(ctx
, block
, instructions
, idx
);
1122 block
->instructions
= std::move(instructions
);
1124 add_branch_code(ctx
, block
);
1126 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1129 } /* end namespace */
1132 void insert_exec_mask(Program
*program
)
1134 exec_ctx
ctx(program
);
1136 if (program
->needs_wqm
&& program
->needs_exact
)
1137 calculate_wqm_needs(ctx
);
1139 for (Block
& block
: program
->blocks
)
1140 process_block(ctx
, &block
);