Alias "const Op*" -> "Ref".

This commit is contained in:
ReinUsesLisp 2018-08-28 04:01:21 -03:00
parent 765c8833c4
commit 954774f9e8
8 changed files with 77 additions and 78 deletions

View File

@ -21,6 +21,8 @@ static const std::uint32_t Undefined = UINT32_MAX;
class Op; class Op;
class Operand; class Operand;
typedef const Op* Ref;
class Module { class Module {
public: public:
explicit Module(); explicit Module();
@ -47,110 +49,109 @@ public:
void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model); void SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryModel memory_model);
/// Adds an entry point. /// Adds an entry point.
void AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_point, void AddEntryPoint(spv::ExecutionModel execution_model, Ref entry_point,
const std::string& name, const std::vector<const Op*>& interfaces = {}); const std::string& name, const std::vector<Ref>& interfaces = {});
/** /**
* Adds an instruction to module's code * Adds an instruction to module's code
* @param op Instruction to insert into code. Types and constants must not be emitted. * @param op Instruction to insert into code. Types and constants must not be emitted.
* @return Returns op. * @return Returns op.
*/ */
const Op* Emit(const Op* op); Ref Emit(Ref op);
// Types // Types
/// Returns type void. /// Returns type void.
const Op* TypeVoid(); Ref TypeVoid();
/// Returns type bool. /// Returns type bool.
const Op* TypeBool(); Ref TypeBool();
/// Returns type integer. /// Returns type integer.
const Op* TypeInt(int width, bool is_signed); Ref TypeInt(int width, bool is_signed);
/// Returns type float. /// Returns type float.
const Op* TypeFloat(int width); Ref TypeFloat(int width);
/// Returns type vector. /// Returns type vector.
const Op* TypeVector(const Op* component_type, int component_count); Ref TypeVector(Ref component_type, int component_count);
/// Returns type matrix. /// Returns type matrix.
const Op* TypeMatrix(const Op* column_type, int column_count); Ref TypeMatrix(Ref column_type, int column_count);
/// Returns type image. /// Returns type image.
const Op* TypeImage(const Op* sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms, Ref TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms,
int sampled, spv::ImageFormat image_format, int sampled, spv::ImageFormat image_format,
spv::AccessQualifier access_qualifier = static_cast<spv::AccessQualifier>(Undefined)); spv::AccessQualifier access_qualifier = static_cast<spv::AccessQualifier>(Undefined));
/// Returns type sampler. /// Returns type sampler.
const Op* TypeSampler(); Ref TypeSampler();
/// Returns type sampled image. /// Returns type sampled image.
const Op* TypeSampledImage(const Op* image_type); Ref TypeSampledImage(Ref image_type);
/// Returns type array. /// Returns type array.
const Op* TypeArray(const Op* element_type, const Op* length); Ref TypeArray(Ref element_type, Ref length);
/// Returns type runtime array. /// Returns type runtime array.
const Op* TypeRuntimeArray(const Op* element_type); Ref TypeRuntimeArray(Ref element_type);
/// Returns type struct. /// Returns type struct.
const Op* TypeStruct(const std::vector<const Op*>& members = {}); Ref TypeStruct(const std::vector<Ref>& members = {});
/// Returns type opaque. /// Returns type opaque.
const Op* TypeOpaque(const std::string& name); Ref TypeOpaque(const std::string& name);
/// Returns type pointer. /// Returns type pointer.
const Op* TypePointer(spv::StorageClass storage_class, const Op* type); Ref TypePointer(spv::StorageClass storage_class, Ref type);
/// Returns type function. /// Returns type function.
const Op* TypeFunction(const Op* return_type, const std::vector<const Op*>& arguments = {}); Ref TypeFunction(Ref return_type, const std::vector<Ref>& arguments = {});
/// Returns type event. /// Returns type event.
const Op* TypeEvent(); Ref TypeEvent();
/// Returns type device event. /// Returns type device event.
const Op* TypeDeviceEvent(); Ref TypeDeviceEvent();
/// Returns type reserve id. /// Returns type reserve id.
const Op* TypeReserveId(); Ref TypeReserveId();
/// Returns type queue. /// Returns type queue.
const Op* TypeQueue(); Ref TypeQueue();
/// Returns type pipe. /// Returns type pipe.
const Op* TypePipe(spv::AccessQualifier access_qualifier); Ref TypePipe(spv::AccessQualifier access_qualifier);
// Constant // Constant
/// Returns a true scalar constant. /// Returns a true scalar constant.
const Op* ConstantTrue(const Op* result_type); Ref ConstantTrue(Ref result_type);
/// Returns a false scalar constant. /// Returns a false scalar constant.
const Op* ConstantFalse(const Op* result_type); Ref ConstantFalse(Ref result_type);
/// Returns a numeric scalar constant. /// Returns a numeric scalar constant.
const Op* Constant(const Op* result_type, Operand* literal); Ref Constant(Ref result_type, Operand* literal);
/// Returns a numeric scalar constant. /// Returns a numeric scalar constant.
const Op* ConstantComposite(const Op* result_type, const std::vector<const Op*>& constituents); Ref ConstantComposite(Ref result_type, const std::vector<Ref>& constituents);
// Function // Function
/// Emits a function. /// Emits a function.
const Op* Function(const Op* result_type, spv::FunctionControlMask function_control, Ref Function(Ref result_type, spv::FunctionControlMask function_control, Ref function_type);
const Op* function_type);
/// Emits a function end. /// Emits a function end.
const Op* FunctionEnd(); Ref FunctionEnd();
// Flow // Flow
/// Emits a label. It starts a block. /// Emits a label. It starts a block.
const Op* Label(); Ref Label();
/// Emits a return. It ends a block. /// Emits a return. It ends a block.
const Op* Return(); Ref Return();
// Literals // Literals
static Operand* Literal(std::uint32_t value); static Operand* Literal(std::uint32_t value);
@ -161,11 +162,11 @@ public:
static Operand* Literal(double value); static Operand* Literal(double value);
private: private:
const Op* AddCode(Op* op); Ref AddCode(Op* op);
const Op* AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX); Ref AddCode(spv::Op opcode, std::uint32_t id = UINT32_MAX);
const Op* AddDeclaration(Op* op); Ref AddDeclaration(Op* op);
std::uint32_t bound{1}; std::uint32_t bound{1};
@ -188,7 +189,7 @@ private:
std::vector<std::unique_ptr<Op>> declarations; std::vector<std::unique_ptr<Op>> declarations;
std::vector<const Op*> code; std::vector<Ref> code;
std::vector<std::unique_ptr<Op>> code_store; std::vector<std::unique_ptr<Op>> code_store;
}; };

