Merge branch 'master' into feature/dynamic

This commit is contained in:
nlewycky
2020-08-21 10:43:09 -07:00
committed by GitHub
7 changed files with 53 additions and 34 deletions

View File

@@ -1,10 +1,12 @@
use crate::config::LLVM;
use crate::trampoline::FuncTrampoline;
use crate::translator::FuncTranslator;
use crate::CompiledKind;
use inkwell::context::Context;
use inkwell::memory_buffer::MemoryBuffer;
use inkwell::module::Module;
use inkwell::module::{Linkage, Module};
use inkwell::targets::FileType;
use inkwell::DLLStorageClass;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use wasmer_compiler::{
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
@@ -186,6 +188,8 @@ impl LLVMCompiler {
let metadata_gv =
merged_module.add_global(metadata_init.get_type(), None, "WASMER_METADATA");
metadata_gv.set_initializer(&metadata_init);
metadata_gv.set_linkage(Linkage::DLLExport);
metadata_gv.set_dll_storage_class(DLLStorageClass::Export);
if self.config().enable_verifier {
merged_module.verify().unwrap();
@@ -194,6 +198,9 @@ impl LLVMCompiler {
let memory_buffer = target_machine
.write_to_memory_buffer(&merged_module, FileType::Object)
.unwrap();
if let Some(ref callbacks) = self.config.callbacks {
callbacks.obj_memory_buffer(&CompiledKind::Module, &memory_buffer);
}
Ok(memory_buffer.as_slice().to_vec())
}

View File

@@ -19,24 +19,22 @@ pub type InkwellMemoryBuffer = inkwell::memory_buffer::MemoryBuffer;
/// The compiled function kind, used for debugging in the `LLVMCallbacks`.
#[derive(Debug, Clone)]
pub enum CompiledFunctionKind {
// A locally-defined function in the Wasm file
pub enum CompiledKind {
// A locally-defined function in the Wasm file.
Local(LocalFunctionIndex),
// A function call trampoline for a given signature
// A function call trampoline for a given signature.
FunctionCallTrampoline(FunctionType),
// A dynamic function trampoline for a given signature
// A dynamic function trampoline for a given signature.
DynamicFunctionTrampoline(FunctionType),
// An entire Wasm module.
Module,
}
/// Callbacks to the different LLVM compilation phases.
pub trait LLVMCallbacks: Debug + Send + Sync {
fn preopt_ir(&self, function: &CompiledFunctionKind, module: &InkwellModule);
fn postopt_ir(&self, function: &CompiledFunctionKind, module: &InkwellModule);
fn obj_memory_buffer(
&self,
function: &CompiledFunctionKind,
memory_buffer: &InkwellMemoryBuffer,
);
fn preopt_ir(&self, function: &CompiledKind, module: &InkwellModule);
fn postopt_ir(&self, function: &CompiledKind, module: &InkwellModule);
fn obj_memory_buffer(&self, function: &CompiledKind, memory_buffer: &InkwellMemoryBuffer);
}
#[derive(Debug, Clone)]

View File

@@ -21,6 +21,4 @@ mod trampoline;
mod translator;
pub use crate::compiler::LLVMCompiler;
pub use crate::config::{
CompiledFunctionKind, InkwellMemoryBuffer, InkwellModule, LLVMCallbacks, LLVM,
};
pub use crate::config::{CompiledKind, InkwellMemoryBuffer, InkwellModule, LLVMCallbacks, LLVM};

View File

@@ -1,4 +1,4 @@
use crate::config::{CompiledFunctionKind, LLVM};
use crate::config::{CompiledKind, LLVM};
use crate::object_file::{load_object_file, CompiledFunction};
use crate::translator::abi::{
func_type_to_llvm, get_vmctx_ptr_param, is_sret, pack_values_for_register_return,
@@ -13,7 +13,7 @@ use inkwell::{
targets::{FileType, TargetMachine},
types::BasicType,
values::{BasicValue, FunctionValue},
AddressSpace,
AddressSpace, DLLStorageClass,
};
use std::cmp;
use std::convert::TryInto;
@@ -42,7 +42,7 @@ impl FuncTrampoline {
name: &str,
) -> Result<Module, CompileError> {
// The function type, used for the callbacks.
let function = CompiledFunctionKind::FunctionCallTrampoline(ty.clone());
let function = CompiledKind::FunctionCallTrampoline(ty.clone());
let module = self.ctx.create_module("");
let target_machine = &self.target_machine;
let target_triple = target_machine.get_triple();
@@ -66,6 +66,12 @@ impl FuncTrampoline {
trampoline_func
.as_global_value()
.set_section(FUNCTION_SECTION);
trampoline_func
.as_global_value()
.set_linkage(Linkage::DLLExport);
trampoline_func
.as_global_value()
.set_dll_storage_class(DLLStorageClass::Export);
generate_trampoline(trampoline_func, ty, &callee_attrs, &self.ctx, &intrinsics)?;
if let Some(ref callbacks) = config.callbacks {
@@ -96,7 +102,7 @@ impl FuncTrampoline {
name: &str,
) -> Result<FunctionBody, CompileError> {
let module = self.trampoline_to_module(ty, config, name)?;
let function = CompiledFunctionKind::FunctionCallTrampoline(ty.clone());
let function = CompiledKind::FunctionCallTrampoline(ty.clone());
let target_machine = &self.target_machine;
let memory_buffer = target_machine
@@ -166,7 +172,7 @@ impl FuncTrampoline {
name: &str,
) -> Result<Module, CompileError> {
// The function type, used for the callbacks
let function = CompiledFunctionKind::DynamicFunctionTrampoline(ty.clone());
let function = CompiledKind::DynamicFunctionTrampoline(ty.clone());
let module = self.ctx.create_module("");
let target_machine = &self.target_machine;
let target_triple = target_machine.get_triple();
@@ -182,6 +188,12 @@ impl FuncTrampoline {
trampoline_func
.as_global_value()
.set_section(FUNCTION_SECTION);
trampoline_func
.as_global_value()
.set_linkage(Linkage::DLLExport);
trampoline_func
.as_global_value()
.set_dll_storage_class(DLLStorageClass::Export);
generate_dynamic_trampoline(trampoline_func, ty, &self.ctx, &intrinsics)?;
if let Some(ref callbacks) = config.callbacks {
@@ -210,7 +222,7 @@ impl FuncTrampoline {
config: &LLVM,
name: &str,
) -> Result<FunctionBody, CompileError> {
let function = CompiledFunctionKind::DynamicFunctionTrampoline(ty.clone());
let function = CompiledKind::DynamicFunctionTrampoline(ty.clone());
let target_machine = &self.target_machine;
let module = self.dynamic_trampoline_to_module(ty, config, name)?;

View File

@@ -18,11 +18,11 @@ use inkwell::{
BasicValue, BasicValueEnum, FloatValue, FunctionValue, InstructionOpcode, InstructionValue,
IntValue, PhiValue, PointerValue, VectorValue,
},
AddressSpace, AtomicOrdering, AtomicRMWBinOp, FloatPredicate, IntPredicate,
AddressSpace, AtomicOrdering, AtomicRMWBinOp, DLLStorageClass, FloatPredicate, IntPredicate,
};
use smallvec::SmallVec;
use crate::config::{CompiledFunctionKind, LLVM};
use crate::config::{CompiledKind, LLVM};
use crate::object_file::{load_object_file, CompiledFunction};
use wasmer_compiler::wasmparser::{MemoryImmediate, Operator};
use wasmer_compiler::{
@@ -79,7 +79,7 @@ impl FuncTranslator {
symbol_registry: &dyn SymbolRegistry,
) -> Result<Module, CompileError> {
// The function type, used for the callbacks.
let function = CompiledFunctionKind::Local(*local_func_index);
let function = CompiledKind::Local(*local_func_index);
let func_index = wasm_module.func_index(*local_func_index);
let function_name =
symbol_registry.symbol_to_name(Symbol::LocalFunction(*local_func_index));
@@ -112,6 +112,9 @@ impl FuncTranslator {
func.add_attribute(AttributeLoc::Function, intrinsics.stack_probe);
func.set_personality_function(intrinsics.personality);
func.as_global_value().set_section(FUNCTION_SECTION);
func.set_linkage(Linkage::DLLExport);
func.as_global_value()
.set_dll_storage_class(DLLStorageClass::Export);
let entry = self.ctx.append_basic_block(func, "entry");
let start_of_code = self.ctx.append_basic_block(func, "start_of_code");
@@ -295,7 +298,7 @@ impl FuncTranslator {
table_styles,
symbol_registry,
)?;
let function = CompiledFunctionKind::Local(*local_func_index);
let function = CompiledKind::Local(*local_func_index);
let target_machine = &self.target_machine;
let memory_buffer = target_machine
.write_to_memory_buffer(&module, FileType::Object)