Path: blob/21.2-virgl/src/amd/compiler/aco_insert_NOPs.cpp
4550 views
/*1* Copyright © 2019 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22*/2324#include "aco_builder.h"25#include "aco_ir.h"2627#include <algorithm>28#include <bitset>29#include <stack>30#include <vector>3132namespace aco {33namespace {3435struct NOP_ctx_gfx6 {36void join(const NOP_ctx_gfx6& other)37{38set_vskip_mode_then_vector =39MAX2(set_vskip_mode_then_vector, other.set_vskip_mode_then_vector);40valu_wr_vcc_then_vccz = MAX2(valu_wr_vcc_then_vccz, other.valu_wr_vcc_then_vccz);41valu_wr_exec_then_execz = MAX2(valu_wr_exec_then_execz, other.valu_wr_exec_then_execz);42valu_wr_vcc_then_div_fmas = MAX2(valu_wr_vcc_then_div_fmas, other.valu_wr_vcc_then_div_fmas);43salu_wr_m0_then_gds_msg_ttrace =44MAX2(salu_wr_m0_then_gds_msg_ttrace, other.salu_wr_m0_then_gds_msg_ttrace);45valu_wr_exec_then_dpp = MAX2(valu_wr_exec_then_dpp, other.valu_wr_exec_then_dpp);46salu_wr_m0_then_lds = MAX2(salu_wr_m0_then_lds, other.salu_wr_m0_then_lds);47salu_wr_m0_then_moverel = MAX2(salu_wr_m0_then_moverel, other.salu_wr_m0_then_moverel);48setreg_then_getsetreg = MAX2(setreg_then_getsetreg, other.setreg_then_getsetreg);49vmem_store_then_wr_data |= other.vmem_store_then_wr_data;50smem_clause |= other.smem_clause;51smem_write |= other.smem_write;52for (unsigned i = 0; i < BITSET_WORDS(128); i++) {53smem_clause_read_write[i] |= other.smem_clause_read_write[i];54smem_clause_write[i] |= other.smem_clause_write[i];55}56}5758bool operator==(const NOP_ctx_gfx6& other)59{60return set_vskip_mode_then_vector == other.set_vskip_mode_then_vector &&61valu_wr_vcc_then_vccz == other.valu_wr_vcc_then_vccz &&62valu_wr_exec_then_execz == other.valu_wr_exec_then_execz &&63valu_wr_vcc_then_div_fmas == other.valu_wr_vcc_then_div_fmas &&64vmem_store_then_wr_data == other.vmem_store_then_wr_data &&65salu_wr_m0_then_gds_msg_ttrace == other.salu_wr_m0_then_gds_msg_ttrace &&66valu_wr_exec_then_dpp == other.valu_wr_exec_then_dpp &&67salu_wr_m0_then_lds == other.salu_wr_m0_then_lds &&68salu_wr_m0_then_moverel == other.salu_wr_m0_then_moverel &&69setreg_then_getsetreg == other.setreg_then_getsetreg &&70smem_clause == other.smem_clause && smem_write == other.smem_write &&71BITSET_EQUAL(smem_clause_read_write, other.smem_clause_read_write) &&72BITSET_EQUAL(smem_clause_write, other.smem_clause_write);73}7475void add_wait_states(unsigned amount)76{77if ((set_vskip_mode_then_vector -= amount) < 0)78set_vskip_mode_then_vector = 0;7980if ((valu_wr_vcc_then_vccz -= amount) < 0)81valu_wr_vcc_then_vccz = 0;8283if ((valu_wr_exec_then_execz -= amount) < 0)84valu_wr_exec_then_execz = 0;8586if ((valu_wr_vcc_then_div_fmas -= amount) < 0)87valu_wr_vcc_then_div_fmas = 0;8889if ((salu_wr_m0_then_gds_msg_ttrace -= amount) < 0)90salu_wr_m0_then_gds_msg_ttrace = 0;9192if ((valu_wr_exec_then_dpp -= amount) < 0)93valu_wr_exec_then_dpp = 0;9495if ((salu_wr_m0_then_lds -= amount) < 0)96salu_wr_m0_then_lds = 0;9798if ((salu_wr_m0_then_moverel -= amount) < 0)99salu_wr_m0_then_moverel = 0;100101if ((setreg_then_getsetreg -= amount) < 0)102setreg_then_getsetreg = 0;103104vmem_store_then_wr_data.reset();105}106107/* setting MODE.vskip and then any vector op requires 2 wait states */108int8_t set_vskip_mode_then_vector = 0;109110/* VALU writing VCC/EXEC and then a VALU reading VCCZ/EXECZ requires 5 wait states */111int8_t valu_wr_vcc_then_vccz = 0;112int8_t valu_wr_exec_then_execz = 0;113114/* VALU writing VCC followed by v_div_fmas require 4 wait states */115int8_t valu_wr_vcc_then_div_fmas = 0;116117/* SALU writing M0 followed by GDS, s_sendmsg or s_ttrace_data requires 1 wait state */118int8_t salu_wr_m0_then_gds_msg_ttrace = 0;119120/* VALU writing EXEC followed by DPP requires 5 wait states */121int8_t valu_wr_exec_then_dpp = 0;122123/* SALU writing M0 followed by some LDS instructions requires 1 wait state on GFX10 */124int8_t salu_wr_m0_then_lds = 0;125126/* SALU writing M0 followed by s_moverel requires 1 wait state on GFX9 */127int8_t salu_wr_m0_then_moverel = 0;128129/* s_setreg followed by a s_getreg/s_setreg of the same register needs 2 wait states130* currently we don't look at the actual register */131int8_t setreg_then_getsetreg = 0;132133/* some memory instructions writing >64bit followed by a instructions134* writing the VGPRs holding the writedata requires 1 wait state */135std::bitset<256> vmem_store_then_wr_data;136137/* we break up SMEM clauses that contain stores or overwrite an138* operand/definition of another instruction in the clause */139bool smem_clause = false;140bool smem_write = false;141BITSET_DECLARE(smem_clause_read_write, 128) = {0};142BITSET_DECLARE(smem_clause_write, 128) = {0};143};144145struct NOP_ctx_gfx10 {146bool has_VOPC = false;147bool has_nonVALU_exec_read = false;148bool has_VMEM = false;149bool has_branch_after_VMEM = false;150bool has_DS = false;151bool has_branch_after_DS = false;152bool has_NSA_MIMG = false;153bool has_writelane = false;154std::bitset<128> sgprs_read_by_VMEM;155std::bitset<128> sgprs_read_by_SMEM;156157void join(const NOP_ctx_gfx10& other)158{159has_VOPC |= other.has_VOPC;160has_nonVALU_exec_read |= other.has_nonVALU_exec_read;161has_VMEM |= other.has_VMEM;162has_branch_after_VMEM |= other.has_branch_after_VMEM;163has_DS |= other.has_DS;164has_branch_after_DS |= other.has_branch_after_DS;165has_NSA_MIMG |= other.has_NSA_MIMG;166has_writelane |= other.has_writelane;167sgprs_read_by_VMEM |= other.sgprs_read_by_VMEM;168sgprs_read_by_SMEM |= other.sgprs_read_by_SMEM;169}170171bool operator==(const NOP_ctx_gfx10& other)172{173return has_VOPC == other.has_VOPC && has_nonVALU_exec_read == other.has_nonVALU_exec_read &&174has_VMEM == other.has_VMEM && has_branch_after_VMEM == other.has_branch_after_VMEM &&175has_DS == other.has_DS && has_branch_after_DS == other.has_branch_after_DS &&176has_NSA_MIMG == other.has_NSA_MIMG && has_writelane == other.has_writelane &&177sgprs_read_by_VMEM == other.sgprs_read_by_VMEM &&178sgprs_read_by_SMEM == other.sgprs_read_by_SMEM;179}180};181182int183get_wait_states(aco_ptr<Instruction>& instr)184{185if (instr->opcode == aco_opcode::s_nop)186return instr->sopp().imm + 1;187else if (instr->opcode == aco_opcode::p_constaddr)188return 3; /* lowered to 3 instructions in the assembler */189else190return 1;191}192193bool194regs_intersect(PhysReg a_reg, unsigned a_size, PhysReg b_reg, unsigned b_size)195{196return a_reg > b_reg ? (a_reg - b_reg < b_size) : (b_reg - a_reg < a_size);197}198199template <bool Valu, bool Vintrp, bool Salu>200int201handle_raw_hazard_internal(Program* program, Block* block, int nops_needed, PhysReg reg,202uint32_t mask)203{204unsigned mask_size = util_last_bit(mask);205for (int pred_idx = block->instructions.size() - 1; pred_idx >= 0; pred_idx--) {206aco_ptr<Instruction>& pred = block->instructions[pred_idx];207208uint32_t writemask = 0;209for (Definition& def : pred->definitions) {210if (regs_intersect(reg, mask_size, def.physReg(), def.size())) {211unsigned start = def.physReg() > reg ? def.physReg() - reg : 0;212unsigned end = MIN2(mask_size, start + def.size());213writemask |= u_bit_consecutive(start, end - start);214}215}216217bool is_hazard = writemask != 0 && ((pred->isVALU() && Valu) ||218(pred->isVINTRP() && Vintrp) || (pred->isSALU() && Salu));219if (is_hazard)220return nops_needed;221222mask &= ~writemask;223nops_needed -= get_wait_states(pred);224225if (nops_needed <= 0 || mask == 0)226return 0;227}228229int res = 0;230231/* Loops require branch instructions, which count towards the wait232* states. So even with loops this should finish unless nops_needed is some233* huge value. */234for (unsigned lin_pred : block->linear_preds) {235res = std::max(res, handle_raw_hazard_internal<Valu, Vintrp, Salu>(236program, &program->blocks[lin_pred], nops_needed, reg, mask));237}238return res;239}240241template <bool Valu, bool Vintrp, bool Salu>242void243handle_raw_hazard(Program* program, Block* cur_block, int* NOPs, int min_states, Operand op)244{245if (*NOPs >= min_states)246return;247int res = handle_raw_hazard_internal<Valu, Vintrp, Salu>(248program, cur_block, min_states, op.physReg(), u_bit_consecutive(0, op.size()));249*NOPs = MAX2(*NOPs, res);250}251252static auto handle_valu_then_read_hazard = handle_raw_hazard<true, true, false>;253static auto handle_vintrp_then_read_hazard = handle_raw_hazard<false, true, false>;254static auto handle_valu_salu_then_read_hazard = handle_raw_hazard<true, true, true>;255256void257set_bitset_range(BITSET_WORD* words, unsigned start, unsigned size)258{259unsigned end = start + size - 1;260unsigned start_mod = start % BITSET_WORDBITS;261if (start_mod + size <= BITSET_WORDBITS) {262BITSET_SET_RANGE(words, start, end);263} else {264unsigned first_size = BITSET_WORDBITS - start_mod;265set_bitset_range(words, start, BITSET_WORDBITS - start_mod);266set_bitset_range(words, start + first_size, size - first_size);267}268}269270bool271test_bitset_range(BITSET_WORD* words, unsigned start, unsigned size)272{273unsigned end = start + size - 1;274unsigned start_mod = start % BITSET_WORDBITS;275if (start_mod + size <= BITSET_WORDBITS) {276return BITSET_TEST_RANGE(words, start, end);277} else {278unsigned first_size = BITSET_WORDBITS - start_mod;279return test_bitset_range(words, start, BITSET_WORDBITS - start_mod) ||280test_bitset_range(words, start + first_size, size - first_size);281}282}283284/* A SMEM clause is any group of consecutive SMEM instructions. The285* instructions in this group may return out of order and/or may be replayed.286*287* To fix this potential hazard correctly, we have to make sure that when a288* clause has more than one instruction, no instruction in the clause writes289* to a register that is read by another instruction in the clause (including290* itself). In this case, we have to break the SMEM clause by inserting non291* SMEM instructions.292*293* SMEM clauses are only present on GFX8+, and only matter when XNACK is set.294*/295void296handle_smem_clause_hazards(Program* program, NOP_ctx_gfx6& ctx, aco_ptr<Instruction>& instr,297int* NOPs)298{299/* break off from previous SMEM clause if needed */300if (!*NOPs & (ctx.smem_clause || ctx.smem_write)) {301/* Don't allow clauses with store instructions since the clause's302* instructions may use the same address. */303if (ctx.smem_write || instr->definitions.empty() ||304instr_info.is_atomic[(unsigned)instr->opcode]) {305*NOPs = 1;306} else if (program->dev.xnack_enabled) {307for (Operand op : instr->operands) {308if (!op.isConstant() &&309test_bitset_range(ctx.smem_clause_write, op.physReg(), op.size())) {310*NOPs = 1;311break;312}313}314315Definition def = instr->definitions[0];316if (!*NOPs && test_bitset_range(ctx.smem_clause_read_write, def.physReg(), def.size()))317*NOPs = 1;318}319}320}321322/* TODO: we don't handle accessing VCC using the actual SGPR instead of using the alias */323void324handle_instruction_gfx6(Program* program, Block* cur_block, NOP_ctx_gfx6& ctx,325aco_ptr<Instruction>& instr,326std::vector<aco_ptr<Instruction>>& new_instructions)327{328/* check hazards */329int NOPs = 0;330331if (instr->isSMEM()) {332if (program->chip_class == GFX6) {333/* A read of an SGPR by SMRD instruction requires 4 wait states334* when the SGPR was written by a VALU instruction. According to LLVM,335* there is also an undocumented hardware behavior when the buffer336* descriptor is written by a SALU instruction */337for (unsigned i = 0; i < instr->operands.size(); i++) {338Operand op = instr->operands[i];339if (op.isConstant())340continue;341342bool is_buffer_desc = i == 0 && op.size() > 2;343if (is_buffer_desc)344handle_valu_salu_then_read_hazard(program, cur_block, &NOPs, 4, op);345else346handle_valu_then_read_hazard(program, cur_block, &NOPs, 4, op);347}348}349350handle_smem_clause_hazards(program, ctx, instr, &NOPs);351} else if (instr->isSALU()) {352if (instr->opcode == aco_opcode::s_setreg_b32 ||353instr->opcode == aco_opcode::s_setreg_imm32_b32 ||354instr->opcode == aco_opcode::s_getreg_b32) {355NOPs = MAX2(NOPs, ctx.setreg_then_getsetreg);356}357358if (program->chip_class == GFX9) {359if (instr->opcode == aco_opcode::s_movrels_b32 ||360instr->opcode == aco_opcode::s_movrels_b64 ||361instr->opcode == aco_opcode::s_movreld_b32 ||362instr->opcode == aco_opcode::s_movreld_b64) {363NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_moverel);364}365}366367if (instr->opcode == aco_opcode::s_sendmsg || instr->opcode == aco_opcode::s_ttracedata)368NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_gds_msg_ttrace);369} else if (instr->isDS() && instr->ds().gds) {370NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_gds_msg_ttrace);371} else if (instr->isVALU() || instr->isVINTRP()) {372for (Operand op : instr->operands) {373if (op.physReg() == vccz)374NOPs = MAX2(NOPs, ctx.valu_wr_vcc_then_vccz);375if (op.physReg() == execz)376NOPs = MAX2(NOPs, ctx.valu_wr_exec_then_execz);377}378379if (instr->isDPP()) {380NOPs = MAX2(NOPs, ctx.valu_wr_exec_then_dpp);381handle_valu_then_read_hazard(program, cur_block, &NOPs, 2, instr->operands[0]);382}383384for (Definition def : instr->definitions) {385if (def.regClass().type() != RegType::sgpr) {386for (unsigned i = 0; i < def.size(); i++)387NOPs = MAX2(NOPs, ctx.vmem_store_then_wr_data[(def.physReg() & 0xff) + i]);388}389}390391if ((instr->opcode == aco_opcode::v_readlane_b32 ||392instr->opcode == aco_opcode::v_readlane_b32_e64 ||393instr->opcode == aco_opcode::v_writelane_b32 ||394instr->opcode == aco_opcode::v_writelane_b32_e64) &&395!instr->operands[1].isConstant()) {396handle_valu_then_read_hazard(program, cur_block, &NOPs, 4, instr->operands[1]);397}398399/* It's required to insert 1 wait state if the dst VGPR of any v_interp_*400* is followed by a read with v_readfirstlane or v_readlane to fix GPU401* hangs on GFX6. Note that v_writelane_* is apparently not affected.402* This hazard isn't documented anywhere but AMD confirmed that hazard.403*/404if (program->chip_class == GFX6 &&405(instr->opcode == aco_opcode::v_readlane_b32 || /* GFX6 doesn't have v_readlane_b32_e64 */406instr->opcode == aco_opcode::v_readfirstlane_b32)) {407handle_vintrp_then_read_hazard(program, cur_block, &NOPs, 1, instr->operands[0]);408}409410if (instr->opcode == aco_opcode::v_div_fmas_f32 ||411instr->opcode == aco_opcode::v_div_fmas_f64)412NOPs = MAX2(NOPs, ctx.valu_wr_vcc_then_div_fmas);413} else if (instr->isVMEM() || instr->isFlatLike()) {414/* If the VALU writes the SGPR that is used by a VMEM, the user must add five wait states. */415for (Operand op : instr->operands) {416if (!op.isConstant() && !op.isUndefined() && op.regClass().type() == RegType::sgpr)417handle_valu_then_read_hazard(program, cur_block, &NOPs, 5, op);418}419}420421if (!instr->isSALU() && instr->format != Format::SMEM)422NOPs = MAX2(NOPs, ctx.set_vskip_mode_then_vector);423424if (program->chip_class == GFX9) {425bool lds_scratch_global = (instr->isScratch() || instr->isGlobal()) && instr->flatlike().lds;426if (instr->isVINTRP() || lds_scratch_global ||427instr->opcode == aco_opcode::ds_read_addtid_b32 ||428instr->opcode == aco_opcode::ds_write_addtid_b32 ||429instr->opcode == aco_opcode::buffer_store_lds_dword) {430NOPs = MAX2(NOPs, ctx.salu_wr_m0_then_lds);431}432}433434ctx.add_wait_states(NOPs + get_wait_states(instr));435436// TODO: try to schedule the NOP-causing instruction up to reduce the number of stall cycles437if (NOPs) {438/* create NOP */439aco_ptr<SOPP_instruction> nop{440create_instruction<SOPP_instruction>(aco_opcode::s_nop, Format::SOPP, 0, 0)};441nop->imm = NOPs - 1;442nop->block = -1;443new_instructions.emplace_back(std::move(nop));444}445446/* update information to check for later hazards */447if ((ctx.smem_clause || ctx.smem_write) && (NOPs || instr->format != Format::SMEM)) {448ctx.smem_clause = false;449ctx.smem_write = false;450451if (program->dev.xnack_enabled) {452BITSET_ZERO(ctx.smem_clause_read_write);453BITSET_ZERO(ctx.smem_clause_write);454}455}456457if (instr->isSMEM()) {458if (instr->definitions.empty() || instr_info.is_atomic[(unsigned)instr->opcode]) {459ctx.smem_write = true;460} else {461ctx.smem_clause = true;462463if (program->dev.xnack_enabled) {464for (Operand op : instr->operands) {465if (!op.isConstant()) {466set_bitset_range(ctx.smem_clause_read_write, op.physReg(), op.size());467}468}469470Definition def = instr->definitions[0];471set_bitset_range(ctx.smem_clause_read_write, def.physReg(), def.size());472set_bitset_range(ctx.smem_clause_write, def.physReg(), def.size());473}474}475} else if (instr->isVALU()) {476for (Definition def : instr->definitions) {477if (def.regClass().type() == RegType::sgpr) {478if (def.physReg() == vcc || def.physReg() == vcc_hi) {479ctx.valu_wr_vcc_then_vccz = 5;480ctx.valu_wr_vcc_then_div_fmas = 4;481}482if (def.physReg() == exec || def.physReg() == exec_hi) {483ctx.valu_wr_exec_then_execz = 5;484ctx.valu_wr_exec_then_dpp = 5;485}486}487}488} else if (instr->isSALU() && !instr->definitions.empty()) {489if (!instr->definitions.empty()) {490/* all other definitions should be SCC */491Definition def = instr->definitions[0];492if (def.physReg() == m0) {493ctx.salu_wr_m0_then_gds_msg_ttrace = 1;494ctx.salu_wr_m0_then_lds = 1;495ctx.salu_wr_m0_then_moverel = 1;496}497} else if (instr->opcode == aco_opcode::s_setreg_b32 ||498instr->opcode == aco_opcode::s_setreg_imm32_b32) {499SOPK_instruction& sopk = instr->sopk();500unsigned offset = (sopk.imm >> 6) & 0x1f;501unsigned size = ((sopk.imm >> 11) & 0x1f) + 1;502unsigned reg = sopk.imm & 0x3f;503ctx.setreg_then_getsetreg = 2;504505if (reg == 1 && offset >= 28 && size > (28 - offset))506ctx.set_vskip_mode_then_vector = 2;507}508} else if (instr->isVMEM() || instr->isFlatLike()) {509/* >64-bit MUBUF/MTBUF store with a constant in SOFFSET */510bool consider_buf = (instr->isMUBUF() || instr->isMTBUF()) && instr->operands.size() == 4 &&511instr->operands[3].size() > 2 && instr->operands[2].physReg() >= 128;512/* MIMG store with a 128-bit T# with more than two bits set in dmask (making it a >64-bit513* store) */514bool consider_mimg = instr->isMIMG() &&515instr->operands[1].regClass().type() == RegType::vgpr &&516instr->operands[1].size() > 2 && instr->operands[0].size() == 4;517/* FLAT/GLOBAL/SCRATCH store with >64-bit data */518bool consider_flat =519instr->isFlatLike() && instr->operands.size() == 3 && instr->operands[2].size() > 2;520if (consider_buf || consider_mimg || consider_flat) {521PhysReg wrdata = instr->operands[consider_flat ? 2 : 3].physReg();522unsigned size = instr->operands[consider_flat ? 2 : 3].size();523for (unsigned i = 0; i < size; i++)524ctx.vmem_store_then_wr_data[(wrdata & 0xff) + i] = 1;525}526}527}528529template <std::size_t N>530bool531check_written_regs(const aco_ptr<Instruction>& instr, const std::bitset<N>& check_regs)532{533return std::any_of(instr->definitions.begin(), instr->definitions.end(),534[&check_regs](const Definition& def) -> bool535{536bool writes_any = false;537for (unsigned i = 0; i < def.size(); i++) {538unsigned def_reg = def.physReg() + i;539writes_any |= def_reg < check_regs.size() && check_regs[def_reg];540}541return writes_any;542});543}544545template <std::size_t N>546void547mark_read_regs(const aco_ptr<Instruction>& instr, std::bitset<N>& reg_reads)548{549for (const Operand& op : instr->operands) {550for (unsigned i = 0; i < op.size(); i++) {551unsigned reg = op.physReg() + i;552if (reg < reg_reads.size())553reg_reads.set(reg);554}555}556}557558bool559VALU_writes_sgpr(aco_ptr<Instruction>& instr)560{561if (instr->isVOPC())562return true;563if (instr->isVOP3() && instr->definitions.size() == 2)564return true;565if (instr->opcode == aco_opcode::v_readfirstlane_b32 ||566instr->opcode == aco_opcode::v_readlane_b32 ||567instr->opcode == aco_opcode::v_readlane_b32_e64)568return true;569return false;570}571572bool573instr_writes_exec(const aco_ptr<Instruction>& instr)574{575return std::any_of(instr->definitions.begin(), instr->definitions.end(),576[](const Definition& def) -> bool577{ return def.physReg() == exec_lo || def.physReg() == exec_hi; });578}579580bool581instr_writes_sgpr(const aco_ptr<Instruction>& instr)582{583return std::any_of(instr->definitions.begin(), instr->definitions.end(),584[](const Definition& def) -> bool585{ return def.getTemp().type() == RegType::sgpr; });586}587588inline bool589instr_is_branch(const aco_ptr<Instruction>& instr)590{591return instr->opcode == aco_opcode::s_branch || instr->opcode == aco_opcode::s_cbranch_scc0 ||592instr->opcode == aco_opcode::s_cbranch_scc1 ||593instr->opcode == aco_opcode::s_cbranch_vccz ||594instr->opcode == aco_opcode::s_cbranch_vccnz ||595instr->opcode == aco_opcode::s_cbranch_execz ||596instr->opcode == aco_opcode::s_cbranch_execnz ||597instr->opcode == aco_opcode::s_cbranch_cdbgsys ||598instr->opcode == aco_opcode::s_cbranch_cdbguser ||599instr->opcode == aco_opcode::s_cbranch_cdbgsys_or_user ||600instr->opcode == aco_opcode::s_cbranch_cdbgsys_and_user ||601instr->opcode == aco_opcode::s_subvector_loop_begin ||602instr->opcode == aco_opcode::s_subvector_loop_end ||603instr->opcode == aco_opcode::s_setpc_b64 || instr->opcode == aco_opcode::s_swappc_b64 ||604instr->opcode == aco_opcode::s_getpc_b64 || instr->opcode == aco_opcode::s_call_b64;605}606607void608handle_instruction_gfx10(Program* program, Block* cur_block, NOP_ctx_gfx10& ctx,609aco_ptr<Instruction>& instr,610std::vector<aco_ptr<Instruction>>& new_instructions)611{612// TODO: s_dcache_inv needs to be in it's own group on GFX10613614/* VMEMtoScalarWriteHazard615* Handle EXEC/M0/SGPR write following a VMEM instruction without a VALU or "waitcnt vmcnt(0)"616* in-between.617*/618if (instr->isVMEM() || instr->isFlatLike() || instr->isDS()) {619/* Remember all SGPRs that are read by the VMEM instruction */620mark_read_regs(instr, ctx.sgprs_read_by_VMEM);621ctx.sgprs_read_by_VMEM.set(exec);622if (program->wave_size == 64)623ctx.sgprs_read_by_VMEM.set(exec_hi);624} else if (instr->isSALU() || instr->isSMEM()) {625if (instr->opcode == aco_opcode::s_waitcnt) {626/* Hazard is mitigated by "s_waitcnt vmcnt(0)" */627uint16_t imm = instr->sopp().imm;628unsigned vmcnt = (imm & 0xF) | ((imm & (0x3 << 14)) >> 10);629if (vmcnt == 0)630ctx.sgprs_read_by_VMEM.reset();631} else if (instr->opcode == aco_opcode::s_waitcnt_depctr) {632/* Hazard is mitigated by a s_waitcnt_depctr with a magic imm */633if (instr->sopp().imm == 0xffe3)634ctx.sgprs_read_by_VMEM.reset();635}636637/* Check if SALU writes an SGPR that was previously read by the VALU */638if (check_written_regs(instr, ctx.sgprs_read_by_VMEM)) {639ctx.sgprs_read_by_VMEM.reset();640641/* Insert s_waitcnt_depctr instruction with magic imm to mitigate the problem */642aco_ptr<SOPP_instruction> depctr{643create_instruction<SOPP_instruction>(aco_opcode::s_waitcnt_depctr, Format::SOPP, 0, 0)};644depctr->imm = 0xffe3;645depctr->block = -1;646new_instructions.emplace_back(std::move(depctr));647}648} else if (instr->isVALU()) {649/* Hazard is mitigated by any VALU instruction */650ctx.sgprs_read_by_VMEM.reset();651}652653/* VcmpxPermlaneHazard654* Handle any permlane following a VOPC instruction, insert v_mov between them.655*/656if (instr->isVOPC()) {657ctx.has_VOPC = true;658} else if (ctx.has_VOPC && (instr->opcode == aco_opcode::v_permlane16_b32 ||659instr->opcode == aco_opcode::v_permlanex16_b32)) {660ctx.has_VOPC = false;661662/* v_nop would be discarded by SQ, so use v_mov with the first operand of the permlane */663aco_ptr<VOP1_instruction> v_mov{664create_instruction<VOP1_instruction>(aco_opcode::v_mov_b32, Format::VOP1, 1, 1)};665v_mov->definitions[0] = Definition(instr->operands[0].physReg(), v1);666v_mov->operands[0] = Operand(instr->operands[0].physReg(), v1);667new_instructions.emplace_back(std::move(v_mov));668} else if (instr->isVALU() && instr->opcode != aco_opcode::v_nop) {669ctx.has_VOPC = false;670}671672/* VcmpxExecWARHazard673* Handle any VALU instruction writing the exec mask after it was read by a non-VALU instruction.674*/675if (!instr->isVALU() && instr->reads_exec()) {676ctx.has_nonVALU_exec_read = true;677} else if (instr->isVALU()) {678if (instr_writes_exec(instr)) {679ctx.has_nonVALU_exec_read = false;680681/* Insert s_waitcnt_depctr instruction with magic imm to mitigate the problem */682aco_ptr<SOPP_instruction> depctr{683create_instruction<SOPP_instruction>(aco_opcode::s_waitcnt_depctr, Format::SOPP, 0, 0)};684depctr->imm = 0xfffe;685depctr->block = -1;686new_instructions.emplace_back(std::move(depctr));687} else if (instr_writes_sgpr(instr)) {688/* Any VALU instruction that writes an SGPR mitigates the problem */689ctx.has_nonVALU_exec_read = false;690}691} else if (instr->opcode == aco_opcode::s_waitcnt_depctr) {692/* s_waitcnt_depctr can mitigate the problem if it has a magic imm */693if ((instr->sopp().imm & 0xfffe) == 0xfffe)694ctx.has_nonVALU_exec_read = false;695}696697/* SMEMtoVectorWriteHazard698* Handle any VALU instruction writing an SGPR after an SMEM reads it.699*/700if (instr->isSMEM()) {701/* Remember all SGPRs that are read by the SMEM instruction */702mark_read_regs(instr, ctx.sgprs_read_by_SMEM);703} else if (VALU_writes_sgpr(instr)) {704/* Check if VALU writes an SGPR that was previously read by SMEM */705if (check_written_regs(instr, ctx.sgprs_read_by_SMEM)) {706ctx.sgprs_read_by_SMEM.reset();707708/* Insert s_mov to mitigate the problem */709aco_ptr<SOP1_instruction> s_mov{710create_instruction<SOP1_instruction>(aco_opcode::s_mov_b32, Format::SOP1, 1, 1)};711s_mov->definitions[0] = Definition(sgpr_null, s1);712s_mov->operands[0] = Operand::zero();713new_instructions.emplace_back(std::move(s_mov));714}715} else if (instr->isSALU()) {716if (instr->format != Format::SOPP) {717/* SALU can mitigate the hazard */718ctx.sgprs_read_by_SMEM.reset();719} else {720/* Reducing lgkmcnt count to 0 always mitigates the hazard. */721const SOPP_instruction& sopp = instr->sopp();722if (sopp.opcode == aco_opcode::s_waitcnt_lgkmcnt) {723if (sopp.imm == 0 && sopp.definitions[0].physReg() == sgpr_null)724ctx.sgprs_read_by_SMEM.reset();725} else if (sopp.opcode == aco_opcode::s_waitcnt) {726unsigned lgkm = (sopp.imm >> 8) & 0x3f;727if (lgkm == 0)728ctx.sgprs_read_by_SMEM.reset();729}730}731}732733/* LdsBranchVmemWARHazard734* Handle VMEM/GLOBAL/SCRATCH->branch->DS and DS->branch->VMEM/GLOBAL/SCRATCH patterns.735*/736if (instr->isVMEM() || instr->isGlobal() || instr->isScratch()) {737ctx.has_VMEM = true;738ctx.has_branch_after_VMEM = false;739/* Mitigation for DS is needed only if there was already a branch after */740ctx.has_DS = ctx.has_branch_after_DS;741} else if (instr->isDS()) {742ctx.has_DS = true;743ctx.has_branch_after_DS = false;744/* Mitigation for VMEM is needed only if there was already a branch after */745ctx.has_VMEM = ctx.has_branch_after_VMEM;746} else if (instr_is_branch(instr)) {747ctx.has_branch_after_VMEM = ctx.has_VMEM;748ctx.has_branch_after_DS = ctx.has_DS;749} else if (instr->opcode == aco_opcode::s_waitcnt_vscnt) {750/* Only s_waitcnt_vscnt can mitigate the hazard */751const SOPK_instruction& sopk = instr->sopk();752if (sopk.definitions[0].physReg() == sgpr_null && sopk.imm == 0)753ctx.has_VMEM = ctx.has_branch_after_VMEM = ctx.has_DS = ctx.has_branch_after_DS = false;754}755if ((ctx.has_VMEM && ctx.has_branch_after_DS) || (ctx.has_DS && ctx.has_branch_after_VMEM)) {756ctx.has_VMEM = ctx.has_branch_after_VMEM = ctx.has_DS = ctx.has_branch_after_DS = false;757758/* Insert s_waitcnt_vscnt to mitigate the problem */759aco_ptr<SOPK_instruction> wait{760create_instruction<SOPK_instruction>(aco_opcode::s_waitcnt_vscnt, Format::SOPK, 0, 1)};761wait->definitions[0] = Definition(sgpr_null, s1);762wait->imm = 0;763new_instructions.emplace_back(std::move(wait));764}765766/* NSAToVMEMBug767* Handles NSA MIMG (4 or more dwords) immediately followed by MUBUF/MTBUF (with offset[2:1] !=768* 0).769*/770if (instr->isMIMG() && get_mimg_nsa_dwords(instr.get()) > 1) {771ctx.has_NSA_MIMG = true;772} else if (ctx.has_NSA_MIMG) {773ctx.has_NSA_MIMG = false;774775if (instr->isMUBUF() || instr->isMTBUF()) {776uint32_t offset = instr->isMUBUF() ? instr->mubuf().offset : instr->mtbuf().offset;777if (offset & 6)778Builder(program, &new_instructions).sopp(aco_opcode::s_nop, -1, 0);779}780}781782/* waNsaCannotFollowWritelane783* Handles NSA MIMG immediately following a v_writelane_b32.784*/785if (instr->opcode == aco_opcode::v_writelane_b32_e64) {786ctx.has_writelane = true;787} else if (ctx.has_writelane) {788ctx.has_writelane = false;789if (instr->isMIMG() && get_mimg_nsa_dwords(instr.get()) > 0)790Builder(program, &new_instructions).sopp(aco_opcode::s_nop, -1, 0);791}792}793794template <typename Ctx>795using HandleInstr = void (*)(Program*, Block* block, Ctx&, aco_ptr<Instruction>&,796std::vector<aco_ptr<Instruction>>&);797798template <typename Ctx, HandleInstr<Ctx> Handle>799void800handle_block(Program* program, Ctx& ctx, Block& block)801{802if (block.instructions.empty())803return;804805std::vector<aco_ptr<Instruction>> old_instructions = std::move(block.instructions);806807block.instructions.clear(); // Silence clang-analyzer-cplusplus.Move warning808block.instructions.reserve(old_instructions.size());809810for (aco_ptr<Instruction>& instr : old_instructions) {811Handle(program, &block, ctx, instr, block.instructions);812block.instructions.emplace_back(std::move(instr));813}814}815816template <typename Ctx, HandleInstr<Ctx> Handle>817void818mitigate_hazards(Program* program)819{820std::vector<Ctx> all_ctx(program->blocks.size());821std::stack<unsigned> loop_header_indices;822823for (unsigned i = 0; i < program->blocks.size(); i++) {824Block& block = program->blocks[i];825Ctx& ctx = all_ctx[i];826827if (block.kind & block_kind_loop_header) {828loop_header_indices.push(i);829} else if (block.kind & block_kind_loop_exit) {830/* Go through the whole loop again */831for (unsigned idx = loop_header_indices.top(); idx < i; idx++) {832Ctx loop_block_ctx;833for (unsigned b : program->blocks[idx].linear_preds)834loop_block_ctx.join(all_ctx[b]);835836handle_block<Ctx, Handle>(program, loop_block_ctx, program->blocks[idx]);837838/* We only need to continue if the loop header context changed */839if (idx == loop_header_indices.top() && loop_block_ctx == all_ctx[idx])840break;841842all_ctx[idx] = loop_block_ctx;843}844845loop_header_indices.pop();846}847848for (unsigned b : block.linear_preds)849ctx.join(all_ctx[b]);850851handle_block<Ctx, Handle>(program, ctx, block);852}853}854855} /* end namespace */856857void858insert_NOPs(Program* program)859{860if (program->chip_class >= GFX10_3)861; /* no hazards/bugs to mitigate */862else if (program->chip_class >= GFX10)863mitigate_hazards<NOP_ctx_gfx10, handle_instruction_gfx10>(program);864else865mitigate_hazards<NOP_ctx_gfx6, handle_instruction_gfx6>(program);866}867868} // namespace aco869870871