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 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), exec_mask
);
319 ctx
.info
[idx
].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_wqm
);
322 /* otherwise, the WQM mask should be one below the current mask */
323 ctx
.info
[idx
].exec
.pop_back();
324 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_wqm
);
325 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
326 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
327 ctx
.info
[idx
].exec
.back().first
);
330 void transition_to_Exact(exec_ctx
& ctx
, Builder bld
, unsigned idx
)
332 if (ctx
.info
[idx
].exec
.back().second
& mask_type_exact
)
334 /* We can't remove the loop exec mask, because that can cause exec.size() to
335 * be less than num_exec_masks. The loop exec mask also needs to be kept
336 * around for various uses. */
337 if ((ctx
.info
[idx
].exec
.back().second
& mask_type_global
) &&
338 !(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
)) {
339 ctx
.info
[idx
].exec
.pop_back();
340 assert(ctx
.info
[idx
].exec
.back().second
& mask_type_exact
);
341 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
342 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
343 ctx
.info
[idx
].exec
.back().first
);
346 /* otherwise, we create an exact mask and push to the stack */
347 Temp wqm
= ctx
.info
[idx
].exec
.back().first
;
348 Temp exact
= bld
.tmp(bld
.lm
);
349 wqm
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
350 bld
.exec(Definition(exact
)), ctx
.info
[idx
].exec
[0].first
, bld
.exec(wqm
));
351 ctx
.info
[idx
].exec
.back().first
= wqm
;
352 ctx
.info
[idx
].exec
.emplace_back(exact
, mask_type_exact
);
355 unsigned add_coupling_code(exec_ctx
& ctx
, Block
* block
,
356 std::vector
<aco_ptr
<Instruction
>>& instructions
)
358 unsigned idx
= block
->index
;
359 Builder
bld(ctx
.program
, &instructions
);
360 std::vector
<unsigned>& preds
= block
->linear_preds
;
364 aco_ptr
<Instruction
>& startpgm
= block
->instructions
[0];
365 assert(startpgm
->opcode
== aco_opcode::p_startpgm
);
366 Temp exec_mask
= startpgm
->definitions
.back().getTemp();
367 bld
.insert(std::move(startpgm
));
369 /* exec seems to need to be manually initialized with combined shaders */
370 if (util_bitcount(ctx
.program
->stage
& sw_mask
) > 1) {
371 bld
.sop1(Builder::s_mov
, bld
.exec(Definition(exec_mask
)), bld
.lm
== s2
? Operand(UINT64_MAX
) : Operand(UINT32_MAX
));
372 instructions
[0]->definitions
.pop_back();
375 if (ctx
.handle_wqm
) {
376 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask_type_global
| mask_type_exact
| mask_type_initial
);
377 /* if this block only needs WQM, initialize already */
378 if (ctx
.info
[0].block_needs
== WQM
)
379 transition_to_WQM(ctx
, bld
, 0);
381 uint8_t mask
= mask_type_global
;
382 if (ctx
.program
->needs_wqm
) {
383 exec_mask
= bld
.sop1(Builder::s_wqm
, bld
.def(bld
.lm
, exec
), bld
.def(s1
, scc
), bld
.exec(exec_mask
));
384 mask
|= mask_type_wqm
;
386 mask
|= mask_type_exact
;
388 ctx
.info
[0].exec
.emplace_back(exec_mask
, mask
);
394 /* loop entry block */
395 if (block
->kind
& block_kind_loop_header
) {
396 assert(preds
[0] == idx
- 1);
397 ctx
.info
[idx
].exec
= ctx
.info
[idx
- 1].exec
;
398 loop_info
& info
= ctx
.loop
.back();
399 while (ctx
.info
[idx
].exec
.size() > info
.num_exec_masks
)
400 ctx
.info
[idx
].exec
.pop_back();
402 /* create ssa names for outer exec masks */
403 if (info
.has_discard
) {
404 aco_ptr
<Pseudo_instruction
> phi
;
405 for (int i
= 0; i
< info
.num_exec_masks
- 1; i
++) {
406 phi
.reset(create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1));
407 phi
->definitions
[0] = bld
.def(bld
.lm
);
408 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
[i
].first
);
409 ctx
.info
[idx
].exec
[i
].first
= bld
.insert(std::move(phi
));
413 /* create ssa name for restore mask */
414 if (info
.has_divergent_break
) {
415 /* this phi might be trivial but ensures a parallelcopy on the loop header */
416 aco_ptr
<Pseudo_instruction
> phi
{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
[info
.num_exec_masks
- 1].first
);
419 ctx
.info
[idx
].exec
.back().first
= bld
.insert(std::move(phi
));
422 /* create ssa name for loop active mask */
423 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
424 if (info
.has_divergent_continue
)
425 phi
->definitions
[0] = bld
.def(bld
.lm
);
427 phi
->definitions
[0] = bld
.def(bld
.lm
, exec
);
428 phi
->operands
[0] = Operand(ctx
.info
[preds
[0]].exec
.back().first
);
429 Temp loop_active
= bld
.insert(std::move(phi
));
431 if (info
.has_divergent_break
) {
432 uint8_t mask_type
= (ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
)) | mask_type_loop
;
433 ctx
.info
[idx
].exec
.emplace_back(loop_active
, mask_type
);
435 ctx
.info
[idx
].exec
.back().first
= loop_active
;
436 ctx
.info
[idx
].exec
.back().second
|= mask_type_loop
;
439 /* create a parallelcopy to move the active mask to exec */
441 if (info
.has_divergent_continue
) {
442 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
443 bld
.insert(std::move(block
->instructions
[i
]));
446 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
447 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
448 ctx
.info
[idx
].exec
.emplace_back(bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
449 ctx
.info
[idx
].exec
.back().first
), mask_type
);
455 /* loop exit block */
456 if (block
->kind
& block_kind_loop_exit
) {
457 Block
* header
= ctx
.loop
.back().loop_header
;
458 loop_info
& info
= ctx
.loop
.back();
460 for (ASSERTED
unsigned pred
: preds
)
461 assert(ctx
.info
[pred
].exec
.size() >= info
.num_exec_masks
);
463 /* fill the loop header phis */
464 std::vector
<unsigned>& header_preds
= header
->linear_preds
;
466 if (info
.has_discard
) {
467 while (k
< info
.num_exec_masks
- 1) {
468 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
469 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
470 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
471 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[k
].first
);
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
[info
.num_exec_masks
- 1].first
);
480 if (info
.has_divergent_break
) {
481 aco_ptr
<Instruction
>& phi
= header
->instructions
[k
];
482 assert(phi
->opcode
== aco_opcode::p_linear_phi
);
483 for (unsigned i
= 1; i
< phi
->operands
.size(); i
++)
484 phi
->operands
[i
] = Operand(ctx
.info
[header_preds
[i
]].exec
[info
.num_exec_masks
].first
);
487 assert(!(block
->kind
& block_kind_top_level
) || info
.num_exec_masks
<= 2);
489 /* create the loop exit phis if not trivial */
490 bool need_parallelcopy
= false;
491 for (unsigned k
= 0; k
< info
.num_exec_masks
; k
++) {
492 Temp same
= ctx
.info
[preds
[0]].exec
[k
].first
;
493 uint8_t type
= ctx
.info
[header_preds
[0]].exec
[k
].second
;
496 for (unsigned i
= 1; i
< preds
.size() && trivial
; i
++) {
497 if (ctx
.info
[preds
[i
]].exec
[k
].first
!= same
)
501 if (k
== info
.num_exec_masks
- 1u) {
502 bool all_liveout_exec
= true;
503 bool all_not_liveout_exec
= true;
504 for (unsigned pred
: preds
) {
505 all_liveout_exec
= all_liveout_exec
&& same
== ctx
.program
->blocks
[pred
].live_out_exec
;
506 all_not_liveout_exec
= all_not_liveout_exec
&& same
!= ctx
.program
->blocks
[pred
].live_out_exec
;
508 if (!all_liveout_exec
&& !all_not_liveout_exec
)
510 else if (all_not_liveout_exec
)
511 need_parallelcopy
= true;
513 need_parallelcopy
|= !trivial
;
517 ctx
.info
[idx
].exec
.emplace_back(same
, type
);
519 /* create phi for loop footer */
520 aco_ptr
<Pseudo_instruction
> phi
{create_instruction
<Pseudo_instruction
>(aco_opcode::p_linear_phi
, Format::PSEUDO
, preds
.size(), 1)};
521 phi
->definitions
[0] = bld
.def(bld
.lm
);
522 if (k
== info
.num_exec_masks
- 1) {
523 phi
->definitions
[0].setFixed(exec
);
524 need_parallelcopy
= false;
526 for (unsigned i
= 0; i
< phi
->operands
.size(); i
++)
527 phi
->operands
[i
] = Operand(ctx
.info
[preds
[i
]].exec
[k
].first
);
528 ctx
.info
[idx
].exec
.emplace_back(bld
.insert(std::move(phi
)), type
);
531 assert(ctx
.info
[idx
].exec
.size() == info
.num_exec_masks
);
533 /* create a parallelcopy to move the live mask to exec */
535 while (block
->instructions
[i
]->opcode
!= aco_opcode::p_logical_start
) {
536 bld
.insert(std::move(block
->instructions
[i
]));
540 if (ctx
.handle_wqm
) {
541 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
542 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
543 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
544 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
545 transition_to_Exact(ctx
, bld
, idx
);
546 ctx
.handle_wqm
= false;
549 if (ctx
.info
[idx
].block_needs
== WQM
)
550 transition_to_WQM(ctx
, bld
, idx
);
551 else if (ctx
.info
[idx
].block_needs
== Exact
)
552 transition_to_Exact(ctx
, bld
, idx
);
555 assert(ctx
.info
[idx
].exec
.back().first
.size() == bld
.lm
.size());
556 if (need_parallelcopy
) {
557 /* only create this parallelcopy is needed, since the operand isn't
558 * fixed to exec which causes the spiller to miscalculate register demand */
559 /* TODO: Fix register_demand calculation for spilling on loop exits.
560 * The problem is only mitigated because the register demand could be
561 * higher if the exec phi doesn't get assigned to exec. */
562 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
),
563 ctx
.info
[idx
].exec
.back().first
);
570 if (preds
.size() == 1) {
571 ctx
.info
[idx
].exec
= ctx
.info
[preds
[0]].exec
;
573 assert(preds
.size() == 2);
574 /* if one of the predecessors ends in exact mask, we pop it from stack */
575 unsigned num_exec_masks
= std::min(ctx
.info
[preds
[0]].exec
.size(),
576 ctx
.info
[preds
[1]].exec
.size());
577 if (block
->kind
& block_kind_top_level
&& !(block
->kind
& block_kind_merge
))
578 num_exec_masks
= std::min(num_exec_masks
, 2u);
580 /* create phis for diverged exec masks */
581 for (unsigned i
= 0; i
< num_exec_masks
; i
++) {
582 bool in_exec
= i
== num_exec_masks
- 1 && !(block
->kind
& block_kind_merge
);
583 if (!in_exec
&& ctx
.info
[preds
[0]].exec
[i
].first
== ctx
.info
[preds
[1]].exec
[i
].first
) {
584 assert(ctx
.info
[preds
[0]].exec
[i
].second
== ctx
.info
[preds
[1]].exec
[i
].second
);
585 ctx
.info
[idx
].exec
.emplace_back(ctx
.info
[preds
[0]].exec
[i
]);
589 Temp phi
= bld
.pseudo(aco_opcode::p_linear_phi
, in_exec
? bld
.def(bld
.lm
, exec
) : bld
.def(bld
.lm
),
590 ctx
.info
[preds
[0]].exec
[i
].first
,
591 ctx
.info
[preds
[1]].exec
[i
].first
);
592 uint8_t mask_type
= ctx
.info
[preds
[0]].exec
[i
].second
& ctx
.info
[preds
[1]].exec
[i
].second
;
593 ctx
.info
[idx
].exec
.emplace_back(phi
, mask_type
);
598 while (block
->instructions
[i
]->opcode
== aco_opcode::p_phi
||
599 block
->instructions
[i
]->opcode
== aco_opcode::p_linear_phi
) {
600 bld
.insert(std::move(block
->instructions
[i
]));
604 if (block
->kind
& block_kind_merge
)
605 ctx
.info
[idx
].exec
.pop_back();
607 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 3) {
608 assert(ctx
.info
[idx
].exec
.back().second
== mask_type_exact
);
609 assert(block
->kind
& block_kind_merge
);
610 ctx
.info
[idx
].exec
.pop_back();
613 /* try to satisfy the block's needs */
614 if (ctx
.handle_wqm
) {
615 if (block
->kind
& block_kind_top_level
&& ctx
.info
[idx
].exec
.size() == 2) {
616 if ((ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == 0 ||
617 (ctx
.info
[idx
].block_needs
| ctx
.info
[idx
].ever_again_needs
) == Exact
) {
618 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
619 transition_to_Exact(ctx
, bld
, idx
);
620 ctx
.handle_wqm
= false;
623 if (ctx
.info
[idx
].block_needs
== WQM
)
624 transition_to_WQM(ctx
, bld
, idx
);
625 else if (ctx
.info
[idx
].block_needs
== Exact
)
626 transition_to_Exact(ctx
, bld
, idx
);
629 if (block
->kind
& block_kind_merge
) {
630 Temp restore
= ctx
.info
[idx
].exec
.back().first
;
631 assert(restore
.size() == bld
.lm
.size());
632 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), restore
);
638 void lower_fs_buffer_store_smem(Builder
& bld
, bool need_check
, aco_ptr
<Instruction
>& instr
, Temp cur_exec
)
640 Operand offset
= instr
->operands
[1];
642 /* if exec is zero, then use UINT32_MAX as an offset and make this store a no-op */
643 Temp nonempty
= bld
.sopc(Builder::s_cmp_lg
, bld
.def(s1
, scc
), cur_exec
, Operand(0u));
645 if (offset
.isLiteral())
646 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.def(s1
), offset
);
648 offset
= bld
.sop2(aco_opcode::s_cselect_b32
, bld
.hint_m0(bld
.def(s1
)),
649 offset
, Operand(UINT32_MAX
), bld
.scc(nonempty
));
650 } else if (offset
.isConstant() && offset
.constantValue() > 0xFFFFF) {
651 offset
= bld
.sop1(aco_opcode::s_mov_b32
, bld
.hint_m0(bld
.def(s1
)), offset
);
653 if (!offset
.isConstant())
656 switch (instr
->operands
[2].size()) {
658 instr
->opcode
= aco_opcode::s_buffer_store_dword
;
661 instr
->opcode
= aco_opcode::s_buffer_store_dwordx2
;
664 instr
->opcode
= aco_opcode::s_buffer_store_dwordx4
;
667 unreachable("Invalid SMEM buffer store size");
669 instr
->operands
[1] = offset
;
670 /* as_uniform() needs to be done here so it's done in exact mode and helper
671 * lanes don't contribute. */
672 instr
->operands
[2] = Operand(bld
.as_uniform(instr
->operands
[2]));
675 void process_instructions(exec_ctx
& ctx
, Block
* block
,
676 std::vector
<aco_ptr
<Instruction
>>& instructions
,
680 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_wqm
)
683 assert(!ctx
.handle_wqm
|| ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
);
687 /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
688 bool process
= (ctx
.handle_wqm
&&
689 (ctx
.info
[block
->index
].block_needs
& state
) !=
690 (ctx
.info
[block
->index
].block_needs
& (WQM
| Exact
))) ||
691 block
->kind
& block_kind_uses_discard_if
||
692 block
->kind
& block_kind_uses_demote
||
693 block
->kind
& block_kind_needs_lowering
;
695 std::vector
<aco_ptr
<Instruction
>>::iterator it
= std::next(block
->instructions
.begin(), idx
);
696 instructions
.insert(instructions
.end(),
697 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(it
),
698 std::move_iterator
<std::vector
<aco_ptr
<Instruction
>>::iterator
>(block
->instructions
.end()));
702 Builder
bld(ctx
.program
, &instructions
);
704 for (; idx
< block
->instructions
.size(); idx
++) {
705 aco_ptr
<Instruction
> instr
= std::move(block
->instructions
[idx
]);
707 WQMState needs
= ctx
.handle_wqm
? ctx
.info
[block
->index
].instr_needs
[idx
] : Unspecified
;
709 if (instr
->opcode
== aco_opcode::p_discard_if
) {
710 if (ctx
.info
[block
->index
].block_needs
& Preserve_WQM
) {
711 assert(block
->kind
& block_kind_top_level
);
712 transition_to_WQM(ctx
, bld
, block
->index
);
713 ctx
.info
[block
->index
].exec
.back().second
&= ~mask_type_global
;
715 int num
= ctx
.info
[block
->index
].exec
.size();
717 Operand cond
= instr
->operands
[0];
718 for (int i
= num
- 1; i
>= 0; i
--) {
719 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
720 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
722 andn2
->operands
[0].setFixed(exec
);
723 andn2
->definitions
[0].setFixed(exec
);
726 instr
->opcode
= aco_opcode::p_exit_early_if
;
727 instr
->operands
[0] = bld
.scc(andn2
->definitions
[1].getTemp());
729 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
731 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
733 } else if (needs
== WQM
&& state
!= WQM
) {
734 transition_to_WQM(ctx
, bld
, block
->index
);
736 } else if (needs
== Exact
&& state
!= Exact
) {
737 transition_to_Exact(ctx
, bld
, block
->index
);
741 if (instr
->opcode
== aco_opcode::p_is_helper
|| instr
->opcode
== aco_opcode::p_load_helper
) {
742 Definition dst
= instr
->definitions
[0];
743 assert(dst
.size() == bld
.lm
.size());
744 if (state
== Exact
) {
745 instr
.reset(create_instruction
<SOP1_instruction
>(bld
.w64or32(Builder::s_mov
), Format::SOP1
, 1, 1));
746 instr
->operands
[0] = Operand(0u);
747 instr
->definitions
[0] = dst
;
749 std::pair
<Temp
, uint8_t>& exact_mask
= ctx
.info
[block
->index
].exec
[0];
750 if (instr
->opcode
== aco_opcode::p_load_helper
&&
751 !(ctx
.info
[block
->index
].exec
[0].second
& mask_type_initial
)) {
752 /* find last initial exact mask */
753 for (int i
= block
->index
; i
>= 0; i
--) {
754 if (ctx
.program
->blocks
[i
].kind
& block_kind_top_level
&&
755 ctx
.info
[i
].exec
[0].second
& mask_type_initial
) {
756 exact_mask
= ctx
.info
[i
].exec
[0];
762 assert(instr
->opcode
== aco_opcode::p_is_helper
|| exact_mask
.second
& mask_type_initial
);
763 assert(exact_mask
.second
& mask_type_exact
);
765 instr
.reset(create_instruction
<SOP2_instruction
>(bld
.w64or32(Builder::s_andn2
), Format::SOP2
, 2, 2));
766 instr
->operands
[0] = Operand(ctx
.info
[block
->index
].exec
.back().first
); /* current exec */
767 instr
->operands
[1] = Operand(exact_mask
.first
);
768 instr
->definitions
[0] = dst
;
769 instr
->definitions
[1] = bld
.def(s1
, scc
);
771 } else if (instr
->opcode
== aco_opcode::p_demote_to_helper
) {
772 /* turn demote into discard_if with only exact masks */
773 assert((ctx
.info
[block
->index
].exec
[0].second
& (mask_type_exact
| mask_type_global
)) == (mask_type_exact
| mask_type_global
));
774 ctx
.info
[block
->index
].exec
[0].second
&= ~mask_type_initial
;
777 Temp cond
, exit_cond
;
778 if (instr
->operands
[0].isConstant()) {
779 assert(instr
->operands
[0].constantValue() == -1u);
780 /* transition to exact and set exec to zero */
781 Temp old_exec
= ctx
.info
[block
->index
].exec
.back().first
;
782 Temp new_exec
= bld
.tmp(bld
.lm
);
783 exit_cond
= bld
.tmp(s1
);
784 cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.scc(Definition(exit_cond
)),
785 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
787 num
= ctx
.info
[block
->index
].exec
.size() - 2;
788 if (ctx
.info
[block
->index
].exec
.back().second
& mask_type_exact
) {
789 ctx
.info
[block
->index
].exec
.back().first
= new_exec
;
791 ctx
.info
[block
->index
].exec
.back().first
= cond
;
792 ctx
.info
[block
->index
].exec
.emplace_back(new_exec
, mask_type_exact
);
795 /* demote_if: transition to exact */
796 transition_to_Exact(ctx
, bld
, block
->index
);
797 assert(instr
->operands
[0].isTemp());
798 cond
= instr
->operands
[0].getTemp();
799 num
= ctx
.info
[block
->index
].exec
.size() - 1;
802 for (int i
= num
; i
>= 0; i
--) {
803 if (ctx
.info
[block
->index
].exec
[i
].second
& mask_type_exact
) {
804 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
805 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
806 if (i
== (int)ctx
.info
[block
->index
].exec
.size() - 1) {
807 andn2
->operands
[0].setFixed(exec
);
808 andn2
->definitions
[0].setFixed(exec
);
811 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
812 exit_cond
= andn2
->definitions
[1].getTemp();
817 instr
->opcode
= aco_opcode::p_exit_early_if
;
818 instr
->operands
[0] = bld
.scc(exit_cond
);
821 } else if (instr
->opcode
== aco_opcode::p_fs_buffer_store_smem
) {
822 bool need_check
= ctx
.info
[block
->index
].exec
.size() != 1 &&
823 !(ctx
.info
[block
->index
].exec
[ctx
.info
[block
->index
].exec
.size() - 2].second
& Exact
);
824 lower_fs_buffer_store_smem(bld
, need_check
, instr
, ctx
.info
[block
->index
].exec
.back().first
);
827 bld
.insert(std::move(instr
));
831 void add_branch_code(exec_ctx
& ctx
, Block
* block
)
833 unsigned idx
= block
->index
;
834 Builder
bld(ctx
.program
, block
);
836 if (idx
== ctx
.program
->blocks
.size() - 1)
839 /* try to disable wqm handling */
840 if (ctx
.handle_wqm
&& block
->kind
& block_kind_top_level
) {
841 if (ctx
.info
[idx
].exec
.size() == 3) {
842 assert(ctx
.info
[idx
].exec
[1].second
== mask_type_wqm
);
843 ctx
.info
[idx
].exec
.pop_back();
845 assert(ctx
.info
[idx
].exec
.size() <= 2);
847 if (ctx
.info
[idx
].ever_again_needs
== 0 ||
848 ctx
.info
[idx
].ever_again_needs
== Exact
) {
849 /* transition to Exact */
850 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
851 block
->instructions
.pop_back();
852 ctx
.info
[idx
].exec
.back().second
|= mask_type_global
;
853 transition_to_Exact(ctx
, bld
, idx
);
854 bld
.insert(std::move(branch
));
855 ctx
.handle_wqm
= false;
857 } else if (ctx
.info
[idx
].block_needs
& Preserve_WQM
) {
858 /* transition to WQM and remove global flag */
859 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
860 block
->instructions
.pop_back();
861 transition_to_WQM(ctx
, bld
, idx
);
862 ctx
.info
[idx
].exec
.back().second
&= ~mask_type_global
;
863 bld
.insert(std::move(branch
));
867 if (block
->kind
& block_kind_loop_preheader
) {
868 /* collect information about the succeeding loop */
869 bool has_divergent_break
= false;
870 bool has_divergent_continue
= false;
871 bool has_discard
= false;
873 unsigned loop_nest_depth
= ctx
.program
->blocks
[idx
+ 1].loop_nest_depth
;
875 for (unsigned i
= idx
+ 1; ctx
.program
->blocks
[i
].loop_nest_depth
>= loop_nest_depth
; i
++) {
876 Block
& loop_block
= ctx
.program
->blocks
[i
];
877 needs
|= ctx
.info
[i
].block_needs
;
879 if (loop_block
.kind
& block_kind_uses_discard_if
||
880 loop_block
.kind
& block_kind_discard
||
881 loop_block
.kind
& block_kind_uses_demote
)
883 if (loop_block
.loop_nest_depth
!= loop_nest_depth
)
886 if (loop_block
.kind
& block_kind_uniform
)
888 else if (loop_block
.kind
& block_kind_break
)
889 has_divergent_break
= true;
890 else if (loop_block
.kind
& block_kind_continue
)
891 has_divergent_continue
= true;
894 if (ctx
.handle_wqm
) {
896 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
897 block
->instructions
.pop_back();
898 transition_to_WQM(ctx
, bld
, idx
);
899 bld
.insert(std::move(branch
));
901 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
902 block
->instructions
.pop_back();
903 transition_to_Exact(ctx
, bld
, idx
);
904 bld
.insert(std::move(branch
));
908 unsigned num_exec_masks
= ctx
.info
[idx
].exec
.size();
909 if (block
->kind
& block_kind_top_level
)
910 num_exec_masks
= std::min(num_exec_masks
, 2u);
912 ctx
.loop
.emplace_back(&ctx
.program
->blocks
[block
->linear_succs
[0]],
916 has_divergent_continue
,
920 if (block
->kind
& block_kind_discard
) {
922 assert(block
->instructions
.back()->format
== Format::PSEUDO_BRANCH
);
923 aco_ptr
<Instruction
> branch
= std::move(block
->instructions
.back());
924 block
->instructions
.pop_back();
926 /* create a discard_if() instruction with the exec mask as condition */
928 if (ctx
.loop
.size()) {
929 /* if we're in a loop, only discard from the outer exec masks */
930 num
= ctx
.loop
.back().num_exec_masks
;
932 num
= ctx
.info
[idx
].exec
.size() - 1;
935 Temp old_exec
= ctx
.info
[idx
].exec
.back().first
;
936 Temp new_exec
= bld
.tmp(bld
.lm
);
937 Temp cond
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
938 bld
.exec(Definition(new_exec
)), Operand(0u), bld
.exec(old_exec
));
939 ctx
.info
[idx
].exec
.back().first
= new_exec
;
941 for (int i
= num
- 1; i
>= 0; i
--) {
942 Instruction
*andn2
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
943 ctx
.info
[block
->index
].exec
[i
].first
, cond
);
944 if (i
== (int)ctx
.info
[idx
].exec
.size() - 1)
945 andn2
->definitions
[0].setFixed(exec
);
947 bld
.pseudo(aco_opcode::p_exit_early_if
, bld
.scc(andn2
->definitions
[1].getTemp()));
948 ctx
.info
[block
->index
].exec
[i
].first
= andn2
->definitions
[0].getTemp();
950 assert(!ctx
.handle_wqm
|| (ctx
.info
[block
->index
].exec
[0].second
& mask_type_wqm
) == 0);
952 if ((block
->kind
& (block_kind_break
| block_kind_uniform
)) == block_kind_break
)
953 ctx
.info
[idx
].exec
.back().first
= cond
;
954 bld
.insert(std::move(branch
));
955 /* no return here as it can be followed by a divergent break */
958 if (block
->kind
& block_kind_continue_or_break
) {
959 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0]].kind
& block_kind_loop_header
);
960 assert(ctx
.program
->blocks
[ctx
.program
->blocks
[block
->linear_succs
[0]].linear_succs
[0]].kind
& block_kind_loop_exit
);
961 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
962 block
->instructions
.pop_back();
964 while (!(ctx
.info
[idx
].exec
.back().second
& mask_type_loop
))
965 ctx
.info
[idx
].exec
.pop_back();
967 ctx
.info
[idx
].exec
.back().first
= bld
.pseudo(aco_opcode::p_parallelcopy
, bld
.def(bld
.lm
, exec
), ctx
.info
[idx
].exec
.back().first
);
968 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.exec(ctx
.info
[idx
].exec
.back().first
), block
->linear_succs
[1], block
->linear_succs
[0]);
972 if (block
->kind
& block_kind_uniform
) {
973 Pseudo_branch_instruction
* branch
= static_cast<Pseudo_branch_instruction
*>(block
->instructions
.back().get());
974 if (branch
->opcode
== aco_opcode::p_branch
) {
975 branch
->target
[0] = block
->linear_succs
[0];
977 branch
->target
[0] = block
->linear_succs
[1];
978 branch
->target
[1] = block
->linear_succs
[0];
983 if (block
->kind
& block_kind_branch
) {
985 if (ctx
.handle_wqm
&&
986 ctx
.info
[idx
].exec
.size() >= 2 &&
987 ctx
.info
[idx
].exec
.back().second
== mask_type_exact
&&
988 !(ctx
.info
[idx
].block_needs
& Exact_Branch
) &&
989 ctx
.info
[idx
].exec
[ctx
.info
[idx
].exec
.size() - 2].second
& mask_type_wqm
) {
990 /* return to wqm before branching */
991 ctx
.info
[idx
].exec
.pop_back();
994 // orig = s_and_saveexec_b64
995 assert(block
->linear_succs
.size() == 2);
996 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_z
);
997 Temp cond
= block
->instructions
.back()->operands
[0].getTemp();
998 block
->instructions
.pop_back();
1000 if (ctx
.info
[idx
].block_needs
& Exact_Branch
)
1001 transition_to_Exact(ctx
, bld
, idx
);
1003 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1004 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
& (mask_type_wqm
| mask_type_exact
);
1006 Temp then_mask
= bld
.tmp(bld
.lm
);
1007 Temp old_exec
= bld
.sop1(Builder::s_and_saveexec
, bld
.def(bld
.lm
), bld
.def(s1
, scc
),
1008 bld
.exec(Definition(then_mask
)), cond
, bld
.exec(current_exec
));
1010 ctx
.info
[idx
].exec
.back().first
= old_exec
;
1012 /* add next current exec to the stack */
1013 ctx
.info
[idx
].exec
.emplace_back(then_mask
, mask_type
);
1015 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(then_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1019 if (block
->kind
& block_kind_invert
) {
1020 // exec = s_andn2_b64 (original_exec, exec)
1021 assert(block
->instructions
.back()->opcode
== aco_opcode::p_cbranch_nz
);
1022 block
->instructions
.pop_back();
1023 Temp then_mask
= ctx
.info
[idx
].exec
.back().first
;
1024 uint8_t mask_type
= ctx
.info
[idx
].exec
.back().second
;
1025 ctx
.info
[idx
].exec
.pop_back();
1026 Temp orig_exec
= ctx
.info
[idx
].exec
.back().first
;
1027 Temp else_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
, exec
),
1028 bld
.def(s1
, scc
), orig_exec
, bld
.exec(then_mask
));
1030 /* add next current exec to the stack */
1031 ctx
.info
[idx
].exec
.emplace_back(else_mask
, mask_type
);
1033 bld
.branch(aco_opcode::p_cbranch_z
, bld
.exec(else_mask
), block
->linear_succs
[1], block
->linear_succs
[0]);
1037 if (block
->kind
& block_kind_break
) {
1038 // loop_mask = s_andn2_b64 (loop_mask, exec)
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
--) {
1046 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1047 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1048 exec_mask
, current_exec
);
1049 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1050 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1054 /* check if the successor is the merge block, otherwise set exec to 0 */
1055 // TODO: this could be done better by directly branching to the merge block
1056 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1057 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1058 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1059 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1062 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1066 if (block
->kind
& block_kind_continue
) {
1067 assert(block
->instructions
.back()->opcode
== aco_opcode::p_branch
);
1068 block
->instructions
.pop_back();
1070 Temp current_exec
= ctx
.info
[idx
].exec
.back().first
;
1072 for (int exec_idx
= ctx
.info
[idx
].exec
.size() - 2; exec_idx
>= 0; exec_idx
--) {
1073 if (ctx
.info
[idx
].exec
[exec_idx
].second
& mask_type_loop
)
1076 Temp exec_mask
= ctx
.info
[idx
].exec
[exec_idx
].first
;
1077 exec_mask
= bld
.sop2(Builder::s_andn2
, bld
.def(bld
.lm
), bld
.scc(Definition(cond
)),
1078 exec_mask
, bld
.exec(current_exec
));
1079 ctx
.info
[idx
].exec
[exec_idx
].first
= exec_mask
;
1081 assert(cond
!= Temp());
1083 /* check if the successor is the merge block, otherwise set exec to 0 */
1084 // TODO: this could be done better by directly branching to the merge block
1085 unsigned succ_idx
= ctx
.program
->blocks
[block
->linear_succs
[1]].linear_succs
[0];
1086 Block
& succ
= ctx
.program
->blocks
[succ_idx
];
1087 if (!(succ
.kind
& block_kind_invert
|| succ
.kind
& block_kind_merge
)) {
1088 ctx
.info
[idx
].exec
.back().first
= bld
.sop1(Builder::s_mov
, bld
.def(bld
.lm
, exec
), Operand(0u));
1091 bld
.branch(aco_opcode::p_cbranch_nz
, bld
.scc(cond
), block
->linear_succs
[1], block
->linear_succs
[0]);
1096 void process_block(exec_ctx
& ctx
, Block
* block
)
1098 std::vector
<aco_ptr
<Instruction
>> instructions
;
1099 instructions
.reserve(block
->instructions
.size());
1101 unsigned idx
= add_coupling_code(ctx
, block
, instructions
);
1103 assert(block
->index
!= ctx
.program
->blocks
.size() - 1 ||
1104 ctx
.info
[block
->index
].exec
.size() <= 2);
1106 process_instructions(ctx
, block
, instructions
, idx
);
1108 block
->instructions
= std::move(instructions
);
1110 add_branch_code(ctx
, block
);
1112 block
->live_out_exec
= ctx
.info
[block
->index
].exec
.back().first
;
1115 } /* end namespace */
1118 void insert_exec_mask(Program
*program
)
1120 exec_ctx
ctx(program
);
1122 if (program
->needs_wqm
&& program
->needs_exact
)
1123 calculate_wqm_needs(ctx
);
1125 for (Block
& block
: program
->blocks
)
1126 process_block(ctx
, &block
);