yuzu/src/shader_recompiler/frontend/maxwell/program.cpp

71 lines
2.9 KiB
C++
Raw Normal View History

2021-01-09 06:30:07 +00:00
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <memory>
2021-02-06 02:11:23 +00:00
#include "shader_recompiler/frontend/ir/basic_block.h"
2021-01-09 06:30:07 +00:00
#include "shader_recompiler/frontend/maxwell/program.h"
#include "shader_recompiler/frontend/maxwell/termination_code.h"
#include "shader_recompiler/frontend/maxwell/translate/translate.h"
2021-02-03 00:07:00 +00:00
#include "shader_recompiler/ir_opt/passes.h"
2021-01-09 06:30:07 +00:00
namespace Shader::Maxwell {
2021-02-03 00:07:00 +00:00
namespace {
2021-02-06 02:11:23 +00:00
void TranslateCode(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
Environment& env, const Flow::Function& cfg_function, IR::Function& function,
std::span<IR::Block*> block_map) {
2021-02-03 00:07:00 +00:00
const size_t num_blocks{cfg_function.blocks.size()};
function.blocks.reserve(num_blocks);
2021-01-09 06:30:07 +00:00
2021-02-03 00:07:00 +00:00
for (const Flow::BlockId block_id : cfg_function.blocks) {
const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
2021-01-09 06:30:07 +00:00
2021-02-06 02:11:23 +00:00
IR::Block* const ir_block{block_pool.Create(Translate(inst_pool, env, flow_block))};
block_map[flow_block.id] = ir_block;
function.blocks.emplace_back(ir_block);
2021-02-03 00:07:00 +00:00
}
}
2021-01-09 06:30:07 +00:00
2021-02-03 00:07:00 +00:00
void EmitTerminationInsts(const Flow::Function& cfg_function,
std::span<IR::Block* const> block_map) {
for (const Flow::BlockId block_id : cfg_function.blocks) {
const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
EmitTerminationCode(flow_block, block_map);
}
}
2021-01-09 06:30:07 +00:00
2021-02-06 02:11:23 +00:00
void TranslateFunction(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
Environment& env, const Flow::Function& cfg_function,
IR::Function& function) {
2021-02-03 00:07:00 +00:00
std::vector<IR::Block*> block_map;
block_map.resize(cfg_function.blocks_data.size());
2021-01-09 06:30:07 +00:00
2021-02-06 02:11:23 +00:00
TranslateCode(inst_pool, block_pool, env, cfg_function, function, block_map);
2021-02-03 00:07:00 +00:00
EmitTerminationInsts(cfg_function, block_map);
}
} // Anonymous namespace
2021-01-09 06:30:07 +00:00
2021-02-06 02:11:23 +00:00
IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
Environment& env, const Flow::CFG& cfg) {
IR::Program program;
auto& functions{program.functions};
2021-02-03 00:07:00 +00:00
functions.reserve(cfg.Functions().size());
for (const Flow::Function& cfg_function : cfg.Functions()) {
2021-02-06 02:11:23 +00:00
TranslateFunction(inst_pool, block_pool, env, cfg_function, functions.emplace_back());
2021-02-03 00:07:00 +00:00
}
std::ranges::for_each(functions, Optimization::SsaRewritePass);
for (IR::Function& function : functions) {
Optimization::Invoke(Optimization::GlobalMemoryToStorageBufferPass, function);
Optimization::Invoke(Optimization::ConstantPropagationPass, function);
2021-02-03 00:07:00 +00:00
Optimization::Invoke(Optimization::DeadCodeEliminationPass, function);
Optimization::IdentityRemovalPass(function);
Optimization::VerificationPass(function);
2021-01-09 06:30:07 +00:00
}
2021-02-03 19:43:04 +00:00
//*/
2021-02-06 02:11:23 +00:00
return program;
2021-01-09 06:30:07 +00:00
}
} // namespace Shader::Maxwell