This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch ci-docker-staging in repository https://gitbox.apache.org/repos/asf/tvm.git
commit 5651bd62ad698587ee4eeb8c157192d90a0d3cc0 Author: Jared Roesch <jroe...@octoml.ai> AuthorDate: Thu Feb 18 22:49:57 2021 -0800 Fixes for version bump --- rust/tvm-rt/Cargo.toml | 4 +- rust/tvm-rt/src/ndarray.rs | 4 +- rust/tvm-sys/Cargo.toml | 8 +-- rust/tvm-sys/build.rs | 114 +++++++++++++++++++++++++---------------- rust/tvm-sys/src/byte_array.rs | 8 +-- 5 files changed, 85 insertions(+), 53 deletions(-) diff --git a/rust/tvm-rt/Cargo.toml b/rust/tvm-rt/Cargo.toml index 13c0537..03d12d4 100644 --- a/rust/tvm-rt/Cargo.toml +++ b/rust/tvm-rt/Cargo.toml @@ -30,8 +30,8 @@ edition = "2018" [features] default = ["dynamic-linking"] -dynamic-linking = ["tvm-sys/bindings"] -static-linking = [] +dynamic-linking = ["tvm-sys/dynamic-linking"] +static-linking = ["tvm-sys/static-linking"] blas = ["ndarray/blas"] [dependencies] diff --git a/rust/tvm-rt/src/ndarray.rs b/rust/tvm-rt/src/ndarray.rs index 4c48ce5..0e2d283 100644 --- a/rust/tvm-rt/src/ndarray.rs +++ b/rust/tvm-rt/src/ndarray.rs @@ -287,7 +287,7 @@ impl NDArray { check_call!(ffi::TVMArrayCopyFromBytes( self.as_raw_dltensor(), data.as_ptr() as *mut _, - data.len() * mem::size_of::<T>() + (data.len() * mem::size_of::<T>()) as _, )); } @@ -296,7 +296,7 @@ impl NDArray { check_call!(ffi::TVMArrayCopyToBytes( self.as_raw_dltensor(), data.as_ptr() as *mut _, - self.size(), + self.size() as _, )); } diff --git a/rust/tvm-sys/Cargo.toml b/rust/tvm-sys/Cargo.toml index 2952aa4..6075421 100644 --- a/rust/tvm-sys/Cargo.toml +++ b/rust/tvm-sys/Cargo.toml @@ -23,8 +23,9 @@ license = "Apache-2.0" edition = "2018" [features] -default = [] -bindings = [] +default = ["dynamic-linking"] +static-linking = [] +dynamic-linking = [] [dependencies] thiserror = "^1.0" @@ -33,5 +34,6 @@ ndarray = "0.12" enumn = "^0.1" [build-dependencies] -bindgen = { version="0.51", default-features=false } +bindgen = { version="0.57", default-features=false } anyhow = "^1.0" +tvm-build = { git = "https://github.com/octoml/tvm-build", branch = "main" } diff --git a/rust/tvm-sys/build.rs b/rust/tvm-sys/build.rs index 1590234..0638f71 100644 --- a/rust/tvm-sys/build.rs +++ b/rust/tvm-sys/build.rs @@ -19,65 +19,93 @@ extern crate bindgen; -use std::env; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; +use tvm_build::BuildConfig; + +fn find_using_tvm_path<P: AsRef<Path>>(tvm_path: P) -> Result<TVMInstall> { + Ok(TVMInstall { + source_path: tvm_path.as_ref().into(), + build_path: tvm_path.as_ref().into(), + }) +} + +fn if_unset<K: AsRef<std::ffi::OsStr>, V: AsRef<std::ffi::OsStr>>(k: K, v: V) -> Result<()> { + match std::env::var(k.as_ref()) { + Ok(other) if other != "" => { + println!("cargo:warning=Using existing environment variable setting {:?}={:?}", k.as_ref(), v.as_ref()); + } + _ => std::env::set_var(k, v) + } + + Ok(()) +} + +fn set_env_vars() -> Result<()> { + if_unset("LIBCLANG_PATH", "/opt/homebrew/opt/llvm/lib")?; + if_unset("LLVM_CONFIG_PATH", "/opt/homebrew/opt/llvm/bin/llvm-config") +} + +struct TVMInstall { + source_path: PathBuf, + build_path: PathBuf, +} + +fn find_using_tvm_build() -> Result<TVMInstall> { + set_env_vars()?; + let mut build_config = BuildConfig::default(); + build_config.repository = Some("https://github.com/jroesch/tvm".to_string()); + build_config.branch = Some("rust-tvm-build".to_string()); + let build_result = tvm_build::build(build_config)?; + let source_path = build_result.revision_path.join("source"); + let build_path = build_result.revision_path.join("build"); + Ok(TVMInstall { + source_path, + build_path, + }) +} fn main() -> Result<()> { - let tvm_home = option_env!("TVM_HOME") - .map::<Result<String>, _>(|s: &str| Ok(str::to_string(s))) - .unwrap_or_else(|| { - let crate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .canonicalize() - .with_context(|| { - format!( - "failed to cannonicalize() CARGO_MANIFEST_DIR={}", - env!("CARGO_MANIFEST_DIR") - ) - })?; - - Ok(crate_dir - .parent() - .with_context(|| { - format!( - "failed to find parent of CARGO_MANIFEST_DIR={}", - env!("CARGO_MANIFEST_DIR") - ) - })? - .parent() - .with_context(|| { - format!( - "failed to find the parent of the parent of CARGO MANIFEST_DIR={}", - env!("CARGO_MANIFEST_DIR") - ) - })? - .to_str() - .context("failed to convert to strings")? - .to_string()) - })?; - - if cfg!(feature = "bindings") { - println!("cargo:rerun-if-env-changed=TVM_HOME"); + let TVMInstall { source_path, build_path } = match option_env!("TVM_HOME") { + Some(tvm_path) if tvm_path != "" => find_using_tvm_path(tvm_path), + _ => find_using_tvm_build(), + }?; + + println!("cargo:rerun-if-env-changed=TVM_HOME"); + + if cfg!(feature = "static-linking") { + println!("cargo:rustc-link-lib=static=tvm"); + println!("cargo:rustc-link-search=native={}", build_path.display()); + + } + + if cfg!(feature = "dynamic-linking") { println!("cargo:rustc-link-lib=dylib=tvm"); - println!("cargo:rustc-link-search=native={}/build", tvm_home); + println!("cargo:rustc-link-search=native={}", build_path.display()); } + let runtime_api = source_path.join("include/tvm/runtime/c_runtime_api.h"); + let backend_api = source_path.join("include/tvm/runtime/c_backend_api.h"); + let source_path = source_path.display().to_string(); + let dlpack_include = format!("-I{}/3rdparty/dlpack/include/", source_path); + let tvm_include = format!("-I{}/include/", source_path); + // @see rust-bindgen#550 for `blacklist_type` bindgen::Builder::default() - .header(format!("{}/include/tvm/runtime/c_runtime_api.h", tvm_home)) - .header(format!("{}/include/tvm/runtime/c_backend_api.h", tvm_home)) - .clang_arg(format!("-I{}/3rdparty/dlpack/include/", tvm_home)) - .clang_arg(format!("-I{}/include/", tvm_home)) + .header(runtime_api.display().to_string()) + .header(backend_api.display().to_string()) + .clang_arg(dlpack_include) + .clang_arg(tvm_include) .blacklist_type("max_align_t") .layout_tests(false) .derive_partialeq(true) .derive_eq(true) .derive_default(true) .generate() - .map_err(|()| anyhow::anyhow!("failed to generate bindings"))? + .map_err(|()| anyhow::anyhow!("bindgen failed to generate the Rust bindings for the C API"))? .write_to_file(PathBuf::from("src/c_runtime_api.rs")) - .context("failed to write bindings")?; + .context("failed to write the generated Rust binding to disk")?; Ok(()) } diff --git a/rust/tvm-sys/src/byte_array.rs b/rust/tvm-sys/src/byte_array.rs index 0f02771..b1b75fc 100644 --- a/rust/tvm-sys/src/byte_array.rs +++ b/rust/tvm-sys/src/byte_array.rs @@ -41,12 +41,14 @@ pub struct ByteArray { impl ByteArray { /// Gets the underlying byte-array pub fn data(&self) -> &'static [u8] { - unsafe { std::slice::from_raw_parts(self.array.data as *const u8, self.array.size) } + unsafe { std::slice::from_raw_parts( + self.array.data as *const u8, + self.array.size as _) } } /// Gets the length of the underlying byte-array pub fn len(&self) -> usize { - self.array.size + self.array.size as _ } /// Converts the underlying byte-array to `Vec<u8>` @@ -66,7 +68,7 @@ impl<T: AsRef<[u8]>> From<T> for ByteArray { ByteArray { array: TVMByteArray { data: arg.as_ptr() as *const c_char, - size: arg.len(), + size: arg.len() as _, }, } }