Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ Released YYYY-MM-DD.

### Added

* TODO (or remove section if none)
* `ModuleConfig::function_order` takes a closure that decides the order
defined functions are emitted, and so numbered, in. It is given every
defined function as a `FunctionInfo`, with its size and how many times its
index is written, in the default order (largest first) to reorder. For
example, sorting by `Reverse(f.uses())` gives the shortest LEB128 indices to
the most-referenced functions, which makes most modules smaller. The default
order isn't guaranteed and may change; give a closure to depend on one.

### Changed

Expand Down
180 changes: 180 additions & 0 deletions crates/tests/tests/function_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//! Tests for `ModuleConfig::function_order`, which decides the order defined
//! functions are emitted, and so numbered, in.
//!
//! The default order is not guaranteed; `default_order` checks what it is
//! today and should be updated, not preserved, if it changes.

use std::cmp::Reverse;
use std::sync::{Arc, Mutex};

use walrus::ir::Instr;
use walrus::{FunctionId, Module, ModuleConfig};

/// Sizes (in instructions): `big` 9, `caller` 7, `small` 1, `in_table` 0.
/// Uses: `small` 4 (three `call`s and a `return_call`), `in_table` 2 (listed
/// twice in an element segment), `big` 1 (an export), `caller` 0.
const WAT: &str = r#"
(module
(table 2 funcref)
(func $big (export "big") (result i32)
i32.const 1 i32.const 2 i32.add i32.const 3 i32.add
i32.const 4 i32.add i32.const 5 i32.add)
(func $small (result i32) i32.const 0)
(func $caller (result i32)
call $small call $small i32.add call $small i32.add drop
return_call $small)
(func $in_table)
(elem (i32.const 0) func $in_table $in_table))
"#;

/// Parses `WAT`, emits it with `config`, and returns the names of the
/// emitted module's defined functions in index order.
fn emitted_order(config: &ModuleConfig) -> Vec<String> {
let wasm = wat::parse_str(WAT).unwrap();
let mut module = config.parse(&wasm).unwrap();
let emitted = module.emit_wasm();
let module = Module::from_buffer(&emitted).unwrap();
check_calls(&module);
module
.funcs
.iter_local()
.map(|(id, _)| name(&module, id))
.collect()
}

fn name(module: &Module, id: FunctionId) -> String {
module.funcs.get(id).name.clone().unwrap()
}

/// The renumbered calls in `caller` still reach `small`.
fn check_calls(module: &Module) {
let caller = module.funcs.by_name("caller").unwrap();
let local = module.funcs.get(caller).kind.unwrap_local();
let targets: Vec<_> = local
.block(local.entry_block())
.instrs
.iter()
.filter_map(|(instr, _)| match instr {
Instr::Call(c) => Some(c.func),
Instr::ReturnCall(c) => Some(c.func),
_ => None,
})
.map(|f| name(module, f))
.collect();
assert_eq!(targets, ["small"; 4]);
}

#[test]
fn default_order() {
// Largest first.
assert_eq!(
emitted_order(&ModuleConfig::new()),
["big", "caller", "small", "in_table"]
);
}

#[test]
fn most_used_first() {
let mut config = ModuleConfig::new();
config.function_order(|_, functions| functions.sort_by_key(|f| Reverse(f.uses())));
assert_eq!(
emitted_order(&config),
["small", "in_table", "big", "caller"]
);
}

#[test]
fn closure_sees_default_order_sizes_and_uses() {
let seen = Arc::new(Mutex::new(Vec::new()));
let mut config = ModuleConfig::new();
let record = seen.clone();
config.function_order(move |module, functions| {
*record.lock().unwrap() = functions
.iter()
.map(|f| (name(module, f.id()), f.size(), f.uses()))
.collect();
});
let order = emitted_order(&config);

let expected = [
("big", 9, 1),
("caller", 7, 0),
("small", 1, 4),
("in_table", 0, 2),
]
.map(|(n, size, uses)| (n.to_string(), size, uses));
assert_eq!(*seen.lock().unwrap(), expected);
// Leaving the slice as it is keeps the default order.
assert_eq!(order, ["big", "caller", "small", "in_table"]);
}

