2019-09-17 13:22:17 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2018 Valve Corporation
|
|
|
|
|
*
|
2024-04-08 09:02:30 +02:00
|
|
|
* SPDX-License-Identifier: MIT
|
2019-09-17 13:22:17 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-06-09 15:40:03 +02:00
|
|
|
#include "aco_ir.h"
|
2022-08-22 17:26:34 +02:00
|
|
|
#include "aco_util.h"
|
2021-06-09 15:40:03 +02:00
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
#include <unordered_map>
|
2021-06-09 15:40:03 +02:00
|
|
|
#include <vector>
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Implements the algorithm for dominator-tree value numbering
|
|
|
|
|
* from "Value Numbering" by Briggs, Cooper, and Simpson.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace aco {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2020-03-10 10:00:32 +01:00
|
|
|
inline uint32_t
|
|
|
|
|
murmur_32_scramble(uint32_t h, uint32_t k)
|
|
|
|
|
{
|
|
|
|
|
k *= 0xcc9e2d51;
|
|
|
|
|
k = (k << 15) | (k >> 17);
|
|
|
|
|
h ^= k * 0x1b873593;
|
|
|
|
|
h = (h << 13) | (h >> 19);
|
|
|
|
|
h = h * 5 + 0xe6546b64;
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
struct InstrHash {
|
2020-03-10 10:00:32 +01:00
|
|
|
/* This hash function uses the Murmur3 algorithm written by Austin Appleby
|
|
|
|
|
* https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
|
|
|
|
|
*
|
|
|
|
|
* In order to calculate the expression set, only the right-hand-side of an
|
|
|
|
|
* instruction is used for the hash, i.e. everything except the definitions.
|
|
|
|
|
*/
|
2019-09-17 13:22:17 +02:00
|
|
|
std::size_t operator()(Instruction* instr) const
|
|
|
|
|
{
|
2024-04-02 16:36:20 +02:00
|
|
|
uint32_t hash = uint32_t(instr->format) << 16 | uint32_t(instr->opcode);
|
2020-03-10 10:00:32 +01:00
|
|
|
|
2024-04-02 16:36:20 +02:00
|
|
|
for (const Operand& op : instr->operands)
|
|
|
|
|
hash = murmur_32_scramble(hash, op.constantValue());
|
2020-05-22 15:42:39 +01:00
|
|
|
|
2024-04-02 16:36:20 +02:00
|
|
|
size_t data_size = get_instr_data_size(instr->format);
|
2023-03-22 11:32:04 +01:00
|
|
|
|
2024-04-02 16:36:20 +02:00
|
|
|
/* skip format, opcode and pass_flags and op/def spans */
|
|
|
|
|
for (unsigned i = sizeof(Instruction) >> 2; i < (data_size >> 2); i++) {
|
|
|
|
|
uint32_t u;
|
|
|
|
|
/* Accesses it though a byte array, so doesn't violate the strict aliasing rule */
|
|
|
|
|
memcpy(&u, reinterpret_cast<uint8_t*>(instr) + i * 4, 4);
|
|
|
|
|
hash = murmur_32_scramble(hash, u);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2024-04-02 16:36:20 +02:00
|
|
|
|
|
|
|
|
/* Finalize. */
|
|
|
|
|
uint32_t len = instr->operands.size() + instr->definitions.size();
|
|
|
|
|
hash ^= len;
|
|
|
|
|
hash ^= hash >> 16;
|
|
|
|
|
hash *= 0x85ebca6b;
|
|
|
|
|
hash ^= hash >> 13;
|
|
|
|
|
hash *= 0xc2b2ae35;
|
|
|
|
|
hash ^= hash >> 16;
|
|
|
|
|
return hash;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct InstrPred {
|
|
|
|
|
bool operator()(Instruction* a, Instruction* b) const
|
|
|
|
|
{
|
|
|
|
|
if (a->format != b->format)
|
|
|
|
|
return false;
|
|
|
|
|
if (a->opcode != b->opcode)
|
|
|
|
|
return false;
|
|
|
|
|
if (a->operands.size() != b->operands.size() ||
|
|
|
|
|
a->definitions.size() != b->definitions.size())
|
|
|
|
|
return false; /* possible with pseudo-instructions */
|
|
|
|
|
for (unsigned i = 0; i < a->operands.size(); i++) {
|
|
|
|
|
if (a->operands[i].isConstant()) {
|
|
|
|
|
if (!b->operands[i].isConstant())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->operands[i].constantValue() != b->operands[i].constantValue())
|
|
|
|
|
return false;
|
|
|
|
|
} else if (a->operands[i].isTemp()) {
|
|
|
|
|
if (!b->operands[i].isTemp())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->operands[i].tempId() != b->operands[i].tempId())
|
|
|
|
|
return false;
|
|
|
|
|
} else if (a->operands[i].isUndefined() ^ b->operands[i].isUndefined())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->operands[i].isFixed()) {
|
|
|
|
|
if (!b->operands[i].isFixed())
|
|
|
|
|
return false;
|
2019-11-11 11:41:31 +01:00
|
|
|
if (a->operands[i].physReg() != b->operands[i].physReg())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->operands[i].physReg() == exec && a->pass_flags != b->pass_flags)
|
2019-09-17 13:22:17 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (unsigned i = 0; i < a->definitions.size(); i++) {
|
|
|
|
|
if (a->definitions[i].isTemp()) {
|
|
|
|
|
if (!b->definitions[i].isTemp())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->definitions[i].regClass() != b->definitions[i].regClass())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (a->definitions[i].isFixed()) {
|
|
|
|
|
if (!b->definitions[i].isFixed())
|
|
|
|
|
return false;
|
2019-11-11 11:41:31 +01:00
|
|
|
if (a->definitions[i].physReg() != b->definitions[i].physReg())
|
|
|
|
|
return false;
|
|
|
|
|
if (a->definitions[i].physReg() == exec)
|
2019-09-17 13:22:17 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-11 11:41:31 +01:00
|
|
|
|
2023-05-03 13:07:35 +02:00
|
|
|
if (a->isVALU()) {
|
|
|
|
|
VALU_instruction& aV = a->valu();
|
|
|
|
|
VALU_instruction& bV = b->valu();
|
|
|
|
|
if (aV.abs != bV.abs || aV.neg != bV.neg || aV.clamp != bV.clamp || aV.omod != bV.omod ||
|
|
|
|
|
aV.opsel != bV.opsel || aV.opsel_lo != bV.opsel_lo || aV.opsel_hi != bV.opsel_hi)
|
|
|
|
|
return false;
|
2023-11-04 11:01:41 +01:00
|
|
|
|
|
|
|
|
if (a->opcode == aco_opcode::v_permlane16_b32 ||
|
|
|
|
|
a->opcode == aco_opcode::v_permlanex16_b32 ||
|
2024-01-18 22:57:45 +01:00
|
|
|
a->opcode == aco_opcode::v_permlane64_b32 ||
|
2023-11-04 11:01:41 +01:00
|
|
|
a->opcode == aco_opcode::v_readfirstlane_b32)
|
|
|
|
|
return aV.pass_flags == bV.pass_flags;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2021-11-29 00:12:04 +09:00
|
|
|
if (a->isDPP16()) {
|
|
|
|
|
DPP16_instruction& aDPP = a->dpp16();
|
|
|
|
|
DPP16_instruction& bDPP = b->dpp16();
|
2021-01-21 16:13:34 +00:00
|
|
|
return aDPP.pass_flags == bDPP.pass_flags && aDPP.dpp_ctrl == bDPP.dpp_ctrl &&
|
|
|
|
|
aDPP.bank_mask == bDPP.bank_mask && aDPP.row_mask == bDPP.row_mask &&
|
2023-10-02 15:47:11 +01:00
|
|
|
aDPP.bound_ctrl == bDPP.bound_ctrl && aDPP.fetch_inactive == bDPP.fetch_inactive;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2021-11-29 00:12:04 +09:00
|
|
|
if (a->isDPP8()) {
|
|
|
|
|
DPP8_instruction& aDPP = a->dpp8();
|
|
|
|
|
DPP8_instruction& bDPP = b->dpp8();
|
2023-10-02 15:47:11 +01:00
|
|
|
return aDPP.pass_flags == bDPP.pass_flags && aDPP.lane_sel == bDPP.lane_sel &&
|
|
|
|
|
aDPP.fetch_inactive == bDPP.fetch_inactive;
|
2021-11-29 00:12:04 +09:00
|
|
|
}
|
2020-05-22 15:42:39 +01:00
|
|
|
if (a->isSDWA()) {
|
2021-01-21 16:13:34 +00:00
|
|
|
SDWA_instruction& aSDWA = a->sdwa();
|
|
|
|
|
SDWA_instruction& bSDWA = b->sdwa();
|
|
|
|
|
return aSDWA.sel[0] == bSDWA.sel[0] && aSDWA.sel[1] == bSDWA.sel[1] &&
|
2023-05-03 13:07:35 +02:00
|
|
|
aSDWA.dst_sel == bSDWA.dst_sel;
|
2020-05-22 15:42:39 +01:00
|
|
|
}
|
2019-11-29 16:47:13 +01:00
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
switch (a->format) {
|
2022-10-24 02:14:24 +00:00
|
|
|
case Format::SOP1: {
|
|
|
|
|
if (a->opcode == aco_opcode::s_sendmsg_rtn_b32 ||
|
|
|
|
|
a->opcode == aco_opcode::s_sendmsg_rtn_b64)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
case Format::SOPK: {
|
2020-06-18 14:45:31 +01:00
|
|
|
if (a->opcode == aco_opcode::s_getreg_b32)
|
|
|
|
|
return false;
|
2024-03-19 15:46:56 +01:00
|
|
|
SALU_instruction& aK = a->salu();
|
|
|
|
|
SALU_instruction& bK = b->salu();
|
2021-01-21 16:13:34 +00:00
|
|
|
return aK.imm == bK.imm;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
case Format::SMEM: {
|
2021-01-21 16:13:34 +00:00
|
|
|
SMEM_instruction& aS = a->smem();
|
|
|
|
|
SMEM_instruction& bS = b->smem();
|
2024-05-14 18:34:01 +01:00
|
|
|
return aS.sync == bS.sync && aS.cache.value == bS.cache.value;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
case Format::VINTRP: {
|
2022-09-14 11:19:30 +01:00
|
|
|
VINTRP_instruction& aI = a->vintrp();
|
|
|
|
|
VINTRP_instruction& bI = b->vintrp();
|
2024-03-27 18:02:08 +01:00
|
|
|
return aI.attribute == bI.attribute && aI.component == bI.component &&
|
|
|
|
|
aI.high_16bits == bI.high_16bits;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2022-06-17 13:53:08 +01:00
|
|
|
case Format::VINTERP_INREG: {
|
|
|
|
|
VINTERP_inreg_instruction& aI = a->vinterp_inreg();
|
|
|
|
|
VINTERP_inreg_instruction& bI = b->vinterp_inreg();
|
2023-05-03 13:07:35 +02:00
|
|
|
return aI.wait_exp == bI.wait_exp;
|
2022-06-17 13:53:08 +01:00
|
|
|
}
|
2019-09-23 14:31:24 +01:00
|
|
|
case Format::PSEUDO_REDUCTION: {
|
2021-01-21 16:13:34 +00:00
|
|
|
Pseudo_reduction_instruction& aR = a->reduction();
|
|
|
|
|
Pseudo_reduction_instruction& bR = b->reduction();
|
|
|
|
|
return aR.pass_flags == bR.pass_flags && aR.reduce_op == bR.reduce_op &&
|
|
|
|
|
aR.cluster_size == bR.cluster_size;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2020-12-22 11:59:26 +01:00
|
|
|
case Format::DS: {
|
|
|
|
|
assert(a->opcode == aco_opcode::ds_bpermute_b32 ||
|
|
|
|
|
a->opcode == aco_opcode::ds_permute_b32 || a->opcode == aco_opcode::ds_swizzle_b32);
|
|
|
|
|
DS_instruction& aD = a->ds();
|
|
|
|
|
DS_instruction& bD = b->ds();
|
2021-01-21 16:13:34 +00:00
|
|
|
return aD.sync == bD.sync && aD.pass_flags == bD.pass_flags && aD.gds == bD.gds &&
|
2020-12-22 11:59:26 +01:00
|
|
|
aD.offset0 == bD.offset0 && aD.offset1 == bD.offset1;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2022-06-17 13:53:08 +01:00
|
|
|
case Format::LDSDIR: {
|
|
|
|
|
LDSDIR_instruction& aD = a->ldsdir();
|
|
|
|
|
LDSDIR_instruction& bD = b->ldsdir();
|
|
|
|
|
return aD.sync == bD.sync && aD.attr == bD.attr && aD.attr_chan == bD.attr_chan &&
|
|
|
|
|
aD.wait_vdst == bD.wait_vdst;
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
case Format::MTBUF: {
|
2021-01-21 16:13:34 +00:00
|
|
|
MTBUF_instruction& aM = a->mtbuf();
|
|
|
|
|
MTBUF_instruction& bM = b->mtbuf();
|
2020-12-22 11:59:26 +01:00
|
|
|
return aM.sync == bM.sync && aM.dfmt == bM.dfmt && aM.nfmt == bM.nfmt &&
|
2021-01-21 16:13:34 +00:00
|
|
|
aM.offset == bM.offset && aM.offen == bM.offen && aM.idxen == bM.idxen &&
|
2024-05-14 18:34:01 +01:00
|
|
|
aM.cache.value == bM.cache.value && aM.tfe == bM.tfe &&
|
2021-01-21 16:13:34 +00:00
|
|
|
aM.disable_wqm == bM.disable_wqm;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2020-01-03 17:38:23 +00:00
|
|
|
case Format::MUBUF: {
|
2021-01-21 16:13:34 +00:00
|
|
|
MUBUF_instruction& aM = a->mubuf();
|
|
|
|
|
MUBUF_instruction& bM = b->mubuf();
|
2020-12-22 11:59:26 +01:00
|
|
|
return aM.sync == bM.sync && aM.offset == bM.offset && aM.offen == bM.offen &&
|
2024-05-14 18:34:01 +01:00
|
|
|
aM.idxen == bM.idxen && aM.cache.value == bM.cache.value && aM.tfe == bM.tfe &&
|
|
|
|
|
aM.lds == bM.lds && aM.disable_wqm == bM.disable_wqm;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
case Format::MIMG: {
|
2021-01-21 16:13:34 +00:00
|
|
|
MIMG_instruction& aM = a->mimg();
|
|
|
|
|
MIMG_instruction& bM = b->mimg();
|
2020-12-22 11:59:26 +01:00
|
|
|
return aM.sync == bM.sync && aM.dmask == bM.dmask && aM.unrm == bM.unrm &&
|
2024-05-14 18:34:01 +01:00
|
|
|
aM.cache.value == bM.cache.value && aM.tfe == bM.tfe && aM.da == bM.da &&
|
2021-01-21 16:13:34 +00:00
|
|
|
aM.lwe == bM.lwe && aM.r128 == bM.r128 && aM.a16 == bM.a16 && aM.d16 == bM.d16 &&
|
|
|
|
|
aM.disable_wqm == bM.disable_wqm;
|
2021-06-09 10:14:54 +02:00
|
|
|
}
|
2020-12-22 11:59:26 +01:00
|
|
|
case Format::FLAT:
|
|
|
|
|
case Format::GLOBAL:
|
|
|
|
|
case Format::SCRATCH:
|
|
|
|
|
case Format::EXP:
|
|
|
|
|
case Format::SOPP:
|
|
|
|
|
case Format::PSEUDO_BRANCH:
|
2025-07-23 09:17:35 +02:00
|
|
|
case Format::PSEUDO_BARRIER: UNREACHABLE("unsupported instruction format");
|
2019-09-17 13:22:17 +02:00
|
|
|
default: return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-22 17:26:34 +02:00
|
|
|
using expr_set = aco::unordered_map<Instruction*, uint32_t, InstrHash, InstrPred>;
|
2019-10-19 16:11:13 +02:00
|
|
|
|
|
|
|
|
struct vn_ctx {
|
|
|
|
|
Program* program;
|
2022-08-22 17:26:34 +02:00
|
|
|
monotonic_buffer_resource m;
|
2019-10-19 16:11:13 +02:00
|
|
|
expr_set expr_values;
|
2022-08-22 17:26:34 +02:00
|
|
|
aco::unordered_map<uint32_t, Temp> renames;
|
aco/vn: remove dead instructions early
Dead p_create_vector/p_split_vector left behind by instruction selection slow down
the other passes and negatively affect extract labels in aco_optimizer.
Foz-DB GFX1201:
Totals from 964 (1.20% of 80251) affected shaders:
MaxWaves: 29206 -> 29030 (-0.60%); split: +0.08%, -0.68%
Instrs: 669369 -> 668842 (-0.08%); split: -0.16%, +0.09%
CodeSize: 3385192 -> 3383216 (-0.06%); split: -0.13%, +0.07%
VGPRs: 46788 -> 46848 (+0.13%); split: -0.85%, +0.97%
Latency: 3985660 -> 3892742 (-2.33%); split: -2.54%, +0.21%
InvThroughput: 538296 -> 536761 (-0.29%); split: -0.38%, +0.10%
VClause: 8336 -> 8418 (+0.98%); split: -0.17%, +1.15%
SClause: 17111 -> 17120 (+0.05%); split: -0.20%, +0.25%
Copies: 44393 -> 44239 (-0.35%); split: -1.25%, +0.91%
PreSGPRs: 45417 -> 45419 (+0.00%)
PreVGPRs: 30401 -> 31644 (+4.09%); split: -0.00%, +4.09%
VALU: 348282 -> 348167 (-0.03%); split: -0.15%, +0.12%
SALU: 121454 -> 121410 (-0.04%); split: -0.04%, +0.01%
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35825>
2025-07-07 13:19:33 +02:00
|
|
|
std::vector<uint16_t> uses;
|
2019-11-11 11:41:31 +01:00
|
|
|
|
|
|
|
|
/* The exec id should be the same on the same level of control flow depth.
|
|
|
|
|
* Together with the check for dominator relations, it is safe to assume
|
|
|
|
|
* that the same exec_id also means the same execution mask.
|
|
|
|
|
* Discards increment the exec_id, so that it won't return to the previous value.
|
|
|
|
|
*/
|
|
|
|
|
uint32_t exec_id = 1;
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2022-08-22 17:26:34 +02:00
|
|
|
vn_ctx(Program* program_) : program(program_), m(), expr_values(m), renames(m)
|
2020-11-03 14:40:05 +01:00
|
|
|
{
|
2020-03-10 10:00:32 +01:00
|
|
|
static_assert(sizeof(Temp) == 4, "Temp must fit in 32bits");
|
|
|
|
|
unsigned size = 0;
|
|
|
|
|
for (Block& block : program->blocks)
|
|
|
|
|
size += block.instructions.size();
|
|
|
|
|
expr_values.reserve(size);
|
aco/vn: remove dead instructions early
Dead p_create_vector/p_split_vector left behind by instruction selection slow down
the other passes and negatively affect extract labels in aco_optimizer.
Foz-DB GFX1201:
Totals from 964 (1.20% of 80251) affected shaders:
MaxWaves: 29206 -> 29030 (-0.60%); split: +0.08%, -0.68%
Instrs: 669369 -> 668842 (-0.08%); split: -0.16%, +0.09%
CodeSize: 3385192 -> 3383216 (-0.06%); split: -0.13%, +0.07%
VGPRs: 46788 -> 46848 (+0.13%); split: -0.85%, +0.97%
Latency: 3985660 -> 3892742 (-2.33%); split: -2.54%, +0.21%
InvThroughput: 538296 -> 536761 (-0.29%); split: -0.38%, +0.10%
VClause: 8336 -> 8418 (+0.98%); split: -0.17%, +1.15%
SClause: 17111 -> 17120 (+0.05%); split: -0.20%, +0.25%
Copies: 44393 -> 44239 (-0.35%); split: -1.25%, +0.91%
PreSGPRs: 45417 -> 45419 (+0.00%)
PreVGPRs: 30401 -> 31644 (+4.09%); split: -0.00%, +4.09%
VALU: 348282 -> 348167 (-0.03%); split: -0.15%, +0.12%
SALU: 121454 -> 121410 (-0.04%); split: -0.04%, +0.01%
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35825>
2025-07-07 13:19:33 +02:00
|
|
|
uses = dead_code_analysis(program);
|
2020-03-10 10:00:32 +01:00
|
|
|
}
|
2019-10-19 16:11:13 +02:00
|
|
|
};
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2019-11-26 15:28:54 +01:00
|
|
|
/* dominates() returns true if the parent block dominates the child block and
|
|
|
|
|
* if the parent block is part of the same loop or has a smaller loop nest depth.
|
|
|
|
|
*/
|
2019-10-19 16:11:13 +02:00
|
|
|
bool
|
|
|
|
|
dominates(vn_ctx& ctx, uint32_t parent, uint32_t child)
|
|
|
|
|
{
|
2024-07-11 11:42:28 +01:00
|
|
|
Block& parent_b = ctx.program->blocks[parent];
|
|
|
|
|
Block& child_b = ctx.program->blocks[child];
|
|
|
|
|
if (!dominates_logical(parent_b, child_b) || parent_b.loop_nest_depth > child_b.loop_nest_depth)
|
|
|
|
|
return false;
|
|
|
|
|
if (parent_b.loop_nest_depth == child_b.loop_nest_depth && parent_b.loop_nest_depth == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
2019-11-26 15:28:54 +01:00
|
|
|
unsigned parent_loop_nest_depth = ctx.program->blocks[parent].loop_nest_depth;
|
|
|
|
|
while (parent < child && parent_loop_nest_depth <= ctx.program->blocks[child].loop_nest_depth)
|
2019-10-19 16:11:13 +02:00
|
|
|
child = ctx.program->blocks[child].logical_idom;
|
|
|
|
|
|
|
|
|
|
return parent == child;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 11:59:26 +01:00
|
|
|
/** Returns whether this instruction can safely be removed
|
|
|
|
|
* and replaced by an equal expression.
|
|
|
|
|
* This is in particular true for ALU instructions and
|
|
|
|
|
* read-only memory instructions.
|
|
|
|
|
*
|
|
|
|
|
* Note that expr_set must not be used with instructions
|
|
|
|
|
* which cannot be eliminated.
|
|
|
|
|
*/
|
|
|
|
|
bool
|
|
|
|
|
can_eliminate(aco_ptr<Instruction>& instr)
|
|
|
|
|
{
|
|
|
|
|
switch (instr->format) {
|
|
|
|
|
case Format::FLAT:
|
|
|
|
|
case Format::GLOBAL:
|
|
|
|
|
case Format::SCRATCH:
|
|
|
|
|
case Format::EXP:
|
|
|
|
|
case Format::SOPP:
|
|
|
|
|
case Format::PSEUDO_BRANCH:
|
|
|
|
|
case Format::PSEUDO_BARRIER: return false;
|
|
|
|
|
case Format::DS:
|
|
|
|
|
return instr->opcode == aco_opcode::ds_bpermute_b32 ||
|
|
|
|
|
instr->opcode == aco_opcode::ds_permute_b32 ||
|
|
|
|
|
instr->opcode == aco_opcode::ds_swizzle_b32;
|
|
|
|
|
case Format::SMEM:
|
2020-01-03 17:38:23 +00:00
|
|
|
case Format::MUBUF:
|
2019-09-17 13:22:17 +02:00
|
|
|
case Format::MIMG:
|
2020-12-22 11:59:26 +01:00
|
|
|
case Format::MTBUF:
|
|
|
|
|
if (!get_sync_info(instr.get()).can_reorder())
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (instr->definitions.empty() || instr->opcode == aco_opcode::p_phi ||
|
2023-04-03 21:22:02 +03:00
|
|
|
instr->opcode == aco_opcode::p_linear_phi ||
|
|
|
|
|
instr->opcode == aco_opcode::p_pops_gfx9_add_exiting_wave_id ||
|
2024-12-04 13:36:04 +00:00
|
|
|
instr->opcode == aco_opcode::p_shader_cycles_hi_lo_hi ||
|
2023-04-03 21:22:02 +03:00
|
|
|
instr->definitions[0].isNoCSE())
|
2020-12-22 11:59:26 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 13:47:25 +02:00
|
|
|
bool
|
|
|
|
|
is_trivial_phi(Block& block, Instruction* instr)
|
|
|
|
|
{
|
|
|
|
|
if (!is_phi(instr))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
/* Logical LCSSA phis must be kept in order to prevent the optimizer
|
|
|
|
|
* from doing invalid transformations. */
|
|
|
|
|
if (instr->opcode == aco_opcode::p_phi && (block.kind & block_kind_loop_exit))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return std::all_of(instr->operands.begin(), instr->operands.end(),
|
|
|
|
|
[&](Operand& op) { return op == instr->operands[0]; });
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
void
|
|
|
|
|
process_block(vn_ctx& ctx, Block& block)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
|
|
|
|
std::vector<aco_ptr<Instruction>> new_instructions;
|
|
|
|
|
new_instructions.reserve(block.instructions.size());
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
for (aco_ptr<Instruction>& instr : block.instructions) {
|
aco/vn: remove dead instructions early
Dead p_create_vector/p_split_vector left behind by instruction selection slow down
the other passes and negatively affect extract labels in aco_optimizer.
Foz-DB GFX1201:
Totals from 964 (1.20% of 80251) affected shaders:
MaxWaves: 29206 -> 29030 (-0.60%); split: +0.08%, -0.68%
Instrs: 669369 -> 668842 (-0.08%); split: -0.16%, +0.09%
CodeSize: 3385192 -> 3383216 (-0.06%); split: -0.13%, +0.07%
VGPRs: 46788 -> 46848 (+0.13%); split: -0.85%, +0.97%
Latency: 3985660 -> 3892742 (-2.33%); split: -2.54%, +0.21%
InvThroughput: 538296 -> 536761 (-0.29%); split: -0.38%, +0.10%
VClause: 8336 -> 8418 (+0.98%); split: -0.17%, +1.15%
SClause: 17111 -> 17120 (+0.05%); split: -0.20%, +0.25%
Copies: 44393 -> 44239 (-0.35%); split: -1.25%, +0.91%
PreSGPRs: 45417 -> 45419 (+0.00%)
PreVGPRs: 30401 -> 31644 (+4.09%); split: -0.00%, +4.09%
VALU: 348282 -> 348167 (-0.03%); split: -0.15%, +0.12%
SALU: 121454 -> 121410 (-0.04%); split: -0.04%, +0.01%
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35825>
2025-07-07 13:19:33 +02:00
|
|
|
/* Clean up dead create_vector/split_vector left behind by instruction selection. */
|
|
|
|
|
if (is_dead(ctx.uses, instr.get()))
|
|
|
|
|
continue;
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
/* first, rename operands */
|
|
|
|
|
for (Operand& op : instr->operands) {
|
|
|
|
|
if (!op.isTemp())
|
|
|
|
|
continue;
|
2019-10-19 16:11:13 +02:00
|
|
|
auto it = ctx.renames.find(op.tempId());
|
|
|
|
|
if (it != ctx.renames.end())
|
2019-09-17 13:22:17 +02:00
|
|
|
op.setTemp(it->second);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 16:47:13 +01:00
|
|
|
if (instr->opcode == aco_opcode::p_discard_if ||
|
aco: insert a single p_end_wqm after the last derivative calculation
This new instruction replaces p_wqm.
Totals from 28065 (36.65% of 76572) affected shaders: (GFX11)
MaxWaves: 823922 -> 823952 (+0.00%); split: +0.01%, -0.01%
Instrs: 22221375 -> 22180465 (-0.18%); split: -0.26%, +0.08%
CodeSize: 117310676 -> 117040684 (-0.23%); split: -0.30%, +0.07%
VGPRs: 1183476 -> 1186656 (+0.27%); split: -0.19%, +0.46%
SpillSGPRs: 2305 -> 2302 (-0.13%)
Latency: 176559310 -> 176427793 (-0.07%); split: -0.21%, +0.14%
InvThroughput: 26245204 -> 26195550 (-0.19%); split: -0.26%, +0.07%
VClause: 368058 -> 369460 (+0.38%); split: -0.21%, +0.59%
SClause: 857077 -> 842588 (-1.69%); split: -2.06%, +0.37%
Copies: 1245650 -> 1249434 (+0.30%); split: -0.33%, +0.63%
Branches: 394837 -> 396070 (+0.31%); split: -0.01%, +0.32%
PreSGPRs: 1019139 -> 1019567 (+0.04%); split: -0.02%, +0.06%
PreVGPRs: 925739 -> 931860 (+0.66%); split: -0.00%, +0.66%
Changes are due to scheduling and re-enabling cross-lane optimizations.
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25038>
2023-09-02 11:14:33 +02:00
|
|
|
instr->opcode == aco_opcode::p_demote_to_helper || instr->opcode == aco_opcode::p_end_wqm)
|
2019-11-29 16:47:13 +01:00
|
|
|
ctx.exec_id++;
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
/* simple copy-propagation through renaming */
|
2020-10-15 15:18:40 +01:00
|
|
|
bool copy_instr =
|
2024-04-08 13:47:25 +02:00
|
|
|
is_trivial_phi(block, instr.get()) || instr->opcode == aco_opcode::p_parallelcopy ||
|
2020-10-15 15:18:40 +01:00
|
|
|
(instr->opcode == aco_opcode::p_create_vector && instr->operands.size() == 1);
|
2020-10-14 15:35:20 +01:00
|
|
|
if (copy_instr && !instr->definitions[0].isFixed() && instr->operands[0].isTemp() &&
|
2020-10-15 15:18:40 +01:00
|
|
|
instr->operands[0].regClass() == instr->definitions[0].regClass()) {
|
2019-10-19 16:11:13 +02:00
|
|
|
ctx.renames[instr->definitions[0].tempId()] = instr->operands[0].getTemp();
|
2020-10-26 19:22:14 +00:00
|
|
|
continue;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-08 13:47:25 +02:00
|
|
|
if (!can_eliminate(instr)) {
|
|
|
|
|
new_instructions.emplace_back(std::move(instr));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
instr->pass_flags = ctx.exec_id;
|
|
|
|
|
std::pair<expr_set::iterator, bool> res = ctx.expr_values.emplace(instr.get(), block.index);
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
/* if there was already an expression with the same value number */
|
|
|
|
|
if (!res.second) {
|
2019-10-19 16:11:13 +02:00
|
|
|
Instruction* orig_instr = res.first->first;
|
2019-09-17 13:22:17 +02:00
|
|
|
assert(instr->definitions.size() == orig_instr->definitions.size());
|
2019-10-19 16:11:13 +02:00
|
|
|
/* check if the original instruction dominates the current one */
|
2019-11-09 20:51:45 +00:00
|
|
|
if (dominates(ctx, res.first->second, block.index) &&
|
|
|
|
|
ctx.program->blocks[res.first->second].fp_mode.canReplace(block.fp_mode)) {
|
2019-10-19 16:11:13 +02:00
|
|
|
for (unsigned i = 0; i < instr->definitions.size(); i++) {
|
|
|
|
|
assert(instr->definitions[i].regClass() == orig_instr->definitions[i].regClass());
|
2019-11-29 16:47:13 +01:00
|
|
|
assert(instr->definitions[i].isTemp());
|
2019-10-19 16:11:13 +02:00
|
|
|
ctx.renames[instr->definitions[i].tempId()] = orig_instr->definitions[i].getTemp();
|
2020-05-15 13:58:20 +01:00
|
|
|
if (instr->definitions[i].isPrecise())
|
|
|
|
|
orig_instr->definitions[i].setPrecise(true);
|
2024-09-17 19:09:16 +02:00
|
|
|
if (instr->definitions[i].isSZPreserve())
|
|
|
|
|
orig_instr->definitions[i].setSZPreserve(true);
|
|
|
|
|
if (instr->definitions[i].isInfPreserve())
|
|
|
|
|
orig_instr->definitions[i].setInfPreserve(true);
|
|
|
|
|
if (instr->definitions[i].isNaNPreserve())
|
|
|
|
|
orig_instr->definitions[i].setNaNPreserve(true);
|
2019-10-15 17:25:57 +01:00
|
|
|
/* SPIR_V spec says that an instruction marked with NUW wrapping
|
|
|
|
|
* around is undefined behaviour, so we can break additions in
|
|
|
|
|
* other contexts.
|
|
|
|
|
*/
|
|
|
|
|
if (instr->definitions[i].isNUW())
|
|
|
|
|
orig_instr->definitions[i].setNUW(true);
|
2019-10-19 16:11:13 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ctx.expr_values.erase(res.first);
|
|
|
|
|
ctx.expr_values.emplace(instr.get(), block.index);
|
|
|
|
|
new_instructions.emplace_back(std::move(instr));
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
new_instructions.emplace_back(std::move(instr));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
block.instructions = std::move(new_instructions);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-08-22 17:26:34 +02:00
|
|
|
rename_phi_operands(Block& block, aco::unordered_map<uint32_t, Temp>& renames)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
|
|
|
|
for (aco_ptr<Instruction>& phi : block.instructions) {
|
2024-04-08 13:47:25 +02:00
|
|
|
if (!is_phi(phi))
|
2019-09-17 13:22:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
for (Operand& op : phi->operands) {
|
|
|
|
|
if (!op.isTemp())
|
|
|
|
|
continue;
|
|
|
|
|
auto it = renames.find(op.tempId());
|
|
|
|
|
if (it != renames.end())
|
|
|
|
|
op.setTemp(it->second);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} /* end namespace */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
value_numbering(Program* program)
|
|
|
|
|
{
|
2019-10-19 16:11:13 +02:00
|
|
|
vn_ctx ctx(program);
|
2019-11-11 11:41:31 +01:00
|
|
|
std::vector<unsigned> loop_headers;
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
for (Block& block : program->blocks) {
|
2019-11-11 11:41:31 +01:00
|
|
|
assert(ctx.exec_id > 0);
|
|
|
|
|
/* decrement exec_id when leaving nested control flow */
|
|
|
|
|
if (block.kind & block_kind_loop_header)
|
|
|
|
|
loop_headers.push_back(block.index);
|
|
|
|
|
if (block.kind & block_kind_merge) {
|
|
|
|
|
ctx.exec_id--;
|
|
|
|
|
} else if (block.kind & block_kind_loop_exit) {
|
2019-11-29 16:47:13 +01:00
|
|
|
ctx.exec_id -= program->blocks[loop_headers.back()].linear_preds.size();
|
|
|
|
|
ctx.exec_id -= block.linear_preds.size();
|
2019-11-11 11:41:31 +01:00
|
|
|
loop_headers.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 17:14:51 +01:00
|
|
|
if (block.logical_idom == (int)block.index)
|
|
|
|
|
ctx.expr_values.clear();
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
if (block.logical_idom != -1)
|
|
|
|
|
process_block(ctx, block);
|
|
|
|
|
else
|
|
|
|
|
rename_phi_operands(block, ctx.renames);
|
|
|
|
|
|
2019-11-11 11:41:31 +01:00
|
|
|
/* increment exec_id when entering nested control flow */
|
|
|
|
|
if (block.kind & block_kind_branch || block.kind & block_kind_loop_preheader ||
|
2022-01-31 15:11:22 +01:00
|
|
|
block.kind & block_kind_break || block.kind & block_kind_continue)
|
2019-11-11 11:41:31 +01:00
|
|
|
ctx.exec_id++;
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-19 16:11:13 +02:00
|
|
|
/* rename loop header phi operands */
|
|
|
|
|
for (Block& block : program->blocks) {
|
|
|
|
|
if (block.kind & block_kind_loop_header)
|
|
|
|
|
rename_phi_operands(block, ctx.renames);
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace aco
|