mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-06 10:00:22 +00:00
wasm: fix DCE for signatures and debug names
This commit is contained in:
parent
c169815c9e
commit
acebdee3dd
1 changed files with 23 additions and 18 deletions
|
@ -5,6 +5,8 @@ pub mod parse;
|
||||||
pub mod sections;
|
pub mod sections;
|
||||||
pub mod serialize;
|
pub mod serialize;
|
||||||
|
|
||||||
|
use std::iter::repeat;
|
||||||
|
|
||||||
pub use code_builder::{Align, CodeBuilder, LocalId, ValueType, VmSymbolState};
|
pub use code_builder::{Align, CodeBuilder, LocalId, ValueType, VmSymbolState};
|
||||||
pub use linking::{OffsetRelocType, RelocationEntry, SymInfo};
|
pub use linking::{OffsetRelocType, RelocationEntry, SymInfo};
|
||||||
pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature};
|
pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature};
|
||||||
|
@ -211,35 +213,38 @@ impl<'a> WasmModule<'a> {
|
||||||
// Remove all unused JS imports
|
// Remove all unused JS imports
|
||||||
// We don't want to force the web page to provide dummy JS functions, it's a pain!
|
// We don't want to force the web page to provide dummy JS functions, it's a pain!
|
||||||
//
|
//
|
||||||
|
let mut live_import_fns = Vec::with_capacity_in(self.import.imports.len(), arena);
|
||||||
let mut fn_index = 0;
|
let mut fn_index = 0;
|
||||||
self.import.imports.retain(|import| {
|
self.import.imports.retain(|import| {
|
||||||
if !matches!(import.description, ImportDesc::Func { .. }) {
|
if !matches!(import.description, ImportDesc::Func { .. }) {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
let live = live_flags[fn_index];
|
let live = live_flags[fn_index];
|
||||||
|
if live {
|
||||||
|
live_import_fns.push(fn_index);
|
||||||
|
}
|
||||||
fn_index += 1;
|
fn_index += 1;
|
||||||
live
|
live
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
// signatures
|
||||||
// Update function signatures & debug names for imports that changed index
|
let live_import_count = live_import_fns.len();
|
||||||
//
|
let dead_import_count = host_fn_min as usize - live_import_count;
|
||||||
let live_import_fns = Vec::from_iter_in(
|
let signature_count = self.function.signatures.len();
|
||||||
live_flags
|
self.function
|
||||||
.iter_ones()
|
.signatures
|
||||||
.take_while(|i| *i < self.import.imports.len()),
|
.extend(repeat(0).take(dead_import_count));
|
||||||
arena,
|
self.function
|
||||||
);
|
.signatures
|
||||||
for (new_index, old_index) in live_import_fns.iter().enumerate() {
|
.copy_within(0..signature_count, dead_import_count);
|
||||||
// Safe because `old_index >= new_index`
|
|
||||||
self.function.signatures[new_index] = self.function.signatures[*old_index];
|
// debug names
|
||||||
self.names.function_names[new_index] = self.names.function_names[*old_index];
|
for (new_index, &old_index) in live_import_fns.iter().enumerate() {
|
||||||
}
|
let old_name: &str = self.names.function_names[old_index].1;
|
||||||
let first_dead_import_index = live_import_fns.last().map(|x| x + 1).unwrap_or(0) as u32;
|
let new_name: &str = self.names.function_names[new_index].1;
|
||||||
for i in first_dead_import_index..host_fn_min {
|
self.names.function_names[new_index].1 = old_name;
|
||||||
self.names.function_names[i as usize] = (i, "unused_host_import");
|
self.names.function_names[old_index].1 = new_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue