[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>
This commit is contained in:
dan sinclair 2020-06-22 20:18:26 +00:00 committed by dan sinclair
parent 011aed9b82
commit e87ba1fd37
9 changed files with 56 additions and 50 deletions

View File

@ -63,7 +63,7 @@ namespace writer {
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 @@ bool Builder::GenerateEntryPoint(ast::EntryPoint* ep) {
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 @@ bool Builder::GenerateFunction(ast::Function* func) {
{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 @@ uint32_t Builder::GenerateFunctionTypeIfNeeded(ast::Function* func) {
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 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
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 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr,
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 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr,
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 @@ uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) {
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 @@ uint32_t Builder::GenerateTypeConstructorExpression(
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 @@ uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) {
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 @@ uint32_t Builder::GenerateIntrinsic(const std::string& name,
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 @@ bool Builder::GenerateSwitchStatement(ast::SwitchStatement* stmt) {
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 @@ bool Builder::GenerateStructType(ast::type::StructType* struct_type,
{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) {

View File

@ -123,39 +123,39 @@ class Builder {
/// @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 @@ class Builder {
/// 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 @@ class Builder {
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_;

View File

@ -24,7 +24,7 @@ Function::Function()
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;

View File

@ -39,7 +39,7 @@ class Function {
/// @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 @@ class Function {
/// 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 @@ class Function {
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

View File

@ -20,7 +20,7 @@ namespace tint {
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;

View File

@ -30,7 +30,7 @@ class Instruction {
/// 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 @@ class Instruction {
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

View File

@ -16,6 +16,7 @@
#define SRC_WRITER_SPIRV_OPERAND_H_
#include <string>
#include <vector>
namespace tint {
namespace writer {
@ -88,6 +89,9 @@ class Operand {
std::string str_val_;
};
/// A list of operands
using OperandList = std::vector<Operand>;
} // namespace spirv
} // namespace writer
} // namespace tint

View File

@ -75,7 +75,7 @@ std::string DumpInstruction(const Instruction& inst) {
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) {

View File

@ -38,7 +38,7 @@ std::string DumpInstruction(const Instruction& inst);
/// 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