2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
#include <map>
|
2021-04-22 21:34:34 +00:00
|
|
|
#include <set>
|
2021-01-09 06:30:07 +00:00
|
|
|
|
|
|
|
#include "shader_recompiler/exception.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
2021-04-21 03:35:47 +00:00
|
|
|
#include "shader_recompiler/frontend/ir/value.h"
|
2021-01-09 06:30:07 +00:00
|
|
|
#include "shader_recompiler/ir_opt/passes.h"
|
|
|
|
|
|
|
|
namespace Shader::Optimization {
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
static void ValidateTypes(const IR::Program& program) {
|
|
|
|
for (const auto& block : program.blocks) {
|
2021-02-05 08:58:02 +00:00
|
|
|
for (const IR::Inst& inst : *block) {
|
2021-04-06 02:25:22 +00:00
|
|
|
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
2021-02-11 19:39:06 +00:00
|
|
|
// Skip validation on phi nodes
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-05 08:58:02 +00:00
|
|
|
const size_t num_args{inst.NumArgs()};
|
|
|
|
for (size_t i = 0; i < num_args; ++i) {
|
|
|
|
const IR::Type t1{inst.Arg(i).Type()};
|
2021-04-06 02:25:22 +00:00
|
|
|
const IR::Type t2{IR::ArgTypeOf(inst.GetOpcode(), i)};
|
2021-02-05 08:58:02 +00:00
|
|
|
if (!IR::AreTypesCompatible(t1, t2)) {
|
|
|
|
throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
|
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
static void ValidateUses(const IR::Program& program) {
|
2021-01-09 06:30:07 +00:00
|
|
|
std::map<IR::Inst*, int> actual_uses;
|
2021-03-14 06:41:05 +00:00
|
|
|
for (const auto& block : program.blocks) {
|
2021-02-05 08:58:02 +00:00
|
|
|
for (const IR::Inst& inst : *block) {
|
|
|
|
const size_t num_args{inst.NumArgs()};
|
|
|
|
for (size_t i = 0; i < num_args; ++i) {
|
|
|
|
const IR::Value arg{inst.Arg(i)};
|
|
|
|
if (!arg.IsImmediate()) {
|
|
|
|
++actual_uses[arg.Inst()];
|
|
|
|
}
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 17:22:34 +00:00
|
|
|
for (const auto& [inst, uses] : actual_uses) {
|
2021-01-09 06:30:07 +00:00
|
|
|
if (inst->UseCount() != uses) {
|
2021-03-14 06:41:05 +00:00
|
|
|
throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 21:34:34 +00:00
|
|
|
static void ValidateForwardDeclarations(const IR::Program& program) {
|
|
|
|
std::set<const IR::Inst*> definitions;
|
|
|
|
for (const IR::Block* const block : program.blocks) {
|
|
|
|
for (const IR::Inst& inst : *block) {
|
|
|
|
definitions.emplace(&inst);
|
|
|
|
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
|
|
|
// Phi nodes can have forward declarations
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const size_t num_args{inst.NumArgs()};
|
|
|
|
for (size_t arg = 0; arg < num_args; ++arg) {
|
|
|
|
if (inst.Arg(arg).IsImmediate()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!definitions.contains(inst.Arg(arg).Inst())) {
|
|
|
|
throw LogicError("Forward declaration in block: {}", IR::DumpBlock(*block));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ValidatePhiNodes(const IR::Program& program) {
|
|
|
|
for (const IR::Block* const block : program.blocks) {
|
|
|
|
bool no_more_phis{false};
|
|
|
|
for (const IR::Inst& inst : *block) {
|
|
|
|
if (inst.GetOpcode() == IR::Opcode::Phi) {
|
|
|
|
if (no_more_phis) {
|
|
|
|
throw LogicError("Interleaved phi nodes: {}", IR::DumpBlock(*block));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
no_more_phis = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 06:41:05 +00:00
|
|
|
void VerificationPass(const IR::Program& program) {
|
|
|
|
ValidateTypes(program);
|
|
|
|
ValidateUses(program);
|
2021-04-22 21:34:34 +00:00
|
|
|
ValidateForwardDeclarations(program);
|
|
|
|
ValidatePhiNodes(program);
|
2021-01-09 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Optimization
|