Skip to content

Commit eb34ad4

Browse files
committed
Wasmtime: Add (optional) bottom-up function inlining to Wasm compilation
This commit plumbs together two pieces of recently-added infrastructure: 1. function inlining in Cranelift, and 2. the parallel bottom-up inlining scheduler in Wasmtime. Sprinkle some very simple inlining heuristics on top, and this gives us function inlining in Wasm compilation. The default Wasmtime configuration does not enable inlining, and when we do enable it, we only enable it for cross-component calls by default (since presumably the toolchain that produced a particular core Wasm module, like LLVM, already performed any inlining that was beneficial within that module, but that toolchain couldn't know how that Wasm module would be getting linked together with other modules via component composition, and so it could not have done any cross-component inlining). For what it is worth, there is a config knob to enable intra-module function inlining, but this is primarily for use by our fuzzers, so that they can easily excercise and explore this new inlining functionality. All this plumbing required some changes to the `wasmtime_environ::Compiler` trait, since Winch cannot do inlining but Cranelift can. This is mostly encapsulated in the new `wasmtime_environ::InliningCompiler` trait, for the most part. Additionally, we take care not to construct the call graph, or any other data structures required only by the inliner and not regular compilation, both when using Winch and when using Cranelift with inlining disabled. Finally, we add a `disas` test to verify that we successfully inline a series of calls from a function in one component, to a cross-component adapter function, to a function in another component. Most test coverage is expected to come from our fuzzing, however.
1 parent 9e9c4f3 commit eb34ad4

File tree

21 files changed

+1140
-65
lines changed

21 files changed

+1140
-65
lines changed

cranelift/codegen/src/inline.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub trait Inline {
7373
/// Failure to uphold these invariants may result in panics during
7474
/// compilation or incorrect runtime behavior in the generated code.
7575
fn inline(
76-
&self,
76+
&mut self,
7777
caller: &ir::Function,
7878
call_inst: ir::Inst,
7979
call_opcode: ir::Opcode,
@@ -82,12 +82,12 @@ pub trait Inline {
8282
) -> InlineCommand<'_>;
8383
}
8484

85-
impl<'a, T> Inline for &'a T
85+
impl<'a, T> Inline for &'a mut T
8686
where
8787
T: Inline,
8888
{
8989
fn inline(
90-
&self,
90+
&mut self,
9191
caller: &ir::Function,
9292
inst: ir::Inst,
9393
opcode: ir::Opcode,
@@ -102,7 +102,12 @@ where
102102
/// instruction, and inline the callee when directed to do so.
103103
///
104104
/// Returns whether any call was inlined.
105-
pub(crate) fn do_inlining(func: &mut ir::Function, inliner: impl Inline) -> CodegenResult<bool> {
105+
pub(crate) fn do_inlining(
106+
func: &mut ir::Function,
107+
mut inliner: impl Inline,
108+
) -> CodegenResult<bool> {
109+
trace!("function {} before inlining: {}", func.name, func);
110+
106111
let mut inlined_any = false;
107112
let mut allocs = InliningAllocs::default();
108113

@@ -175,6 +180,12 @@ pub(crate) fn do_inlining(func: &mut ir::Function, inliner: impl Inline) -> Code
175180
}
176181
}
177182

183+
if inlined_any {
184+
trace!("function {} after inlining: {}", func.name, func);
185+
} else {
186+
trace!("function {} did not have any callees inlined", func.name);
187+
}
188+
178189
Ok(inlined_any)
179190
}
180191

cranelift/codegen/src/ir/dfg.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ impl<'a> Iterator for Values<'a> {
333333
.find(|kv| valid_valuedata(*kv.1))
334334
.map(|kv| kv.0)
335335
}
336+
337+
fn size_hint(&self) -> (usize, Option<usize>) {
338+
self.inner.size_hint()
339+
}
340+
}
341+
342+
impl ExactSizeIterator for Values<'_> {
343+
fn len(&self) -> usize {
344+
self.inner.len()
345+
}
336346
}
337347

338348
/// Handling values.

cranelift/entity/src/primary.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ where
306306
}
307307
}
308308

309+
impl<K, V> From<PrimaryMap<K, V>> for Vec<V>
310+
where
311+
K: EntityRef,
312+
{
313+
fn from(map: PrimaryMap<K, V>) -> Self {
314+
map.elems
315+
}
316+
}
317+
309318
impl<K: EntityRef + fmt::Debug, V: fmt::Debug> fmt::Debug for PrimaryMap<K, V> {
310319
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311320
let mut struct_ = f.debug_struct("PrimaryMap");

cranelift/filetests/src/test_inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct Inliner<'a>(Ref<'a, HashMap<ir::UserFuncName, ir::Function>>);
121121

