mirror of
https://git.suyu.dev/suyu/sirit.git
synced 2026-01-04 21:56:14 +01:00
Upgrade to C++20 and use std::span
This commit is contained in:
parent
eefca56afd
commit
c4ea8f4b76
18 changed files with 119 additions and 102 deletions
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
namespace Sirit {
|
||||
|
||||
Id Module::Decorate(Id target, spv::Decoration decoration, const std::vector<Literal>& literals) {
|
||||
Id Module::Decorate(Id target, spv::Decoration decoration, std::span<const Literal> literals) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpDecorate)};
|
||||
op->Add(target);
|
||||
op->Add(static_cast<u32>(decoration));
|
||||
|
|
@ -22,7 +22,7 @@ Id Module::Decorate(Id target, spv::Decoration decoration, const std::vector<Lit
|
|||
}
|
||||
|
||||
Id Module::MemberDecorate(Id structure_type, Literal member, spv::Decoration decoration,
|
||||
const std::vector<Literal>& literals) {
|
||||
std::span<const Literal> literals) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpMemberDecorate)};
|
||||
op->Add(structure_type);
|
||||
op->Add(member);
|
||||
|
|
|
|||
|
|
@ -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<Id>& constituents) {
|
||||
Id Module::ConstantComposite(Id result_type, std::span<const Id> constituents) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpConstantComposite, bound, result_type)};
|
||||
op->Add(constituents);
|
||||
return AddDeclaration(std::move(op));
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
namespace Sirit {
|
||||
|
||||
Id Module::OpExtInst(Id result_type, Id set, u32 instruction, const std::vector<Id>& operands) {
|
||||
Id Module::OpExtInst(Id result_type, Id set, u32 instruction, std::span<const Id> operands) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpExtInst, bound++, result_type)};
|
||||
op->Add(set);
|
||||
op->Add(instruction);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
namespace Sirit {
|
||||
|
||||
Id Module::OpLoopMerge(Id merge_block, Id continue_target, spv::LoopControlMask loop_control,
|
||||
const std::vector<Id>& literals) {
|
||||
std::span<const Id> literals) {
|
||||
auto op{std::make_unique<Op>(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<Literal>& literals,
|
||||
const std::vector<Id>& labels) {
|
||||
Id Module::OpSwitch(Id selector, Id default_label, std::span<const Literal> literals,
|
||||
std::span<const Id> labels) {
|
||||
const std::size_t size = literals.size();
|
||||
assert(literals.size() == labels.size());
|
||||
auto op{std::make_unique<Op>(spv::Op::OpSwitch)};
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ Id Module::OpFunctionEnd() {
|
|||
return AddCode(spv::Op::OpFunctionEnd);
|
||||
}
|
||||
|
||||
Id Module::OpFunctionCall(Id result_type, Id function, const std::vector<Id>& arguments) {
|
||||
Id Module::OpFunctionCall(Id result_type, Id function, std::span<const Id> arguments) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpFunctionCall, bound++, result_type)};
|
||||
op->Add(function);
|
||||
op->Add(arguments);
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
namespace Sirit {
|
||||
|
||||
static void AddImageOperands(Op* op, std::optional<spv::ImageOperandsMask> image_operands,
|
||||
const std::vector<Id>& operands) {
|
||||
std::span<const Id> operands) {
|
||||
if (!image_operands)
|
||||
return;
|
||||
op->Add(static_cast<u32>(*image_operands));
|
||||
|
|
@ -21,7 +21,7 @@ static void AddImageOperands(Op* op, std::optional<spv::ImageOperandsMask> image
|
|||
#define DEFINE_IMAGE_OP(opcode) \
|
||||
Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, \
|
||||
std::optional<spv::ImageOperandsMask> image_operands, \
|
||||
const std::vector<Id>& operands) { \
|
||||
std::span<const Id> operands) { \
|
||||
auto op{std::make_unique<Op>(spv::Op::opcode, bound++, result_type)}; \
|
||||
op->Add(sampled_image); \
|
||||
op->Add(coordinate); \
|
||||
|
|
@ -31,7 +31,7 @@ static void AddImageOperands(Op* op, std::optional<spv::ImageOperandsMask> 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<Id>& operands) { \
|
||||
spv::ImageOperandsMask image_operands, std::span<const Id> operands) { \
|
||||
auto op{std::make_unique<Op>(spv::Op::opcode, bound++, result_type)}; \
|
||||
op->Add(sampled_image); \
|
||||
op->Add(coordinate); \
|
||||
|
|
@ -43,7 +43,7 @@ static void AddImageOperands(Op* op, std::optional<spv::ImageOperandsMask> image
|
|||
#define DEFINE_IMAGE_EXTRA_OP(opcode) \
|
||||
Id Module::opcode(Id result_type, Id sampled_image, Id coordinate, Id extra, \
|
||||
std::optional<spv::ImageOperandsMask> image_operands, \
|
||||
const std::vector<Id>& operands) { \
|
||||
std::span<const Id> operands) { \
|
||||
auto op{std::make_unique<Op>(spv::Op::opcode, bound++, result_type)}; \
|
||||
op->Add(sampled_image); \
|
||||
op->Add(coordinate); \
|
||||
|
|
@ -54,7 +54,7 @@ static void AddImageOperands(Op* op, std::optional<spv::ImageOperandsMask> 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<Id>& operands) { \
|
||||
spv::ImageOperandsMask image_operands, std::span<const Id> operands) { \
|
||||
auto op{std::make_unique<Op>(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<spv::ImageOperandsMask> image_operands,
|
||||
const std::vector<Id>& operands) {
|
||||
std::span<const Id> operands) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpImageWrite)};
|
||||
op->Add(image);
|
||||
op->Add(coordinate);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ Id Module::OpStore(Id pointer, Id object, std::optional<spv::MemoryAccessMask> m
|
|||
return AddCode(std::move(op));
|
||||
}
|
||||
|
||||
Id Module::OpAccessChain(Id result_type, Id base, const std::vector<Id>& indexes) {
|
||||
Id Module::OpAccessChain(Id result_type, Id base, std::span<const Id> indexes) {
|
||||
assert(indexes.size() > 0);
|
||||
auto op{std::make_unique<Op>(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<Literal>& indexes) {
|
||||
std::span<const Literal> indexes) {
|
||||
auto op{std::make_unique<Op>(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<Literal>& indexes) {
|
||||
Id Module::OpCompositeExtract(Id result_type, Id composite, std::span<const Literal> indexes) {
|
||||
auto op{std::make_unique<Op>(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<Id>& ids) {
|
||||
Id Module::OpCompositeConstruct(Id result_type, std::span<const Id> ids) {
|
||||
assert(ids.size() >= 1);
|
||||
auto op{std::make_unique<Op>(spv::Op::OpCompositeConstruct, bound++, result_type)};
|
||||
op->Add(ids);
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ Id Module::TypeRuntimeArray(Id element_type) {
|
|||
return AddDeclaration(std::move(op));
|
||||
}
|
||||
|
||||
Id Module::TypeStruct(const std::vector<Id>& members) {
|
||||
Id Module::TypeStruct(std::span<const Id> members) {
|
||||
auto op{std::make_unique<Op>(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<Id>& arguments) {
|
||||
Id Module::TypeFunction(Id return_type, std::span<const Id> arguments) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpTypeFunction, bound)};
|
||||
op->Add(return_type);
|
||||
op->Add(arguments);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 <typename T>
|
||||
static std::unique_ptr<LiteralNumber> Create(T value) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<Literal>& literals) {
|
||||
void Op::Add(std::span<const Literal> literals) {
|
||||
for (const auto& literal : literals) {
|
||||
Add(literal);
|
||||
}
|
||||
|
|
@ -112,7 +112,7 @@ void Op::Add(std::string string) {
|
|||
Sink(std::make_unique<LiteralString>(std::move(string)));
|
||||
}
|
||||
|
||||
void Op::Add(const std::vector<Id>& ids) {
|
||||
void Op::Add(std::span<const Id> ids) {
|
||||
assert(std::all_of(ids.begin(), ids.end(), [](auto id_) { return id_; }));
|
||||
operands.insert(operands.end(), ids.begin(), ids.end());
|
||||
}
|
||||
|
|
|
|||
6
src/op.h
6
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<Literal>& literals);
|
||||
void Add(std::span<const Literal> literals);
|
||||
|
||||
void Add(const Operand* operand);
|
||||
|
||||
|
|
@ -44,7 +44,7 @@ public:
|
|||
|
||||
void Add(std::string string);
|
||||
|
||||
void Add(const std::vector<Id>& ids);
|
||||
void Add(std::span<const Id> ids);
|
||||
|
||||
private:
|
||||
u16 CalculateTotalWords() const noexcept;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<Id>& interfaces) {
|
||||
void Module::AddEntryPoint(spv::ExecutionModel execution_model, Id entry_point, std::string name,
|
||||
std::span<const Id> interfaces) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpEntryPoint)};
|
||||
op->Add(static_cast<u32>(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<Literal>& literals) {
|
||||
std::span<const Literal> literals) {
|
||||
auto op{std::make_unique<Op>(spv::Op::OpExecutionMode)};
|
||||
op->Add(entry_point);
|
||||
op->Add(static_cast<u32>(mode));
|
||||
|
|
@ -123,14 +124,14 @@ Id Module::AddCode(spv::Op opcode, std::optional<u32> id) {
|
|||
}
|
||||
|
||||
Id Module::AddDeclaration(std::unique_ptr<Op> 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue