emit_glasm.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(T data_) : data{data_} {}
  26. 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. auto Extract() {
  64. if (inst) {
  65. reg_alloc.Unref(*inst);
  66. } else {
  67. reg_alloc.FreeReg(reg);
  68. }
  69. return std::conditional_t<scalar, ScalarRegister, Register>{Value{reg}};
  70. }
  71. private:
  72. RegAlloc& reg_alloc;
  73. IR::Inst* inst{};
  74. Register reg{};
  75. };
  76. template <typename ArgType>
  77. class ValueWrapper {
  78. public:
  79. ValueWrapper(EmitContext& ctx, const IR::Value& ir_value_)
  80. : reg_alloc{ctx.reg_alloc}, ir_value{ir_value_}, value{reg_alloc.Peek(ir_value)} {}
  81. ArgType Extract() {
  82. if (!ir_value.IsImmediate()) {
  83. reg_alloc.Unref(*ir_value.InstRecursive());
  84. }
  85. return value;
  86. }
  87. private:
  88. RegAlloc& reg_alloc;
  89. const IR::Value& ir_value;
  90. ArgType value;
  91. };
  92. template <typename ArgType>
  93. auto Arg(EmitContext& ctx, const IR::Value& arg) {
  94. if constexpr (std::is_same_v<ArgType, Register>) {
  95. return RegWrapper<false>{ctx, arg};
  96. } else if constexpr (std::is_same_v<ArgType, ScalarRegister>) {
  97. return RegWrapper<true>{ctx, arg};
  98. } else if constexpr (std::is_base_of_v<Value, ArgType>) {
  99. return ValueWrapper<ArgType>{ctx, arg};
  100. } else if constexpr (std::is_same_v<ArgType, const IR::Value&>) {
  101. return Identity<const IR::Value&>{arg};
  102. } else if constexpr (std::is_same_v<ArgType, u32>) {
  103. return Identity{arg.U32()};
  104. } else if constexpr (std::is_same_v<ArgType, IR::Block*>) {
  105. return Identity{arg.Label()};
  106. } else if constexpr (std::is_same_v<ArgType, IR::Attribute>) {
  107. return Identity{arg.Attribute()};
  108. } else if constexpr (std::is_same_v<ArgType, IR::Patch>) {
  109. return Identity{arg.Patch()};
  110. } else if constexpr (std::is_same_v<ArgType, IR::Reg>) {
  111. return Identity{arg.Reg()};
  112. }
  113. }
  114. template <auto func, bool is_first_arg_inst>
  115. struct InvokeCall {
  116. template <typename... Args>
  117. InvokeCall(EmitContext& ctx, IR::Inst* inst, Args&&... args) {
  118. if constexpr (is_first_arg_inst) {
  119. func(ctx, *inst, args.Extract()...);
  120. } else {
  121. func(ctx, args.Extract()...);
  122. }
  123. }
  124. };
  125. template <auto func, bool is_first_arg_inst, size_t... I>
  126. void Invoke(EmitContext& ctx, IR::Inst* inst, std::index_sequence<I...>) {
  127. using Traits = FuncTraits<decltype(func)>;
  128. if constexpr (is_first_arg_inst) {
  129. InvokeCall<func, is_first_arg_inst>{
  130. ctx, inst, Arg<typename Traits::template ArgType<I + 2>>(ctx, inst->Arg(I))...};
  131. } else {
  132. InvokeCall<func, is_first_arg_inst>{
  133. ctx, inst, Arg<typename Traits::template ArgType<I + 1>>(ctx, inst->Arg(I))...};
  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. if (info.uses_subgroup_invocation_id || info.uses_subgroup_mask) {
  170. header += "OPTION NV_shader_thread_group;";
  171. }
  172. if (info.uses_subgroup_shuffles) {
  173. header += "OPTION NV_shader_thread_shuffle;";
  174. }
  175. // TODO: Track the shared atomic ops
  176. header +=
  177. "OPTION NV_shader_storage_buffer;OPTION NV_gpu_program_fp64;OPTION NV_bindless_texture;";
  178. }
  179. } // Anonymous namespace
  180. std::string EmitGLASM(const Profile&, IR::Program& program, Bindings&) {
  181. EmitContext ctx{program};
  182. for (IR::Block* const block : program.blocks) {
  183. for (IR::Inst& inst : block->Instructions()) {
  184. EmitInst(ctx, &inst);
  185. }
  186. }
  187. std::string header = "!!NVcp5.0\n"
  188. "OPTION NV_internal;";
  189. SetupOptions(header, program.info);
  190. switch (program.stage) {
  191. case Stage::Compute:
  192. header += fmt::format("GROUP_SIZE {} {} {};", program.workgroup_size[0],
  193. program.workgroup_size[1], program.workgroup_size[2]);
  194. break;
  195. default:
  196. break;
  197. }
  198. if (program.shared_memory_size > 0) {
  199. header += fmt::format("SHARED_MEMORY {};", program.shared_memory_size);
  200. header += fmt::format("SHARED shared_mem[]={{program.sharedmem}};");
  201. }
  202. header += "TEMP ";
  203. for (size_t index = 0; index < ctx.reg_alloc.NumUsedRegisters(); ++index) {
  204. header += fmt::format("R{},", index);
  205. }
  206. header += "RC;"
  207. "LONG TEMP ";
  208. for (size_t index = 0; index < ctx.reg_alloc.NumUsedLongRegisters(); ++index) {
  209. header += fmt::format("D{},", index);
  210. }
  211. header += "DC;";
  212. ctx.code.insert(0, header);
  213. ctx.code += "END";
  214. return ctx.code;
  215. }
  216. } // namespace Shader::Backend::GLASM