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.
This commit is contained in:
ReinUsesLisp 2019-10-18 04:25:57 -03:00
parent 8cf3d225db
commit 42248408a9
5 changed files with 48 additions and 32 deletions

View file

@ -70,12 +70,26 @@ public:
} }
/** /**
* Adds an instruction to module's code * Adds an existing label to the code
* @param op Instruction to insert into code. Types and constants must not * @param label Label to insert into code.
* be emitted. * @return Returns label.
* @return Returns op.
*/ */
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 * Adds a global variable

View file

@ -30,7 +30,7 @@ Id Module::OpSelectionMerge(Id merge_block, spv::SelectionControlMask selection_
} }
Id Module::OpLabel() { Id Module::OpLabel() {
return AddCode(spv::Op::OpLabel, bound++); return code_store.emplace_back(std::make_unique<Op>(spv::Op::OpLabel, bound++)).get();
} }
Id Module::OpBranch(Id target_label) { Id Module::OpBranch(Id target_label) {

View file

@ -16,7 +16,7 @@ Id Module::OpVariable(Id result_type, spv::StorageClass storage_class, Id initia
if (initializer) { if (initializer) {
op->Add(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<spv::MemoryAccessMask> memory_access) { Id Module::OpLoad(Id result_type, Id pointer, std::optional<spv::MemoryAccessMask> memory_access) {

View file

@ -98,22 +98,24 @@ void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode,
execution_modes.push_back(std::move(op)); execution_modes.push_back(std::move(op));
} }
Id Module::Emit(Id op) { Id Module::AddLabel(Id label) {
assert(op != nullptr); assert(label != nullptr);
code.push_back(op); return code.emplace_back(label);
return op; }
Id Module::AddLocalVariable(Id variable) {
assert(variable != nullptr);
return code.emplace_back(variable);
} }
Id Module::AddGlobalVariable(Id variable) { Id Module::AddGlobalVariable(Id variable) {
assert(variable); assert(variable);
global_variables.push_back(variable); return global_variables.emplace_back(variable);
return variable;
} }
Id Module::AddCode(std::unique_ptr<Op> op) { Id Module::AddCode(std::unique_ptr<Op> op) {
const auto id = op.get(); const Id id = code_store.emplace_back(std::move(op)).get();
code_store.push_back(std::move(op)); return code.emplace_back(id);
return id;
} }
Id Module::AddCode(spv::Op opcode, std::optional<u32> id) { Id Module::AddCode(spv::Op opcode, std::optional<u32> id) {

View file

@ -46,30 +46,30 @@ public:
AddGlobalVariable(in_pos); AddGlobalVariable(in_pos);
AddGlobalVariable(per_vertex); AddGlobalVariable(per_vertex);
const auto main_func = Emit( const auto main_func =
Name(OpFunction(t_void, spv::FunctionControlMask::MaskNone, TypeFunction(t_void)), Name(OpFunction(t_void, spv::FunctionControlMask::MaskNone, TypeFunction(t_void)),
"main")); "main");
Emit(OpLabel()); AddLabel();
const auto ptr_pos_x = Emit(OpAccessChain(in_float, in_pos, Constant(t_uint, 0u))); const auto ptr_pos_x = 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_y = OpAccessChain(in_float, in_pos, Constant(t_uint, 1u));
const auto pos_x = Emit(OpLoad(t_float, ptr_pos_x)); const auto pos_x = OpLoad(t_float, ptr_pos_x);
const auto pos_y = Emit(OpLoad(t_float, ptr_pos_y)); 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, Decorate(tmp_position, spv::Decoration::FPRoundingMode,
static_cast<u32>(spv::FPRoundingMode::RTE)); static_cast<u32>(spv::FPRoundingMode::RTE));
tmp_position = Emit(OpCompositeInsert(float4, pos_x, tmp_position, 0)); tmp_position = OpCompositeInsert(float4, pos_x, tmp_position, 0);
tmp_position = Emit(OpCompositeInsert(float4, pos_y, tmp_position, 1)); tmp_position = OpCompositeInsert(float4, pos_y, tmp_position, 1);
tmp_position = Emit(OpCompositeInsert(float4, Constant(t_float, 0.f), tmp_position, 2)); tmp_position = 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, Constant(t_float, 1.f), tmp_position, 3);
const auto gl_position = Emit(OpAccessChain(out_float4, per_vertex, Constant(t_uint, 0u))); const auto gl_position = OpAccessChain(out_float4, per_vertex, Constant(t_uint, 0u));
Emit(OpStore(gl_position, tmp_position)); OpStore(gl_position, tmp_position);
Emit(OpReturn()); OpReturn();
Emit(OpFunctionEnd()); OpFunctionEnd();
AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", in_pos, per_vertex); AddEntryPoint(spv::ExecutionModel::Vertex, main_func, "main", in_pos, per_vertex);
} }