Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Fix the existing test cases before refactoring. #5122

Merged
merged 2 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
members = [
"common",
"macros",
"macros_raw",
"runtime",
"runtime/tests/test_tvm_basic",
"runtime/tests/test_tvm_dso",
Expand Down
1 change: 1 addition & 0 deletions rust/frontend/tests/callback/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name = "callback"
version = "0.0.0"
authors = ["TVM Contributors"]
edition = "2018"

[dependencies]
ndarray = "0.12"
Expand Down
5 changes: 1 addition & 4 deletions rust/frontend/tests/callback/src/bin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

use std::panic;

#[macro_use]
extern crate tvm_frontend as tvm;

use tvm::{errors::Error, *};
use tvm_frontend::{errors::Error, *};

fn main() {
register_global_func! {
Expand Down
9 changes: 7 additions & 2 deletions rust/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
name = "tvm-macros"
version = "0.1.1"
license = "Apache-2.0"
description = "Proc macros used by the TVM crates."
description = "Procedural macros of the TVM crate."
repository = "https://github.com/apache/incubator-tvm"
readme = "README.md"
keywords = ["tvm"]
authors = ["TVM Contributors"]
edition = "2018"

[lib]
proc-macro = true

[dependencies]
tvm-macros-raw = { path = "../macros_raw" }
goblin = "0.0.24"
proc-macro2 = "^1.0"
quote = "1.0"
syn = "1.0"
123 changes: 117 additions & 6 deletions rust/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,123 @@
* under the License.
*/

#[macro_use]
extern crate tvm_macros_raw;
extern crate proc_macro;

#[macro_export]
macro_rules! import_module {
($module_path:literal) => {
$crate::import_module_raw!(file!(), $module_path);
use std::{fs::File, io::Read};
use syn::parse::{Parse, ParseStream, Result};
use syn::{LitStr};
use quote::quote;

use std::path::PathBuf;

struct ImportModule {
importing_file: LitStr,
}

impl Parse for ImportModule {
fn parse(input: ParseStream) -> Result<Self> {
let importing_file: LitStr = input.parse()?;
Ok(ImportModule {
importing_file,
})
}
}

#[proc_macro]
pub fn import_module(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let import_module_args = syn::parse_macro_input!(input as ImportModule);

let manifest = std::env::var("CARGO_MANIFEST_DIR")
.expect("variable should always be set by Cargo.");

let mut path = PathBuf::new();
path.push(manifest);
path = path.join(import_module_args.importing_file.value());

let mut fd = File::open(&path)
.unwrap_or_else(|_| panic!("Unable to find TVM object file at `{}`", path.display()));
let mut buffer = Vec::new();
fd.read_to_end(&mut buffer).unwrap();

let fn_names = match goblin::Object::parse(&buffer).unwrap() {
goblin::Object::Elf(elf) => elf
.syms
.iter()
.filter_map(|s| {
if s.st_type() == 0 || goblin::elf::sym::type_to_str(s.st_type()) == "FILE" {
return None;
}
match elf.strtab.get(s.st_name) {
Some(Ok(name)) if name != "" => {
Some(syn::Ident::new(name, proc_macro2::Span::call_site()))
}
_ => None,
}
})
.collect::<Vec<_>>(),
goblin::Object::Mach(goblin::mach::Mach::Binary(obj)) => {
obj.symbols()
.filter_map(|s| match s {
Ok((name, ref nlist))
if nlist.is_global()
&& nlist.n_sect != 0
&& !name.ends_with("tvm_module_ctx") =>
{
Some(syn::Ident::new(
if name.starts_with('_') {
// Mach objects prepend a _ to globals.
&name[1..]
} else {
&name
},
proc_macro2::Span::call_site(),
))
}
_ => None,
})
.collect::<Vec<_>>()
}
_ => panic!("Unsupported object format."),
};

let extern_fns = quote! {
mod ext {
extern "C" {
#(
pub(super) fn #fn_names(
args: *const tvm_runtime::ffi::TVMValue,
type_codes: *const std::os::raw::c_int,
num_args: std::os::raw::c_int
) -> std::os::raw::c_int;
)*
}
}
};

let fns = quote! {
use tvm_runtime::{ffi::TVMValue, TVMArgValue, TVMRetValue, FuncCallError};
#extern_fns

#(
pub fn #fn_names(args: &[TVMArgValue]) -> Result<TVMRetValue, FuncCallError> {
let (values, type_codes): (Vec<TVMValue>, Vec<i32>) = args
.into_iter()
.map(|arg| {
let (val, code) = arg.to_tvm_value();
(val, code as i32)
})
.unzip();
let exit_code = unsafe {
ext::#fn_names(values.as_ptr(), type_codes.as_ptr(), values.len() as i32)
};
if exit_code == 0 {
Ok(TVMRetValue::default())
} else {
Err(FuncCallError::get_with_context(stringify!(#fn_names).to_string()))
}
}
)*
};

proc_macro::TokenStream::from(fns)
}
36 changes: 0 additions & 36 deletions rust/macros_raw/Cargo.toml

This file was deleted.

141 changes: 0 additions & 141 deletions rust/macros_raw/src/lib.rs

This file was deleted.

14 changes: 12 additions & 2 deletions rust/runtime/tests/test_nn/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ fn main() {
.unwrap_or("")
);

let mut builder = Builder::new(File::create(format!("{}/libgraph.a", out_dir)).unwrap());
let lib_file = format!("{}/libtestnn.a", out_dir);
let file = File::create(&lib_file).unwrap();
let mut builder = Builder::new(file);
builder.append_path(format!("{}/graph.o", out_dir)).unwrap();

println!("cargo:rustc-link-lib=static=graph");
let status = Command::new("ranlib")
.arg(&lib_file)
.status()
.expect("fdjlksafjdsa");

assert!(status.success());


println!("cargo:rustc-link-lib=static=testnn");
println!("cargo:rustc-link-search=native={}", out_dir);
}
Loading