verification_pass.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include "shader_recompiler/exception.h"
  6. #include "shader_recompiler/frontend/ir/basic_block.h"
  7. #include "shader_recompiler/frontend/ir/microinstruction.h"
  8. #include "shader_recompiler/ir_opt/passes.h"
  9. namespace Shader::Optimization {
  10. static void ValidateTypes(const IR::Program& program) {
  11. for (const auto& block : program.blocks) {
  12. for (const IR::Inst& inst : *block) {
  13. if (inst.Opcode() == IR::Opcode::Phi) {
  14. // Skip validation on phi nodes
  15. continue;
  16. }
  17. const size_t num_args{inst.NumArgs()};
  18. for (size_t i = 0; i < num_args; ++i) {
  19. const IR::Type t1{inst.Arg(i).Type()};
  20. const IR::Type t2{IR::ArgTypeOf(inst.Opcode(), i)};
  21. if (!IR::AreTypesCompatible(t1, t2)) {
  22. throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
  23. }
  24. }
  25. }
  26. }
  27. }
  28. static void ValidateUses(const IR::Program& program) {
  29. std::map<IR::Inst*, int> actual_uses;
  30. for (const auto& block : program.blocks) {
  31. for (const IR::Inst& inst : *block) {
  32. const size_t num_args{inst.NumArgs()};
  33. for (size_t i = 0; i < num_args; ++i) {
  34. const IR::Value arg{inst.Arg(i)};
  35. if (!arg.IsImmediate()) {
  36. ++actual_uses[arg.Inst()];
  37. }
  38. }
  39. }
  40. }
  41. for (const auto [inst, uses] : actual_uses) {
  42. if (inst->UseCount() != uses) {
  43. throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
  44. }
  45. }
  46. }
  47. void VerificationPass(const IR::Program& program) {
  48. ValidateTypes(program);
  49. ValidateUses(program);
  50. }
  51. } // namespace Shader::Optimization