#[test]
fn closure_can_read_the_module() {
let mut config = ModuleConfig::new();
config.function_order(|module, functions| {
functions.sort_by_key(|f| module.funcs.get(f.id()).name.clone());
});
assert_eq!(
emitted_order(&config),
["big", "caller", "in_table", "small"]
);
}

#[test]
fn clone_keeps_the_closure() {
let mut config = ModuleConfig::new();
config.function_order(|_, functions| functions.reverse());
assert_eq!(
emitted_order(&config.clone()),
["in_table", "small", "caller", "big"]
);
}

#[test]
fn uses_count_every_index_outside_code() {
let wat = r#"
(module
(table 2 funcref)
(table 1 funcref (ref.func $in_table))
(global funcref (ref.func $in_global))
(elem (i32.const 0) funcref (ref.func $in_elem_expr) (ref.func $in_elem_expr))
(elem declare func $by_ref_func)
(start $start)
(func $start)
(func $in_global)
(func $in_table)
(func $in_elem_expr)
(func $by_ref_func)
(func $user
ref.func $by_ref_func drop
ref.func $by_ref_func drop))
"#;
let seen = Arc::new(Mutex::new(Vec::new()));
let mut config = ModuleConfig::new();
let record = seen.clone();
config.function_order(move |module, functions| {
let mut uses: Vec<_> = functions
.iter()
.map(|f| (name(module, f.id()), f.uses()))
.collect();
uses.sort();
*record.lock().unwrap() = uses;
});
let mut module = config.parse(&wat::parse_str(wat).unwrap()).unwrap();
module.emit_wasm();
let expected = [
// Two `ref.func`s in code and its `declare` segment.
("by_ref_func", 3),
// Listed twice in an element segment's expressions.
("in_elem_expr", 2),
// The global initializer, and the `declare` segment walrus adds for
// functions referenced from one.
("in_global", 2),
// A table initializer declares the function itself.
("in_table", 1),
("start", 1),
("user", 0),
]
.map(|(n, uses)| (n.to_string(), uses));
assert_eq!(*seen.lock().unwrap(), expected);
}
2 changes: 2 additions & 0 deletions src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct EmitContext<'a> {
pub wasm_module: wasm_encoder::Module,
/// Facts about every local function's body, gathered before emission.
pub func_facts: IdHashMap<Function, EmitFacts>,
/// The order that local functions are emitted, and so numbered, in.
pub function_order: Vec<FunctionId>,
pub code_transform: CodeTransform,
}

Expand Down
48 changes: 46 additions & 2 deletions src/module/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::error::Result;
use crate::ir::InstrLocId;
use crate::module::Module;
use crate::module::{FunctionInfo, Module};
use crate::parse::IndicesToIds;
use std::fmt;
use std::path::Path;
use std::sync::Arc;
use wasmparser::WasmFeatures;

/// Type alias for the on_parse callback function.
Expand All @@ -12,6 +13,9 @@ type OnParseFn = Box<dyn Fn(&mut Module, &IndicesToIds) -> Result<()> + Sync + S
/// Type alias for the on_instr_loc callback function.
type OnInstrLocFn = Box<dyn Fn(&usize) -> InstrLocId + Sync + Send + 'static>;

/// Type alias for the function_order callback function.
type FunctionOrderFn = Arc<dyn Fn(&Module, &mut [FunctionInfo]) + Sync + Send + 'static>;

