2021-01-09 07:30:07 +01: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-11 20:39:06 +01:00
|
|
|
#include <vector>
|
2021-01-09 07:30:07 +01:00
|
|
|
|
2021-02-06 03:11:23 +01:00
|
|
|
#include "shader_recompiler/frontend/ir/basic_block.h"
|
2021-02-15 00:15:42 +01:00
|
|
|
#include "shader_recompiler/frontend/ir/post_order.h"
|
2021-02-11 20:39:06 +01:00
|
|
|
#include "shader_recompiler/frontend/ir/structured_control_flow.h"
|
2021-01-09 07:30:07 +01:00
|
|
|
#include "shader_recompiler/frontend/maxwell/program.h"
|
|
|
|
#include "shader_recompiler/frontend/maxwell/translate/translate.h"
|
2021-02-03 01:07:00 +01:00
|
|
|
#include "shader_recompiler/ir_opt/passes.h"
|
2021-01-09 07:30:07 +01:00
|
|
|
|
|
|
|
namespace Shader::Maxwell {
|
2021-02-03 01:07:00 +01:00
|
|
|
namespace {
|
2021-02-11 20:39:06 +01:00
|
|
|
IR::BlockList TranslateCode(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
|
|
|
|
Environment& env, Flow::Function& cfg_function) {
|
2021-02-03 01:07:00 +01:00
|
|
|
const size_t num_blocks{cfg_function.blocks.size()};
|
2021-02-11 20:39:06 +01:00
|
|
|
std::vector<IR::Block*> blocks(cfg_function.blocks.size());
|
|
|
|
std::ranges::for_each(cfg_function.blocks, [&, i = size_t{0}](auto& cfg_block) mutable {
|
|
|
|
const u32 begin{cfg_block.begin.Offset()};
|
|
|
|
const u32 end{cfg_block.end.Offset()};
|
|
|
|
blocks[i] = block_pool.Create(inst_pool, begin, end);
|
|
|
|
cfg_block.ir = blocks[i];
|
|
|
|
++i;
|
|
|
|
});
|
|
|
|
std::ranges::for_each(cfg_function.blocks, [&, i = size_t{0}](auto& cfg_block) mutable {
|
|
|
|
IR::Block* const block{blocks[i]};
|
|
|
|
++i;
|
|
|
|
if (cfg_block.end_class != Flow::EndClass::Branch) {
|
|
|
|
block->SetReturn();
|
|
|
|
} else if (cfg_block.cond == IR::Condition{true}) {
|
|
|
|
block->SetBranch(cfg_block.branch_true->ir);
|
|
|
|
} else if (cfg_block.cond == IR::Condition{false}) {
|
|
|
|
block->SetBranch(cfg_block.branch_false->ir);
|
|
|
|
} else {
|
|
|
|
block->SetBranches(cfg_block.cond, cfg_block.branch_true->ir,
|
|
|
|
cfg_block.branch_false->ir);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return IR::VisitAST(inst_pool, block_pool, blocks,
|
|
|
|
[&](IR::Block* block) { Translate(env, block); });
|
2021-02-03 01:07:00 +01:00
|
|
|
}
|
|
|
|
} // Anonymous namespace
|
2021-01-09 07:30:07 +01:00
|
|
|
|
2021-02-06 03:11:23 +01:00
|
|
|
IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
|
2021-02-11 20:39:06 +01:00
|
|
|
Environment& env, Flow::CFG& cfg) {
|
2021-02-06 03:11:23 +01:00
|
|
|
IR::Program program;
|
|
|
|
auto& functions{program.functions};
|
2021-02-03 01:07:00 +01:00
|
|
|
functions.reserve(cfg.Functions().size());
|
2021-02-11 20:39:06 +01:00
|
|
|
for (Flow::Function& cfg_function : cfg.Functions()) {
|
|
|
|
functions.push_back(IR::Function{
|
|
|
|
.blocks{TranslateCode(inst_pool, block_pool, env, cfg_function)},
|
2021-02-16 08:10:22 +01:00
|
|
|
.post_order_blocks{},
|
2021-02-11 20:39:06 +01:00
|
|
|
});
|
2021-02-03 01:07:00 +01:00
|
|
|
}
|
2021-02-21 21:50:14 +01:00
|
|
|
fmt::print(stdout, "{}\n", IR::DumpProgram(program));
|
2021-02-19 22:10:18 +01:00
|
|
|
Optimization::LowerFp16ToFp32(program);
|
2021-02-03 01:07:00 +01:00
|
|
|
for (IR::Function& function : functions) {
|
2021-02-15 00:15:42 +01:00
|
|
|
function.post_order_blocks = PostOrder(function.blocks);
|
|
|
|
Optimization::SsaRewritePass(function.post_order_blocks);
|
|
|
|
}
|
2021-02-16 08:10:22 +01:00
|
|
|
Optimization::GlobalMemoryToStorageBufferPass(program);
|
2021-02-15 00:15:42 +01:00
|
|
|
for (IR::Function& function : functions) {
|
|
|
|
Optimization::PostOrderInvoke(Optimization::ConstantPropagationPass, function);
|
|
|
|
Optimization::PostOrderInvoke(Optimization::DeadCodeEliminationPass, function);
|
2021-02-05 09:58:02 +01:00
|
|
|
Optimization::IdentityRemovalPass(function);
|
|
|
|
Optimization::VerificationPass(function);
|
2021-01-09 07:30:07 +01:00
|
|
|
}
|
2021-02-16 08:10:22 +01:00
|
|
|
Optimization::CollectShaderInfoPass(program);
|
2021-02-19 22:10:18 +01:00
|
|
|
|
2021-02-17 04:59:28 +01:00
|
|
|
fmt::print(stdout, "{}\n", IR::DumpProgram(program));
|
2021-02-06 03:11:23 +01:00
|
|
|
return program;
|
2021-01-09 07:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Shader::Maxwell
|