diff --git a/.travis.yml b/.travis.yml index a6a8b655..de257e3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -172,7 +172,7 @@ matrix: install: - git clone https://github.com/WebAssembly/wabt - mkdir -p wabt/build - - (cd wabt/wabt && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=sccache -DCMAKE_CXX_COMPILER_ARG1=c++ && cmake --build . -- -j4) + - (cd wabt/build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=sccache -DCMAKE_CXX_COMPILER_ARG1=c++ -DBUILD_TESTS=OFF && cmake --build . -- -j4) - export PATH=$PATH:`pwd`/wabt/build script: cargo test -p wasm-bindgen-gc if: branch = master diff --git a/crates/gc/src/bitvec.rs b/crates/gc/src/bitvec.rs index faa8b65b..98cdee83 100644 --- a/crates/gc/src/bitvec.rs +++ b/crates/gc/src/bitvec.rs @@ -29,6 +29,15 @@ impl BitSet { } } + pub fn remove(&mut self, i: &u32) { + let i = *i as usize; + let idx = i / BITS; + let bit = 1 << (i % BITS); + if let Some(slot) = self.bits.get_mut(idx) { + *slot &= !bit; + } + } + pub fn contains(&self, i: &u32) -> bool { let i = *i as usize; let idx = i / BITS; diff --git a/crates/gc/src/lib.rs b/crates/gc/src/lib.rs index 7f200906..4ce1fb7f 100644 --- a/crates/gc/src/lib.rs +++ b/crates/gc/src/lib.rs @@ -51,7 +51,7 @@ impl Config { } fn run(config: &mut Config, module: &mut Module) { - let analysis = { + let mut analysis = { let mut cx = LiveContext::new(&module); cx.blacklist.insert("rust_eh_personality"); cx.blacklist.insert("__indirect_function_table"); @@ -80,7 +80,7 @@ fn run(config: &mut Config, module: &mut Module) { cx.analysis }; - let cx = RemapContext::new(&module, &analysis, config); + let cx = RemapContext::new(&module, &mut analysis, config); for i in (0..module.sections().len()).rev() { let retain = match module.sections_mut()[i] { Section::Unparsed { .. } => { @@ -476,7 +476,7 @@ struct RemapContext<'a> { } impl<'a> RemapContext<'a> { - fn new(m: &Module, analysis: &'a Analysis, config: &'a Config) -> RemapContext<'a> { + fn new(m: &Module, analysis: &'a mut Analysis, config: &'a Config) -> RemapContext<'a> { let mut nfunctions = 0; let mut functions = Vec::new(); let mut nglobals = 0; @@ -494,6 +494,7 @@ impl<'a> RemapContext<'a> { if analysis.types.contains(&(i as u32)) { if let Some(prev) = map.get(&ty) { types.push(*prev); + analysis.types.remove(&(i as u32)); continue } map.insert(ty, ntypes); diff --git a/crates/gc/tests/wat/remove-unused-type.wat b/crates/gc/tests/wat/remove-unused-type.wat new file mode 100644 index 00000000..b80d55d9 --- /dev/null +++ b/crates/gc/tests/wat/remove-unused-type.wat @@ -0,0 +1,34 @@ +(module + (type (func)) + (type (func (param i32))) + (type (func (param i32))) + (type (func (result i32))) + + (func $f1 (type 0)) + (func $f2 (type 1)) + (func $f3 (type 2)) + (func $f4 (type 3) + i32.const 0 + ) + + (export "a" (func $f1)) + (export "b" (func $f2)) + (export "c" (func $f3)) + (export "d" (func $f4)) + ) + +;; STDOUT (update this section with `BLESS_TESTS=1` while running tests) +;; (module +;; (type (;0;) (func)) +;; (type (;1;) (func (param i32))) +;; (type (;2;) (func (result i32))) +;; (func $f1 (type 0)) +;; (func $f2 (type 1) (param i32)) +;; (func $f3 (type 1) (param i32)) +;; (func $f4 (type 2) (result i32) +;; i32.const 0) +;; (export "a" (func $f1)) +;; (export "b" (func $f2)) +;; (export "c" (func $f3)) +;; (export "d" (func $f4))) +;; STDOUT