/// Configuration for a `Module` which currently affects parsing.
#[derive(Default)]
pub struct ModuleConfig {
Expand All @@ -25,6 +29,7 @@ pub struct ModuleConfig {
pub(crate) preserve_code_transform: bool,
pub(crate) on_parse: Option<OnParseFn>,
pub(crate) on_instr_loc: Option<OnInstrLocFn>,
pub(crate) function_order: Option<FunctionOrderFn>,
}

impl Clone for ModuleConfig {
Expand All @@ -40,8 +45,9 @@ impl Clone for ModuleConfig {
skip_name_section: self.skip_name_section,
skip_legacy_exceptions: self.skip_legacy_exceptions,
preserve_code_transform: self.preserve_code_transform,
function_order: self.function_order.clone(),

// ... and this is left empty.
// ... and these are left empty.
on_parse: None,
on_instr_loc: None,
}
Expand All @@ -63,6 +69,7 @@ impl fmt::Debug for ModuleConfig {
ref preserve_code_transform,
ref on_parse,
ref on_instr_loc,
ref function_order,
} = self;

f.debug_struct("ModuleConfig")
Expand All @@ -79,6 +86,7 @@ impl fmt::Debug for ModuleConfig {
.field("preserve_code_transform", preserve_code_transform)
.field("on_parse", &on_parse.as_ref().map(|_| ".."))
.field("on_instr_loc", &on_instr_loc.as_ref().map(|_| ".."))
.field("function_order", &function_order.as_ref().map(|_| ".."))
.finish()
}
}
Expand Down Expand Up @@ -251,6 +259,42 @@ impl ModuleConfig {
self
}

/// Provide a function that decides the order defined functions are
/// emitted, and so numbered, in.
///
/// When a module is emitted, `f` is given every defined function, in
/// walrus's default order, to reorder in place. Imported functions always
/// come first in the function index space, so they aren't included.
///
/// The default order is largest function first, so that engines that
/// compile functions in parallel start on the longest ones early. It is
/// not guaranteed and may change between releases; give a closure to
/// depend on a particular order. A stable sort keeps the default order
/// among functions that compare equal. For example, to give the shortest
/// function indices to the functions whose index is written most often:
///
/// ```
/// use std::cmp::Reverse;
///
/// let mut config = walrus::ModuleConfig::new();
/// config.function_order(|_module, functions| {
/// functions.sort_by_key(|f| Reverse(f.uses()));
/// });
/// ```
///
/// Note that only one `function_order` function may be registered and
/// subsequent registrations will override the old ones.
///
/// Unlike `on_parse`, cloning a `ModuleConfig` keeps its `function_order`
/// function.
pub fn function_order<F>(&mut self, f: F) -> &mut ModuleConfig
where
F: Fn(&Module, &mut [FunctionInfo]) + Send + Sync + 'static,
{
self.function_order = Some(Arc::new(f) as _);
self
}

/// Parses an in-memory WebAssembly file into a `Module` using this
/// configuration.
pub fn parse(&self, wasm: &[u8]) -> Result<Module> {
Expand Down
20 changes: 17 additions & 3 deletions src/module/functions/local_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{ir::*, HeapType, RefType};
use crate::{Data, DataId, FunctionBuilder, FunctionId, MemoryId, Module, Result, TypeId, ValType};
use std::collections::BTreeMap;
use std::ops::Range;
use std::sync::atomic::{AtomicU64, Ordering};
use wasmparser::{FuncValidator, Operator, ValidatorResources};

/// Return type for emit_locals: (encoded locals, local index map)
Expand Down Expand Up @@ -183,8 +184,14 @@ impl LocalFunction {
/// reachable from the entry block. In particular, `size` must not be
/// computed from the arena of instruction sequences: that would count
/// dangling sequences too.
pub(crate) fn emit_facts(&self) -> EmitFacts {
///
/// With `uses`, every function index the body writes (`call`,
/// `return_call`, `ref.func`) also increments that function's counter,
/// which is indexed by [`FunctionId::index`]. Bodies may be walked in
/// parallel, so the counts are only meaningful once every walk is done.
pub(crate) fn emit_facts(&self, uses: Option<&[AtomicU64]>) -> EmitFacts {
let mut v = FactsVisitor {
uses,
facts: EmitFacts {
size: 0,
uses_data_segments: false,
Expand All @@ -195,11 +202,12 @@ impl LocalFunction {
dfs_in_order(&mut v, self, self.entry_block());
return v.facts;

struct FactsVisitor {
struct FactsVisitor<'a> {
uses: Option<&'a [AtomicU64]>,
facts: EmitFacts,
}

impl<'instr> Visitor<'instr> for FactsVisitor {
impl<'instr> Visitor<'instr> for FactsVisitor<'_> {
fn start_instr_seq(&mut self, seq: &'instr InstrSeq) {
self.facts.size += seq.len() as u64;
}
Expand All @@ -212,6 +220,12 @@ impl LocalFunction {
self.facts.ref_funcs.push(instr.func);
}

fn visit_function_id(&mut self, id: &FunctionId) {
if let Some(uses) = self.uses {
uses[id.index()].fetch_add(1, Ordering::Relaxed);
}
}

fn visit_local_id(&mut self, id: &LocalId) {
self.facts.used_locals.insert(*id);
}
Expand Down
Loading
Loading