mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 12:41:32 +00:00
Reset LLVM related code to master
This commit is contained in:
@ -24,7 +24,6 @@ use wasmer_runtime_core::{
|
|||||||
CacheGen, RunnableModule,
|
CacheGen, RunnableModule,
|
||||||
},
|
},
|
||||||
cache::Error as CacheError,
|
cache::Error as CacheError,
|
||||||
codegen::BkptInfo,
|
|
||||||
module::ModuleInfo,
|
module::ModuleInfo,
|
||||||
structures::TypedIndex,
|
structures::TypedIndex,
|
||||||
typed_func::{Wasm, WasmTrapInfo},
|
typed_func::{Wasm, WasmTrapInfo},
|
||||||
@ -191,8 +190,6 @@ fn get_callbacks() -> Callbacks {
|
|||||||
|
|
||||||
fn_name!("vm.exception.trap") => throw_trap as _,
|
fn_name!("vm.exception.trap") => throw_trap as _,
|
||||||
|
|
||||||
fn_name!("vm.breakpoint") => vm_breakpoint as _,
|
|
||||||
|
|
||||||
_ => ptr::null(),
|
_ => ptr::null(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -212,21 +209,6 @@ fn get_callbacks() -> Callbacks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "C" fn vm_breakpoint(
|
|
||||||
_ctx: &mut vm::Ctx,
|
|
||||||
breakpoints: *const Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>,
|
|
||||||
index: u32,
|
|
||||||
) -> i32 {
|
|
||||||
unsafe extern "C" fn do_throw() -> ! {
|
|
||||||
let ptr: *mut i32 = ::std::ptr::null_mut();
|
|
||||||
*ptr = 42;
|
|
||||||
::std::process::abort();
|
|
||||||
}
|
|
||||||
let breakpoints: &Vec<_> = &*breakpoints;
|
|
||||||
breakpoints[index as usize](BkptInfo { throw: do_throw });
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum Buffer {
|
pub enum Buffer {
|
||||||
LlvmMemory(MemoryBuffer),
|
LlvmMemory(MemoryBuffer),
|
||||||
Memory(Memory),
|
Memory(Memory),
|
||||||
@ -249,15 +231,10 @@ pub struct LLVMBackend {
|
|||||||
module: *mut LLVMModule,
|
module: *mut LLVMModule,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
buffer: Arc<Buffer>,
|
buffer: Arc<Buffer>,
|
||||||
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LLVMBackend {
|
impl LLVMBackend {
|
||||||
pub fn new(
|
pub fn new(module: Module, _intrinsics: Intrinsics) -> (Self, LLVMCache) {
|
||||||
module: Module,
|
|
||||||
_intrinsics: Intrinsics,
|
|
||||||
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
|
|
||||||
) -> (Self, LLVMCache) {
|
|
||||||
Target::initialize_x86(&InitializationConfig {
|
Target::initialize_x86(&InitializationConfig {
|
||||||
asm_parser: true,
|
asm_parser: true,
|
||||||
asm_printer: true,
|
asm_printer: true,
|
||||||
@ -312,16 +289,12 @@ impl LLVMBackend {
|
|||||||
Self {
|
Self {
|
||||||
module,
|
module,
|
||||||
buffer: Arc::clone(&buffer),
|
buffer: Arc::clone(&buffer),
|
||||||
breakpoints,
|
|
||||||
},
|
},
|
||||||
LLVMCache { buffer },
|
LLVMCache { buffer },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn from_buffer(
|
pub unsafe fn from_buffer(memory: Memory) -> Result<(Self, LLVMCache), String> {
|
||||||
memory: Memory,
|
|
||||||
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
|
|
||||||
) -> Result<(Self, LLVMCache), String> {
|
|
||||||
let callbacks = get_callbacks();
|
let callbacks = get_callbacks();
|
||||||
let mut module: *mut LLVMModule = ptr::null_mut();
|
let mut module: *mut LLVMModule = ptr::null_mut();
|
||||||
|
|
||||||
@ -345,7 +318,6 @@ impl LLVMBackend {
|
|||||||
Self {
|
Self {
|
||||||
module,
|
module,
|
||||||
buffer: Arc::clone(&buffer),
|
buffer: Arc::clone(&buffer),
|
||||||
breakpoints,
|
|
||||||
},
|
},
|
||||||
LLVMCache { buffer },
|
LLVMCache { buffer },
|
||||||
))
|
))
|
||||||
|
@ -398,7 +398,6 @@ pub struct LLVMFunctionCodeGenerator {
|
|||||||
num_params: usize,
|
num_params: usize,
|
||||||
ctx: Option<CtxType<'static>>,
|
ctx: Option<CtxType<'static>>,
|
||||||
unreachable_depth: usize,
|
unreachable_depth: usize,
|
||||||
breakpoints: Option<Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
||||||
@ -471,13 +470,13 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
|
fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
|
||||||
match event {
|
let op = match event {
|
||||||
Event::Internal(InternalEvent::FunctionBegin(_))
|
Event::Wasm(x) => x,
|
||||||
| Event::Internal(InternalEvent::FunctionEnd) => {
|
Event::Internal(_x) => {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
_ => {}
|
Event::WasmOwned(ref x) => x,
|
||||||
}
|
};
|
||||||
|
|
||||||
let mut state = &mut self.state;
|
let mut state = &mut self.state;
|
||||||
let builder = self.builder.as_ref().unwrap();
|
let builder = self.builder.as_ref().unwrap();
|
||||||
@ -490,13 +489,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
|||||||
let mut ctx = self.ctx.as_mut().unwrap();
|
let mut ctx = self.ctx.as_mut().unwrap();
|
||||||
|
|
||||||
if !state.reachable {
|
if !state.reachable {
|
||||||
let op = match event {
|
|
||||||
Event::Wasm(x) => x,
|
|
||||||
Event::WasmOwned(ref x) => x,
|
|
||||||
Event::Internal(_x) => {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match *op {
|
match *op {
|
||||||
Operator::Block { ty: _ } | Operator::Loop { ty: _ } | Operator::If { ty: _ } => {
|
Operator::Block { ty: _ } | Operator::Loop { ty: _ } | Operator::If { ty: _ } => {
|
||||||
self.unreachable_depth += 1;
|
self.unreachable_depth += 1;
|
||||||
@ -519,48 +511,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let op = match event {
|
|
||||||
Event::Wasm(x) => x,
|
|
||||||
Event::WasmOwned(ref x) => x,
|
|
||||||
Event::Internal(x) => {
|
|
||||||
match x {
|
|
||||||
InternalEvent::Breakpoint(handler) => {
|
|
||||||
let mut breakpoints = self.breakpoints.as_mut().unwrap();
|
|
||||||
breakpoints.push(handler);
|
|
||||||
let ptr: *mut Vec<_> = &mut **breakpoints;
|
|
||||||
let ptr_const = intrinsics
|
|
||||||
.i64_ty
|
|
||||||
.const_int(ptr as usize as u64, false)
|
|
||||||
.as_basic_value_enum();
|
|
||||||
builder.build_call(
|
|
||||||
intrinsics.breakpoint,
|
|
||||||
&[
|
|
||||||
ctx.basic(),
|
|
||||||
ptr_const,
|
|
||||||
intrinsics
|
|
||||||
.i32_ty
|
|
||||||
.const_int((breakpoints.len() - 1) as u64, false)
|
|
||||||
.as_basic_value_enum(),
|
|
||||||
],
|
|
||||||
&state.var_name(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
InternalEvent::GetInternal(index) => {
|
|
||||||
let ptr = ctx.internal_pointer(index as usize, intrinsics);
|
|
||||||
let value = builder.build_load(ptr, "internal_value");
|
|
||||||
state.push1(value);
|
|
||||||
}
|
|
||||||
InternalEvent::SetInternal(index) => {
|
|
||||||
let value = state.pop1()?;
|
|
||||||
let ptr = ctx.internal_pointer(index as usize, intrinsics);
|
|
||||||
builder.build_store(ptr, value);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match *op {
|
match *op {
|
||||||
/***************************
|
/***************************
|
||||||
* Control Flow instructions.
|
* Control Flow instructions.
|
||||||
@ -2561,18 +2511,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
_module_info: Arc<RwLock<ModuleInfo>>,
|
_module_info: Arc<RwLock<ModuleInfo>>,
|
||||||
) -> Result<&mut LLVMFunctionCodeGenerator, CodegenError> {
|
) -> Result<&mut LLVMFunctionCodeGenerator, CodegenError> {
|
||||||
// Creates a new function and returns the function-scope code generator for it.
|
// Creates a new function and returns the function-scope code generator for it.
|
||||||
let (context, builder, intrinsics, breakpoints) = match self.functions.last_mut() {
|
let (context, builder, intrinsics) = match self.functions.last_mut() {
|
||||||
Some(x) => (
|
Some(x) => (
|
||||||
x.context.take().unwrap(),
|
x.context.take().unwrap(),
|
||||||
x.builder.take().unwrap(),
|
x.builder.take().unwrap(),
|
||||||
x.intrinsics.take().unwrap(),
|
x.intrinsics.take().unwrap(),
|
||||||
x.breakpoints.take().unwrap(),
|
|
||||||
),
|
),
|
||||||
None => (
|
None => (
|
||||||
self.context.take().unwrap(),
|
self.context.take().unwrap(),
|
||||||
self.builder.take().unwrap(),
|
self.builder.take().unwrap(),
|
||||||
self.intrinsics.take().unwrap(),
|
self.intrinsics.take().unwrap(),
|
||||||
Box::new(Vec::new()),
|
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2631,7 +2579,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
num_params,
|
num_params,
|
||||||
ctx: None,
|
ctx: None,
|
||||||
unreachable_depth: 0,
|
unreachable_depth: 0,
|
||||||
breakpoints: Some(breakpoints),
|
|
||||||
};
|
};
|
||||||
self.functions.push(code);
|
self.functions.push(code);
|
||||||
Ok(self.functions.last_mut().unwrap())
|
Ok(self.functions.last_mut().unwrap())
|
||||||
@ -2641,18 +2588,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
mut self,
|
mut self,
|
||||||
module_info: &ModuleInfo,
|
module_info: &ModuleInfo,
|
||||||
) -> Result<(LLVMBackend, Box<dyn CacheGen>), CodegenError> {
|
) -> Result<(LLVMBackend, Box<dyn CacheGen>), CodegenError> {
|
||||||
let (context, builder, intrinsics, breakpoints) = match self.functions.last_mut() {
|
let (context, builder, intrinsics) = match self.functions.last_mut() {
|
||||||
Some(x) => (
|
Some(x) => (
|
||||||
x.context.take().unwrap(),
|
x.context.take().unwrap(),
|
||||||
x.builder.take().unwrap(),
|
x.builder.take().unwrap(),
|
||||||
x.intrinsics.take().unwrap(),
|
x.intrinsics.take().unwrap(),
|
||||||
x.breakpoints.take().unwrap(),
|
|
||||||
),
|
),
|
||||||
None => (
|
None => (
|
||||||
self.context.take().unwrap(),
|
self.context.take().unwrap(),
|
||||||
self.builder.take().unwrap(),
|
self.builder.take().unwrap(),
|
||||||
self.intrinsics.take().unwrap(),
|
self.intrinsics.take().unwrap(),
|
||||||
Box::new(Vec::new()),
|
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
self.context = Some(context);
|
self.context = Some(context);
|
||||||
@ -2683,8 +2628,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
|
|
||||||
// self.module.print_to_stderr();
|
// self.module.print_to_stderr();
|
||||||
|
|
||||||
let (backend, cache_gen) =
|
let (backend, cache_gen) = LLVMBackend::new(self.module, self.intrinsics.take().unwrap());
|
||||||
LLVMBackend::new(self.module, self.intrinsics.take().unwrap(), breakpoints);
|
|
||||||
Ok((backend, Box::new(cache_gen)))
|
Ok((backend, Box::new(cache_gen)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2716,11 +2660,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn from_cache(_artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
|
unsafe fn from_cache(artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
|
||||||
Err(CacheError::Unknown(
|
|
||||||
"caching is broken for LLVM backend".into(),
|
|
||||||
))
|
|
||||||
/*
|
|
||||||
let (info, _, memory) = artifact.consume();
|
let (info, _, memory) = artifact.consume();
|
||||||
let (backend, cache_gen) =
|
let (backend, cache_gen) =
|
||||||
LLVMBackend::from_buffer(memory).map_err(CacheError::DeserializeError)?;
|
LLVMBackend::from_buffer(memory).map_err(CacheError::DeserializeError)?;
|
||||||
@ -2730,6 +2670,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
|||||||
cache_gen: Box::new(cache_gen),
|
cache_gen: Box::new(cache_gen),
|
||||||
|
|
||||||
info,
|
info,
|
||||||
})*/
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,8 +113,6 @@ pub struct Intrinsics {
|
|||||||
pub memory_size_static_import: FunctionValue,
|
pub memory_size_static_import: FunctionValue,
|
||||||
pub memory_size_shared_import: FunctionValue,
|
pub memory_size_shared_import: FunctionValue,
|
||||||
|
|
||||||
pub breakpoint: FunctionValue,
|
|
||||||
|
|
||||||
pub throw_trap: FunctionValue,
|
pub throw_trap: FunctionValue,
|
||||||
|
|
||||||
pub ctx_ptr_ty: PointerType,
|
pub ctx_ptr_ty: PointerType,
|
||||||
@ -165,7 +163,6 @@ impl Intrinsics {
|
|||||||
let stack_lower_bound_ty = i8_ty;
|
let stack_lower_bound_ty = i8_ty;
|
||||||
let memory_base_ty = i8_ty;
|
let memory_base_ty = i8_ty;
|
||||||
let memory_bound_ty = void_ty;
|
let memory_bound_ty = void_ty;
|
||||||
let internals_ty = i64_ty;
|
|
||||||
let local_function_ty = i8_ptr_ty;
|
let local_function_ty = i8_ptr_ty;
|
||||||
|
|
||||||
let anyfunc_ty = context.struct_type(
|
let anyfunc_ty = context.struct_type(
|
||||||
@ -221,9 +218,6 @@ impl Intrinsics {
|
|||||||
memory_bound_ty
|
memory_bound_ty
|
||||||
.ptr_type(AddressSpace::Generic)
|
.ptr_type(AddressSpace::Generic)
|
||||||
.as_basic_type_enum(),
|
.as_basic_type_enum(),
|
||||||
internals_ty
|
|
||||||
.ptr_type(AddressSpace::Generic)
|
|
||||||
.as_basic_type_enum(),
|
|
||||||
local_function_ty
|
local_function_ty
|
||||||
.ptr_type(AddressSpace::Generic)
|
.ptr_type(AddressSpace::Generic)
|
||||||
.as_basic_type_enum(),
|
.as_basic_type_enum(),
|
||||||
@ -252,11 +246,6 @@ impl Intrinsics {
|
|||||||
|
|
||||||
let ret_i1_take_i1_i1 = i1_ty.fn_type(&[i1_ty_basic, i1_ty_basic], false);
|
let ret_i1_take_i1_i1 = i1_ty.fn_type(&[i1_ty_basic, i1_ty_basic], false);
|
||||||
|
|
||||||
let ret_i32_take_ctx_i64_i32 = i32_ty.fn_type(
|
|
||||||
&[ctx_ptr_ty.as_basic_type_enum(), i64_ty_basic, i32_ty_basic],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
ctlz_i32: module.add_function("llvm.ctlz.i32", ret_i32_take_i32_i1, None),
|
ctlz_i32: module.add_function("llvm.ctlz.i32", ret_i32_take_i32_i1, None),
|
||||||
ctlz_i64: module.add_function("llvm.ctlz.i64", ret_i64_take_i64_i1, None),
|
ctlz_i64: module.add_function("llvm.ctlz.i64", ret_i64_take_i64_i1, None),
|
||||||
@ -389,7 +378,6 @@ impl Intrinsics {
|
|||||||
ret_i32_take_ctx_i32,
|
ret_i32_take_ctx_i32,
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
breakpoint: module.add_function("vm.breakpoint", ret_i32_take_ctx_i64_i32, None),
|
|
||||||
throw_trap: module.add_function(
|
throw_trap: module.add_function(
|
||||||
"vm.exception.trap",
|
"vm.exception.trap",
|
||||||
void_ty.fn_type(&[i32_ty_basic], false),
|
void_ty.fn_type(&[i32_ty_basic], false),
|
||||||
@ -440,7 +428,6 @@ pub struct CtxType<'a> {
|
|||||||
cached_tables: HashMap<TableIndex, TableCache>,
|
cached_tables: HashMap<TableIndex, TableCache>,
|
||||||
cached_sigindices: HashMap<SigIndex, IntValue>,
|
cached_sigindices: HashMap<SigIndex, IntValue>,
|
||||||
cached_globals: HashMap<GlobalIndex, GlobalCache>,
|
cached_globals: HashMap<GlobalIndex, GlobalCache>,
|
||||||
cached_internals: HashMap<usize, PointerValue>,
|
|
||||||
cached_imported_functions: HashMap<ImportedFuncIndex, ImportedFuncCache>,
|
cached_imported_functions: HashMap<ImportedFuncIndex, ImportedFuncCache>,
|
||||||
|
|
||||||
_phantom: PhantomData<&'a FunctionValue>,
|
_phantom: PhantomData<&'a FunctionValue>,
|
||||||
@ -466,7 +453,6 @@ impl<'a> CtxType<'a> {
|
|||||||
cached_tables: HashMap::new(),
|
cached_tables: HashMap::new(),
|
||||||
cached_sigindices: HashMap::new(),
|
cached_sigindices: HashMap::new(),
|
||||||
cached_globals: HashMap::new(),
|
cached_globals: HashMap::new(),
|
||||||
cached_internals: HashMap::new(),
|
|
||||||
cached_imported_functions: HashMap::new(),
|
cached_imported_functions: HashMap::new(),
|
||||||
|
|
||||||
_phantom: PhantomData,
|
_phantom: PhantomData,
|
||||||
@ -690,29 +676,6 @@ impl<'a> CtxType<'a> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn internal_pointer(&mut self, index: usize, intrinsics: &Intrinsics) -> PointerValue {
|
|
||||||
let (cached_internals, ctx_ptr_value, _info, cache_builder) = (
|
|
||||||
&mut self.cached_internals,
|
|
||||||
self.ctx_ptr_value,
|
|
||||||
self.info,
|
|
||||||
&self.cache_builder,
|
|
||||||
);
|
|
||||||
*cached_internals.entry(index).or_insert_with(|| {
|
|
||||||
let array_ptr_ptr = unsafe {
|
|
||||||
cache_builder.build_struct_gep(
|
|
||||||
ctx_ptr_value,
|
|
||||||
offset_to_index(Ctx::offset_internals()),
|
|
||||||
"internals_array_ptr_ptr",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
let array_ptr = cache_builder
|
|
||||||
.build_load(array_ptr_ptr, "internals_array_ptr")
|
|
||||||
.into_pointer_value();
|
|
||||||
let const_index = intrinsics.i32_ty.const_int(index as u64, false);
|
|
||||||
unsafe { cache_builder.build_in_bounds_gep(array_ptr, &[const_index], "element_ptr") }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn global_cache(&mut self, index: GlobalIndex, intrinsics: &Intrinsics) -> GlobalCache {
|
pub fn global_cache(&mut self, index: GlobalIndex, intrinsics: &Intrinsics) -> GlobalCache {
|
||||||
let (cached_globals, ctx_ptr_value, info, cache_builder) = (
|
let (cached_globals, ctx_ptr_value, info, cache_builder) = (
|
||||||
&mut self.cached_globals,
|
&mut self.cached_globals,
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#![cfg_attr(nightly, feature(unwind_attributes))]
|
#![cfg_attr(nightly, feature(unwind_attributes))]
|
||||||
|
|
||||||
mod backend;
|
mod backend;
|
||||||
pub mod code;
|
mod code;
|
||||||
mod intrinsics;
|
mod intrinsics;
|
||||||
mod platform;
|
mod platform;
|
||||||
mod read_info;
|
mod read_info;
|
||||||
|
Reference in New Issue
Block a user