From c4ea8f4b76d416db1a53819390bc9581ae038116 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 29 Jul 2020 05:46:50 -0300 Subject: [PATCH] Upgrade to C++20 and use std::span --- CMakeLists.txt | 2 +- include/sirit/sirit.h | 136 ++++++++++++++++++-------------- src/instructions/annotation.cpp | 4 +- src/instructions/constant.cpp | 2 +- src/instructions/extension.cpp | 2 +- src/instructions/flow.cpp | 6 +- src/instructions/function.cpp | 2 +- src/instructions/image.cpp | 12 +-- src/instructions/memory.cpp | 8 +- src/instructions/type.cpp | 4 +- src/literal_number.cpp | 2 +- src/literal_number.h | 2 +- src/literal_string.cpp | 2 +- src/literal_string.h | 2 +- src/op.cpp | 8 +- src/op.h | 6 +- src/operand.h | 6 +- src/sirit.cpp | 15 ++-- 18 files changed, 119 insertions(+), 102 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 047041f..3b0574f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ if (NOT CMAKE_BUILD_TYPE) endif() # Set hard requirements for C++ -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) diff --git a/include/sirit/sirit.h b/include/sirit/sirit.h index 5d90b33..3d47d93 100644 --- a/include/sirit/sirit.h +++ b/include/sirit/sirit.h @@ -6,13 +6,16 @@ #pragma once +#include #include #include #include +#include #include #include #include #include + #include namespace Sirit { @@ -50,23 +53,25 @@ public: /// Adds an entry point. void AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, std::string name, - const std::vector& interfaces = {}); + std::span interfaces = {}); /// Adds an entry point. template void AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, std::string name, Ts&&... interfaces) { - AddEntryPoint(execution_model, std::move(entry_point), name, {interfaces...}); + AddEntryPoint(execution_model, std::move(entry_point), name, + std::span{std::array{interfaces...}}); } /// Declare an execution mode for an entry point. void AddExecutionMode(Id entry_point, spv::ExecutionMode mode, - const std::vector& literals = {}); + std::span literals = {}); /// Declare an execution mode for an entry point. template void AddExecutionMode(Id entry_point, spv::ExecutionMode mode, Ts&&... literals) { - AddExecutionMode(entry_point, mode, {literals...}); + const Literal stack_literals[] = {std::forward(literals)...}; + AddExecutionMode(entry_point, mode, std::span{stack_literals}); } /** @@ -136,12 +141,12 @@ public: Id TypeRuntimeArray(Id element_type); /// Returns type struct. - Id TypeStruct(const std::vector& members = {}); + Id TypeStruct(std::span members = {}); /// Returns type struct. template Id TypeStruct(Ts&&... members) { - return TypeStruct({members...}); + return TypeStruct(std::span{std::array{members...}}); } /// Returns type opaque. @@ -151,12 +156,12 @@ public: Id TypePointer(spv::StorageClass storage_class, Id type); /// Returns type function. - Id TypeFunction(Id return_type, const std::vector& arguments = {}); + Id TypeFunction(Id return_type, std::span arguments = {}); /// Returns type function. template Id TypeFunction(Id return_type, Ts&&... arguments) { - return OpTypeFunction(return_type, {arguments...}); + return TypeFunction(return_type, std::span{std::array{arguments...}}); } /// Returns type event. @@ -186,12 +191,12 @@ public: Id Constant(Id result_type, const Literal& literal); /// Returns a numeric scalar constant. - Id ConstantComposite(Id result_type, const std::vector& constituents); + Id ConstantComposite(Id result_type, std::span constituents); /// Returns a numeric scalar constant. template Id ConstantComposite(Id result_type, Ts&&... constituents) { - return ConstantComposite(result_type, {constituents...}); + return ConstantComposite(result_type, std::span{std::array{constituents...}}); } /// Returns a sampler constant. @@ -210,25 +215,26 @@ public: Id OpFunctionEnd(); /// Call a function. - Id OpFunctionCall(Id result_type, Id function, const std::vector& arguments = {}); + Id OpFunctionCall(Id result_type, Id function, std::span arguments = {}); /// Call a function. template Id OpFunctionCall(Id result_type, Id function, Ts&&... arguments) { - return OpFunctionCall(result_type, function, {arguments...}); + return OpFunctionCall(result_type, function, std::span{std::array{arguments...}}); } // Flow /// Declare a structured loop. Id OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control, - const std::vector& literals = {}); + std::span literals = {}); /// Declare a structured loop. template Id OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control, Ts&&... literals) { - return OpLoopMerge(merge_block, continue_target, loop_control, {literals...}); + return OpLoopMerge(merge_block, continue_target, loop_control, + std::span{std::array{literals...}}); } /// Declare a structured selection. @@ -251,8 +257,8 @@ public: std::uint32_t true_weight = 0, std::uint32_t false_weight = 0); /// Multi-way branch to one of the operand label. - Id OpSwitch(Id selector, Id default_label, const std::vector& literals, - const std::vector& labels); + Id OpSwitch(Id selector, Id default_label, std::span literals, + std::span labels); /// Returns with no value from a function with void return type. Id OpReturn(); @@ -284,7 +290,8 @@ public: /// Allocate an object in memory, resulting in a copy to it. Id OpVariable(Id result_type, spv::StorageClass storage_class, Id initializer = nullptr); - /// Form a pointer to a texel of an image. Use of such a pointer is limited to atomic operations. + /// Form a pointer to a texel of an image. Use of such a pointer is limited to atomic + /// operations. Id OpImageTexelPointer(Id result_type, Id image, Id coordinate, Id sample); /// Load through a pointer. @@ -294,12 +301,12 @@ public: Id OpStore(Id pointer, Id object, std::optional memory_access = {}); /// Create a pointer into a composite object that can be used with OpLoad and OpStore. - Id OpAccessChain(Id result_type, Id base, const std::vector& indexes = {}); + Id OpAccessChain(Id result_type, Id base, std::span indexes = {}); /// Create a pointer into a composite object that can be used with OpLoad and OpStore. template Id OpAccessChain(Id result_type, Id base, Ts&&... indexes) { - return OpAccessChain(result_type, base, {indexes...}); + return OpAccessChain(result_type, base, std::span{std::array{indexes...}}); } /// Extract a single, dynamically selected, component of a vector. @@ -310,50 +317,56 @@ public: /// Make a copy of a composite object, while modifying one part of it. Id OpCompositeInsert(Id result_type, Id object, Id composite, - const std::vector& indexes = {}); + std::span indexes = {}); /// Make a copy of a composite object, while modifying one part of it. template Id OpCompositeInsert(Id result_type, Id object, Id composite, Ts&&... indexes) { - return OpCompositeInsert(result_type, object, composite, {indexes...}); + const Literal stack_indexes[] = {std::forward(indexes)...}; + return OpCompositeInsert(result_type, object, composite, + std::span{stack_indexes}); } /// Extract a part of a composite object. - Id OpCompositeExtract(Id result_type, Id composite, const std::vector& indexes = {}); + Id OpCompositeExtract(Id result_type, Id composite, std::span indexes = {}); /// Extract a part of a composite object. template Id OpCompositeExtract(Id result_type, Id composite, Ts&&... indexes) { - return OpCompositeExtract(result_type, composite, {indexes...}); + const Literal stack_indexes[] = {std::forward(indexes)...}; + return OpCompositeExtract(result_type, composite, std::span{stack_indexes}); } /// Construct a new composite object from a set of constituent objects that will fully form it. - Id OpCompositeConstruct(Id result_type, const std::vector& ids); + Id OpCompositeConstruct(Id result_type, std::span ids); /// Construct a new composite object from a set of constituent objects that will fully form it. template Id OpCompositeConstruct(Id result_type, Ts&&... ids) { - return OpCompositeConstruct(result_type, {ids...}); + return OpCompositeConstruct(result_type, std::span{std::array{ids...}}); } // Annotation /// Add a decoration to target. - Id Decorate(Id target, spv::Decoration decoration, const std::vector& literals = {}); + Id Decorate(Id target, spv::Decoration decoration, std::span literals = {}); /// Add a decoration to target. template Id Decorate(Id target, spv::Decoration decoration, Ts&&... literals) { - return Decorate(target, decoration, {literals...}); + const Literal stack_literals[] = {std::forward(literals)...}; + return Decorate(target, decoration, std::span{stack_literals}); } Id MemberDecorate(Id structure_type, Literal member, spv::Decoration decoration, - const std::vector& literals = {}); + std::span literals = {}); template Id MemberDecorate(Id structure_type, Literal member, spv::Decoration decoration, Ts&&... literals) { - return MemberDecorate(structure_type, member, decoration, {literals...}); + const Literal stack_literals[] = {std::forward(literals)...}; + return MemberDecorate(structure_type, member, decoration, + std::span{stack_literals}); } // Misc @@ -608,13 +621,13 @@ public: // Extensions /// Execute an instruction in an imported set of extended instructions. - Id OpExtInst(Id result_type, Id set, std::uint32_t instruction, - const std::vector& operands); + Id OpExtInst(Id result_type, Id set, std::uint32_t instruction, std::span operands); /// Execute an instruction in an imported set of extended instructions. template Id OpExtInst(Id result_type, Id set, std::uint32_t instruction, Ts&&... operands) { - return OpExtInst(result_type, set, instruction, {operands...}); + return OpExtInst(result_type, set, instruction, + std::span{std::array{operands...}}); } /// Result is x if x >= 0; otherwise result is -x. @@ -764,86 +777,88 @@ public: /// Sample an image with an implicit level of detail. Id OpImageSampleImplicitLod(Id result_type, Id sampled_image, Id coordinate, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image with an implicit level of detail. template Id OpImageSampleImplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleImplicitLod(result_type, sampled_image, coordinate, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Sample an image using an explicit level of detail. Id OpImageSampleExplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image using an explicit level of detail. template Id OpImageSampleExplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleExplicitLod(result_type, sampled_image, coordinate, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Sample an image doing depth-comparison with an implicit level of detail. Id OpImageSampleDrefImplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image doing depth-comparison with an implicit level of detail. template Id OpImageSampleDrefImplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleDrefImplicitLod(result_type, sampled_image, coordinate, dref, - image_operands, {operands...}); + image_operands, + std::span{std::array{operands...}}); } /// Sample an image doing depth-comparison using an explicit level of detail. Id OpImageSampleDrefExplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image doing depth-comparison using an explicit level of detail. template Id OpImageSampleDrefExplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleDrefExplicitLod(result_type, sampled_image, coordinate, dref, - image_operands, {operands...}); + image_operands, + std::span{std::array{operands...}}); } /// Sample an image with with a project coordinate and an implicit level of detail. Id OpImageSampleProjImplicitLod(Id result_type, Id sampled_image, Id coordinate, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image with with a project coordinate and an implicit level of detail. template Id OpImageSampleProjImplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleProjImplicitLod(result_type, sampled_image, coordinate, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Sample an image with a project coordinate using an explicit level of detail. Id OpImageSampleProjExplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image with a project coordinate using an explicit level of detail. template Id OpImageSampleProjExplicitLod(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleProjExplicitLod(result_type, sampled_image, coordinate, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Sample an image with a project coordinate, doing depth-comparison, with an implicit level of /// detail. Id OpImageSampleProjDrefImplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image with a project coordinate, doing depth-comparison, with an implicit level of /// detail. @@ -851,14 +866,15 @@ public: Id OpImageSampleProjDrefImplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleProjDrefImplicitLod(result_type, sampled_image, coordinate, dref, - image_operands, {operands...}); + image_operands, + std::span{std::array{operands...}}); } /// Sample an image with a project coordinate, doing depth-comparison, using an explicit level /// of detail. Id OpImageSampleProjDrefExplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, - const std::vector& operands = {}); + std::span operands = {}); /// Sample an image with a project coordinate, doing depth-comparison, using an explicit level /// of detail. @@ -866,69 +882,73 @@ public: Id OpImageSampleProjDrefExplicitLod(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageSampleProjDrefExplicitLod(result_type, sampled_image, coordinate, dref, - image_operands, {operands...}); + image_operands, + std::span{std::array{operands...}}); } /// Fetch a single texel from an image whose Sampled operand is 1. Id OpImageFetch(Id result_type, Id sampled_image, Id coordinate, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Fetch a single texel from an image whose Sampled operand is 1. template Id OpImageFetch(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { - return OpImageFetch(result_type, sampled_image, coordinate, image_operands, {operands...}); + return OpImageFetch(result_type, sampled_image, coordinate, image_operands, + std::span{std::array{operands...}}); } /// Gathers the requested component from four texels. Id OpImageGather(Id result_type, Id sampled_image, Id coordinate, Id component, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Gathers the requested component from four texels. template Id OpImageGather(Id result_type, Id sampled_image, Id coordinate, Id component, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageGather(result_type, sampled_image, coordinate, component, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Gathers the requested depth-comparison from four texels. Id OpImageDrefGather(Id result_type, Id sampled_image, Id coordinate, Id dref, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Gathers the requested depth-comparison from four texels. template Id OpImageDrefGather(Id result_type, Id sampled_image, Id coordinate, Id dref, spv::ImageOperandsMask image_operands, Ts&&... operands) { return OpImageDrefGather(result_type, sampled_image, coordinate, dref, image_operands, - {operands...}); + std::span{std::array{operands...}}); } /// Read a texel from an image without a sampler. Id OpImageRead(Id result_type, Id sampled_image, Id coordinate, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Read a texel from an image without a sampler. template Id OpImageRead(Id result_type, Id sampled_image, Id coordinate, spv::ImageOperandsMask image_operands, Ts&&... operands) { - return OpImageRead(result_type, sampled_image, coordinate, image_operands, {operands...}); + return OpImageRead(result_type, sampled_image, coordinate, image_operands, + std::span{std::array{operands...}}); } /// Write a texel to an image without a sampler. Id OpImageWrite(Id image, Id coordinate, Id texel, std::optional image_operands = {}, - const std::vector& operands = {}); + std::span operands = {}); /// Write a texel to an image without a sampler. template Id OpImageWrite(Id image, Id coordinate, Id texel, spv::ImageOperandsMask image_operands, Ts&&... operands) { - return OpImageWrite(image, coordinate, texel, image_operands, {operands...}); + return OpImageWrite(image, coordinate, texel, image_operands, + std::span{std::array{operands...}}); } /// Extract the image from a sampled image. diff --git a/src/instructions/annotation.cpp b/src/instructions/annotation.cpp index da79684..cbe4343 100644 --- a/src/instructions/annotation.cpp +++ b/src/instructions/annotation.cpp @@ -12,7 +12,7 @@ namespace Sirit { -Id Module::Decorate(Id target, spv::Decoration decoration, const std::vector& literals) { +Id Module::Decorate(Id target, spv::Decoration decoration, std::span literals) { auto op{std::make_unique(spv::Op::OpDecorate)}; op->Add(target); op->Add(static_cast(decoration)); @@ -22,7 +22,7 @@ Id Module::Decorate(Id target, spv::Decoration decoration, const std::vector& literals) { + std::span literals) { auto op{std::make_unique(spv::Op::OpMemberDecorate)}; op->Add(structure_type); op->Add(member); diff --git a/src/instructions/constant.cpp b/src/instructions/constant.cpp index 1d50327..25b0fa5 100644 --- a/src/instructions/constant.cpp +++ b/src/instructions/constant.cpp @@ -24,7 +24,7 @@ Id Module::Constant(Id result_type, const Literal& literal) { return AddDeclaration(std::move(op)); } -Id Module::ConstantComposite(Id result_type, const std::vector& constituents) { +Id Module::ConstantComposite(Id result_type, std::span constituents) { auto op{std::make_unique(spv::Op::OpConstantComposite, bound, result_type)}; op->Add(constituents); return AddDeclaration(std::move(op)); diff --git a/src/instructions/extension.cpp b/src/instructions/extension.cpp index a806347..332d972 100644 --- a/src/instructions/extension.cpp +++ b/src/instructions/extension.cpp @@ -12,7 +12,7 @@ namespace Sirit { -Id Module::OpExtInst(Id result_type, Id set, u32 instruction, const std::vector& operands) { +Id Module::OpExtInst(Id result_type, Id set, u32 instruction, std::span operands) { auto op{std::make_unique(spv::Op::OpExtInst, bound++, result_type)}; op->Add(set); op->Add(instruction); diff --git a/src/instructions/flow.cpp b/src/instructions/flow.cpp index ccec4bf..2520e6c 100644 --- a/src/instructions/flow.cpp +++ b/src/instructions/flow.cpp @@ -13,7 +13,7 @@ namespace Sirit { Id Module::OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control, - const std::vector& literals) { + std::span literals) { auto op{std::make_unique(spv::Op::OpLoopMerge)}; op->Add(merge_block); op->Add(continue_target); @@ -52,8 +52,8 @@ Id Module::OpBranchConditional(Id condition, Id true_label, Id false_label, u32 return AddCode(std::move(op)); } -Id Module::OpSwitch(Id selector, Id default_label, const std::vector& literals, - const std::vector& labels) { +Id Module::OpSwitch(Id selector, Id default_label, std::span literals, + std::span labels) { const std::size_t size = literals.size(); assert(literals.size() == labels.size()); auto op{std::make_unique(spv::Op::OpSwitch)}; diff --git a/src/instructions/function.cpp b/src/instructions/function.cpp index 3fb1126..f5982e8 100644 --- a/src/instructions/function.cpp +++ b/src/instructions/function.cpp @@ -21,7 +21,7 @@ Id Module::OpFunctionEnd() { return AddCode(spv::Op::OpFunctionEnd); } -Id Module::OpFunctionCall(Id result_type, Id function, const std::vector& arguments) { +Id Module::OpFunctionCall(Id result_type, Id function, std::span arguments) { auto op{std::make_unique(spv::Op::OpFunctionCall, bound++, result_type)}; op->Add(function); op->Add(arguments); diff --git a/src/instructions/image.cpp b/src/instructions/image.cpp index d12dc2c..7db8f1c 100644 --- a/src/instructions/image.cpp +++ b/src/instructions/image.cpp @@ -11,7 +11,7 @@ namespace Sirit { static void AddImageOperands(Op* op, std::optional image_operands, - const std::vector& operands) { + std::span operands) { if (!image_operands) return; op->Add(static_cast(*image_operands)); @@ -21,7 +21,7 @@ static void AddImageOperands(Op* op, std::optional image #define DEFINE_IMAGE_OP(opcode) \ Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, \ std::optional image_operands, \ - const std::vector& operands) { \ + std::span operands) { \ auto op{std::make_unique(spv::Op::opcode, bound++, result_type)}; \ op->Add(sampled_image); \ op->Add(coordinate); \ @@ -31,7 +31,7 @@ static void AddImageOperands(Op* op, std::optional image #define DEFINE_IMAGE_EXP_OP(opcode) \ Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, \ - spv::ImageOperandsMask image_operands, const std::vector& operands) { \ + spv::ImageOperandsMask image_operands, std::span operands) { \ auto op{std::make_unique(spv::Op::opcode, bound++, result_type)}; \ op->Add(sampled_image); \ op->Add(coordinate); \ @@ -43,7 +43,7 @@ static void AddImageOperands(Op* op, std::optional image #define DEFINE_IMAGE_EXTRA_OP(opcode) \ Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, Id extra, \ std::optional image_operands, \ - const std::vector& operands) { \ + std::span operands) { \ auto op{std::make_unique(spv::Op::opcode, bound++, result_type)}; \ op->Add(sampled_image); \ op->Add(coordinate); \ @@ -54,7 +54,7 @@ static void AddImageOperands(Op* op, std::optional image #define DEFINE_IMAGE_EXTRA_EXP_OP(opcode) \ Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, Id extra, \ - spv::ImageOperandsMask image_operands, const std::vector& operands) { \ + spv::ImageOperandsMask image_operands, std::span operands) { \ auto op{std::make_unique(spv::Op::opcode, bound++, result_type)}; \ op->Add(sampled_image); \ op->Add(coordinate); \ @@ -106,7 +106,7 @@ Id Module::OpSampledImage(Id result_type, Id image, Id sampler) { Id Module::OpImageWrite(Id image, Id coordinate, Id texel, std::optional image_operands, - const std::vector& operands) { + std::span operands) { auto op{std::make_unique(spv::Op::OpImageWrite)}; op->Add(image); op->Add(coordinate); diff --git a/src/instructions/memory.cpp b/src/instructions/memory.cpp index 33c8b07..8b3a159 100644 --- a/src/instructions/memory.cpp +++ b/src/instructions/memory.cpp @@ -46,7 +46,7 @@ Id Module::OpStore(Id pointer, Id object, std::optional m return AddCode(std::move(op)); } -Id Module::OpAccessChain(Id result_type, Id base, const std::vector& indexes) { +Id Module::OpAccessChain(Id result_type, Id base, std::span indexes) { assert(indexes.size() > 0); auto op{std::make_unique(spv::Op::OpAccessChain, bound++, result_type)}; op->Add(base); @@ -70,7 +70,7 @@ Id Module::OpVectorInsertDynamic(Id result_type, Id vector, Id component, Id ind } Id Module::OpCompositeInsert(Id result_type, Id object, Id composite, - const std::vector& indexes) { + std::span indexes) { auto op{std::make_unique(spv::Op::OpCompositeInsert, bound++, result_type)}; op->Add(object); op->Add(composite); @@ -78,14 +78,14 @@ Id Module::OpCompositeInsert(Id result_type, Id object, Id composite, return AddCode(std::move(op)); } -Id Module::OpCompositeExtract(Id result_type, Id composite, const std::vector& indexes) { +Id Module::OpCompositeExtract(Id result_type, Id composite, std::span indexes) { auto op{std::make_unique(spv::Op::OpCompositeExtract, bound++, result_type)}; op->Add(composite); op->Add(indexes); return AddCode(std::move(op)); } -Id Module::OpCompositeConstruct(Id result_type, const std::vector& ids) { +Id Module::OpCompositeConstruct(Id result_type, std::span ids) { assert(ids.size() >= 1); auto op{std::make_unique(spv::Op::OpCompositeConstruct, bound++, result_type)}; op->Add(ids); diff --git a/src/instructions/type.cpp b/src/instructions/type.cpp index 2f2b15a..e302cd2 100644 --- a/src/instructions/type.cpp +++ b/src/instructions/type.cpp @@ -90,7 +90,7 @@ Id Module::TypeRuntimeArray(Id element_type) { return AddDeclaration(std::move(op)); } -Id Module::TypeStruct(const std::vector& members) { +Id Module::TypeStruct(std::span members) { auto op{std::make_unique(spv::Op::OpTypeStruct, bound)}; op->Add(members); return AddDeclaration(std::move(op)); @@ -109,7 +109,7 @@ Id Module::TypePointer(spv::StorageClass storage_class, Id type) { return AddDeclaration(std::move(op)); } -Id Module::TypeFunction(Id return_type, const std::vector& arguments) { +Id Module::TypeFunction(Id return_type, std::span arguments) { auto op{std::make_unique(spv::Op::OpTypeFunction, bound)}; op->Add(return_type); op->Add(arguments); diff --git a/src/literal_number.cpp b/src/literal_number.cpp index 3dd8668..a4c436e 100644 --- a/src/literal_number.cpp +++ b/src/literal_number.cpp @@ -26,7 +26,7 @@ std::size_t LiteralNumber::GetWordCount() const noexcept { return is_32 ? 1 : 2; } -bool LiteralNumber::operator==(const Operand& other) const noexcept { +bool LiteralNumber::Equal(const Operand& other) const noexcept { if (!EqualType(other)) { return false; } diff --git a/src/literal_number.h b/src/literal_number.h index 6fd60d1..46d19b0 100644 --- a/src/literal_number.h +++ b/src/literal_number.h @@ -24,7 +24,7 @@ public: std::size_t GetWordCount() const noexcept override; - bool operator==(const Operand& other) const noexcept override; + bool Equal(const Operand& other) const noexcept override; template static std::unique_ptr Create(T value) { diff --git a/src/literal_string.cpp b/src/literal_string.cpp index b201228..58aa40f 100644 --- a/src/literal_string.cpp +++ b/src/literal_string.cpp @@ -23,7 +23,7 @@ std::size_t LiteralString::GetWordCount() const noexcept { return string.size() / 4 + 1; } -bool LiteralString::operator==(const Operand& other) const noexcept { +bool LiteralString::Equal(const Operand& other) const noexcept { if (!EqualType(other)) { return false; } diff --git a/src/literal_string.h b/src/literal_string.h index 7c051d1..608ac53 100644 --- a/src/literal_string.h +++ b/src/literal_string.h @@ -21,7 +21,7 @@ public: std::size_t GetWordCount() const noexcept override; - bool operator==(const Operand& other) const noexcept override; + bool Equal(const Operand& other) const noexcept override; private: std::string string; diff --git a/src/op.cpp b/src/op.cpp index a735519..d8d7ade 100644 --- a/src/op.cpp +++ b/src/op.cpp @@ -30,7 +30,7 @@ std::size_t Op::GetWordCount() const noexcept { return 1; } -bool Op::operator==(const Operand& other) const noexcept { +bool Op::Equal(const Operand& other) const noexcept { if (!EqualType(other)) { return false; } @@ -38,7 +38,7 @@ bool Op::operator==(const Operand& other) const noexcept { if (op.opcode == opcode && result_type == op.result_type && operands.size() == op.operands.size()) { for (std::size_t i = 0; i < operands.size(); i++) { - if (*operands[i] != *op.operands[i]) { + if (!operands[i]->Equal(*op.operands[i])) { return false; } } @@ -89,7 +89,7 @@ void Op::Add(const Literal& literal) { }()); } -void Op::Add(const std::vector& literals) { +void Op::Add(std::span literals) { for (const auto& literal : literals) { Add(literal); } @@ -112,7 +112,7 @@ void Op::Add(std::string string) { Sink(std::make_unique(std::move(string))); } -void Op::Add(const std::vector& ids) { +void Op::Add(std::span ids) { assert(std::all_of(ids.begin(), ids.end(), [](auto id_) { return id_; })); operands.insert(operands.end(), ids.begin(), ids.end()); } diff --git a/src/op.h b/src/op.h index cc16ac3..276bfc9 100644 --- a/src/op.h +++ b/src/op.h @@ -26,7 +26,7 @@ public: std::size_t GetWordCount() const noexcept override; - bool operator==(const Operand& other) const noexcept override; + bool Equal(const Operand& other) const noexcept override; void Write(Stream& stream) const; @@ -34,7 +34,7 @@ public: void Add(const Literal& literal); - void Add(const std::vector& literals); + void Add(std::span literals); void Add(const Operand* operand); @@ -44,7 +44,7 @@ public: void Add(std::string string); - void Add(const std::vector& ids); + void Add(std::span ids); private: u16 CalculateTotalWords() const noexcept; diff --git a/src/operand.h b/src/operand.h index 4e19f80..90b11f1 100644 --- a/src/operand.h +++ b/src/operand.h @@ -23,11 +23,7 @@ public: virtual std::size_t GetWordCount() const noexcept = 0; - virtual bool operator==(const Operand& other) const noexcept = 0; - - bool operator!=(const Operand& other) const noexcept { - return !operator==(other); - } + virtual bool Equal(const Operand& other) const noexcept = 0; bool EqualType(const Operand& other) const noexcept { return operand_type == other.operand_type; diff --git a/src/sirit.cpp b/src/sirit.cpp index 473830c..c95634a 100644 --- a/src/sirit.cpp +++ b/src/sirit.cpp @@ -74,13 +74,14 @@ void Module::AddCapability(spv::Capability capability) { capabilities.insert(capability); } -void Module::SetMemoryModel(spv::AddressingModel addressing_model_, spv::MemoryModel memory_model_) { +void Module::SetMemoryModel(spv::AddressingModel addressing_model_, + spv::MemoryModel memory_model_) { this->addressing_model = addressing_model_; this->memory_model = memory_model_; } -void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, - std::string name, const std::vector& interfaces) { +void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, std::string name, + std::span interfaces) { auto op{std::make_unique(spv::Op::OpEntryPoint)}; op->Add(static_cast(execution_model)); op->Add(entry_point); @@ -90,7 +91,7 @@ void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, } void Module::AddExecutionMode(Id entry_point, spv::ExecutionMode mode, - const std::vector& literals) { + std::span literals) { auto op{std::make_unique(spv::Op::OpExecutionMode)}; op->Add(entry_point); op->Add(static_cast(mode)); @@ -123,14 +124,14 @@ Id Module::AddCode(spv::Op opcode, std::optional id) { } Id Module::AddDeclaration(std::unique_ptr op) { - const auto& found{std::find_if(declarations.begin(), declarations.end(), - [&op](const auto& other) { return *other == *op; })}; + const auto found = std::find_if(declarations.begin(), declarations.end(), + [&op](const auto& other) { return op->Equal(*other); }); if (found != declarations.end()) { return found->get(); } const auto id = op.get(); declarations.push_back(std::move(op)); - bound++; + ++bound; return id; }