Skip to content

Commit

Permalink
Remove implicit call to GetId in ConvertToSampledImagePass.
Browse files Browse the repository at this point in the history
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 KhronosGroup#5691
  • Loading branch information
s-perron committed May 30, 2024
1 parent 142bf7d commit b4143d3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
6 changes: 2 additions & 4 deletions source/opt/convert_to_sampled_image_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 3 additions & 6 deletions source/opt/type_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Instruction> type_inst(
new Instruction(context(), spv::Op::OpTypePointer, 0, resultId,
{{spv_operand_type_t::SPV_OPERAND_TYPE_STORAGE_CLASS,
Expand Down

0 comments on commit b4143d3

Please sign in to comment.