Skip to content

Commit

Permalink
Reset LLVM related code to master
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Jun 3, 2019
1 parent 151af82 commit 66f9049
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 138 deletions.
32 changes: 2 additions & 30 deletions lib/llvm-backend/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use wasmer_runtime_core::{
CacheGen, RunnableModule,
},
cache::Error as CacheError,
codegen::BkptInfo,
module::ModuleInfo,
structures::TypedIndex,
typed_func::{Wasm, WasmTrapInfo},
Expand Down Expand Up @@ -191,8 +190,6 @@ fn get_callbacks() -> Callbacks {

fn_name!("vm.exception.trap") => throw_trap as _,

fn_name!("vm.breakpoint") => vm_breakpoint as _,

_ => ptr::null(),
}
}
Expand All @@ -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 {
LlvmMemory(MemoryBuffer),
Memory(Memory),
Expand All @@ -249,15 +231,10 @@ pub struct LLVMBackend {
module: *mut LLVMModule,
#[allow(dead_code)]
buffer: Arc<Buffer>,
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
}

impl LLVMBackend {
pub fn new(
module: Module,
_intrinsics: Intrinsics,
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
) -> (Self, LLVMCache) {
pub fn new(module: Module, _intrinsics: Intrinsics) -> (Self, LLVMCache) {
Target::initialize_x86(&InitializationConfig {
asm_parser: true,
asm_printer: true,
Expand Down Expand Up @@ -312,16 +289,12 @@ impl LLVMBackend {
Self {
module,
buffer: Arc::clone(&buffer),
breakpoints,
},
LLVMCache { buffer },
)
}

pub unsafe fn from_buffer(
memory: Memory,
breakpoints: Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
) -> Result<(Self, LLVMCache), String> {
pub unsafe fn from_buffer(memory: Memory) -> Result<(Self, LLVMCache), String> {
let callbacks = get_callbacks();
let mut module: *mut LLVMModule = ptr::null_mut();

Expand All @@ -345,7 +318,6 @@ impl LLVMBackend {
Self {
module,
buffer: Arc::clone(&buffer),
breakpoints,
},
LLVMCache { buffer },
))
Expand Down
80 changes: 10 additions & 70 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ pub struct LLVMFunctionCodeGenerator {
num_params: usize,
ctx: Option<CtxType<'static>>,
unreachable_depth: usize,
breakpoints: Option<Box<Vec<Box<Fn(BkptInfo) + Send + Sync + 'static>>>>,
}

impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
Expand Down Expand Up @@ -471,13 +470,13 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
}

fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
match event {
Event::Internal(InternalEvent::FunctionBegin(_))
| Event::Internal(InternalEvent::FunctionEnd) => {
let op = match event {
Event::Wasm(x) => x,
Event::Internal(_x) => {
return Ok(());
}
_ => {}
}
Event::WasmOwned(ref x) => x,
};

let mut state = &mut self.state;
let builder = self.builder.as_ref().unwrap();
Expand All @@ -490,13 +489,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let mut ctx = self.ctx.as_mut().unwrap();

if !state.reachable {
let op = match event {
Event::Wasm(x) => x,
Event::WasmOwned(ref x) => x,
Event::Internal(_x) => {
return Ok(());
}
};
match *op {
Operator::Block { ty: _ } | Operator::Loop { ty: _ } | Operator::If { ty: _ } => {
self.unreachable_depth += 1;
Expand All @@ -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 {
/***************************
* Control Flow instructions.
Expand Down Expand Up @@ -2561,18 +2511,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
_module_info: Arc<RwLock<ModuleInfo>>,
) -> Result<&mut LLVMFunctionCodeGenerator, CodegenError> {
// 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) => (
x.context.take().unwrap(),
x.builder.take().unwrap(),
x.intrinsics.take().unwrap(),
x.breakpoints.take().unwrap(),
),
None => (
self.context.take().unwrap(),
self.builder.take().unwrap(),
self.intrinsics.take().unwrap(),
Box::new(Vec::new()),
),
};

Expand Down Expand Up @@ -2631,7 +2579,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
num_params,
ctx: None,
unreachable_depth: 0,
breakpoints: Some(breakpoints),
};
self.functions.push(code);
Ok(self.functions.last_mut().unwrap())
Expand All @@ -2641,18 +2588,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
mut self,
module_info: &ModuleInfo,
) -> 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) => (
x.context.take().unwrap(),
x.builder.take().unwrap(),
x.intrinsics.take().unwrap(),
x.breakpoints.take().unwrap(),
),
None => (
self.context.take().unwrap(),
self.builder.take().unwrap(),
self.intrinsics.take().unwrap(),
Box::new(Vec::new()),
),
};
self.context = Some(context);
Expand Down Expand Up @@ -2683,8 +2628,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>

// self.module.print_to_stderr();

let (backend, cache_gen) =
LLVMBackend::new(self.module, self.intrinsics.take().unwrap(), breakpoints);
let (backend, cache_gen) = LLVMBackend::new(self.module, self.intrinsics.take().unwrap());
Ok((backend, Box::new(cache_gen)))
}

Expand Down Expand Up @@ -2716,11 +2660,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
Ok(())
}

unsafe fn from_cache(_artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
Err(CacheError::Unknown(
"caching is broken for LLVM backend".into(),
))
/*
unsafe fn from_cache(artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
let (info, _, memory) = artifact.consume();
let (backend, cache_gen) =
LLVMBackend::from_buffer(memory).map_err(CacheError::DeserializeError)?;
Expand All @@ -2730,6 +2670,6 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
cache_gen: Box::new(cache_gen),

info,
})*/
})
}
}
37 changes: 0 additions & 37 deletions lib/llvm-backend/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ pub struct Intrinsics {
pub memory_size_static_import: FunctionValue,
pub memory_size_shared_import: FunctionValue,

pub breakpoint: FunctionValue,

pub throw_trap: FunctionValue,

pub ctx_ptr_ty: PointerType,
Expand Down Expand Up @@ -165,7 +163,6 @@ impl Intrinsics {
let stack_lower_bound_ty = i8_ty;
let memory_base_ty = i8_ty;
let memory_bound_ty = void_ty;
let internals_ty = i64_ty;
let local_function_ty = i8_ptr_ty;

let anyfunc_ty = context.struct_type(
Expand Down Expand Up @@ -221,9 +218,6 @@ impl Intrinsics {
memory_bound_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),
internals_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),
local_function_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),
Expand Down Expand Up @@ -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_i32_take_ctx_i64_i32 = i32_ty.fn_type(
&[ctx_ptr_ty.as_basic_type_enum(), i64_ty_basic, i32_ty_basic],
false,
);

Self {
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),
Expand Down Expand Up @@ -389,7 +378,6 @@ impl Intrinsics {
ret_i32_take_ctx_i32,
None,
),
breakpoint: module.add_function("vm.breakpoint", ret_i32_take_ctx_i64_i32, None),
throw_trap: module.add_function(
"vm.exception.trap",
void_ty.fn_type(&[i32_ty_basic], false),
Expand Down Expand Up @@ -440,7 +428,6 @@ pub struct CtxType<'a> {
cached_tables: HashMap<TableIndex, TableCache>,
cached_sigindices: HashMap<SigIndex, IntValue>,
cached_globals: HashMap<GlobalIndex, GlobalCache>,
cached_internals: HashMap<usize, PointerValue>,
cached_imported_functions: HashMap<ImportedFuncIndex, ImportedFuncCache>,

_phantom: PhantomData<&'a FunctionValue>,
Expand All @@ -466,7 +453,6 @@ impl<'a> CtxType<'a> {
cached_tables: HashMap::new(),
cached_sigindices: HashMap::new(),
cached_globals: HashMap::new(),
cached_internals: HashMap::new(),
cached_imported_functions: HashMap::new(),

_phantom: PhantomData,
Expand Down Expand Up @@ -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 {
let (cached_globals, ctx_ptr_value, info, cache_builder) = (
&mut self.cached_globals,
Expand Down
2 changes: 1 addition & 1 deletion lib/llvm-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![cfg_attr(nightly, feature(unwind_attributes))]

mod backend;
pub mod code;
mod code;
mod intrinsics;
mod platform;
mod read_info;
Expand Down

0 comments on commit 66f9049

Please sign in to comment.