Path: blob/21.2-virgl/src/amd/compiler/aco_opt_value_numbering.cpp
4550 views
/*1* Copyright © 2018 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_ir.h"2526#include <map>27#include <unordered_map>28#include <vector>2930/*31* Implements the algorithm for dominator-tree value numbering32* from "Value Numbering" by Briggs, Cooper, and Simpson.33*/3435namespace aco {36namespace {3738inline uint32_t39murmur_32_scramble(uint32_t h, uint32_t k)40{41k *= 0xcc9e2d51;42k = (k << 15) | (k >> 17);43h ^= k * 0x1b873593;44h = (h << 13) | (h >> 19);45h = h * 5 + 0xe6546b64;46return h;47}4849template <typename T>50uint32_t51hash_murmur_32(Instruction* instr)52{53uint32_t hash = uint32_t(instr->format) << 16 | uint32_t(instr->opcode);5455for (const Operand& op : instr->operands)56hash = murmur_32_scramble(hash, op.constantValue());5758/* skip format, opcode and pass_flags */59for (unsigned i = 2; i < (sizeof(T) >> 2); i++) {60uint32_t u;61/* Accesses it though a byte array, so doesn't violate the strict aliasing rule */62memcpy(&u, reinterpret_cast<uint8_t*>(instr) + i * 4, 4);63hash = murmur_32_scramble(hash, u);64}6566/* Finalize. */67uint32_t len = instr->operands.size() + instr->definitions.size() + sizeof(T);68hash ^= len;69hash ^= hash >> 16;70hash *= 0x85ebca6b;71hash ^= hash >> 13;72hash *= 0xc2b2ae35;73hash ^= hash >> 16;74return hash;75}7677struct InstrHash {78/* This hash function uses the Murmur3 algorithm written by Austin Appleby79* https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp80*81* In order to calculate the expression set, only the right-hand-side of an82* instruction is used for the hash, i.e. everything except the definitions.83*/84std::size_t operator()(Instruction* instr) const85{86if (instr->isVOP3())87return hash_murmur_32<VOP3_instruction>(instr);8889if (instr->isDPP())90return hash_murmur_32<DPP_instruction>(instr);9192if (instr->isSDWA())93return hash_murmur_32<SDWA_instruction>(instr);9495switch (instr->format) {96case Format::SMEM: return hash_murmur_32<SMEM_instruction>(instr);97case Format::VINTRP: return hash_murmur_32<Interp_instruction>(instr);98case Format::DS: return hash_murmur_32<DS_instruction>(instr);99case Format::SOPP: return hash_murmur_32<SOPP_instruction>(instr);100case Format::SOPK: return hash_murmur_32<SOPK_instruction>(instr);101case Format::EXP: return hash_murmur_32<Export_instruction>(instr);102case Format::MUBUF: return hash_murmur_32<MUBUF_instruction>(instr);103case Format::MIMG: return hash_murmur_32<MIMG_instruction>(instr);104case Format::MTBUF: return hash_murmur_32<MTBUF_instruction>(instr);105case Format::FLAT: return hash_murmur_32<FLAT_instruction>(instr);106case Format::PSEUDO_BRANCH: return hash_murmur_32<Pseudo_branch_instruction>(instr);107case Format::PSEUDO_REDUCTION: return hash_murmur_32<Pseudo_reduction_instruction>(instr);108default: return hash_murmur_32<Instruction>(instr);109}110}111};112113struct InstrPred {114bool operator()(Instruction* a, Instruction* b) const115{116if (a->format != b->format)117return false;118if (a->opcode != b->opcode)119return false;120if (a->operands.size() != b->operands.size() ||121a->definitions.size() != b->definitions.size())122return false; /* possible with pseudo-instructions */123for (unsigned i = 0; i < a->operands.size(); i++) {124if (a->operands[i].isConstant()) {125if (!b->operands[i].isConstant())126return false;127if (a->operands[i].constantValue() != b->operands[i].constantValue())128return false;129} else if (a->operands[i].isTemp()) {130if (!b->operands[i].isTemp())131return false;132if (a->operands[i].tempId() != b->operands[i].tempId())133return false;134} else if (a->operands[i].isUndefined() ^ b->operands[i].isUndefined())135return false;136if (a->operands[i].isFixed()) {137if (!b->operands[i].isFixed())138return false;139if (a->operands[i].physReg() != b->operands[i].physReg())140return false;141if (a->operands[i].physReg() == exec && a->pass_flags != b->pass_flags)142return false;143}144}145for (unsigned i = 0; i < a->definitions.size(); i++) {146if (a->definitions[i].isTemp()) {147if (!b->definitions[i].isTemp())148return false;149if (a->definitions[i].regClass() != b->definitions[i].regClass())150return false;151}152if (a->definitions[i].isFixed()) {153if (!b->definitions[i].isFixed())154return false;155if (a->definitions[i].physReg() != b->definitions[i].physReg())156return false;157if (a->definitions[i].physReg() == exec)158return false;159}160}161162if (a->opcode == aco_opcode::v_readfirstlane_b32)163return a->pass_flags == b->pass_flags;164165if (a->isVOP3()) {166VOP3_instruction& a3 = a->vop3();167VOP3_instruction& b3 = b->vop3();168for (unsigned i = 0; i < 3; i++) {169if (a3.abs[i] != b3.abs[i] || a3.neg[i] != b3.neg[i])170return false;171}172return a3.clamp == b3.clamp && a3.omod == b3.omod && a3.opsel == b3.opsel;173}174if (a->isDPP()) {175DPP_instruction& aDPP = a->dpp();176DPP_instruction& bDPP = b->dpp();177return aDPP.pass_flags == bDPP.pass_flags && aDPP.dpp_ctrl == bDPP.dpp_ctrl &&178aDPP.bank_mask == bDPP.bank_mask && aDPP.row_mask == bDPP.row_mask &&179aDPP.bound_ctrl == bDPP.bound_ctrl && aDPP.abs[0] == bDPP.abs[0] &&180aDPP.abs[1] == bDPP.abs[1] && aDPP.neg[0] == bDPP.neg[0] &&181aDPP.neg[1] == bDPP.neg[1];182}183if (a->isSDWA()) {184SDWA_instruction& aSDWA = a->sdwa();185SDWA_instruction& bSDWA = b->sdwa();186return aSDWA.sel[0] == bSDWA.sel[0] && aSDWA.sel[1] == bSDWA.sel[1] &&187aSDWA.dst_sel == bSDWA.dst_sel && aSDWA.abs[0] == bSDWA.abs[0] &&188aSDWA.abs[1] == bSDWA.abs[1] && aSDWA.neg[0] == bSDWA.neg[0] &&189aSDWA.neg[1] == bSDWA.neg[1] && aSDWA.dst_preserve == bSDWA.dst_preserve &&190aSDWA.clamp == bSDWA.clamp && aSDWA.omod == bSDWA.omod;191}192193switch (a->format) {194case Format::SOPK: {195if (a->opcode == aco_opcode::s_getreg_b32)196return false;197SOPK_instruction& aK = a->sopk();198SOPK_instruction& bK = b->sopk();199return aK.imm == bK.imm;200}201case Format::SMEM: {202SMEM_instruction& aS = a->smem();203SMEM_instruction& bS = b->smem();204/* isel shouldn't be creating situations where this assertion fails */205assert(aS.prevent_overflow == bS.prevent_overflow);206return aS.sync == bS.sync && aS.glc == bS.glc && aS.dlc == bS.dlc && aS.nv == bS.nv &&207aS.disable_wqm == bS.disable_wqm && aS.prevent_overflow == bS.prevent_overflow;208}209case Format::VINTRP: {210Interp_instruction& aI = a->vintrp();211Interp_instruction& bI = b->vintrp();212if (aI.attribute != bI.attribute)213return false;214if (aI.component != bI.component)215return false;216return true;217}218case Format::VOP3P: {219VOP3P_instruction& a3P = a->vop3p();220VOP3P_instruction& b3P = b->vop3p();221for (unsigned i = 0; i < 3; i++) {222if (a3P.neg_lo[i] != b3P.neg_lo[i] || a3P.neg_hi[i] != b3P.neg_hi[i])223return false;224}225return a3P.opsel_lo == b3P.opsel_lo && a3P.opsel_hi == b3P.opsel_hi &&226a3P.clamp == b3P.clamp;227}228case Format::PSEUDO_REDUCTION: {229Pseudo_reduction_instruction& aR = a->reduction();230Pseudo_reduction_instruction& bR = b->reduction();231return aR.pass_flags == bR.pass_flags && aR.reduce_op == bR.reduce_op &&232aR.cluster_size == bR.cluster_size;233}234case Format::DS: {235assert(a->opcode == aco_opcode::ds_bpermute_b32 ||236a->opcode == aco_opcode::ds_permute_b32 || a->opcode == aco_opcode::ds_swizzle_b32);237DS_instruction& aD = a->ds();238DS_instruction& bD = b->ds();239return aD.sync == bD.sync && aD.pass_flags == bD.pass_flags && aD.gds == bD.gds &&240aD.offset0 == bD.offset0 && aD.offset1 == bD.offset1;241}242case Format::MTBUF: {243MTBUF_instruction& aM = a->mtbuf();244MTBUF_instruction& bM = b->mtbuf();245return aM.sync == bM.sync && aM.dfmt == bM.dfmt && aM.nfmt == bM.nfmt &&246aM.offset == bM.offset && aM.offen == bM.offen && aM.idxen == bM.idxen &&247aM.glc == bM.glc && aM.dlc == bM.dlc && aM.slc == bM.slc && aM.tfe == bM.tfe &&248aM.disable_wqm == bM.disable_wqm;249}250case Format::MUBUF: {251MUBUF_instruction& aM = a->mubuf();252MUBUF_instruction& bM = b->mubuf();253return aM.sync == bM.sync && aM.offset == bM.offset && aM.offen == bM.offen &&254aM.idxen == bM.idxen && aM.glc == bM.glc && aM.dlc == bM.dlc && aM.slc == bM.slc &&255aM.tfe == bM.tfe && aM.lds == bM.lds && aM.disable_wqm == bM.disable_wqm;256}257case Format::MIMG: {258MIMG_instruction& aM = a->mimg();259MIMG_instruction& bM = b->mimg();260return aM.sync == bM.sync && aM.dmask == bM.dmask && aM.unrm == bM.unrm &&261aM.glc == bM.glc && aM.slc == bM.slc && aM.tfe == bM.tfe && aM.da == bM.da &&262aM.lwe == bM.lwe && aM.r128 == bM.r128 && aM.a16 == bM.a16 && aM.d16 == bM.d16 &&263aM.disable_wqm == bM.disable_wqm;264}265case Format::FLAT:266case Format::GLOBAL:267case Format::SCRATCH:268case Format::EXP:269case Format::SOPP:270case Format::PSEUDO_BRANCH:271case Format::PSEUDO_BARRIER: assert(false);272default: return true;273}274}275};276277using expr_set = std::unordered_map<Instruction*, uint32_t, InstrHash, InstrPred>;278279struct vn_ctx {280Program* program;281expr_set expr_values;282std::map<uint32_t, Temp> renames;283284/* The exec id should be the same on the same level of control flow depth.285* Together with the check for dominator relations, it is safe to assume286* that the same exec_id also means the same execution mask.287* Discards increment the exec_id, so that it won't return to the previous value.288*/289uint32_t exec_id = 1;290291vn_ctx(Program* program_) : program(program_)292{293static_assert(sizeof(Temp) == 4, "Temp must fit in 32bits");294unsigned size = 0;295for (Block& block : program->blocks)296size += block.instructions.size();297expr_values.reserve(size);298}299};300301/* dominates() returns true if the parent block dominates the child block and302* if the parent block is part of the same loop or has a smaller loop nest depth.303*/304bool305dominates(vn_ctx& ctx, uint32_t parent, uint32_t child)306{307unsigned parent_loop_nest_depth = ctx.program->blocks[parent].loop_nest_depth;308while (parent < child && parent_loop_nest_depth <= ctx.program->blocks[child].loop_nest_depth)309child = ctx.program->blocks[child].logical_idom;310311return parent == child;312}313314/** Returns whether this instruction can safely be removed315* and replaced by an equal expression.316* This is in particular true for ALU instructions and317* read-only memory instructions.318*319* Note that expr_set must not be used with instructions320* which cannot be eliminated.321*/322bool323can_eliminate(aco_ptr<Instruction>& instr)324{325switch (instr->format) {326case Format::FLAT:327case Format::GLOBAL:328case Format::SCRATCH:329case Format::EXP:330case Format::SOPP:331case Format::PSEUDO_BRANCH:332case Format::PSEUDO_BARRIER: return false;333case Format::DS:334return instr->opcode == aco_opcode::ds_bpermute_b32 ||335instr->opcode == aco_opcode::ds_permute_b32 ||336instr->opcode == aco_opcode::ds_swizzle_b32;337case Format::SMEM:338case Format::MUBUF:339case Format::MIMG:340case Format::MTBUF:341if (!get_sync_info(instr.get()).can_reorder())342return false;343break;344default: break;345}346347if (instr->definitions.empty() || instr->opcode == aco_opcode::p_phi ||348instr->opcode == aco_opcode::p_linear_phi || instr->definitions[0].isNoCSE())349return false;350351return true;352}353354void355process_block(vn_ctx& ctx, Block& block)356{357std::vector<aco_ptr<Instruction>> new_instructions;358new_instructions.reserve(block.instructions.size());359360for (aco_ptr<Instruction>& instr : block.instructions) {361/* first, rename operands */362for (Operand& op : instr->operands) {363if (!op.isTemp())364continue;365auto it = ctx.renames.find(op.tempId());366if (it != ctx.renames.end())367op.setTemp(it->second);368}369370if (instr->opcode == aco_opcode::p_discard_if ||371instr->opcode == aco_opcode::p_demote_to_helper)372ctx.exec_id++;373374if (!can_eliminate(instr)) {375new_instructions.emplace_back(std::move(instr));376continue;377}378379/* simple copy-propagation through renaming */380bool copy_instr =381instr->opcode == aco_opcode::p_parallelcopy ||382(instr->opcode == aco_opcode::p_create_vector && instr->operands.size() == 1);383if (copy_instr && !instr->definitions[0].isFixed() && instr->operands[0].isTemp() &&384instr->operands[0].regClass() == instr->definitions[0].regClass()) {385ctx.renames[instr->definitions[0].tempId()] = instr->operands[0].getTemp();386continue;387}388389instr->pass_flags = ctx.exec_id;390std::pair<expr_set::iterator, bool> res = ctx.expr_values.emplace(instr.get(), block.index);391392/* if there was already an expression with the same value number */393if (!res.second) {394Instruction* orig_instr = res.first->first;395assert(instr->definitions.size() == orig_instr->definitions.size());396/* check if the original instruction dominates the current one */397if (dominates(ctx, res.first->second, block.index) &&398ctx.program->blocks[res.first->second].fp_mode.canReplace(block.fp_mode)) {399for (unsigned i = 0; i < instr->definitions.size(); i++) {400assert(instr->definitions[i].regClass() == orig_instr->definitions[i].regClass());401assert(instr->definitions[i].isTemp());402ctx.renames[instr->definitions[i].tempId()] = orig_instr->definitions[i].getTemp();403if (instr->definitions[i].isPrecise())404orig_instr->definitions[i].setPrecise(true);405/* SPIR_V spec says that an instruction marked with NUW wrapping406* around is undefined behaviour, so we can break additions in407* other contexts.408*/409if (instr->definitions[i].isNUW())410orig_instr->definitions[i].setNUW(true);411}412} else {413ctx.expr_values.erase(res.first);414ctx.expr_values.emplace(instr.get(), block.index);415new_instructions.emplace_back(std::move(instr));416}417} else {418new_instructions.emplace_back(std::move(instr));419}420}421422block.instructions = std::move(new_instructions);423}424425void426rename_phi_operands(Block& block, std::map<uint32_t, Temp>& renames)427{428for (aco_ptr<Instruction>& phi : block.instructions) {429if (phi->opcode != aco_opcode::p_phi && phi->opcode != aco_opcode::p_linear_phi)430break;431432for (Operand& op : phi->operands) {433if (!op.isTemp())434continue;435auto it = renames.find(op.tempId());436if (it != renames.end())437op.setTemp(it->second);438}439}440}441} /* end namespace */442443void444value_numbering(Program* program)445{446vn_ctx ctx(program);447std::vector<unsigned> loop_headers;448449for (Block& block : program->blocks) {450assert(ctx.exec_id > 0);451/* decrement exec_id when leaving nested control flow */452if (block.kind & block_kind_loop_header)453loop_headers.push_back(block.index);454if (block.kind & block_kind_merge) {455ctx.exec_id--;456} else if (block.kind & block_kind_loop_exit) {457ctx.exec_id -= program->blocks[loop_headers.back()].linear_preds.size();458ctx.exec_id -= block.linear_preds.size();459loop_headers.pop_back();460}461462if (block.logical_idom != -1)463process_block(ctx, block);464else465rename_phi_operands(block, ctx.renames);466467/* increment exec_id when entering nested control flow */468if (block.kind & block_kind_branch || block.kind & block_kind_loop_preheader ||469block.kind & block_kind_break || block.kind & block_kind_continue ||470block.kind & block_kind_discard)471ctx.exec_id++;472else if (block.kind & block_kind_continue_or_break)473ctx.exec_id += 2;474}475476/* rename loop header phi operands */477for (Block& block : program->blocks) {478if (block.kind & block_kind_loop_header)479rename_phi_operands(block, ctx.renames);480}481}482483} // namespace aco484485486