Skip to content

Commit

Permalink
Remove use of TypeManager::GetId
Browse files Browse the repository at this point in the history
This removes a use of TypeManager::GetId by keeping the id around. This
avoid a potential problem if the type manager gets confused. These types
of bugs are hard to generate test cases for, so I do not have a test.
However, existing tests make sure that do not regress.
  • Loading branch information
s-perron committed May 27, 2024
1 parent 519a78d commit 944f651
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions source/opt/folding_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2005,15 +2005,15 @@ bool DoInsertedValuesCoverEntireObject(
return true;
}

// Returns the type of the element that immediately contains the element being
// inserted by the OpCompositeInsert instruction |inst|.
const analysis::Type* GetContainerType(Instruction* inst) {
// Returns id of the type of the element that immediately contains the element
// being inserted by the OpCompositeInsert instruction |inst|. Returns 0 if it
// could not be found.
uint32_t GetContainerTypeId(Instruction* inst) {
assert(inst->opcode() == spv::Op::OpCompositeInsert);
analysis::DefUseManager* def_use_manager = inst->context()->get_def_use_mgr();
uint32_t container_type_id = GetElementType(
inst->type_id(), inst->begin() + 4, inst->end() - 1, def_use_manager);
analysis::TypeManager* type_mgr = inst->context()->get_type_mgr();
return type_mgr->GetType(container_type_id);
return container_type_id;
}

// Returns an OpCompositeConstruct instruction that build an object with
Expand Down Expand Up @@ -2060,18 +2060,20 @@ bool CompositeInsertToCompositeConstruct(
if (inst->NumInOperands() < 3) return false;

std::map<uint32_t, uint32_t> values_inserted = GetInsertedValues(inst);
const analysis::Type* container_type = GetContainerType(inst);
if (container_type == nullptr) {
uint32_t container_type_id = GetContainerTypeId(inst);
if (container_type_id == 0) {
return false;
}

analysis::TypeManager* type_mgr = context->get_type_mgr();
const analysis::Type* container_type = type_mgr->GetType(container_type_id);
assert(container_type && "GetContainerTypeId returned a bad id.");
if (!DoInsertedValuesCoverEntireObject(container_type, values_inserted)) {
return false;
}

analysis::TypeManager* type_mgr = context->get_type_mgr();
Instruction* construct = BuildCompositeConstruct(
type_mgr->GetId(container_type), values_inserted, inst);
Instruction* construct =
BuildCompositeConstruct(container_type_id, values_inserted, inst);
InsertConstructedObject(inst, construct);
return true;
}
Expand Down

0 comments on commit 944f651

Please sign in to comment.