Wire up experimental_native_compile_module for LLVM.

Move Symbol and SymbolRegistry to wasmer_compiler crate.
This commit is contained in:
Nick Lewycky
2020-08-05 17:44:52 -07:00
parent 0e6bc3191f
commit 2c2da8ec3c
8 changed files with 78 additions and 47 deletions

View File

@@ -11,9 +11,8 @@ use wasm_common::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
use wasmer_compiler::{ use wasmer_compiler::{
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection, Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
Dwarf, FunctionBodyData, ModuleTranslationState, RelocationTarget, SectionBody, SectionIndex, Dwarf, FunctionBodyData, ModuleTranslationState, RelocationTarget, SectionBody, SectionIndex,
Target, Symbol, SymbolRegistry, Target,
}; };
use wasmer_object::{Symbol, SymbolRegistry};
//use std::sync::{Arc, Mutex}; //use std::sync::{Arc, Mutex};
@@ -73,13 +72,14 @@ impl SymbolRegistry for ShortNames {
} }
impl LLVMCompiler { impl LLVMCompiler {
fn _compile_native_object<'data, 'module>( fn compile_native_object<'data, 'module>(
&self, &self,
target: &Target, target: &Target,
compile_info: &'module CompileModuleInfo, compile_info: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState, module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>, function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
symbol_registry: &dyn SymbolRegistry, symbol_registry: &dyn SymbolRegistry,
wasmer_metadata: &[u8],
) -> Result<Vec<u8>, CompileError> { ) -> Result<Vec<u8>, CompileError> {
let target_machine = self.config().target_machine(target); let target_machine = self.config().target_machine(target);
let ctx = Context::create(); let ctx = Context::create();
@@ -168,6 +168,18 @@ impl LLVMCompiler {
merged_module.link_in_module(m).unwrap(); merged_module.link_in_module(m).unwrap();
}); });
let i8_ty = ctx.i8_type();
let metadata_init = i8_ty.const_array(
wasmer_metadata
.iter()
.map(|v| i8_ty.const_int(*v as u64, false))
.collect::<Vec<_>>()
.as_slice(),
);
let metadata_gv =
merged_module.add_global(metadata_init.get_type(), None, "WASMER_METADATA");
metadata_gv.set_initializer(&metadata_init);
if self.config().enable_verifier { if self.config().enable_verifier {
merged_module.verify().unwrap(); merged_module.verify().unwrap();
} }
@@ -181,6 +193,27 @@ impl LLVMCompiler {
} }
impl Compiler for LLVMCompiler { impl Compiler for LLVMCompiler {
fn experimental_native_compile_module<'data, 'module>(
&self,
target: &Target,
module: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
symbol_registry: &dyn SymbolRegistry,
// The metadata to inject into the wasmer_metadata section of the object file.
wasmer_metadata: &[u8],
) -> Result<Vec<u8>, CompileError> {
self.compile_native_object(
target,
module,
module_translation,
function_body_inputs,
symbol_registry,
wasmer_metadata,
)
}
/// Compile the module using LLVM, producing a compilation result with /// Compile the module using LLVM, producing a compilation result with
/// associated relocations. /// associated relocations.
fn compile_module<'data, 'module>( fn compile_module<'data, 'module>(

View File

@@ -32,9 +32,8 @@ use wasm_common::{
use wasmer_compiler::wasmparser::{MemoryImmediate, Operator}; use wasmer_compiler::wasmparser::{MemoryImmediate, Operator};
use wasmer_compiler::{ use wasmer_compiler::{
to_wasm_error, wptype_to_type, CompileError, FunctionBodyData, GenerateMiddlewareChain, to_wasm_error, wptype_to_type, CompileError, FunctionBodyData, GenerateMiddlewareChain,
MiddlewareBinaryReader, ModuleTranslationState, RelocationTarget, MiddlewareBinaryReader, ModuleTranslationState, RelocationTarget, Symbol, SymbolRegistry,
}; };
use wasmer_object::{Symbol, SymbolRegistry};
use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle}; use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle};
fn to_compile_error(err: impl std::error::Error) -> CompileError { fn to_compile_error(err: impl std::error::Error) -> CompileError {

View File

@@ -10,8 +10,9 @@ use crate::target::Target;
use crate::translator::FunctionMiddlewareGenerator; use crate::translator::FunctionMiddlewareGenerator;
use crate::FunctionBodyData; use crate::FunctionBodyData;
use crate::ModuleTranslationState; use crate::ModuleTranslationState;
use crate::SectionIndex;
use wasm_common::entity::PrimaryMap; use wasm_common::entity::PrimaryMap;
use wasm_common::{Features, LocalFunctionIndex}; use wasm_common::{Features, FunctionIndex, LocalFunctionIndex, SignatureIndex};
use wasmparser::{validate, OperatorValidatorConfig, ValidatingParserConfig}; use wasmparser::{validate, OperatorValidatorConfig, ValidatingParserConfig};
/// The compiler configuration options. /// The compiler configuration options.
@@ -87,16 +88,44 @@ pub trait Compiler {
/// It returns the bytes as a `&[u8]` or a [`CompileError`]. /// It returns the bytes as a `&[u8]` or a [`CompileError`].
fn experimental_native_compile_module<'data, 'module>( fn experimental_native_compile_module<'data, 'module>(
&self, &self,
target: &Target, _target: &Target,
module: &'module CompileModuleInfo, _module: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState, _module_translation: &ModuleTranslationState,
// The list of function bodies // The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>, _function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
_symbol_registry: &dyn SymbolRegistry,
// The metadata to inject into the wasmer_metadata section of the object file. // The metadata to inject into the wasmer_metadata section of the object file.
wasmer_metadata: &[u8], _wasmer_metadata: &[u8],
) -> Result<Vec<u8>, CompileError> { ) -> Result<Vec<u8>, CompileError> {
Err(CompileError::UnsupportedFeature( Err(CompileError::UnsupportedFeature(
"native compilation not supported".into(), "native compilation not supported".into(),
)) ))
} }
} }
/// The kinds of wasm_common objects that might be found in a native object file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Symbol {
/// A function defined in the wasm.
LocalFunction(LocalFunctionIndex),
/// A wasm section.
Section(SectionIndex),
/// The function call trampoline for a given signature.
FunctionCallTrampoline(SignatureIndex),
/// The dynamic function trampoline for a given function.
DynamicFunctionTrampoline(FunctionIndex),
}
/// This trait facilitates symbol name lookups in a native object file.
pub trait SymbolRegistry: Send + Sync {
/// Given a `Symbol` it returns the name for that symbol in the object file
fn symbol_to_name(&self, symbol: Symbol) -> String;
/// Given a name it returns the `Symbol` for that name in the object file
///
/// This function is the inverse of [`SymbolRegistry::symbol_to_name`]
fn name_to_symbol(&self, name: &str) -> Option<Symbol>;
}

View File

@@ -68,7 +68,7 @@ mod sourceloc;
pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap}; pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap};
#[cfg(feature = "translator")] #[cfg(feature = "translator")]
pub use crate::compiler::{Compiler, CompilerConfig}; pub use crate::compiler::{Compiler, CompilerConfig, Symbol, SymbolRegistry};
pub use crate::error::{CompileError, ParseCpuFeatureError, WasmError, WasmResult}; pub use crate::error::{CompileError, ParseCpuFeatureError, WasmError, WasmResult};
pub use crate::function::{ pub use crate::function::{
Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody, Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody,

View File

@@ -26,7 +26,7 @@ use wasmer_compiler::{
Compilation, CompileModuleInfo, Compiler, FunctionBodyData, ModuleEnvironment, Compilation, CompileModuleInfo, Compiler, FunctionBodyData, ModuleEnvironment,
ModuleTranslationState, Target, ModuleTranslationState, Target,
}; };
use wasmer_compiler::{CompileError, Features, OperatingSystem, Triple}; use wasmer_compiler::{CompileError, Features, OperatingSystem, Symbol, SymbolRegistry, Triple};
use wasmer_engine::{ use wasmer_engine::{
Artifact, DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError, Artifact, DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError,
}; };
@@ -34,7 +34,6 @@ use wasmer_engine::{
use wasmer_engine::{Engine, Tunables}; use wasmer_engine::{Engine, Tunables};
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
use wasmer_object::{emit_compilation, emit_data, get_object_for_target}; use wasmer_object::{emit_compilation, emit_data, get_object_for_target};
use wasmer_object::{Symbol, SymbolRegistry};
use wasmer_vm::{ use wasmer_vm::{
FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMSharedSignatureIndex, FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMSharedSignatureIndex,
VMTrampoline, VMTrampoline,
@@ -194,6 +193,7 @@ impl NativeArtifact {
&metadata.compile_info, &metadata.compile_info,
module_translation.as_ref().unwrap(), module_translation.as_ref().unwrap(),
function_body_inputs, function_body_inputs,
&metadata,
&serialized_data, &serialized_data,
)?; )?;

View File

@@ -1,8 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasm_common::entity::{EntityRef, PrimaryMap}; use wasm_common::entity::{EntityRef, PrimaryMap};
use wasm_common::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex}; use wasm_common::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex};
use wasmer_compiler::{CompileModuleInfo, SectionIndex}; use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry};
use wasmer_object::{Symbol, SymbolRegistry};
/// Serializable struct that represents the compiled metadata. /// Serializable struct that represents the compiled metadata.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View File

@@ -24,6 +24,4 @@ mod error;
mod module; mod module;
pub use crate::error::ObjectError; pub use crate::error::ObjectError;
pub use crate::module::{ pub use crate::module::{emit_compilation, emit_data, get_object_for_target};
emit_compilation, emit_data, get_object_for_target, Symbol, SymbolRegistry,
};

View File

@@ -4,36 +4,9 @@ use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, Symbol
use wasm_common::{FunctionIndex, LocalFunctionIndex, SignatureIndex}; use wasm_common::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
use wasmer_compiler::{ use wasmer_compiler::{
Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness, RelocationTarget, Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness, RelocationTarget,
SectionIndex, Triple, SectionIndex, Symbol, SymbolRegistry, Triple,
}; };
/// The kinds of wasm_common objects that might be found in a native object file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Symbol {
/// A function defined in the wasm.
LocalFunction(LocalFunctionIndex),
/// A wasm section.
Section(SectionIndex),
/// The function call trampoline for a given signature.
FunctionCallTrampoline(SignatureIndex),
/// The dynamic function trampoline for a given function.
DynamicFunctionTrampoline(FunctionIndex),
}
/// This trait facilitates symbol name lookups in a native object file.
pub trait SymbolRegistry: Send + Sync {
/// Given a `Symbol` it returns the name for that symbol in the object file
fn symbol_to_name(&self, symbol: Symbol) -> String;
/// Given a name it returns the `Symbol` for that name in the object file
///
/// This function is the inverse of [`SymbolRegistry::symbol_to_name`]
fn name_to_symbol(&self, name: &str) -> Option<Symbol>;
}
/// Create an object for a given target `Triple`. /// Create an object for a given target `Triple`.
/// ///
/// # Usage /// # Usage