Skip to content

Commit

Permalink
Merge #3290
Browse files Browse the repository at this point in the history
3290: Limit the use of clone when handling Compilation object r=ptitSeb a=ptitSeb

# Description
Small improvment to limit the number of clone when handling Compilation object.

Based on near/wasmer#140

For #3305 

Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
  • Loading branch information
bors[bot] and ptitSeb authored Nov 21, 2022
2 parents f503052 + 2a6d5e7 commit d4d2d10
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 149 deletions.
8 changes: 4 additions & 4 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ impl Compiler for CraneliftCompiler {
.into_iter()
.collect::<PrimaryMap<FunctionIndex, FunctionBody>>();

Ok(Compilation::new(
functions.into_iter().collect(),
Ok(Compilation {
functions: functions.into_iter().collect(),
custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
dwarf,
))
debug: dwarf,
})
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,12 @@ impl Compiler for LLVMCompiler {
.into_iter()
.collect::<PrimaryMap<_, _>>();

Ok(Compilation::new(
Ok(Compilation {
functions,
module_custom_sections,
custom_sections: module_custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
dwarf,
))
debug: dwarf,
})
}
}
8 changes: 4 additions & 4 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ impl Compiler for SinglepassCompiler {
#[cfg(not(feature = "unwind"))]
let dwarf = None;

Ok(Compilation::new(
functions.into_iter().collect(),
Ok(Compilation {
functions: functions.into_iter().collect(),
custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
dwarf,
))
debug: dwarf,
})
}
}

Expand Down
32 changes: 20 additions & 12 deletions lib/compiler/src/artifact_builders/artifact_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ impl ArtifactBuild {
translation.module_translation_state.as_ref().unwrap(),
translation.function_body_inputs,
)?;
let function_call_trampolines = compilation.get_function_call_trampolines();
let dynamic_function_trampolines = compilation.get_dynamic_function_trampolines();

