Skip to content

Commit 5a7a281

Browse files
committed
Add a new feature llvmjit to enable WAMR llvmjit running mode
by default, WAMR is running under fast-interp+aot mode if tends to enable llvmjit to gain performance advantage, $ cargo build --features llvmjit
1 parent 375c372 commit 5a7a281

File tree

10 files changed

+169
-36
lines changed

10 files changed

+169
-36
lines changed

language-bindings/rust/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ name = "wamr-rust-sdk"
66
version = "0.1.0"
77
edition = "2021"
88
license = "Apache-2.0 WITH LLVM-exception"
9+
resolver = "2"
910

1011
[dependencies]
1112
wamr-sys = { path = "crates/wamr-sys", version = "0.1.0" }
1213

1314
[workspace]
14-
members = ["crates/wamr-sys"]
15-
exclude = ["examples/wasi-hello", "resources/test/hello"]
15+
members = ["crates/wamr-sys", ]
16+
exclude = ["examples/wasi-hello", "resources/test/gcd", "resources/test/add-extra"]
17+
18+
[features]
19+
llvmjit = ["wamr-sys/llvmjit"]

language-bindings/rust/crates/wamr-sys/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ version = "0.1.0"
77
edition = "2021"
88
license = "Apache-2.0 WITH LLVM-exception"
99
authors = ["Sven Pfennig <s.pfennig@reply.de>"]
10+
links = "vmlib"
1011

1112
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1213

@@ -16,3 +17,6 @@ authors = ["Sven Pfennig <s.pfennig@reply.de>"]
1617
cc = "1.0"
1718
cmake = "0.1"
1819
bindgen = "0.66"
20+
21+
[features]
22+
llvmjit = []

language-bindings/rust/crates/wamr-sys/build.rs

+102-13
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ extern crate bindgen;
77
extern crate cmake;
88

99
use cmake::Config;
10-
use std::{env, path::PathBuf};
10+
use std::{env, path::Path, path::PathBuf};
1111

