Skip to content

Commit b73f38f

Browse files
fitzgenbongjunj
authored andcommitted
Wasmtime: Add (optional) bottom-up function inlining to Wasm compilation (bytecodealliance#11283)
* 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. * Fix dead code warning when not `cfg(feature = "component-model")` * fix winch trampoline compilation * Move CLI options to codegen * Move parameters into struct * Use an index set for call-graph construction * Smuggle inlining heuristic options through cranelift flags * Remove old CLI flags * set tunables before settings * Only configure inlining options for cranelift in fuzzing
1 parent 06f2fe3 commit b73f38f

File tree

23 files changed

+1271
-82
lines changed

23 files changed

+1271
-82
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ wasmtime_option_group! {
237237
/// object files.
238238
pub native_unwind_info: Option<bool>,
239239

240+
/// Whether to perform function inlining during compilation.
241+
pub inlining: Option<bool>,
242+
240243
#[prefixed = "cranelift"]
241244
#[serde(default)]
242245
/// Set a cranelift-specific option. Use `wasmtime settings` to see
@@ -822,6 +825,9 @@ impl CommonOptions {
822825
if let Some(enable) = self.codegen.native_unwind_info {
823826
config.native_unwind_info(enable);
824827
}
828+
if let Some(enable) = self.codegen.inlining {
829+
config.compiler_inlining(enable);
830+
}
825831

826832
// async_stack_size enabled by either async or stack-switching, so
827833
// cannot directly use match_feature!

crates/cli-flags/src/opt.rs

Lines changed: 3 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
$(

crates/cranelift/src/builder.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,27 @@ impl CompilerBuilder for Builder {
6565

6666
fn set(&mut self, name: &str, value: &str) -> Result<()> {
6767
// Special wasmtime-cranelift-only settings first
68-
if name == "wasmtime_linkopt_padding_between_functions" {
69-
self.linkopts.padding_between_functions = value.parse()?;
70-
return Ok(());
68+
match name {
69+
"wasmtime_linkopt_padding_between_functions" => {
70+
self.linkopts.padding_between_functions = value.parse()?;
71+
}
72+
"wasmtime_linkopt_force_jump_veneer" => {
73+
self.linkopts.force_jump_veneers = value.parse()?;
74+
}
75+
"wasmtime_inlining_intra_module" => {
76+
self.tunables.as_mut().unwrap().inlining_intra_module = value.parse()?;
77+
}
78+
"wasmtime_inlining_small_callee_size" => {
79+
self.tunables.as_mut().unwrap().inlining_small_callee_size = value.parse()?;
80+
}
81+
"wasmtime_inlining_sum_size_threshold" => {
82+
self.tunables.as_mut().unwrap().inlining_sum_size_threshold = value.parse()?;
83+
}
84+
_ => {
85+
self.inner.set(name, value)?;
86+
}
7187
}
72-
if name == "wasmtime_linkopt_force_jump_veneer" {
73-
self.linkopts.force_jump_veneers = value.parse()?;
74-
return Ok(());
75-
}
76-
77-
self.inner.set(name, value)
88+
Ok(())
7889
}
7990

8091
fn enable(&mut self, name: &str) -> Result<()> {

0 commit comments

Comments
 (0)