diff --git a/source/opt/instruction.cpp b/source/opt/instruction.cpp index 2461e41e9..418f1213a 100644 --- a/source/opt/instruction.cpp +++ b/source/opt/instruction.cpp @@ -76,10 +76,9 @@ Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst, dbg_scope_(kNoDebugScope, kNoInlinedAt) { for (uint32_t i = 0; i < inst.num_operands; ++i) { const auto& current_payload = inst.operands[i]; - std::vector words( - inst.words + current_payload.offset, + operands_.emplace_back( + current_payload.type, inst.words + current_payload.offset, inst.words + current_payload.offset + current_payload.num_words); - operands_.emplace_back(current_payload.type, std::move(words)); } assert((!IsLineInst() || dbg_line.empty()) && "Op(No)Line attaching to Op(No)Line found"); @@ -96,10 +95,9 @@ Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst, dbg_scope_(dbg_scope) { for (uint32_t i = 0; i < inst.num_operands; ++i) { const auto& current_payload = inst.operands[i]; - std::vector words( - inst.words + current_payload.offset, + operands_.emplace_back( + current_payload.type, inst.words + current_payload.offset, inst.words + current_payload.offset + current_payload.num_words); - operands_.emplace_back(current_payload.type, std::move(words)); } } diff --git a/source/opt/instruction.h b/source/opt/instruction.h index 066e98707..2163d99b3 100644 --- a/source/opt/instruction.h +++ b/source/opt/instruction.h @@ -84,6 +84,11 @@ struct Operand { Operand(spv_operand_type_t t, const OperandData& w) : type(t), words(w) {} + template + Operand(spv_operand_type_t t, InputIt firstOperandData, + InputIt lastOperandData) + : type(t), words(firstOperandData, lastOperandData) {} + spv_operand_type_t type; // Type of this logical operand. OperandData words; // Binary segments of this logical operand. diff --git a/source/util/small_vector.h b/source/util/small_vector.h index f1762a9f2..4e8e0fd04 100644 --- a/source/util/small_vector.h +++ b/source/util/small_vector.h @@ -64,6 +64,11 @@ class SmallVector { } } + template + SmallVector(InputIt first, InputIt last) : SmallVector() { + insert(end(), first, last); + } + SmallVector(std::vector&& vec) : SmallVector() { if (vec.size() > small_size) { large_data_ = MakeUnique>(std::move(vec)); diff --git a/test/util/small_vector_test.cpp b/test/util/small_vector_test.cpp index 01d7df185..6224a5fe0 100644 --- a/test/util/small_vector_test.cpp +++ b/test/util/small_vector_test.cpp @@ -56,6 +56,18 @@ TEST(SmallVectorTest, Initialize_list2) { } } +TEST(SmallVectorTest, Initialize_list3) { + std::vector result = {0, 1, 2, 3}; + SmallVector vec(result.begin(), result.end()); + + EXPECT_FALSE(vec.empty()); + EXPECT_EQ(vec.size(), 4); + + for (uint32_t i = 0; i < vec.size(); ++i) { + EXPECT_EQ(vec[i], result[i]); + } +} + TEST(SmallVectorTest, Initialize_copy1) { SmallVector vec1 = {0, 1, 2, 3}; SmallVector vec2(vec1);