aco: improve WAR hazard workaround with >64bit stores
[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 <algorithm>
26
27 #include "aco_ir.h"
28 #include <stack>
29
30 namespace aco {
31 namespace {
32
33 struct NOP_ctx_gfx8_9 {
34 enum chip_class chip_class;
35 unsigned vcc_physical;
36
37 /* just initialize these with something less than max NOPs */
38 int VALU_wrexec = -10;
39 int VALU_wrvcc = -10;
40 int VALU_wrsgpr = -10;
41
42 NOP_ctx_gfx8_9(Program* program) : chip_class(program->chip_class) {
43 vcc_physical = program->config->num_sgprs - 2;
44 }
45 };
46
47 struct NOP_ctx_gfx10 {
48 bool has_VOPC = false;
49 bool has_nonVALU_exec_read = false;
50 bool has_VMEM = false;
51 bool has_branch_after_VMEM = false;
52 bool has_DS = false;
53 bool has_branch_after_DS = false;
54 std::bitset<128> sgprs_read_by_VMEM;
55 std::bitset<128> sgprs_read_by_SMEM;
56
57 void join(const NOP_ctx_gfx10 &other) {
58 has_VOPC |= other.has_VOPC;
59 has_nonVALU_exec_read |= other.has_nonVALU_exec_read;
60 has_VMEM |= other.has_VMEM;
61 has_branch_after_VMEM |= other.has_branch_after_VMEM;
62 has_DS |= other.has_DS;
63 has_branch_after_DS |= other.has_branch_after_DS;
64 sgprs_read_by_VMEM |= other.sgprs_read_by_VMEM;
65 sgprs_read_by_SMEM |= other.sgprs_read_by_SMEM;
66 }
67
68 bool operator==(const NOP_ctx_gfx10 &other)
69 {
70 return
71 has_VOPC == other.has_VOPC &&
72 has_nonVALU_exec_read == other.has_nonVALU_exec_read &&
73 has_VMEM == other.has_VMEM &&
74 has_branch_after_VMEM == other.has_branch_after_VMEM &&
75 has_DS == other.has_DS &&
76 has_branch_after_DS == other.has_branch_after_DS &&
77 sgprs_read_by_VMEM == other.sgprs_read_by_VMEM &&
78 sgprs_read_by_SMEM == other.sgprs_read_by_SMEM;
79 }
80 };
81
82 template <std::size_t N>
83 bool check_written_regs(const aco_ptr<Instruction> &instr, const std::bitset<N> &check_regs)
84 {
85 return std::any_of(instr->definitions.begin(), instr->definitions.end(), [&check_regs](const Definition &def) -> bool {
86 bool writes_any = false;
87 for (unsigned i = 0; i < def.size(); i++) {
88 unsigned def_reg = def.physReg() + i;
89 writes_any |= def_reg < check_regs.size() && check_regs[def_reg];
90 }
91 return writes_any;
92 });
93 }
94
95 template <std::size_t N>
96 void mark_read_regs(const aco_ptr<Instruction> &instr, std::bitset<N> &reg_reads)
97 {
98 for (const Operand &op : instr->operands) {
99 for (unsigned i = 0; i < op.size(); i++) {
100 unsigned reg = op.physReg() + i;
101 if (reg < reg_reads.size())
102 reg_reads.set(reg);
103 }
104 }
105 }
106
107 bool VALU_writes_sgpr(aco_ptr<Instruction>& instr)
108 {
109 if ((uint32_t) instr->format & (uint32_t) Format::VOPC)
110 return true;
111 if (instr->isVOP3() && instr->definitions.size() == 2)
112 return true;
113 if (instr->opcode == aco_opcode::v_readfirstlane_b32 || instr->opcode == aco_opcode::v_readlane_b32)
114 return true;
115 return false;
116 }
117
118 bool instr_writes_exec(const aco_ptr<Instruction>& instr)
119 {
120 return std::any_of(instr->definitions.begin(), instr->definitions.end(), [](const Definition &def) -> bool {
121 return def.physReg() == exec_lo || def.physReg() == exec_hi;
122 });
123 }
124
125 bool instr_writes_sgpr(const aco_ptr<Instruction>& instr)
126 {
127 return std::any_of(instr->definitions.begin(), instr->definitions.end(), [](const Definition &def) -> bool {
128 return def.getTemp().type() == RegType::sgpr;
129 });
130 }
131
132 inline bool instr_is_branch(const aco_ptr<Instruction>& instr)
133 {
134 return instr->opcode == aco_opcode::s_branch ||
135 instr->opcode == aco_opcode::s_cbranch_scc0 ||
136 instr->opcode == aco_opcode::s_cbranch_scc1 ||
137 instr->opcode == aco_opcode::s_cbranch_vccz ||
138 instr->opcode == aco_opcode::s_cbranch_vccnz ||
139 instr->opcode == aco_opcode::s_cbranch_execz ||
140 instr->opcode == aco_opcode::s_cbranch_execnz ||
141 instr->opcode == aco_opcode::s_cbranch_cdbgsys ||
142 instr->opcode == aco_opcode::s_cbranch_cdbguser ||
143 instr->opcode == aco_opcode::s_cbranch_cdbgsys_or_user ||
144 instr->opcode == aco_opcode::s_cbranch_cdbgsys_and_user ||
145 instr->opcode == aco_opcode::s_subvector_loop_begin ||
146 instr->opcode == aco_opcode::s_subvector_loop_end ||
147 instr->opcode == aco_opcode::s_setpc_b64 ||
148 instr->opcode == aco_opcode::s_swappc_b64 ||
149 instr->opcode == aco_opcode::s_getpc_b64 ||
150 instr->opcode == aco_opcode::s_call_b64;
151 }
152
153 bool regs_intersect(PhysReg a_reg, unsigned a_size, PhysReg b_reg, unsigned b_size)
154 {
155 return a_reg > b_reg ?
156 (a_reg - b_reg < b_size) :
157 (b_reg - a_reg < a_size);
158 }
159
160 unsigned handle_SMEM_clause(aco_ptr<Instruction>& instr, int new_idx,
161 std::vector<aco_ptr<Instruction>>& new_instructions)
162 {
163 //TODO: s_dcache_inv needs to be in it's own group on GFX10 (and previous versions?)
164 const bool is_store = instr->definitions.empty();
165 for (int pred_idx = new_idx - 1; pred_idx >= 0; pred_idx--) {
166 aco_ptr<Instruction>& pred = new_instructions[pred_idx];
167 if (pred->format != Format::SMEM)
168 break;
169
170 /* Don't allow clauses with store instructions since the clause's
171 * instructions may use the same address. */
172 if (is_store || pred->definitions.empty())
173 return 1;
174
175 Definition& instr_def = instr->definitions[0];
176 Definition& pred_def = pred->definitions[0];
177
178 /* ISA reference doesn't say anything about this, but best to be safe */
179 if (regs_intersect(instr_def.physReg(), instr_def.size(), pred_def.physReg(), pred_def.size()))
180 return 1;
181
182 for (const Operand& op : pred->operands) {
183 if (op.isConstant() || !op.isFixed())
184 continue;
185 if (regs_intersect(instr_def.physReg(), instr_def.size(), op.physReg(), op.size()))
186 return 1;
187 }
188 for (const Operand& op : instr->operands) {
189 if (op.isConstant() || !op.isFixed())
190 continue;
191 if (regs_intersect(pred_def.physReg(), pred_def.size(), op.physReg(), op.size()))
192 return 1;
193 }
194 }
195
196 return 0;
197 }
198
199 int handle_instruction_gfx8_9(NOP_ctx_gfx8_9& ctx, aco_ptr<Instruction>& instr,
200 std::vector<aco_ptr<Instruction>>& old_instructions,
201 std::vector<aco_ptr<Instruction>>& new_instructions)
202 {
203 int new_idx = new_instructions.size();
204
205 // TODO: setreg / getreg / m0 writes
206 // TODO: try to schedule the NOP-causing instruction up to reduce the number of stall cycles
207
208 /* break off from prevous SMEM clause if needed */
209 if (instr->format == Format::SMEM && ctx.chip_class >= GFX8) {
210 return handle_SMEM_clause(instr, new_idx, new_instructions);
211 } else if (instr->isVALU() || instr->format == Format::VINTRP) {
212 int NOPs = 0;
213
214 if (instr->isDPP()) {
215 /* VALU does not forward EXEC to DPP. */
216 if (ctx.VALU_wrexec + 5 >= new_idx)
217 NOPs = 5 + ctx.VALU_wrexec - new_idx + 1;
218
219 /* VALU DPP reads VGPR written by VALU */
220 for (int pred_idx = new_idx - 1; pred_idx >= 0 && pred_idx >= new_idx - 2; pred_idx--) {
221 aco_ptr<Instruction>& pred = new_instructions[pred_idx];
222 if ((pred->isVALU() || pred->format == Format::VINTRP) &&
223 !pred->definitions.empty() &&
224 pred->definitions[0].physReg() == instr->operands[0].physReg()) {
225 NOPs = std::max(NOPs, 2 + pred_idx - new_idx + 1);
226 break;
227 }
228 }
229 }
230
231 /* SALU writes M0 */
232 if (instr->format == Format::VINTRP && new_idx > 0 && ctx.chip_class >= GFX9) {
233 aco_ptr<Instruction>& pred = new_instructions.back();
234 if (pred->isSALU() &&
235 !pred->definitions.empty() &&
236 pred->definitions[0].physReg() == m0)
237 NOPs = std::max(NOPs, 1);
238 }
239
240 for (const Operand& op : instr->operands) {
241 /* VALU which uses VCCZ */
242 if (op.physReg() == PhysReg{251} &&
243 ctx.VALU_wrvcc + 5 >= new_idx)
244 NOPs = std::max(NOPs, 5 + ctx.VALU_wrvcc - new_idx + 1);
245
246 /* VALU which uses EXECZ */
247 if (op.physReg() == PhysReg{252} &&
248 ctx.VALU_wrexec + 5 >= new_idx)
249 NOPs = std::max(NOPs, 5 + ctx.VALU_wrexec - new_idx + 1);
250
251 /* VALU which reads VCC as a constant */
252 if (ctx.VALU_wrvcc + 1 >= new_idx) {
253 for (unsigned k = 0; k < op.size(); k++) {
254 unsigned reg = op.physReg() + k;
255 if (reg == ctx.vcc_physical || reg == ctx.vcc_physical + 1)
256 NOPs = std::max(NOPs, 1);
257 }
258 }
259 }
260
261 switch (instr->opcode) {
262 case aco_opcode::v_readlane_b32:
263 case aco_opcode::v_writelane_b32: {
264 if (ctx.VALU_wrsgpr + 4 < new_idx)
265 break;
266 PhysReg reg = instr->operands[1].physReg();
267 for (int pred_idx = new_idx - 1; pred_idx >= 0 && pred_idx >= new_idx - 4; pred_idx--) {
268 aco_ptr<Instruction>& pred = new_instructions[pred_idx];
269 if (!pred->isVALU() || !VALU_writes_sgpr(pred))
270 continue;
271 for (const Definition& def : pred->definitions) {
272 if (def.physReg() == reg)
273 NOPs = std::max(NOPs, 4 + pred_idx - new_idx + 1);
274 }
275 }
276 break;
277 }
278 case aco_opcode::v_div_fmas_f32:
279 case aco_opcode::v_div_fmas_f64: {
280 if (ctx.VALU_wrvcc + 4 >= new_idx)
281 NOPs = std::max(NOPs, 4 + ctx.VALU_wrvcc - new_idx + 1);
282 break;
283 }
284 default:
285 break;
286 }
287
288 /* Write VGPRs holding writedata > 64 bit from MIMG/MUBUF instructions */
289 // FIXME: handle case if the last instruction of a block without branch is such store
290 if (new_idx > 0) {
291 aco_ptr<Instruction>& pred = new_instructions.back();
292 /* >64-bit MUBUF/MTBUF store with a constant in SOFFSET */
293 bool consider_buf = (pred->format == Format::MUBUF || pred->format == Format::MTBUF) &&
294 pred->operands.size() == 4 &&
295 pred->operands[3].size() > 2 &&
296 pred->operands[2].physReg() >= 128;
297 /* MIMG store with a 128-bit T# with more than two bits set in dmask (making it a >64-bit store) */
298 bool consider_mimg = pred->format == Format::MIMG &&
299 pred->operands.size() == 4 &&
300 pred->operands[3].size() > 2 &&
301 pred->operands[1].size() != 8;
302 /* FLAT/GLOBAL/SCRATCH store with >64-bit data */
303 bool consider_flat = (pred->isFlatOrGlobal() || pred->format == Format::SCRATCH) &&
304 pred->operands.size() == 3 &&
305 pred->operands[2].size() > 2;
306 if (consider_buf || consider_mimg || consider_flat) {
307 PhysReg wrdata = pred->operands[3].physReg();
308 unsigned size = pred->operands[3].size();
309 assert(wrdata >= 256);
310 for (const Definition& def : instr->definitions) {
311 if (regs_intersect(def.physReg(), def.size(), wrdata, size))
312 NOPs = std::max(NOPs, 1);
313 }
314 }
315 }
316
317 if (VALU_writes_sgpr(instr)) {
318 for (const Definition& def : instr->definitions) {
319 if (def.physReg() == vcc)
320 ctx.VALU_wrvcc = NOPs ? new_idx : new_idx + 1;
321 else if (def.physReg() == exec)
322 ctx.VALU_wrexec = NOPs ? new_idx : new_idx + 1;
323 else if (def.physReg() <= 102)
324 ctx.VALU_wrsgpr = NOPs ? new_idx : new_idx + 1;
325 }
326 }
327 return NOPs;
328 } else if (instr->isVMEM() && ctx.VALU_wrsgpr + 5 >= new_idx) {
329 /* If the VALU writes the SGPR that is used by a VMEM, the user must add five wait states. */
330 for (int pred_idx = new_idx - 1; pred_idx >= 0 && pred_idx >= new_idx - 5; pred_idx--) {
331 aco_ptr<Instruction>& pred = new_instructions[pred_idx];
332 if (!(pred->isVALU() && VALU_writes_sgpr(pred)))
333 continue;
334
335 for (const Definition& def : pred->definitions) {
336 if (def.physReg() > 102)
337 continue;
338
339 if (instr->operands.size() > 1 &&
340 regs_intersect(instr->operands[1].physReg(), instr->operands[1].size(),
341 def.physReg(), def.size())) {
342 return 5 + pred_idx - new_idx + 1;
343 }
344
345 if (instr->operands.size() > 2 &&
346 regs_intersect(instr->operands[2].physReg(), instr->operands[2].size(),
347 def.physReg(), def.size())) {
348 return 5 + pred_idx - new_idx + 1;
349 }
350 }
351 }
352 }
353
354 return 0;
355 }
356
357 void handle_block_gfx8_9(NOP_ctx_gfx8_9& ctx, Block& block)
358 {
359 std::vector<aco_ptr<Instruction>> instructions;
360 instructions.reserve(block.instructions.size());
361 for (unsigned i = 0; i < block.instructions.size(); i++) {
362 aco_ptr<Instruction>& instr = block.instructions[i];
363 unsigned NOPs = handle_instruction_gfx8_9(ctx, instr, block.instructions, instructions);
364 if (NOPs) {
365 // TODO: try to move the instruction down
366 /* create NOP */
367 aco_ptr<SOPP_instruction> nop{create_instruction<SOPP_instruction>(aco_opcode::s_nop, Format::SOPP, 0, 0)};
368 nop->imm = NOPs - 1;
369 nop->block = -1;
370 instructions.emplace_back(std::move(nop));
371 }
372
373 instructions.emplace_back(std::move(instr));
374 }
375
376 ctx.VALU_wrvcc -= instructions.size();
377 ctx.VALU_wrexec -= instructions.size();
378 ctx.VALU_wrsgpr -= instructions.size();
379 block.instructions = std::move(instructions);
380 }
381
382 void insert_NOPs_gfx8_9(Program* program)
383 {
384 NOP_ctx_gfx8_9 ctx(program);
385
386 for (Block& block : program->blocks) {
387 if (block.instructions.empty())
388 continue;
389
390 handle_block_gfx8_9(ctx, block);
391 }
392 }
393
394 void handle_instruction_gfx10(Program *program, NOP_ctx_gfx10 &ctx, aco_ptr<Instruction>& instr,
395 std::vector<aco_ptr<Instruction>>& old_instructions,
396 std::vector<aco_ptr<Instruction>>& new_instructions)
397 {
398 /* VMEMtoScalarWriteHazard
399 * Handle EXEC/M0/SGPR write following a VMEM instruction without a VALU or "waitcnt vmcnt(0)" in-between.
400 */
401 if (instr->isVMEM() || instr->format == Format::FLAT || instr->format == Format::GLOBAL ||
402 instr->format == Format::SCRATCH || instr->format == Format::DS) {
403 /* Remember all SGPRs that are read by the VMEM instruction */
404 mark_read_regs(instr, ctx.sgprs_read_by_VMEM);
405 ctx.sgprs_read_by_VMEM.set(exec);
406 if (program->wave_size == 64)
407 ctx.sgprs_read_by_VMEM.set(exec_hi);
408 } else if (instr->isSALU() || instr->format == Format::SMEM) {
409 /* Check if SALU writes an SGPR that was previously read by the VALU */
410 if (check_written_regs(instr, ctx.sgprs_read_by_VMEM)) {
411 ctx.sgprs_read_by_VMEM.reset();
412
413 /* Insert v_nop to mitigate the problem */
414 aco_ptr<VOP1_instruction> nop{create_instruction<VOP1_instruction>(aco_opcode::v_nop, Format::VOP1, 0, 0)};
415 new_instructions.emplace_back(std::move(nop));
416 }
417 } else if (instr->opcode == aco_opcode::s_waitcnt) {
418 /* Hazard is mitigated by "s_waitcnt vmcnt(0)" */
419 uint16_t imm = static_cast<SOPP_instruction*>(instr.get())->imm;
420 unsigned vmcnt = (imm & 0xF) | ((imm & (0x3 << 14)) >> 10);
421 if (vmcnt == 0)
422 ctx.sgprs_read_by_VMEM.reset();
423 } else if (instr->isVALU()) {
424 /* Hazard is mitigated by any VALU instruction */
425 ctx.sgprs_read_by_VMEM.reset();
426 }
427
428 /* VcmpxPermlaneHazard
429 * Handle any permlane following a VOPC instruction, insert v_mov between them.
430 */
431 if (instr->format == Format::VOPC) {
432 ctx.has_VOPC = true;
433 } else if (ctx.has_VOPC &&
434 (instr->opcode == aco_opcode::v_permlane16_b32 ||
435 instr->opcode == aco_opcode::v_permlanex16_b32)) {
436 ctx.has_VOPC = false;
437
438 /* v_nop would be discarded by SQ, so use v_mov with the first operand of the permlane */
439 aco_ptr<VOP1_instruction> v_mov{create_instruction<VOP1_instruction>(aco_opcode::v_mov_b32, Format::VOP1, 1, 1)};
440 v_mov->definitions[0] = Definition(instr->operands[0].physReg(), v1);
441 v_mov->operands[0] = Operand(instr->operands[0].physReg(), v1);
442 new_instructions.emplace_back(std::move(v_mov));
443 } else if (instr->isVALU() && instr->opcode != aco_opcode::v_nop) {
444 ctx.has_VOPC = false;
445 }
446
447 /* VcmpxExecWARHazard
448 * Handle any VALU instruction writing the exec mask after it was read by a non-VALU instruction.
449 */
450 if (!instr->isVALU() && instr->reads_exec()) {
451 ctx.has_nonVALU_exec_read = true;
452 } else if (instr->isVALU()) {
453 if (instr_writes_exec(instr)) {
454 ctx.has_nonVALU_exec_read = false;
455
456 /* Insert s_waitcnt_depctr instruction with magic imm to mitigate the problem */
457 aco_ptr<SOPP_instruction> depctr{create_instruction<SOPP_instruction>(aco_opcode::s_waitcnt_depctr, Format::SOPP, 0, 1)};
458 depctr->imm = 0xfffe;
459 depctr->definitions[0] = Definition(sgpr_null, s1);
460 new_instructions.emplace_back(std::move(depctr));
461 } else if (instr_writes_sgpr(instr)) {
462 /* Any VALU instruction that writes an SGPR mitigates the problem */
463 ctx.has_nonVALU_exec_read = false;
464 }
465 } else if (instr->opcode == aco_opcode::s_waitcnt_depctr) {
466 /* s_waitcnt_depctr can mitigate the problem if it has a magic imm */
467 const SOPP_instruction *sopp = static_cast<const SOPP_instruction *>(instr.get());
468 if ((sopp->imm & 0xfffe) == 0xfffe)
469 ctx.has_nonVALU_exec_read = false;
470 }
471
472 /* SMEMtoVectorWriteHazard
473 * Handle any VALU instruction writing an SGPR after an SMEM reads it.
474 */
475 if (instr->format == Format::SMEM) {
476 /* Remember all SGPRs that are read by the SMEM instruction */
477 mark_read_regs(instr, ctx.sgprs_read_by_SMEM);
478 } else if (VALU_writes_sgpr(instr)) {
479 /* Check if VALU writes an SGPR that was previously read by SMEM */
480 if (check_written_regs(instr, ctx.sgprs_read_by_SMEM)) {
481 ctx.sgprs_read_by_SMEM.reset();
482
483 /* Insert s_mov to mitigate the problem */
484 aco_ptr<SOP1_instruction> s_mov{create_instruction<SOP1_instruction>(aco_opcode::s_mov_b32, Format::SOP1, 1, 1)};
485 s_mov->definitions[0] = Definition(sgpr_null, s1);
486 s_mov->operands[0] = Operand(0u);
487 new_instructions.emplace_back(std::move(s_mov));
488 }
489 } else if (instr->isSALU()) {
490 if (instr->format != Format::SOPP) {
491 /* SALU can mitigate the hazard */
492 ctx.sgprs_read_by_SMEM.reset();
493 } else {
494 /* Reducing lgkmcnt count to 0 always mitigates the hazard. */
495 const SOPP_instruction *sopp = static_cast<const SOPP_instruction *>(instr.get());
496 if (sopp->opcode == aco_opcode::s_waitcnt_lgkmcnt) {
497 if (sopp->imm == 0 && sopp->definitions[0].physReg() == sgpr_null)
498 ctx.sgprs_read_by_SMEM.reset();
499 } else if (sopp->opcode == aco_opcode::s_waitcnt) {
500 unsigned lgkm = (sopp->imm >> 8) & 0x3f;
501 if (lgkm == 0)
502 ctx.sgprs_read_by_SMEM.reset();
503 }
504 }
505 }
506
507 /* LdsBranchVmemWARHazard
508 * Handle VMEM/GLOBAL/SCRATCH->branch->DS and DS->branch->VMEM/GLOBAL/SCRATCH patterns.
509 */
510 if (instr->isVMEM() || instr->format == Format::GLOBAL || instr->format == Format::SCRATCH) {
511 ctx.has_VMEM = true;
512 ctx.has_branch_after_VMEM = false;
513 /* Mitigation for DS is needed only if there was already a branch after */
514 ctx.has_DS = ctx.has_branch_after_DS;
515 } else if (instr->format == Format::DS) {
516 ctx.has_DS = true;
517 ctx.has_branch_after_DS = false;
518 /* Mitigation for VMEM is needed only if there was already a branch after */
519 ctx.has_VMEM = ctx.has_branch_after_VMEM;
520 } else if (instr_is_branch(instr)) {
521 ctx.has_branch_after_VMEM = ctx.has_VMEM;
522 ctx.has_branch_after_DS = ctx.has_DS;
523 } else if (instr->opcode == aco_opcode::s_waitcnt_vscnt) {
524 /* Only s_waitcnt_vscnt can mitigate the hazard */
525 const SOPK_instruction *sopk = static_cast<const SOPK_instruction *>(instr.get());
526 if (sopk->definitions[0].physReg() == sgpr_null && sopk->imm == 0)
527 ctx.has_VMEM = ctx.has_branch_after_VMEM = ctx.has_DS = ctx.has_branch_after_DS = false;
528 }
529 if ((ctx.has_VMEM && ctx.has_branch_after_DS) || (ctx.has_DS && ctx.has_branch_after_VMEM)) {
530 ctx.has_VMEM = ctx.has_branch_after_VMEM = ctx.has_DS = ctx.has_branch_after_DS = false;
531
532 /* Insert s_waitcnt_vscnt to mitigate the problem */
533 aco_ptr<SOPK_instruction> wait{create_instruction<SOPK_instruction>(aco_opcode::s_waitcnt_vscnt, Format::SOPK, 0, 1)};
534 wait->definitions[0] = Definition(sgpr_null, s1);
535 wait->imm = 0;
536 new_instructions.emplace_back(std::move(wait));
537 }
538 }
539
540 void handle_block_gfx10(Program *program, NOP_ctx_gfx10& ctx, Block& block)
541 {
542 if (block.instructions.empty())
543 return;
544
545 std::vector<aco_ptr<Instruction>> instructions;
546 instructions.reserve(block.instructions.size());
547
548 for (aco_ptr<Instruction>& instr : block.instructions) {
549 handle_instruction_gfx10(program, ctx, instr, block.instructions, instructions);
550 instructions.emplace_back(std::move(instr));
551 }
552
553 block.instructions = std::move(instructions);
554 }
555
556 void mitigate_hazards_gfx10(Program *program)
557 {
558 NOP_ctx_gfx10 all_ctx[program->blocks.size()];
559 std::stack<unsigned> loop_header_indices;
560
561 for (unsigned i = 0; i < program->blocks.size(); i++) {
562 Block& block = program->blocks[i];
563 NOP_ctx_gfx10 &ctx = all_ctx[i];
564
565 if (block.kind & block_kind_loop_header) {
566 loop_header_indices.push(i);
567 } else if (block.kind & block_kind_loop_exit) {
568 /* Go through the whole loop again */
569 for (unsigned idx = loop_header_indices.top(); idx < i; idx++) {
570 NOP_ctx_gfx10 loop_block_ctx;
571 for (unsigned b : program->blocks[idx].linear_preds)
572 loop_block_ctx.join(all_ctx[b]);
573
574 handle_block_gfx10(program, loop_block_ctx, program->blocks[idx]);
575
576 /* We only need to continue if the loop header context changed */
577 if (idx == loop_header_indices.top() && loop_block_ctx == all_ctx[idx])
578 break;
579
580 all_ctx[idx] = loop_block_ctx;
581 }
582
583 loop_header_indices.pop();
584 }
585
586 for (unsigned b : block.linear_preds)
587 ctx.join(all_ctx[b]);
588
589 handle_block_gfx10(program, ctx, block);
590 }
591 }
592
593 } /* end namespace */
594
595 void insert_NOPs(Program* program)
596 {
597 if (program->chip_class >= GFX10)
598 mitigate_hazards_gfx10(program);
599 else
600 insert_NOPs_gfx8_9(program);
601 }
602
603 }