[Fix] (ShaderTranspiler): preserve block identity when deduplicating image types

This commit is contained in:
2026-10-05 01:27:50 -04:00
parent c464947bdd
commit c5d25868cf
5 changed files with 125 additions and 4 deletions
@@ -38,6 +38,7 @@
#include "SpirvPasses/FixIterationRPBarrierPass.h"
#include "SpirvPasses/FixIterationRPSubgroupScratchPass.h"
#include "SpirvPasses/NormalizeRectCoordinatesPass.h"
#include "SpirvPasses/DeduplicateImageTypesPass.h"
#include "SpirvPasses/Lower1DArrayImagesPass.h"
#include "SpirvPasses/Lower1DSampledImagesPass.h"
#include "SpirvPasses/BakeImageFormatsPass.h"
@@ -1814,7 +1815,7 @@ namespace MobileGL {
Optimizer optimizer(SPV_ENV_VULKAN_1_1);
optimizer.RegisterPass(NormalizeRectCoordinatesPass::CreateNormalizeRectCoordinatesPass());
// Rect and 2D images of the same format become the same type after lowering.
optimizer.RegisterPass(CreateRemoveDuplicatesPass());
optimizer.RegisterPass(DeduplicateImageTypesPass::Create());
return RunOptimizerChecked("LowerRectImages", optimizer, inputBinary, outputBinary, true, enableSpirvValidation);
}
@@ -2106,10 +2107,9 @@ namespace MobileGL {
outputBinary.insert(outputBinary.begin() + static_cast<std::ptrdiff_t>(capabilityInsertOffset),
addedCapabilities.begin(), addedCapabilities.end());
// Erasing the format can make distinct image types identical. Use the same
// cleanup as the image-widening/lowering paths to merge their dependent types
// and update every use before validating the rewritten module.
// image-only cleanup as rectangle lowering; named blocks retain their identity.
spvtools::Optimizer optimizer(SPV_ENV_VULKAN_1_1);
optimizer.RegisterPass(spvtools::CreateRemoveDuplicatesPass());
optimizer.RegisterPass(DeduplicateImageTypesPass::Create());
Vector<Uint32> rewrittenBinary = std::move(outputBinary);
return RunOptimizerChecked("UseUnformattedFloatStorageImagesForVulkan", optimizer,
rewrittenBinary, outputBinary, true, enableSpirvValidation);
@@ -0,0 +1,75 @@
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/DeduplicateImageTypesPass.cpp
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#include "DeduplicateImageTypesPass.h"
#include "source/opt/decoration_manager.h"
#include "source/opt/ir_context.h"
#include "source/util/make_unique.h"
#include <map>
#include <unordered_set>
#include <vector>
namespace MobileGL::MG_Util::ShaderTranspiler {
spvtools::opt::Pass::Status DeduplicateImageTypesPass::Process() {
using spvtools::opt::Instruction;
std::unordered_set<uint32_t> imageTypes;
std::map<std::vector<uint32_t>, std::vector<Instruction*>> canonical;
std::vector<Instruction*> duplicates;
for (auto& type : context()->types_values()) {
bool imageRelated = false;
switch (type.opcode()) {
case spv::Op::OpTypeImage:
imageRelated = true;
break;
case spv::Op::OpTypeSampledImage:
case spv::Op::OpTypeArray:
case spv::Op::OpTypeRuntimeArray:
case spv::Op::OpTypePointer:
case spv::Op::OpTypeFunction:
type.ForEachInId([&](const uint32_t* id) {
imageRelated |= imageTypes.count(*id) != 0;
});
break;
default:
break;
}
if (!imageRelated) continue;
imageTypes.insert(type.result_id());
// Compare operand IDs, not structural equivalence. Distinct struct IDs
// must never collapse just because their member layouts happen to match.
std::vector<uint32_t> key{static_cast<uint32_t>(type.opcode())};
for (uint32_t i = 0; i < type.NumInOperands(); ++i) {
const auto& words = type.GetInOperand(i).words;
key.insert(key.end(), words.begin(), words.end());
}
auto& candidates = canonical[key];
Instruction* match = nullptr;
for (auto* candidate : candidates) {
if (context()->get_decoration_mgr()->HaveTheSameDecorations(
type.result_id(), candidate->result_id())) {
match = candidate;
break;
}
}
if (!match) {
candidates.push_back(&type);
continue;
}
context()->KillNamesAndDecorates(type.result_id());
context()->ReplaceAllUsesWith(type.result_id(), match->result_id());
duplicates.push_back(&type);
}
for (auto* type : duplicates) context()->KillInst(type);
return duplicates.empty() ? Status::SuccessWithoutChange : Status::SuccessWithChange;
}
spvtools::Optimizer::PassToken DeduplicateImageTypesPass::Create() {
return spvtools::Optimizer::PassToken(spvtools::MakeUnique<DeduplicateImageTypesPass>());
}
}
@@ -0,0 +1,21 @@
// MobileGL - MobileGL/MG_Util/ShaderTranspiler/SpirvPasses/DeduplicateImageTypesPass.h
// Copyright (c) 2025-2026 MobileGL-Dev
// Licensed under the GNU Lesser General Public License v3.0:
// https://www.gnu.org/licenses/gpl-3.0.txt
// https://www.gnu.org/licenses/lgpl-3.0.txt
// SPDX-License-Identifier: LGPL-3.0-only
// End of Source File Header
#pragma once
#include "spirv-tools/optimizer.hpp"
#include "source/opt/pass.h"
namespace MobileGL::MG_Util::ShaderTranspiler {
// Preserve named structs: GL reflection and cross-stage interfaces use their identity.
class DeduplicateImageTypesPass final : public spvtools::opt::Pass {
public:
const char* name() const override { return "mobilegl-deduplicate-image-types"; }
Status Process() override;
static spvtools::Optimizer::PassToken Create();
};
}