verification_pass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <set>
  6. #include "shader_recompiler/exception.h"
  7. #include "shader_recompiler/frontend/ir/basic_block.h"
  8. #include "shader_recompiler/frontend/ir/value.h"
  9. #include "shader_recompiler/ir_opt/passes.h"
  10. namespace Shader::Optimization {
  11. static void ValidateTypes(const IR::Program& program) {
  12. for (const auto& block : program.blocks) {
  13. for (const IR::Inst& inst : *block) {
  14. if (inst.GetOpcode() == IR::Opcode::Phi) {
  15. // Skip validation on phi nodes
  16. continue;
  17. }
  18. const size_t num_args{inst.NumArgs()};
  19. for (size_t i = 0; i < num_args; ++i) {
  20. const IR::Type t1{inst.Arg(i).Type()};
  21. const IR::Type t2{IR::ArgTypeOf(inst.GetOpcode(), i)};
  22. if (!IR::AreTypesCompatible(t1, t2)) {
  23. throw LogicError("Invalid types in block:\n{}", IR::DumpBlock(*block));
  24. }
  25. }
  26. }
  27. }
  28. }
  29. static void ValidateUses(const IR::Program& program) {
  30. std::map<IR::Inst*, int> actual_uses;
  31. for (const auto& block : program.blocks) {
  32. for (const IR::Inst& inst : *block) {
  33. const size_t num_args{inst.NumArgs()};
  34. for (size_t i = 0; i < num_args; ++i) {
  35. const IR::Value arg{inst.Arg(i)};
  36. if (!arg.IsImmediate()) {
  37. ++actual_uses[arg.Inst()];
  38. }
  39. }
  40. }
  41. }
  42. for (const auto [inst, uses] : actual_uses) {
  43. if (inst->UseCount() != uses) {
  44. throw LogicError("Invalid uses in block: {}", IR::DumpProgram(program));
  45. }
  46. }
  47. }
  48. static void ValidateForwardDeclarations(const IR::Program& program) {
  49. std::set<const IR::Inst*> definitions;
  50. for (const IR::Block* const block : program.blocks) {
  51. for (const IR::Inst& inst : *block) {
  52. definitions.emplace(&inst);
  53. if (inst.GetOpcode() == IR::Opcode::Phi) {
  54. // Phi nodes can have forward declarations
  55. continue;
  56. }
  57. const size_t num_args{inst.NumArgs()};
  58. for (size_t arg = 0; arg < num_args; ++arg) {
  59. if (inst.Arg(arg).IsImmediate()) {
  60. continue;
  61. }
  62. if (!definitions.contains(inst.Arg(arg).Inst())) {
  63. fmt::print("{}\n", IR::DumpBlock(*block));
  64. throw LogicError("Forward declaration in block: {}", IR::DumpBlock(*block));
  65. }
  66. }
  67. }
  68. }
  69. }
  70. static void ValidatePhiNodes(const IR::Program& program) {
  71. for (const IR::Block* const block : program.blocks) {
  72. bool no_more_phis{false};
  73. for (const IR::Inst& inst : *block) {
  74. if (inst.GetOpcode() == IR::Opcode::Phi) {
  75. if (no_more_phis) {
  76. fmt::print("{}\n", IR::DumpBlock(*block));
  77. throw LogicError("Interleaved phi nodes: {}", IR::DumpBlock(*block));
  78. }
  79. } else {
  80. no_more_phis = true;
  81. }
  82. }
  83. }
  84. }
  85. void VerificationPass(const IR::Program& program) {
  86. ValidateTypes(program);
  87. ValidateUses(program);
  88. ValidateForwardDeclarations(program);
  89. ValidatePhiNodes(program);
  90. }
  91. } // namespace Shader::Optimization