From 1bace0e33fef9bcdb62466950bcbe22d7deae566 Mon Sep 17 00:00:00 2001 From: Daria Sukhonina Date: Tue, 2 Jul 2024 22:09:05 +0300 Subject: [PATCH] GC private tables (#265) --- crates/tests/tests/spec-tests.rs | 6 ++++-- src/passes/used.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/tests/tests/spec-tests.rs b/crates/tests/tests/spec-tests.rs index 27e3ab89..82cdb084 100644 --- a/crates/tests/tests/spec-tests.rs +++ b/crates/tests/tests/spec-tests.rs @@ -148,8 +148,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 f39a1174..11911996 100644 --- a/src/passes/used.rs +++ b/src/passes/used.rs @@ -130,9 +130,15 @@ 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. - ElementKind::Active { .. } | ElementKind::Declared => { + // tables. + ElementKind::Active { table, .. } => { + 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()); } ElementKind::Passive => {}