2021-01-09 07:30:07 +01:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "shader_recompiler/exception.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
|
|
|
#include "shader_recompiler/frontend/ir/microinstruction.h"
|
|
|
|
#include "shader_recompiler/ir_opt/passes.h"
|
|
|
|
|
|
|
|
namespace Shader::Optimization {
|
|
|
|
|
2021-03-14 07:41:05 +01:00
|
|
|
static void ValidateTypes(const IR::Program& program) {
|
|
|
|
for (const auto& block : program.blocks) {
|
2021-02-05 09:58:02 +01:00
|
|
|
for (const IR::Inst& inst : *block) {
|
2021-02-11 20:39:06 +01:00
|
|
|
if (inst.Opcode() == IR::Opcode::Phi) {
|
|
|
|
// Skip validation on phi nodes
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-05 09:58:02 +01: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()};
|
|
|
|
const IR::Type t2{IR::ArgTypeOf(inst.Opcode(), i)};
|
|
|
|
if (!IR::AreTypesCompatible(t1, t2)) {
|
|
|
|
throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
|
|
|
|
}
|
2021-01-09 07:30:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:41:05 +01:00
|
|
|
static void ValidateUses(const IR::Program& program) {
|
2021-01-09 07:30:07 +01:00
|
|
|
std::map<IR::Inst*, int> actual_uses;
|
2021-03-14 07:41:05 +01:00
|
|
|
for (const auto& block : program.blocks) {
|
2021-02-05 09:58:02 +01: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 07:30:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto [inst, uses] : actual_uses) {
|
|
|
|
if (inst->UseCount() != uses) {
|
2021-03-14 07:41:05 +01:00
|
|
|
throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
|
2021-01-09 07:30:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 07:41:05 +01:00
|
|
|
void VerificationPass(const IR::Program& program) {
|
|
|
|
ValidateTypes(program);
|
|
|
|
ValidateUses(program);
|
2021-01-09 07:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Optimization
|