decode.cpp 7.6 KB

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