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 return instr
->definitions
[0].getTemp().type() == RegType::vgpr
;
111 case aco_opcode::p_extract_vector
:
112 case aco_opcode::p_split_vector
:
113 return instr
->operands
[0].getTemp().type() == RegType::vgpr
;
114 case aco_opcode::p_spill
:
115 case aco_opcode::p_reload
:
122 if (instr
->opcode
== aco_opcode::v_readlane_b32
||
123 instr
->opcode
== aco_opcode::v_readlane_b32_e64
||
124 instr
->opcode
== aco_opcode::v_writelane_b32
||
125 instr
->opcode
== aco_opcode::v_writelane_b32_e64
)
131 bool needs_exact(aco_ptr
<Instruction
>& instr
) {
132 if (instr
->format
== Format::MUBUF
) {
133 MUBUF_instruction
*mubuf
= static_cast<MUBUF_instruction
*>(instr
.get());
134 return mubuf
->disable_wqm
;
135 } else if (instr
->format
== Format::MTBUF
) {
136 MTBUF_instruction
*mtbuf
= static_cast<MTBUF_instruction
*>(instr
.get());
137 return mtbuf
->disable_wqm
;
138 } else if (instr
->format
== Format::MIMG
) {
139 MIMG_instruction
*mimg
= static_cast<MIMG_instruction
*>(instr
.get());
140 return mimg
->disable_wqm
;
141 } else if (instr
->format
== Format::FLAT
|| instr
->format
== Format::GLOBAL
) {
142 FLAT_instruction
*flat
= static_cast<FLAT_instruction
*>(instr
.get());
143 return flat
->disable_wqm
;
145 return instr
->format
== Format::EXP
|| instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
;
149 void set_needs_wqm(wqm_ctx
&ctx
, Temp tmp
)
151 if (!ctx
.needs_wqm
[tmp
.id()]) {
152 ctx
.needs_wqm
[tmp
.id()] = true;
153 if (ctx
.defined_in
[tmp
.id()] != 0xFFFF)
154 ctx
.worklist
.insert(ctx
.defined_in
[tmp
.id()]);
158 void mark_block_wqm(wqm_ctx
&ctx
, unsigned block_idx
)
160 if (ctx
.branch_wqm
[block_idx
])
163 ctx
.branch_wqm
[block_idx
] = true;
164 Block
& block
= ctx
.program
->blocks
[block_idx
];
165 aco_ptr
<Instruction
>& branch
= block
.instructions
.back();
167 if (branch
->opcode
!= aco_opcode::p_branch
) {
168 assert(!branch
->operands
.empty() && branch
->operands
[0].isTemp());
169 set_needs_wqm(ctx
, branch
->operands
[0].getTemp());
172 /* TODO: this sets more branch conditions to WQM than it needs to
173 * it should be enough to stop at the "exec mask top level" */
174 if (block
.kind
& block_kind_top_level
)
177 for (unsigned pred_idx
: block
.logical_preds
)
178 mark_block_wqm(ctx
, pred_idx
);
181 void get_block_needs(wqm_ctx
&ctx
, exec_ctx
&exec_ctx
, Block
* block
)
183 block_info
& info
= exec_ctx
.info
[block
->index
];
185 std::vector
<WQMState
> instr_needs(block
->instructions
.size());
187 if (block
->kind
& block_kind_top_level
) {
188 if (ctx
.loop
&& ctx
.wqm
) {
189 /* mark all break conditions as WQM */
190 unsigned block_idx
= block
->index
+ 1;
191 while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
)) {
192 if (ctx
.program
->blocks
[block_idx
].kind
& block_kind_break
)
193 mark_block_wqm(ctx
, block_idx
);
196 } else if (ctx
.loop
&& !ctx
.wqm
) {
197 /* Ensure a branch never results in an exec mask with only helper
198 * invocations (which can cause a loop to repeat infinitively if it's
199 * break branches are done in exact). */
200 unsigned block_idx
= block
->index
;
202 if ((ctx
.program
->blocks
[block_idx
].kind
& block_kind_branch
))
203 exec_ctx
.info
[block_idx
].block_needs
|= Exact_Branch
;
205 } while (!(ctx
.program
->blocks
[block_idx
].kind
& block_kind_top_level
));
212 for (int i
= block
->instructions
.size() - 1; i
>= 0; --i
) {
213 aco_ptr
<Instruction
>& instr
= block
->instructions
[i
];
215 WQMState needs
= needs_exact(instr
) ? Exact
: Unspecified
;
216 bool propagate_wqm
= instr
->opcode
== aco_opcode::p_wqm
;
217 bool preserve_wqm
= instr
->opcode
== aco_opcode::p_discard_if
;
218 bool pred_by_exec
= pred_by_exec_mask(instr
);
219 for (const Definition
& definition
: instr
->definitions
) {
220 if (!definition
.isTemp())
222 const unsigned def
= definition
.tempId();
223 ctx
.defined_in
[def
] = block
->index
;
224 if (needs
== Unspecified
&& ctx
.needs_wqm
[def
]) {
225 needs
= pred_by_exec
? WQM
: Unspecified
;
226 propagate_wqm
= true;
231 for (const Operand
& op
: instr
->operands
) {
233 set_needs_wqm(ctx
, op
.getTemp());
236 } else if (preserve_wqm
&& info
.block_needs
& WQM
) {
237 needs
= Preserve_WQM
;
240 /* ensure the condition controlling the control flow for this phi is in WQM */
241 if (needs
== WQM
&& instr
->opcode
== aco_opcode::p_phi
) {
242 for (unsigned pred_idx
: block
->logical_preds
) {
243 mark_block_wqm(ctx
, pred_idx
);
244 exec_ctx
.info
[pred_idx
].logical_end_wqm
= true;
245 ctx
.worklist
.insert(pred_idx
);
249 if ((instr
->opcode
== aco_opcode::p_logical_end
&& info
.logical_end_wqm
) ||
250 instr
->opcode
== aco_opcode::p_wqm
) {
251 assert(needs
!= Exact
);
255 instr_needs
[i
] = needs
;
256 info
.block_needs
|= needs
;
259 info
.instr_needs
= instr_needs
;
261 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
262 * <cond> should be computed in WQM */
263 if (info
.block_needs
& WQM
&& !(block
->kind
& block_kind_top_level
)) {
264 for (unsigned pred_idx
: block
->logical_preds
)
265 mark_block_wqm(ctx
, pred_idx
);
268 if (block
->kind
& block_kind_loop_header
)
272 void calculate_wqm_needs(exec_ctx
& exec_ctx
)
274 wqm_ctx
ctx(exec_ctx
.program
);
276 while (!ctx
.worklist
.empty()) {
277 unsigned block_index
= *std::prev(ctx
.worklist
.end());
278 ctx
.worklist
.erase(std::prev(ctx
.worklist
.end()));
280 get_block_needs(ctx
, exec_ctx
, &exec_ctx
.program
->blocks
[block_index
]);
283 uint8_t ever_again_needs
= 0;
284 for (int i
= exec_ctx
.program
->blocks
.size() - 1; i
>= 0; i
--) {
285 exec_ctx
.info
[i
].ever_again_needs
= ever_again_needs
;
286 Block
& block
= exec_ctx
.program
->blocks
[i
];
288 if (block
.kind
& block_kind_needs_lowering
)
289 exec_ctx
.info
[i
].block_needs
|= Exact
;
291 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
292 if ((block
.kind
& block_kind_discard
||
293 block
.kind
& block_kind_uses_discard_if
) &&
294 ever_again_needs
& WQM
)
295 exec_ctx
.info
[i
].block_needs
|= Preserve_WQM
;
297 ever_again_needs
|= exec_ctx
.info
[i
].block_needs
& ~Exact_Branch
;
298 if (block
.kind
& block_kind_discard
||
299 block
.kind
& block_kind_uses_discard_if
||
300 block
.kind
& block_kind_uses_demote
)
301 ever_again_needs
|= Exact
;
303 /* don't propagate WQM preservation further than the next top_level block */
304 if (block
.kind
& block_kind_top_level
)
305 ever_again_needs
&= ~Preserve_WQM
;
307 exec_ctx
.info
[i
].block_needs
&= ~Preserve_WQM
;
309 exec_ctx
.handle_wqm
= true;
312 void transition_to_WQM(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
314 if (ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
)
316 if (ctx
.info
[idx
].exec
.back().second
& mask_type_global
) {
317 Temp exec_mask
= ctx
.info
[idx
].exec
.back().first
;
318 /* TODO: we might generate better code if we pass the uncopied "exec_mask"
319 * directly to the s_wqm (we still need to keep this parallelcopy for
320 * potential later uses of exec_mask though). We currently can't do this
321 * because of a RA bug. */
322 exec_mask
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
), bld
.exec(exec_mask
));
323 ctx
.info
[idx
].exec
.back().first
= exec_mask
;
325 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), exec_mask
);
326 ctx
.info
[idx
].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_wqm
);
329 /* otherwise, the WQM mask should be one below the current mask */
330 ctx
.info
[idx
].exec
.pop_back();
331 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
);
332 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
333 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
334 ctx
.info
[idx
].exec
.back().first
);
337 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
339 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
341 /* We can't remove the loop exec mask, because that can cause exec.size() to
342 * be less than num_exec_masks. The loop exec mask also needs to be kept
343 * around for various uses. */
344 if ((ctx
.info
[idx
].exec
.back().second
& mask_type_global
) &&
345 !(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
346 ctx
.info
[idx
].exec
.pop_back();
347 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_exact
);
348 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
349 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
350 ctx
.info
[idx
].exec
.back().first
);
353 /* otherwise, we create an exact mask and push to the stack */
354 Temp wqm
= ctx
.info
[idx
].exec
.back().first
;
355 Temp exact
= bld
.tmp(bld
.lm
);
356 wqm
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
357 bld
.exec(Definition(exact
)), ctx
.info
[idx
].exec
[0].first
, bld
.exec(wqm
));
358 ctx
.info
[idx
].exec
.back().first
= wqm
;
359 ctx
.info
[idx
].exec
.emplace_back(exact
, mask_type_exact
);
362 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
363 std::vector
<aco_ptr
<Instruction
>>& instructions
)
365 unsigned idx
= block
->index
;
366 Builder
bld(ctx
.program
, &instructions
);
367 std::vector
<unsigned>& preds
= block
->linear_preds
;
371 aco_ptr
<Instruction
>& startpgm
= block
->instructions
[0];
372 assert(startpgm
->opcode
== aco_opcode::p_startpgm
);
373 Temp exec_mask
= startpgm
->definitions
.back().getTemp();
374 bld
.insert(std::move(startpgm
));
376 /* exec seems to need to be manually initialized with combined shaders */
377 if (util_bitcount(ctx
.program
->stage
& sw_mask
) > 1) {
378 bld
.sop1(Builder::s_mov
, bld
.exec(Definition(exec_mask
)), bld
.lm
== s2
? Operand(UINT64_MAX
) : Operand(UINT32_MAX
));
379 instructions
[0]->definitions
.pop_back();
382 if (ctx
.handle_wqm
) {
383 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_exact
| mask_type_initial
);
384 /* if this block only needs WQM, initialize already */
385 if (ctx
.info
[0].block_needs
== WQM
)
386 transition_to_WQM(ctx
, bld
, 0);
388 uint8_t mask
= mask_type_global
;
389 if (ctx
.program
->needs_wqm
) {
390 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), bld
.exec(exec_mask
));
391 mask
|= mask_type_wqm
;
393 mask
|= mask_type_exact
;
395 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
401 /* loop entry block */
402 if (block
->kind
& block_kind_loop_header
) {
403 assert(preds
[0] == idx
- 1);
404 ctx
.info
[idx
].exec
= ctx
.info
[idx
- 1].exec
;
405 loop_info
& info
= ctx
.loop
.back();
406 while (ctx
.info
[idx
].exec
.size() > info
.num_exec_masks
)
407 ctx
.info
[idx
].exec
.pop_back();
409 /* create ssa names for outer exec masks */
410 if (info
.has_discard
) {
411 aco_ptr
<Pseudo_instruction
> phi
;
412 for (int i
= 0; i
< info
.num_exec_masks
- 1; i
++) {
413 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1));
414 phi
->definitions
[0] = bld
.def(bld
.lm
);
415 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[i
].first
);
416 ctx
.info
[idx
].exec
[i
].first
= bld
.insert(std::move(phi
));
420 /* create ssa name for restore mask */
421 if (info
.has_divergent_break
) {
422 /* this phi might be trivial but ensures a parallelcopy on the loop header */
423 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
424 phi
->definitions
[0] = bld
.def(bld
.lm
);
425 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[info
.num_exec_masks
- 1].first
);
426 ctx
.info
[idx
].exec
.back().first
= bld
.insert(std::move(phi
));
429 /* create ssa name for loop active mask */
430 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
431 if (info
.has_divergent_continue
)
432 phi
->definitions
[0] = bld
.def(bld
.lm
);
434 phi
->definitions
[0] = bld
.def(bld
.lm
, exec
);
435 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
.back().first
);
436 Temp loop_active
= bld
.insert(std::move(phi
));
438 if (info
.has_divergent_break
) {
439 uint8_t mask_type
= (ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
)) | mask_type_loop
;
440 ctx
.info
[idx
].exec
.emplace_back(loop_active
, mask_type
);
442 ctx
.info
[idx
].exec
.back().first
= loop_active
;
443 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
446 /* create a parallelcopy to move the active mask to exec */
448 if (info
.has_divergent_continue
) {
449 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
450 bld
.insert(std::move(block
->instructions
[i
]));
453 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
454 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
455 ctx
.info
[idx
].exec
.emplace_back(bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
456 ctx
.info
[idx
].exec
.back().first
), mask_type
);
462 /* loop exit block */
463 if (block
->kind
& block_kind_loop_exit
) {
464 Block
* header
= ctx
.loop
.back().loop_header
;
465 loop_info
& info
= ctx
.loop
.back();
467 for (ASSERTED
unsigned pred
: preds
)
468 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
470 /* fill the loop header phis */
471 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
473 if (info
.has_discard
) {
474 while (k
< info
.num_exec_masks
- 1) {
475 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
476 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
477 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
478 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[k
].first
);
482 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
++];
483 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
484 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
485 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
- 1].first
);
487 if (info
.has_divergent_break
) {
488 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
489 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
490 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
491 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
].first
);
494 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
496 /* create the loop exit phis if not trivial */
497 bool need_parallelcopy
= false;
498 for (unsigned k
= 0; k
< info
.num_exec_masks
; k
++) {
499 Temp same
= ctx
.info
[preds
[0]].exec
[k
].first
;
500 uint8_t type
= ctx
.info
[header_preds
[0]].exec
[k
].second
;
503 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
504 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
508 if (k
== info
.num_exec_masks
- 1u) {
509 bool all_liveout_exec
= true;
510 bool all_not_liveout_exec
= true;
511 for (unsigned pred
: preds
) {
512 all_liveout_exec
= all_liveout_exec
&& same
== ctx
.program
->blocks
[pred
].live_out_exec
;
513 all_not_liveout_exec
= all_not_liveout_exec
&& same
!= ctx
.program
->blocks
[pred
].live_out_exec
;
515 if (!all_liveout_exec
&& !all_not_liveout_exec
)
517 else if (all_not_liveout_exec
)
518 need_parallelcopy
= true;
520 need_parallelcopy
|= !trivial
;
524 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
526 /* create phi for loop footer */
527 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
528 phi
->definitions
[0] = bld
.def(bld
.lm
);
529 if (k
== info
.num_exec_masks
- 1) {
530 phi
->definitions
[0].setFixed(exec
);
531 need_parallelcopy
= false;
533 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++)
534 phi
->operands
[i
] = Operand(ctx
.info
[preds
[i
]].exec
[k
].first
);
535 ctx
.info
[idx
].exec
.emplace_back(bld
.insert(std::move(phi
)), type
);
538 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
540 /* create a parallelcopy to move the live mask to exec */
542 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
543 bld
.insert(std::move(block
->instructions
[i
]));
547 if (ctx
.handle_wqm
) {
548 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
549 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
550 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
551 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
552 transition_to_Exact(ctx
, bld
, idx
);
553 ctx
.handle_wqm
= false;
556 if (ctx
.info
[idx
].block_needs
== WQM
)
557 transition_to_WQM(ctx
, bld
, idx
);
558 else if (ctx
.info
[idx
].block_needs
== Exact
)
559 transition_to_Exact(ctx
, bld
, idx
);
562 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
563 if (need_parallelcopy
) {
564 /* only create this parallelcopy is needed, since the operand isn't
565 * fixed to exec which causes the spiller to miscalculate register demand */
566 /* TODO: Fix register_demand calculation for spilling on loop exits.
567 * The problem is only mitigated because the register demand could be
568 * higher if the exec phi doesn't get assigned to exec. */
569 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
570 ctx
.info
[idx
].exec
.back().first
);
577 if (preds
.size() == 1) {
578 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
580 assert(preds
.size() == 2);
581 /* if one of the predecessors ends in exact mask, we pop it from stack */
582 unsigned num_exec_masks
= std::min(ctx
.info
[preds
[0]].exec
.size(),
583 ctx
.info
[preds
[1]].exec
.size());
584 if (block
->kind
& block_kind_top_level
&& !(block
->kind
& block_kind_merge
))
585 num_exec_masks
= std::min(num_exec_masks
, 2u);
587 /* create phis for diverged exec masks */
588 for (unsigned i
= 0; i
< num_exec_masks
; i
++) {
589 bool in_exec
= i
== num_exec_masks
- 1 && !(block
->kind
& block_kind_merge
);
590 if (!in_exec
&& ctx
.info
[preds
[0]].exec
[i
].first
== ctx
.info
[preds
[1]].exec
[i
].first
) {
591 assert(ctx
.info
[preds
[0]].exec
[i
].second
== ctx
.info
[preds
[1]].exec
[i
].second
);
592 ctx
.info
[idx
].exec
.emplace_back(ctx
.info
[preds
[0]].exec
[i
]);
596 Temp phi
= bld
.pseudo(aco_opcode::p_linear_phi
, in_exec
? bld
.def(bld
.lm
, exec
) : bld
.def(bld
.lm
),
597 ctx
.info
[preds
[0]].exec
[i
].first
,
598 ctx
.info
[preds
[1]].exec
[i
].first
);
599 uint8_t mask_type
= ctx
.info
[preds
[0]].exec
[i
].second
& ctx
.info
[preds
[1]].exec
[i
].second
;
600 ctx
.info
[idx
].exec
.emplace_back(phi
, mask_type
);
605 while (block
->instructions
[i
]->opcode
== aco_opcode::p_phi
||
606 block
->instructions
[i
]->opcode
== aco_opcode::p_linear_phi
) {
607 bld
.insert(std::move(block
->instructions
[i
]));
611 if (block
->kind
& block_kind_merge
)
612 ctx
.info
[idx
].exec
.pop_back();
614 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 3) {
615 assert(ctx
.info
[idx
].exec
.back().second
== mask_type_exact
);
616 assert(block
->kind
& block_kind_merge
);
617 ctx
.info
[idx
].exec
.pop_back();
620 /* try to satisfy the block's needs */
621 if (ctx
.handle_wqm
) {
622 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
623 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
624 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
625 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
626 transition_to_Exact(ctx
, bld
, idx
);
627 ctx
.handle_wqm
= false;
630 if (ctx
.info
[idx
].block_needs
== WQM
)
631 transition_to_WQM(ctx
, bld
, idx
);
632 else if (ctx
.info
[idx
].block_needs
== Exact
)
633 transition_to_Exact(ctx
, bld
, idx
);
636 if (block
->kind
& block_kind_merge
) {
637 Temp restore
= ctx
.info
[idx
].exec
.back().first
;
638 assert(restore
.size() == bld
.lm
.size());
639 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), restore
);
645 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
647 Operand offset
= instr
->operands
[1];
649 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
650 Temp nonempty
= bld
.sopc(Builder::s_cmp_lg
, bld
.def(s1
, scc
), cur_exec
, Operand(0u));
652 if (offset
.isLiteral())
653 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
655 offset
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.hint_m0(bld
.def(s1
)),
656 offset
, Operand(UINT32_MAX
), bld
.scc(nonempty
));
657 } else if (offset
.isConstant() && offset
.constantValue() > 0xFFFFF) {
658 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.hint_m0(bld
.def(s1
)), offset
);
660 if (!offset
.isConstant())
663 switch (instr
->operands
[2].size()) {
665 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
668 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
671 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
674 unreachable("Invalid SMEM buffer store size");
676 instr
->operands
[1] = offset
;
677 /* as_uniform() needs to be done here so it's done in exact mode and helper
678 * lanes don't contribute. */
679 instr
->operands
[2] = Operand(bld
.as_uniform(instr
->operands
[2]));
682 void process_instructions(exec_ctx
& ctx
, Block
* block
,
683 std::vector
<aco_ptr
<Instruction
>>& instructions
,
687 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
690 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
694 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
695 bool process
= (ctx
.handle_wqm
&&
696 (ctx
.info
[block
->index
].block_needs
& state
) !=
697 (ctx
.info
[block
->index
].block_needs
& (WQM
| Exact
))) ||
698 block
->kind
& block_kind_uses_discard_if
||
699 block
->kind
& block_kind_uses_demote
||
700 block
->kind
& block_kind_needs_lowering
;
702 std::vector
<aco_ptr
<Instruction
>>::iterator it
= std::next(block
->instructions
.begin(), idx
);
703 instructions
.insert(instructions
.end(),
704 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(it
),
705 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(block
->instructions
.end()));
709 Builder
bld(ctx
.program
, &instructions
);
711 for (; idx
< block
->instructions
.size(); idx
++) {
712 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
714 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
716 if (instr
->opcode
== aco_opcode::p_discard_if
) {
717 if (ctx
.info
[block
->index
].block_needs
& Preserve_WQM
) {
718 assert(block
->kind
& block_kind_top_level
);
719 transition_to_WQM(ctx
, bld
, block
->index
);
720 ctx
.info
[block
->index
].exec
.back().second
&= ~mask_type_global
;
722 int num
= ctx
.info
[block
->index
].exec
.size();
724 Operand cond
= instr
->operands
[0];
725 for (int i
= num
- 1; i
>= 0; i
--) {
726 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
727 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
729 andn2
->operands
[0].setFixed(exec
);
730 andn2
->definitions
[0].setFixed(exec
);
733 instr
->opcode
= aco_opcode::p_exit_early_if
;
734 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
736 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
738 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
740 } else if (needs
== WQM
&& state
!= WQM
) {
741 transition_to_WQM(ctx
, bld
, block
->index
);
743 } else if (needs
== Exact
&& state
!= Exact
) {
744 transition_to_Exact(ctx
, bld
, block
->index
);
748 if (instr
->opcode
== aco_opcode::p_is_helper
|| instr
->opcode
== aco_opcode::p_load_helper
) {
749 Definition dst
= instr
->definitions
[0];
750 assert(dst
.size() == bld
.lm
.size());
751 if (state
== Exact
) {
752 instr
.reset(create_instruction
<SOP1_instruction
>(bld
.w64or32(Builder::s_mov
), Format::SOP1
, 1, 1));
753 instr
->operands
[0] = Operand(0u);
754 instr
->definitions
[0] = dst
;
756 std::pair
<Temp
, uint8_t>& exact_mask
= ctx
.info
[block
->index
].exec
[0];
757 if (instr
->opcode
== aco_opcode::p_load_helper
&&
758 !(ctx
.info
[block
->index
].exec
[0].second
& mask_type_initial
)) {
759 /* find last initial exact mask */
760 for (int i
= block
->index
; i
>= 0; i
--) {
761 if (ctx
.program
->blocks
[i
].kind
& block_kind_top_level
&&
762 ctx
.info
[i
].exec
[0].second
& mask_type_initial
) {
763 exact_mask
= ctx
.info
[i
].exec
[0];
769 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
770 assert(exact_mask
.second
& mask_type_exact
);
772 instr
.reset(create_instruction
<SOP2_instruction
>(bld
.w64or32(Builder::s_andn2
), Format::SOP2
, 2, 2));
773 instr
->operands
[0] = Operand(ctx
.info
[block
->index
].exec
.back().first
); /* current exec */
774 instr
->operands
[1] = Operand(exact_mask
.first
);
775 instr
->definitions
[0] = dst
;
776 instr
->definitions
[1] = bld
.def(s1
, scc
);
778 } else if (instr
->opcode
== aco_opcode::p_demote_to_helper
) {
779 /* turn demote into discard_if with only exact masks */
780 assert((ctx
.info
[block
->index
].exec
[0].second
& (mask_type_exact
| mask_type_global
)) == (mask_type_exact
| mask_type_global
));
781 ctx
.info
[block
->index
].exec
[0].second
&= ~mask_type_initial
;
784 Temp cond
, exit_cond
;
785 if (instr
->operands
[0].isConstant()) {
786 assert(instr
->operands
[0].constantValue() == -1u);
787 /* transition to exact and set exec to zero */
788 Temp old_exec
= ctx
.info
[block
->index
].exec
.back().first
;
789 Temp new_exec
= bld
.tmp(bld
.lm
);
790 exit_cond
= bld
.tmp(s1
);
791 cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.scc(Definition(exit_cond
)),
792 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
794 num
= ctx
.info
[block
->index
].exec
.size() - 2;
795 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
) {
796 ctx
.info
[block
->index
].exec
.back().first
= new_exec
;
798 ctx
.info
[block
->index
].exec
.back().first
= cond
;
799 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
802 /* demote_if: transition to exact */
803 transition_to_Exact(ctx
, bld
, block
->index
);
804 assert(instr
->operands
[0].isTemp());
805 cond
= instr
->operands
[0].getTemp();
806 num
= ctx
.info
[block
->index
].exec
.size() - 1;
809 for (int i
= num
; i
>= 0; i
--) {
810 if (ctx
.info
[block
->index
].exec
[i
].second
& mask_type_exact
) {
811 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
812 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
813 if (i
== (int)ctx
.info
[block
->index
].exec
.size() - 1) {
814 andn2
->operands
[0].setFixed(exec
);
815 andn2
->definitions
[0].setFixed(exec
);
818 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
819 exit_cond
= andn2
->definitions
[1].getTemp();
824 instr
->opcode
= aco_opcode::p_exit_early_if
;
825 instr
->operands
[0] = bld
.scc(exit_cond
);
828 } else if (instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
) {
829 bool need_check
= ctx
.info
[block
->index
].exec
.size() != 1 &&
830 !(ctx
.info
[block
->index
].exec
[ctx
.info
[block
->index
].exec
.size() - 2].second
& Exact
);
831 lower_fs_buffer_store_smem(bld
, need_check
, instr
, ctx
.info
[block
->index
].exec
.back().first
);
834 bld
.insert(std::move(instr
));
838 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
840 unsigned idx
= block
->index
;
841 Builder
bld(ctx
.program
, block
);
843 if (idx
== ctx
.program
->blocks
.size() - 1)
846 /* try to disable wqm handling */
847 if (ctx
.handle_wqm
&& block
->kind
& block_kind_top_level
) {
848 if (ctx
.info
[idx
].exec
.size() == 3) {
849 assert(ctx
.info
[idx
].exec
[1].second
== mask_type_wqm
);
850 ctx
.info
[idx
].exec
.pop_back();
852 assert(ctx
.info
[idx
].exec
.size() <= 2);
854 if (ctx
.info
[idx
].ever_again_needs
== 0 ||
855 ctx
.info
[idx
].ever_again_needs
== Exact
) {
856 /* transition to Exact */
857 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
858 block
->instructions
.pop_back();
859 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
860 transition_to_Exact(ctx
, bld
, idx
);
861 bld
.insert(std::move(branch
));
862 ctx
.handle_wqm
= false;
864 } else if (ctx
.info
[idx
].block_needs
& Preserve_WQM
) {
865 /* transition to WQM and remove global flag */
866 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
867 block
->instructions
.pop_back();
868 transition_to_WQM(ctx
, bld
, idx
);
869 ctx
.info
[idx
].exec
.back().second
&= ~mask_type_global
;
870 bld
.insert(std::move(branch
));
874 if (block
->kind
& block_kind_loop_preheader
) {
875 /* collect information about the succeeding loop */
876 bool has_divergent_break
= false;
877 bool has_divergent_continue
= false;
878 bool has_discard
= false;
880 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
882 for (unsigned i
= idx
+ 1; ctx
.program
->blocks
[i
].loop_nest_depth
>= loop_nest_depth
; i
++) {
883 Block
& loop_block
= ctx
.program
->blocks
[i
];
884 needs
|= ctx
.info
[i
].block_needs
;
886 if (loop_block
.kind
& block_kind_uses_discard_if
||
887 loop_block
.kind
& block_kind_discard
||
888 loop_block
.kind
& block_kind_uses_demote
)
890 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
893 if (loop_block
.kind
& block_kind_uniform
)
895 else if (loop_block
.kind
& block_kind_break
)
896 has_divergent_break
= true;
897 else if (loop_block
.kind
& block_kind_continue
)
898 has_divergent_continue
= true;
901 if (ctx
.handle_wqm
) {
903 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
904 block
->instructions
.pop_back();
905 transition_to_WQM(ctx
, bld
, idx
);
906 bld
.insert(std::move(branch
));
908 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
909 block
->instructions
.pop_back();
910 transition_to_Exact(ctx
, bld
, idx
);
911 bld
.insert(std::move(branch
));
915 unsigned num_exec_masks
= ctx
.info
[idx
].exec
.size();
916 if (block
->kind
& block_kind_top_level
)
917 num_exec_masks
= std::min(num_exec_masks
, 2u);
919 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
923 has_divergent_continue
,
927 if (block
->kind
& block_kind_discard
) {
929 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
930 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
931 block
->instructions
.pop_back();
933 /* create a discard_if() instruction with the exec mask as condition */
935 if (ctx
.loop
.size()) {
936 /* if we're in a loop, only discard from the outer exec masks */
937 num
= ctx
.loop
.back().num_exec_masks
;
939 num
= ctx
.info
[idx
].exec
.size() - 1;
942 Temp old_exec
= ctx
.info
[idx
].exec
.back().first
;
943 Temp new_exec
= bld
.tmp(bld
.lm
);
944 Temp cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
945 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
946 ctx
.info
[idx
].exec
.back().first
= new_exec
;
948 for (int i
= num
- 1; i
>= 0; i
--) {
949 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
950 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
951 if (i
== (int)ctx
.info
[idx
].exec
.size() - 1)
952 andn2
->definitions
[0].setFixed(exec
);
954 bld
.pseudo(aco_opcode::p_exit_early_if
, bld
.scc(andn2
->definitions
[1].getTemp()));
955 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
957 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
959 if ((block
->kind
& (block_kind_break
| block_kind_uniform
)) == block_kind_break
)
960 ctx
.info
[idx
].exec
.back().first
= cond
;
961 bld
.insert(std::move(branch
));
962 /* no return here as it can be followed by a divergent break */
965 if (block
->kind
& block_kind_continue_or_break
) {
966 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0]].kind
& block_kind_loop_header
);
967 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[0]].linear_succs
[0]].kind
& block_kind_loop_exit
);
968 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
969 block
->instructions
.pop_back();
971 while (!(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
))
972 ctx
.info
[idx
].exec
.pop_back();
974 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), ctx
.info
[idx
].exec
.back().first
);
975 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.exec(ctx
.info
[idx
].exec
.back().first
), block
->linear_succs
[1], block
->linear_succs
[0]);
979 if (block
->kind
& block_kind_uniform
) {
980 Pseudo_branch_instruction
* branch
= static_cast<Pseudo_branch_instruction
*>(block
->instructions
.back().get());
981 if (branch
->opcode
== aco_opcode::p_branch
) {
982 branch
->target
[0] = block
->linear_succs
[0];
984 branch
->target
[0] = block
->linear_succs
[1];
985 branch
->target
[1] = block
->linear_succs
[0];
990 if (block
->kind
& block_kind_branch
) {
992 if (ctx
.handle_wqm
&&
993 ctx
.info
[idx
].exec
.size() >= 2 &&
994 ctx
.info
[idx
].exec
.back().second
== mask_type_exact
&&
995 !(ctx
.info
[idx
].block_needs
& Exact_Branch
) &&
996 ctx
.info
[idx
].exec
[ctx
.info
[idx
].exec
.size() - 2].second
& mask_type_wqm
) {
997 /* return to wqm before branching */
998 ctx
.info
[idx
].exec
.pop_back();
1001 // orig = s_and_saveexec_b64
1002 assert(block
->linear_succs
.size() == 2);
1003 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_z
);
1004 Temp cond
= block
->instructions
.back()->operands
[0].getTemp();
1005 block
->instructions
.pop_back();
1007 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
1008 transition_to_Exact(ctx
, bld
, idx
);
1010 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1011 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
1013 Temp then_mask
= bld
.tmp(bld
.lm
);
1014 Temp old_exec
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
1015 bld
.exec(Definition(then_mask
)), cond
, bld
.exec(current_exec
));
1017 ctx
.info
[idx
].exec
.back().first
= old_exec
;
1019 /* add next current exec to the stack */
1020 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
1022 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1026 if (block
->kind
& block_kind_invert
) {
1027 // exec = s_andn2_b64 (original_exec, exec)
1028 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_nz
);
1029 block
->instructions
.pop_back();
1030 Temp then_mask
= ctx
.info
[idx
].exec
.back().first
;
1031 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
;
1032 ctx
.info
[idx
].exec
.pop_back();
1033 Temp orig_exec
= ctx
.info
[idx
].exec
.back().first
;
1034 Temp else_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
, exec
),
1035 bld
.def(s1
, scc
), orig_exec
, bld
.exec(then_mask
));
1037 /* add next current exec to the stack */
1038 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
1040 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1044 if (block
->kind
& block_kind_break
) {
1045 // loop_mask = s_andn2_b64 (loop_mask, exec)
1046 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1047 block
->instructions
.pop_back();
1049 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1051 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1053 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1054 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1055 exec_mask
, current_exec
);
1056 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1057 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1061 /* check if the successor is the merge block, otherwise set exec to 0 */
1062 // TODO: this could be done better by directly branching to the merge block
1063 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1064 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1065 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1066 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1069 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1073 if (block
->kind
& block_kind_continue
) {
1074 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1075 block
->instructions
.pop_back();
1077 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1079 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1080 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1083 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1084 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1085 exec_mask
, bld
.exec(current_exec
));
1086 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1088 assert(cond
!= Temp());
1090 /* check if the successor is the merge block, otherwise set exec to 0 */
1091 // TODO: this could be done better by directly branching to the merge block
1092 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1093 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1094 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1095 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1098 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1103 void process_block(exec_ctx
& ctx
, Block
* block
)
1105 std::vector
<aco_ptr
<Instruction
>> instructions
;
1106 instructions
.reserve(block
->instructions
.size());
1108 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1110 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1111 ctx
.info
[block
->index
].exec
.size() <= 2);
1113 process_instructions(ctx
, block
, instructions
, idx
);
1115 block
->instructions
= std::move(instructions
);
1117 add_branch_code(ctx
, block
);
1119 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1122 } /* end namespace */
1125 void insert_exec_mask(Program
*program
)
1127 exec_ctx
ctx(program
);
1129 if (program
->needs_wqm
&& program
->needs_exact
)
1130 calculate_wqm_needs(ctx
);
1132 for (Block
& block
: program
->blocks
)
1133 process_block(ctx
, &block
);