From 839c2976912ef401f80c52e0931a938e6fcf5ce1 Mon Sep 17 00:00:00 2001 From: Daria Sukhonina Date: Mon, 6 May 2024 14:52:44 +0300 Subject: [PATCH 1/2] GC private tables GC now does not consider elem to private tables to be used --- crates/tests/tests/spec-tests.rs | 6 ++++-- src/passes/used.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/tests/tests/spec-tests.rs b/crates/tests/tests/spec-tests.rs index 1b1b9e28..0c9fb7a3 100644 --- a/crates/tests/tests/spec-tests.rs +++ b/crates/tests/tests/spec-tests.rs @@ -159,8 +159,10 @@ fn run(wast: &Path) -> Result<(), anyhow::Error> { // Tests which assert that they're not linkable tend to not work with // the gc pass because it removes things which would cause a module to // become unlinkable. This doesn't matter too much in the real world - // (hopefully), so just don't gc assert_unlinkable modules. - if cmd != "assert_unlinkable" { + // (hopefully), so just don't gc assert_unlinkable modules. The same + // applies to assert_uninstantiable modules due to removal of unused + // elements and tables. + if !matches!(cmd.as_str(), "assert_unlinkable" | "assert_uninstantiable") { walrus::passes::gc::run(&mut module); } diff --git a/src/passes/used.rs b/src/passes/used.rs index 163e5329..55c97c13 100644 --- a/src/passes/used.rs +++ b/src/passes/used.rs @@ -132,7 +132,14 @@ impl Used { // Active segments are rooted because they initialize imported // or exported tables. Declared segments can probably get gc'd // but for now we're conservative and we root them. - ElementKind::Active { .. } | ElementKind::Declared => { + ElementKind::Active { table, .. } => { + if module.exports.get_exported_table(table).is_some() + || module.tables.get(table).import.is_some() + { + stack.push_element(elem.id()); + } + } + ElementKind::Declared => { stack.push_element(elem.id()); } ElementKind::Passive => {} From 4751bd98b3fbbaf81bc31e183b39c35eded0a25e Mon Sep 17 00:00:00 2001 From: Daria Sukhonina Date: Mon, 6 May 2024 18:24:35 +0300 Subject: [PATCH 2/2] Do not mark as used elements with export tables Export tables are used anyway --- src/passes/used.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/passes/used.rs b/src/passes/used.rs index 55c97c13..cbae1ba9 100644 --- a/src/passes/used.rs +++ b/src/passes/used.rs @@ -130,15 +130,14 @@ impl Used { for elem in module.elements.iter() { match elem.kind { // Active segments are rooted because they initialize imported - // or exported tables. Declared segments can probably get gc'd - // but for now we're conservative and we root them. + // tables. ElementKind::Active { table, .. } => { - if module.exports.get_exported_table(table).is_some() - || module.tables.get(table).import.is_some() - { + if module.tables.get(table).import.is_some() { stack.push_element(elem.id()); } } + // Declared segments can probably get gc'd but for now we're + // conservative and we root them ElementKind::Declared => { stack.push_element(elem.id()); }