let data_initializers = translation
.data_initializers
Expand All @@ -85,25 +83,35 @@ impl ArtifactBuild {
.collect::<Vec<_>>()
.into_boxed_slice();

let frame_infos = compilation.get_frame_info();

// Synthesize a custom section to hold the libcall trampolines.
let mut custom_sections = compilation.get_custom_sections();
let mut custom_section_relocations = compilation.get_custom_section_relocations();
let mut function_frame_info = PrimaryMap::with_capacity(compilation.functions.len());
let mut function_bodies = PrimaryMap::with_capacity(compilation.functions.len());
let mut function_relocations = PrimaryMap::with_capacity(compilation.functions.len());
for (_, func) in compilation.functions.into_iter() {
function_bodies.push(func.body);
function_relocations.push(func.relocations);
function_frame_info.push(func.frame_info);
}
let mut custom_sections = compilation.custom_sections.clone();
let mut custom_section_relocations = compilation
.custom_sections
.iter()
.map(|(_, section)| section.relocations.clone())
.collect::<PrimaryMap<SectionIndex, _>>();
let libcall_trampolines_section = make_libcall_trampolines(target);
custom_section_relocations.push(libcall_trampolines_section.relocations.clone());
let libcall_trampolines = custom_sections.push(libcall_trampolines_section);
let libcall_trampoline_len = libcall_trampoline_len(target) as u32;

let serializable_compilation = SerializableCompilation {
function_bodies: compilation.get_function_bodies(),
function_relocations: compilation.get_relocations(),
function_frame_info: frame_infos,
function_call_trampolines,
dynamic_function_trampolines,
function_bodies,
function_relocations,
function_frame_info,
function_call_trampolines: compilation.function_call_trampolines,
dynamic_function_trampolines: compilation.dynamic_function_trampolines,
custom_sections,
custom_section_relocations,
debug: compilation.get_debug(),
debug: compilation.debug,
libcall_trampolines,
libcall_trampoline_len,
};
Expand Down
26 changes: 16 additions & 10 deletions lib/object/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,19 @@ pub fn emit_compilation(
symbol_registry: &impl SymbolRegistry,
triple: &Triple,
) -> Result<(), ObjectError> {
let function_bodies = compilation.get_function_bodies();
let function_relocations = compilation.get_relocations();
let custom_sections = compilation.get_custom_sections();
let custom_section_relocations = compilation.get_custom_section_relocations();
let function_call_trampolines = compilation.get_function_call_trampolines();
let dynamic_function_trampolines = compilation.get_dynamic_function_trampolines();
let mut function_bodies = PrimaryMap::with_capacity(compilation.functions.len());
let mut function_relocations = PrimaryMap::with_capacity(compilation.functions.len());
for (_, func) in compilation.functions.into_iter() {
function_bodies.push(func.body);
function_relocations.push(func.relocations);
}
let custom_section_relocations = compilation
.custom_sections
.iter()
.map(|(_, section)| section.relocations.clone())
.collect::<PrimaryMap<SectionIndex, _>>();

let debug_index = compilation.get_debug().map(|d| d.eh_frame);
let debug_index = compilation.debug.map(|d| d.eh_frame);

let align = match triple.architecture {
Architecture::X86_64 => 1,
Expand All @@ -149,7 +154,8 @@ pub fn emit_compilation(
};

// Add sections
let custom_section_ids = custom_sections
let custom_section_ids = compilation
.custom_sections
.into_iter()
.map(|(section_index, custom_section)| {
if debug_index.map_or(false, |d| d == section_index) {
Expand Down Expand Up @@ -223,7 +229,7 @@ pub fn emit_compilation(
.collect::<PrimaryMap<LocalFunctionIndex, _>>();

// Add function call trampolines
for (signature_index, function) in function_call_trampolines.into_iter() {
for (signature_index, function) in compilation.function_call_trampolines.into_iter() {
let function_name =
symbol_registry.symbol_to_name(Symbol::FunctionCallTrampoline(signature_index));
let section_id = obj.section_id(StandardSection::Text);
Expand All @@ -241,7 +247,7 @@ pub fn emit_compilation(
}

// Add dynamic function trampolines
for (func_index, function) in dynamic_function_trampolines.into_iter() {
for (func_index, function) in compilation.dynamic_function_trampolines.into_iter() {
let function_name =
symbol_registry.symbol_to_name(Symbol::DynamicFunctionTrampoline(func_index));
let section_id = obj.section_id(StandardSection::Text);
Expand Down
120 changes: 5 additions & 115 deletions lib/types/src/compilation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ impl Dwarf {
#[derive(Debug, PartialEq, Eq)]
pub struct Compilation {
/// Compiled code for the function bodies.
functions: Functions,
pub functions: Functions,

/// Custom sections for the module.
/// It will hold the data, for example, for constants used in a
/// function, global variables, rodata_64, hot/cold function partitioning, ...
custom_sections: CustomSections,
pub custom_sections: CustomSections,

/// Trampolines to call a function defined locally in the wasm via a
/// provided `Vec` of values.
Expand All @@ -111,7 +111,7 @@ pub struct Compilation {
/// let func = instance.exports.get_function("my_func");
/// func.call(&[Value::I32(1)]);
/// ```
function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
pub function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,

/// Trampolines to call a dynamic function defined in
/// a host, from a Wasm module.
Expand All @@ -132,118 +132,8 @@ pub struct Compilation {
/// ```
///
/// Note: Dynamic function trampolines are only compiled for imported function types.
dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
pub dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,

/// Section ids corresponding to the Dwarf debug info
debug: Option<Dwarf>,
}

impl Compilation {
/// Creates a compilation artifact from a contiguous function buffer and a set of ranges
pub fn new(
functions: Functions,
custom_sections: CustomSections,
function_call_trampolines: PrimaryMap<SignatureIndex, FunctionBody>,
dynamic_function_trampolines: PrimaryMap<FunctionIndex, FunctionBody>,
debug: Option<Dwarf>,
) -> Self {
Self {
functions,
custom_sections,
function_call_trampolines,
dynamic_function_trampolines,
debug,
}
}

/// Gets the bytes of a single function
pub fn get(&self, func: LocalFunctionIndex) -> &CompiledFunction {
&self.functions[func]
}

/// Gets the number of functions defined.
pub fn len(&self) -> usize {
self.functions.len()
}

/// Returns whether there are no functions defined.
pub fn is_empty(&self) -> bool {
self.functions.is_empty()
}

/// Gets functions relocations.
pub fn get_relocations(&self) -> PrimaryMap<LocalFunctionIndex, Vec<Relocation>> {
self.functions
.iter()
.map(|(_, func)| func.relocations.clone())
.collect::<PrimaryMap<LocalFunctionIndex, _>>()
}

/// Gets functions bodies.
pub fn get_function_bodies(&self) -> PrimaryMap<LocalFunctionIndex, FunctionBody> {
self.functions
.iter()
.map(|(_, func)| func.body.clone())
.collect::<PrimaryMap<LocalFunctionIndex, _>>()
}

/// Gets functions frame info.
pub fn get_frame_info(&self) -> PrimaryMap<LocalFunctionIndex, CompiledFunctionFrameInfo> {
self.functions
.iter()
.map(|(_, func)| func.frame_info.clone())
.collect::<PrimaryMap<LocalFunctionIndex, _>>()
}

/// Gets function call trampolines.
pub fn get_function_call_trampolines(&self) -> PrimaryMap<SignatureIndex, FunctionBody> {
self.function_call_trampolines.clone()
}

/// Gets function call trampolines.
pub fn get_dynamic_function_trampolines(&self) -> PrimaryMap<FunctionIndex, FunctionBody> {
self.dynamic_function_trampolines.clone()
}

/// Gets custom section data.
pub fn get_custom_sections(&self) -> PrimaryMap<SectionIndex, CustomSection> {
self.custom_sections.clone()
}

/// Gets relocations that apply to custom sections.
pub fn get_custom_section_relocations(&self) -> PrimaryMap<SectionIndex, Vec<Relocation>> {
self.custom_sections
.iter()
.map(|(_, section)| section.relocations.clone())
.collect::<PrimaryMap<SectionIndex, _>>()
}

/// Returns the Dwarf info.
pub fn get_debug(&self) -> Option<Dwarf> {
self.debug.clone()
}
}

impl<'a> IntoIterator for &'a Compilation {
type IntoIter = Iter<'a>;
type Item = <Self::IntoIter as Iterator>::Item;

fn into_iter(self) -> Self::IntoIter {
Iter {
iterator: self.functions.iter(),
}
}
}

/// `Functions` iterator.
pub struct Iter<'a> {
iterator: <&'a Functions as IntoIterator>::IntoIter,
}

impl<'a> Iterator for Iter<'a> {
type Item = &'a CompiledFunction;

fn next(&mut self) -> Option<Self::Item> {
self.iterator.next().map(|(_, b)| b)
}
pub debug: Option<Dwarf>,
}

0 comments on commit d4d2d10

Please sign in to comment.