Skip to content

Commit

Permalink
Merge #941
Browse files Browse the repository at this point in the history
941: Emit direct calls for local functions. r=nlewycky a=nlewycky



Co-authored-by: Nick Lewycky <[email protected]>
  • Loading branch information
bors[bot] and nlewycky authored Nov 11, 2019
2 parents 5e71a8f + ed6ce4b commit 8233f4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 72 deletions.
40 changes: 22 additions & 18 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use inkwell::{
use smallvec::SmallVec;
use std::{
cell::RefCell,
collections::HashMap,
rc::Rc,
sync::{Arc, RwLock},
};
Expand Down Expand Up @@ -851,6 +852,7 @@ pub struct LLVMModuleCodeGenerator {
signatures: Map<SigIndex, FunctionType>,
signatures_raw: Map<SigIndex, FuncSig>,
function_signatures: Option<Arc<Map<FuncIndex, SigIndex>>>,
llvm_functions: Rc<RefCell<HashMap<FuncIndex, FunctionValue>>>,
func_import_count: usize,
personality_func: FunctionValue,
module: Rc<RefCell<Module>>,
Expand All @@ -865,6 +867,7 @@ pub struct LLVMFunctionCodeGenerator {
alloca_builder: Option<Builder>,
intrinsics: Option<Intrinsics>,
state: State,
llvm_functions: Rc<RefCell<HashMap<FuncIndex, FunctionValue>>>,
function: FunctionValue,
func_sig: FuncSig,
signatures: Map<SigIndex, FunctionType>,
Expand Down Expand Up @@ -1692,7 +1695,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
let func_sig = &info.signatures[sigindex];

let (params, func_ptr) = match func_index.local_or_import(info) {
LocalOrImport::Local(local_func_index) => {
LocalOrImport::Local(_) => {
let params: Vec<_> = std::iter::once(ctx.basic())
.chain(
state
Expand Down Expand Up @@ -1722,15 +1725,9 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
)
.collect();

let func_ptr = ctx.local_func(
local_func_index,
llvm_sig,
intrinsics,
self.module.clone(),
builder,
);
let func_ptr = self.llvm_functions.borrow_mut()[&func_index];

(params, func_ptr)
(params, func_ptr.as_global_value().as_pointer_value())
}
LocalOrImport::Import(import_func_index) => {
let (func_ptr_untyped, ctx_ptr) =
Expand Down Expand Up @@ -1766,7 +1763,6 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
.collect();

let func_ptr_ty = llvm_sig.ptr_type(AddressSpace::Generic);

let func_ptr = builder.build_pointer_cast(
func_ptr_untyped,
func_ptr_ty,
Expand Down Expand Up @@ -8019,6 +8015,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
signatures: Map::new(),
signatures_raw: Map::new(),
function_signatures: None,
llvm_functions: Rc::new(RefCell::new(HashMap::new())),
func_import_count: 0,
personality_func,
stackmaps: Rc::new(RefCell::new(StackmapRegistry::default())),
Expand Down Expand Up @@ -8053,15 +8050,11 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
),
};

let sig_id = self.function_signatures.as_ref().unwrap()
[FuncIndex::new(self.func_import_count + self.functions.len())];
let func_index = FuncIndex::new(self.func_import_count + self.functions.len());
let sig_id = self.function_signatures.as_ref().unwrap()[func_index];
let func_sig = self.signatures_raw[sig_id].clone();

let function = self.module.borrow_mut().add_function(
&format!("fn{}", self.func_import_count + self.functions.len()),
self.signatures[sig_id],
Some(Linkage::External),
);
let function = &self.llvm_functions.borrow_mut()[&func_index];
function.set_personality_function(self.personality_func);

let mut state = State::new();
Expand Down Expand Up @@ -8119,7 +8112,8 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
builder: Some(builder),
alloca_builder: Some(alloca_builder),
intrinsics: Some(intrinsics),
function,
llvm_functions: self.llvm_functions.clone(),
function: *function,
func_sig: func_sig,
locals,
signatures: self.signatures.clone(),
Expand Down Expand Up @@ -8233,6 +8227,16 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
&mut self,
assoc: Map<FuncIndex, SigIndex>,
) -> Result<(), CodegenError> {
for (index, sig_id) in &assoc {
if index.index() >= self.func_import_count {
let function = self.module.borrow_mut().add_function(
&format!("fn{}", index.index()),
self.signatures[*sig_id],
Some(Linkage::External),
);
self.llvm_functions.borrow_mut().insert(index, function);
}
}
self.function_signatures = Some(Arc::new(assoc));
Ok(())
}
Expand Down
56 changes: 2 additions & 54 deletions lib/llvm-backend/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use inkwell::{
builder::Builder,
context::Context,
module::Module,
types::{
BasicType, FloatType, FunctionType, IntType, PointerType, StructType, VectorType, VoidType,
},
types::{BasicType, FloatType, IntType, PointerType, StructType, VectorType, VoidType},
values::{
BasicValue, BasicValueEnum, FloatValue, FunctionValue, InstructionValue, IntValue,
PointerValue, VectorValue,
Expand All @@ -21,8 +19,7 @@ use wasmer_runtime_core::{
module::ModuleInfo,
structures::TypedIndex,
types::{
GlobalIndex, ImportedFuncIndex, LocalFuncIndex, LocalOrImport, MemoryIndex, SigIndex,
TableIndex, Type,
GlobalIndex, ImportedFuncIndex, LocalOrImport, MemoryIndex, SigIndex, TableIndex, Type,
},
units::Pages,
vm::{Ctx, INTERNALS_SIZE},
Expand Down Expand Up @@ -895,55 +892,6 @@ impl<'a> CtxType<'a> {
(base_ptr, bounds)
}

pub fn local_func(
&mut self,
index: LocalFuncIndex,
fn_ty: FunctionType,
intrinsics: &Intrinsics,
module: Rc<RefCell<Module>>,
builder: &Builder,
) -> PointerValue {
let local_func_array_ptr_ptr = unsafe {
builder.build_struct_gep(
self.ctx_ptr_value,
offset_to_index(Ctx::offset_local_functions()),
"local_func_array_ptr_ptr",
)
};
let local_func_array_ptr = builder
.build_load(local_func_array_ptr_ptr, "local_func_array_ptr")
.into_pointer_value();
tbaa_label(
module.clone(),
intrinsics,
"context_field_ptr_to_local_funcs",
local_func_array_ptr.as_instruction_value().unwrap(),
None,
);
let local_func_ptr_ptr = unsafe {
builder.build_in_bounds_gep(
local_func_array_ptr,
&[intrinsics.i32_ty.const_int(index.index() as u64, false)],
"local_func_ptr_ptr",
)
};
let local_func_ptr = builder
.build_load(local_func_ptr_ptr, "local_func_ptr")
.into_pointer_value();
tbaa_label(
module.clone(),
intrinsics,
"local_func_ptr",
local_func_ptr.as_instruction_value().unwrap(),
Some(index.index() as u32),
);
builder.build_pointer_cast(
local_func_ptr,
fn_ty.ptr_type(AddressSpace::Generic),
"local_func_ptr",
)
}

pub fn dynamic_sigindex(&mut self, index: SigIndex, intrinsics: &Intrinsics) -> IntValue {
let (cached_sigindices, ctx_ptr_value, cache_builder) = (
&mut self.cached_sigindices,
Expand Down

0 comments on commit 8233f4c

Please sign in to comment.