From b4143d34617c1cbd0291a6f41cea17760d5e1bb2 Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Wed, 29 May 2024 13:44:34 -0400 Subject: [PATCH] Remove implicit call to GetId in ConvertToSampledImagePass. We replace getting the id of a poitner type with a specific funciton call to FindPointerToType. Also, FindPointerToType is updated to not indirectly call GetId. This leads to a linear search for an existing type in all cases, but it is necessary. Note that this function could have a similar problem. There could be two pointer types with the same pointee and storage class, and the first one will be returned. I have checked the ~20 uses, and they are all used in situations where the id is used to create something new, and it does not have to match an existing type. These will not cause problems. Part of #5691 --- source/opt/convert_to_sampled_image_pass.cpp | 6 ++---- source/opt/type_manager.cpp | 9 +++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/source/opt/convert_to_sampled_image_pass.cpp b/source/opt/convert_to_sampled_image_pass.cpp index c82db41cec..d2da4d1e0b 100644 --- a/source/opt/convert_to_sampled_image_pass.cpp +++ b/source/opt/convert_to_sampled_image_pass.cpp @@ -329,12 +329,10 @@ bool ConvertToSampledImagePass::ConvertImageVariableToSampledImage( if (sampled_image_type == nullptr) return false; auto storage_class = GetStorageClass(*image_variable); if (storage_class == spv::StorageClass::Max) return false; - analysis::Pointer sampled_image_pointer(sampled_image_type, storage_class); - // Make sure |image_variable| is behind its type i.e., avoid the forward // reference. - uint32_t type_id = - context()->get_type_mgr()->GetTypeInstruction(&sampled_image_pointer); + uint32_t type_id = context()->get_type_mgr()->FindPointerToType( + sampled_image_type_id, storage_class); MoveInstructionNextToType(image_variable, type_id); return true; } diff --git a/source/opt/type_manager.cpp b/source/opt/type_manager.cpp index 7b609bc776..62f93698ff 100644 --- a/source/opt/type_manager.cpp +++ b/source/opt/type_manager.cpp @@ -454,12 +454,7 @@ uint32_t TypeManager::FindPointerToType(uint32_t type_id, spv::StorageClass storage_class) { Type* pointeeTy = GetType(type_id); Pointer pointerTy(pointeeTy, storage_class); - if (pointeeTy->IsUniqueType()) { - // Non-ambiguous type. Get the pointer type through the type manager. - return GetTypeInstruction(&pointerTy); - } - // Ambiguous type, do a linear search. Module::inst_iterator type_itr = context()->module()->types_values_begin(); for (; type_itr != context()->module()->types_values_end(); ++type_itr) { const Instruction* type_inst = &*type_itr; @@ -472,8 +467,10 @@ uint32_t TypeManager::FindPointerToType(uint32_t type_id, } // Must create the pointer type. - // TODO(1841): Handle id overflow. uint32_t resultId = context()->TakeNextId(); + if (resultId == 0) { + return 0; + } std::unique_ptr type_inst( new Instruction(context(), spv::Op::OpTypePointer, 0, resultId, {{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,