1212
fn main() {
13-
let wamr_root = "../../../../";
14-
let llvm_dir = format!("{wamr_root}core/deps/llvm/build");
15-
let dst = Config::new(wamr_root)
16-
.define("LLVM_DIR", llvm_dir)
13+
let wamr_root = Path::new("../../../../").canonicalize().unwrap();
14+
assert!(wamr_root.exists());
15+
16+
let llvm_dir = wamr_root.join("core/deps/llvm/build");
17+
assert!(llvm_dir.exists());
18+
19+
let enable_llvm_jit = if cfg!(feature = "llvmjit") { "1" } else { "0" };
20+
let dst = Config::new(&wamr_root)
1721
// running mode
22+
.define("WAMR_BUILD_AOT", "1")
1823
.define("WAMR_BUILD_INTERP", "1")
1924
.define("WAMR_BUILD_FAST_INTERP", "1")
20-
// .define("WAMR_BUILD_JIT", "1")
25+
.define("WAMR_BUILD_JIT", enable_llvm_jit)
2126
// mvp
2227
.define("WAMR_BUILD_BULK_MEMORY", "1")
2328
.define("WAMR_BUILD_REF_TYPES", "1")
@@ -26,19 +31,103 @@ fn main() {
2631
.define("WAMR_BUILD_LIBC_WASI", "1")
2732
// `nostdlib`
2833
.define("WAMR_BUILD_LIBC_BUILTIN", "1")
34+
.build_target("iwasm_static")
2935
.build();
3036

31-
println!(
32-
"cargo:rustc-link-search=native={}",
33-
dst.join("build").display()
34-
);
35-
println!("cargo:rustc-link-lib=vmlib");
37+
println!("cargo:rustc-link-search=native={}/build", dst.display());
38+
println!("cargo:rustc-link-lib=static=vmlib");
39+
40+
// //TODO: support macos?
41+
if cfg!(feature = "llvmjit") {
42+
println!("cargo:rustc-link-lib=dylib=dl");
43+
println!("cargo:rustc-link-lib=dylib=m");
44+
println!("cargo:rustc-link-lib=dylib=rt");
45+
println!("cargo:rustc-link-lib=dylib=stdc++");
46+
println!("cargo:rustc-link-lib=dylib=z");
47+
48+
println!("cargo:libdir={}/lib", llvm_dir.display());
49+
println!("cargo:rustc-link-search=native={}/lib", llvm_dir.display());
50+
println!("cargo:rustc-link-lib=static=LLVMXRay");
51+
println!("cargo:rustc-link-lib=static=LLVMLibDriver");
52+
println!("cargo:rustc-link-lib=static=LLVMDlltoolDriver");
53+
println!("cargo:rustc-link-lib=static=LLVMCoverage");
54+
println!("cargo:rustc-link-lib=static=LLVMLineEditor");
55+
println!("cargo:rustc-link-lib=static=LLVMX86Disassembler");
56+
println!("cargo:rustc-link-lib=static=LLVMX86AsmParser");
57+
println!("cargo:rustc-link-lib=static=LLVMX86CodeGen");
58+
println!("cargo:rustc-link-lib=static=LLVMX86Desc");
59+
println!("cargo:rustc-link-lib=static=LLVMX86Info");
60+
println!("cargo:rustc-link-lib=static=LLVMOrcJIT");
61+
println!("cargo:rustc-link-lib=static=LLVMMCJIT");
62+
println!("cargo:rustc-link-lib=static=LLVMJITLink");
63+
println!("cargo:rustc-link-lib=static=LLVMInterpreter");
64+
println!("cargo:rustc-link-lib=static=LLVMExecutionEngine");
65+
println!("cargo:rustc-link-lib=static=LLVMRuntimeDyld");
66+
println!("cargo:rustc-link-lib=static=LLVMOrcTargetProcess");
67+
println!("cargo:rustc-link-lib=static=LLVMOrcShared");
68+
println!("cargo:rustc-link-lib=static=LLVMDWP");
69+
println!("cargo:rustc-link-lib=static=LLVMSymbolize");
70+
println!("cargo:rustc-link-lib=static=LLVMDebugInfoPDB");
71+
println!("cargo:rustc-link-lib=static=LLVMDebugInfoGSYM");
72+
println!("cargo:rustc-link-lib=static=LLVMOption");
73+
println!("cargo:rustc-link-lib=static=LLVMObjectYAML");
74+
println!("cargo:rustc-link-lib=static=LLVMMCA");
75+
println!("cargo:rustc-link-lib=static=LLVMMCDisassembler");
76+
println!("cargo:rustc-link-lib=static=LLVMLTO");
77+
println!("cargo:rustc-link-lib=static=LLVMPasses");
78+
println!("cargo:rustc-link-lib=static=LLVMCFGuard");
79+
println!("cargo:rustc-link-lib=static=LLVMCoroutines");
80+
println!("cargo:rustc-link-lib=static=LLVMObjCARCOpts");
81+
println!("cargo:rustc-link-lib=static=LLVMipo");
82+
println!("cargo:rustc-link-lib=static=LLVMVectorize");
83+
println!("cargo:rustc-link-lib=static=LLVMLinker");
84+
println!("cargo:rustc-link-lib=static=LLVMInstrumentation");
85+
println!("cargo:rustc-link-lib=static=LLVMFrontendOpenMP");
86+
println!("cargo:rustc-link-lib=static=LLVMFrontendOpenACC");
87+
println!("cargo:rustc-link-lib=static=LLVMExtensions");
88+
println!("cargo:rustc-link-lib=static=LLVMDWARFLinker");
89+
println!("cargo:rustc-link-lib=static=LLVMGlobalISel");
90+
println!("cargo:rustc-link-lib=static=LLVMMIRParser");
91+
println!("cargo:rustc-link-lib=static=LLVMAsmPrinter");
92+
println!("cargo:rustc-link-lib=static=LLVMDebugInfoMSF");
93+
println!("cargo:rustc-link-lib=static=LLVMDebugInfoDWARF");
94+
println!("cargo:rustc-link-lib=static=LLVMSelectionDAG");
95+
println!("cargo:rustc-link-lib=static=LLVMCodeGen");
96+
println!("cargo:rustc-link-lib=static=LLVMIRReader");
97+
println!("cargo:rustc-link-lib=static=LLVMAsmParser");
98+
println!("cargo:rustc-link-lib=static=LLVMInterfaceStub");
99+
println!("cargo:rustc-link-lib=static=LLVMFileCheck");
100+
println!("cargo:rustc-link-lib=static=LLVMFuzzMutate");
101+
println!("cargo:rustc-link-lib=static=LLVMTarget");
102+
println!("cargo:rustc-link-lib=static=LLVMScalarOpts");
103+
println!("cargo:rustc-link-lib=static=LLVMInstCombine");
104+
println!("cargo:rustc-link-lib=static=LLVMAggressiveInstCombine");
105+
println!("cargo:rustc-link-lib=static=LLVMTransformUtils");
106+
println!("cargo:rustc-link-lib=static=LLVMBitWriter");
107+
println!("cargo:rustc-link-lib=static=LLVMAnalysis");
108+
println!("cargo:rustc-link-lib=static=LLVMProfileData");
109+
println!("cargo:rustc-link-lib=static=LLVMObject");
110+
println!("cargo:rustc-link-lib=static=LLVMTextAPI");
111+
println!("cargo:rustc-link-lib=static=LLVMMCParser");
112+
println!("cargo:rustc-link-lib=static=LLVMMC");
113+
println!("cargo:rustc-link-lib=static=LLVMDebugInfoCodeView");
114+
println!("cargo:rustc-link-lib=static=LLVMBitReader");
115+
println!("cargo:rustc-link-lib=static=LLVMCore");
116+
println!("cargo:rustc-link-lib=static=LLVMRemarks");
117+
println!("cargo:rustc-link-lib=static=LLVMBitstreamReader");
118+
println!("cargo:rustc-link-lib=static=LLVMBinaryFormat");
119+
println!("cargo:rustc-link-lib=static=LLVMTableGen");
120+
println!("cargo:rustc-link-lib=static=LLVMSupport");
121+
println!("cargo:rustc-link-lib=static=LLVMDemangle");
122+
}
123+
124+
let wamr_header = wamr_root.join("core/iwasm/include/wasm_export.h");
125+
assert!(wamr_header.exists());
36126

37-
let wamr_header = format!("{wamr_root}core/iwasm/include/wasm_export.h");
38127
let bindings = bindgen::Builder::default()
39128
.ctypes_prefix("::core::ffi")
40129
.use_core()
41-
.header(wamr_header)
130+
.header(wamr_header.into_os_string().into_string().unwrap())
42131
.derive_default(true)
43132
.generate()
44133
.expect("Unable to generate bindings");

language-bindings/rust/resources/test/add-extra/Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
[package]
5+
name = "add_extra"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#[link(wasm_import_module = "host")]
7+
extern "C" {
8+
fn extra() -> u32;
9+
}
10+
11+
#[export_name = "add"]
12+
pub fn add_ex(m: u32, n: u32) -> u32 {
13+
m + n + unsafe { extra() }
14+
}
15+
16+
fn main() {
17+
println!("Hello, world! Please call add(10, 20) to see the result.");
18+
}
Binary file not shown.

language-bindings/rust/resources/test/gcd/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#[no_mangle]
6+
#[export_name = "gcd"]
77
pub fn gcd(m: u32, n: u32) -> u32 {
88
let mut a = m;
99
let mut b = n;
Binary file not shown.

language-bindings/rust/src/runtime.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::sync::{Arc, OnceLock};
1313

1414
use wamr_sys::{
1515
mem_alloc_type_t_Alloc_With_Pool, mem_alloc_type_t_Alloc_With_System_Allocator,
16-
wasm_runtime_destroy, wasm_runtime_full_init, wasm_runtime_init, RunningMode_Mode_Fast_JIT,
17-
RunningMode_Mode_Interp, RunningMode_Mode_LLVM_JIT, RuntimeInitArgs,
16+
wasm_runtime_destroy, wasm_runtime_full_init, wasm_runtime_init, RunningMode_Mode_Interp,
17+
RunningMode_Mode_LLVM_JIT, RuntimeInitArgs,
1818
};
1919

2020
use crate::RuntimeError;
@@ -43,22 +43,14 @@ impl RuntimeBuilder {
4343
self
4444
}
4545

46-
//TODO: feature
4746
/// use interpreter mode
4847
pub fn run_as_interpreter(mut self) -> RuntimeBuilder {
4948
self.args.running_mode = RunningMode_Mode_Interp;
5049
self
5150
}
5251

53-
//TODO: feature
54-
/// use fast-jit mode
55-
pub fn run_as_fast_jit(mut self, code_cache_size: u32) -> RuntimeBuilder {
56-
self.args.running_mode = RunningMode_Mode_Fast_JIT;
57-
self.args.fast_jit_code_cache_size = code_cache_size;
58-
self
59-
}
52+
/// TODO: use fast-jit mode
6053
61-
//TODO: feature
6254
/// use llvm-jit mode
6355
pub fn run_as_llvm_jit(mut self, opt_level: u32, size_level: u32) -> RuntimeBuilder {
6456
self.args.running_mode = RunningMode_Mode_LLVM_JIT;
@@ -145,6 +137,7 @@ impl Drop for Runtime {
145137
#[cfg(test)]
146138
mod tests {
147139
use super::*;
140+
use wamr_sys::{wasm_runtime_free, wasm_runtime_malloc};
148141

149142
#[test]
150143
fn test_runtime_new() {
@@ -164,13 +157,22 @@ mod tests {
164157
let runtime = Runtime::new();
165158
assert!(runtime.is_ok());
166159
}
160+
161+
let small_buf = unsafe { wasm_runtime_malloc(16) };
162+
assert!(!small_buf.is_null());
163+
unsafe { wasm_runtime_free(small_buf) };
167164
}
168165

169166
#[test]
170167
fn test_runtime_builder_default() {
168+
// use Mode_Default
171169
let runtime = Runtime::builder().use_system_allocator().build();
172170
assert!(runtime.is_ok());
173171
drop(runtime);
172+
173+
let small_buf = unsafe { wasm_runtime_malloc(16) };
174+
assert!(!small_buf.is_null());
175+
unsafe { wasm_runtime_free(small_buf) };
174176
}
175177

176178
#[test]
@@ -181,16 +183,10 @@ mod tests {
181183
.build();
182184
assert!(runtime.is_ok());
183185
drop(runtime);
184-
}
185186

186-
#[test]
187-
fn test_runtime_builder_fast_jit() {
188-
let runtime = Runtime::builder()
189-
.run_as_fast_jit(1024)
190-
.use_system_allocator()
191-
.build();
192-
assert!(runtime.is_ok());
193-
drop(runtime);
187+
let small_buf = unsafe { wasm_runtime_malloc(16) };
188+
assert!(!small_buf.is_null());
189+
unsafe { wasm_runtime_free(small_buf) };
194190
}
195191

196192
#[test]
@@ -201,5 +197,9 @@ mod tests {
201197
.build();
202198
assert!(runtime.is_ok());
203199
drop(runtime);
200+
201+
let small_buf = unsafe { wasm_runtime_malloc(16) };
202+
assert!(!small_buf.is_null());
203+
unsafe { wasm_runtime_free(small_buf) };
204204
}
205205
}

0 commit comments

Comments
 (0)