Reset the id bound on the module in compact ids (#4744)

If the body of the module does not have any ids change, compact ids will
not change the id bound.  This can cause problems because the id bound
could be much higher than the largest id in that is used.  It should be
reset any time it is not the larger id used + 1.

Fixes #4604
This commit is contained in:
Steven Perron 2022-03-07 20:33:01 +00:00 committed by GitHub
parent 48a36c72e4
commit 0741f42738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -86,7 +86,8 @@ Pass::Status CompactIdsPass::Process() {
},
true);
if (modified) {
if (context()->module()->id_bound() != result_id_mapping.size() + 1) {
modified = true;
context()->module()->SetIdBound(
static_cast<uint32_t>(result_id_mapping.size() + 1));
// There are ids in the feature manager that could now be invalid

View File

@ -310,6 +310,41 @@ OpFunctionEnd
EXPECT_THAT(disassembly, ::testing::Eq(expected));
}
TEST(CompactIds, ResetIdBound) {
const std::string input(R"(OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %1 "main"
OpExecutionMode %1 OriginUpperLeft
%void = OpTypeVoid
%3 = OpTypeFunction %void
%1 = OpFunction %void None %3
%4 = OpLabel
OpReturn
OpFunctionEnd
)");
spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_1);
std::unique_ptr<IRContext> context =
BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, input,
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
ASSERT_NE(context, nullptr);
CompactIdsPass compact_id_pass;
context->module()->SetIdBound(20000);
const auto status = compact_id_pass.Run(context.get());
EXPECT_EQ(status, Pass::Status::SuccessWithChange);
EXPECT_EQ(context->module()->id_bound(), 5);
// Test output just in case
std::vector<uint32_t> binary;
context->module()->ToBinary(&binary, false);
std::string disassembly;
tools.Disassemble(binary, &disassembly,
SpirvTools::kDefaultDisassembleOption);
EXPECT_THAT(disassembly, ::testing::Eq(input));
}
} // namespace
} // namespace opt
} // namespace spvtools