From 42248408a970f11d0c256fe5d33dee8d03fb3982 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Fri, 18 Oct 2019 04:25:57 -0300 Subject: [PATCH] Remove Emit entry in favor of auto-emitting code All instructions but OpVariable and OpLabel are automatically emitted. These functions have to call AddLocalVariable/AddGlobalVariable or AddLabel respectively. --- include/sirit/sirit.h | 24 +++++++++++++++++++----- src/instructions/flow.cpp | 2 +- src/instructions/memory.cpp | 2 +- src/sirit.cpp | 20 +++++++++++--------- tests/main.cpp | 32 ++++++++++++++++---------------- 5 files changed, 48 insertions(+), 32 deletions(-) diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index ccc06e4..d889cbc 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -70,12 +70,26 @@ public: } /** - * Adds an instruction to module's code - * @param op Instruction to insert into code. Types and constants must not - * be emitted. - * @return Returns op. + * Adds an existing label to the code + * @param label Label to insert into code. + * @return Returns label. */ - Id Emit(Id op); + Id AddLabel(Id label); + + /** + * Adds a label to the code + * @return Returns the created label. + */ + Id AddLabel() { + return AddLabel(OpLabel()); + } + + /** + * Adds a local variable to the code + * @param variable Variable to insert into code. + * @return Returns variable. + */ + Id AddLocalVariable(Id label); /** * Adds a global variable diff --git a/src/instructions/flow.cpp b/src/instructions/flow.cpp index a86b8e1..ccec4bf 100644 --- a/src/instructions/flow.cpp +++ b/src/instructions/flow.cpp @@ -30,7 +30,7 @@ Id Module::OpSelectionMerge(Id merge_block, spv::SelectionControlMask selection_ } Id Module::OpLabel() { - return AddCode(spv::Op::OpLabel, bound++); + return code_store.emplace_back(std::make_unique(spv::Op::OpLabel, bound++)).get(); } Id Module::OpBranch(Id target_label) { diff --git a/src/instructions/memory.cpp b/src/instructions/memory.cpp index 884af75..c2cdb52 100644 --- a/src/instructions/memory.cpp +++ b/src/instructions/memory.cpp @@ -16,7 +16,7 @@ Id Module::OpVariable(Id result_type, spv::StorageClass storage_class, Id initia if (initializer) { op->Add(initializer); } - return AddCode(std::move(op)); + return code_store.emplace_back(std::move(op)).get(); } Id Module::OpLoad(Id result_type, Id pointer, std::optional memory_access) { diff --git a/src/sirit.cpp b/src/sirit.cpp index 6e28bdf..2fb8580 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -98,22 +98,24 @@ void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode, execution_modes.push_back(std::move(op)); } -Id Module::Emit(Id op) { - assert(op != nullptr); - code.push_back(op); - return op; +Id Module::AddLabel(Id label) { + assert(label != nullptr); + return code.emplace_back(label); +} + +Id Module::AddLocalVariable(Id variable) { + assert(variable != nullptr); + return code.emplace_back(variable); } Id Module::AddGlobalVariable(Id variable) { assert(variable); - global_variables.push_back(variable); - return variable; + return global_variables.emplace_back(variable); } Id Module::AddCode(std::unique_ptr op) { - const auto id = op.get(); - code_store.push_back(std::move(op)); - return id; + const Id id = code_store.emplace_back(std::move(op)).get(); + return code.emplace_back(id); } Id Module::AddCode(spv::Op opcode, std::optional id) { diff --git a/tests/main.cpp b/tests/main.cpp index e004e59..a2b94d2 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -46,30 +46,30 @@ public: AddGlobalVariable(in_pos); AddGlobalVariable(per_vertex); - const auto main_func = Emit( + const auto main_func = Name(OpFunction(t_void, spv::FunctionControlMask::MaskNone, TypeFunction(t_void)), - "main")); - Emit(OpLabel()); + "main"); + AddLabel(); - const auto ptr_pos_x = Emit(OpAccessChain(in_float, in_pos, Constant(t_uint, 0u))); - const auto ptr_pos_y = Emit(OpAccessChain(in_float, in_pos, Constant(t_uint, 1u))); + const auto ptr_pos_x = OpAccessChain(in_float, in_pos, Constant(t_uint, 0u)); + const auto ptr_pos_y = OpAccessChain(in_float, in_pos, Constant(t_uint, 1u)); - const auto pos_x = Emit(OpLoad(t_float, ptr_pos_x)); - const auto pos_y = Emit(OpLoad(t_float, ptr_pos_y)); + const auto pos_x = OpLoad(t_float, ptr_pos_x); + const auto pos_y = OpLoad(t_float, ptr_pos_y); - auto tmp_position = Emit(OpUndef(float4)); + auto tmp_position = OpUndef(float4); Decorate(tmp_position, spv::Decoration::FPRoundingMode, static_cast(spv::FPRoundingMode::RTE)); - tmp_position = Emit(OpCompositeInsert(float4, pos_x, tmp_position, 0)); - tmp_position = Emit(OpCompositeInsert(float4, pos_y, tmp_position, 1)); - tmp_position = Emit(OpCompositeInsert(float4, Constant(t_float, 0.f), tmp_position, 2)); - tmp_position = Emit(OpCompositeInsert(float4, Constant(t_float, 1.f), tmp_position, 3)); + tmp_position = OpCompositeInsert(float4, pos_x, tmp_position, 0); + tmp_position = OpCompositeInsert(float4, pos_y, tmp_position, 1); + tmp_position = OpCompositeInsert(float4, Constant(t_float, 0.f), tmp_position, 2); + tmp_position = OpCompositeInsert(float4, Constant(t_float, 1.f), tmp_position, 3); - const auto gl_position = Emit(OpAccessChain(out_float4, per_vertex, Constant(t_uint, 0u))); - Emit(OpStore(gl_position, tmp_position)); + const auto gl_position = OpAccessChain(out_float4, per_vertex, Constant(t_uint, 0u)); + OpStore(gl_position, tmp_position); - Emit(OpReturn()); - Emit(OpFunctionEnd()); + OpReturn(); + OpFunctionEnd(); AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", in_pos, per_vertex); }