program.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include "shader_recompiler/frontend/ir/basic_block.h"
  7. #include "shader_recompiler/frontend/maxwell/program.h"
  8. #include "shader_recompiler/frontend/maxwell/termination_code.h"
  9. #include "shader_recompiler/frontend/maxwell/translate/translate.h"
  10. #include "shader_recompiler/ir_opt/passes.h"
  11. namespace Shader::Maxwell {
  12. namespace {
  13. void TranslateCode(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
  14. Environment& env, const Flow::Function& cfg_function, IR::Function& function,
  15. std::span<IR::Block*> block_map) {
  16. const size_t num_blocks{cfg_function.blocks.size()};
  17. function.blocks.reserve(num_blocks);
  18. for (const Flow::BlockId block_id : cfg_function.blocks) {
  19. const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
  20. IR::Block* const ir_block{block_pool.Create(Translate(inst_pool, env, flow_block))};
  21. block_map[flow_block.id] = ir_block;
  22. function.blocks.emplace_back(ir_block);
  23. }
  24. }
  25. void EmitTerminationInsts(const Flow::Function& cfg_function,
  26. std::span<IR::Block* const> block_map) {
  27. for (const Flow::BlockId block_id : cfg_function.blocks) {
  28. const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
  29. EmitTerminationCode(flow_block, block_map);
  30. }
  31. }
  32. void TranslateFunction(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
  33. Environment& env, const Flow::Function& cfg_function,
  34. IR::Function& function) {
  35. std::vector<IR::Block*> block_map;
  36. block_map.resize(cfg_function.blocks_data.size());
  37. TranslateCode(inst_pool, block_pool, env, cfg_function, function, block_map);
  38. EmitTerminationInsts(cfg_function, block_map);
  39. }
  40. } // Anonymous namespace
  41. IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
  42. Environment& env, const Flow::CFG& cfg) {
  43. IR::Program program;
  44. auto& functions{program.functions};
  45. functions.reserve(cfg.Functions().size());
  46. for (const Flow::Function& cfg_function : cfg.Functions()) {
  47. TranslateFunction(inst_pool, block_pool, env, cfg_function, functions.emplace_back());
  48. }
  49. std::ranges::for_each(functions, Optimization::SsaRewritePass);
  50. for (IR::Function& function : functions) {
  51. Optimization::Invoke(Optimization::GlobalMemoryToStorageBufferPass, function);
  52. Optimization::Invoke(Optimization::ConstantPropagationPass, function);
  53. Optimization::Invoke(Optimization::DeadCodeEliminationPass, function);
  54. Optimization::IdentityRemovalPass(function);
  55. Optimization::VerificationPass(function);
  56. }
  57. //*/
  58. return program;
  59. }
  60. } // namespace Shader::Maxwell