emit_glasm.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <string>
  5. #include <tuple>
  6. #include "shader_recompiler/backend/bindings.h"
  7. #include "shader_recompiler/backend/glasm/emit_context.h"
  8. #include "shader_recompiler/backend/glasm/emit_glasm.h"
  9. #include "shader_recompiler/backend/glasm/emit_glasm_instructions.h"
  10. #include "shader_recompiler/frontend/ir/program.h"
  11. #include "shader_recompiler/profile.h"
  12. namespace Shader::Backend::GLASM {
  13. namespace {
  14. template <class Func>
  15. struct FuncTraits {};
  16. template <class ReturnType_, class... Args>
  17. struct FuncTraits<ReturnType_ (*)(Args...)> {
  18. using ReturnType = ReturnType_;
  19. static constexpr size_t NUM_ARGS = sizeof...(Args);
  20. template <size_t I>
  21. using ArgType = std::tuple_element_t<I, std::tuple<Args...>>;
  22. };
  23. template <typename T>
  24. struct Identity {
  25. Identity(const T& data_) : data{data_} {}
  26. const T& Extract() {
  27. return data;
  28. }
  29. T data;
  30. };
  31. template <bool scalar>
  32. class RegWrapper {
  33. public:
  34. RegWrapper(EmitContext& ctx, const IR::Value& ir_value) : reg_alloc{ctx.reg_alloc} {
  35. const Value value{reg_alloc.Peek(ir_value)};
  36. if (value.type == Type::Register) {
  37. inst = ir_value.InstRecursive();
  38. reg = Register{value};
  39. } else {
  40. const bool is_long{value.type == Type::F64 || value.type == Type::U64};
  41. reg = is_long ? reg_alloc.AllocLongReg() : reg_alloc.AllocReg();
  42. }
  43. switch (value.type) {
  44. case Type::Register:
  45. break;
  46. case Type::U32:
  47. ctx.Add("MOV.U {}.x,{};", reg, value.imm_u32);
  48. break;
  49. case Type::S32:
  50. ctx.Add("MOV.S {}.x,{};", reg, value.imm_s32);
  51. break;
  52. case Type::F32:
  53. ctx.Add("MOV.F {}.x,{};", reg, value.imm_f32);
  54. break;
  55. case Type::U64:
  56. ctx.Add("MOV.U64 {}.x,{};", reg, value.imm_u64);
  57. break;
  58. case Type::F64:
  59. ctx.Add("MOV.F64 {}.x,{};", reg, value.imm_f64);
  60. break;
  61. }
  62. }
  63. ~RegWrapper() {
  64. if (inst) {
  65. reg_alloc.Unref(*inst);
  66. } else {
  67. reg_alloc.FreeReg(reg);
  68. }
  69. }
  70. auto Extract() {
  71. return std::conditional_t<scalar, ScalarRegister, Register>{Value{reg}};
  72. }
  73. private:
  74. RegAlloc& reg_alloc;
  75. IR::Inst* inst{};
  76. Register reg{};
  77. };
  78. template <typename ArgType>
  79. class ValueWrapper {
  80. public:
  81. ValueWrapper(EmitContext& ctx, const IR::Value& ir_value_)
  82. : reg_alloc{ctx.reg_alloc}, ir_value{ir_value_}, value{reg_alloc.Peek(ir_value)} {}
  83. ~ValueWrapper() {
  84. if (!ir_value.IsImmediate()) {
  85. reg_alloc.Unref(*ir_value.InstRecursive());
  86. }
  87. }
  88. ArgType Extract() {
  89. return value;
  90. }
  91. private:
  92. RegAlloc& reg_alloc;
  93. const IR::Value& ir_value;
  94. ArgType value;
  95. };
  96. template <typename ArgType>
  97. auto Arg(EmitContext& ctx, const IR::Value& arg) {
  98. if constexpr (std::is_same_v<ArgType, Register>) {
  99. return RegWrapper<false>{ctx, arg};
  100. } else if constexpr (std::is_same_v<ArgType, ScalarRegister>) {
  101. return RegWrapper<true>{ctx, arg};
  102. } else if constexpr (std::is_base_of_v<Value, ArgType>) {
  103. return ValueWrapper<ArgType>{ctx, arg};
  104. } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
  105. return Identity{arg};
  106. } else if constexpr (std::is_same_v<ArgType, u32>) {
  107. return Identity{arg.U32()};
  108. } else if constexpr (std::is_same_v<ArgType, IR::Block*>) {
  109. return Identity{arg.Label()};
  110. } else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
  111. return Identity{arg.Attribute()};
  112. } else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
  113. return Identity{arg.Patch()};
  114. } else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
  115. return Identity{arg.Reg()};
  116. }
  117. }
  118. template <auto func, bool is_first_arg_inst, typename... Args>
  119. void InvokeCall(EmitContext& ctx, IR::Inst* inst, Args&&... args) {
  120. if constexpr (is_first_arg_inst) {
  121. func(ctx, *inst, std::forward<Args>(args.Extract())...);
  122. } else {
  123. func(ctx, std::forward<Args>(args.Extract())...);
  124. }
  125. }
  126. template <auto func, bool is_first_arg_inst, size_t... I>
  127. void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
  128. using Traits = FuncTraits<decltype(func)>;
  129. if constexpr (is_first_arg_inst) {
  130. func(ctx, *inst,
  131. Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I)).Extract()...);
  132. } else {
  133. func(ctx, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I)).Extract()...);
  134. }
  135. }
  136. template <auto func>
  137. void Invoke(EmitContext& ctx, IR::Inst* inst) {
  138. using Traits = FuncTraits<decltype(func)>;
  139. static_assert(Traits::NUM_ARGS >= 1, "Insufficient arguments");
  140. if constexpr (Traits::NUM_ARGS == 1) {
  141. Invoke<func, false>(ctx, inst, std::make_index_sequence<0>{});
  142. } else {
  143. using FirstArgType = typename Traits::template ArgType<1>;
  144. static constexpr bool is_first_arg_inst = std::is_same_v<FirstArgType, IR::Inst&>;
  145. using Indices = std::make_index_sequence<Traits::NUM_ARGS - (is_first_arg_inst ? 2 : 1)>;
  146. Invoke<func, is_first_arg_inst>(ctx, inst, Indices{});
  147. }
  148. }
  149. void EmitInst(EmitContext& ctx, IR::Inst* inst) {
  150. switch (inst->GetOpcode()) {
  151. #define OPCODE(name, result_type, ...) \
  152. case IR::Opcode::name: \
  153. return Invoke<&Emit##name>(ctx, inst);
  154. #include "shader_recompiler/frontend/ir/opcodes.inc"
  155. #undef OPCODE
  156. }
  157. throw LogicError("Invalid opcode {}", inst->GetOpcode());
  158. }
  159. void SetupOptions(std::string& header, Info info) {
  160. if (info.uses_int64_bit_atomics) {
  161. header += "OPTION NV_shader_atomic_int64;";
  162. }
  163. if (info.uses_atomic_f32_add) {
  164. header += "OPTION NV_shader_atomic_float;";
  165. }
  166. if (info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max) {
  167. header += "OPTION NV_shader_atomic_fp16_vector;";
  168. }
  169. }
  170. } // Anonymous namespace
  171. std::string EmitGLASM(const Profile&, IR::Program& program, Bindings&) {
  172. EmitContext ctx{program};
  173. for (IR::Block* const block : program.blocks) {
  174. for (IR::Inst& inst : block->Instructions()) {
  175. EmitInst(ctx, &inst);
  176. }
  177. }
  178. std::string header = "!!NVcp5.0\n"
  179. "OPTION NV_internal;";
  180. SetupOptions(header, program.info);
  181. switch (program.stage) {
  182. case Stage::Compute:
  183. header += fmt::format("GROUP_SIZE {} {} {};", program.workgroup_size[0],
  184. program.workgroup_size[1], program.workgroup_size[2]);
  185. break;
  186. default:
  187. break;
  188. }
  189. header += "TEMP ";
  190. for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) {
  191. header += fmt::format("R{},", index);
  192. }
  193. header += "RC;"
  194. "LONG TEMP ";
  195. for (size_t index = 0; index < ctx.reg_alloc.NumUsedLongRegisters(); ++index) {
  196. header += fmt::format("D{},", index);
  197. }
  198. header += "DC;";
  199. ctx.code.insert(0, header);
  200. ctx.code += "END";
  201. return ctx.code;
  202. }
  203. } // namespace Shader::Backend::GLASM