decode.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <set>
  6. #include <fmt/format.h>
  7. #include "common/common_types.h"
  8. #include "video_core/engines/shader_bytecode.h"
  9. #include "video_core/engines/shader_header.h"
  10. #include "video_core/shader/shader_ir.h"
  11. namespace VideoCommon::Shader {
  12. using Tegra::Shader::Instruction;
  13. using Tegra::Shader::OpCode;
  14. /// Merges exit method of two parallel branches.
  15. constexpr ExitMethod ParallelExit(ExitMethod a, ExitMethod b) {
  16. if (a == ExitMethod::Undetermined) {
  17. return b;
  18. }
  19. if (b == ExitMethod::Undetermined) {
  20. return a;
  21. }
  22. if (a == b) {
  23. return a;
  24. }
  25. return ExitMethod::Conditional;
  26. }
  27. /**
  28. * Returns whether the instruction at the specified offset is a 'sched' instruction.
  29. * Sched instructions always appear before a sequence of 3 instructions.
  30. */
  31. constexpr bool IsSchedInstruction(u32 offset, u32 main_offset) {
  32. constexpr u32 SchedPeriod = 4;
  33. u32 absolute_offset = offset - main_offset;
  34. return (absolute_offset % SchedPeriod) == 0;
  35. }
  36. void ShaderIR::Decode() {
  37. std::memcpy(&header, program_code.data(), sizeof(Tegra::Shader::Header));
  38. std::set<u32> labels;
  39. const ExitMethod exit_method = Scan(main_offset, MAX_PROGRAM_LENGTH, labels);
  40. if (exit_method != ExitMethod::AlwaysEnd) {
  41. UNREACHABLE_MSG("Program does not always end");
  42. }
  43. if (labels.empty()) {
  44. basic_blocks.insert({main_offset, DecodeRange(main_offset, MAX_PROGRAM_LENGTH)});
  45. return;
  46. }
  47. labels.insert(main_offset);
  48. for (const u32 label : labels) {
  49. const auto next_it = labels.lower_bound(label + 1);
  50. const u32 next_label = next_it == labels.end() ? MAX_PROGRAM_LENGTH : *next_it;
  51. basic_blocks.insert({label, DecodeRange(label, next_label)});
  52. }
  53. }
  54. ExitMethod ShaderIR::Scan(u32 begin, u32 end, std::set<u32>& labels) {
  55. const auto [iter, inserted] =
  56. exit_method_map.emplace(std::make_pair(begin, end), ExitMethod::Undetermined);
  57. ExitMethod& exit_method = iter->second;
  58. if (!inserted)
  59. return exit_method;
  60. for (u32 offset = begin; offset != end && offset != MAX_PROGRAM_LENGTH; ++offset) {
  61. coverage_begin = std::min(coverage_begin, offset);
  62. coverage_end = std::max(coverage_end, offset + 1);
  63. const Instruction instr = {program_code[offset]};
  64. const auto opcode = OpCode::Decode(instr);
  65. if (!opcode)
  66. continue;
  67. switch (opcode->get().GetId()) {
  68. case OpCode::Id::EXIT: {
  69. // The EXIT instruction can be predicated, which means that the shader can conditionally
  70. // end on this instruction. We have to consider the case where the condition is not met
  71. // and check the exit method of that other basic block.
  72. using Tegra::Shader::Pred;
  73. if (instr.pred.pred_index == static_cast<u64>(Pred::UnusedIndex)) {
  74. return exit_method = ExitMethod::AlwaysEnd;
  75. } else {
  76. const ExitMethod not_met = Scan(offset + 1, end, labels);
  77. return exit_method = ParallelExit(ExitMethod::AlwaysEnd, not_met);
  78. }
  79. }
  80. case OpCode::Id::BRA: {
  81. const u32 target = offset + instr.bra.GetBranchTarget();
  82. labels.insert(target);
  83. const ExitMethod no_jmp = Scan(offset + 1, end, labels);
  84. const ExitMethod jmp = Scan(target, end, labels);
  85. return exit_method = ParallelExit(no_jmp, jmp);
  86. }
  87. case OpCode::Id::SSY:
  88. case OpCode::Id::PBK: {
  89. // The SSY and PBK use a similar encoding as the BRA instruction.
  90. UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
  91. "Constant buffer branching is not supported");
  92. const u32 target = offset + instr.bra.GetBranchTarget();
  93. labels.insert(target);
  94. // Continue scanning for an exit method.
  95. break;
  96. }
  97. }
  98. }
  99. return exit_method = ExitMethod::AlwaysReturn;
  100. }
  101. BasicBlock ShaderIR::DecodeRange(u32 begin, u32 end) {
  102. BasicBlock basic_block;
  103. for (u32 pc = begin; pc < (begin > end ? MAX_PROGRAM_LENGTH : end);) {
  104. pc = DecodeInstr(basic_block, pc);
  105. }
  106. return std::move(basic_block);
  107. }
  108. u32 ShaderIR::DecodeInstr(BasicBlock& bb, u32 pc) {
  109. // Ignore sched instructions when generating code.
  110. if (IsSchedInstruction(pc, main_offset)) {
  111. return pc + 1;
  112. }
  113. const Instruction instr = {program_code[pc]};
  114. const auto opcode = OpCode::Decode(instr);
  115. // Decoding failure
  116. if (!opcode) {
  117. UNIMPLEMENTED_MSG("Unhandled instruction: {0:x}", instr.value);
  118. return pc + 1;
  119. }
  120. bb.push_back(
  121. Comment(fmt::format("{}: {} (0x{:016x})", pc, opcode->get().GetName(), instr.value)));
  122. using Tegra::Shader::Pred;
  123. UNIMPLEMENTED_IF_MSG(instr.pred.full_pred == Pred::NeverExecute,
  124. "NeverExecute predicate not implemented");
  125. static const std::map<OpCode::Type, u32 (ShaderIR::*)(BasicBlock & code, u32 pc)> decoders = {
  126. {OpCode::Type::Arithmetic, &ShaderIR::DecodeArithmetic},
  127. {OpCode::Type::ArithmeticImmediate, &ShaderIR::DecodeArithmeticImmediate},
  128. {OpCode::Type::Bfe, &ShaderIR::DecodeBfe},
  129. {OpCode::Type::Bfi, &ShaderIR::DecodeBfi},
  130. {OpCode::Type::Shift, &ShaderIR::DecodeShift},
  131. {OpCode::Type::ArithmeticInteger, &ShaderIR::DecodeArithmeticInteger},
  132. {OpCode::Type::ArithmeticIntegerImmediate, &ShaderIR::DecodeArithmeticIntegerImmediate},
  133. {OpCode::Type::ArithmeticHalf, &ShaderIR::DecodeArithmeticHalf},
  134. {OpCode::Type::ArithmeticHalfImmediate, &ShaderIR::DecodeArithmeticHalfImmediate},
  135. {OpCode::Type::Ffma, &ShaderIR::DecodeFfma},
  136. {OpCode::Type::Hfma2, &ShaderIR::DecodeHfma2},
  137. {OpCode::Type::Conversion, &ShaderIR::DecodeConversion},
  138. {OpCode::Type::Memory, &ShaderIR::DecodeMemory},
  139. {OpCode::Type::FloatSetPredicate, &ShaderIR::DecodeFloatSetPredicate},
  140. {OpCode::Type::IntegerSetPredicate, &ShaderIR::DecodeIntegerSetPredicate},
  141. {OpCode::Type::HalfSetPredicate, &ShaderIR::DecodeHalfSetPredicate},
  142. {OpCode::Type::PredicateSetRegister, &ShaderIR::DecodePredicateSetRegister},
  143. {OpCode::Type::PredicateSetPredicate, &ShaderIR::DecodePredicateSetPredicate},
  144. {OpCode::Type::RegisterSetPredicate, &ShaderIR::DecodeRegisterSetPredicate},
  145. {OpCode::Type::FloatSet, &ShaderIR::DecodeFloatSet},
  146. {OpCode::Type::IntegerSet, &ShaderIR::DecodeIntegerSet},
  147. {OpCode::Type::HalfSet, &ShaderIR::DecodeHalfSet},
  148. {OpCode::Type::Xmad, &ShaderIR::DecodeXmad},
  149. };
  150. std::vector<Node> code;
  151. if (const auto decoder = decoders.find(opcode->get().GetType()); decoder != decoders.end()) {
  152. pc = (this->*decoder->second)(code, pc);
  153. } else {
  154. pc = DecodeOther(code, pc);
  155. }
  156. // Some instructions (like SSY) don't have a predicate field, they are always unconditionally
  157. // executed.
  158. const bool can_be_predicated = OpCode::IsPredicatedInstruction(opcode->get().GetId());
  159. const auto pred_index = static_cast<u32>(instr.pred.pred_index);
  160. if (can_be_predicated && pred_index != static_cast<u32>(Pred::UnusedIndex)) {
  161. bb.push_back(
  162. Conditional(GetPredicate(pred_index, instr.negate_pred != 0), std::move(code)));
  163. } else {
  164. for (auto& node : code) {
  165. bb.push_back(std::move(node));
  166. }
  167. }
  168. return pc + 1;
  169. }
  170. } // namespace VideoCommon::Shader