[spirv-writer] Add Operand and Instruction list aliases.
This CL adds OperandList and InstructionList aliases.
Bug: tint:5
Change-Id: I74263e937ed6007bb44c8d502b122d55af4c7b21
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23622
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index b65802c..586b045 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -63,7 +63,7 @@
namespace spirv {
namespace {
-uint32_t size_of(const std::vector<Instruction>& instructions) {
+uint32_t size_of(const InstructionList& instructions) {
uint32_t size = 0;
for (const auto& inst : instructions)
size += inst.word_length();
@@ -294,8 +294,8 @@
return false;
}
- std::vector<Operand> operands = {Operand::Int(stage), Operand::Int(id),
- Operand::String(name)};
+ OperandList operands = {Operand::Int(stage), Operand::Int(id),
+ Operand::String(name)};
// TODO(dsinclair): This could be made smarter by only listing the
// input/output variables which are used by the entry point instead of just
// listing all module scoped variables of type input/output.
@@ -396,7 +396,7 @@
{Operand::Int(ret_id), func_op, Operand::Int(SpvFunctionControlMaskNone),
Operand::Int(func_type_id)}};
- std::vector<Instruction> params;
+ InstructionList params;
for (const auto& param : func->params()) {
auto param_op = result_op();
auto param_id = param_op.to_i();
@@ -442,7 +442,7 @@
return 0;
}
- std::vector<Operand> ops = {func_op, Operand::Int(ret_id)};
+ OperandList ops = {func_op, Operand::Int(ret_id)};
for (const auto& param : func->params()) {
auto param_type_id = GenerateTypeIfNeeded(param->type());
if (param_type_id == 0) {
@@ -555,8 +555,8 @@
push_debug(spv::Op::OpName,
{Operand::Int(var_id), Operand::String(var->name())});
- std::vector<Operand> ops = {Operand::Int(type_id), result,
- Operand::Int(ConvertStorageClass(sc))};
+ OperandList ops = {Operand::Int(type_id), result,
+ Operand::Int(ConvertStorageClass(sc))};
if (var->has_constructor()) {
ops.push_back(Operand::Int(init_id));
} else {
@@ -728,8 +728,8 @@
auto extract = result_op();
auto extract_id = extract.to_i();
- std::vector<Operand> ops = {Operand::Int(result_type_id), extract,
- Operand::Int(info->source_id)};
+ OperandList ops = {Operand::Int(result_type_id), extract,
+ Operand::Int(info->source_id)};
for (auto id : info->access_chain_indices) {
ops.push_back(Operand::Int(id));
}
@@ -751,8 +751,8 @@
auto result = result_op();
auto result_id = result.to_i();
- std::vector<Operand> ops = {Operand::Int(result_type_id), result,
- Operand::Int(vec_id), Operand::Int(vec_id)};
+ OperandList ops = {Operand::Int(result_type_id), result, Operand::Int(vec_id),
+ Operand::Int(vec_id)};
for (uint32_t i = 0; i < swiz.size(); ++i) {
auto val = IndexFromName(swiz[i]);
@@ -824,8 +824,8 @@
auto result = result_op();
auto result_id = result.to_i();
- std::vector<Operand> ops = {Operand::Int(result_type_id), result,
- Operand::Int(info.source_id)};
+ OperandList ops = {Operand::Int(result_type_id), result,
+ Operand::Int(info.source_id)};
for (auto id : info.access_chain_indices) {
ops.push_back(Operand::Int(id));
}
@@ -952,7 +952,7 @@
std::ostringstream out;
out << "__const";
- std::vector<Operand> ops;
+ OperandList ops;
bool constructor_is_const = true;
for (const auto& e : init->values()) {
if (!e->IsConstructor()) {
@@ -1343,7 +1343,7 @@
auto result_id = result.to_i();
spv::Op op = spv::Op::OpNop;
- std::vector<Operand> ops = {Operand::Int(type_id), result};
+ OperandList ops = {Operand::Int(type_id), result};
// Handle regular function calls
if (!ident->has_path()) {
@@ -1406,7 +1406,7 @@
return 0;
}
- std::vector<Operand> params = {Operand::Int(result_type_id), result};
+ OperandList params = {Operand::Int(result_type_id), result};
for (const auto& p : call->params()) {
auto val_id = GenerateExpression(p.get());
if (val_id == 0) {
@@ -1630,8 +1630,7 @@
auto default_block = result_op();
auto default_block_id = default_block.to_i();
- std::vector<Operand> params = {Operand::Int(cond_id),
- Operand::Int(default_block_id)};
+ OperandList params = {Operand::Int(cond_id), Operand::Int(default_block_id)};
std::vector<uint32_t> case_ids;
for (const auto& item : stmt->body()) {
@@ -1943,7 +1942,7 @@
{Operand::Int(struct_id), Operand::String(struct_type->name())});
}
- std::vector<Operand> ops;
+ OperandList ops;
ops.push_back(result);
if (impl->decoration() == ast::StructDecoration::kBlock) {
diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h
index a111094..4213462 100644
--- a/src/writer/spirv/builder.h
+++ b/src/writer/spirv/builder.h
@@ -123,39 +123,39 @@
/// @param cap the capability to set
void push_capability(uint32_t cap);
/// @returns the capabilities
- const std::vector<Instruction>& capabilities() const { return capabilities_; }
+ const InstructionList& capabilities() const { return capabilities_; }
/// Adds an instruction to the preamble
/// @param op the op to set
/// @param operands the operands for the instruction
- void push_preamble(spv::Op op, const std::vector<Operand>& operands) {
+ void push_preamble(spv::Op op, const OperandList& operands) {
preamble_.push_back(Instruction{op, operands});
}
/// @returns the preamble
- const std::vector<Instruction>& preamble() const { return preamble_; }
+ const InstructionList& preamble() const { return preamble_; }
/// Adds an instruction to the debug
/// @param op the op to set
/// @param operands the operands for the instruction
- void push_debug(spv::Op op, const std::vector<Operand>& operands) {
+ void push_debug(spv::Op op, const OperandList& operands) {
debug_.push_back(Instruction{op, operands});
}
/// @returns the debug instructions
- const std::vector<Instruction>& debug() const { return debug_; }
+ const InstructionList& debug() const { return debug_; }
/// Adds an instruction to the types
/// @param op the op to set
/// @param operands the operands for the instruction
- void push_type(spv::Op op, const std::vector<Operand>& operands) {
+ void push_type(spv::Op op, const OperandList& operands) {
types_.push_back(Instruction{op, operands});
}
/// @returns the type instructions
- const std::vector<Instruction>& types() const { return types_; }
+ const InstructionList& types() const { return types_; }
/// Adds an instruction to the annotations
/// @param op the op to set
/// @param operands the operands for the instruction
- void push_annot(spv::Op op, const std::vector<Operand>& operands) {
+ void push_annot(spv::Op op, const OperandList& operands) {
annotations_.push_back(Instruction{op, operands});
}
/// @returns the annotations
- const std::vector<Instruction>& annots() const { return annotations_; }
+ const InstructionList& annots() const { return annotations_; }
/// Adds a function to the builder
/// @param func the function to add
@@ -168,12 +168,12 @@
/// Pushes an instruction to the current function
/// @param op the operation
/// @param operands the operands
- void push_function_inst(spv::Op op, const std::vector<Operand>& operands) {
+ void push_function_inst(spv::Op op, const OperandList& operands) {
functions_.back().push_inst(op, operands);
}
/// Pushes a variable to the current function
/// @param operands the variable operands
- void push_function_var(const std::vector<Operand>& operands) {
+ void push_function_var(const OperandList& operands) {
functions_.back().push_var(operands);
}
@@ -406,11 +406,11 @@
std::string error_;
uint32_t next_id_ = 1;
uint32_t current_label_id_ = 0;
- std::vector<Instruction> capabilities_;
- std::vector<Instruction> preamble_;
- std::vector<Instruction> debug_;
- std::vector<Instruction> types_;
- std::vector<Instruction> annotations_;
+ InstructionList capabilities_;
+ InstructionList preamble_;
+ InstructionList debug_;
+ InstructionList types_;
+ InstructionList annotations_;
std::vector<Function> functions_;
std::unordered_map<std::string, uint32_t> import_name_to_id_;
diff --git a/src/writer/spirv/function.cc b/src/writer/spirv/function.cc
index b040ff0..01da96c 100644
--- a/src/writer/spirv/function.cc
+++ b/src/writer/spirv/function.cc
@@ -24,7 +24,7 @@
Function::Function(const Instruction& declaration,
const Operand& label_op,
- const std::vector<Instruction>& params)
+ const InstructionList& params)
: declaration_(declaration), label_op_(label_op), params_(params) {}
Function::Function(const Function& other) = default;
diff --git a/src/writer/spirv/function.h b/src/writer/spirv/function.h
index ddc2d0d..8421452 100644
--- a/src/writer/spirv/function.h
+++ b/src/writer/spirv/function.h
@@ -39,7 +39,7 @@
/// @param params the function parameters
Function(const Instruction& declaration,
const Operand& label_op,
- const std::vector<Instruction>& params);
+ const InstructionList& params);
/// Copy constructor
/// @param other the function to copy
Function(const Function& other);
@@ -58,19 +58,19 @@
/// Adds an instruction to the instruction list
/// @param op the op to set
/// @param operands the operands for the instruction
- void push_inst(spv::Op op, const std::vector<Operand>& operands) {
+ void push_inst(spv::Op op, const OperandList& operands) {
instructions_.push_back(Instruction{op, operands});
}
/// @returns the instruction list
- const std::vector<Instruction>& instructions() const { return instructions_; }
+ const InstructionList& instructions() const { return instructions_; }
/// Adds a variable to the variable list
/// @param operands the operands for the variable
- void push_var(const std::vector<Operand>& operands) {
+ void push_var(const OperandList& operands) {
vars_.push_back(Instruction{spv::Op::OpVariable, operands});
}
/// @returns the variable list
- const std::vector<Instruction>& variables() const { return vars_; }
+ const InstructionList& variables() const { return vars_; }
/// @returns the word length of the function
uint32_t word_length() const {
@@ -92,9 +92,9 @@
private:
Instruction declaration_;
Operand label_op_;
- std::vector<Instruction> params_;
- std::vector<Instruction> vars_;
- std::vector<Instruction> instructions_;
+ InstructionList params_;
+ InstructionList vars_;
+ InstructionList instructions_;
};
} // namespace spirv
diff --git a/src/writer/spirv/instruction.cc b/src/writer/spirv/instruction.cc
index 78f6afa..9a1c174 100644
--- a/src/writer/spirv/instruction.cc
+++ b/src/writer/spirv/instruction.cc
@@ -20,7 +20,7 @@
namespace writer {
namespace spirv {
-Instruction::Instruction(spv::Op op, std::vector<Operand> operands)
+Instruction::Instruction(spv::Op op, OperandList operands)
: op_(op), operands_(std::move(operands)) {}
Instruction::Instruction(const Instruction&) = default;
diff --git a/src/writer/spirv/instruction.h b/src/writer/spirv/instruction.h
index c321310..649746c 100644
--- a/src/writer/spirv/instruction.h
+++ b/src/writer/spirv/instruction.h
@@ -30,7 +30,7 @@
/// Constructor
/// @param op the op to generate
/// @param operands the operand values for the instruction
- Instruction(spv::Op op, std::vector<Operand> operands);
+ Instruction(spv::Op op, OperandList operands);
/// Copy Constructor
Instruction(const Instruction&);
~Instruction();
@@ -39,16 +39,19 @@
spv::Op opcode() const { return op_; }
/// @returns the instructions operands
- const std::vector<Operand>& operands() const { return operands_; }
+ const OperandList& operands() const { return operands_; }
/// @returns the number of uint32_t's needed to hold the instruction
uint32_t word_length() const;
private:
spv::Op op_ = spv::Op::OpNop;
- std::vector<Operand> operands_;
+ OperandList operands_;
};
+/// A list of instructions
+using InstructionList = std::vector<Instruction>;
+
} // namespace spirv
} // namespace writer
} // namespace tint
diff --git a/src/writer/spirv/operand.h b/src/writer/spirv/operand.h
index 79ad2eb..a2eafce 100644
--- a/src/writer/spirv/operand.h
+++ b/src/writer/spirv/operand.h
@@ -16,6 +16,7 @@
#define SRC_WRITER_SPIRV_OPERAND_H_
#include <string>
+#include <vector>
namespace tint {
namespace writer {
@@ -88,6 +89,9 @@
std::string str_val_;
};
+/// A list of operands
+using OperandList = std::vector<Operand>;
+
} // namespace spirv
} // namespace writer
} // namespace tint
diff --git a/src/writer/spirv/spv_dump.cc b/src/writer/spirv/spv_dump.cc
index c649acc..6c5cd8f 100644
--- a/src/writer/spirv/spv_dump.cc
+++ b/src/writer/spirv/spv_dump.cc
@@ -75,7 +75,7 @@
return Disassemble(writer.result());
}
-std::string DumpInstructions(const std::vector<Instruction>& insts) {
+std::string DumpInstructions(const InstructionList& insts) {
BinaryWriter writer;
writer.WriteHeader(kDefaultMaxIdBound);
for (const auto& inst : insts) {
diff --git a/src/writer/spirv/spv_dump.h b/src/writer/spirv/spv_dump.h
index c9079c7..deae123 100644
--- a/src/writer/spirv/spv_dump.h
+++ b/src/writer/spirv/spv_dump.h
@@ -38,7 +38,7 @@
/// Dumps the given instructions to a SPIR-V disassembly string
/// @param insts the instructions to dump
/// @returns the instruction as a SPIR-V disassembly string
-std::string DumpInstructions(const std::vector<Instruction>& insts);
+std::string DumpInstructions(const InstructionList& insts);
} // namespace spirv
} // namespace writer