2019-09-17 13:22:17 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2018 Valve Corporation
|
|
|
|
|
* Copyright © 2018 Google
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "aco_ir.h"
|
2021-06-09 10:14:54 +02:00
|
|
|
|
2019-09-13 16:41:00 +01:00
|
|
|
#include "util/u_math.h"
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace aco {
|
2021-06-09 10:14:54 +02:00
|
|
|
RegisterDemand
|
|
|
|
|
get_live_changes(aco_ptr<Instruction>& instr)
|
2020-02-21 20:14:03 +00:00
|
|
|
{
|
|
|
|
|
RegisterDemand changes;
|
|
|
|
|
for (const Definition& def : instr->definitions) {
|
|
|
|
|
if (!def.isTemp() || def.isKill())
|
|
|
|
|
continue;
|
|
|
|
|
changes += def.getTemp();
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2020-02-21 20:14:03 +00:00
|
|
|
for (const Operand& op : instr->operands) {
|
|
|
|
|
if (!op.isTemp() || !op.isFirstKill())
|
|
|
|
|
continue;
|
|
|
|
|
changes -= op.getTemp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return changes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
RegisterDemand
|
|
|
|
|
get_temp_registers(aco_ptr<Instruction>& instr)
|
2020-02-21 20:14:03 +00:00
|
|
|
{
|
|
|
|
|
RegisterDemand temp_registers;
|
2020-02-21 15:46:39 +00:00
|
|
|
|
2020-02-21 20:14:03 +00:00
|
|
|
for (Definition def : instr->definitions) {
|
|
|
|
|
if (!def.isTemp())
|
|
|
|
|
continue;
|
|
|
|
|
if (def.isKill())
|
|
|
|
|
temp_registers += def.getTemp();
|
|
|
|
|
}
|
2020-02-21 15:46:39 +00:00
|
|
|
|
|
|
|
|
for (Operand op : instr->operands) {
|
|
|
|
|
if (op.isTemp() && op.isLateKill() && op.isFirstKill())
|
|
|
|
|
temp_registers += op.getTemp();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 20:14:03 +00:00
|
|
|
return temp_registers;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
RegisterDemand
|
|
|
|
|
get_demand_before(RegisterDemand demand, aco_ptr<Instruction>& instr,
|
|
|
|
|
aco_ptr<Instruction>& instr_before)
|
2020-02-21 20:14:03 +00:00
|
|
|
{
|
|
|
|
|
demand -= get_live_changes(instr);
|
|
|
|
|
demand -= get_temp_registers(instr);
|
|
|
|
|
if (instr_before)
|
|
|
|
|
demand += get_temp_registers(instr_before);
|
|
|
|
|
return demand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
2021-07-26 17:55:48 +01:00
|
|
|
struct PhiInfo {
|
|
|
|
|
uint16_t logical_phi_sgpr_ops = 0;
|
|
|
|
|
uint16_t linear_phi_ops = 0;
|
|
|
|
|
uint16_t linear_phi_defs = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-16 10:56:26 +01:00
|
|
|
bool
|
|
|
|
|
instr_needs_vcc(Instruction* instr)
|
|
|
|
|
{
|
|
|
|
|
if (instr->isVOPC())
|
|
|
|
|
return true;
|
|
|
|
|
if (instr->isVOP2() && !instr->isVOP3()) {
|
|
|
|
|
if (instr->operands.size() == 3 && instr->operands[2].isTemp() &&
|
|
|
|
|
instr->operands[2].regClass().type() == RegType::sgpr)
|
|
|
|
|
return true;
|
|
|
|
|
if (instr->definitions.size() == 2)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
void
|
2021-07-26 17:55:48 +01:00
|
|
|
process_live_temps_per_block(Program* program, live& lives, Block* block, unsigned& worklist,
|
|
|
|
|
std::vector<PhiInfo>& phi_info)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
|
|
|
|
std::vector<RegisterDemand>& register_demand = lives.register_demand[block->index];
|
|
|
|
|
RegisterDemand new_demand;
|
|
|
|
|
|
|
|
|
|
register_demand.resize(block->instructions.size());
|
2021-04-19 11:24:03 +01:00
|
|
|
RegisterDemand block_register_demand;
|
2020-09-14 16:45:55 +01:00
|
|
|
IDSet live = lives.live_out[block->index];
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2020-03-10 13:39:42 +01:00
|
|
|
/* initialize register demand */
|
2020-09-14 16:45:55 +01:00
|
|
|
for (unsigned t : live)
|
|
|
|
|
new_demand += Temp(t, program->temp_rc[t]);
|
2021-07-26 17:55:48 +01:00
|
|
|
new_demand.sgpr -= phi_info[block->index].logical_phi_sgpr_ops;
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
/* traverse the instructions backwards */
|
2019-10-29 11:56:09 +01:00
|
|
|
int idx;
|
2021-06-09 10:14:54 +02:00
|
|
|
for (idx = block->instructions.size() - 1; idx >= 0; idx--) {
|
|
|
|
|
Instruction* insn = block->instructions[idx].get();
|
2019-10-29 11:56:09 +01:00
|
|
|
if (is_phi(insn))
|
|
|
|
|
break;
|
|
|
|
|
|
2022-03-16 10:56:26 +01:00
|
|
|
program->needs_vcc |= instr_needs_vcc(insn);
|
aco: always set exec_live=false
Register demand calculation for exec masks doesn't always match
get_live_changes() and get_temp_registers(). For now, just set
exec_live=false.
fossil-db (GFX10.3):
Totals from 108230 (77.64% of 139391) affected shaders:
SGPRs: 5759658 -> 5756818 (-0.05%); split: -0.08%, +0.03%
VGPRs: 4061104 -> 4061248 (+0.00%); split: -0.00%, +0.01%
SpillSGPRs: 14114 -> 15198 (+7.68%); split: -0.10%, +7.78%
CodeSize: 266548396 -> 266603288 (+0.02%); split: -0.01%, +0.03%
MaxWaves: 1390885 -> 1390855 (-0.00%); split: +0.00%, -0.00%
Instrs: 50983353 -> 50992972 (+0.02%); split: -0.02%, +0.04%
Cycles: 1733042048 -> 1735443264 (+0.14%); split: -0.02%, +0.16%
VMEM: 41933625 -> 41914722 (-0.05%); split: +0.04%, -0.09%
SMEM: 7197675 -> 7197789 (+0.00%); split: +0.16%, -0.16%
VClause: 1050885 -> 1050978 (+0.01%); split: -0.02%, +0.03%
SClause: 2074913 -> 2071844 (-0.15%); split: -0.23%, +0.08%
Copies: 3181464 -> 3188125 (+0.21%); split: -0.38%, +0.59%
Branches: 1127526 -> 1127716 (+0.02%); split: -0.10%, +0.12%
PreSGPRs: 3376687 -> 3586076 (+6.20%); split: -0.00%, +6.20%
PreVGPRs: 3339740 -> 3339811 (+0.00%)
Signed-off-by: Rhys Perry <pendingchaos02@gmail.com>
Reviewed-by: Daniel Schürmann <daniel@schuermann.dev>
Cc: mesa-stable
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8807>
2021-01-27 14:04:10 +00:00
|
|
|
register_demand[idx] = RegisterDemand(new_demand.vgpr, new_demand.sgpr);
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
/* KILL */
|
|
|
|
|
for (Definition& definition : insn->definitions) {
|
|
|
|
|
if (!definition.isTemp()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-03-16 10:56:26 +01:00
|
|
|
if (definition.isFixed() && definition.physReg() == vcc)
|
2020-02-21 12:23:28 +00:00
|
|
|
program->needs_vcc = true;
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
const Temp temp = definition.getTemp();
|
2020-09-14 16:45:55 +01:00
|
|
|
const size_t n = live.erase(temp.id());
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
|
new_demand -= temp;
|
|
|
|
|
definition.setKill(false);
|
|
|
|
|
} else {
|
2020-02-21 15:46:39 +00:00
|
|
|
register_demand[idx] += temp;
|
2019-09-17 13:22:17 +02:00
|
|
|
definition.setKill(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* GEN */
|
2019-10-29 11:56:09 +01:00
|
|
|
if (insn->opcode == aco_opcode::p_logical_end) {
|
2021-07-26 17:55:48 +01:00
|
|
|
new_demand.sgpr += phi_info[block->index].logical_phi_sgpr_ops;
|
2019-09-17 13:22:17 +02:00
|
|
|
} else {
|
2020-01-21 14:24:01 +00:00
|
|
|
/* we need to do this in a separate loop because the next one can
|
|
|
|
|
* setKill() for several operands at once and we don't want to
|
|
|
|
|
* overwrite that in a later iteration */
|
|
|
|
|
for (Operand& op : insn->operands)
|
|
|
|
|
op.setKill(false);
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
for (unsigned i = 0; i < insn->operands.size(); ++i) {
|
2019-09-17 13:22:17 +02:00
|
|
|
Operand& operand = insn->operands[i];
|
2020-02-21 12:23:28 +00:00
|
|
|
if (!operand.isTemp())
|
2019-09-17 13:22:17 +02:00
|
|
|
continue;
|
2020-02-21 12:23:28 +00:00
|
|
|
if (operand.isFixed() && operand.physReg() == vcc)
|
|
|
|
|
program->needs_vcc = true;
|
2019-09-17 13:22:17 +02:00
|
|
|
const Temp temp = operand.getTemp();
|
2020-09-14 16:45:55 +01:00
|
|
|
const bool inserted = live.insert(temp.id()).second;
|
2019-09-17 13:22:17 +02:00
|
|
|
if (inserted) {
|
|
|
|
|
operand.setFirstKill(true);
|
|
|
|
|
for (unsigned j = i + 1; j < insn->operands.size(); ++j) {
|
2021-06-09 10:14:54 +02:00
|
|
|
if (insn->operands[j].isTemp() &&
|
|
|
|
|
insn->operands[j].tempId() == operand.tempId()) {
|
2019-09-17 13:22:17 +02:00
|
|
|
insn->operands[j].setFirstKill(false);
|
|
|
|
|
insn->operands[j].setKill(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-21 15:46:39 +00:00
|
|
|
if (operand.isLateKill())
|
|
|
|
|
register_demand[idx] += temp;
|
2019-09-17 13:22:17 +02:00
|
|
|
new_demand += temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 11:24:03 +01:00
|
|
|
block_register_demand.update(register_demand[idx]);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 11:56:09 +01:00
|
|
|
/* update block's register demand for a last time */
|
2021-04-19 11:24:03 +01:00
|
|
|
block_register_demand.update(new_demand);
|
2021-04-20 17:35:41 +01:00
|
|
|
if (program->progress < CompilationProgress::after_ra)
|
2021-04-19 11:24:03 +01:00
|
|
|
block->register_demand = block_register_demand;
|
2019-10-29 11:56:09 +01:00
|
|
|
|
|
|
|
|
/* handle phi definitions */
|
2021-07-26 17:55:48 +01:00
|
|
|
uint16_t linear_phi_defs = 0;
|
2019-10-29 11:56:09 +01:00
|
|
|
int phi_idx = idx;
|
|
|
|
|
while (phi_idx >= 0) {
|
|
|
|
|
register_demand[phi_idx] = new_demand;
|
2021-06-09 10:14:54 +02:00
|
|
|
Instruction* insn = block->instructions[phi_idx].get();
|
2019-10-29 11:56:09 +01:00
|
|
|
|
2021-02-04 15:55:23 +01:00
|
|
|
assert(is_phi(insn) && insn->definitions.size() == 1);
|
|
|
|
|
if (!insn->definitions[0].isTemp()) {
|
|
|
|
|
assert(insn->definitions[0].isFixed() && insn->definitions[0].physReg() == exec);
|
|
|
|
|
phi_idx--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-10-29 11:56:09 +01:00
|
|
|
Definition& definition = insn->definitions[0];
|
2022-03-16 10:56:26 +01:00
|
|
|
if (definition.isFixed() && definition.physReg() == vcc)
|
2020-02-21 12:23:28 +00:00
|
|
|
program->needs_vcc = true;
|
2019-10-29 11:56:09 +01:00
|
|
|
const Temp temp = definition.getTemp();
|
2020-09-14 16:45:55 +01:00
|
|
|
const size_t n = live.erase(temp.id());
|
2019-10-29 11:56:09 +01:00
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
|
definition.setKill(false);
|
|
|
|
|
else
|
|
|
|
|
definition.setKill(true);
|
|
|
|
|
|
2021-07-26 17:55:48 +01:00
|
|
|
if (insn->opcode == aco_opcode::p_linear_phi) {
|
|
|
|
|
assert(definition.getTemp().type() == RegType::sgpr);
|
|
|
|
|
linear_phi_defs += definition.size();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 11:56:09 +01:00
|
|
|
phi_idx--;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 17:55:48 +01:00
|
|
|
for (unsigned pred_idx : block->linear_preds)
|
|
|
|
|
phi_info[pred_idx].linear_phi_defs = linear_phi_defs;
|
|
|
|
|
|
2020-03-10 13:39:42 +01:00
|
|
|
/* now, we need to merge the live-ins into the live-out sets */
|
2022-08-17 00:23:36 +02:00
|
|
|
bool fast_merge =
|
|
|
|
|
block->logical_preds.size() == 0 || block->logical_preds == block->linear_preds;
|
2019-09-17 13:22:17 +02:00
|
|
|
|
2020-03-10 13:39:42 +01:00
|
|
|
#ifndef NDEBUG
|
2022-08-17 00:23:36 +02:00
|
|
|
if ((block->linear_preds.empty() && !live.empty()) ||
|
|
|
|
|
(block->logical_preds.empty() && new_demand.vgpr > 0))
|
|
|
|
|
fast_merge = false; /* we might have errors */
|
2020-03-10 13:39:42 +01:00
|
|
|
#endif
|
|
|
|
|
|
2022-08-17 00:23:36 +02:00
|
|
|
if (fast_merge) {
|
|
|
|
|
for (unsigned pred_idx : block->linear_preds) {
|
|
|
|
|
if (lives.live_out[pred_idx].insert(live))
|
2021-07-14 14:56:48 +02:00
|
|
|
worklist = std::max(worklist, pred_idx + 1);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2022-08-17 00:23:36 +02:00
|
|
|
} else {
|
|
|
|
|
for (unsigned t : live) {
|
|
|
|
|
RegClass rc = program->temp_rc[t];
|
|
|
|
|
std::vector<unsigned>& preds = rc.is_linear() ? block->linear_preds : block->logical_preds;
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
if (preds.empty())
|
|
|
|
|
aco_err(program, "Temporary never defined or are defined after use: %%%d in BB%d", t,
|
|
|
|
|
block->index);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
for (unsigned pred_idx : preds) {
|
|
|
|
|
auto it = lives.live_out[pred_idx].insert(t);
|
|
|
|
|
if (it.second)
|
|
|
|
|
worklist = std::max(worklist, pred_idx + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 11:56:09 +01:00
|
|
|
/* handle phi operands */
|
|
|
|
|
phi_idx = idx;
|
|
|
|
|
while (phi_idx >= 0) {
|
2021-06-09 10:14:54 +02:00
|
|
|
Instruction* insn = block->instructions[phi_idx].get();
|
2019-10-29 11:56:09 +01:00
|
|
|
assert(is_phi(insn));
|
|
|
|
|
/* directly insert into the predecessors live-out set */
|
2021-06-09 10:14:54 +02:00
|
|
|
std::vector<unsigned>& preds =
|
|
|
|
|
insn->opcode == aco_opcode::p_phi ? block->logical_preds : block->linear_preds;
|
2019-10-29 11:56:09 +01:00
|
|
|
for (unsigned i = 0; i < preds.size(); ++i) {
|
2021-06-09 10:14:54 +02:00
|
|
|
Operand& operand = insn->operands[i];
|
2020-02-21 12:23:28 +00:00
|
|
|
if (!operand.isTemp())
|
2019-10-29 11:56:09 +01:00
|
|
|
continue;
|
2020-02-21 12:23:28 +00:00
|
|
|
if (operand.isFixed() && operand.physReg() == vcc)
|
|
|
|
|
program->needs_vcc = true;
|
2019-10-29 11:56:09 +01:00
|
|
|
/* check if we changed an already processed block */
|
2020-09-14 16:45:55 +01:00
|
|
|
const bool inserted = lives.live_out[preds[i]].insert(operand.tempId()).second;
|
2019-10-29 11:56:09 +01:00
|
|
|
if (inserted) {
|
2021-07-14 14:56:48 +02:00
|
|
|
worklist = std::max(worklist, preds[i] + 1);
|
2021-07-26 17:55:48 +01:00
|
|
|
if (insn->opcode == aco_opcode::p_phi && operand.getTemp().type() == RegType::sgpr) {
|
|
|
|
|
phi_info[preds[i]].logical_phi_sgpr_ops += operand.size();
|
|
|
|
|
} else if (insn->opcode == aco_opcode::p_linear_phi) {
|
|
|
|
|
assert(operand.getTemp().type() == RegType::sgpr);
|
|
|
|
|
phi_info[preds[i]].linear_phi_ops += operand.size();
|
|
|
|
|
}
|
2019-10-29 11:56:09 +01:00
|
|
|
}
|
2021-03-04 16:31:22 +01:00
|
|
|
|
|
|
|
|
/* set if the operand is killed by this (or another) phi instruction */
|
|
|
|
|
operand.setKill(!live.count(operand.tempId()));
|
2019-10-29 11:56:09 +01:00
|
|
|
}
|
|
|
|
|
phi_idx--;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-10 13:39:42 +01:00
|
|
|
assert(block->index != 0 || (new_demand == RegisterDemand() && live.empty()));
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
2019-12-18 16:18:35 +00:00
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
unsigned
|
|
|
|
|
calc_waves_per_workgroup(Program* program)
|
2019-12-18 16:18:35 +00:00
|
|
|
{
|
2020-03-12 16:28:48 +01:00
|
|
|
/* When workgroup size is not known, just go with wave_size */
|
2021-06-09 10:14:54 +02:00
|
|
|
unsigned workgroup_size =
|
|
|
|
|
program->workgroup_size == UINT_MAX ? program->wave_size : program->workgroup_size;
|
2020-03-12 16:28:48 +01:00
|
|
|
|
2019-12-18 16:18:35 +00:00
|
|
|
return align(workgroup_size, program->wave_size) / program->wave_size;
|
|
|
|
|
}
|
2019-09-17 13:22:17 +02:00
|
|
|
} /* end namespace */
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t
|
|
|
|
|
get_extra_sgprs(Program* program)
|
2019-09-13 16:41:00 +01:00
|
|
|
{
|
2022-05-19 14:12:08 +01:00
|
|
|
/* We don't use this register on GFX6-8 and it's removed on GFX10+. */
|
2023-03-15 00:14:07 +01:00
|
|
|
bool needs_flat_scr = program->config->scratch_bytes_per_wave && program->gfx_level == GFX9;
|
2022-05-19 14:12:08 +01:00
|
|
|
|
2022-05-12 02:50:17 -04:00
|
|
|
if (program->gfx_level >= GFX10) {
|
2021-01-28 13:07:11 +00:00
|
|
|
assert(!program->dev.xnack_enabled);
|
aco: use VCC as regular SGPR pair on GFX10
There is no need to reserve it for special purposes, only.
Totals from 139391 (100.00% of 139391) affected shaders (Navi10):
VGPRs: 4738296 -> 4738156 (-0.00%); split: -0.01%, +0.00%
SpillSGPRs: 16188 -> 14968 (-7.54%); split: -7.60%, +0.06%
CodeSize: 294204472 -> 294118048 (-0.03%); split: -0.04%, +0.01%
MaxWaves: 2119584 -> 2119619 (+0.00%); split: +0.00%, -0.00%
Instrs: 56075079 -> 56056235 (-0.03%); split: -0.05%, +0.01%
Cycles: 1757781564 -> 1755354032 (-0.14%); split: -0.16%, +0.02%
VMEM: 52995887 -> 52996319 (+0.00%); split: +0.07%, -0.07%
SMEM: 9005338 -> 9004858 (-0.01%); split: +0.16%, -0.17%
VClause: 1178436 -> 1178331 (-0.01%); split: -0.02%, +0.01%
SClause: 2403649 -> 2404542 (+0.04%); split: -0.14%, +0.18%
Copies: 3447073 -> 3432417 (-0.43%); split: -0.66%, +0.23%
Branches: 1166542 -> 1166422 (-0.01%); split: -0.11%, +0.10%
PreSGPRs: 4229322 -> 4235538 (+0.15%)
PreVGPRs: 3817111 -> 3817040 (-0.00%)
Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8921>
2021-02-02 17:46:35 +01:00
|
|
|
return 0;
|
2022-05-12 02:50:17 -04:00
|
|
|
} else if (program->gfx_level >= GFX8) {
|
2022-05-19 14:12:08 +01:00
|
|
|
if (needs_flat_scr)
|
2019-09-13 16:41:00 +01:00
|
|
|
return 6;
|
2021-01-28 13:07:11 +00:00
|
|
|
else if (program->dev.xnack_enabled)
|
2019-09-13 16:41:00 +01:00
|
|
|
return 4;
|
|
|
|
|
else if (program->needs_vcc)
|
|
|
|
|
return 2;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
2021-01-28 13:07:11 +00:00
|
|
|
assert(!program->dev.xnack_enabled);
|
2022-05-19 14:12:08 +01:00
|
|
|
if (needs_flat_scr)
|
2019-09-13 16:41:00 +01:00
|
|
|
return 4;
|
|
|
|
|
else if (program->needs_vcc)
|
|
|
|
|
return 2;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t
|
|
|
|
|
get_sgpr_alloc(Program* program, uint16_t addressable_sgprs)
|
2019-09-13 16:41:00 +01:00
|
|
|
{
|
|
|
|
|
uint16_t sgprs = addressable_sgprs + get_extra_sgprs(program);
|
2021-01-28 13:07:11 +00:00
|
|
|
uint16_t granule = program->dev.sgpr_alloc_granule;
|
2021-02-02 17:33:09 +01:00
|
|
|
return ALIGN_NPOT(std::max(sgprs, granule), granule);
|
2019-09-13 16:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t
|
|
|
|
|
get_vgpr_alloc(Program* program, uint16_t addressable_vgprs)
|
2019-12-03 14:21:16 +00:00
|
|
|
{
|
2021-01-28 13:07:11 +00:00
|
|
|
assert(addressable_vgprs <= program->dev.vgpr_limit);
|
|
|
|
|
uint16_t granule = program->dev.vgpr_alloc_granule;
|
2022-09-26 17:18:48 +01:00
|
|
|
return ALIGN_NPOT(std::max(addressable_vgprs, granule), granule);
|
2019-12-03 14:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
unsigned
|
|
|
|
|
round_down(unsigned a, unsigned b)
|
2019-09-13 16:41:00 +01:00
|
|
|
{
|
2021-02-05 14:36:39 +01:00
|
|
|
return a - (a % b);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t
|
|
|
|
|
get_addr_sgpr_from_waves(Program* program, uint16_t waves)
|
2021-02-05 14:36:39 +01:00
|
|
|
{
|
|
|
|
|
/* it's not possible to allocate more than 128 SGPRs */
|
2021-01-28 13:07:11 +00:00
|
|
|
uint16_t sgprs = std::min(program->dev.physical_sgprs / waves, 128);
|
|
|
|
|
sgprs = round_down(sgprs, program->dev.sgpr_alloc_granule);
|
2021-02-02 17:33:09 +01:00
|
|
|
sgprs -= get_extra_sgprs(program);
|
2021-01-28 13:07:11 +00:00
|
|
|
return std::min(sgprs, program->dev.sgpr_limit);
|
2019-09-13 16:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t
|
|
|
|
|
get_addr_vgpr_from_waves(Program* program, uint16_t waves)
|
2019-12-03 14:21:16 +00:00
|
|
|
{
|
2022-09-26 17:18:48 +01:00
|
|
|
uint16_t vgprs = program->dev.physical_vgprs / waves;
|
|
|
|
|
vgprs = vgprs / program->dev.vgpr_alloc_granule * program->dev.vgpr_alloc_granule;
|
2021-02-05 14:38:08 +01:00
|
|
|
vgprs -= program->config->num_shared_vgprs / 2;
|
2021-01-28 13:07:11 +00:00
|
|
|
return std::min(vgprs, program->dev.vgpr_limit);
|
2019-12-03 14:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
void
|
|
|
|
|
calc_min_waves(Program* program)
|
2019-12-18 16:18:35 +00:00
|
|
|
{
|
|
|
|
|
unsigned waves_per_workgroup = calc_waves_per_workgroup(program);
|
2021-01-28 13:07:11 +00:00
|
|
|
unsigned simd_per_cu_wgp = program->dev.simd_per_cu * (program->wgp_mode ? 2 : 1);
|
2019-12-18 16:18:35 +00:00
|
|
|
program->min_waves = DIV_ROUND_UP(waves_per_workgroup, simd_per_cu_wgp);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 11:32:56 +02:00
|
|
|
uint16_t
|
|
|
|
|
max_suitable_waves(Program* program, uint16_t waves)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
2022-04-19 11:32:56 +02:00
|
|
|
unsigned num_simd = program->dev.simd_per_cu * (program->wgp_mode ? 2 : 1);
|
|
|
|
|
unsigned waves_per_workgroup = calc_waves_per_workgroup(program);
|
|
|
|
|
unsigned num_workgroups = waves * num_simd / waves_per_workgroup;
|
|
|
|
|
|
|
|
|
|
/* Adjust #workgroups for LDS */
|
|
|
|
|
unsigned lds_per_workgroup = align(program->config->lds_size * program->dev.lds_encoding_granule,
|
|
|
|
|
program->dev.lds_alloc_granule);
|
|
|
|
|
|
|
|
|
|
if (program->stage == fragment_fs) {
|
|
|
|
|
/* PS inputs are moved from PC (parameter cache) to LDS before PS waves are launched.
|
|
|
|
|
* Each PS input occupies 3x vec4 of LDS space. See Figure 10.3 in GCN3 ISA manual.
|
|
|
|
|
* These limit occupancy the same way as other stages' LDS usage does.
|
|
|
|
|
*/
|
|
|
|
|
unsigned lds_bytes_per_interp = 3 * 16;
|
2022-05-05 11:32:53 +10:00
|
|
|
unsigned lds_param_bytes = lds_bytes_per_interp * program->info.ps.num_interp;
|
2022-04-19 11:32:56 +02:00
|
|
|
lds_per_workgroup += align(lds_param_bytes, program->dev.lds_alloc_granule);
|
|
|
|
|
}
|
2021-01-28 13:07:11 +00:00
|
|
|
unsigned lds_limit = program->wgp_mode ? program->dev.lds_limit * 2 : program->dev.lds_limit;
|
2022-04-19 11:32:56 +02:00
|
|
|
if (lds_per_workgroup)
|
|
|
|
|
num_workgroups = std::min(num_workgroups, lds_limit / lds_per_workgroup);
|
|
|
|
|
|
|
|
|
|
/* Hardware limitation */
|
|
|
|
|
if (waves_per_workgroup > 1)
|
|
|
|
|
num_workgroups = std::min(num_workgroups, program->wgp_mode ? 32u : 16u);
|
|
|
|
|
|
|
|
|
|
/* Adjust #waves for workgroup multiples:
|
|
|
|
|
* In cases like waves_per_workgroup=3 or lds=65536 and
|
|
|
|
|
* waves_per_workgroup=1, we want the maximum possible number of waves per
|
|
|
|
|
* SIMD and not the minimum. so DIV_ROUND_UP is used
|
|
|
|
|
*/
|
|
|
|
|
unsigned workgroup_waves = num_workgroups * waves_per_workgroup;
|
|
|
|
|
return DIV_ROUND_UP(workgroup_waves, num_simd);
|
|
|
|
|
}
|
2019-10-18 19:06:10 +01:00
|
|
|
|
2022-04-19 11:32:56 +02:00
|
|
|
void
|
|
|
|
|
update_vgpr_sgpr_demand(Program* program, const RegisterDemand new_demand)
|
|
|
|
|
{
|
2021-02-05 14:36:39 +01:00
|
|
|
assert(program->min_waves >= 1);
|
|
|
|
|
uint16_t sgpr_limit = get_addr_sgpr_from_waves(program, program->min_waves);
|
|
|
|
|
uint16_t vgpr_limit = get_addr_vgpr_from_waves(program, program->min_waves);
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
/* this won't compile, register pressure reduction necessary */
|
2021-02-05 14:36:39 +01:00
|
|
|
if (new_demand.vgpr > vgpr_limit || new_demand.sgpr > sgpr_limit) {
|
2019-09-17 13:22:17 +02:00
|
|
|
program->num_waves = 0;
|
|
|
|
|
program->max_reg_demand = new_demand;
|
|
|
|
|
} else {
|
2021-01-28 13:07:11 +00:00
|
|
|
program->num_waves = program->dev.physical_sgprs / get_sgpr_alloc(program, new_demand.sgpr);
|
2021-06-09 10:14:54 +02:00
|
|
|
uint16_t vgpr_demand =
|
|
|
|
|
get_vgpr_alloc(program, new_demand.vgpr) + program->config->num_shared_vgprs / 2;
|
|
|
|
|
program->num_waves =
|
|
|
|
|
std::min<uint16_t>(program->num_waves, program->dev.physical_vgprs / vgpr_demand);
|
2022-04-19 16:58:26 +02:00
|
|
|
uint16_t max_waves = program->dev.max_wave64_per_simd * (64 / program->wave_size);
|
|
|
|
|
program->num_waves = std::min(program->num_waves, max_waves);
|
2019-10-18 19:06:10 +01:00
|
|
|
|
2022-04-19 11:32:56 +02:00
|
|
|
/* Adjust for LDS and workgroup multiples and calculate max_reg_demand */
|
|
|
|
|
program->num_waves = max_suitable_waves(program, program->num_waves);
|
2019-12-03 14:21:16 +00:00
|
|
|
program->max_reg_demand.vgpr = get_addr_vgpr_from_waves(program, program->num_waves);
|
2019-09-13 16:41:00 +01:00
|
|
|
program->max_reg_demand.sgpr = get_addr_sgpr_from_waves(program, program->num_waves);
|
2019-09-17 13:22:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
live
|
|
|
|
|
live_var_analysis(Program* program)
|
2019-09-17 13:22:17 +02:00
|
|
|
{
|
|
|
|
|
live result;
|
|
|
|
|
result.live_out.resize(program->blocks.size());
|
|
|
|
|
result.register_demand.resize(program->blocks.size());
|
2021-07-14 14:56:48 +02:00
|
|
|
unsigned worklist = program->blocks.size();
|
2021-07-26 17:55:48 +01:00
|
|
|
std::vector<PhiInfo> phi_info(program->blocks.size());
|
2019-09-17 13:22:17 +02:00
|
|
|
RegisterDemand new_demand;
|
|
|
|
|
|
2022-05-12 02:50:17 -04:00
|
|
|
program->needs_vcc = program->gfx_level >= GFX10;
|
2020-02-21 12:23:28 +00:00
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
/* this implementation assumes that the block idx corresponds to the block's position in
|
|
|
|
|
* program->blocks vector */
|
2021-07-14 14:56:48 +02:00
|
|
|
while (worklist) {
|
|
|
|
|
unsigned block_idx = --worklist;
|
2021-06-09 10:14:54 +02:00
|
|
|
process_live_temps_per_block(program, result, &program->blocks[block_idx], worklist,
|
2021-07-26 17:55:48 +01:00
|
|
|
phi_info);
|
2019-09-17 13:22:17 +02:00
|
|
|
new_demand.update(program->blocks[block_idx].register_demand);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 17:55:48 +01:00
|
|
|
/* Handle branches: we will insert copies created for linear phis just before the branch. */
|
|
|
|
|
for (Block& block : program->blocks) {
|
|
|
|
|
result.register_demand[block.index].back().sgpr += phi_info[block.index].linear_phi_defs;
|
|
|
|
|
result.register_demand[block.index].back().sgpr -= phi_info[block.index].linear_phi_ops;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 13:22:17 +02:00
|
|
|
/* calculate the program's register demand and number of waves */
|
2021-04-20 17:35:41 +01:00
|
|
|
if (program->progress < CompilationProgress::after_ra)
|
2021-04-19 11:24:03 +01:00
|
|
|
update_vgpr_sgpr_demand(program, new_demand);
|
2019-09-17 13:22:17 +02:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 10:14:54 +02:00
|
|
|
} // namespace aco
|