From ecc0c9e547f57373dac97162899099ab602815db Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Wed, 9 Nov 2022 13:23:44 +0100 Subject: [PATCH 1/2] Limit the use of clone when handling Compilation object --- lib/compiler-cranelift/src/compiler.rs | 8 ++--- lib/compiler-llvm/src/compiler.rs | 8 ++--- lib/compiler-singlepass/src/compiler.rs | 8 ++--- .../src/artifact_builders/artifact_builder.rs | 32 ++++++++++++------- lib/object/src/module.rs | 26 +++++++++------ lib/types/src/compilation/function.rs | 13 ++++---- 6 files changed, 55 insertions(+), 40 deletions(-) diff --git a/lib/compiler-cranelift/src/compiler.rs b/lib/compiler-cranelift/src/compiler.rs index 94b7857502d..0303e963b2b 100644 --- a/lib/compiler-cranelift/src/compiler.rs +++ b/lib/compiler-cranelift/src/compiler.rs @@ -380,13 +380,13 @@ impl Compiler for CraneliftCompiler { .into_iter() .collect::>(); - 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, + }) } } diff --git a/lib/compiler-llvm/src/compiler.rs b/lib/compiler-llvm/src/compiler.rs index 7ff8ba78b5e..8f888dc6e16 100644 --- a/lib/compiler-llvm/src/compiler.rs +++ b/lib/compiler-llvm/src/compiler.rs @@ -360,12 +360,12 @@ impl Compiler for LLVMCompiler { .into_iter() .collect::>(); - Ok(Compilation::new( + Ok(Compilation { functions, - module_custom_sections, + custom_sections: module_custom_sections, function_call_trampolines, dynamic_function_trampolines, - dwarf, - )) + debug: dwarf, + }) } } diff --git a/lib/compiler-singlepass/src/compiler.rs b/lib/compiler-singlepass/src/compiler.rs index a2694c9f727..510dc47bb7a 100644 --- a/lib/compiler-singlepass/src/compiler.rs +++ b/lib/compiler-singlepass/src/compiler.rs @@ -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, + }) } } diff --git a/lib/compiler/src/artifact_builders/artifact_builder.rs b/lib/compiler/src/artifact_builders/artifact_builder.rs index 098cd0b8fd4..1f0a06307f2 100644 --- a/lib/compiler/src/artifact_builders/artifact_builder.rs +++ b/lib/compiler/src/artifact_builders/artifact_builder.rs @@ -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 @@ -85,25 +83,35 @@ impl ArtifactBuild { .collect::>() .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::>(); 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, }; diff --git a/lib/object/src/module.rs b/lib/object/src/module.rs index ad20961acb4..cece8077b2e 100644 --- a/lib/object/src/module.rs +++ b/lib/object/src/module.rs @@ -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::>(); - 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, @@ -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) { @@ -223,7 +229,7 @@ pub fn emit_compilation( .collect::>(); // 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); @@ -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); diff --git a/lib/types/src/compilation/function.rs b/lib/types/src/compilation/function.rs index 86033d82554..a8290b46d05 100644 --- a/lib/types/src/compilation/function.rs +++ b/lib/types/src/compilation/function.rs @@ -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. @@ -111,7 +111,7 @@ pub struct Compilation { /// let func = instance.exports.get_function("my_func"); /// func.call(&[Value::I32(1)]); /// ``` - function_call_trampolines: PrimaryMap, + pub function_call_trampolines: PrimaryMap, /// Trampolines to call a dynamic function defined in /// a host, from a Wasm module. @@ -132,12 +132,12 @@ pub struct Compilation { /// ``` /// /// Note: Dynamic function trampolines are only compiled for imported function types. - dynamic_function_trampolines: PrimaryMap, + pub dynamic_function_trampolines: PrimaryMap, /// Section ids corresponding to the Dwarf debug info - debug: Option, + pub debug: Option, } - +/* impl Compilation { /// Creates a compilation artifact from a contiguous function buffer and a set of ranges pub fn new( @@ -247,3 +247,4 @@ impl<'a> Iterator for Iter<'a> { self.iterator.next().map(|(_, b)| b) } } +*/ From d5be7db3934969fd95ebe36bffbac23fe653aeb2 Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Thu, 10 Nov 2022 08:08:52 +0100 Subject: [PATCH 2/2] Removed commented code --- lib/types/src/compilation/function.rs | 111 -------------------------- 1 file changed, 111 deletions(-) diff --git a/lib/types/src/compilation/function.rs b/lib/types/src/compilation/function.rs index a8290b46d05..e87d15c59ed 100644 --- a/lib/types/src/compilation/function.rs +++ b/lib/types/src/compilation/function.rs @@ -137,114 +137,3 @@ pub struct Compilation { /// Section ids corresponding to the Dwarf debug info pub debug: Option, } -/* -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, - dynamic_function_trampolines: PrimaryMap, - debug: Option, - ) -> 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> { - self.functions - .iter() - .map(|(_, func)| func.relocations.clone()) - .collect::>() - } - - /// Gets functions bodies. - pub fn get_function_bodies(&self) -> PrimaryMap { - self.functions - .iter() - .map(|(_, func)| func.body.clone()) - .collect::>() - } - - /// Gets functions frame info. - pub fn get_frame_info(&self) -> PrimaryMap { - self.functions - .iter() - .map(|(_, func)| func.frame_info.clone()) - .collect::>() - } - - /// Gets function call trampolines. - pub fn get_function_call_trampolines(&self) -> PrimaryMap { - self.function_call_trampolines.clone() - } - - /// Gets function call trampolines. - pub fn get_dynamic_function_trampolines(&self) -> PrimaryMap { - self.dynamic_function_trampolines.clone() - } - - /// Gets custom section data. - pub fn get_custom_sections(&self) -> PrimaryMap { - self.custom_sections.clone() - } - - /// Gets relocations that apply to custom sections. - pub fn get_custom_section_relocations(&self) -> PrimaryMap> { - self.custom_sections - .iter() - .map(|(_, section)| section.relocations.clone()) - .collect::>() - } - - /// Returns the Dwarf info. - pub fn get_debug(&self) -> Option { - self.debug.clone() - } -} - -impl<'a> IntoIterator for &'a Compilation { - type IntoIter = Iter<'a>; - type Item = ::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.iterator.next().map(|(_, b)| b) - } -} -*/