View File

@ -11,7 +11,7 @@
namespace Sirit { namespace Sirit {
Op::Op(spv::Op opcode_, u32 id_, const Op* result_type_) Op::Op(spv::Op opcode_, u32 id_, Ref result_type_)
: opcode(opcode_), id(id_), result_type(result_type_) { : opcode(opcode_), id(id_), result_type(result_type_) {
operand_type = OperandType::Op; operand_type = OperandType::Op;
} }
@ -76,8 +76,8 @@ void Op::Add(const std::string& string) {
Add(new LiteralString(string)); Add(new LiteralString(string));
} }
void Op::Add(const std::vector<const Op*>& ids) { void Op::Add(const std::vector<Ref>& ids) {
for (const Op* op : ids) { for (Ref op : ids) {
Add(op); Add(op);
} }
} }

View File

@ -15,7 +15,7 @@ namespace Sirit {
class Op : public Operand { class Op : public Operand {
public: public:
explicit Op(spv::Op opcode, u32 id = UINT32_MAX, const Op* result_type = nullptr); explicit Op(spv::Op opcode, u32 id = UINT32_MAX, Ref result_type = nullptr);
~Op(); ~Op();
virtual void Fetch(Stream& stream) const; virtual void Fetch(Stream& stream) const;
@ -33,14 +33,14 @@ public:
void Add(const std::string& string); void Add(const std::string& string);
void Add(const std::vector<const Op*>& ids); void Add(const std::vector<Ref>& ids);
private: private:
u16 WordCount() const; u16 WordCount() const;
spv::Op opcode; spv::Op opcode;
const Op* result_type; Ref result_type;
u32 id; u32 id;

View File

@ -10,22 +10,21 @@
namespace Sirit { namespace Sirit {
const Op* Module::ConstantTrue(const Op* result_type) { Ref Module::ConstantTrue(Ref result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantTrue, bound, result_type)); return AddDeclaration(new Op(spv::Op::OpConstantTrue, bound, result_type));
} }
const Op* Module::ConstantFalse(const Op* result_type) { Ref Module::ConstantFalse(Ref result_type) {
return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type)); return AddDeclaration(new Op(spv::Op::OpConstantFalse, bound, result_type));
} }
const Op* Module::Constant(const Op* result_type, Operand* literal) { Ref Module::Constant(Ref result_type, Operand* literal) {
Op* op{new Op(spv::Op::OpConstant, bound, result_type)}; Op* op{new Op(spv::Op::OpConstant, bound, result_type)};
op->Add(literal); op->Add(literal);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::ConstantComposite(const Op* result_type, Ref Module::ConstantComposite(Ref result_type, const std::vector<Ref>& constituents) {
const std::vector<const Op*>& constituents) {
Op* op{new Op(spv::Op::OpConstantComposite, bound, result_type)}; Op* op{new Op(spv::Op::OpConstantComposite, bound, result_type)};
op->Add(constituents); op->Add(constituents);
return AddDeclaration(op); return AddDeclaration(op);

View File

@ -9,11 +9,11 @@
namespace Sirit { namespace Sirit {
const Op* Module::Label() { Ref Module::Label() {
return AddCode(spv::Op::OpLabel, bound++); return AddCode(spv::Op::OpLabel, bound++);
} }
const Op* Module::Return() { Ref Module::Return() {
return AddCode(spv::Op::OpReturn); return AddCode(spv::Op::OpReturn);
} }

View File

@ -9,15 +9,14 @@
namespace Sirit { namespace Sirit {
const Op* Module::Function(const Op* result_type, spv::FunctionControlMask function_control, Ref Module::Function(Ref result_type, spv::FunctionControlMask function_control, Ref function_type) {
const Op* function_type) {
Op* op{new Op{spv::Op::OpFunction, bound++, result_type}}; Op* op{new Op{spv::Op::OpFunction, bound++, result_type}};
op->Add(static_cast<u32>(function_control)); op->Add(static_cast<u32>(function_control));
op->Add(function_type); op->Add(function_type);
return AddCode(op); return AddCode(op);
} }
const Op* Module::FunctionEnd() { Ref Module::FunctionEnd() {
return AddCode(spv::Op::OpFunctionEnd); return AddCode(spv::Op::OpFunctionEnd);
} }

View File

@ -10,15 +10,15 @@
namespace Sirit { namespace Sirit {
const Op* Module::TypeVoid() { Ref Module::TypeVoid() {
return AddDeclaration(new Op(spv::Op::OpTypeVoid, bound)); return AddDeclaration(new Op(spv::Op::OpTypeVoid, bound));
} }
const Op* Module::TypeBool() { Ref Module::TypeBool() {
return AddDeclaration(new Op(spv::Op::OpTypeBool, bound)); return AddDeclaration(new Op(spv::Op::OpTypeBool, bound));
} }
const Op* Module::TypeInt(int width, bool is_signed) { Ref Module::TypeInt(int width, bool is_signed) {
if (width == 8) { if (width == 8) {
AddCapability(spv::Capability::Int8); AddCapability(spv::Capability::Int8);
} else if (width == 16) { } else if (width == 16) {
@ -32,7 +32,7 @@ const Op* Module::TypeInt(int width, bool is_signed) {
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeFloat(int width) { Ref Module::TypeFloat(int width) {
if (width == 16) { if (width == 16) {
AddCapability(spv::Capability::Float16); AddCapability(spv::Capability::Float16);
} else if (width == 64) { } else if (width == 64) {
@ -43,7 +43,7 @@ const Op* Module::TypeFloat(int width) {
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeVector(const Op* component_type, int component_count) { Ref Module::TypeVector(Ref component_type, int component_count) {
assert(component_count >= 2); assert(component_count >= 2);
Op* op{new Op(spv::Op::OpTypeVector, bound)}; Op* op{new Op(spv::Op::OpTypeVector, bound)};
op->Add(component_type); op->Add(component_type);
@ -51,7 +51,7 @@ const Op* Module::TypeVector(const Op* component_type, int component_count) {
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeMatrix(const Op* column_type, int column_count) { Ref Module::TypeMatrix(Ref column_type, int column_count) {
assert(column_count >= 2); assert(column_count >= 2);
AddCapability(spv::Capability::Matrix); AddCapability(spv::Capability::Matrix);
Op* op{new Op(spv::Op::OpTypeMatrix, bound)}; Op* op{new Op(spv::Op::OpTypeMatrix, bound)};
@ -60,7 +60,7 @@ const Op* Module::TypeMatrix(const Op* column_type, int column_count) {
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeImage(const Op* sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms, Ref Module::TypeImage(Ref sampled_type, spv::Dim dim, int depth, bool arrayed, bool ms,
int sampled, spv::ImageFormat image_format, int sampled, spv::ImageFormat image_format,
spv::AccessQualifier access_qualifier) { spv::AccessQualifier access_qualifier) {
switch (dim) { switch (dim) {
@ -138,44 +138,44 @@ const Op* Module::TypeImage(const Op* sampled_type, spv::Dim dim, int depth, boo
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeSampler() { Ref Module::TypeSampler() {
return AddDeclaration(new Op(spv::Op::OpTypeSampler, bound)); return AddDeclaration(new Op(spv::Op::OpTypeSampler, bound));
} }
const Op* Module::TypeSampledImage(const Op* image_type) { Ref Module::TypeSampledImage(Ref image_type) {
Op* op{new Op(spv::Op::OpTypeSampledImage, bound)}; Op* op{new Op(spv::Op::OpTypeSampledImage, bound)};
op->Add(image_type); op->Add(image_type);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeArray(const Op* element_type, const Op* length) { Ref Module::TypeArray(Ref element_type, Ref length) {
Op* op{new Op(spv::Op::OpTypeArray, bound)}; Op* op{new Op(spv::Op::OpTypeArray, bound)};
op->Add(element_type); op->Add(element_type);
op->Add(length); op->Add(length);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeRuntimeArray(const Op* element_type) { Ref Module::TypeRuntimeArray(Ref element_type) {
AddCapability(spv::Capability::Shader); AddCapability(spv::Capability::Shader);
Op* op{new Op(spv::Op::OpTypeRuntimeArray, bound)}; Op* op{new Op(spv::Op::OpTypeRuntimeArray, bound)};
op->Add(element_type); op->Add(element_type);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeStruct(const std::vector<const Op*>& members) { Ref Module::TypeStruct(const std::vector<Ref>& members) {
Op* op{new Op(spv::Op::OpTypeStruct, bound)}; Op* op{new Op(spv::Op::OpTypeStruct, bound)};
op->Add(members); op->Add(members);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeOpaque(const std::string& name) { Ref Module::TypeOpaque(const std::string& name) {
AddCapability(spv::Capability::Kernel); AddCapability(spv::Capability::Kernel);
Op* op{new Op(spv::Op::OpTypeOpaque, bound)}; Op* op{new Op(spv::Op::OpTypeOpaque, bound)};
op->Add(name); op->Add(name);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypePointer(spv::StorageClass storage_class, const Op* type) { Ref Module::TypePointer(spv::StorageClass storage_class, Ref type) {
switch (storage_class) { switch (storage_class) {
case spv::StorageClass::Uniform: case spv::StorageClass::Uniform:
case spv::StorageClass::Output: case spv::StorageClass::Output:
@ -197,34 +197,34 @@ const Op* Module::TypePointer(spv::StorageClass storage_class, const Op* type) {
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeFunction(const Op* return_type, const std::vector<const Op*>& arguments) { Ref Module::TypeFunction(Ref return_type, const std::vector<Ref>& arguments) {
Op* op{new Op(spv::Op::OpTypeFunction, bound)}; Op* op{new Op(spv::Op::OpTypeFunction, bound)};
op->Add(return_type); op->Add(return_type);
op->Add(arguments); op->Add(arguments);
return AddDeclaration(op); return AddDeclaration(op);
} }
const Op* Module::TypeEvent() { Ref Module::TypeEvent() {
AddCapability(spv::Capability::Kernel); AddCapability(spv::Capability::Kernel);
return AddDeclaration(new Op(spv::Op::OpTypeEvent, bound)); return AddDeclaration(new Op(spv::Op::OpTypeEvent, bound));
} }
const Op* Module::TypeDeviceEvent() { Ref Module::TypeDeviceEvent() {
AddCapability(spv::Capability::DeviceEnqueue); AddCapability(spv::Capability::DeviceEnqueue);
return AddDeclaration(new Op(spv::Op::OpTypeDeviceEvent, bound)); return AddDeclaration(new Op(spv::Op::OpTypeDeviceEvent, bound));
} }
const Op* Module::TypeReserveId() { Ref Module::TypeReserveId() {
AddCapability(spv::Capability::Pipes); AddCapability(spv::Capability::Pipes);
return AddDeclaration(new Op(spv::Op::OpTypeReserveId, bound)); return AddDeclaration(new Op(spv::Op::OpTypeReserveId, bound));
} }
const Op* Module::TypeQueue() { Ref Module::TypeQueue() {
AddCapability(spv::Capability::DeviceEnqueue); AddCapability(spv::Capability::DeviceEnqueue);
return AddDeclaration(new Op(spv::Op::OpTypeQueue, bound)); return AddDeclaration(new Op(spv::Op::OpTypeQueue, bound));
} }
const Op* Module::TypePipe(spv::AccessQualifier access_qualifier) { Ref Module::TypePipe(spv::AccessQualifier access_qualifier) {
AddCapability(spv::Capability::Pipes); AddCapability(spv::Capability::Pipes);
Op* op{new Op(spv::Op::OpTypePipe, bound)}; Op* op{new Op(spv::Op::OpTypePipe, bound)};
op->Add(static_cast<u32>(access_qualifier)); op->Add(static_cast<u32>(access_qualifier));

View File

@ -73,8 +73,8 @@ void Module::SetMemoryModel(spv::AddressingModel addressing_model, spv::MemoryMo
this->memory_model = memory_model; this->memory_model = memory_model;
} }
void Module::AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_point, void Module::AddEntryPoint(spv::ExecutionModel execution_model, Ref entry_point,
const std::string& name, const std::vector<const Op*>& interfaces) { const std::string& name, const std::vector<Ref>& interfaces) {
Op* op{new Op(spv::Op::OpEntryPoint)}; Op* op{new Op(spv::Op::OpEntryPoint)};
op->Add(static_cast<u32>(execution_model)); op->Add(static_cast<u32>(execution_model));
op->Add(entry_point); op->Add(entry_point);
@ -83,22 +83,22 @@ void Module::AddEntryPoint(spv::ExecutionModel execution_model, const Op* entry_
entry_points.push_back(std::unique_ptr<Op>(op)); entry_points.push_back(std::unique_ptr<Op>(op));
} }
const Op* Module::Emit(const Op* op) { Ref Module::Emit(Ref op) {
assert(op); assert(op);
code.push_back(op); code.push_back(op);
return op; return op;
} }
const Op* Module::AddCode(Op* op) { Ref Module::AddCode(Op* op) {
code_store.push_back(std::unique_ptr<Op>(op)); code_store.push_back(std::unique_ptr<Op>(op));
return op; return op;
} }
const Op* Module::AddCode(spv::Op opcode, u32 id) { Ref Module::AddCode(spv::Op opcode, u32 id) {
return AddCode(new Op{opcode, id}); return AddCode(new Op{opcode, id});
} }
const Op* Module::AddDeclaration(Op* op) { Ref Module::AddDeclaration(Op* op) {
const auto& found{std::find_if(declarations.begin(), declarations.end(), [=](const auto& other) { const auto& found{std::find_if(declarations.begin(), declarations.end(), [=](const auto& other) {
return *other == *op; return *other == *op;
})}; })};