122122
impl<'a> Inline for Inliner<'a> {
123123
fn inline(
124-
&self,
124+
&mut self,
125125
caller: &ir::Function,
126126
_inst: ir::Inst,
127127
_opcode: ir::Opcode,

crates/cli-flags/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ wasmtime_option_group! {
111111
/// dense.
112112
pub memory_guaranteed_dense_image_size: Option<u64>,
113113

114+
/// Whether to perform function inlining during compilation.
115+
pub compiler_inlining: Option<bool>,
116+
117+
/// Whether to perform function inlining within the same core Wasm
118+
/// module or only for calls from one module into a different
119+
/// module. Only exposed for fuzzing.
120+
#[doc(hidden)]
121+
#[serde(default)]
122+
#[serde(deserialize_with = "crate::opt::cli_parse_wrapper")]
123+
pub compiler_inlining_intra_module: Option<wasmtime::IntraModuleInlining>,
124+
125+
/// Configure the "small-callee" size for function inlining
126+
/// heuristics. Only exposed for fuzzing.
127+
#[doc(hidden)]
128+
pub compiler_inlining_small_callee_size: Option<u32>,
129+
130+
/// Configure the "sum size threshold" for function inlining
131+
/// heuristics. Only exposed for fuzzing.
132+
#[doc(hidden)]
133+
pub compiler_inlining_sum_size_threshold: Option<u32>,
134+
114135
/// The maximum number of WebAssembly instances which can be created
115136
/// with the pooling allocator.
116137
pub pooling_total_core_instances: Option<u32>,
@@ -816,6 +837,18 @@ impl CommonOptions {
816837
if let Some(size) = self.opts.memory_guaranteed_dense_image_size {
817838
config.memory_guaranteed_dense_image_size(size);
818839
}
840+
if let Some(enable) = self.opts.compiler_inlining {
841+
config.compiler_inlining(enable);
842+
}
843+
if let Some(cfg) = self.opts.compiler_inlining_intra_module {
844+
config.compiler_inlining_intra_module(cfg);
845+
}
846+
if let Some(size) = self.opts.compiler_inlining_small_callee_size {
847+
config.compiler_inlining_small_callee_size(size);
848+
}
849+
if let Some(size) = self.opts.compiler_inlining_sum_size_threshold {
850+
config.compiler_inlining_sum_size_threshold(size);
851+
}
819852
if let Some(enable) = self.opts.signals_based_traps {
820853
config.signals_based_traps(enable);
821854
}

crates/cli-flags/src/opt.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ macro_rules! wasmtime_option_group {
2323
pub struct $opts:ident {
2424
$(
2525
$(#[doc = $doc:tt])*
26+
$(#[doc($doc_attr:meta)])?
2627
$(#[serde($serde_attr:meta)])*
2728
pub $opt:ident: $container:ident<$payload:ty>,
2829
)+
@@ -31,6 +32,7 @@ macro_rules! wasmtime_option_group {
3132
#[prefixed = $prefix:tt]
3233
$(#[serde($serde_attr2:meta)])*
3334
$(#[doc = $prefixed_doc:tt])*
35+
$(#[doc($prefixed_doc_attr:meta)])?
3436
pub $prefixed:ident: Vec<(String, Option<String>)>,
3537
)?
3638
}
@@ -43,6 +45,7 @@ macro_rules! wasmtime_option_group {
4345
pub struct $opts {
4446
$(
4547
$(#[serde($serde_attr)])*
48+
$(#[doc($doc_attr)])?
4649
pub $opt: $container<$payload>,
4750
)+
4851
$(
@@ -555,6 +558,30 @@ impl WasmtimeOptionValue for wasmtime::MpkEnabled {
555558
}
556559
}
557560

561+
impl WasmtimeOptionValue for wasmtime::IntraModuleInlining {
562+
const VAL_HELP: &'static str = "[=y|n|gc]";
563+
564+
fn parse(val: Option<&str>) -> Result<Self> {
565+
match val {
566+
None | Some("n") | Some("no") | Some("false") => Ok(wasmtime::IntraModuleInlining::Yes),
567+
Some("y") | Some("yes") | Some("true") => Ok(wasmtime::IntraModuleInlining::No),
568+
Some("gc") => Ok(wasmtime::IntraModuleInlining::WhenUsingGc),
569+
Some(s) => bail!(
570+
"unknown compiler intra-module inlining flag `{s}`, \
571+
only yes,no,gc,<nothing> accepted"
572+
),
573+
}
574+
}
575+
576+
fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
577+
match self {
578+
wasmtime::IntraModuleInlining::Yes => f.write_str("y"),
579+
wasmtime::IntraModuleInlining::No => f.write_str("n"),
580+
wasmtime::IntraModuleInlining::WhenUsingGc => f.write_str("gc"),
581+
}
582+
}
583+
}
584+
558585
impl WasmtimeOptionValue for WasiNnGraph {
559586
const VAL_HELP: &'static str = "=<format>::<dir>";
560587
fn parse(val: Option<&str>) -> Result<Self> {

0 commit comments

Comments
 (0)