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 assert(needs
== Unspecified
);
254 instr_needs
[i
] = needs
;
255 info
.block_needs
|= needs
;
258 info
.instr_needs
= instr_needs
;
260 /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
261 * <cond> should be computed in WQM */
262 if (info
.block_needs
& WQM
&& !(block
->kind
& block_kind_top_level
)) {
263 for (unsigned pred_idx
: block
->logical_preds
)
264 mark_block_wqm(ctx
, pred_idx
);
267 if (block
->kind
& block_kind_loop_header
)
271 void calculate_wqm_needs(exec_ctx
& exec_ctx
)
273 wqm_ctx
ctx(exec_ctx
.program
);
275 while (!ctx
.worklist
.empty()) {
276 unsigned block_index
= *std::prev(ctx
.worklist
.end());
277 ctx
.worklist
.erase(std::prev(ctx
.worklist
.end()));
279 get_block_needs(ctx
, exec_ctx
, &exec_ctx
.program
->blocks
[block_index
]);
282 uint8_t ever_again_needs
= 0;
283 for (int i
= exec_ctx
.program
->blocks
.size() - 1; i
>= 0; i
--) {
284 exec_ctx
.info
[i
].ever_again_needs
= ever_again_needs
;
285 Block
& block
= exec_ctx
.program
->blocks
[i
];
287 if (block
.kind
& block_kind_needs_lowering
)
288 exec_ctx
.info
[i
].block_needs
|= Exact
;
290 /* if discard is used somewhere in nested CF, we need to preserve the WQM mask */
291 if ((block
.kind
& block_kind_discard
||
292 block
.kind
& block_kind_uses_discard_if
) &&
293 ever_again_needs
& WQM
)
294 exec_ctx
.info
[i
].block_needs
|= Preserve_WQM
;
296 ever_again_needs
|= exec_ctx
.info
[i
].block_needs
& ~Exact_Branch
;
297 if (block
.kind
& block_kind_discard
||
298 block
.kind
& block_kind_uses_discard_if
||
299 block
.kind
& block_kind_uses_demote
)
300 ever_again_needs
|= Exact
;
302 /* don't propagate WQM preservation further than the next top_level block */
303 if (block
.kind
& block_kind_top_level
)
304 ever_again_needs
&= ~Preserve_WQM
;
306 exec_ctx
.info
[i
].block_needs
&= ~Preserve_WQM
;
308 exec_ctx
.handle_wqm
= true;
311 void transition_to_WQM(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
313 if (ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
)
315 if (ctx
.info
[idx
].exec
.back().second
& mask_type_global
) {
316 Temp exec_mask
= ctx
.info
[idx
].exec
.back().first
;
317 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), exec_mask
);
318 ctx
.info
[idx
].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_wqm
);
321 /* otherwise, the WQM mask should be one below the current mask */
322 ctx
.info
[idx
].exec
.pop_back();
323 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
);
324 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
325 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
326 ctx
.info
[idx
].exec
.back().first
);
329 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
331 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
333 /* We can't remove the loop exec mask, because that can cause exec.size() to
334 * be less than num_exec_masks. The loop exec mask also needs to be kept
335 * around for various uses. */
336 if ((ctx
.info
[idx
].exec
.back().second
& mask_type_global
) &&
337 !(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
338 ctx
.info
[idx
].exec
.pop_back();
339 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_exact
);
340 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
341 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
342 ctx
.info
[idx
].exec
.back().first
);
345 /* otherwise, we create an exact mask and push to the stack */
346 Temp wqm
= ctx
.info
[idx
].exec
.back().first
;
347 Temp exact
= bld
.tmp(bld
.lm
);
348 wqm
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
349 bld
.exec(Definition(exact
)), ctx
.info
[idx
].exec
[0].first
, bld
.exec(wqm
));
350 ctx
.info
[idx
].exec
.back().first
= wqm
;
351 ctx
.info
[idx
].exec
.emplace_back(exact
, mask_type_exact
);
354 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
355 std::vector
<aco_ptr
<Instruction
>>& instructions
)
357 unsigned idx
= block
->index
;
358 Builder
bld(ctx
.program
, &instructions
);
359 std::vector
<unsigned>& preds
= block
->linear_preds
;
363 aco_ptr
<Instruction
>& startpgm
= block
->instructions
[0];
364 assert(startpgm
->opcode
== aco_opcode::p_startpgm
);
365 Temp exec_mask
= startpgm
->definitions
.back().getTemp();
366 bld
.insert(std::move(startpgm
));
368 /* exec seems to need to be manually initialized with combined shaders */
369 if (util_bitcount(ctx
.program
->stage
& sw_mask
) > 1) {
370 bld
.sop1(Builder::s_mov
, bld
.exec(Definition(exec_mask
)), bld
.lm
== s2
? Operand(UINT64_MAX
) : Operand(UINT32_MAX
));
371 instructions
[0]->definitions
.pop_back();
374 if (ctx
.handle_wqm
) {
375 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_exact
| mask_type_initial
);
376 /* if this block only needs WQM, initialize already */
377 if (ctx
.info
[0].block_needs
== WQM
)
378 transition_to_WQM(ctx
, bld
, 0);
380 uint8_t mask
= mask_type_global
;
381 if (ctx
.program
->needs_wqm
) {
382 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), bld
.exec(exec_mask
));
383 mask
|= mask_type_wqm
;
385 mask
|= mask_type_exact
;
387 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
393 /* loop entry block */
394 if (block
->kind
& block_kind_loop_header
) {
395 assert(preds
[0] == idx
- 1);
396 ctx
.info
[idx
].exec
= ctx
.info
[idx
- 1].exec
;
397 loop_info
& info
= ctx
.loop
.back();
398 while (ctx
.info
[idx
].exec
.size() > info
.num_exec_masks
)
399 ctx
.info
[idx
].exec
.pop_back();
401 /* create ssa names for outer exec masks */
402 if (info
.has_discard
) {
403 aco_ptr
<Pseudo_instruction
> phi
;
404 for (int i
= 0; i
< info
.num_exec_masks
- 1; i
++) {
405 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1));
406 phi
->definitions
[0] = bld
.def(bld
.lm
);
407 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[i
].first
);
408 ctx
.info
[idx
].exec
[i
].first
= bld
.insert(std::move(phi
));
412 /* create ssa name for restore mask */
413 if (info
.has_divergent_break
) {
414 /* this phi might be trivial but ensures a parallelcopy on the loop header */
415 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
416 phi
->definitions
[0] = bld
.def(bld
.lm
);
417 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[info
.num_exec_masks
- 1].first
);
418 ctx
.info
[idx
].exec
.back().first
= bld
.insert(std::move(phi
));
421 /* create ssa name for loop active mask */
422 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
423 if (info
.has_divergent_continue
)
424 phi
->definitions
[0] = bld
.def(bld
.lm
);
426 phi
->definitions
[0] = bld
.def(bld
.lm
, exec
);
427 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
.back().first
);
428 Temp loop_active
= bld
.insert(std::move(phi
));
430 if (info
.has_divergent_break
) {
431 uint8_t mask_type
= (ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
)) | mask_type_loop
;
432 ctx
.info
[idx
].exec
.emplace_back(loop_active
, mask_type
);
434 ctx
.info
[idx
].exec
.back().first
= loop_active
;
435 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
438 /* create a parallelcopy to move the active mask to exec */
440 if (info
.has_divergent_continue
) {
441 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
442 bld
.insert(std::move(block
->instructions
[i
]));
445 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
446 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
447 ctx
.info
[idx
].exec
.emplace_back(bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
448 ctx
.info
[idx
].exec
.back().first
), mask_type
);
454 /* loop exit block */
455 if (block
->kind
& block_kind_loop_exit
) {
456 Block
* header
= ctx
.loop
.back().loop_header
;
457 loop_info
& info
= ctx
.loop
.back();
459 for (ASSERTED
unsigned pred
: preds
)
460 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
462 /* fill the loop header phis */
463 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
465 if (info
.has_discard
) {
466 while (k
< info
.num_exec_masks
- 1) {
467 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
468 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
469 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
470 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[k
].first
);
474 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
++];
475 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
476 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
477 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
- 1].first
);
479 if (info
.has_divergent_break
) {
480 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
481 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
482 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
483 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
].first
);
486 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
488 /* create the loop exit phis if not trivial */
489 for (unsigned k
= 0; k
< info
.num_exec_masks
; k
++) {
490 Temp same
= ctx
.info
[preds
[0]].exec
[k
].first
;
491 uint8_t type
= ctx
.info
[header_preds
[0]].exec
[k
].second
;
494 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
495 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
500 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
502 /* create phi for loop footer */
503 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
504 phi
->definitions
[0] = bld
.def(bld
.lm
);
505 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++)
506 phi
->operands
[i
] = Operand(ctx
.info
[preds
[i
]].exec
[k
].first
);
507 ctx
.info
[idx
].exec
.emplace_back(bld
.insert(std::move(phi
)), type
);
510 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
512 /* create a parallelcopy to move the live mask to exec */
514 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
515 bld
.insert(std::move(block
->instructions
[i
]));
519 if (ctx
.handle_wqm
) {
520 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
521 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
522 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
523 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
524 transition_to_Exact(ctx
, bld
, idx
);
525 ctx
.handle_wqm
= false;
528 if (ctx
.info
[idx
].block_needs
== WQM
)
529 transition_to_WQM(ctx
, bld
, idx
);
530 else if (ctx
.info
[idx
].block_needs
== Exact
)
531 transition_to_Exact(ctx
, bld
, idx
);
534 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
535 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
536 ctx
.info
[idx
].exec
.back().first
);
542 if (preds
.size() == 1) {
543 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
545 assert(preds
.size() == 2);
546 /* if one of the predecessors ends in exact mask, we pop it from stack */
547 unsigned num_exec_masks
= std::min(ctx
.info
[preds
[0]].exec
.size(),
548 ctx
.info
[preds
[1]].exec
.size());
549 if (block
->kind
& block_kind_top_level
&& !(block
->kind
& block_kind_merge
))
550 num_exec_masks
= std::min(num_exec_masks
, 2u);
552 /* create phis for diverged exec masks */
553 for (unsigned i
= 0; i
< num_exec_masks
; i
++) {
554 bool in_exec
= i
== num_exec_masks
- 1 && !(block
->kind
& block_kind_merge
);
555 if (!in_exec
&& ctx
.info
[preds
[0]].exec
[i
].first
== ctx
.info
[preds
[1]].exec
[i
].first
) {
556 assert(ctx
.info
[preds
[0]].exec
[i
].second
== ctx
.info
[preds
[1]].exec
[i
].second
);
557 ctx
.info
[idx
].exec
.emplace_back(ctx
.info
[preds
[0]].exec
[i
]);
561 Temp phi
= bld
.pseudo(aco_opcode::p_linear_phi
, in_exec
? bld
.def(bld
.lm
, exec
) : bld
.def(bld
.lm
),
562 ctx
.info
[preds
[0]].exec
[i
].first
,
563 ctx
.info
[preds
[1]].exec
[i
].first
);
564 uint8_t mask_type
= ctx
.info
[preds
[0]].exec
[i
].second
& ctx
.info
[preds
[1]].exec
[i
].second
;
565 ctx
.info
[idx
].exec
.emplace_back(phi
, mask_type
);
570 while (block
->instructions
[i
]->opcode
== aco_opcode::p_phi
||
571 block
->instructions
[i
]->opcode
== aco_opcode::p_linear_phi
) {
572 bld
.insert(std::move(block
->instructions
[i
]));
576 if (block
->kind
& block_kind_merge
)
577 ctx
.info
[idx
].exec
.pop_back();
579 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 3) {
580 assert(ctx
.info
[idx
].exec
.back().second
== mask_type_exact
);
581 assert(block
->kind
& block_kind_merge
);
582 ctx
.info
[idx
].exec
.pop_back();
585 /* try to satisfy the block's needs */
586 if (ctx
.handle_wqm
) {
587 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
588 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
589 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
590 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
591 transition_to_Exact(ctx
, bld
, idx
);
592 ctx
.handle_wqm
= false;
595 if (ctx
.info
[idx
].block_needs
== WQM
)
596 transition_to_WQM(ctx
, bld
, idx
);
597 else if (ctx
.info
[idx
].block_needs
== Exact
)
598 transition_to_Exact(ctx
, bld
, idx
);
601 if (block
->kind
& block_kind_merge
) {
602 Temp restore
= ctx
.info
[idx
].exec
.back().first
;
603 assert(restore
.size() == bld
.lm
.size());
604 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), restore
);
610 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
612 Operand offset
= instr
->operands
[1];
614 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
615 Temp nonempty
= bld
.sopc(Builder::s_cmp_lg
, bld
.def(s1
, scc
), cur_exec
, Operand(0u));
617 if (offset
.isLiteral())
618 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
620 offset
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.hint_m0(bld
.def(s1
)),
621 offset
, Operand(UINT32_MAX
), bld
.scc(nonempty
));
622 } else if (offset
.isConstant() && offset
.constantValue() > 0xFFFFF) {
623 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.hint_m0(bld
.def(s1
)), offset
);
625 if (!offset
.isConstant())
628 switch (instr
->operands
[2].size()) {
630 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
633 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
636 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
639 unreachable("Invalid SMEM buffer store size");
641 instr
->operands
[1] = offset
;
642 /* as_uniform() needs to be done here so it's done in exact mode and helper
643 * lanes don't contribute. */
644 instr
->operands
[2] = Operand(bld
.as_uniform(instr
->operands
[2]));
647 void process_instructions(exec_ctx
& ctx
, Block
* block
,
648 std::vector
<aco_ptr
<Instruction
>>& instructions
,
652 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
655 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
659 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
660 bool process
= (ctx
.handle_wqm
&&
661 (ctx
.info
[block
->index
].block_needs
& state
) !=
662 (ctx
.info
[block
->index
].block_needs
& (WQM
| Exact
))) ||
663 block
->kind
& block_kind_uses_discard_if
||
664 block
->kind
& block_kind_uses_demote
||
665 block
->kind
& block_kind_needs_lowering
;
667 std::vector
<aco_ptr
<Instruction
>>::iterator it
= std::next(block
->instructions
.begin(), idx
);
668 instructions
.insert(instructions
.end(),
669 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(it
),
670 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(block
->instructions
.end()));
674 Builder
bld(ctx
.program
, &instructions
);
676 for (; idx
< block
->instructions
.size(); idx
++) {
677 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
679 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
681 if (instr
->opcode
== aco_opcode::p_discard_if
) {
682 if (ctx
.info
[block
->index
].block_needs
& Preserve_WQM
) {
683 assert(block
->kind
& block_kind_top_level
);
684 transition_to_WQM(ctx
, bld
, block
->index
);
685 ctx
.info
[block
->index
].exec
.back().second
&= ~mask_type_global
;
687 int num
= ctx
.info
[block
->index
].exec
.size();
689 Operand cond
= instr
->operands
[0];
690 for (int i
= num
- 1; i
>= 0; i
--) {
691 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
692 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
694 andn2
->operands
[0].setFixed(exec
);
695 andn2
->definitions
[0].setFixed(exec
);
698 instr
->opcode
= aco_opcode::p_exit_early_if
;
699 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
701 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
703 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
705 } else if (needs
== WQM
&& state
!= WQM
) {
706 transition_to_WQM(ctx
, bld
, block
->index
);
708 } else if (needs
== Exact
&& state
!= Exact
) {
709 transition_to_Exact(ctx
, bld
, block
->index
);
713 if (instr
->opcode
== aco_opcode::p_is_helper
|| instr
->opcode
== aco_opcode::p_load_helper
) {
714 Definition dst
= instr
->definitions
[0];
715 assert(dst
.size() == bld
.lm
.size());
716 if (state
== Exact
) {
717 instr
.reset(create_instruction
<SOP1_instruction
>(bld
.w64or32(Builder::s_mov
), Format::SOP1
, 1, 1));
718 instr
->operands
[0] = Operand(0u);
719 instr
->definitions
[0] = dst
;
721 std::pair
<Temp
, uint8_t>& exact_mask
= ctx
.info
[block
->index
].exec
[0];
722 if (instr
->opcode
== aco_opcode::p_load_helper
&&
723 !(ctx
.info
[block
->index
].exec
[0].second
& mask_type_initial
)) {
724 /* find last initial exact mask */
725 for (int i
= block
->index
; i
>= 0; i
--) {
726 if (ctx
.program
->blocks
[i
].kind
& block_kind_top_level
&&
727 ctx
.info
[i
].exec
[0].second
& mask_type_initial
) {
728 exact_mask
= ctx
.info
[i
].exec
[0];
734 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
735 assert(exact_mask
.second
& mask_type_exact
);
737 instr
.reset(create_instruction
<SOP2_instruction
>(bld
.w64or32(Builder::s_andn2
), Format::SOP2
, 2, 2));
738 instr
->operands
[0] = Operand(ctx
.info
[block
->index
].exec
.back().first
); /* current exec */
739 instr
->operands
[1] = Operand(exact_mask
.first
);
740 instr
->definitions
[0] = dst
;
741 instr
->definitions
[1] = bld
.def(s1
, scc
);
743 } else if (instr
->opcode
== aco_opcode::p_demote_to_helper
) {
744 /* turn demote into discard_if with only exact masks */
745 assert((ctx
.info
[block
->index
].exec
[0].second
& (mask_type_exact
| mask_type_global
)) == (mask_type_exact
| mask_type_global
));
746 ctx
.info
[block
->index
].exec
[0].second
&= ~mask_type_initial
;
749 Temp cond
, exit_cond
;
750 if (instr
->operands
[0].isConstant()) {
751 assert(instr
->operands
[0].constantValue() == -1u);
752 /* transition to exact and set exec to zero */
753 Temp old_exec
= ctx
.info
[block
->index
].exec
.back().first
;
754 Temp new_exec
= bld
.tmp(bld
.lm
);
755 exit_cond
= bld
.tmp(s1
);
756 cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.scc(Definition(exit_cond
)),
757 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
759 num
= ctx
.info
[block
->index
].exec
.size() - 2;
760 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
) {
761 ctx
.info
[block
->index
].exec
.back().first
= new_exec
;
763 ctx
.info
[block
->index
].exec
.back().first
= cond
;
764 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
767 /* demote_if: transition to exact */
768 transition_to_Exact(ctx
, bld
, block
->index
);
769 assert(instr
->operands
[0].isTemp());
770 cond
= instr
->operands
[0].getTemp();
771 num
= ctx
.info
[block
->index
].exec
.size() - 1;
774 for (int i
= num
; i
>= 0; i
--) {
775 if (ctx
.info
[block
->index
].exec
[i
].second
& mask_type_exact
) {
776 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
777 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
778 if (i
== (int)ctx
.info
[block
->index
].exec
.size() - 1) {
779 andn2
->operands
[0].setFixed(exec
);
780 andn2
->definitions
[0].setFixed(exec
);
783 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
784 exit_cond
= andn2
->definitions
[1].getTemp();
789 instr
->opcode
= aco_opcode::p_exit_early_if
;
790 instr
->operands
[0] = bld
.scc(exit_cond
);
793 } else if (instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
) {
794 bool need_check
= ctx
.info
[block
->index
].exec
.size() != 1 &&
795 !(ctx
.info
[block
->index
].exec
[ctx
.info
[block
->index
].exec
.size() - 2].second
& Exact
);
796 lower_fs_buffer_store_smem(bld
, need_check
, instr
, ctx
.info
[block
->index
].exec
.back().first
);
799 bld
.insert(std::move(instr
));
803 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
805 unsigned idx
= block
->index
;
806 Builder
bld(ctx
.program
, block
);
808 if (idx
== ctx
.program
->blocks
.size() - 1)
811 /* try to disable wqm handling */
812 if (ctx
.handle_wqm
&& block
->kind
& block_kind_top_level
) {
813 if (ctx
.info
[idx
].exec
.size() == 3) {
814 assert(ctx
.info
[idx
].exec
[1].second
== mask_type_wqm
);
815 ctx
.info
[idx
].exec
.pop_back();
817 assert(ctx
.info
[idx
].exec
.size() <= 2);
819 if (ctx
.info
[idx
].ever_again_needs
== 0 ||
820 ctx
.info
[idx
].ever_again_needs
== Exact
) {
821 /* transition to Exact */
822 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
823 block
->instructions
.pop_back();
824 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
825 transition_to_Exact(ctx
, bld
, idx
);
826 bld
.insert(std::move(branch
));
827 ctx
.handle_wqm
= false;
829 } else if (ctx
.info
[idx
].block_needs
& Preserve_WQM
) {
830 /* transition to WQM and remove global flag */
831 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
832 block
->instructions
.pop_back();
833 transition_to_WQM(ctx
, bld
, idx
);
834 ctx
.info
[idx
].exec
.back().second
&= ~mask_type_global
;
835 bld
.insert(std::move(branch
));
839 if (block
->kind
& block_kind_loop_preheader
) {
840 /* collect information about the succeeding loop */
841 bool has_divergent_break
= false;
842 bool has_divergent_continue
= false;
843 bool has_discard
= false;
845 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
847 for (unsigned i
= idx
+ 1; ctx
.program
->blocks
[i
].loop_nest_depth
>= loop_nest_depth
; i
++) {
848 Block
& loop_block
= ctx
.program
->blocks
[i
];
849 needs
|= ctx
.info
[i
].block_needs
;
851 if (loop_block
.kind
& block_kind_uses_discard_if
||
852 loop_block
.kind
& block_kind_discard
||
853 loop_block
.kind
& block_kind_uses_demote
)
855 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
858 if (loop_block
.kind
& block_kind_uniform
)
860 else if (loop_block
.kind
& block_kind_break
)
861 has_divergent_break
= true;
862 else if (loop_block
.kind
& block_kind_continue
)
863 has_divergent_continue
= true;
866 if (ctx
.handle_wqm
) {
868 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
869 block
->instructions
.pop_back();
870 transition_to_WQM(ctx
, bld
, idx
);
871 bld
.insert(std::move(branch
));
873 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
874 block
->instructions
.pop_back();
875 transition_to_Exact(ctx
, bld
, idx
);
876 bld
.insert(std::move(branch
));
880 unsigned num_exec_masks
= ctx
.info
[idx
].exec
.size();
881 if (block
->kind
& block_kind_top_level
)
882 num_exec_masks
= std::min(num_exec_masks
, 2u);
884 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
888 has_divergent_continue
,
892 if (block
->kind
& block_kind_discard
) {
894 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
895 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
896 block
->instructions
.pop_back();
898 /* create a discard_if() instruction with the exec mask as condition */
900 if (ctx
.loop
.size()) {
901 /* if we're in a loop, only discard from the outer exec masks */
902 num
= ctx
.loop
.back().num_exec_masks
;
904 num
= ctx
.info
[idx
].exec
.size() - 1;
907 Temp old_exec
= ctx
.info
[idx
].exec
.back().first
;
908 Temp new_exec
= bld
.tmp(bld
.lm
);
909 Temp cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
910 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
911 ctx
.info
[idx
].exec
.back().first
= new_exec
;
913 for (int i
= num
- 1; i
>= 0; i
--) {
914 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
915 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
916 if (i
== (int)ctx
.info
[idx
].exec
.size() - 1)
917 andn2
->definitions
[0].setFixed(exec
);
919 bld
.pseudo(aco_opcode::p_exit_early_if
, bld
.scc(andn2
->definitions
[1].getTemp()));
920 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
922 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
924 if ((block
->kind
& (block_kind_break
| block_kind_uniform
)) == block_kind_break
)
925 ctx
.info
[idx
].exec
.back().first
= cond
;
926 bld
.insert(std::move(branch
));
927 /* no return here as it can be followed by a divergent break */
930 if (block
->kind
& block_kind_continue_or_break
) {
931 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0]].kind
& block_kind_loop_header
);
932 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[0]].linear_succs
[0]].kind
& block_kind_loop_exit
);
933 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
934 block
->instructions
.pop_back();
936 while (!(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
))
937 ctx
.info
[idx
].exec
.pop_back();
939 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), ctx
.info
[idx
].exec
.back().first
);
940 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.exec(ctx
.info
[idx
].exec
.back().first
), block
->linear_succs
[1], block
->linear_succs
[0]);
944 if (block
->kind
& block_kind_uniform
) {
945 Pseudo_branch_instruction
* branch
= static_cast<Pseudo_branch_instruction
*>(block
->instructions
.back().get());
946 if (branch
->opcode
== aco_opcode::p_branch
) {
947 branch
->target
[0] = block
->linear_succs
[0];
949 branch
->target
[0] = block
->linear_succs
[1];
950 branch
->target
[1] = block
->linear_succs
[0];
955 if (block
->kind
& block_kind_branch
) {
957 if (ctx
.handle_wqm
&&
958 ctx
.info
[idx
].exec
.size() >= 2 &&
959 ctx
.info
[idx
].exec
.back().second
== mask_type_exact
&&
960 !(ctx
.info
[idx
].block_needs
& Exact_Branch
) &&
961 ctx
.info
[idx
].exec
[ctx
.info
[idx
].exec
.size() - 2].second
& mask_type_wqm
) {
962 /* return to wqm before branching */
963 ctx
.info
[idx
].exec
.pop_back();
966 // orig = s_and_saveexec_b64
967 assert(block
->linear_succs
.size() == 2);
968 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_z
);
969 Temp cond
= block
->instructions
.back()->operands
[0].getTemp();
970 block
->instructions
.pop_back();
972 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
973 transition_to_Exact(ctx
, bld
, idx
);
975 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
976 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
978 Temp then_mask
= bld
.tmp(bld
.lm
);
979 Temp old_exec
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
980 bld
.exec(Definition(then_mask
)), cond
, bld
.exec(current_exec
));
982 ctx
.info
[idx
].exec
.back().first
= old_exec
;
984 /* add next current exec to the stack */
985 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
987 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
991 if (block
->kind
& block_kind_invert
) {
992 // exec = s_andn2_b64 (original_exec, exec)
993 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_nz
);
994 block
->instructions
.pop_back();
995 Temp then_mask
= ctx
.info
[idx
].exec
.back().first
;
996 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
;
997 ctx
.info
[idx
].exec
.pop_back();
998 Temp orig_exec
= ctx
.info
[idx
].exec
.back().first
;
999 Temp else_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
, exec
),
1000 bld
.def(s1
, scc
), orig_exec
, bld
.exec(then_mask
));
1002 /* add next current exec to the stack */
1003 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
1005 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1009 if (block
->kind
& block_kind_break
) {
1010 // loop_mask = s_andn2_b64 (loop_mask, exec)
1011 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1012 block
->instructions
.pop_back();
1014 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1016 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1018 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1019 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1020 exec_mask
, current_exec
);
1021 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1022 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1026 /* check if the successor is the merge block, otherwise set exec to 0 */
1027 // TODO: this could be done better by directly branching to the merge block
1028 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1029 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1030 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1031 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1034 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1038 if (block
->kind
& block_kind_continue
) {
1039 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1040 block
->instructions
.pop_back();
1042 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1044 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1045 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1048 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1049 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1050 exec_mask
, bld
.exec(current_exec
));
1051 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1053 assert(cond
!= Temp());
1055 /* check if the successor is the merge block, otherwise set exec to 0 */
1056 // TODO: this could be done better by directly branching to the merge block
1057 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1058 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1059 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1060 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1063 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1068 void process_block(exec_ctx
& ctx
, Block
* block
)
1070 std::vector
<aco_ptr
<Instruction
>> instructions
;
1071 instructions
.reserve(block
->instructions
.size());
1073 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1075 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1076 ctx
.info
[block
->index
].exec
.size() <= 2);
1078 process_instructions(ctx
, block
, instructions
, idx
);
1080 block
->instructions
= std::move(instructions
);
1082 add_branch_code(ctx
, block
);
1084 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1087 } /* end namespace */
1090 void insert_exec_mask(Program
*program
)
1092 exec_ctx
ctx(program
);
1094 if (program
->needs_wqm
&& program
->needs_exact
)
1095 calculate_wqm_needs(ctx
);
1097 for (Block
& block
: program
->blocks
)
1098 process_block(ctx
, &block
);