2269324e7d43e03f77f9451fddfc1b5d593924a7
[platform/upstream/mesa.git] / src / amd / compiler / aco_insert_NOPs.cpp
1 /*
2  * Copyright © 2019 Valve Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include "aco_builder.h"
26 #include "aco_ir.h"
27
28 #include <algorithm>
29 #include <bitset>
30 #include <set>
31 #include <stack>
32 #include <vector>
33
34 namespace aco {
35 namespace {
36
37 struct State {
38    Program* program;
39    Block* block;
40    std::vector<aco_ptr<Instruction>> old_instructions;
41 };
42
43 struct NOP_ctx_gfx6 {
44    void join(const NOP_ctx_gfx6& other)
45    {
46       set_vskip_mode_then_vector =
47          MAX2(set_vskip_mode_then_vector, other.set_vskip_mode_then_vector);
48       valu_wr_vcc_then_vccz = MAX2(valu_wr_vcc_then_vccz, other.valu_wr_vcc_then_vccz);
49       valu_wr_exec_then_execz = MAX2(valu_wr_exec_then_execz, other.valu_wr_exec_then_execz);
50       valu_wr_vcc_then_div_fmas = MAX2(valu_wr_vcc_then_div_fmas, other.valu_wr_vcc_then_div_fmas);
51       salu_wr_m0_then_gds_msg_ttrace =
52          MAX2(salu_wr_m0_then_gds_msg_ttrace, other.salu_wr_m0_then_gds_msg_ttrace);
53       valu_wr_exec_then_dpp = MAX2(valu_wr_exec_then_dpp, other.valu_wr_exec_then_dpp);
54       salu_wr_m0_then_lds = MAX2(salu_wr_m0_then_lds, other.salu_wr_m0_then_lds);
55       salu_wr_m0_then_moverel = MAX2(salu_wr_m0_then_moverel, other.salu_wr_m0_then_moverel);
56       setreg_then_getsetreg = MAX2(setreg_then_getsetreg, other.setreg_then_getsetreg);
57       vmem_store_then_wr_data |= other.vmem_store_then_wr_data;
58       smem_clause |= other.smem_clause;
59       smem_write |= other.smem_write;
60       for (unsigned i = 0; i < BITSET_WORDS(128); i++) {
61          smem_clause_read_write[i] |= other.smem_clause_read_write[i];
62          smem_clause_write[i] |= other.smem_clause_write[i];
63       }
64    }
65
66    bool operator==(const NOP_ctx_gfx6& other)
67    {
68       return set_vskip_mode_then_vector == other.set_vskip_mode_then_vector &&
69              valu_wr_vcc_then_vccz == other.valu_wr_vcc_then_vccz &&
70              valu_wr_exec_then_execz == other.valu_wr_exec_then_execz &&
71              valu_wr_vcc_then_div_fmas == other.valu_wr_vcc_then_div_fmas &&
72              vmem_store_then_wr_data == other.vmem_store_then_wr_data &&
73              salu_wr_m0_then_gds_msg_ttrace == other.salu_wr_m0_then_gds_msg_ttrace &&
74              valu_wr_exec_then_dpp == other.valu_wr_exec_then_dpp &&
75              salu_wr_m0_then_lds == other.salu_wr_m0_then_lds &&
76              salu_wr_m0_then_moverel == other.salu_wr_m0_then_moverel &&
77              setreg_then_getsetreg == other.setreg_then_getsetreg &&
78              smem_clause == other.smem_clause && smem_write == other.smem_write &&
79              BITSET_EQUAL(smem_clause_read_write, other.smem_clause_read_write) &&
80              BITSET_EQUAL(smem_clause_write, other.smem_clause_write);
81    }
82
83    void add_wait_states(unsigned amount)
84    {
85       if ((set_vskip_mode_then_vector -= amount) < 0)
86          set_vskip_mode_then_vector = 0;
87
88       if ((valu_wr_vcc_then_vccz -= amount) < 0)
89          valu_wr_vcc_then_vccz = 0;
90
91       if ((valu_wr_exec_then_execz -= amount) < 0)
92          valu_wr_exec_then_execz = 0;
93
94       if ((valu_wr_vcc_then_div_fmas -= amount) < 0)
95          valu_wr_vcc_then_div_fmas = 0;
96
97       if ((salu_wr_m0_then_gds_msg_ttrace -= amount) < 0)
98          salu_wr_m0_then_gds_msg_ttrace = 0;
99
100       if ((valu_wr_exec_then_dpp -= amount) < 0)
101          valu_wr_exec_then_dpp = 0;
102
103       if ((salu_wr_m0_then_lds -= amount) < 0)
104          salu_wr_m0_then_lds = 0;
105
106       if ((salu_wr_m0_then_moverel -= amount) < 0)
107          salu_wr_m0_then_moverel = 0;
108
109       if ((setreg_then_getsetreg -= amount) < 0)
110          setreg_then_getsetreg = 0;
111
112       vmem_store_then_wr_data.reset();
113    }
114
115    /* setting MODE.vskip and then any vector op requires 2 wait states */
116    int8_t set_vskip_mode_then_vector = 0;
117
118    /* VALU writing VCC/EXEC and then a VALU reading VCCZ/EXECZ requires 5 wait states */
119    int8_t valu_wr_vcc_then_vccz = 0;
120    int8_t valu_wr_exec_then_execz = 0;
121
122    /* VALU writing VCC followed by v_div_fmas require 4 wait states */
123    int8_t valu_wr_vcc_then_div_fmas = 0;
124
125    /* SALU writing M0 followed by GDS, s_sendmsg or s_ttrace_data requires 1 wait state */
126    int8_t salu_wr_m0_then_gds_msg_ttrace = 0;
127
128    /* VALU writing EXEC followed by DPP requires 5 wait states */
129    int8_t valu_wr_exec_then_dpp = 0;
130
131    /* SALU writing M0 followed by some LDS instructions requires 1 wait state on GFX10 */
132    int8_t salu_wr_m0_then_lds = 0;
133
134    /* SALU writing M0 followed by s_moverel requires 1 wait state on GFX9 */
135    int8_t salu_wr_m0_then_moverel = 0;
136
137    /* s_setreg followed by a s_getreg/s_setreg of the same register needs 2 wait states
138     * currently we don't look at the actual register */
139    int8_t setreg_then_getsetreg = 0;
140
141    /* some memory instructions writing >64bit followed by a instructions
142     * writing the VGPRs holding the writedata requires 1 wait state */
143    std::bitset<256> vmem_store_then_wr_data;
144
145    /* we break up SMEM clauses that contain stores or overwrite an
146     * operand/definition of another instruction in the clause */
147    bool smem_clause = false;
148    bool smem_write = false;
149    BITSET_DECLARE(smem_clause_read_write, 128) = {0};
150    BITSET_DECLARE(smem_clause_write, 128) = {0};
151 };
152
153 struct NOP_ctx_gfx10 {
154    bool has_VOPC_write_exec = false;
155    bool has_nonVALU_exec_read = false;
156    bool has_VMEM = false;
157    bool has_branch_after_VMEM = false;
158    bool has_DS = false;
159    bool has_branch_after_DS = false;
160    bool has_NSA_MIMG = false;
161    bool has_writelane = false;
162    std::bitset<128> sgprs_read_by_VMEM;
163    std::bitset<128> sgprs_read_by_VMEM_store;
164    std::bitset<128> sgprs_read_by_DS;
165    std::bitset<128> sgprs_read_by_SMEM;
166
167    void join(const NOP_ctx_gfx10& other)
168    {
169       has_VOPC_write_exec |= other.has_VOPC_write_exec;
170       has_nonVALU_exec_read |= other.has_nonVALU_exec_read;
171       has_VMEM |= other.has_VMEM;
172       has_branch_after_VMEM |= other.has_branch_after_VMEM;
173       has_DS |= other.has_DS;
174       has_branch_after_DS |= other.has_branch_after_DS;
175       has_NSA_MIMG |= other.has_NSA_MIMG;
176       has_writelane |= other.has_writelane;
177       sgprs_read_by_VMEM |= other.sgprs_read_by_VMEM;
178       sgprs_read_by_DS |= other.sgprs_read_by_DS;
179       sgprs_read_by_VMEM_store |= other.sgprs_read_by_VMEM_store;
180       sgprs_read_by_SMEM |= other.sgprs_read_by_SMEM;
181    }
182
183    bool operator==(const NOP_ctx_gfx10& other)
184    {
185       return has_VOPC_write_exec == other.has_VOPC_write_exec &&
186              has_nonVALU_exec_read == other.has_nonVALU_exec_read && has_VMEM == other.has_VMEM &&
187              has_branch_after_VMEM == other.has_branch_after_VMEM && has_DS == other.has_DS &&
188              has_branch_after_DS == other.has_branch_after_DS &&
189              has_NSA_MIMG == other.has_NSA_MIMG && has_writelane == other.has_writelane &&
190              sgprs_read_by_VMEM == other.sgprs_read_by_VMEM &&
191              sgprs_read_by_DS == other.sgprs_read_by_DS &&
192              sgprs_read_by_VMEM_store == other.sgprs_read_by_VMEM_store &&
193              sgprs_read_by_SMEM == other.sgprs_read_by_SMEM;
194    }
195 };
196
197 template <int Max> struct VGPRCounterMap {
198 public:
199    int base = 0;
200    BITSET_DECLARE(resident, 256);
201    int val[256];
202
203    /* Initializes all counters to Max. */
204    VGPRCounterMap() { BITSET_ZERO(resident); }
205
206    /* Increase all counters, clamping at Max. */
207    void inc() { base++; }
208
209    /* Set counter to 0. */
210    void set(unsigned idx)
211    {
212       val[idx] = -base;
213       BITSET_SET(resident, idx);
214    }
215
216    void set(PhysReg reg, unsigned bytes)
217    {
218       if (reg.reg() < 256)
219          return;
220
221       for (unsigned i = 0; i < DIV_ROUND_UP(bytes, 4); i++)
222          set(reg.reg() - 256 + i);
223    }
224
225    /* Reset all counters to Max. */
226    void reset()
227    {
228       base = 0;
229       BITSET_ZERO(resident);
230    }
231
232    void reset(PhysReg reg, unsigned bytes)
233    {
234       if (reg.reg() < 256)
235          return;
236
237       for (unsigned i = 0; i < DIV_ROUND_UP(bytes, 4); i++)
238          BITSET_CLEAR(resident, reg.reg() - 256 + i);
239    }
240
241    uint8_t get(unsigned idx)
242    {
243       return BITSET_TEST(resident, idx) ? MIN2(val[idx] + base, Max) : Max;
244    }
245
246    uint8_t get(PhysReg reg, unsigned offset = 0)
247    {
248       assert(reg.reg() >= 256);
249       return get(reg.reg() - 256 + offset);
250    }
251
252    void join_min(const VGPRCounterMap& other)
253    {
254       unsigned i;
255       BITSET_FOREACH_SET(i, other.resident, 256)
256       {
257          if (BITSET_TEST(resident, i))
258             val[i] = MIN2(val[i] + base, other.val[i] + other.base) - base;
259          else
260             val[i] = other.val[i] + other.base - base;
261       }
262       BITSET_OR(resident, resident, other.resident);
263    }
264
265    bool operator==(const VGPRCounterMap& other) const
266    {
267       if (!BITSET_EQUAL(resident, other.resident))
268          return false;
269
270       unsigned i;
271       BITSET_FOREACH_SET(i, other.resident, 256)
272       {
273          if (!BITSET_TEST(resident, i))
274             return false;
275          if (val[i] + base != other.val[i] + other.base)
276             return false;
277       }
278       return true;
279    }
280 };
281
282 struct NOP_ctx_gfx11 {
283    /* VcmpxPermlaneHazard */
284    bool has_Vcmpx = false;
285
286    /* LdsDirectVMEMHazard */
287    std::bitset<256> vgpr_used_by_vmem_load;
288    std::bitset<256> vgpr_used_by_vmem_store;
289    std::bitset<256> vgpr_used_by_ds;
290
291    /* VALUTransUseHazard */
292    VGPRCounterMap<15> valu_since_wr_by_trans;
293    VGPRCounterMap<2> trans_since_wr_by_trans;
294
295    /* VALUMaskWriteHazard */
296    std::bitset<128> sgpr_read_by_valu_as_lanemask;
297    std::bitset<128> sgpr_read_by_valu_as_lanemask_then_wr_by_salu;
298
299    void join(const NOP_ctx_gfx11& other)
300    {
301       has_Vcmpx |= other.has_Vcmpx;
302       vgpr_used_by_vmem_load |= other.vgpr_used_by_vmem_load;
303       vgpr_used_by_vmem_store |= other.vgpr_used_by_vmem_store;
304       vgpr_used_by_ds |= other.vgpr_used_by_ds;
305       valu_since_wr_by_trans.join_min(other.valu_since_wr_by_trans);
306       trans_since_wr_by_trans.join_min(other.trans_since_wr_by_trans);
307       sgpr_read_by_valu_as_lanemask |= other.sgpr_read_by_valu_as_lanemask;
308       sgpr_read_by_valu_as_lanemask_then_wr_by_salu |=
309          other.sgpr_read_by_valu_as_lanemask_then_wr_by_salu;
310    }
311
312    bool operator==(const NOP_ctx_gfx11& other)
313    {
314       return has_Vcmpx == other.has_Vcmpx &&
315              vgpr_used_by_vmem_load == other.vgpr_used_by_vmem_load &&
316              vgpr_used_by_vmem_store == other.vgpr_used_by_vmem_store &&
317              vgpr_used_by_ds == other.vgpr_used_by_ds &&
318              valu_since_wr_by_trans == other.valu_since_wr_by_trans &&
319              trans_since_wr_by_trans == other.trans_since_wr_by_trans &&
320              sgpr_read_by_valu_as_lanemask == other.sgpr_read_by_valu_as_lanemask &&
321              sgpr_read_by_valu_as_lanemask_then_wr_by_salu ==
322                 other.sgpr_read_by_valu_as_lanemask_then_wr_by_salu;
323    }
324 };
325
326 int
327 get_wait_states(aco_ptr<Instruction>& instr)
328 {
329    if (instr->opcode == aco_opcode::s_nop)
330       return instr->sopp().imm + 1;
331    else if (instr->opcode == aco_opcode::p_constaddr)
332       return 3; /* lowered to 3 instructions in the assembler */
333    else
334       return 1;
335 }
336
337 bool
338 regs_intersect(PhysReg a_reg, unsigned a_size, PhysReg b_reg, unsigned b_size)
339 {
340    return a_reg > b_reg ? (a_reg - b_reg < b_size) : (b_reg - a_reg < a_size);
341 }
342
343 template <typename GlobalState, typename BlockState,
344           bool (*block_cb)(GlobalState&, BlockState&, Block*),
345           bool (*instr_cb)(GlobalState&, BlockState&, aco_ptr<Instruction>&)>
346 void
347 search_backwards_internal(State& state, GlobalState& global_state, BlockState block_state,
348                           Block* block, bool start_at_end)
349 {
350    if (block == state.block && start_at_end) {
351       /* If it's the current block, block->instructions is incomplete. */
352       for (int pred_idx = state.old_instructions.size() - 1; pred_idx >= 0; pred_idx--) {
353          aco_ptr<Instruction>& instr = state.old_instructions[pred_idx];
354          if (!instr)
355             break; /* Instruction has been moved to block->instructions. */
356          if (instr_cb(global_state, block_state, instr))
357             return;
358       }
359    }
360
361    for (int pred_idx = block->instructions.size() - 1; pred_idx >= 0; pred_idx--) {
362       if (instr_cb(global_state, block_state, block->instructions[pred_idx]))
363          return;
364    }
365
366 PRAGMA_DIAGNOSTIC_PUSH
367 PRAGMA_DIAGNOSTIC_IGNORED(-Waddress)
368    if (block_cb != nullptr && !block_cb(global_state, block_state, block))
369       return;
370 PRAGMA_DIAGNOSTIC_POP
371
372    for (unsigned lin_pred : block->linear_preds) {
373       search_backwards_internal<GlobalState, BlockState, block_cb, instr_cb>(
374          state, global_state, block_state, &state.program->blocks[lin_pred], true);
375    }
376 }
377
378 template <typename GlobalState, typename BlockState,
379           bool (*block_cb)(GlobalState&, BlockState&, Block*),
380           bool (*instr_cb)(GlobalState&, BlockState&, aco_ptr<Instruction>&)>
381 void
382 search_backwards(State& state, GlobalState& global_state, BlockState& block_state)
383 {
384    search_backwards_internal<GlobalState, BlockState, block_cb, instr_cb>(
385       state, global_state, block_state, state.block, false);
386 }
387
388 struct HandleRawHazardGlobalState {
389    PhysReg reg;
390    int nops_needed;
391 };
392
393 struct HandleRawHazardBlockState {
394    uint32_t mask;
395    int nops_needed;
396 };
397
398 template <bool Valu, bool Vintrp, bool Salu>
399 bool
400 handle_raw_hazard_instr(HandleRawHazardGlobalState& global_state,
401                         HandleRawHazardBlockState& block_state, aco_ptr<Instruction>& pred)
402 {
403    unsigned mask_size = util_last_bit(block_state.mask);
404
405    uint32_t writemask = 0;
406    for (Definition& def : pred->definitions) {
407       if (regs_intersect(global_state.reg, mask_size, def.physReg(), def.size())) {
408          unsigned start = def.physReg() > global_state.reg ? def.physReg() - global_state.reg : 0;
409          unsigned end = MIN2(mask_size, start + def.size());
410          writemask |= u_bit_consecutive(start, end - start);
411       }
412    }
413
414    bool is_hazard = writemask != 0 && ((pred->isVALU() && Valu) || (pred->isVINTRP() && Vintrp) ||
415                                        (pred->isSALU() && Salu));
416    if (is_hazard) {
417       global_state.nops_needed = MAX2(global_state.nops_needed, block_state.nops_needed);
418       return true;
419    }
420
421    block_state.mask &= ~writemask;
422    block_state.nops_needed = MAX2(block_state.nops_needed - get_wait_states(pred), 0);
423
424    if (block_state.mask == 0)
425       block_state.nops_needed = 0;
426
427    return block_state.nops_needed == 0;
428 }
429
430 template <bool Valu, bool Vintrp, bool Salu>
431 void
432 handle_raw_hazard(State& state, int* NOPs, int min_states, Operand op)
433 {
434    if (*NOPs >= min_states)
435       return;
436
437    HandleRawHazardGlobalState global = {op.physReg(), 0};
438    HandleRawHazardBlockState block = {u_bit_consecutive(0, op.size()), min_states};
439
440    /* Loops require branch instructions, which count towards the wait
441     * states. So even with loops this should finish unless nops_needed is some
442     * huge value. */
443    search_backwards<HandleRawHazardGlobalState, HandleRawHazardBlockState, nullptr,
444                     handle_raw_hazard_instr<Valu, Vintrp, Salu>>(state, global, block);
445
446    *NOPs = MAX2(*NOPs, global.nops_needed);
447 }
448
449 static auto handle_valu_then_read_hazard = handle_raw_hazard<true, true, false>;
450 static auto handle_vintrp_then_read_hazard = handle_raw_hazard<false, true, false>;
451 static auto handle_valu_salu_then_read_hazard = handle_raw_hazard<true, true, true>;
452
453 void
454 set_bitset_range(BITSET_WORD* words, unsigned start, unsigned size)
455 {
456    unsigned end = start + size - 1;
457    unsigned start_mod = start % BITSET_WORDBITS;
458    if (start_mod + size <= BITSET_WORDBITS) {
459       BITSET_SET_RANGE_INSIDE_WORD(words, start, end);
460    } else {
461       unsigned first_size = BITSET_WORDBITS - start_mod;
462       set_bitset_range(words, start, BITSET_WORDBITS - start_mod);
463       set_bitset_range(words, start + first_size, size - first_size);
464    }
465 }
466
467 bool
468 test_bitset_range(BITSET_WORD* words, unsigned start, unsigned size)
469 {
470    unsigned end = start + size - 1;
471    unsigned start_mod = start % BITSET_WORDBITS;
472    if (start_mod + size <= BITSET_WORDBITS) {
473       return BITSET_TEST_RANGE(words, start, end);
474    } else {
475       unsigned first_size = BITSET_WORDBITS - start_mod;
476       return test_bitset_range(words, start, BITSET_WORDBITS - start_mod) ||
477              test_bitset_range(words, start + first_size, size - first_size);
478    }
479 }
480
481 /* A SMEM clause is any group of consecutive SMEM instructions. The
482  * instructions in this group may return out of order and/or may be replayed.
483  *
484  * To fix this potential hazard correctly, we have to make sure that when a
485  * clause has more than one instruction, no instruction in the clause writes
486  * to a register that is read by another instruction in the clause (including
487  * itself). In this case, we have to break the SMEM clause by inserting non
488  * SMEM instructions.
489  *
490  * SMEM clauses are only present on GFX8+, and only matter when XNACK is set.
491  */
492 void
493 handle_smem_clause_hazards(Program* program, NOP_ctx_gfx6& ctx, aco_ptr<Instruction>& instr,
494                            int* NOPs)
495 {
496    /* break off from previous SMEM clause if needed */
497    if (!*NOPs & (ctx.smem_clause || ctx.smem_write)) {
498       /* Don't allow clauses with store instructions since the clause's
499        * instructions may use the same address. */
500       if (ctx.smem_write || instr->definitions.empty() ||
501           instr_info.is_atomic[(unsigned)instr->opcode]) {
502          *NOPs = 1;
503       } else if (program->dev.xnack_enabled) {
504          for (Operand op : instr->operands) {
505             if (!op.isConstant() &&
506                 test_bitset_range(ctx.smem_clause_write, op.physReg(), op.size())) {
507                *NOPs = 1;
508                break;
509             }
510          }
511
512          Definition def = instr->definitions[0];
513          if (!*NOPs && test_bitset_range(ctx.smem_clause_read_write, def.physReg(), def.size()))
514             *NOPs = 1;
515       }
516    }
517 }
518
519 /* TODO: we don't handle accessing VCC using the actual SGPR instead of using the alias */
520 void
521 handle_instruction_gfx6(State& state, NOP_ctx_gfx6& ctx, aco_ptr<Instruction>& instr,
522                         std::vector<aco_ptr<Instruction>>& new_instructions)
523 {
524    /* check hazards */
525    int NOPs = 0;
526
527    if (instr->isSMEM()) {
528       if (state.program->gfx_level == GFX6) {
529          /* A read of an SGPR by SMRD instruction requires 4 wait states
530           * when the SGPR was written by a VALU instruction. According to LLVM,
531           * there is also an undocumented hardware behavior when the buffer
532           * descriptor is written by a SALU instruction */
533          for (unsigned i = 0; i < instr->operands.size(); i++) {
534             Operand op = instr->operands[i];
535             if (op.isConstant())
536                continue;
537
538             bool is_buffer_desc = i == 0 && op.size() > 2;
539             if (is_buffer_desc)
540                handle_valu_salu_then_read_hazard(state, &NOPs, 4, op);
541             else
542                handle_valu_then_read_hazard(state, &NOPs, 4, op);
543          }
544       }
545
546       handle_smem_clause_hazards(state.program, ctx, instr, &NOPs);
547    } else if (instr->isSALU()) {
548       if (instr->opcode == aco_opcode::s_setreg_b32 ||
549           instr->opcode == aco_opcode::s_setreg_imm32_b32 ||
550           instr->opcode == aco_opcode::s_getreg_b32) {
551          NOPs = MAX2(NOPs, ctx.setreg_then_getsetreg);
552       }
553
554       if (state.program->gfx_level == GFX9) {
555          if (instr->opcode == aco_opcode::s_movrels_b32 ||
556              instr->opcode == aco_opcode::s_movrels_b64 ||
557              instr->opcode == aco_opcode::s_movreld_b32 ||
558              instr->opcode == aco_opcode::s_movreld_b64) {
559             NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_moverel);
560          }
561       }
562
563       if (instr->opcode == aco_opcode::s_sendmsg || instr->opcode == aco_opcode::s_ttracedata)
564          NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_gds_msg_ttrace);
565    } else if (instr->isDS() && instr->ds().gds) {
566       NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_gds_msg_ttrace);
567    } else if (instr->isVALU() || instr->isVINTRP()) {
568       for (Operand op : instr->operands) {
569          if (op.physReg() == vccz)
570             NOPs = MAX2(NOPs, ctx.valu_wr_vcc_then_vccz);
571          if (op.physReg() == execz)
572             NOPs = MAX2(NOPs, ctx.valu_wr_exec_then_execz);
573       }
574
575       if (instr->isDPP()) {
576          NOPs = MAX2(NOPs, ctx.valu_wr_exec_then_dpp);
577          handle_valu_then_read_hazard(state, &NOPs, 2, instr->operands[0]);
578       }
579
580       for (Definition def : instr->definitions) {
581          if (def.regClass().type() != RegType::sgpr) {
582             for (unsigned i = 0; i < def.size(); i++)
583                NOPs = MAX2(NOPs, ctx.vmem_store_then_wr_data[(def.physReg() & 0xff) + i]);
584          }
585       }
586
587       if ((instr->opcode == aco_opcode::v_readlane_b32 ||
588            instr->opcode == aco_opcode::v_readlane_b32_e64 ||
589            instr->opcode == aco_opcode::v_writelane_b32 ||
590            instr->opcode == aco_opcode::v_writelane_b32_e64) &&
591           !instr->operands[1].isConstant()) {
592          handle_valu_then_read_hazard(state, &NOPs, 4, instr->operands[1]);
593       }
594
595       /* It's required to insert 1 wait state if the dst VGPR of any v_interp_*
596        * is followed by a read with v_readfirstlane or v_readlane to fix GPU
597        * hangs on GFX6. Note that v_writelane_* is apparently not affected.
598        * This hazard isn't documented anywhere but AMD confirmed that hazard.
599        */
600       if (state.program->gfx_level == GFX6 &&
601           (instr->opcode == aco_opcode::v_readlane_b32 || /* GFX6 doesn't have v_readlane_b32_e64 */
602            instr->opcode == aco_opcode::v_readfirstlane_b32)) {
603          handle_vintrp_then_read_hazard(state, &NOPs, 1, instr->operands[0]);
604       }
605
606       if (instr->opcode == aco_opcode::v_div_fmas_f32 ||
607           instr->opcode == aco_opcode::v_div_fmas_f64)
608          NOPs = MAX2(NOPs, ctx.valu_wr_vcc_then_div_fmas);
609    } else if (instr->isVMEM() || instr->isFlatLike()) {
610       /* If the VALU writes the SGPR that is used by a VMEM, the user must add five wait states. */
611       for (Operand op : instr->operands) {
612          if (!op.isConstant() && !op.isUndefined() && op.regClass().type() == RegType::sgpr)
613             handle_valu_then_read_hazard(state, &NOPs, 5, op);
614       }
615    }
616
617    if (!instr->isSALU() && instr->format != Format::SMEM)
618       NOPs = MAX2(NOPs, ctx.set_vskip_mode_then_vector);
619
620    if (state.program->gfx_level == GFX9) {
621       bool lds_scratch_global = (instr->isScratch() || instr->isGlobal()) && instr->flatlike().lds;
622       if (instr->isVINTRP() || lds_scratch_global ||
623           instr->opcode == aco_opcode::ds_read_addtid_b32 ||
624           instr->opcode == aco_opcode::ds_write_addtid_b32 ||
625           instr->opcode == aco_opcode::buffer_store_lds_dword) {
626          NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_lds);
627       }
628    }
629
630    ctx.add_wait_states(NOPs + get_wait_states(instr));
631
632    // TODO: try to schedule the NOP-causing instruction up to reduce the number of stall cycles
633    if (NOPs) {
634       /* create NOP */
635       aco_ptr<SOPP_instruction> nop{
636          create_instruction<SOPP_instruction>(aco_opcode::s_nop, Format::SOPP, 0, 0)};
637       nop->imm = NOPs - 1;
638       nop->block = -1;
639       new_instructions.emplace_back(std::move(nop));
640    }
641
642    /* update information to check for later hazards */
643    if ((ctx.smem_clause || ctx.smem_write) && (NOPs || instr->format != Format::SMEM)) {
644       ctx.smem_clause = false;
645       ctx.smem_write = false;
646
647       if (state.program->dev.xnack_enabled) {
648          BITSET_ZERO(ctx.smem_clause_read_write);
649          BITSET_ZERO(ctx.smem_clause_write);
650       }
651    }
652
653    if (instr->isSMEM()) {
654       if (instr->definitions.empty() || instr_info.is_atomic[(unsigned)instr->opcode]) {
655          ctx.smem_write = true;
656       } else {
657          ctx.smem_clause = true;
658
659          if (state.program->dev.xnack_enabled) {
660             for (Operand op : instr->operands) {
661                if (!op.isConstant()) {
662                   set_bitset_range(ctx.smem_clause_read_write, op.physReg(), op.size());
663                }
664             }
665
666             Definition def = instr->definitions[0];
667             set_bitset_range(ctx.smem_clause_read_write, def.physReg(), def.size());
668             set_bitset_range(ctx.smem_clause_write, def.physReg(), def.size());
669          }
670       }
671    } else if (instr->isVALU()) {
672       for (Definition def : instr->definitions) {
673          if (def.regClass().type() == RegType::sgpr) {
674             if (def.physReg() == vcc || def.physReg() == vcc_hi) {
675                ctx.valu_wr_vcc_then_vccz = 5;
676                ctx.valu_wr_vcc_then_div_fmas = 4;
677             }
678             if (def.physReg() == exec || def.physReg() == exec_hi) {
679                ctx.valu_wr_exec_then_execz = 5;
680                ctx.valu_wr_exec_then_dpp = 5;
681             }
682          }
683       }
684    } else if (instr->isSALU() && !instr->definitions.empty()) {
685       if (!instr->definitions.empty()) {
686          /* all other definitions should be SCC */
687          Definition def = instr->definitions[0];
688          if (def.physReg() == m0) {
689             ctx.salu_wr_m0_then_gds_msg_ttrace = 1;
690             ctx.salu_wr_m0_then_lds = 1;
691             ctx.salu_wr_m0_then_moverel = 1;
692          }
693       } else if (instr->opcode == aco_opcode::s_setreg_b32 ||
694                  instr->opcode == aco_opcode::s_setreg_imm32_b32) {
695          SOPK_instruction& sopk = instr->sopk();
696          unsigned offset = (sopk.imm >> 6) & 0x1f;
697          unsigned size = ((sopk.imm >> 11) & 0x1f) + 1;
698          unsigned reg = sopk.imm & 0x3f;
699          ctx.setreg_then_getsetreg = 2;
700
701          if (reg == 1 && offset >= 28 && size > (28 - offset))
702             ctx.set_vskip_mode_then_vector = 2;
703       }
704    } else if (instr->isVMEM() || instr->isFlatLike()) {
705       /* >64-bit MUBUF/MTBUF store with a constant in SOFFSET */
706       bool consider_buf = (instr->isMUBUF() || instr->isMTBUF()) && instr->operands.size() == 4 &&
707                           instr->operands[3].size() > 2 && instr->operands[2].physReg() >= 128;
708       /* MIMG store with a 128-bit T# with more than two bits set in dmask (making it a >64-bit
709        * store) */
710       bool consider_mimg = instr->isMIMG() &&
711                            instr->operands[1].regClass().type() == RegType::vgpr &&
712                            instr->operands[1].size() > 2 && instr->operands[0].size() == 4;
713       /* FLAT/GLOBAL/SCRATCH store with >64-bit data */
714       bool consider_flat =
715          instr->isFlatLike() && instr->operands.size() == 3 && instr->operands[2].size() > 2;
716       if (consider_buf || consider_mimg || consider_flat) {
717          PhysReg wrdata = instr->operands[consider_flat ? 2 : 3].physReg();
718          unsigned size = instr->operands[consider_flat ? 2 : 3].size();
719          for (unsigned i = 0; i < size; i++)
720             ctx.vmem_store_then_wr_data[(wrdata & 0xff) + i] = 1;
721       }
722    }
723 }
724
725 template <std::size_t N>
726 bool
727 check_written_regs(const aco_ptr<Instruction>& instr, const std::bitset<N>& check_regs)
728 {
729    return std::any_of(instr->definitions.begin(), instr->definitions.end(),
730                       [&check_regs](const Definition& def) -> bool
731                       {
732                          bool writes_any = false;
733                          for (unsigned i = 0; i < def.size(); i++) {
734                             unsigned def_reg = def.physReg() + i;
735                             writes_any |= def_reg < check_regs.size() && check_regs[def_reg];
736                          }
737                          return writes_any;
738                       });
739 }
740
741 template <std::size_t N>
742 bool
743 check_read_regs(const aco_ptr<Instruction>& instr, const std::bitset<N>& check_regs)
744 {
745    return std::any_of(instr->operands.begin(), instr->operands.end(),
746                       [&check_regs](const Operand& op) -> bool
747                       {
748                          if (op.isConstant())
749                             return false;
750                          bool writes_any = false;
751                          for (unsigned i = 0; i < op.size(); i++) {
752                             unsigned op_reg = op.physReg() + i;
753                             writes_any |= op_reg < check_regs.size() && check_regs[op_reg];
754                          }
755                          return writes_any;
756                       });
757 }
758
759 template <std::size_t N>
760 void
761 mark_read_regs(const aco_ptr<Instruction>& instr, std::bitset<N>& reg_reads)
762 {
763    for (const Operand& op : instr->operands) {
764       for (unsigned i = 0; i < op.size(); i++) {
765          unsigned reg = op.physReg() + i;
766          if (reg < reg_reads.size())
767             reg_reads.set(reg);
768       }
769    }
770 }
771
772 template <std::size_t N>
773 void
774 mark_read_regs_exec(State& state, const aco_ptr<Instruction>& instr, std::bitset<N>& reg_reads)
775 {
776    mark_read_regs(instr, reg_reads);
777    reg_reads.set(exec);
778    if (state.program->wave_size == 64)
779       reg_reads.set(exec_hi);
780 }
781
782 bool
783 VALU_writes_sgpr(aco_ptr<Instruction>& instr)
784 {
785    if (instr->isVOPC())
786       return true;
787    if (instr->isVOP3() && instr->definitions.size() == 2)
788       return true;
789    if (instr->opcode == aco_opcode::v_readfirstlane_b32 ||
790        instr->opcode == aco_opcode::v_readlane_b32 ||
791        instr->opcode == aco_opcode::v_readlane_b32_e64)
792       return true;
793    return false;
794 }
795
796 bool
797 instr_writes_exec(const aco_ptr<Instruction>& instr)
798 {
799    return std::any_of(instr->definitions.begin(), instr->definitions.end(),
800                       [](const Definition& def) -> bool
801                       { return def.physReg() == exec_lo || def.physReg() == exec_hi; });
802 }
803
804 bool
805 instr_writes_sgpr(const aco_ptr<Instruction>& instr)
806 {
807    return std::any_of(instr->definitions.begin(), instr->definitions.end(),
808                       [](const Definition& def) -> bool
809                       { return def.getTemp().type() == RegType::sgpr; });
810 }
811
812 inline bool
813 instr_is_branch(const aco_ptr<Instruction>& instr)
814 {
815    return instr->opcode == aco_opcode::s_branch || instr->opcode == aco_opcode::s_cbranch_scc0 ||
816           instr->opcode == aco_opcode::s_cbranch_scc1 ||
817           instr->opcode == aco_opcode::s_cbranch_vccz ||
818           instr->opcode == aco_opcode::s_cbranch_vccnz ||
819           instr->opcode == aco_opcode::s_cbranch_execz ||
820           instr->opcode == aco_opcode::s_cbranch_execnz ||
821           instr->opcode == aco_opcode::s_cbranch_cdbgsys ||
822           instr->opcode == aco_opcode::s_cbranch_cdbguser ||
823           instr->opcode == aco_opcode::s_cbranch_cdbgsys_or_user ||
824           instr->opcode == aco_opcode::s_cbranch_cdbgsys_and_user ||
825           instr->opcode == aco_opcode::s_subvector_loop_begin ||
826           instr->opcode == aco_opcode::s_subvector_loop_end ||
827           instr->opcode == aco_opcode::s_setpc_b64 || instr->opcode == aco_opcode::s_swappc_b64 ||
828           instr->opcode == aco_opcode::s_getpc_b64 || instr->opcode == aco_opcode::s_call_b64;
829 }
830
831 void
832 handle_instruction_gfx10(State& state, NOP_ctx_gfx10& ctx, aco_ptr<Instruction>& instr,
833                          std::vector<aco_ptr<Instruction>>& new_instructions)
834 {
835    // TODO: s_dcache_inv needs to be in it's own group on GFX10
836
837    Builder bld(state.program, &new_instructions);
838
839    unsigned vm_vsrc = 7;
840    unsigned sa_sdst = 1;
841    if (debug_flags & DEBUG_FORCE_WAITDEPS) {
842       bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0x0000);
843       vm_vsrc = 0;
844       sa_sdst = 0;
845    } else if (instr->opcode == aco_opcode::s_waitcnt_depctr) {
846       vm_vsrc = (instr->sopp().imm >> 2) & 0x7;
847       sa_sdst = instr->sopp().imm & 0x1;
848    }
849
850    /* VMEMtoScalarWriteHazard
851     * Handle EXEC/M0/SGPR write following a VMEM/DS instruction without a VALU or "waitcnt vmcnt(0)"
852     * in-between.
853     */
854    if (instr->isVMEM() || instr->isFlatLike() || instr->isDS()) {
855       /* Remember all SGPRs that are read by the VMEM/DS instruction */
856       if (instr->isVMEM() || instr->isFlatLike())
857          mark_read_regs_exec(
858             state, instr,
859             instr->definitions.empty() ? ctx.sgprs_read_by_VMEM_store : ctx.sgprs_read_by_VMEM);
860       if (instr->isFlat() || instr->isDS())
861          mark_read_regs_exec(state, instr, ctx.sgprs_read_by_DS);
862    } else if (instr->isSALU() || instr->isSMEM()) {
863       if (instr->opcode == aco_opcode::s_waitcnt) {
864          wait_imm imm(state.program->gfx_level, instr->sopp().imm);
865          if (imm.vm == 0)
866             ctx.sgprs_read_by_VMEM.reset();
867          if (imm.lgkm == 0)
868             ctx.sgprs_read_by_DS.reset();
869       } else if (instr->opcode == aco_opcode::s_waitcnt_vscnt && instr->sopk().imm == 0) {
870          ctx.sgprs_read_by_VMEM_store.reset();
871       } else if (vm_vsrc == 0) {
872          ctx.sgprs_read_by_VMEM.reset();
873          ctx.sgprs_read_by_DS.reset();
874          ctx.sgprs_read_by_VMEM_store.reset();
875       }
876
877       /* Check if SALU writes an SGPR that was previously read by the VALU */
878       if (check_written_regs(instr, ctx.sgprs_read_by_VMEM) ||
879           check_written_regs(instr, ctx.sgprs_read_by_DS) ||
880           check_written_regs(instr, ctx.sgprs_read_by_VMEM_store)) {
881          ctx.sgprs_read_by_VMEM.reset();
882          ctx.sgprs_read_by_DS.reset();
883          ctx.sgprs_read_by_VMEM_store.reset();
884
885          /* Insert s_waitcnt_depctr instruction with magic imm to mitigate the problem */
886          bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0xffe3);
887       }
888    } else if (instr->isVALU()) {
889       /* Hazard is mitigated by any VALU instruction */
890       ctx.sgprs_read_by_VMEM.reset();
891       ctx.sgprs_read_by_DS.reset();
892       ctx.sgprs_read_by_VMEM_store.reset();
893    }
894
895    /* VcmpxPermlaneHazard
896     * Handle any permlane following a VOPC instruction writing exec, insert v_mov between them.
897     */
898    if (instr->isVOPC() && instr->definitions[0].physReg() == exec) {
899       /* we only need to check definitions[0] because since GFX10 v_cmpx only writes one dest */
900       ctx.has_VOPC_write_exec = true;
901    } else if (ctx.has_VOPC_write_exec && (instr->opcode == aco_opcode::v_permlane16_b32 ||
902                                           instr->opcode == aco_opcode::v_permlanex16_b32)) {
903       ctx.has_VOPC_write_exec = false;
904
905       /* v_nop would be discarded by SQ, so use v_mov with the first operand of the permlane */
906       bld.vop1(aco_opcode::v_mov_b32, Definition(instr->operands[0].physReg(), v1),
907                Operand(instr->operands[0].physReg(), v1));
908    } else if (instr->isVALU() && instr->opcode != aco_opcode::v_nop) {
909       ctx.has_VOPC_write_exec = false;
910    }
911
912    /* VcmpxExecWARHazard
913     * Handle any VALU instruction writing the exec mask after it was read by a non-VALU instruction.
914     */
915    if (!instr->isVALU() && instr->reads_exec()) {
916       ctx.has_nonVALU_exec_read = true;
917    } else if (instr->isVALU()) {
918       if (instr_writes_exec(instr)) {
919          ctx.has_nonVALU_exec_read = false;
920
921          /* Insert s_waitcnt_depctr instruction with magic imm to mitigate the problem */
922          bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0xfffe);
923       } else if (instr_writes_sgpr(instr)) {
924          /* Any VALU instruction that writes an SGPR mitigates the problem */
925          ctx.has_nonVALU_exec_read = false;
926       }
927    } else if (sa_sdst == 0) {
928       ctx.has_nonVALU_exec_read = false;
929    }
930
931    /* SMEMtoVectorWriteHazard
932     * Handle any VALU instruction writing an SGPR after an SMEM reads it.
933     */
934    if (instr->isSMEM()) {
935       /* Remember all SGPRs that are read by the SMEM instruction */
936       mark_read_regs(instr, ctx.sgprs_read_by_SMEM);
937    } else if (VALU_writes_sgpr(instr)) {
938       /* Check if VALU writes an SGPR that was previously read by SMEM */
939       if (check_written_regs(instr, ctx.sgprs_read_by_SMEM)) {
940          ctx.sgprs_read_by_SMEM.reset();
941
942          /* Insert s_mov to mitigate the problem */
943          bld.sop1(aco_opcode::s_mov_b32, Definition(sgpr_null, s1), Operand::zero());
944       }
945    } else if (instr->isSALU()) {
946       if (instr->format != Format::SOPP) {
947          /* SALU can mitigate the hazard */
948          ctx.sgprs_read_by_SMEM.reset();
949       } else {
950          /* Reducing lgkmcnt count to 0 always mitigates the hazard. */
951          const SOPP_instruction& sopp = instr->sopp();
952          if (sopp.opcode == aco_opcode::s_waitcnt_lgkmcnt) {
953             if (sopp.imm == 0 && sopp.definitions[0].physReg() == sgpr_null)
954                ctx.sgprs_read_by_SMEM.reset();
955          } else if (sopp.opcode == aco_opcode::s_waitcnt) {
956             wait_imm imm(state.program->gfx_level, instr->sopp().imm);
957             if (imm.lgkm == 0)
958                ctx.sgprs_read_by_SMEM.reset();
959          }
960       }
961    }
962
963    /* LdsBranchVmemWARHazard
964     * Handle VMEM/GLOBAL/SCRATCH->branch->DS and DS->branch->VMEM/GLOBAL/SCRATCH patterns.
965     */
966    if (instr->isVMEM() || instr->isGlobal() || instr->isScratch()) {
967       if (ctx.has_branch_after_DS)
968          bld.sopk(aco_opcode::s_waitcnt_vscnt, Definition(sgpr_null, s1), 0);
969       ctx.has_branch_after_VMEM = ctx.has_branch_after_DS = ctx.has_DS = false;
970       ctx.has_VMEM = true;
971    } else if (instr->isDS()) {
972       if (ctx.has_branch_after_VMEM)
973          bld.sopk(aco_opcode::s_waitcnt_vscnt, Definition(sgpr_null, s1), 0);
974       ctx.has_branch_after_VMEM = ctx.has_branch_after_DS = ctx.has_VMEM = false;
975       ctx.has_DS = true;
976    } else if (instr_is_branch(instr)) {
977       ctx.has_branch_after_VMEM |= ctx.has_VMEM;
978       ctx.has_branch_after_DS |= ctx.has_DS;
979       ctx.has_VMEM = ctx.has_DS = false;
980    } else if (instr->opcode == aco_opcode::s_waitcnt_vscnt) {
981       /* Only s_waitcnt_vscnt can mitigate the hazard */
982       const SOPK_instruction& sopk = instr->sopk();
983       if (sopk.definitions[0].physReg() == sgpr_null && sopk.imm == 0)
984          ctx.has_VMEM = ctx.has_branch_after_VMEM = ctx.has_DS = ctx.has_branch_after_DS = false;
985    }
986
987    /* NSAToVMEMBug
988     * Handles NSA MIMG (4 or more dwords) immediately followed by MUBUF/MTBUF (with offset[2:1] !=
989     * 0).
990     */
991    if (instr->isMIMG() && get_mimg_nsa_dwords(instr.get()) > 1) {
992       ctx.has_NSA_MIMG = true;
993    } else if (ctx.has_NSA_MIMG) {
994       ctx.has_NSA_MIMG = false;
995
996       if (instr->isMUBUF() || instr->isMTBUF()) {
997          uint32_t offset = instr->isMUBUF() ? instr->mubuf().offset : instr->mtbuf().offset;
998          if (offset & 6)
999             bld.sopp(aco_opcode::s_nop, -1, 0);
1000       }
1001    }
1002
1003    /* waNsaCannotFollowWritelane
1004     * Handles NSA MIMG immediately following a v_writelane_b32.
1005     */
1006    if (instr->opcode == aco_opcode::v_writelane_b32_e64) {
1007       ctx.has_writelane = true;
1008    } else if (ctx.has_writelane) {
1009       ctx.has_writelane = false;
1010       if (instr->isMIMG() && get_mimg_nsa_dwords(instr.get()) > 0)
1011          bld.sopp(aco_opcode::s_nop, -1, 0);
1012    }
1013 }
1014
1015 void
1016 fill_vgpr_bitset(std::bitset<256>& set, PhysReg reg, unsigned bytes)
1017 {
1018    if (reg.reg() < 256)
1019       return;
1020    for (unsigned i = 0; i < DIV_ROUND_UP(bytes, 4); i++)
1021       set.set(reg.reg() - 256 + i);
1022 }
1023
1024 /* GFX11 */
1025 unsigned
1026 parse_vdst_wait(aco_ptr<Instruction>& instr)
1027 {
1028    if (instr->isVMEM() || instr->isFlatLike() || instr->isDS() || instr->isEXP())
1029       return 0;
1030    else if (instr->isLDSDIR())
1031       return instr->ldsdir().wait_vdst;
1032    else if (instr->opcode == aco_opcode::s_waitcnt_depctr)
1033       return (instr->sopp().imm >> 12) & 0xf;
1034    else
1035       return 15;
1036 }
1037
1038 struct LdsDirectVALUHazardGlobalState {
1039    unsigned wait_vdst = 15;
1040    PhysReg vgpr;
1041    std::set<unsigned> loop_headers_visited;
1042 };
1043
1044 struct LdsDirectVALUHazardBlockState {
1045    unsigned num_valu = 0;
1046    bool has_trans = false;
1047 };
1048
1049 bool
1050 handle_lds_direct_valu_hazard_instr(LdsDirectVALUHazardGlobalState& global_state,
1051                                     LdsDirectVALUHazardBlockState& block_state,
1052                                     aco_ptr<Instruction>& instr)
1053 {
1054    if (instr->isVALU() || instr->isVINTERP_INREG()) {
1055       instr_class cls = instr_info.classes[(int)instr->opcode];
1056       block_state.has_trans |= cls == instr_class::valu_transcendental32 ||
1057                                cls == instr_class::valu_double_transcendental;
1058
1059       bool uses_vgpr = false;
1060       for (Definition& def : instr->definitions)
1061          uses_vgpr |= regs_intersect(def.physReg(), def.size(), global_state.vgpr, 1);
1062       for (Operand& op : instr->operands) {
1063          uses_vgpr |=
1064             !op.isConstant() && regs_intersect(op.physReg(), op.size(), global_state.vgpr, 1);
1065       }
1066       if (uses_vgpr) {
1067          /* Transcendentals execute in parallel to other VALU and va_vdst count becomes unusable */
1068          global_state.wait_vdst =
1069             MIN2(global_state.wait_vdst, block_state.has_trans ? 0 : block_state.num_valu);
1070          return true;
1071       }
1072
1073       block_state.num_valu++;
1074    }
1075
1076    if (parse_vdst_wait(instr) == 0)
1077       return true;
1078
1079    return block_state.num_valu >= global_state.wait_vdst;
1080 }
1081
1082 bool
1083 handle_lds_direct_valu_hazard_block(LdsDirectVALUHazardGlobalState& global_state,
1084                                     LdsDirectVALUHazardBlockState& block_state, Block* block)
1085 {
1086    if (block->kind & block_kind_loop_header) {
1087       if (global_state.loop_headers_visited.count(block->index))
1088          return false;
1089       global_state.loop_headers_visited.insert(block->index);
1090    }
1091
1092    return true;
1093 }
1094
1095 unsigned
1096 handle_lds_direct_valu_hazard(State& state, aco_ptr<Instruction>& instr)
1097 {
1098    /* LdsDirectVALUHazard
1099     * Handle LDSDIR writing a VGPR after it's used by a VALU instruction.
1100     */
1101    if (instr->ldsdir().wait_vdst == 0)
1102       return 0; /* early exit */
1103
1104    LdsDirectVALUHazardGlobalState global_state;
1105    global_state.wait_vdst = instr->ldsdir().wait_vdst;
1106    global_state.vgpr = instr->definitions[0].physReg();
1107    LdsDirectVALUHazardBlockState block_state;
1108    search_backwards<LdsDirectVALUHazardGlobalState, LdsDirectVALUHazardBlockState,
1109                     &handle_lds_direct_valu_hazard_block, &handle_lds_direct_valu_hazard_instr>(
1110       state, global_state, block_state);
1111    return global_state.wait_vdst;
1112 }
1113
1114 enum VALUPartialForwardingHazardState : uint8_t {
1115    nothing_written,
1116    written_after_exec_write,
1117    exec_written,
1118 };
1119
1120 struct VALUPartialForwardingHazardGlobalState {
1121    bool hazard_found = false;
1122    std::set<unsigned> loop_headers_visited;
1123 };
1124
1125 struct VALUPartialForwardingHazardBlockState {
1126    /* initialized by number of VGPRs read by VALU, decrement when encountered to return early */
1127    uint8_t num_vgprs_read = 0;
1128    BITSET_DECLARE(vgprs_read, 256) = {0};
1129    enum VALUPartialForwardingHazardState state = nothing_written;
1130    unsigned num_valu_since_read = 0;
1131    unsigned num_valu_since_write = 0;
1132 };
1133
1134 bool
1135 handle_valu_partial_forwarding_hazard_instr(VALUPartialForwardingHazardGlobalState& global_state,
1136                                             VALUPartialForwardingHazardBlockState& block_state,
1137                                             aco_ptr<Instruction>& instr)
1138 {
1139    if (instr->isSALU() && !instr->definitions.empty()) {
1140       if (block_state.state == written_after_exec_write && instr_writes_exec(instr))
1141          block_state.state = exec_written;
1142    } else if (instr->isVALU() || instr->isVINTERP_INREG()) {
1143       bool vgpr_write = false;
1144       for (Definition& def : instr->definitions) {
1145          if (def.physReg().reg() < 256)
1146             continue;
1147
1148          for (unsigned i = 0; i < def.size(); i++) {
1149             unsigned reg = def.physReg().reg() - 256 + i;
1150             if (!BITSET_TEST(block_state.vgprs_read, reg))
1151                continue;
1152
1153             if (block_state.state == exec_written && block_state.num_valu_since_write < 3) {
1154                global_state.hazard_found = true;
1155                return true;
1156             }
1157
1158             BITSET_CLEAR(block_state.vgprs_read, reg);
1159             block_state.num_vgprs_read--;
1160             vgpr_write = true;
1161          }
1162       }
1163
1164       if (vgpr_write) {
1165          /* If the state is nothing_written: the check below should ensure that this write is
1166           * close enough to the read.
1167           *
1168           * If the state is exec_written: the current choice of second write has failed. Reset and
1169           * try with the current write as the second one, if it's close enough to the read.
1170           *
1171           * If the state is written_after_exec_write: a further second write would be better, if
1172           * it's close enough to the read.
1173           */
1174          if (block_state.state == nothing_written || block_state.num_valu_since_read < 5) {
1175             block_state.state = written_after_exec_write;
1176             block_state.num_valu_since_write = 0;
1177          } else {
1178             block_state.num_valu_since_write++;
1179          }
1180       } else {
1181          block_state.num_valu_since_write++;
1182       }
1183
1184       block_state.num_valu_since_read++;
1185    } else if (parse_vdst_wait(instr) == 0) {
1186       return true;
1187    }
1188
1189    if (block_state.num_valu_since_read >= (block_state.state == nothing_written ? 5 : 8))
1190       return true; /* Hazard not possible at this distance. */
1191    if (block_state.num_vgprs_read == 0)
1192       return true; /* All VGPRs have been written and a hazard was never found. */
1193
1194    return false;
1195 }
1196
1197 bool
1198 handle_valu_partial_forwarding_hazard_block(VALUPartialForwardingHazardGlobalState& global_state,
1199                                             VALUPartialForwardingHazardBlockState& block_state,
1200                                             Block* block)
1201 {
1202    if (block->kind & block_kind_loop_header) {
1203       if (global_state.loop_headers_visited.count(block->index))
1204          return false;
1205       global_state.loop_headers_visited.insert(block->index);
1206    }
1207
1208    return true;
1209 }
1210
1211 bool
1212 handle_valu_partial_forwarding_hazard(State& state, aco_ptr<Instruction>& instr)
1213 {
1214    /* VALUPartialForwardingHazard
1215     * VALU instruction reads two VGPRs: one written before an exec write by SALU and one after.
1216     * For the hazard, there must be less than 3 VALU between the first and second VGPR writes.
1217     * There also must be less than 5 VALU between the second VGPR write and the current instruction.
1218     */
1219    if (state.program->wave_size != 64 || (!instr->isVALU() && !instr->isVINTERP_INREG()))
1220       return false;
1221
1222    unsigned num_vgprs = 0;
1223    for (Operand& op : instr->operands)
1224       num_vgprs += op.physReg().reg() < 256 ? op.size() : 1;
1225    if (num_vgprs <= 1)
1226       return false; /* early exit */
1227
1228    VALUPartialForwardingHazardBlockState block_state;
1229
1230    for (unsigned i = 0; i < instr->operands.size(); i++) {
1231       Operand& op = instr->operands[i];
1232       if (op.physReg().reg() < 256)
1233          continue;
1234       for (unsigned j = 0; j < op.size(); j++)
1235          BITSET_SET(block_state.vgprs_read, op.physReg().reg() - 256 + j);
1236    }
1237    block_state.num_vgprs_read = BITSET_COUNT(block_state.vgprs_read);
1238
1239    if (block_state.num_vgprs_read <= 1)
1240       return false; /* early exit */
1241
1242    VALUPartialForwardingHazardGlobalState global_state;
1243    search_backwards<VALUPartialForwardingHazardGlobalState, VALUPartialForwardingHazardBlockState,
1244                     &handle_valu_partial_forwarding_hazard_block,
1245                     &handle_valu_partial_forwarding_hazard_instr>(state, global_state, block_state);
1246    return global_state.hazard_found;
1247 }
1248
1249 void
1250 handle_instruction_gfx11(State& state, NOP_ctx_gfx11& ctx, aco_ptr<Instruction>& instr,
1251                          std::vector<aco_ptr<Instruction>>& new_instructions)
1252 {
1253    Builder bld(state.program, &new_instructions);
1254
1255    /* VcmpxPermlaneHazard
1256     * Handle any permlane following a VOPC instruction writing exec, insert v_mov between them.
1257     */
1258    if (instr->isVOPC() && instr->definitions[0].physReg() == exec) {
1259       ctx.has_Vcmpx = true;
1260    } else if (ctx.has_Vcmpx && (instr->opcode == aco_opcode::v_permlane16_b32 ||
1261                                 instr->opcode == aco_opcode::v_permlanex16_b32)) {
1262       ctx.has_Vcmpx = false;
1263
1264       /* v_nop would be discarded by SQ, so use v_mov with the first operand of the permlane */
1265       bld.vop1(aco_opcode::v_mov_b32, Definition(instr->operands[0].physReg(), v1),
1266                Operand(instr->operands[0].physReg(), v1));
1267    } else if (instr->isVALU() && instr->opcode != aco_opcode::v_nop) {
1268       ctx.has_Vcmpx = false;
1269    }
1270
1271    unsigned va_vdst = parse_vdst_wait(instr);
1272    unsigned vm_vsrc = 7;
1273    unsigned sa_sdst = 1;
1274
1275    if (debug_flags & DEBUG_FORCE_WAITDEPS) {
1276       bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0x0000);
1277       va_vdst = 0;
1278       vm_vsrc = 0;
1279       sa_sdst = 0;
1280    } else if (instr->opcode == aco_opcode::s_waitcnt_depctr) {
1281       /* va_vdst already obtained through parse_vdst_wait(). */
1282       vm_vsrc = (instr->sopp().imm >> 2) & 0x7;
1283       sa_sdst = instr->sopp().imm & 0x1;
1284    }
1285
1286    if (instr->isLDSDIR()) {
1287       unsigned count = handle_lds_direct_valu_hazard(state, instr);
1288       LDSDIR_instruction* ldsdir = &instr->ldsdir();
1289       if (count < va_vdst) {
1290          ldsdir->wait_vdst = MIN2(ldsdir->wait_vdst, count);
1291          va_vdst = MIN2(va_vdst, count);
1292       }
1293    }
1294
1295    /* VALUTransUseHazard
1296     * VALU reads VGPR written by transcendental instruction without 6+ VALU or 2+ transcendental
1297     * in-between.
1298     */
1299    if (va_vdst > 0 && (instr->isVALU() || instr->isVINTERP_INREG())) {
1300       uint8_t num_valu = 15;
1301       uint8_t num_trans = 15;
1302       for (Operand& op : instr->operands) {
1303          if (op.physReg().reg() < 256)
1304             continue;
1305          for (unsigned i = 0; i < op.size(); i++) {
1306             num_valu = std::min(num_valu, ctx.valu_since_wr_by_trans.get(op.physReg(), i));
1307             num_trans = std::min(num_trans, ctx.trans_since_wr_by_trans.get(op.physReg(), i));
1308          }
1309       }
1310       if (num_trans <= 1 && num_valu <= 5) {
1311          bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0x0fff);
1312          va_vdst = 0;
1313       }
1314    }
1315
1316    if (va_vdst > 0 && handle_valu_partial_forwarding_hazard(state, instr)) {
1317       bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0x0fff);
1318       va_vdst = 0;
1319    }
1320
1321    /* VALUMaskWriteHazard
1322     * VALU reads SGPR as a lane mask and later written by SALU cannot safely be read by SALU.
1323     */
1324    if (state.program->wave_size == 64 && instr->isSALU() &&
1325        check_written_regs(instr, ctx.sgpr_read_by_valu_as_lanemask)) {
1326       ctx.sgpr_read_by_valu_as_lanemask_then_wr_by_salu = ctx.sgpr_read_by_valu_as_lanemask;
1327       ctx.sgpr_read_by_valu_as_lanemask.reset();
1328    } else if (state.program->wave_size == 64 && instr->isSALU() &&
1329               check_read_regs(instr, ctx.sgpr_read_by_valu_as_lanemask_then_wr_by_salu)) {
1330       bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0xfffe);
1331       sa_sdst = 0;
1332    }
1333
1334    if (va_vdst == 0) {
1335       ctx.valu_since_wr_by_trans.reset();
1336       ctx.trans_since_wr_by_trans.reset();
1337    }
1338
1339    if (sa_sdst == 0)
1340       ctx.sgpr_read_by_valu_as_lanemask_then_wr_by_salu.reset();
1341
1342    if (instr->isVALU() || instr->isVINTERP_INREG()) {
1343       instr_class cls = instr_info.classes[(int)instr->opcode];
1344       bool is_trans = cls == instr_class::valu_transcendental32 ||
1345                       cls == instr_class::valu_double_transcendental;
1346
1347       ctx.valu_since_wr_by_trans.inc();
1348       if (is_trans)
1349          ctx.trans_since_wr_by_trans.inc();
1350
1351       if (is_trans) {
1352          for (Definition& def : instr->definitions) {
1353             ctx.valu_since_wr_by_trans.set(def.physReg(), def.bytes());
1354             ctx.trans_since_wr_by_trans.set(def.physReg(), def.bytes());
1355          }
1356       }
1357
1358       if (state.program->wave_size == 64) {
1359          for (Operand& op : instr->operands) {
1360             if (op.isLiteral() || (!op.isConstant() && op.physReg().reg() < 128))
1361                ctx.sgpr_read_by_valu_as_lanemask.reset();
1362          }
1363          switch (instr->opcode) {
1364          case aco_opcode::v_addc_co_u32:
1365          case aco_opcode::v_subb_co_u32:
1366          case aco_opcode::v_subbrev_co_u32:
1367          case aco_opcode::v_cndmask_b16:
1368          case aco_opcode::v_cndmask_b32:
1369          case aco_opcode::v_div_fmas_f32:
1370          case aco_opcode::v_div_fmas_f64:
1371             if (instr->operands.back().physReg() != exec) {
1372                ctx.sgpr_read_by_valu_as_lanemask.set(instr->operands.back().physReg().reg());
1373                ctx.sgpr_read_by_valu_as_lanemask.set(instr->operands.back().physReg().reg() + 1);
1374             }
1375             break;
1376          default: break;
1377          }
1378       }
1379    }
1380
1381    /* LdsDirectVMEMHazard
1382     * Handle LDSDIR writing a VGPR after it's used by a VMEM/DS instruction.
1383     */
1384    if (instr->isVMEM() || instr->isFlatLike()) {
1385       for (Definition& def : instr->definitions)
1386          fill_vgpr_bitset(ctx.vgpr_used_by_vmem_store, def.physReg(), def.bytes());
1387       if (instr->definitions.empty()) {
1388          for (Operand& op : instr->operands)
1389             fill_vgpr_bitset(ctx.vgpr_used_by_vmem_store, op.physReg(), op.bytes());
1390       } else {
1391          for (Operand& op : instr->operands)
1392             fill_vgpr_bitset(ctx.vgpr_used_by_vmem_load, op.physReg(), op.bytes());
1393       }
1394    }
1395    if (instr->isDS() || instr->isFlat()) {
1396       for (Definition& def : instr->definitions)
1397          fill_vgpr_bitset(ctx.vgpr_used_by_ds, def.physReg(), def.bytes());
1398       for (Operand& op : instr->operands)
1399          fill_vgpr_bitset(ctx.vgpr_used_by_ds, op.physReg(), op.bytes());
1400    }
1401    if (instr->isVALU() || instr->isVINTERP_INREG() || instr->isEXP() || vm_vsrc == 0) {
1402       ctx.vgpr_used_by_vmem_load.reset();
1403       ctx.vgpr_used_by_vmem_store.reset();
1404       ctx.vgpr_used_by_ds.reset();
1405    } else if (instr->opcode == aco_opcode::s_waitcnt) {
1406       wait_imm imm(GFX11, instr->sopp().imm);
1407       if (imm.vm == 0)
1408          ctx.vgpr_used_by_vmem_load.reset();
1409       if (imm.lgkm == 0)
1410          ctx.vgpr_used_by_ds.reset();
1411    } else if (instr->opcode == aco_opcode::s_waitcnt_vscnt && instr->sopk().imm == 0) {
1412       ctx.vgpr_used_by_vmem_store.reset();
1413    }
1414    if (instr->isLDSDIR()) {
1415       if (ctx.vgpr_used_by_vmem_load[instr->definitions[0].physReg().reg() - 256] ||
1416           ctx.vgpr_used_by_vmem_store[instr->definitions[0].physReg().reg() - 256] ||
1417           ctx.vgpr_used_by_ds[instr->definitions[0].physReg().reg() - 256]) {
1418          bld.sopp(aco_opcode::s_waitcnt_depctr, -1, 0xffe3);
1419          ctx.vgpr_used_by_vmem_load.reset();
1420          ctx.vgpr_used_by_vmem_store.reset();
1421          ctx.vgpr_used_by_ds.reset();
1422       }
1423    }
1424 }
1425
1426 template <typename Ctx>
1427 using HandleInstr = void (*)(State& state, Ctx&, aco_ptr<Instruction>&,
1428                              std::vector<aco_ptr<Instruction>>&);
1429
1430 template <typename Ctx, HandleInstr<Ctx> Handle>
1431 void
1432 handle_block(Program* program, Ctx& ctx, Block& block)
1433 {
1434    if (block.instructions.empty())
1435       return;
1436
1437    State state;
1438    state.program = program;
1439    state.block = &block;
1440    state.old_instructions = std::move(block.instructions);
1441
1442    block.instructions.clear(); // Silence clang-analyzer-cplusplus.Move warning
1443    block.instructions.reserve(state.old_instructions.size());
1444
1445    for (aco_ptr<Instruction>& instr : state.old_instructions) {
1446       Handle(state, ctx, instr, block.instructions);
1447       block.instructions.emplace_back(std::move(instr));
1448    }
1449 }
1450
1451 template <typename Ctx, HandleInstr<Ctx> Handle>
1452 void
1453 mitigate_hazards(Program* program)
1454 {
1455    std::vector<Ctx> all_ctx(program->blocks.size());
1456    std::stack<unsigned, std::vector<unsigned>> loop_header_indices;
1457
1458    for (unsigned i = 0; i < program->blocks.size(); i++) {
1459       Block& block = program->blocks[i];
1460       Ctx& ctx = all_ctx[i];
1461
1462       if (block.kind & block_kind_loop_header) {
1463          loop_header_indices.push(i);
1464       } else if (block.kind & block_kind_loop_exit) {
1465          /* Go through the whole loop again */
1466          for (unsigned idx = loop_header_indices.top(); idx < i; idx++) {
1467             Ctx loop_block_ctx;
1468             for (unsigned b : program->blocks[idx].linear_preds)
1469                loop_block_ctx.join(all_ctx[b]);
1470
1471             handle_block<Ctx, Handle>(program, loop_block_ctx, program->blocks[idx]);
1472
1473             /* We only need to continue if the loop header context changed */
1474             if (idx == loop_header_indices.top() && loop_block_ctx == all_ctx[idx])
1475                break;
1476
1477             all_ctx[idx] = loop_block_ctx;
1478          }
1479
1480          loop_header_indices.pop();
1481       }
1482
1483       for (unsigned b : block.linear_preds)
1484          ctx.join(all_ctx[b]);
1485
1486       handle_block<Ctx, Handle>(program, ctx, block);
1487    }
1488 }
1489
1490 } /* end namespace */
1491
1492 void
1493 insert_NOPs(Program* program)
1494 {
1495    if (program->gfx_level >= GFX11)
1496       mitigate_hazards<NOP_ctx_gfx11, handle_instruction_gfx11>(program);
1497    else if (program->gfx_level >= GFX10_3)
1498       ; /* no hazards/bugs to mitigate */
1499    else if (program->gfx_level >= GFX10)
1500       mitigate_hazards<NOP_ctx_gfx10, handle_instruction_gfx10>(program);
1501    else
1502       mitigate_hazards<NOP_ctx_gfx6, handle_instruction_gfx6>(program);
1503 }
1504
1505 } // namespace aco