Eliminate duplicate types in the type section

This commit updates the `wasm-gc` pass of wasm-bindgen to eliminate
duplicate types in the type section, effectively enabling a gc of the
type section itself. The main purpose here is ensure that code generated
by `wasm-bindgen` itself doesn't have to go too far out of its way to
deduplicate at generation time, but rather it can rely on the gc pass to
clean up.

Note that this currently depends on paritytech/parity-wasm#231, but this
can be updated if that ends up not landing.
This commit is contained in:
Alex Crichton 2018-10-28 09:12:58 -07:00
parent ec6e13a0f8
commit 27001a226d
2 changed files with 13 additions and 7 deletions

View File

@ -11,6 +11,6 @@ Support for removing unused items from a wasm executable
"""
[dependencies]
parity-wasm = "0.35"
parity-wasm = "0.35.1"
log = "0.4"
rustc-demangle = "0.1.9"

View File

@ -8,7 +8,7 @@ extern crate parity_wasm;
extern crate log;
extern crate rustc_demangle;
use std::collections::HashSet;
use std::collections::{HashSet, HashMap};
use std::iter;
use std::mem;
@ -474,14 +474,20 @@ impl<'a> RemapContext<'a> {
let mut memories = Vec::new();
if let Some(s) = m.type_section() {
let mut removed = 0;
for i in 0..(s.types().len() as u32) {
if analysis.types.contains(&i) {
types.push(i - removed);
let mut ntypes = 0;
let mut map = HashMap::new();
for (i, ty) in s.types().iter().enumerate() {
if analysis.types.contains(&(i as u32)) {
if let Some(prev) = map.get(&ty) {
types.push(*prev);
continue
}
map.insert(ty, ntypes);
types.push(ntypes);
ntypes += 1;
} else {
debug!("gc type {}", i);
types.push(u32::max_value());
removed += 1;
}
}
}