mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 06:15:33 +00:00
Start producing object files with llvm
This commit is contained in:
parent
73b4f7d337
commit
108973dabe
39
Cargo.lock
generated
39
Cargo.lock
generated
@ -345,6 +345,16 @@ name = "glob"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "goblin"
|
||||
version = "0.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.1.8"
|
||||
@ -439,6 +449,7 @@ dependencies = [
|
||||
name = "llvm-backend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"goblin 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"inkwell 0.1.0 (git+https://github.com/TheDan64/inkwell?branch=llvm7-0)",
|
||||
"smallvec 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -572,6 +583,11 @@ name = "peeking_take_while"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "plain"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "0.4.27"
|
||||
@ -768,6 +784,25 @@ name = "scopeguard"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "scroll"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scroll_derive"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "semver"
|
||||
version = "0.9.0"
|
||||
@ -1259,6 +1294,7 @@ dependencies = [
|
||||
"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
|
||||
"checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
|
||||
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
|
||||
"checksum goblin 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "84473a5302fa5094d3d9911c2f312f522f9a37462a777f195f63fae1bf7faf4d"
|
||||
"checksum hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3bae29b6653b3412c2e71e9d486db9f9df5d701941d86683005efb9f2d28e3da"
|
||||
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
|
||||
"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
|
||||
@ -1285,6 +1321,7 @@ dependencies = [
|
||||
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
|
||||
"checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9"
|
||||
"checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
||||
"checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
|
||||
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
|
||||
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
|
||||
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
|
||||
@ -1309,6 +1346,8 @@ dependencies = [
|
||||
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||
"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7"
|
||||
"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
|
||||
"checksum scroll 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383"
|
||||
"checksum scroll_derive 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb"
|
||||
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||
"checksum serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)" = "2e20fde37801e83c891a2dc4ebd3b81f0da4d1fb67a9e0a2a3b921e2536a58ee"
|
||||
|
@ -10,6 +10,7 @@ wasmparser = "0.28.0"
|
||||
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm7-0" }
|
||||
hashbrown = "0.1.8"
|
||||
smallvec = "0.6.8"
|
||||
goblin = "0.0.20"
|
||||
|
||||
[dev-dependencies]
|
||||
wabt = "0.7.4"
|
||||
|
45
lib/llvm-backend/src/backend.rs
Normal file
45
lib/llvm-backend/src/backend.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use inkwell::{
|
||||
module::Module,
|
||||
execution_engine::{ExecutionEngine, JitFunction},
|
||||
};
|
||||
use crate::intrinsics::Intrinsics;
|
||||
use std::ptr::NonNull;
|
||||
use wasmer_runtime_core::{
|
||||
module::ModuleInner,
|
||||
types::LocalFuncIndex,
|
||||
structures::TypedIndex,
|
||||
backend::{FuncResolver, vm},
|
||||
};
|
||||
|
||||
pub struct LLVMBackend {
|
||||
exec_engine: ExecutionEngine,
|
||||
}
|
||||
|
||||
impl LLVMBackend {
|
||||
pub fn new(module: Module, intrinsics: Intrinsics) -> Self {
|
||||
let exec_engine = module.create_jit_execution_engine(OptimizationLevel::Default).unwrap();
|
||||
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_dynamic_local, vmcalls::local_dynamic_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_static_local, vmcalls::local_static_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_dynamic_import, vmcalls::imported_dynamic_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_static_import, vmcalls::imported_static_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_dynamic_local, vmcalls::local_dynamic_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_static_local, vmcalls::local_static_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_dynamic_import, vmcalls::imported_dynamic_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_static_import, vmcalls::imported_static_memory_size as usize);
|
||||
|
||||
Self { exec_engine }
|
||||
}
|
||||
}
|
||||
|
||||
impl FuncResolver for LLVMBackend {
|
||||
fn get(&self, module: &ModuleInner, local_func_index: LocalFuncIndex) -> Option<NonNull<vm::Func>> {
|
||||
let index = module.info.imported_functions.len() + local_func_index.index();
|
||||
let name = format!("fn{}", index);
|
||||
|
||||
unsafe {
|
||||
let func: JitFunction<unsafe extern fn()> = self.exec_engine.get_function(&name).ok()?;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -59,7 +59,7 @@ fn type_to_llvm(intrinsics: &Intrinsics, ty: Type) -> BasicTypeEnum {
|
||||
pub fn parse_function_bodies(
|
||||
info: &ModuleInfo,
|
||||
code_reader: CodeSectionReader,
|
||||
) -> Result<(), BinaryReaderError> {
|
||||
) -> Result<(Module, Intrinsics), BinaryReaderError> {
|
||||
let context = Context::create();
|
||||
let module = context.create_module("module");
|
||||
let builder = context.create_builder();
|
||||
@ -104,7 +104,22 @@ pub fn parse_function_bodies(
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
let pass_manager = PassManager::create_for_module();
|
||||
pass_manager.add_promote_memory_to_register_pass();
|
||||
pass_manager.add_cfg_simplification_pass();
|
||||
pass_manager.add_instruction_combining_pass();
|
||||
// pass_manager.add_aggressive_inst_combiner_pass();
|
||||
// pass_manager.add_merged_load_store_motion_pass();
|
||||
// pass_manager.add_sccp_pass();
|
||||
pass_manager.add_gvn_pass();
|
||||
pass_manager.add_new_gvn_pass();
|
||||
pass_manager.add_aggressive_dce_pass();
|
||||
pass_manager.add_verifier_pass();
|
||||
pass_manager.run_on_module(&module);
|
||||
|
||||
println!("{}", module.print_to_string().to_string());
|
||||
|
||||
Ok((module, intrinsics))
|
||||
}
|
||||
|
||||
fn parse_function(
|
||||
@ -1668,30 +1683,14 @@ fn parse_function(
|
||||
&state.var_name(),
|
||||
);
|
||||
state.push1(result.try_as_basic_value().left().unwrap());
|
||||
} // op @ _ => {
|
||||
// println!("{}", module.print_to_string().to_string());
|
||||
// unimplemented!("{:?}", op);
|
||||
// }
|
||||
}
|
||||
op @ _ => {
|
||||
println!("{}", module.print_to_string().to_string());
|
||||
unimplemented!("{:?}", op);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("finished translating");
|
||||
|
||||
let pass_manager = PassManager::create_for_module();
|
||||
pass_manager.add_promote_memory_to_register_pass();
|
||||
pass_manager.add_cfg_simplification_pass();
|
||||
pass_manager.add_instruction_combining_pass();
|
||||
// pass_manager.add_aggressive_inst_combiner_pass();
|
||||
// pass_manager.add_merged_load_store_motion_pass();
|
||||
// pass_manager.add_sccp_pass();
|
||||
pass_manager.add_gvn_pass();
|
||||
pass_manager.add_new_gvn_pass();
|
||||
pass_manager.add_aggressive_dce_pass();
|
||||
pass_manager.add_verifier_pass();
|
||||
pass_manager.run_on_module(module);
|
||||
|
||||
println!("{}", module.print_to_string().to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -3,11 +3,17 @@ use wasmer_runtime_core::{
|
||||
error::CompileError,
|
||||
module::ModuleInner,
|
||||
};
|
||||
use inkwell::{
|
||||
execution_engine::JitFunction,
|
||||
targets::{TargetMachine, Target, RelocMode, CodeModel, InitializationConfig, FileType},
|
||||
OptimizationLevel,
|
||||
};
|
||||
|
||||
mod code;
|
||||
mod intrinsics;
|
||||
mod read_info;
|
||||
mod state;
|
||||
// mod backend;
|
||||
|
||||
pub struct LLVMCompiler {
|
||||
_private: (),
|
||||
@ -29,6 +35,7 @@ impl Compiler for LLVMCompiler {
|
||||
|
||||
#[test]
|
||||
fn test_read_module() {
|
||||
use wasmer_runtime_core::vmcalls;
|
||||
use wabt::wat2wasm;
|
||||
// let wasm = include_bytes!("../../spectests/examples/simple/simple.wasm") as &[u8];
|
||||
let wat = r#"
|
||||
@ -39,13 +46,75 @@ fn test_read_module() {
|
||||
(global $g0 (mut i32) (i32.const 0))
|
||||
(func $foo (type $t0) (param i32) (result i32)
|
||||
get_local 0
|
||||
set_global $g0
|
||||
get_global $g0
|
||||
call $foobar
|
||||
memory.grow
|
||||
)
|
||||
(func $foobar (type $t0)
|
||||
get_local 0
|
||||
)
|
||||
(func $bar (type $t0) (param i32) (result i32)
|
||||
get_local 0
|
||||
call $foo
|
||||
))
|
||||
"#;
|
||||
let wasm = wat2wasm(wat).unwrap();
|
||||
|
||||
let (info, code_reader) = read_info::read_module(&wasm).unwrap();
|
||||
|
||||
code::parse_function_bodies(&info, code_reader).unwrap();
|
||||
let (module, intrinsics) = code::parse_function_bodies(&info, code_reader).unwrap();
|
||||
|
||||
{
|
||||
Target::initialize_x86(&InitializationConfig {
|
||||
asm_parser: true,
|
||||
asm_printer: true,
|
||||
base: true,
|
||||
disassembler: true,
|
||||
info: true,
|
||||
machine_code: true,
|
||||
});
|
||||
let triple = TargetMachine::get_default_triple().to_string();
|
||||
let target = Target::from_triple(&triple).unwrap();
|
||||
let target_machine = target.create_target_machine(
|
||||
&triple,
|
||||
&TargetMachine::get_host_cpu_name().to_string(),
|
||||
&TargetMachine::get_host_cpu_features().to_string(),
|
||||
OptimizationLevel::Default,
|
||||
RelocMode::PIC,
|
||||
CodeModel::Default,
|
||||
).unwrap();
|
||||
|
||||
let memory_buffer = target_machine.write_to_memory_buffer(&module, FileType::Object).unwrap();
|
||||
// std::fs::write("memory_buffer", memory_buffer.as_slice()).unwrap();
|
||||
let mem_buf_slice = memory_buffer.as_slice();
|
||||
|
||||
let macho = goblin::mach::MachO::parse(mem_buf_slice, 0).unwrap();
|
||||
let symbols = macho.symbols.as_ref().unwrap();
|
||||
let relocations = macho.relocations().unwrap();
|
||||
for (_, reloc_iter, section) in relocations.into_iter() {
|
||||
println!("section: {:#?}", section);
|
||||
for reloc_info in reloc_iter {
|
||||
let reloc_info = reloc_info.unwrap();
|
||||
println!("\treloc_info: {:#?}", reloc_info);
|
||||
println!("\tsymbol: {:#?}", symbols.get(reloc_info.r_symbolnum()).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let exec_engine = module.create_jit_execution_engine(OptimizationLevel::Default).unwrap();
|
||||
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_dynamic_local, vmcalls::local_dynamic_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_static_local, vmcalls::local_static_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_dynamic_import, vmcalls::imported_dynamic_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_grow_static_import, vmcalls::imported_static_memory_grow as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_dynamic_local, vmcalls::local_dynamic_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_static_local, vmcalls::local_static_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_dynamic_import, vmcalls::imported_dynamic_memory_size as usize);
|
||||
exec_engine.add_global_mapping(&intrinsics.memory_size_static_import, vmcalls::imported_static_memory_size as usize);
|
||||
|
||||
// unsafe {
|
||||
// let func: JitFunction<unsafe extern fn(*mut u8, i32) -> i32> = exec_engine.get_function("fn0").unwrap();
|
||||
// let result = func.call(0 as _, 0);
|
||||
// println!("result: {}", result);
|
||||
// }
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
memory::MemoryType,
|
||||
module::{ModuleInfo, ModuleInner},
|
||||
module::ModuleInfo,
|
||||
structures::TypedIndex,
|
||||
units::Pages,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user