Implement AOT compilation for components (#5160)
* Pull `Module` out of `ModuleTextBuilder` This commit is the first in what will likely be a number towards preparing for serializing a compiled component to bytes, a precompiled artifact. To that end my rough plan is to merge all of the compiled artifacts for a component into one large object file instead of having lots of separate object files and lots of separate mmaps to manage. To that end I plan on eventually using `ModuleTextBuilder` to build one large text section for all core wasm modules and trampolines, meaning that `ModuleTextBuilder` is no longer specific to one module. I've extracted out functionality such as function name calculation as well as relocation resolving (now a closure passed in) in preparation for this. For now this just keeps tests passing, and the trajectory for this should become more clear over the following commits. * Remove component-specific object emission This commit removes the `ComponentCompiler::emit_obj` function in favor of `Compiler::emit_obj`, now renamed `append_code`. This involved significantly refactoring code emission to take a flat list of functions into `append_code` and the caller is responsible for weaving together various "families" of functions and un-weaving them afterwards. * Consolidate ELF parsing in `CodeMemory` This commit moves the ELF file parsing and section iteration from `CompiledModule` into `CodeMemory` so one location keeps track of section ranges and such. This is in preparation for sharing much of this code with components which needs all the same sections to get tracked but won't be using `CompiledModule`. A small side benefit from this is that the section parsing done in `CodeMemory` and `CompiledModule` is no longer duplicated. * Remove separately tracked traps in components Previously components would generate an "always trapping" function and the metadata around which pc was allowed to trap was handled manually for components. With recent refactorings the Wasmtime-standard trap section in object files is now being generated for components as well which means that can be reused instead of custom-tracking this metadata. This commit removes the manual tracking for the `always_trap` functions and plumbs the necessary bits around to make components look more like modules. * Remove a now-unnecessary `Arc` in `Module` Not expected to have any measurable impact on performance, but complexity-wise this should make it a bit easier to understand the internals since there's no longer any need to store this somewhere else than its owner's location. * Merge compilation artifacts of components This commit is a large refactoring of the component compilation process to produce a single artifact instead of multiple binary artifacts. The core wasm compilation process is refactored as well to share as much code as necessary with the component compilation process. This method of representing a compiled component necessitated a few medium-sized changes internally within Wasmtime: * A new data structure was created, `CodeObject`, which represents metadata about a single compiled artifact. This is then stored as an `Arc` within a component and a module. For `Module` this is always uniquely owned and represents a shuffling around of data from one owner to another. For a `Component`, however, this is shared amongst all loaded modules and the top-level component. * The "module registry" which is used for symbolicating backtraces and for trap information has been updated to account for a single region of loaded code holding possibly multiple modules. This involved adding a second-level `BTreeMap` for now. This will likely slow down instantiation slightly but if it poses an issue in the future this should be able to be represented with a more clever data structure. This commit additionally solves a number of longstanding issues with components such as compiling only one host-to-wasm trampoline per signature instead of possibly once-per-module. Additionally the `SignatureCollection` registration now happens once-per-component instead of once-per-module-within-a-component. * Fix compile errors from prior commits * Support AOT-compiling components This commit adds support for AOT-compiled components in the same manner as `Module`, specifically adding: * `Engine::precompile_component` * `Component::serialize` * `Component::deserialize` * `Component::deserialize_file` Internally the support for components looks quite similar to `Module`. All the prior commits to this made adding the support here (unsurprisingly) easy. Components are represented as a single object file as are modules, and the functions for each module are all piled into the same object file next to each other (as are areas such as data sections). Support was also added here to quickly differentiate compiled components vs compiled modules via the `e_flags` field in the ELF header. * Prevent serializing exported modules on components The current representation of a module within a component means that the implementation of `Module::serialize` will not work if the module is exported from a component. The reason for this is that `serialize` doesn't actually do anything and simply returns the underlying mmap as a list of bytes. The mmap, however, has `.wasmtime.info` describing component metadata as opposed to this module's metadata. While rewriting this section could be implemented it's not so easy to do so and is otherwise seen as not super important of a feature right now anyway. * Fix windows build * Fix an unused function warning * Update crates/environ/src/compilation.rs Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com> Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ use crate::func_environ::FuncEnvironment;
|
||||
use crate::obj::ModuleTextBuilder;
|
||||
use crate::{
|
||||
blank_sig, func_signature, indirect_signature, value_type, wasmtime_call_conv,
|
||||
CompiledFunction, CompiledFunctions, FunctionAddressMap, Relocation, RelocationTarget,
|
||||
CompiledFunction, FunctionAddressMap, Relocation, RelocationTarget,
|
||||
};
|
||||
use anyhow::{Context as _, Result};
|
||||
use cranelift_codegen::ir::{
|
||||
@@ -18,8 +18,7 @@ use cranelift_codegen::{CompiledCode, MachSrcLoc, MachStackMap};
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
use cranelift_frontend::FunctionBuilder;
|
||||
use cranelift_wasm::{
|
||||
DefinedFuncIndex, FuncIndex, FuncTranslator, MemoryIndex, OwnedMemoryIndex, SignatureIndex,
|
||||
WasmFuncType,
|
||||
DefinedFuncIndex, FuncIndex, FuncTranslator, MemoryIndex, OwnedMemoryIndex, WasmFuncType,
|
||||
};
|
||||
use object::write::{Object, StandardSegment, SymbolId};
|
||||
use object::{RelocationEncoding, RelocationKind, SectionKind};
|
||||
@@ -32,10 +31,9 @@ use std::mem;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use wasmparser::{FuncValidatorAllocations, FunctionBody};
|
||||
use wasmtime_environ::{
|
||||
AddressMapSection, CacheStore, CompileError, FilePos, FlagValue, FunctionBodyData,
|
||||
FunctionInfo, InstructionAddressMap, Module, ModuleTranslation, ModuleTypes, PtrSize,
|
||||
StackMapInformation, Trampoline, TrapCode, TrapEncodingBuilder, TrapInformation, Tunables,
|
||||
VMOffsets,
|
||||
AddressMapSection, CacheStore, CompileError, FilePos, FlagValue, FunctionBodyData, FunctionLoc,
|
||||
InstructionAddressMap, ModuleTranslation, ModuleTypes, PtrSize, StackMapInformation, TrapCode,
|
||||
TrapEncodingBuilder, TrapInformation, Tunables, VMOffsets, WasmFunctionInfo,
|
||||
};
|
||||
|
||||
#[cfg(feature = "component-model")]
|
||||
@@ -189,7 +187,7 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
input: FunctionBodyData<'_>,
|
||||
tunables: &Tunables,
|
||||
types: &ModuleTypes,
|
||||
) -> Result<Box<dyn Any + Send>, CompileError> {
|
||||
) -> Result<(WasmFunctionInfo, Box<dyn Any + Send>), CompileError> {
|
||||
let isa = &*self.isa;
|
||||
let module = &translation.module;
|
||||
let func_index = module.func_index(func_index);
|
||||
@@ -324,22 +322,22 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
validator_allocations: validator.into_allocations(),
|
||||
});
|
||||
|
||||
Ok(Box::new(CompiledFunction {
|
||||
body: code_buf,
|
||||
relocations: func_relocs,
|
||||
value_labels_ranges: ranges.unwrap_or(Default::default()),
|
||||
sized_stack_slots,
|
||||
unwind_info,
|
||||
traps,
|
||||
info: FunctionInfo {
|
||||
Ok((
|
||||
WasmFunctionInfo {
|
||||
start_srcloc: address_transform.start_srcloc,
|
||||
stack_maps,
|
||||
start: 0,
|
||||
length,
|
||||
alignment,
|
||||
stack_maps: stack_maps.into(),
|
||||
},
|
||||
address_map: address_transform,
|
||||
}))
|
||||
Box::new(CompiledFunction {
|
||||
body: code_buf,
|
||||
relocations: func_relocs,
|
||||
value_labels_ranges: ranges.unwrap_or(Default::default()),
|
||||
sized_stack_slots,
|
||||
unwind_info,
|
||||
traps,
|
||||
alignment,
|
||||
address_map: address_transform,
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
fn compile_host_to_wasm_trampoline(
|
||||
@@ -350,75 +348,44 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
.map(|x| Box::new(x) as Box<_>)
|
||||
}
|
||||
|
||||
fn emit_obj(
|
||||
fn append_code(
|
||||
&self,
|
||||
translation: &ModuleTranslation,
|
||||
funcs: PrimaryMap<DefinedFuncIndex, Box<dyn Any + Send>>,
|
||||
compiled_trampolines: Vec<Box<dyn Any + Send>>,
|
||||
tunables: &Tunables,
|
||||
obj: &mut Object<'static>,
|
||||
) -> Result<(PrimaryMap<DefinedFuncIndex, FunctionInfo>, Vec<Trampoline>)> {
|
||||
let funcs: CompiledFunctions = funcs
|
||||
.into_iter()
|
||||
.map(|(_i, f)| *f.downcast().unwrap())
|
||||
.collect();
|
||||
let compiled_trampolines: Vec<CompiledFunction> = compiled_trampolines
|
||||
.into_iter()
|
||||
.map(|f| *f.downcast().unwrap())
|
||||
.collect();
|
||||
|
||||
let mut builder = ModuleTextBuilder::new(obj, &translation.module, &*self.isa);
|
||||
funcs: &[(String, Box<dyn Any + Send>)],
|
||||
tunables: &Tunables,
|
||||
resolve_reloc: &dyn Fn(usize, FuncIndex) -> usize,
|
||||
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
|
||||
let mut builder = ModuleTextBuilder::new(obj, &*self.isa, funcs.len());
|
||||
if self.linkopts.force_jump_veneers {
|
||||
builder.force_veneers();
|
||||
}
|
||||
let mut addrs = AddressMapSection::default();
|
||||
let mut traps = TrapEncodingBuilder::default();
|
||||
|
||||
let mut func_starts = Vec::with_capacity(funcs.len());
|
||||
for (i, func) in funcs.iter() {
|
||||
let range = builder.func(i, func);
|
||||
let mut ret = Vec::with_capacity(funcs.len());
|
||||
for (i, (sym, func)) in funcs.iter().enumerate() {
|
||||
let func = func.downcast_ref().unwrap();
|
||||
let (sym, range) = builder.append_func(&sym, func, |idx| resolve_reloc(i, idx));
|
||||
if tunables.generate_address_map {
|
||||
addrs.push(range.clone(), &func.address_map.instructions);
|
||||
}
|
||||
traps.push(range.clone(), &func.traps);
|
||||
func_starts.push(range.start);
|
||||
builder.append_padding(self.linkopts.padding_between_functions);
|
||||
let info = FunctionLoc {
|
||||
start: u32::try_from(range.start).unwrap(),
|
||||
length: u32::try_from(range.end - range.start).unwrap(),
|
||||
};
|
||||
ret.push((sym, info));
|
||||
}
|
||||
|
||||
// Build trampolines for every signature that can be used by this module.
|
||||
assert_eq!(
|
||||
translation.exported_signatures.len(),
|
||||
compiled_trampolines.len()
|
||||
);
|
||||
let mut trampolines = Vec::with_capacity(translation.exported_signatures.len());
|
||||
for (i, func) in translation
|
||||
.exported_signatures
|
||||
.iter()
|
||||
.zip(&compiled_trampolines)
|
||||
{
|
||||
assert!(func.traps.is_empty());
|
||||
trampolines.push(builder.trampoline(*i, &func));
|
||||
}
|
||||
builder.finish();
|
||||
|
||||
let symbols = builder.finish()?;
|
||||
|
||||
self.append_dwarf(obj, translation, &funcs, tunables, &symbols)?;
|
||||
if tunables.generate_address_map {
|
||||
addrs.append_to(obj);
|
||||
}
|
||||
traps.append_to(obj);
|
||||
|
||||
Ok((
|
||||
funcs
|
||||
.into_iter()
|
||||
.zip(func_starts)
|
||||
.map(|((_, mut f), start)| {
|
||||
f.info.start = start;
|
||||
f.info
|
||||
})
|
||||
.collect(),
|
||||
trampolines,
|
||||
))
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
fn emit_trampoline_obj(
|
||||
@@ -426,14 +393,21 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
ty: &WasmFuncType,
|
||||
host_fn: usize,
|
||||
obj: &mut Object<'static>,
|
||||
) -> Result<(Trampoline, Trampoline)> {
|
||||
) -> Result<(FunctionLoc, FunctionLoc)> {
|
||||
let host_to_wasm = self.host_to_wasm_trampoline(ty)?;
|
||||
let wasm_to_host = self.wasm_to_host_trampoline(ty, host_fn)?;
|
||||
let module = Module::new();
|
||||
let mut builder = ModuleTextBuilder::new(obj, &module, &*self.isa);
|
||||
let a = builder.trampoline(SignatureIndex::new(0), &host_to_wasm);
|
||||
let b = builder.trampoline(SignatureIndex::new(1), &wasm_to_host);
|
||||
builder.finish()?;
|
||||
let mut builder = ModuleTextBuilder::new(obj, &*self.isa, 2);
|
||||
let (_, a) = builder.append_func("host_to_wasm", &host_to_wasm, |_| unreachable!());
|
||||
let (_, b) = builder.append_func("wasm_to_host", &wasm_to_host, |_| unreachable!());
|
||||
let a = FunctionLoc {
|
||||
start: u32::try_from(a.start).unwrap(),
|
||||
length: u32::try_from(a.end - a.start).unwrap(),
|
||||
};
|
||||
let b = FunctionLoc {
|
||||
start: u32::try_from(b.start).unwrap(),
|
||||
length: u32::try_from(b.end - b.start).unwrap(),
|
||||
};
|
||||
builder.finish();
|
||||
Ok((a, b))
|
||||
}
|
||||
|
||||
@@ -469,6 +443,92 @@ impl wasmtime_environ::Compiler for Compiler {
|
||||
fn component_compiler(&self) -> &dyn wasmtime_environ::component::ComponentCompiler {
|
||||
self
|
||||
}
|
||||
|
||||
fn append_dwarf(
|
||||
&self,
|
||||
obj: &mut Object<'_>,
|
||||
translation: &ModuleTranslation<'_>,
|
||||
funcs: &PrimaryMap<DefinedFuncIndex, (SymbolId, &(dyn Any + Send))>,
|
||||
) -> Result<()> {
|
||||
let ofs = VMOffsets::new(
|
||||
self.isa
|
||||
.triple()
|
||||
.architecture
|
||||
.pointer_width()
|
||||
.unwrap()
|
||||
.bytes(),
|
||||
&translation.module,
|
||||
);
|
||||
|
||||
let memory_offset = if ofs.num_imported_memories > 0 {
|
||||
ModuleMemoryOffset::Imported(ofs.vmctx_vmmemory_import(MemoryIndex::new(0)))
|
||||
} else if ofs.num_defined_memories > 0 {
|
||||
// The addition of shared memory makes the following assumption,
|
||||
// "owned memory index = 0", possibly false. If the first memory
|
||||
// is a shared memory, the base pointer will not be stored in
|
||||
// the `owned_memories` array. The following code should
|
||||
// eventually be fixed to not only handle shared memories but
|
||||
// also multiple memories.
|
||||
assert_eq!(
|
||||
ofs.num_defined_memories, ofs.num_owned_memories,
|
||||
"the memory base pointer may be incorrect due to sharing memory"
|
||||
);
|
||||
ModuleMemoryOffset::Defined(
|
||||
ofs.vmctx_vmmemory_definition_base(OwnedMemoryIndex::new(0)),
|
||||
)
|
||||
} else {
|
||||
ModuleMemoryOffset::None
|
||||
};
|
||||
let compiled_funcs = funcs
|
||||
.iter()
|
||||
.map(|(_, (_, func))| func.downcast_ref().unwrap())
|
||||
.collect();
|
||||
let dwarf_sections = crate::debug::emit_dwarf(
|
||||
&*self.isa,
|
||||
&translation.debuginfo,
|
||||
&compiled_funcs,
|
||||
&memory_offset,
|
||||
)
|
||||
.with_context(|| "failed to emit DWARF debug information")?;
|
||||
|
||||
let (debug_bodies, debug_relocs): (Vec<_>, Vec<_>) = dwarf_sections
|
||||
.iter()
|
||||
.map(|s| ((s.name, &s.body), (s.name, &s.relocs)))
|
||||
.unzip();
|
||||
let mut dwarf_sections_ids = HashMap::new();
|
||||
for (name, body) in debug_bodies {
|
||||
let segment = obj.segment_name(StandardSegment::Debug).to_vec();
|
||||
let section_id = obj.add_section(segment, name.as_bytes().to_vec(), SectionKind::Debug);
|
||||
dwarf_sections_ids.insert(name, section_id);
|
||||
obj.append_section_data(section_id, &body, 1);
|
||||
}
|
||||
|
||||
// Write all debug data relocations.
|
||||
for (name, relocs) in debug_relocs {
|
||||
let section_id = *dwarf_sections_ids.get(name).unwrap();
|
||||
for reloc in relocs {
|
||||
let target_symbol = match reloc.target {
|
||||
DwarfSectionRelocTarget::Func(index) => funcs[DefinedFuncIndex::new(index)].0,
|
||||
DwarfSectionRelocTarget::Section(name) => {
|
||||
obj.section_symbol(dwarf_sections_ids[name])
|
||||
}
|
||||
};
|
||||
obj.add_relocation(
|
||||
section_id,
|
||||
object::write::Relocation {
|
||||
offset: u64::from(reloc.offset),
|
||||
size: reloc.size << 3,
|
||||
kind: RelocationKind::Absolute,
|
||||
encoding: RelocationEncoding::Generic,
|
||||
symbol: target_symbol,
|
||||
addend: i64::from(reloc.addend),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "incremental-cache")]
|
||||
@@ -826,6 +886,7 @@ impl Compiler {
|
||||
.into_iter()
|
||||
.map(mach_trap_to_trap)
|
||||
.collect();
|
||||
let alignment = compiled_code.alignment;
|
||||
|
||||
let unwind_info = if isa.flags().unwind_info() {
|
||||
compiled_code
|
||||
@@ -841,96 +902,11 @@ impl Compiler {
|
||||
relocations: Default::default(),
|
||||
sized_stack_slots: Default::default(),
|
||||
value_labels_ranges: Default::default(),
|
||||
info: Default::default(),
|
||||
address_map: Default::default(),
|
||||
traps,
|
||||
alignment,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn append_dwarf(
|
||||
&self,
|
||||
obj: &mut Object<'_>,
|
||||
translation: &ModuleTranslation<'_>,
|
||||
funcs: &CompiledFunctions,
|
||||
tunables: &Tunables,
|
||||
func_symbols: &PrimaryMap<DefinedFuncIndex, SymbolId>,
|
||||
) -> Result<()> {
|
||||
if !tunables.generate_native_debuginfo || funcs.len() == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let ofs = VMOffsets::new(
|
||||
self.isa
|
||||
.triple()
|
||||
.architecture
|
||||
.pointer_width()
|
||||
.unwrap()
|
||||
.bytes(),
|
||||
&translation.module,
|
||||
);
|
||||
|
||||
let memory_offset = if ofs.num_imported_memories > 0 {
|
||||
ModuleMemoryOffset::Imported(ofs.vmctx_vmmemory_import(MemoryIndex::new(0)))
|
||||
} else if ofs.num_defined_memories > 0 {
|
||||
// The addition of shared memory makes the following assumption,
|
||||
// "owned memory index = 0", possibly false. If the first memory
|
||||
// is a shared memory, the base pointer will not be stored in
|
||||
// the `owned_memories` array. The following code should
|
||||
// eventually be fixed to not only handle shared memories but
|
||||
// also multiple memories.
|
||||
assert_eq!(
|
||||
ofs.num_defined_memories, ofs.num_owned_memories,
|
||||
"the memory base pointer may be incorrect due to sharing memory"
|
||||
);
|
||||
ModuleMemoryOffset::Defined(
|
||||
ofs.vmctx_vmmemory_definition_base(OwnedMemoryIndex::new(0)),
|
||||
)
|
||||
} else {
|
||||
ModuleMemoryOffset::None
|
||||
};
|
||||
let dwarf_sections =
|
||||
crate::debug::emit_dwarf(&*self.isa, &translation.debuginfo, &funcs, &memory_offset)
|
||||
.with_context(|| "failed to emit DWARF debug information")?;
|
||||
|
||||
let (debug_bodies, debug_relocs): (Vec<_>, Vec<_>) = dwarf_sections
|
||||
.iter()
|
||||
.map(|s| ((s.name, &s.body), (s.name, &s.relocs)))
|
||||
.unzip();
|
||||
let mut dwarf_sections_ids = HashMap::new();
|
||||
for (name, body) in debug_bodies {
|
||||
let segment = obj.segment_name(StandardSegment::Debug).to_vec();
|
||||
let section_id = obj.add_section(segment, name.as_bytes().to_vec(), SectionKind::Debug);
|
||||
dwarf_sections_ids.insert(name, section_id);
|
||||
obj.append_section_data(section_id, &body, 1);
|
||||
}
|
||||
|
||||
// Write all debug data relocations.
|
||||
for (name, relocs) in debug_relocs {
|
||||
let section_id = *dwarf_sections_ids.get(name).unwrap();
|
||||
for reloc in relocs {
|
||||
let target_symbol = match reloc.target {
|
||||
DwarfSectionRelocTarget::Func(index) => {
|
||||
func_symbols[DefinedFuncIndex::new(index)]
|
||||
}
|
||||
DwarfSectionRelocTarget::Section(name) => {
|
||||
obj.section_symbol(dwarf_sections_ids[name])
|
||||
}
|
||||
};
|
||||
obj.add_relocation(
|
||||
section_id,
|
||||
object::write::Relocation {
|
||||
offset: u64::from(reloc.offset),
|
||||
size: reloc.size << 3,
|
||||
kind: RelocationKind::Absolute,
|
||||
encoding: RelocationEncoding::Generic,
|
||||
symbol: target_symbol,
|
||||
addend: i64::from(reloc.addend),
|
||||
},
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// Collects an iterator of `InstructionAddressMap` into a `Vec` for insertion
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
//! Compilation support for the component model.
|
||||
|
||||
use crate::compiler::{Compiler, CompilerContext};
|
||||
use crate::obj::ModuleTextBuilder;
|
||||
use crate::CompiledFunction;
|
||||
use anyhow::Result;
|
||||
use cranelift_codegen::ir::{self, InstBuilder, MemFlags};
|
||||
use cranelift_frontend::FunctionBuilder;
|
||||
use object::write::Object;
|
||||
use std::any::Any;
|
||||
use std::ops::Range;
|
||||
use wasmtime_environ::component::{
|
||||
AlwaysTrapInfo, CanonicalOptions, Component, ComponentCompiler, ComponentTypes, FixedEncoding,
|
||||
FunctionInfo, LowerImport, LoweredIndex, RuntimeAlwaysTrapIndex, RuntimeMemoryIndex,
|
||||
RuntimeTranscoderIndex, Transcode, Transcoder, VMComponentOffsets,
|
||||
CanonicalOptions, Component, ComponentCompiler, ComponentTypes, FixedEncoding, LowerImport,
|
||||
RuntimeMemoryIndex, Transcode, Transcoder, VMComponentOffsets,
|
||||
};
|
||||
use wasmtime_environ::{PrimaryMap, PtrSize, SignatureIndex, Trampoline, TrapCode, WasmFuncType};
|
||||
use wasmtime_environ::{PtrSize, WasmFuncType};
|
||||
|
||||
impl ComponentCompiler for Compiler {
|
||||
fn compile_lowered_trampoline(
|
||||
@@ -235,82 +231,6 @@ impl ComponentCompiler for Compiler {
|
||||
});
|
||||
Ok(Box::new(func))
|
||||
}
|
||||
|
||||
fn emit_obj(
|
||||
&self,
|
||||
lowerings: PrimaryMap<LoweredIndex, Box<dyn Any + Send>>,
|
||||
always_trap: PrimaryMap<RuntimeAlwaysTrapIndex, Box<dyn Any + Send>>,
|
||||
transcoders: PrimaryMap<RuntimeTranscoderIndex, Box<dyn Any + Send>>,
|
||||
trampolines: Vec<(SignatureIndex, Box<dyn Any + Send>)>,
|
||||
obj: &mut Object<'static>,
|
||||
) -> Result<(
|
||||
PrimaryMap<LoweredIndex, FunctionInfo>,
|
||||
PrimaryMap<RuntimeAlwaysTrapIndex, AlwaysTrapInfo>,
|
||||
PrimaryMap<RuntimeTranscoderIndex, FunctionInfo>,
|
||||
Vec<Trampoline>,
|
||||
)> {
|
||||
let module = Default::default();
|
||||
let mut text = ModuleTextBuilder::new(obj, &module, &*self.isa);
|
||||
|
||||
let range2info = |range: Range<u64>| FunctionInfo {
|
||||
start: u32::try_from(range.start).unwrap(),
|
||||
length: u32::try_from(range.end - range.start).unwrap(),
|
||||
};
|
||||
let ret_lowerings = lowerings
|
||||
.iter()
|
||||
.map(|(i, lowering)| {
|
||||
let lowering = lowering.downcast_ref::<CompiledFunction>().unwrap();
|
||||
assert!(lowering.traps.is_empty());
|
||||
let range = text.named_func(
|
||||
&format!("_wasm_component_lowering_trampoline{}", i.as_u32()),
|
||||
&lowering,
|
||||
);
|
||||
range2info(range)
|
||||
})
|
||||
.collect();
|
||||
let ret_always_trap = always_trap
|
||||
.iter()
|
||||
.map(|(i, func)| {
|
||||
let func = func.downcast_ref::<CompiledFunction>().unwrap();
|
||||
assert_eq!(func.traps.len(), 1);
|
||||
assert_eq!(func.traps[0].trap_code, TrapCode::AlwaysTrapAdapter);
|
||||
let name = format!("_wasmtime_always_trap{}", i.as_u32());
|
||||
let range = text.named_func(&name, func);
|
||||
AlwaysTrapInfo {
|
||||
info: range2info(range),
|
||||
trap_offset: func.traps[0].code_offset,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let ret_transcoders = transcoders
|
||||
.iter()
|
||||
.map(|(i, func)| {
|
||||
let func = func.downcast_ref::<CompiledFunction>().unwrap();
|
||||
let name = format!("_wasmtime_transcoder{}", i.as_u32());
|
||||
let range = text.named_func(&name, func);
|
||||
range2info(range)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let ret_trampolines = trampolines
|
||||
.iter()
|
||||
.map(|(i, func)| {
|
||||
let func = func.downcast_ref::<CompiledFunction>().unwrap();
|
||||
assert!(func.traps.is_empty());
|
||||
text.trampoline(*i, func)
|
||||
})
|
||||
.collect();
|
||||
|
||||
text.finish()?;
|
||||
|
||||
Ok((
|
||||
ret_lowerings,
|
||||
ret_always_trap,
|
||||
ret_transcoders,
|
||||
ret_trampolines,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
|
||||
@@ -605,7 +605,7 @@ impl AddressTransform {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{build_function_lookup, get_wasm_code_offset, AddressTransform};
|
||||
use crate::{CompiledFunction, CompiledFunctions, FunctionAddressMap};
|
||||
use crate::{CompiledFunction, FunctionAddressMap};
|
||||
use cranelift_entity::PrimaryMap;
|
||||
use gimli::write::Address;
|
||||
use std::iter::FromIterator;
|
||||
@@ -650,13 +650,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_simple_module(address_map: FunctionAddressMap) -> CompiledFunctions {
|
||||
PrimaryMap::from_iter(vec![CompiledFunction {
|
||||
address_map,
|
||||
..Default::default()
|
||||
}])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_build_function_lookup_simple() {
|
||||
let input = create_simple_func(11);
|
||||
@@ -735,7 +728,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_addr_translate() {
|
||||
let input = create_simple_module(create_simple_func(11));
|
||||
let func = CompiledFunction {
|
||||
address_map: create_simple_func(11),
|
||||
..Default::default()
|
||||
};
|
||||
let input = PrimaryMap::from_iter([&func]);
|
||||
let at = AddressTransform::new(
|
||||
&input,
|
||||
&WasmFileInfo {
|
||||
|
||||
@@ -1118,7 +1118,7 @@ mod tests {
|
||||
use wasmtime_environ::WasmFileInfo;
|
||||
let mut module_map = PrimaryMap::new();
|
||||
let code_section_offset: u32 = 100;
|
||||
module_map.push(CompiledFunction {
|
||||
let func = CompiledFunction {
|
||||
address_map: FunctionAddressMap {
|
||||
instructions: vec![
|
||||
InstructionAddressMap {
|
||||
@@ -1145,7 +1145,8 @@ mod tests {
|
||||
body_len: 30,
|
||||
},
|
||||
..Default::default()
|
||||
});
|
||||
};
|
||||
module_map.push(&func);
|
||||
let fi = WasmFileInfo {
|
||||
code_section_offset: code_section_offset.into(),
|
||||
funcs: Vec::new(),
|
||||
|
||||
@@ -10,7 +10,7 @@ use cranelift_entity::PrimaryMap;
|
||||
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, WasmFuncType, WasmType};
|
||||
use target_lexicon::{Architecture, CallingConvention};
|
||||
use wasmtime_environ::{
|
||||
FilePos, FunctionInfo, InstructionAddressMap, ModuleTranslation, ModuleTypes, TrapInformation,
|
||||
FilePos, InstructionAddressMap, ModuleTranslation, ModuleTypes, TrapInformation,
|
||||
};
|
||||
|
||||
pub use builder::builder;
|
||||
@@ -21,7 +21,7 @@ mod debug;
|
||||
mod func_environ;
|
||||
mod obj;
|
||||
|
||||
type CompiledFunctions = PrimaryMap<DefinedFuncIndex, CompiledFunction>;
|
||||
type CompiledFunctions<'a> = PrimaryMap<DefinedFuncIndex, &'a CompiledFunction>;
|
||||
|
||||
/// Compiled function: machine code body, jump table offsets, and unwind information.
|
||||
#[derive(Default)]
|
||||
@@ -43,9 +43,7 @@ pub struct CompiledFunction {
|
||||
relocations: Vec<Relocation>,
|
||||
value_labels_ranges: cranelift_codegen::ValueLabelsRanges,
|
||||
sized_stack_slots: ir::StackSlots,
|
||||
|
||||
// TODO: Add dynamic_stack_slots?
|
||||
info: FunctionInfo,
|
||||
alignment: u32,
|
||||
}
|
||||
|
||||
/// Function and its instructions addresses mappings.
|
||||
|
||||
@@ -26,8 +26,7 @@ use object::write::{Object, SectionId, StandardSegment, Symbol, SymbolId, Symbol
|
||||
use object::{Architecture, SectionKind, SymbolFlags, SymbolKind, SymbolScope};
|
||||
use std::convert::TryFrom;
|
||||
use std::ops::Range;
|
||||
use wasmtime_environ::obj;
|
||||
use wasmtime_environ::{DefinedFuncIndex, Module, PrimaryMap, SignatureIndex, Trampoline};
|
||||
use wasmtime_environ::FuncIndex;
|
||||
|
||||
const TEXT_SECTION_NAME: &[u8] = b".text";
|
||||
|
||||
@@ -46,24 +45,23 @@ pub struct ModuleTextBuilder<'a> {
|
||||
obj: &'a mut Object<'static>,
|
||||
|
||||
/// The WebAssembly module we're generating code for.
|
||||
module: &'a Module,
|
||||
|
||||
text_section: SectionId,
|
||||
|
||||
unwind_info: UnwindInfoBuilder<'a>,
|
||||
|
||||
/// The corresponding symbol for each function, inserted as they're defined.
|
||||
///
|
||||
/// If an index isn't here yet then it hasn't been defined yet.
|
||||
func_symbols: PrimaryMap<DefinedFuncIndex, SymbolId>,
|
||||
|
||||
/// In-progress text section that we're using cranelift's `MachBuffer` to
|
||||
/// build to resolve relocations (calls) between functions.
|
||||
text: Box<dyn TextSectionBuilder>,
|
||||
}
|
||||
|
||||
impl<'a> ModuleTextBuilder<'a> {
|
||||
pub fn new(obj: &'a mut Object<'static>, module: &'a Module, isa: &'a dyn TargetIsa) -> Self {
|
||||
/// Creates a new builder for the text section of an executable.
|
||||
///
|
||||
/// The `.text` section will be appended to the specified `obj` along with
|
||||
/// any unwinding or such information as necessary. The `num_funcs`
|
||||
/// parameter indicates the number of times the `append_func` function will
|
||||
/// be called. The `finish` function will panic if this contract is not met.
|
||||
pub fn new(obj: &'a mut Object<'static>, isa: &'a dyn TargetIsa, num_funcs: usize) -> Self {
|
||||
// Entire code (functions and trampolines) will be placed
|
||||
// in the ".text" section.
|
||||
let text_section = obj.add_section(
|
||||
@@ -72,37 +70,40 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
SectionKind::Text,
|
||||
);
|
||||
|
||||
let num_defined = module.functions.len() - module.num_imported_funcs;
|
||||
Self {
|
||||
isa,
|
||||
obj,
|
||||
module,
|
||||
text_section,
|
||||
func_symbols: PrimaryMap::with_capacity(num_defined),
|
||||
unwind_info: Default::default(),
|
||||
text: isa.text_section_builder(num_defined as u32),
|
||||
text: isa.text_section_builder(num_funcs),
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends the `func` specified named `name` to this object.
|
||||
///
|
||||
/// The `resolve_reloc_target` closure is used to resolve a relocation
|
||||
/// target to an adjacent function which has already been added or will be
|
||||
/// added to this object. The argument is the relocation target specified
|
||||
/// within `CompiledFunction` and the return value must be an index where
|
||||
/// the target will be defined by the `n`th call to `append_func`.
|
||||
///
|
||||
/// Returns the symbol associated with the function as well as the range
|
||||
/// that the function resides within the text section.
|
||||
pub fn append_func(
|
||||
&mut self,
|
||||
labeled: bool,
|
||||
name: Vec<u8>,
|
||||
name: &str,
|
||||
func: &'a CompiledFunction,
|
||||
resolve_reloc_target: impl Fn(FuncIndex) -> usize,
|
||||
) -> (SymbolId, Range<u64>) {
|
||||
let body_len = func.body.len() as u64;
|
||||
let off = self.text.append(
|
||||
labeled,
|
||||
true,
|
||||
&func.body,
|
||||
self.isa.function_alignment().max(func.info.alignment),
|
||||
self.isa.function_alignment().max(func.alignment),
|
||||
);
|
||||
|
||||
let symbol_id = self.obj.add_symbol(Symbol {
|
||||
name,
|
||||
name: name.as_bytes().to_vec(),
|
||||
value: off,
|
||||
size: body_len,
|
||||
kind: SymbolKind::Text,
|
||||
@@ -125,13 +126,11 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
// file, but if it can't handle it then we pass through the
|
||||
// relocation.
|
||||
RelocationTarget::UserFunc(index) => {
|
||||
let defined_index = self.module.defined_func_index(index).unwrap();
|
||||
if self.text.resolve_reloc(
|
||||
off + u64::from(r.offset),
|
||||
r.reloc,
|
||||
r.addend,
|
||||
defined_index.as_u32(),
|
||||
) {
|
||||
let target = resolve_reloc_target(index);
|
||||
if self
|
||||
.text
|
||||
.resolve_reloc(off + u64::from(r.offset), r.reloc, r.addend, target)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -160,31 +159,6 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
(symbol_id, off..off + body_len)
|
||||
}
|
||||
|
||||
/// Appends a function to this object file.
|
||||
///
|
||||
/// This is expected to be called in-order for ascending `index` values.
|
||||
pub fn func(&mut self, index: DefinedFuncIndex, func: &'a CompiledFunction) -> Range<u64> {
|
||||
let name = obj::func_symbol_name(self.module.func_index(index));
|
||||
let (symbol_id, range) = self.append_func(true, name.into_bytes(), func);
|
||||
assert_eq!(self.func_symbols.push(symbol_id), index);
|
||||
range
|
||||
}
|
||||
|
||||
pub fn trampoline(&mut self, sig: SignatureIndex, func: &'a CompiledFunction) -> Trampoline {
|
||||
let name = obj::trampoline_symbol_name(sig);
|
||||
let range = self.named_func(&name, func);
|
||||
Trampoline {
|
||||
signature: sig,
|
||||
start: range.start,
|
||||
length: u32::try_from(range.end - range.start).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn named_func(&mut self, name: &str, func: &'a CompiledFunction) -> Range<u64> {
|
||||
let (_, range) = self.append_func(false, name.as_bytes().to_vec(), func);
|
||||
range
|
||||
}
|
||||
|
||||
/// Forces "veneers" to be used for inter-function calls in the text
|
||||
/// section which means that in-bounds optimized addresses are never used.
|
||||
///
|
||||
@@ -210,7 +184,7 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
///
|
||||
/// Note that this will also write out the unwind information sections if
|
||||
/// necessary.
|
||||
pub fn finish(mut self) -> Result<PrimaryMap<DefinedFuncIndex, SymbolId>> {
|
||||
pub fn finish(mut self) {
|
||||
// Finish up the text section now that we're done adding functions.
|
||||
let text = self.text.finish();
|
||||
self.obj
|
||||
@@ -220,8 +194,6 @@ impl<'a> ModuleTextBuilder<'a> {
|
||||
// Append the unwind information for all our functions, if necessary.
|
||||
self.unwind_info
|
||||
.append_section(self.isa, self.obj, self.text_section);
|
||||
|
||||
Ok(self.func_symbols)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user