mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Wire up experimental_native_compile_module for LLVM.
Move Symbol and SymbolRegistry to wasmer_compiler crate.
This commit is contained in:
@@ -11,9 +11,8 @@ use wasm_common::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
|
||||
use wasmer_compiler::{
|
||||
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
|
||||
Dwarf, FunctionBodyData, ModuleTranslationState, RelocationTarget, SectionBody, SectionIndex,
|
||||
Target,
|
||||
Symbol, SymbolRegistry, Target,
|
||||
};
|
||||
use wasmer_object::{Symbol, SymbolRegistry};
|
||||
|
||||
//use std::sync::{Arc, Mutex};
|
||||
|
||||
@@ -73,13 +72,14 @@ impl SymbolRegistry for ShortNames {
|
||||
}
|
||||
|
||||
impl LLVMCompiler {
|
||||
fn _compile_native_object<'data, 'module>(
|
||||
fn compile_native_object<'data, 'module>(
|
||||
&self,
|
||||
target: &Target,
|
||||
compile_info: &'module CompileModuleInfo,
|
||||
module_translation: &ModuleTranslationState,
|
||||
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
|
||||
symbol_registry: &dyn SymbolRegistry,
|
||||
wasmer_metadata: &[u8],
|
||||
) -> Result<Vec<u8>, CompileError> {
|
||||
let target_machine = self.config().target_machine(target);
|
||||
let ctx = Context::create();
|
||||
@@ -168,6 +168,18 @@ impl LLVMCompiler {
|
||||
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 {
|
||||
merged_module.verify().unwrap();
|
||||
}
|
||||
@@ -181,6 +193,27 @@ impl 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
|
||||
/// associated relocations.
|
||||
fn compile_module<'data, 'module>(
|
||||
|
||||
@@ -32,9 +32,8 @@ use wasm_common::{
|
||||
use wasmer_compiler::wasmparser::{MemoryImmediate, Operator};
|
||||
use wasmer_compiler::{
|
||||
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};
|
||||
|
||||
fn to_compile_error(err: impl std::error::Error) -> CompileError {
|
||||
|
||||
@@ -10,8 +10,9 @@ use crate::target::Target;
|
||||
use crate::translator::FunctionMiddlewareGenerator;
|
||||
use crate::FunctionBodyData;
|
||||
use crate::ModuleTranslationState;
|
||||
use crate::SectionIndex;
|
||||
use wasm_common::entity::PrimaryMap;
|
||||
use wasm_common::{Features, LocalFunctionIndex};
|
||||
use wasm_common::{Features, FunctionIndex, LocalFunctionIndex, SignatureIndex};
|
||||
use wasmparser::{validate, OperatorValidatorConfig, ValidatingParserConfig};
|
||||
|
||||
/// The compiler configuration options.
|
||||
@@ -87,16 +88,44 @@ pub trait Compiler {
|
||||
/// It returns the bytes as a `&[u8]` or a [`CompileError`].
|
||||
fn experimental_native_compile_module<'data, 'module>(
|
||||
&self,
|
||||
target: &Target,
|
||||
module: &'module CompileModuleInfo,
|
||||
module_translation: &ModuleTranslationState,
|
||||
_target: &Target,
|
||||
_module: &'module CompileModuleInfo,
|
||||
_module_translation: &ModuleTranslationState,
|
||||
// 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.
|
||||
wasmer_metadata: &[u8],
|
||||
_wasmer_metadata: &[u8],
|
||||
) -> Result<Vec<u8>, CompileError> {
|
||||
Err(CompileError::UnsupportedFeature(
|
||||
"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>;
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ mod sourceloc;
|
||||
|
||||
pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap};
|
||||
#[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::function::{
|
||||
Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody,
|
||||
|
||||
@@ -26,7 +26,7 @@ use wasmer_compiler::{
|
||||
Compilation, CompileModuleInfo, Compiler, FunctionBodyData, ModuleEnvironment,
|
||||
ModuleTranslationState, Target,
|
||||
};
|
||||
use wasmer_compiler::{CompileError, Features, OperatingSystem, Triple};
|
||||
use wasmer_compiler::{CompileError, Features, OperatingSystem, Symbol, SymbolRegistry, Triple};
|
||||
use wasmer_engine::{
|
||||
Artifact, DeserializeError, InstantiationError, LinkError, RuntimeError, SerializeError,
|
||||
};
|
||||
@@ -34,7 +34,6 @@ use wasmer_engine::{
|
||||
use wasmer_engine::{Engine, Tunables};
|
||||
#[cfg(feature = "compiler")]
|
||||
use wasmer_object::{emit_compilation, emit_data, get_object_for_target};
|
||||
use wasmer_object::{Symbol, SymbolRegistry};
|
||||
use wasmer_vm::{
|
||||
FunctionBodyPtr, MemoryStyle, ModuleInfo, TableStyle, VMFunctionBody, VMSharedSignatureIndex,
|
||||
VMTrampoline,
|
||||
@@ -194,6 +193,7 @@ impl NativeArtifact {
|
||||
&metadata.compile_info,
|
||||
module_translation.as_ref().unwrap(),
|
||||
function_body_inputs,
|
||||
&metadata,
|
||||
&serialized_data,
|
||||
)?;
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_common::entity::{EntityRef, PrimaryMap};
|
||||
use wasm_common::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex};
|
||||
use wasmer_compiler::{CompileModuleInfo, SectionIndex};
|
||||
use wasmer_object::{Symbol, SymbolRegistry};
|
||||
use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry};
|
||||
|
||||
/// Serializable struct that represents the compiled metadata.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
||||
@@ -24,6 +24,4 @@ mod error;
|
||||
mod module;
|
||||
|
||||
pub use crate::error::ObjectError;
|
||||
pub use crate::module::{
|
||||
emit_compilation, emit_data, get_object_for_target, Symbol, SymbolRegistry,
|
||||
};
|
||||
pub use crate::module::{emit_compilation, emit_data, get_object_for_target};
|
||||
|
||||
@@ -4,36 +4,9 @@ use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, Symbol
|
||||
use wasm_common::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
|
||||
use wasmer_compiler::{
|
||||
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`.
|
||||
///
|
||||
/// # Usage
|
||||
|
||||
Reference in New Issue
Block a user