This is an automated email from the ASF dual-hosted git repository. jroesch pushed a commit to branch cargo-build in repository https://gitbox.apache.org/repos/asf/incubator-tvm.git
commit 20c6a28606c053fbf9adf1c36c85fd608e63e024 Author: Jared Roesch <roesch...@gmail.com> AuthorDate: Thu Oct 15 17:03:00 2020 -0700 Clean up exporting to show off new diagnostics --- rust/compiler-ext/src/lib.rs | 12 ++++++++++-- rust/tvm-rt/src/array.rs | 32 ++++++++++++++++++++++++++++++++ rust/tvm/src/bin/tyck.rs | 7 ++++++- rust/tvm/src/ir/diagnostics.rs | 10 +++++----- rust/tvm/src/ir/mod.rs | 1 + rust/tvm/src/ir/module.rs | 3 +++ rust/tvm/src/ir/source_map.rs | 26 +++++++++++++++----------- rust/tvm/src/lib.rs | 24 ++++++++++++++++++++++++ 8 files changed, 96 insertions(+), 19 deletions(-) diff --git a/rust/compiler-ext/src/lib.rs b/rust/compiler-ext/src/lib.rs index 3e37d21..c136d06 100644 --- a/rust/compiler-ext/src/lib.rs +++ b/rust/compiler-ext/src/lib.rs @@ -22,14 +22,22 @@ use tvm; use tvm::runtime::function::register_override; fn test_fn() -> Result<(), tvm::Error> { - println!("Hello from Rust!"); + println!("Hello Greg from Rust!"); Ok(()) } +fn test_fn2(message: tvm::runtime::string::String) -> Result<(), tvm::Error> { + println!("The message: {}", message); + Ok(()) +} + +tvm::export!(test_fn, test_fn2); + #[no_mangle] fn compiler_ext_initialize() -> i32 { let _ = env_logger::try_init(); - register_override(test_fn, "rust_ext.test_fn", true).expect("failed to initialize simplifier"); + tvm_export("rust_ext") + .expect("failed to initialize Rust compiler_ext"); log::debug!("done!"); return 0; } diff --git a/rust/tvm-rt/src/array.rs b/rust/tvm-rt/src/array.rs index 5e19cef..032ca79 100644 --- a/rust/tvm-rt/src/array.rs +++ b/rust/tvm-rt/src/array.rs @@ -19,6 +19,7 @@ use std::convert::{TryFrom, TryInto}; use std::marker::PhantomData; +use std::iter::{IntoIterator, Iterator}; use crate::errors::Error; use crate::object::{IsObjectRef, Object, ObjectPtr, ObjectRef}; @@ -81,6 +82,37 @@ impl<T: IsObjectRef> Array<T> { } } +pub struct IntoIter<T: IsObjectRef> { + array: Array<T>, + pos: isize, + size: isize, +} + +impl<T: IsObjectRef> Iterator for IntoIter<T> { + type Item = T; + + fn next(&mut self) -> Option<Self::Item> { + if self.pos < self.size { + let item = self.array.get(self.pos) + .expect("should not fail"); + self.pos += 1; + Some(item) + } else { + None + } + } +} + +impl<T: IsObjectRef> IntoIterator for Array<T> { + type Item = T; + type IntoIter = IntoIter<T>; + + fn into_iter(self) -> Self::IntoIter { + let size = self.len() as isize; + IntoIter { array: self, pos: 0, size: size } + } +} + impl<T: IsObjectRef> From<Array<T>> for ArgValue<'static> { fn from(array: Array<T>) -> ArgValue<'static> { array.object.into() diff --git a/rust/tvm/src/bin/tyck.rs b/rust/tvm/src/bin/tyck.rs index b869012..e0c7136 100644 --- a/rust/tvm/src/bin/tyck.rs +++ b/rust/tvm/src/bin/tyck.rs @@ -18,6 +18,11 @@ fn main() -> Result<()> { codespan::init().expect("Rust based diagnostics"); let opt = Opt::from_args(); println!("{:?}", &opt); - let file = IRModule::parse_file(opt.input)?; + let module = IRModule::parse_file(opt.input)?; + + // for (k, v) in module.functions { + // println!("Function name: {:?}", v); + // } + Ok(()) } diff --git a/rust/tvm/src/ir/diagnostics.rs b/rust/tvm/src/ir/diagnostics.rs index b76e43f..4975a45 100644 --- a/rust/tvm/src/ir/diagnostics.rs +++ b/rust/tvm/src/ir/diagnostics.rs @@ -135,6 +135,7 @@ pub struct DiagnosticRendererNode { pub base: Object, // TODO(@jroesch): we can't easily exposed packed functions due to // memory layout + // missing field here } // def render(self, ctx): @@ -283,11 +284,10 @@ pub mod codespan { pub fn init() -> Result<()> { let mut files: SimpleFiles<String, String> = SimpleFiles::new(); let render_fn = move |diag_ctx: DiagnosticContext| { - // let source_map = diag_ctx.module.source_map; - // for diagnostic in diag_ctx.diagnostics { - - // } - panic!("render_fn"); + let source_map = diag_ctx.module.source_map.clone(); + for diagnostic in diag_ctx.diagnostics.clone() { + println!("Diagnostic: {}", diagnostic.message); + } }; override_renderer(Some(render_fn))?; diff --git a/rust/tvm/src/ir/mod.rs b/rust/tvm/src/ir/mod.rs index 401b6c2..df9bc68 100644 --- a/rust/tvm/src/ir/mod.rs +++ b/rust/tvm/src/ir/mod.rs @@ -26,6 +26,7 @@ pub mod module; pub mod op; pub mod relay; pub mod span; +pub mod source_map; pub mod tir; pub mod ty; diff --git a/rust/tvm/src/ir/module.rs b/rust/tvm/src/ir/module.rs index e0444b3..5156e74 100644 --- a/rust/tvm/src/ir/module.rs +++ b/rust/tvm/src/ir/module.rs @@ -25,6 +25,7 @@ use crate::runtime::{external, Object, ObjectRef}; use super::expr::GlobalVar; use super::function::BaseFunc; +use super::source_map::SourceMap; use std::io::Result as IOResult; use std::path::Path; @@ -43,6 +44,8 @@ pub struct IRModuleNode { pub base: Object, pub functions: Map<GlobalVar, BaseFunc>, pub type_definitions: Map<GlobalTypeVar, TypeData>, + pub source_map: SourceMap, + // TODO(@jroesch): this is missing some fields } external! { diff --git a/rust/tvm/src/ir/source_map.rs b/rust/tvm/src/ir/source_map.rs index e6c0371..56c0830 100644 --- a/rust/tvm/src/ir/source_map.rs +++ b/rust/tvm/src/ir/source_map.rs @@ -12,7 +12,7 @@ * 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 + * KIND, either exprss or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -20,23 +20,27 @@ use crate::runtime::map::Map; use crate::runtime::object::Object; +use super::span::{SourceName, Span}; + +use tvm_macros::Object; + /// A program source in any language. /// /// Could represent the source from an ML framework or a source of an IRModule. #[repr(C)] #[derive(Object)] #[type_key = "Source"] -#[ref_key = "Source"] -struct SourceNode { +#[ref_name = "Source"] +pub struct SourceNode { pub base: Object, - /*! \brief The source name. */ - SourceName source_name; + /// The source name. */ + pub source_name: SourceName, - /*! \brief The raw source. */ - String source; + /// The raw source. */ + source: String, - /*! \brief A mapping of line breaks into the raw source. */ - std::vector<std::pair<int, int>> line_map; + // A mapping of line breaks into the raw source. + // std::vector<std::pair<int, int>> line_map; } @@ -53,8 +57,8 @@ struct SourceNode { #[repr(C)] #[derive(Object)] #[type_key = "SourceMap"] -#[ref_key = "SourceMap"] -struct SourceMapNode { +#[ref_name = "SourceMap"] +pub struct SourceMapNode { pub base: Object, /// The source mapping. pub source_map: Map<SourceName, Source>, diff --git a/rust/tvm/src/lib.rs b/rust/tvm/src/lib.rs index 36c7503..d193f09 100644 --- a/rust/tvm/src/lib.rs +++ b/rust/tvm/src/lib.rs @@ -47,3 +47,27 @@ pub mod runtime; pub mod transform; pub use runtime::version; + +#[macro_export] +macro_rules! export { + ($($fn_names:expr),*) => { + pub fn tvm_export(ns: &str) -> Result<(), tvm::Error> { + $( + register_override($fn_name, concat!($ns, stringfy!($fn_name)), true)?; + )* + Ok(()) + } + } +} + +#[macro_export] +macro_rules! export_mod { + ($ns:expr, $($mod_name:expr),*) => { + pub fn tvm_mod_export() -> Result<(), tvm::Error> { + $( + $mod_names::tvm_export($ns)?; + )* + Ok(()) + } + } +}