Skip to content

Commit ef2a3b3

Browse files
committed
enable llvm-jit mode
1 parent 375c372 commit ef2a3b3

File tree

10 files changed

+159
-36
lines changed

10 files changed

+159
-36
lines changed

language-bindings/rust/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ license = "Apache-2.0 WITH LLVM-exception"
1111
wamr-sys = { path = "crates/wamr-sys", version = "0.1.0" }
1212

1313
[workspace]
14-
members = ["crates/wamr-sys"]
15-
exclude = ["examples/wasi-hello", "resources/test/hello"]
14+
members = ["crates/wamr-sys", ]
15+
exclude = ["examples/wasi-hello", "resources/test/gcd", "resources/test/add-extra"]

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

+1
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

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

+99-13
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ 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 dst = Config::new(&wamr_root)
1720
// running mode
21+
.define("WAMR_BUILD_AOT", "1")
1822
.define("WAMR_BUILD_INTERP", "1")
1923
.define("WAMR_BUILD_FAST_INTERP", "1")
20-
// .define("WAMR_BUILD_JIT", "1")
24+
.define("WAMR_BUILD_JIT", "1")
2125
// mvp
2226
.define("WAMR_BUILD_BULK_MEMORY", "1")
2327
.define("WAMR_BUILD_REF_TYPES", "1")
@@ -26,19 +30,101 @@ fn main() {
2630
.define("WAMR_BUILD_LIBC_WASI", "1")
2731
// `nostdlib`
2832
.define("WAMR_BUILD_LIBC_BUILTIN", "1")
33+
.build_target("iwasm_static")
2934
.build();
3035

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

37-
let wamr_header = format!("{wamr_root}core/iwasm/include/wasm_export.h");
38124
let bindings = bindgen::Builder::default()
39125
.ctypes_prefix("::core::ffi")
40126
.use_core()
41-
.header(wamr_header)
127+
.header(wamr_header.into_os_string().into_string().unwrap())
42128
.derive_default(true)
43129
.generate()
44130
.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)