summaryrefslogtreecommitdiff
path: root/src/shader_recompiler
diff options
context:
space:
mode:
authorGravatar ReinUsesLisp2021-02-05 23:11:23 -0300
committerGravatar ameerj2021-07-22 21:51:21 -0400
commit16cb00c521cae6e93ec49d10e15b575b7bc4857e (patch)
tree3b283895510af56fced7e62031c6beda999c0a1c /src/shader_recompiler
parentshader: Make typed IR (diff)
downloadyuzu-16cb00c521cae6e93ec49d10e15b575b7bc4857e.tar.gz
yuzu-16cb00c521cae6e93ec49d10e15b575b7bc4857e.tar.xz
yuzu-16cb00c521cae6e93ec49d10e15b575b7bc4857e.zip
shader: Add pools and rename files
Diffstat (limited to '')
-rw-r--r--src/shader_recompiler/CMakeLists.txt14
-rw-r--r--src/shader_recompiler/backend/spirv/emit_spirv.h21
-rw-r--r--src/shader_recompiler/frontend/ir/basic_block.cpp5
-rw-r--r--src/shader_recompiler/frontend/ir/basic_block.h11
-rw-r--r--src/shader_recompiler/frontend/ir/function.h12
-rw-r--r--src/shader_recompiler/frontend/ir/microinstruction.h2
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.cpp (renamed from src/shader_recompiler/frontend/ir/opcode.cpp)4
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.h (renamed from src/shader_recompiler/frontend/ir/opcode.h)2
-rw-r--r--src/shader_recompiler/frontend/ir/opcodes.inc (renamed from src/shader_recompiler/frontend/ir/opcode.inc)0
-rw-r--r--src/shader_recompiler/frontend/ir/program.cpp38
-rw-r--r--src/shader_recompiler/frontend/ir/program.h21
-rw-r--r--src/shader_recompiler/frontend/ir/value.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/control_flow.h2
-rw-r--r--src/shader_recompiler/frontend/maxwell/decode.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/decode.h2
-rw-r--r--src/shader_recompiler/frontend/maxwell/opcodes.cpp (renamed from src/shader_recompiler/frontend/maxwell/opcode.cpp)2
-rw-r--r--src/shader_recompiler/frontend/maxwell/opcodes.h (renamed from src/shader_recompiler/frontend/maxwell/opcode.h)0
-rw-r--r--src/shader_recompiler/frontend/maxwell/program.cpp49
-rw-r--r--src/shader_recompiler/frontend/maxwell/program.h22
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_multi_function.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/load_store_attribute.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/load_store_memory.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/move_register.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp2
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/translate.cpp5
-rw-r--r--src/shader_recompiler/frontend/maxwell/translate/translate.h7
-rw-r--r--src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp28
-rw-r--r--src/shader_recompiler/main.cpp11
-rw-r--r--src/shader_recompiler/object_pool.h89
30 files changed, 255 insertions, 108 deletions
diff --git a/src/shader_recompiler/CMakeLists.txt b/src/shader_recompiler/CMakeLists.txt
index 72d5f41d2..248e90d4b 100644
--- a/src/shader_recompiler/CMakeLists.txt
+++ b/src/shader_recompiler/CMakeLists.txt
@@ -1,4 +1,5 @@
1add_executable(shader_recompiler 1add_executable(shader_recompiler
2 backend/spirv/emit_spirv.h
2 environment.h 3 environment.h
3 exception.h 4 exception.h
4 file_environment.cpp 5 file_environment.cpp
@@ -17,10 +18,12 @@ add_executable(shader_recompiler
17 frontend/ir/ir_emitter.h 18 frontend/ir/ir_emitter.h
18 frontend/ir/microinstruction.cpp 19 frontend/ir/microinstruction.cpp
19 frontend/ir/microinstruction.h 20 frontend/ir/microinstruction.h
20 frontend/ir/opcode.cpp 21 frontend/ir/opcodes.cpp
21 frontend/ir/opcode.h 22 frontend/ir/opcodes.h
22 frontend/ir/opcode.inc 23 frontend/ir/opcodes.inc
23 frontend/ir/pred.h 24 frontend/ir/pred.h
25 frontend/ir/program.cpp
26 frontend/ir/program.h
24 frontend/ir/reg.h 27 frontend/ir/reg.h
25 frontend/ir/type.cpp 28 frontend/ir/type.cpp
26 frontend/ir/type.h 29 frontend/ir/type.h
@@ -33,8 +36,8 @@ add_executable(shader_recompiler
33 frontend/maxwell/instruction.h 36 frontend/maxwell/instruction.h
34 frontend/maxwell/location.h 37 frontend/maxwell/location.h
35 frontend/maxwell/maxwell.inc 38 frontend/maxwell/maxwell.inc
36 frontend/maxwell/opcode.cpp 39 frontend/maxwell/opcodes.cpp
37 frontend/maxwell/opcode.h 40 frontend/maxwell/opcodes.h
38 frontend/maxwell/program.cpp 41 frontend/maxwell/program.cpp
39 frontend/maxwell/program.h 42 frontend/maxwell/program.h
40 frontend/maxwell/termination_code.cpp 43 frontend/maxwell/termination_code.cpp
@@ -67,6 +70,7 @@ add_executable(shader_recompiler
67 ir_opt/ssa_rewrite_pass.cpp 70 ir_opt/ssa_rewrite_pass.cpp
68 ir_opt/verification_pass.cpp 71 ir_opt/verification_pass.cpp
69 main.cpp 72 main.cpp
73 object_pool.h
70) 74)
71target_link_libraries(shader_recompiler PRIVATE fmt::fmt) 75target_link_libraries(shader_recompiler PRIVATE fmt::fmt)
72 76
diff --git a/src/shader_recompiler/backend/spirv/emit_spirv.h b/src/shader_recompiler/backend/spirv/emit_spirv.h
new file mode 100644
index 000000000..99cc8e08a
--- /dev/null
+++ b/src/shader_recompiler/backend/spirv/emit_spirv.h
@@ -0,0 +1,21 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "shader_recompiler/frontend/ir/microinstruction.h"
8#include "shader_recompiler/frontend/ir/program.h"
9
10namespace Shader::Backend::SPIRV {
11
12class EmitSPIRV {
13public:
14private:
15 // Microinstruction emitters
16#define OPCODE(name, result_type, ...) void Emit##name(EmitContext& ctx, IR::Inst* inst);
17#include "shader_recompiler/frontend/ir/opcodes.inc"
18#undef OPCODE
19};
20
21} // namespace Shader::Backend::SPIRV
diff --git a/src/shader_recompiler/frontend/ir/basic_block.cpp b/src/shader_recompiler/frontend/ir/basic_block.cpp
index 249251dd0..1a5d82135 100644
--- a/src/shader_recompiler/frontend/ir/basic_block.cpp
+++ b/src/shader_recompiler/frontend/ir/basic_block.cpp
@@ -14,7 +14,8 @@
14 14
15namespace Shader::IR { 15namespace Shader::IR {
16 16
17Block::Block(u32 begin, u32 end) : location_begin{begin}, location_end{end} {} 17Block::Block(ObjectPool<Inst>& inst_pool_, u32 begin, u32 end)
18 : inst_pool{&inst_pool_}, location_begin{begin}, location_end{end} {}
18 19
19Block::~Block() = default; 20Block::~Block() = default;
20 21
@@ -24,7 +25,7 @@ void Block::AppendNewInst(Opcode op, std::initializer_list<Value> args) {
24 25
25Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op, 26Block::iterator Block::PrependNewInst(iterator insertion_point, Opcode op,
26 std::initializer_list<Value> args, u64 flags) { 27 std::initializer_list<Value> args, u64 flags) {
27 Inst* const inst{std::construct_at(instruction_alloc_pool.allocate(), op, flags)}; 28 Inst* const inst{inst_pool->Create(op, flags)};
28 const auto result_it{instructions.insert(insertion_point, *inst)}; 29 const auto result_it{instructions.insert(insertion_point, *inst)};
29 30
30 if (inst->NumArgs() != args.size()) { 31 if (inst->NumArgs() != args.size()) {
diff --git a/src/shader_recompiler/frontend/ir/basic_block.h b/src/shader_recompiler/frontend/ir/basic_block.h
index ec4a41cb1..ec3ad6263 100644
--- a/src/shader_recompiler/frontend/ir/basic_block.h
+++ b/src/shader_recompiler/frontend/ir/basic_block.h
@@ -10,9 +10,9 @@
10#include <vector> 10#include <vector>
11 11
12#include <boost/intrusive/list.hpp> 12#include <boost/intrusive/list.hpp>
13#include <boost/pool/pool_alloc.hpp>
14 13
15#include "shader_recompiler/frontend/ir/microinstruction.h" 14#include "shader_recompiler/frontend/ir/microinstruction.h"
15#include "shader_recompiler/object_pool.h"
16 16
17namespace Shader::IR { 17namespace Shader::IR {
18 18
@@ -25,7 +25,7 @@ public:
25 using reverse_iterator = InstructionList::reverse_iterator; 25 using reverse_iterator = InstructionList::reverse_iterator;
26 using const_reverse_iterator = InstructionList::const_reverse_iterator; 26 using const_reverse_iterator = InstructionList::const_reverse_iterator;
27 27
28 explicit Block(u32 begin, u32 end); 28 explicit Block(ObjectPool<Inst>& inst_pool_, u32 begin, u32 end);
29 ~Block(); 29 ~Block();
30 30
31 Block(const Block&) = delete; 31 Block(const Block&) = delete;
@@ -119,6 +119,8 @@ public:
119 } 119 }
120 120
121private: 121private:
122 /// Memory pool for instruction list
123 ObjectPool<Inst>* inst_pool;
122 /// Starting location of this block 124 /// Starting location of this block
123 u32 location_begin; 125 u32 location_begin;
124 /// End location of this block 126 /// End location of this block
@@ -127,11 +129,6 @@ private:
127 /// List of instructions in this block 129 /// List of instructions in this block
128 InstructionList instructions; 130 InstructionList instructions;
129 131
130 /// Memory pool for instruction list
131 boost::fast_pool_allocator<Inst, boost::default_user_allocator_malloc_free,
132 boost::details::pool::null_mutex>
133 instruction_alloc_pool;
134
135 /// Block immediate predecessors 132 /// Block immediate predecessors
136 std::vector<IR::Block*> imm_predecessors; 133 std::vector<IR::Block*> imm_predecessors;
137}; 134};
diff --git a/src/shader_recompiler/frontend/ir/function.h b/src/shader_recompiler/frontend/ir/function.h
index 2d4dc5b98..bba7d1d39 100644
--- a/src/shader_recompiler/frontend/ir/function.h
+++ b/src/shader_recompiler/frontend/ir/function.h
@@ -4,22 +4,14 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <memory> 7#include <boost/container/small_vector.hpp>
8#include <vector>
9 8
10#include "shader_recompiler/frontend/ir/basic_block.h" 9#include "shader_recompiler/frontend/ir/basic_block.h"
11 10
12namespace Shader::IR { 11namespace Shader::IR {
13 12
14struct Function { 13struct Function {
15 struct InplaceDelete { 14 boost::container::small_vector<Block*, 16> blocks;
16 void operator()(IR::Block* block) const noexcept {
17 std::destroy_at(block);
18 }
19 };
20 using UniqueBlock = std::unique_ptr<IR::Block, InplaceDelete>;
21
22 std::vector<UniqueBlock> blocks;
23}; 15};
24 16
25} // namespace Shader::IR 17} // namespace Shader::IR
diff --git a/src/shader_recompiler/frontend/ir/microinstruction.h b/src/shader_recompiler/frontend/ir/microinstruction.h
index 22101c9e2..80baffb2e 100644
--- a/src/shader_recompiler/frontend/ir/microinstruction.h
+++ b/src/shader_recompiler/frontend/ir/microinstruction.h
@@ -13,7 +13,7 @@
13#include <boost/intrusive/list.hpp> 13#include <boost/intrusive/list.hpp>
14 14
15#include "common/common_types.h" 15#include "common/common_types.h"
16#include "shader_recompiler/frontend/ir/opcode.h" 16#include "shader_recompiler/frontend/ir/opcodes.h"
17#include "shader_recompiler/frontend/ir/type.h" 17#include "shader_recompiler/frontend/ir/type.h"
18#include "shader_recompiler/frontend/ir/value.h" 18#include "shader_recompiler/frontend/ir/value.h"
19 19
diff --git a/src/shader_recompiler/frontend/ir/opcode.cpp b/src/shader_recompiler/frontend/ir/opcodes.cpp
index 65d074029..1f188411a 100644
--- a/src/shader_recompiler/frontend/ir/opcode.cpp
+++ b/src/shader_recompiler/frontend/ir/opcodes.cpp
@@ -7,7 +7,7 @@
7#include <string_view> 7#include <string_view>
8 8
9#include "shader_recompiler/exception.h" 9#include "shader_recompiler/exception.h"
10#include "shader_recompiler/frontend/ir/opcode.h" 10#include "shader_recompiler/frontend/ir/opcodes.h"
11 11
12namespace Shader::IR { 12namespace Shader::IR {
13namespace { 13namespace {
@@ -26,7 +26,7 @@ constexpr std::array META_TABLE{
26 .type{type_token}, \ 26 .type{type_token}, \
27 .arg_types{__VA_ARGS__}, \ 27 .arg_types{__VA_ARGS__}, \
28 }, 28 },
29#include "opcode.inc" 29#include "opcodes.inc"
30#undef OPCODE 30#undef OPCODE
31}; 31};
32 32
diff --git a/src/shader_recompiler/frontend/ir/opcode.h b/src/shader_recompiler/frontend/ir/opcodes.h
index 1f4440379..999fb2e77 100644
--- a/src/shader_recompiler/frontend/ir/opcode.h
+++ b/src/shader_recompiler/frontend/ir/opcodes.h
@@ -14,7 +14,7 @@ namespace Shader::IR {
14 14
15enum class Opcode { 15enum class Opcode {
16#define OPCODE(name, ...) name, 16#define OPCODE(name, ...) name,
17#include "opcode.inc" 17#include "opcodes.inc"
18#undef OPCODE 18#undef OPCODE
19}; 19};
20 20
diff --git a/src/shader_recompiler/frontend/ir/opcode.inc b/src/shader_recompiler/frontend/ir/opcodes.inc
index 6eb105d92..6eb105d92 100644
--- a/src/shader_recompiler/frontend/ir/opcode.inc
+++ b/src/shader_recompiler/frontend/ir/opcodes.inc
diff --git a/src/shader_recompiler/frontend/ir/program.cpp b/src/shader_recompiler/frontend/ir/program.cpp
new file mode 100644
index 000000000..0ce99ef2a
--- /dev/null
+++ b/src/shader_recompiler/frontend/ir/program.cpp
@@ -0,0 +1,38 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <map>
8#include <string>
9
10#include <fmt/format.h>
11
12#include "shader_recompiler/frontend/ir/function.h"
13#include "shader_recompiler/frontend/ir/program.h"
14
15namespace Shader::IR {
16
17std::string DumpProgram(const Program& program) {
18 size_t index{0};
19 std::map<const IR::Inst*, size_t> inst_to_index;
20 std::map<const IR::Block*, size_t> block_to_index;
21
22 for (const IR::Function& function : program.functions) {
23 for (const IR::Block* const block : function.blocks) {
24 block_to_index.emplace(block, index);
25 ++index;
26 }
27 }
28 std::string ret;
29 for (const IR::Function& function : program.functions) {
30 ret += fmt::format("Function\n");
31 for (const auto& block : function.blocks) {
32 ret += IR::DumpBlock(*block, block_to_index, inst_to_index, index) + '\n';
33 }
34 }
35 return ret;
36}
37
38} // namespace Shader::IR \ No newline at end of file
diff --git a/src/shader_recompiler/frontend/ir/program.h b/src/shader_recompiler/frontend/ir/program.h
new file mode 100644
index 000000000..efaf1aa1e
--- /dev/null
+++ b/src/shader_recompiler/frontend/ir/program.h
@@ -0,0 +1,21 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8
9#include <boost/container/small_vector.hpp>
10
11#include "shader_recompiler/frontend/ir/function.h"
12
13namespace Shader::IR {
14
15struct Program {
16 boost::container::small_vector<Function, 1> functions;
17};
18
19[[nodiscard]] std::string DumpProgram(const Program& program);
20
21} // namespace Shader::IR
diff --git a/src/shader_recompiler/frontend/ir/value.cpp b/src/shader_recompiler/frontend/ir/value.cpp
index 93ff8ccf1..9ea61813b 100644
--- a/src/shader_recompiler/frontend/ir/value.cpp
+++ b/src/shader_recompiler/frontend/ir/value.cpp
@@ -3,7 +3,7 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "shader_recompiler/frontend/ir/microinstruction.h" 5#include "shader_recompiler/frontend/ir/microinstruction.h"
6#include "shader_recompiler/frontend/ir/opcode.h" 6#include "shader_recompiler/frontend/ir/opcodes.h"
7#include "shader_recompiler/frontend/ir/value.h" 7#include "shader_recompiler/frontend/ir/value.h"
8 8
9namespace Shader::IR { 9namespace Shader::IR {
diff --git a/src/shader_recompiler/frontend/maxwell/control_flow.h b/src/shader_recompiler/frontend/maxwell/control_flow.h
index 20ada8afd..49b369282 100644
--- a/src/shader_recompiler/frontend/maxwell/control_flow.h
+++ b/src/shader_recompiler/frontend/maxwell/control_flow.h
@@ -16,7 +16,7 @@
16#include "shader_recompiler/frontend/ir/condition.h" 16#include "shader_recompiler/frontend/ir/condition.h"
17#include "shader_recompiler/frontend/maxwell/instruction.h" 17#include "shader_recompiler/frontend/maxwell/instruction.h"
18#include "shader_recompiler/frontend/maxwell/location.h" 18#include "shader_recompiler/frontend/maxwell/location.h"
19#include "shader_recompiler/frontend/maxwell/opcode.h" 19#include "shader_recompiler/frontend/maxwell/opcodes.h"
20 20
21namespace Shader::Maxwell::Flow { 21namespace Shader::Maxwell::Flow {
22 22
diff --git a/src/shader_recompiler/frontend/maxwell/decode.cpp b/src/shader_recompiler/frontend/maxwell/decode.cpp
index ab1cc6c8d..bd85afa1e 100644
--- a/src/shader_recompiler/frontend/maxwell/decode.cpp
+++ b/src/shader_recompiler/frontend/maxwell/decode.cpp
@@ -11,7 +11,7 @@
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "shader_recompiler/exception.h" 12#include "shader_recompiler/exception.h"
13#include "shader_recompiler/frontend/maxwell/decode.h" 13#include "shader_recompiler/frontend/maxwell/decode.h"
14#include "shader_recompiler/frontend/maxwell/opcode.h" 14#include "shader_recompiler/frontend/maxwell/opcodes.h"
15 15
16namespace Shader::Maxwell { 16namespace Shader::Maxwell {
17namespace { 17namespace {
diff --git a/src/shader_recompiler/frontend/maxwell/decode.h b/src/shader_recompiler/frontend/maxwell/decode.h
index 2a3dd28e8..b4f080fd7 100644
--- a/src/shader_recompiler/frontend/maxwell/decode.h
+++ b/src/shader_recompiler/frontend/maxwell/decode.h
@@ -5,7 +5,7 @@
5#pragma once 5#pragma once
6 6
7#include "common/common_types.h" 7#include "common/common_types.h"
8#include "shader_recompiler/frontend/maxwell/opcode.h" 8#include "shader_recompiler/frontend/maxwell/opcodes.h"
9 9
10namespace Shader::Maxwell { 10namespace Shader::Maxwell {
11 11
diff --git a/src/shader_recompiler/frontend/maxwell/opcode.cpp b/src/shader_recompiler/frontend/maxwell/opcodes.cpp
index 8a7bdb611..12ddf2ac9 100644
--- a/src/shader_recompiler/frontend/maxwell/opcode.cpp
+++ b/src/shader_recompiler/frontend/maxwell/opcodes.cpp
@@ -5,7 +5,7 @@
5#include <array> 5#include <array>
6 6
7#include "shader_recompiler/exception.h" 7#include "shader_recompiler/exception.h"
8#include "shader_recompiler/frontend/maxwell/opcode.h" 8#include "shader_recompiler/frontend/maxwell/opcodes.h"
9 9
10namespace Shader::Maxwell { 10namespace Shader::Maxwell {
11namespace { 11namespace {
diff --git a/src/shader_recompiler/frontend/maxwell/opcode.h b/src/shader_recompiler/frontend/maxwell/opcodes.h
index cd574f29d..cd574f29d 100644
--- a/src/shader_recompiler/frontend/maxwell/opcode.h
+++ b/src/shader_recompiler/frontend/maxwell/opcodes.h
diff --git a/src/shader_recompiler/frontend/maxwell/program.cpp b/src/shader_recompiler/frontend/maxwell/program.cpp
index b3f2de852..8cdd20804 100644
--- a/src/shader_recompiler/frontend/maxwell/program.cpp
+++ b/src/shader_recompiler/frontend/maxwell/program.cpp
@@ -5,6 +5,7 @@
5#include <algorithm> 5#include <algorithm>
6#include <memory> 6#include <memory>
7 7
8#include "shader_recompiler/frontend/ir/basic_block.h"
8#include "shader_recompiler/frontend/maxwell/program.h" 9#include "shader_recompiler/frontend/maxwell/program.h"
9#include "shader_recompiler/frontend/maxwell/termination_code.h" 10#include "shader_recompiler/frontend/maxwell/termination_code.h"
10#include "shader_recompiler/frontend/maxwell/translate/translate.h" 11#include "shader_recompiler/frontend/maxwell/translate/translate.h"
@@ -12,17 +13,18 @@
12 13
13namespace Shader::Maxwell { 14namespace Shader::Maxwell {
14namespace { 15namespace {
15void TranslateCode(Environment& env, const Flow::Function& cfg_function, IR::Function& function, 16void TranslateCode(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
16 std::span<IR::Block*> block_map, IR::Block* block_memory) { 17 Environment& env, const Flow::Function& cfg_function, IR::Function& function,
18 std::span<IR::Block*> block_map) {
17 const size_t num_blocks{cfg_function.blocks.size()}; 19 const size_t num_blocks{cfg_function.blocks.size()};
18 function.blocks.reserve(num_blocks); 20 function.blocks.reserve(num_blocks);
19 21
20 for (const Flow::BlockId block_id : cfg_function.blocks) { 22 for (const Flow::BlockId block_id : cfg_function.blocks) {
21 const Flow::Block& flow_block{cfg_function.blocks_data[block_id]}; 23 const Flow::Block& flow_block{cfg_function.blocks_data[block_id]};
22 24
23 function.blocks.emplace_back(std::construct_at(block_memory, Translate(env, flow_block))); 25 IR::Block* const ir_block{block_pool.Create(Translate(inst_pool, env, flow_block))};
24 block_map[flow_block.id] = function.blocks.back().get(); 26 block_map[flow_block.id] = ir_block;
25 ++block_memory; 27 function.blocks.emplace_back(ir_block);
26 } 28 }
27} 29}
28 30
@@ -34,21 +36,24 @@ void EmitTerminationInsts(const Flow::Function& cfg_function,
34 } 36 }
35} 37}
36 38
37void TranslateFunction(Environment& env, const Flow::Function& cfg_function, IR::Function& function, 39void TranslateFunction(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
38 IR::Block* block_memory) { 40 Environment& env, const Flow::Function& cfg_function,
41 IR::Function& function) {
39 std::vector<IR::Block*> block_map; 42 std::vector<IR::Block*> block_map;
40 block_map.resize(cfg_function.blocks_data.size()); 43 block_map.resize(cfg_function.blocks_data.size());
41 44
42 TranslateCode(env, cfg_function, function, block_map, block_memory); 45 TranslateCode(inst_pool, block_pool, env, cfg_function, function, block_map);
43 EmitTerminationInsts(cfg_function, block_map); 46 EmitTerminationInsts(cfg_function, block_map);
44} 47}
45} // Anonymous namespace 48} // Anonymous namespace
46 49
47Program::Program(Environment& env, const Flow::CFG& cfg) { 50IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool, ObjectPool<IR::Block>& block_pool,
51 Environment& env, const Flow::CFG& cfg) {
52 IR::Program program;
53 auto& functions{program.functions};
48 functions.reserve(cfg.Functions().size()); 54 functions.reserve(cfg.Functions().size());
49 for (const Flow::Function& cfg_function : cfg.Functions()) { 55 for (const Flow::Function& cfg_function : cfg.Functions()) {
50 TranslateFunction(env, cfg_function, functions.emplace_back(), 56 TranslateFunction(inst_pool, block_pool, env, cfg_function, functions.emplace_back());
51 block_alloc_pool.allocate(cfg_function.blocks.size()));
52 } 57 }
53 std::ranges::for_each(functions, Optimization::SsaRewritePass); 58 std::ranges::for_each(functions, Optimization::SsaRewritePass);
54 for (IR::Function& function : functions) { 59 for (IR::Function& function : functions) {
@@ -59,27 +64,7 @@ Program::Program(Environment& env, const Flow::CFG& cfg) {
59 Optimization::VerificationPass(function); 64 Optimization::VerificationPass(function);
60 } 65 }
61 //*/ 66 //*/
62} 67 return program;
63
64std::string DumpProgram(const Program& program) {
65 size_t index{0};
66 std::map<const IR::Inst*, size_t> inst_to_index;
67 std::map<const IR::Block*, size_t> block_to_index;
68
69 for (const IR::Function& function : program.functions) {
70 for (const auto& block : function.blocks) {
71 block_to_index.emplace(block.get(), index);
72 ++index;
73 }
74 }
75 std::string ret;
76 for (const IR::Function& function : program.functions) {
77 ret += fmt::format("Function\n");
78 for (const auto& block : function.blocks) {
79 ret += IR::DumpBlock(*block, block_to_index, inst_to_index, index) + '\n';
80 }
81 }
82 return ret;
83} 68}
84 69
85} // namespace Shader::Maxwell 70} // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/frontend/maxwell/program.h b/src/shader_recompiler/frontend/maxwell/program.h
index 36e678a9e..3355ab129 100644
--- a/src/shader_recompiler/frontend/maxwell/program.h
+++ b/src/shader_recompiler/frontend/maxwell/program.h
@@ -9,28 +9,16 @@
9#include <vector> 9#include <vector>
10 10
11#include <boost/container/small_vector.hpp> 11#include <boost/container/small_vector.hpp>
12#include <boost/pool/pool_alloc.hpp>
13 12
14#include "shader_recompiler/environment.h" 13#include "shader_recompiler/environment.h"
15#include "shader_recompiler/frontend/ir/basic_block.h" 14#include "shader_recompiler/frontend/ir/program.h"
16#include "shader_recompiler/frontend/ir/function.h"
17#include "shader_recompiler/frontend/maxwell/control_flow.h" 15#include "shader_recompiler/frontend/maxwell/control_flow.h"
16#include "shader_recompiler/object_pool.h"
18 17
19namespace Shader::Maxwell { 18namespace Shader::Maxwell {
20 19
21class Program { 20[[nodiscard]] IR::Program TranslateProgram(ObjectPool<IR::Inst>& inst_pool,
22 friend std::string DumpProgram(const Program& program); 21 ObjectPool<IR::Block>& block_pool, Environment& env,
23 22 const Flow::CFG& cfg);
24public:
25 explicit Program(Environment& env, const Flow::CFG& cfg);
26
27private:
28 boost::pool_allocator<IR::Block, boost::default_user_allocator_new_delete,
29 boost::details::pool::null_mutex>
30 block_alloc_pool;
31 boost::container::small_vector<IR::Function, 1> functions;
32};
33
34[[nodiscard]] std::string DumpProgram(const Program& program);
35 23
36} // namespace Shader::Maxwell 24} // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp
index acd8445ad..3d0c48457 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_conversion_integer.cpp
@@ -4,7 +4,7 @@
4 4
5#include "common/common_types.h" 5#include "common/common_types.h"
6#include "shader_recompiler/exception.h" 6#include "shader_recompiler/exception.h"
7#include "shader_recompiler/frontend/maxwell/opcode.h" 7#include "shader_recompiler/frontend/maxwell/opcodes.h"
8#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 8#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
9 9
10namespace Shader::Maxwell { 10namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_multi_function.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_multi_function.cpp
index 90cddb18b..ba005fbf4 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_multi_function.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/floating_point_multi_function.cpp
@@ -5,7 +5,7 @@
5#include "common/bit_field.h" 5#include "common/bit_field.h"
6#include "common/common_types.h" 6#include "common/common_types.h"
7#include "shader_recompiler/exception.h" 7#include "shader_recompiler/exception.h"
8#include "shader_recompiler/frontend/maxwell/opcode.h" 8#include "shader_recompiler/frontend/maxwell/opcodes.h"
9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
10 10
11namespace Shader::Maxwell { 11namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_attribute.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_attribute.cpp
index de65173e8..ad97786d4 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_attribute.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_attribute.cpp
@@ -6,7 +6,7 @@
6#include "common/common_types.h" 6#include "common/common_types.h"
7#include "shader_recompiler/exception.h" 7#include "shader_recompiler/exception.h"
8#include "shader_recompiler/frontend/ir/ir_emitter.h" 8#include "shader_recompiler/frontend/ir/ir_emitter.h"
9#include "shader_recompiler/frontend/maxwell/opcode.h" 9#include "shader_recompiler/frontend/maxwell/opcodes.h"
10#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 10#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
11 11
12namespace Shader::Maxwell { 12namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_memory.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_memory.cpp
index 9f1570479..727524284 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_memory.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/load_store_memory.cpp
@@ -5,7 +5,7 @@
5#include "common/bit_field.h" 5#include "common/bit_field.h"
6#include "common/common_types.h" 6#include "common/common_types.h"
7#include "shader_recompiler/exception.h" 7#include "shader_recompiler/exception.h"
8#include "shader_recompiler/frontend/maxwell/opcode.h" 8#include "shader_recompiler/frontend/maxwell/opcodes.h"
9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
10 10
11namespace Shader::Maxwell { 11namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/move_register.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/move_register.cpp
index 1711d3f48..1f83d1068 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/move_register.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/move_register.cpp
@@ -5,7 +5,7 @@
5#include "common/bit_field.h" 5#include "common/bit_field.h"
6#include "common/common_types.h" 6#include "common/common_types.h"
7#include "shader_recompiler/exception.h" 7#include "shader_recompiler/exception.h"
8#include "shader_recompiler/frontend/maxwell/opcode.h" 8#include "shader_recompiler/frontend/maxwell/opcodes.h"
9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 9#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
10 10
11namespace Shader::Maxwell { 11namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp b/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp
index d70399f6b..1bb160acb 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/impl/not_implemented.cpp
@@ -4,7 +4,7 @@
4 4
5#include "common/common_types.h" 5#include "common/common_types.h"
6#include "shader_recompiler/exception.h" 6#include "shader_recompiler/exception.h"
7#include "shader_recompiler/frontend/maxwell/opcode.h" 7#include "shader_recompiler/frontend/maxwell/opcodes.h"
8#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h" 8#include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
9 9
10namespace Shader::Maxwell { 10namespace Shader::Maxwell {
diff --git a/src/shader_recompiler/frontend/maxwell/translate/translate.cpp b/src/shader_recompiler/frontend/maxwell/translate/translate.cpp
index 66a306745..dcc3f6c0e 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/translate.cpp
+++ b/src/shader_recompiler/frontend/maxwell/translate/translate.cpp
@@ -23,8 +23,9 @@ static void Invoke(TranslatorVisitor& visitor, Location pc, u64 insn) {
23 } 23 }
24} 24}
25 25
26IR::Block Translate(Environment& env, const Flow::Block& flow_block) { 26IR::Block Translate(ObjectPool<IR::Inst>& inst_pool, Environment& env,
27 IR::Block block{flow_block.begin.Offset(), flow_block.end.Offset()}; 27 const Flow::Block& flow_block) {
28 IR::Block block{inst_pool, flow_block.begin.Offset(), flow_block.end.Offset()};
28 TranslatorVisitor visitor{env, block}; 29 TranslatorVisitor visitor{env, block};
29 30
30 const Location pc_end{flow_block.end}; 31 const Location pc_end{flow_block.end};
diff --git a/src/shader_recompiler/frontend/maxwell/translate/translate.h b/src/shader_recompiler/frontend/maxwell/translate/translate.h
index 788742dea..c1c21b278 100644
--- a/src/shader_recompiler/frontend/maxwell/translate/translate.h
+++ b/src/shader_recompiler/frontend/maxwell/translate/translate.h
@@ -6,11 +6,14 @@
6 6
7#include "shader_recompiler/environment.h" 7#include "shader_recompiler/environment.h"
8#include "shader_recompiler/frontend/ir/basic_block.h" 8#include "shader_recompiler/frontend/ir/basic_block.h"
9#include "shader_recompiler/frontend/maxwell/location.h" 9#include "shader_recompiler/frontend/ir/microinstruction.h"
10#include "shader_recompiler/frontend/maxwell/control_flow.h" 10#include "shader_recompiler/frontend/maxwell/control_flow.h"
11#include "shader_recompiler/frontend/maxwell/location.h"
12#include "shader_recompiler/object_pool.h"
11 13
12namespace Shader::Maxwell { 14namespace Shader::Maxwell {
13 15
14[[nodiscard]] IR::Block Translate(Environment& env, const Flow::Block& flow_block); 16[[nodiscard]] IR::Block Translate(ObjectPool<IR::Inst>& inst_pool, Environment& env,
17 const Flow::Block& flow_block);
15 18
16} // namespace Shader::Maxwell 19} // namespace Shader::Maxwell
diff --git a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
index a62d3f56b..7713e3ba9 100644
--- a/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
+++ b/src/shader_recompiler/ir_opt/ssa_rewrite_pass.cpp
@@ -19,7 +19,7 @@
19#include "shader_recompiler/frontend/ir/basic_block.h" 19#include "shader_recompiler/frontend/ir/basic_block.h"
20#include "shader_recompiler/frontend/ir/function.h" 20#include "shader_recompiler/frontend/ir/function.h"
21#include "shader_recompiler/frontend/ir/microinstruction.h" 21#include "shader_recompiler/frontend/ir/microinstruction.h"
22#include "shader_recompiler/frontend/ir/opcode.h" 22#include "shader_recompiler/frontend/ir/opcodes.h"
23#include "shader_recompiler/frontend/ir/pred.h" 23#include "shader_recompiler/frontend/ir/pred.h"
24#include "shader_recompiler/frontend/ir/reg.h" 24#include "shader_recompiler/frontend/ir/reg.h"
25#include "shader_recompiler/ir_opt/passes.h" 25#include "shader_recompiler/ir_opt/passes.h"
@@ -150,52 +150,52 @@ private:
150 150
151void SsaRewritePass(IR::Function& function) { 151void SsaRewritePass(IR::Function& function) {
152 Pass pass; 152 Pass pass;
153 for (const auto& block : function.blocks) { 153 for (IR::Block* const block : function.blocks) {
154 for (IR::Inst& inst : block->Instructions()) { 154 for (IR::Inst& inst : block->Instructions()) {
155 switch (inst.Opcode()) { 155 switch (inst.Opcode()) {
156 case IR::Opcode::SetRegister: 156 case IR::Opcode::SetRegister:
157 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) { 157 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
158 pass.WriteVariable(reg, block.get(), inst.Arg(1)); 158 pass.WriteVariable(reg, block, inst.Arg(1));
159 } 159 }
160 break; 160 break;
161 case IR::Opcode::SetPred: 161 case IR::Opcode::SetPred:
162 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) { 162 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
163 pass.WriteVariable(pred, block.get(), inst.Arg(1)); 163 pass.WriteVariable(pred, block, inst.Arg(1));
164 } 164 }
165 break; 165 break;
166 case IR::Opcode::SetZFlag: 166 case IR::Opcode::SetZFlag:
167 pass.WriteVariable(ZeroFlagTag{}, block.get(), inst.Arg(0)); 167 pass.WriteVariable(ZeroFlagTag{}, block, inst.Arg(0));
168 break; 168 break;
169 case IR::Opcode::SetSFlag: 169 case IR::Opcode::SetSFlag:
170 pass.WriteVariable(SignFlagTag{}, block.get(), inst.Arg(0)); 170 pass.WriteVariable(SignFlagTag{}, block, inst.Arg(0));
171 break; 171 break;
172 case IR::Opcode::SetCFlag: 172 case IR::Opcode::SetCFlag:
173 pass.WriteVariable(CarryFlagTag{}, block.get(), inst.Arg(0)); 173 pass.WriteVariable(CarryFlagTag{}, block, inst.Arg(0));
174 break; 174 break;
175 case IR::Opcode::SetOFlag: 175 case IR::Opcode::SetOFlag:
176 pass.WriteVariable(OverflowFlagTag{}, block.get(), inst.Arg(0)); 176 pass.WriteVariable(OverflowFlagTag{}, block, inst.Arg(0));
177 break; 177 break;
178 case IR::Opcode::GetRegister: 178 case IR::Opcode::GetRegister:
179 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) { 179 if (const IR::Reg reg{inst.Arg(0).Reg()}; reg != IR::Reg::RZ) {
180 inst.ReplaceUsesWith(pass.ReadVariable(reg, block.get())); 180 inst.ReplaceUsesWith(pass.ReadVariable(reg, block));
181 } 181 }
182 break; 182 break;
183 case IR::Opcode::GetPred: 183 case IR::Opcode::GetPred:
184 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) { 184 if (const IR::Pred pred{inst.Arg(0).Pred()}; pred != IR::Pred::PT) {
185 inst.ReplaceUsesWith(pass.ReadVariable(pred, block.get())); 185 inst.ReplaceUsesWith(pass.ReadVariable(pred, block));
186 } 186 }
187 break; 187 break;
188 case IR::Opcode::GetZFlag: 188 case IR::Opcode::GetZFlag:
189 inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block.get())); 189 inst.ReplaceUsesWith(pass.ReadVariable(ZeroFlagTag{}, block));
190 break; 190 break;
191 case IR::Opcode::GetSFlag: 191 case IR::Opcode::GetSFlag:
192 inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block.get())); 192 inst.ReplaceUsesWith(pass.ReadVariable(SignFlagTag{}, block));
193 break; 193 break;
194 case IR::Opcode::GetCFlag: 194 case IR::Opcode::GetCFlag:
195 inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block.get())); 195 inst.ReplaceUsesWith(pass.ReadVariable(CarryFlagTag{}, block));
196 break; 196 break;
197 case IR::Opcode::GetOFlag: 197 case IR::Opcode::GetOFlag:
198 inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block.get())); 198 inst.ReplaceUsesWith(pass.ReadVariable(OverflowFlagTag{}, block));
199 break; 199 break;
200 default: 200 default:
201 break; 201 break;
diff --git a/src/shader_recompiler/main.cpp b/src/shader_recompiler/main.cpp
index e6596d828..19e36590c 100644
--- a/src/shader_recompiler/main.cpp
+++ b/src/shader_recompiler/main.cpp
@@ -56,6 +56,13 @@ int main() {
56 auto cfg{std::make_unique<Flow::CFG>(env, 0)}; 56 auto cfg{std::make_unique<Flow::CFG>(env, 0)};
57 // fmt::print(stdout, "{}\n", cfg->Dot()); 57 // fmt::print(stdout, "{}\n", cfg->Dot());
58 58
59 Program program{env, *cfg}; 59 auto inst_pool{std::make_unique<ObjectPool<IR::Inst>>()};
60 fmt::print(stdout, "{}\n", DumpProgram(program)); 60 auto block_pool{std::make_unique<ObjectPool<IR::Block>>()};
61
62 for (int i = 0; i < 8192 * 4; ++i) {
63 void(inst_pool->Create(IR::Opcode::Void, 0));
64 }
65
66 IR::Program program{TranslateProgram(*inst_pool, *block_pool, env, *cfg)};
67 fmt::print(stdout, "{}\n", IR::DumpProgram(program));
61} 68}
diff --git a/src/shader_recompiler/object_pool.h b/src/shader_recompiler/object_pool.h
new file mode 100644
index 000000000..7c65bbd92
--- /dev/null
+++ b/src/shader_recompiler/object_pool.h
@@ -0,0 +1,89 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include <type_traits>
9
10namespace Shader {
11
12template <typename T, size_t chunk_size = 8192>
13requires std::is_destructible_v<T> class ObjectPool {
14public:
15 ~ObjectPool() {
16 std::unique_ptr<Chunk> tree_owner;
17 Chunk* chunk{&root};
18 while (chunk) {
19 for (size_t obj_id = chunk->free_objects; obj_id < chunk_size; ++obj_id) {
20 chunk->storage[obj_id].object.~T();
21 }
22 tree_owner = std::move(chunk->next);
23 chunk = tree_owner.get();
24 }
25 }
26
27 template <typename... Args>
28 requires std::is_constructible_v<T, Args...> [[nodiscard]] T* Create(Args&&... args) {
29 return std::construct_at(Memory(), std::forward<Args>(args)...);
30 }
31
32 void ReleaseContents() {
33 Chunk* chunk{&root};
34 if (chunk) {
35 const size_t free_objects{chunk->free_objects};
36 if (free_objects == chunk_size) {
37 break;
38 }
39 chunk->free_objects = chunk_size;
40 for (size_t obj_id = free_objects; obj_id < chunk_size; ++obj_id) {
41 chunk->storage[obj_id].object.~T();
42 }
43 chunk = chunk->next.get();
44 }
45 node = &root;
46 }
47
48private:
49 struct NonTrivialDummy {
50 NonTrivialDummy() noexcept {}
51 };
52
53 union Storage {
54 Storage() noexcept {}
55 ~Storage() noexcept {}
56
57 NonTrivialDummy dummy{};
58 T object;
59 };
60
61 struct Chunk {
62 size_t free_objects = chunk_size;
63 std::array<Storage, chunk_size> storage;
64 std::unique_ptr<Chunk> next;
65 };
66
67 [[nodiscard]] T* Memory() {
68 Chunk* const chunk{FreeChunk()};
69 return &chunk->storage[--chunk->free_objects].object;
70 }
71
72 [[nodiscard]] Chunk* FreeChunk() {
73 if (node->free_objects > 0) {
74 return node;
75 }
76 if (node->next) {
77 node = node->next.get();
78 return node;
79 }
80 node->next = std::make_unique<Chunk>();
81 node = node->next.get();
82 return node;
83 }
84
85 Chunk* node{&root};
86 Chunk root;
87};
88
89} // namespace Shader