-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rust] Fixes for wasm32 target (#5489)
* [Rust] Fixes for wasm32 target * [Rust] Add test for wasm32 * allow cargo config to be into repo * Disable wasm tests in CI
- Loading branch information
Showing
15 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "wasm32-wasi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
[package] | ||
name = "test-wasm32" | ||
version = "0.0.0" | ||
license = "Apache-2.0" | ||
authors = ["TVM Contributors"] | ||
|
||
[dependencies] | ||
ndarray="0.12" | ||
tvm-runtime = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
use std::{path::PathBuf, process::Command}; | ||
|
||
fn main() { | ||
let mut out_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
out_dir.push("lib"); | ||
|
||
if !out_dir.is_dir() { | ||
std::fs::create_dir(&out_dir).unwrap(); | ||
} | ||
|
||
let obj_file = out_dir.join("test.o"); | ||
let lib_file = out_dir.join("libtest_wasm32.a"); | ||
|
||
let output = Command::new(concat!( | ||
env!("CARGO_MANIFEST_DIR"), | ||
"/src/build_test_lib.py" | ||
)) | ||
.arg(&out_dir) | ||
.output() | ||
.expect("Failed to execute command"); | ||
assert!( | ||
obj_file.exists(), | ||
"Could not build tvm lib: {}", | ||
String::from_utf8(output.stderr) | ||
.unwrap() | ||
.trim() | ||
.split("\n") | ||
.last() | ||
.unwrap_or("") | ||
); | ||
|
||
let ar = option_env!("LLVM_AR").unwrap_or("llvm-ar-8"); | ||
let output = Command::new(ar) | ||
.arg("rcs") | ||
.arg(&lib_file) | ||
.arg(&obj_file) | ||
.output() | ||
.expect("Failed to execute command"); | ||
assert!( | ||
lib_file.exists(), | ||
"Could not create archive: {}", | ||
String::from_utf8(output.stderr) | ||
.unwrap() | ||
.trim() | ||
.split("\n") | ||
.last() | ||
.unwrap_or("") | ||
); | ||
|
||
println!("cargo:rustc-link-lib=static=test_wasm32"); | ||
println!("cargo:rustc-link-search=native={}", out_dir.display()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env python3 | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Prepares a simple TVM library for testing.""" | ||
|
||
from os import path as osp | ||
import sys | ||
|
||
import tvm | ||
from tvm import te | ||
|
||
def main(): | ||
n = te.var('n') | ||
A = te.placeholder((n,), name='A') | ||
B = te.placeholder((n,), name='B') | ||
C = te.compute(A.shape, lambda *i: A(*i) + B(*i), name='C') | ||
s = tvm.te.create_schedule(C.op) | ||
s[C].parallel(s[C].op.axis[0]) | ||
print(tvm.lower(s, [A, B, C], simple_mode=True)) | ||
tvm.build(s, [A, B, C], 'llvm -target=wasm32-unknown-unknown --system-lib').save(osp.join(sys.argv[1], 'test.o')) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
extern "C" { | ||
static __tvm_module_ctx: i32; | ||
} | ||
|
||
#[no_mangle] | ||
unsafe fn __get_tvm_module_ctx() -> i32 { | ||
// Refer a symbol in the libtest_wasm32.a to make sure that the link of the | ||
// library is not optimized out. | ||
__tvm_module_ctx | ||
} | ||
|
||
extern crate ndarray; | ||
#[macro_use] | ||
extern crate tvm_runtime; | ||
|
||
use ndarray::Array; | ||
use tvm_runtime::{DLTensor, Module as _, SystemLibModule}; | ||
|
||
fn main() { | ||
// try static | ||
let mut a = Array::from_vec(vec![1f32, 2., 3., 4.]); | ||
let mut b = Array::from_vec(vec![1f32, 0., 1., 0.]); | ||
let mut c = Array::from_vec(vec![0f32; 4]); | ||
let e = Array::from_vec(vec![2f32, 2., 4., 4.]); | ||
let mut a_dl: DLTensor = (&mut a).into(); | ||
let mut b_dl: DLTensor = (&mut b).into(); | ||
let mut c_dl: DLTensor = (&mut c).into(); | ||
|
||
let syslib = SystemLibModule::default(); | ||
let add = syslib | ||
.get_function("default_function") | ||
.expect("main function not found"); | ||
call_packed!(add, &mut a_dl, &mut b_dl, &mut c_dl).unwrap(); | ||
assert!(c.all_close(&e, 1e-8f32)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters