From 47989a94f38a607e92ee5e3ce7973487fd1b0ac5 Mon Sep 17 00:00:00 2001 From: Steven Perron Date: Mon, 27 May 2024 08:58:18 -0400 Subject: [PATCH] Remove use of TypeManager::GetId 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. --- source/opt/folding_rules.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/source/opt/folding_rules.cpp b/source/opt/folding_rules.cpp index f41ef868a3f..67ab867ea02 100644 --- a/source/opt/folding_rules.cpp +++ b/source/opt/folding_rules.cpp @@ -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 @@ -2060,18 +2060,19 @@ bool CompositeInsertToCompositeConstruct( if (inst->NumInOperands() < 3) return false; std::map 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); 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; }