Implement lowered-then-lifted functions (#4327)

* Implement lowered-then-lifted functions

This commit is a few features bundled into one, culminating in the
implementation of lowered-then-lifted functions for the component model.
It's probably not going to be used all that often but this is possible
within a valid component so Wasmtime needs to do something relatively
reasonable. The main things implemented in this commit are:

* Component instances are now assigned a `RuntimeComponentInstanceIndex`
  to differentiate each one. This will be used in the future to detect
  fusion (one instance lowering a function from another instance). For
  now it's used to allocate separate `VMComponentFlags` for each
  internal component instance.

* The `CoreExport<FuncIndex>` of lowered functions was changed to a
  `CoreDef` since technically a lowered function can use another lowered
  function as the callee. This ended up being not too difficult to plumb
  through as everything else was already in place.

* A need arose to compile host-to-wasm trampolines which weren't already
  present. Currently wasm in a component is always entered through a
  host-to-wasm trampoline but core wasm modules are the source of all
  the trampolines. In the case of a lowered-then-lifted function there
  may not actually be any core wasm modules, so component objects now
  contain necessary trampolines not otherwise provided by the core wasm
  objects. This feature required splitting a new function into the
  `Compiler` trait for creating a host-to-wasm trampoline. After doing
  this core wasm compilation was also updated to leverage this which
  further enabled compiling trampolines in parallel as opposed to the
  previous synchronous compilation.

* Review comments
This commit is contained in:
Alex Crichton
2022-06-28 13:50:08 -05:00
committed by GitHub
parent df1502531d
commit c1b3962f7b
17 changed files with 400 additions and 107 deletions

View File

@@ -1,5 +1,5 @@
use crate::component::{Component, ComponentTypes, LowerImport, LoweredIndex};
use crate::PrimaryMap;
use crate::{PrimaryMap, SignatureIndex, Trampoline};
use anyhow::Result;
use object::write::Object;
use serde::{Deserialize, Serialize};
@@ -8,7 +8,7 @@ use std::any::Any;
/// Description of where a trampoline is located in the text section of a
/// compiled image.
#[derive(Serialize, Deserialize)]
pub struct TrampolineInfo {
pub struct LoweringInfo {
/// The byte offset from the start of the text section where this trampoline
/// starts.
pub start: u32,
@@ -42,8 +42,8 @@ pub trait ComponentCompiler: Send + Sync {
types: &ComponentTypes,
) -> Result<Box<dyn Any + Send>>;
/// Emits the `trampolines` specified into the in-progress ELF object
/// specified by `obj`.
/// Emits the `lowerings` and `trampolines` specified into the in-progress
/// ELF object specified by `obj`.
///
/// Returns a map of trampoline information for where to find them all in
/// the text section.
@@ -52,7 +52,8 @@ pub trait ComponentCompiler: Send + Sync {
/// trampolines as necessary.
fn emit_obj(
&self,
trampolines: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>,
lowerings: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>,
tramplines: Vec<(SignatureIndex, Box<dyn Any + Send>)>,
obj: &mut Object<'static>,
) -> Result<PrimaryMap<LoweredIndex, TrampolineInfo>>;
) -> Result<(PrimaryMap<LoweredIndex, LoweringInfo>, Vec<Trampoline>)>;
}