@@ -83,7 +83,6 @@ uint32_t Pass::GetNullId(uint32_t type_id) {
8383
8484uint32_t Pass::GenerateCopy (Instruction* object_to_copy, uint32_t new_type_id,
8585 Instruction* insertion_position) {
86- analysis::TypeManager* type_mgr = context ()->get_type_mgr ();
8786 analysis::ConstantManager* const_mgr = context ()->get_constant_mgr ();
8887
8988 uint32_t original_type_id = object_to_copy->type_id ();
@@ -95,55 +94,52 @@ uint32_t Pass::GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id,
9594 context (), insertion_position,
9695 IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse );
9796
98- analysis::Type* original_type = type_mgr->GetType (original_type_id);
99- analysis::Type* new_type = type_mgr->GetType (new_type_id);
100-
101- if (const analysis::Array* original_array_type = original_type->AsArray ()) {
102- uint32_t original_element_type_id =
103- type_mgr->GetId (original_array_type->element_type ());
104-
105- analysis::Array* new_array_type = new_type->AsArray ();
106- assert (new_array_type != nullptr && " Can't copy an array to a non-array." );
107- uint32_t new_element_type_id =
108- type_mgr->GetId (new_array_type->element_type ());
109-
110- std::vector<uint32_t > element_ids;
111- const analysis::Constant* length_const =
112- const_mgr->FindDeclaredConstant (original_array_type->LengthId ());
113- assert (length_const->AsIntConstant ());
114- uint32_t array_length = length_const->AsIntConstant ()->GetU32 ();
115- for (uint32_t i = 0 ; i < array_length; i++) {
116- Instruction* extract = ir_builder.AddCompositeExtract (
117- original_element_type_id, object_to_copy->result_id (), {i});
118- element_ids.push_back (
119- GenerateCopy (extract, new_element_type_id, insertion_position));
97+ Instruction* original_type = get_def_use_mgr ()->GetDef (original_type_id);
98+ Instruction* new_type = get_def_use_mgr ()->GetDef (new_type_id);
99+ assert (new_type->opcode () == original_type->opcode () &&
100+ " Can't copy an aggragate type unless the type correspond." );
101+
102+ switch (original_type->opcode ()) {
103+ case spv::Op::OpTypeArray: {
104+ uint32_t original_element_type_id =
105+ original_type->GetSingleWordInOperand (0 );
106+ uint32_t new_element_type_id = new_type->GetSingleWordInOperand (0 );
107+
108+ std::vector<uint32_t > element_ids;
109+ uint32_t length_id = original_type->GetSingleWordInOperand (1 );
110+ const analysis::Constant* length_const =
111+ const_mgr->FindDeclaredConstant (length_id);
112+ assert (length_const->AsIntConstant ());
113+ uint32_t array_length = length_const->AsIntConstant ()->GetU32 ();
114+ for (uint32_t i = 0 ; i < array_length; i++) {
115+ Instruction* extract = ir_builder.AddCompositeExtract (
116+ original_element_type_id, object_to_copy->result_id (), {i});
117+ element_ids.push_back (
118+ GenerateCopy (extract, new_element_type_id, insertion_position));
119+ }
120+
121+ return ir_builder.AddCompositeConstruct (new_type_id, element_ids)
122+ ->result_id ();
120123 }
121-
122- return ir_builder.AddCompositeConstruct (new_type_id, element_ids)
123- ->result_id ();
124- } else if (const analysis::Struct* original_struct_type =
125- original_type->AsStruct ()) {
126- analysis::Struct* new_struct_type = new_type->AsStruct ();
127-
128- const std::vector<const analysis::Type*>& original_types =
129- original_struct_type->element_types ();
130- const std::vector<const analysis::Type*>& new_types =
131- new_struct_type->element_types ();
132- std::vector<uint32_t > element_ids;
133- for (uint32_t i = 0 ; i < original_types.size (); i++) {
134- Instruction* extract = ir_builder.AddCompositeExtract (
135- type_mgr->GetId (original_types[i]), object_to_copy->result_id (), {i});
136- element_ids.push_back (GenerateCopy (extract, type_mgr->GetId (new_types[i]),
137- insertion_position));
124+ case spv::Op::OpTypeStruct: {
125+ std::vector<uint32_t > element_ids;
126+ for (uint32_t i = 0 ; i < original_type->NumInOperands (); i++) {
127+ uint32_t orig_member_type_id = original_type->GetSingleWordInOperand (i);
128+ uint32_t new_member_type_id = new_type->GetSingleWordInOperand (i);
129+ Instruction* extract = ir_builder.AddCompositeExtract (
130+ orig_member_type_id, object_to_copy->result_id (), {i});
131+ element_ids.push_back (
132+ GenerateCopy (extract, new_member_type_id, insertion_position));
133+ }
134+ return ir_builder.AddCompositeConstruct (new_type_id, element_ids)
135+ ->result_id ();
138136 }
139- return ir_builder.AddCompositeConstruct (new_type_id, element_ids)
140- ->result_id ();
141- } else {
142- // If we do not have an aggregate type, then we have a problem. Either we
143- // found multiple instances of the same type, or we are copying to an
144- // incompatible type. Either way the code is illegal.
145- assert (false &&
146- " Don't know how to copy this type. Code is likely illegal." );
137+ default :
138+ // If we do not have an aggregate type, then we have a problem. Either we
139+ // found multiple instances of the same type, or we are copying to an
140+ // incompatible type. Either way the code is illegal.
141+ assert (false &&
142+ " Don't know how to copy this type. Code is likely illegal." );
147143 }
148144 return 0 ;
149145}
0 commit comments