verification_pass.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. throw LogicError("Forward declaration in block: {}", IR::DumpBlock(*block));
  64. }
  65. }
  66. }
  67. }
  68. }
  69. static void ValidatePhiNodes(const IR::Program& program) {
  70. for (const IR::Block* const block : program.blocks) {
  71. bool no_more_phis{false};
  72. for (const IR::Inst& inst : *block) {
  73. if (inst.GetOpcode() == IR::Opcode::Phi) {
  74. if (no_more_phis) {
  75. throw LogicError("Interleaved phi nodes: {}", IR::DumpBlock(*block));
  76. }
  77. } else {
  78. no_more_phis = true;
  79. }
  80. }
  81. }
  82. }
  83. void VerificationPass(const IR::Program& program) {
  84. ValidateTypes(program);
  85. ValidateUses(program);
  86. ValidateForwardDeclarations(program);
  87. ValidatePhiNodes(program);
  88. }
  89. } // namespace Shader::Optimization