From e87ba1fd37a035e1b6f7b41ec0f8bd69e7625dea Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 22 Jun 2020 20:18:26 +0000 Subject: [PATCH] [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 --- src/writer/spirv/builder.cc | 37 ++++++++++++++++----------------- src/writer/spirv/builder.h | 32 ++++++++++++++-------------- src/writer/spirv/function.cc | 2 +- src/writer/spirv/function.h | 16 +++++++------- src/writer/spirv/instruction.cc | 2 +- src/writer/spirv/instruction.h | 9 +++++--- src/writer/spirv/operand.h | 4 ++++ src/writer/spirv/spv_dump.cc | 2 +- src/writer/spirv/spv_dump.h | 2 +- 9 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index b65802c78d..586b0457a6 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -63,7 +63,7 @@ namespace writer { namespace spirv { namespace { -uint32_t size_of(const std::vector& 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 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 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 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 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 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 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 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 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 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 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 params = {Operand::Int(cond_id), - Operand::Int(default_block_id)}; + OperandList params = {Operand::Int(cond_id), Operand::Int(default_block_id)}; std::vector 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 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 a111094d1e..4213462e98 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -123,39 +123,39 @@ class Builder { /// @param cap the capability to set void push_capability(uint32_t cap); /// @returns the capabilities - const std::vector& 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& operands) { + void push_preamble(spv::Op op, const OperandList& operands) { preamble_.push_back(Instruction{op, operands}); } /// @returns the preamble - const std::vector& 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& operands) { + void push_debug(spv::Op op, const OperandList& operands) { debug_.push_back(Instruction{op, operands}); } /// @returns the debug instructions - const std::vector& 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& operands) { + void push_type(spv::Op op, const OperandList& operands) { types_.push_back(Instruction{op, operands}); } /// @returns the type instructions - const std::vector& 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& operands) { + void push_annot(spv::Op op, const OperandList& operands) { annotations_.push_back(Instruction{op, operands}); } /// @returns the annotations - const std::vector& 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& 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& 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 capabilities_; - std::vector preamble_; - std::vector debug_; - std::vector types_; - std::vector annotations_; + InstructionList capabilities_; + InstructionList preamble_; + InstructionList debug_; + InstructionList types_; + InstructionList annotations_; std::vector functions_; std::unordered_map import_name_to_id_; diff --git a/src/writer/spirv/function.cc b/src/writer/spirv/function.cc index b040ff0a13..01da96ce9d 100644 --- a/src/writer/spirv/function.cc +++ b/src/writer/spirv/function.cc @@ -24,7 +24,7 @@ Function::Function() Function::Function(const Instruction& declaration, const Operand& label_op, - const std::vector& 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 ddc2d0d55e..8421452be8 100644 --- a/src/writer/spirv/function.h +++ b/src/writer/spirv/function.h @@ -39,7 +39,7 @@ class Function { /// @param params the function parameters Function(const Instruction& declaration, const Operand& label_op, - const std::vector& 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& operands) { + void push_inst(spv::Op op, const OperandList& operands) { instructions_.push_back(Instruction{op, operands}); } /// @returns the instruction list - const std::vector& 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& operands) { + void push_var(const OperandList& operands) { vars_.push_back(Instruction{spv::Op::OpVariable, operands}); } /// @returns the variable list - const std::vector& 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 params_; - std::vector vars_; - std::vector instructions_; + InstructionList params_; + InstructionList vars_; + InstructionList instructions_; }; } // namespace spirv diff --git a/src/writer/spirv/instruction.cc b/src/writer/spirv/instruction.cc index 78f6afab65..9a1c174fda 100644 --- a/src/writer/spirv/instruction.cc +++ b/src/writer/spirv/instruction.cc @@ -20,7 +20,7 @@ namespace tint { namespace writer { namespace spirv { -Instruction::Instruction(spv::Op op, std::vector 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 c3213106bf..649746c9c4 100644 --- a/src/writer/spirv/instruction.h +++ b/src/writer/spirv/instruction.h @@ -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 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& 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 operands_; + OperandList operands_; }; +/// A list of instructions +using InstructionList = std::vector; + } // namespace spirv } // namespace writer } // namespace tint diff --git a/src/writer/spirv/operand.h b/src/writer/spirv/operand.h index 79ad2ebc65..a2eafcee86 100644 --- a/src/writer/spirv/operand.h +++ b/src/writer/spirv/operand.h @@ -16,6 +16,7 @@ #define SRC_WRITER_SPIRV_OPERAND_H_ #include +#include namespace tint { namespace writer { @@ -88,6 +89,9 @@ class Operand { std::string str_val_; }; +/// A list of operands +using OperandList = std::vector; + } // namespace spirv } // namespace writer } // namespace tint diff --git a/src/writer/spirv/spv_dump.cc b/src/writer/spirv/spv_dump.cc index c649accd14..6c5cd8fadb 100644 --- a/src/writer/spirv/spv_dump.cc +++ b/src/writer/spirv/spv_dump.cc @@ -75,7 +75,7 @@ std::string DumpInstruction(const Instruction& inst) { return Disassemble(writer.result()); } -std::string DumpInstructions(const std::vector& 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 c9079c78af..deae123e8d 100644 --- a/src/writer/spirv/spv_dump.h +++ b/src/writer/spirv/spv_dump.h @@ -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& insts); +std::string DumpInstructions(const InstructionList& insts); } // namespace spirv } // namespace writer