tint/transform: Strip unused atomic builtins

If a SPIR-V program declared spirv-atomic stubs, but didn't call them then the transform could be skipped, leaving stub functions behind. This could cause writers to vomit.

Ensure that these are correctly stripped.

Bug: oss-fuzz:54057
Change-Id: I27c89a621163b1a3cc5e2ef375f846a094434062
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/113023
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2022-12-06 19:41:22 +00:00 committed by Dawn LUCI CQ
parent d3137f0ac3
commit c07de73b0b
2 changed files with 17 additions and 1 deletions

View File

@ -64,6 +64,8 @@ struct SpirvAtomic::State {
/// Runs the transform
/// @returns the new program or SkipTransform if the transform is not required
ApplyResult Run() {
bool made_changes = false;
// Look for stub functions generated by the SPIR-V reader, which are used as placeholders
// for atomic builtin calls.
for (auto* fn : ctx.src->AST().Functions()) {
@ -104,10 +106,11 @@ struct SpirvAtomic::State {
// Remove the stub from the output program
ctx.Remove(ctx.src->AST().GlobalDeclarations(), fn);
made_changes = true;
}
}
if (atomic_expressions.IsEmpty()) {
if (!made_changes) {
return SkipTransform;
}

View File

@ -151,6 +151,19 @@ class SpirvAtomicTest : public TransformTest {
std::vector<std::unique_ptr<Source::File>> files_;
};
TEST_F(SpirvAtomicTest, StripUnusedBuiltins) {
auto* src = R"(
fn f() {
}
)";
auto* expect = src;
auto got = Run(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(SpirvAtomicTest, ArrayOfU32) {
auto* src = R"(
var<workgroup> wg : array<u32, 4>;