Seven-Streams commented on code in PR #685:
URL: https://github.com/apache/tvm-ffi/pull/685#discussion_r3652825800
##########
rust/tvm-ffi-macros/src/match_any.rs:
##########
@@ -24,6 +24,11 @@ use syn::{braced, parenthesized, Expr, Pat, Path, Result,
Token};
use crate::utils::get_tvm_ffi_crate;
+// Keep single-arm matches ordered. Release benchmarks of the standard
ObjectRef
+// conversion path show that lookup pays off from two arms when a later arm or
+// the fallback is reached.
+const MIN_LOOKUP_TABLE_ARMS: usize = 2;
Review Comment:
only 2?
##########
rust/tvm-ffi-macros/src/match_any.rs:
##########
@@ -105,41 +110,225 @@ pub fn expand(input: proc_macro::TokenStream) ->
proc_macro::TokenStream {
fn expand_match_any(input: MatchAnyInput) -> TokenStream {
let tvm_ffi = get_tvm_ffi_crate();
+ let scrutinee = input.scrutinee;
+ let fallback = input.fallback;
+ let arms = input.arms;
+ let can_attempt_leaf_lookup = arms.len() >= MIN_LOOKUP_TABLE_ARMS
+ && arms
+ .iter()
+ .all(|arm| arm.guard.is_none() && is_simple_binding(&arm.binding));
+
+ if can_attempt_leaf_lookup {
+ expand_leaf_lookup_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ } else {
+ expand_ordered_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ }
+}
+
+fn expand_ordered_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
let span = Span::mixed_site();
let source = Ident::new("__tvm_ffi_match_any_source", span);
let converted = Ident::new("__tvm_ffi_match_any_converted", span);
let view = Ident::new("__tvm_ffi_match_any_view", span);
let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
- let scrutinee = input.scrutinee;
- let fallback = input.fallback;
- let dispatch_fallback = fallback.clone();
- let arms = input.arms;
- let dispatch = arms
- .into_iter()
- .rev()
- .fold(quote!({ #dispatch_fallback }), |next, arm| {
- let matcher = arm.matcher;
- let binding = arm.binding;
- let body = arm.body;
- let matched = if let Some(guard) = arm.guard {
- quote!(::core::result::Result::Ok(#binding) if #guard)
- } else {
- quote!(::core::result::Result::Ok(#binding))
+ let dispatch = expand_ordered_dispatch(arms, fallback, &view, &rejected);
+
+ quote! {
+ {
+ let #source = &(#scrutinee);
+ let #converted: ::core::result::Result<
+ #tvm_ffi::AnyView<'_>,
+ ::core::convert::Infallible,
+ > =
::core::convert::TryInto::<#tvm_ffi::AnyView<'_>>::try_into(#source);
+ let #view = match #converted {
+ ::core::result::Result::Ok(view) => view,
+ ::core::result::Result::Err(error) => match error {},
};
+ if #view.type_index()
+ >= #tvm_ffi::TypeIndex::kTVMFFIStaticObjectBegin as i32
+ {
+ #dispatch
+ } else {
+ #fallback
+ }
+ }
+ }
+}
+
+fn expand_ordered_dispatch(
+ arms: &[TypedArm],
+ fallback: &Expr,
+ view: &Ident,
+ rejected: &Ident,
+) -> TokenStream {
+ arms.iter().rev().fold(quote!({ #fallback }), |next, arm| {
+ let matcher = &arm.matcher;
+ let binding = &arm.binding;
+ let body = &arm.body;
+ let matched = if let Some(guard) = &arm.guard {
+ quote!(::core::result::Result::Ok(#binding) if #guard)
+ } else {
+ quote!(::core::result::Result::Ok(#binding))
+ };
+
+ quote! {
+ match ::core::convert::TryInto::<#matcher>::try_into(#view) {
+ #matched => { #body },
+ #rejected => {
+ ::core::mem::drop(#rejected);
+ #next
+ }
+ }
+ }
+ })
+}
+
+fn expand_leaf_lookup_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
+ let span = Span::mixed_site();
+ let source = Ident::new("__tvm_ffi_match_any_source", span);
+ let converted = Ident::new("__tvm_ffi_match_any_converted", span);
+ let view = Ident::new("__tvm_ffi_match_any_view", span);
+ let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
+ let probe = Ident::new("__tvm_ffi_match_any_probe", span);
+ let pattern_list_id =
Ident::new("__tvm_ffi_match_any_leaf_pattern_list_id", span);
+ let type_indices = Ident::new("__tvm_ffi_match_any_type_indices", span);
+ let static_table = Ident::new("__TVM_FFI_MATCH_ANY_LEAF_TABLE", span);
+ let table = Ident::new("__tvm_ffi_match_any_leaf_table", span);
+ let arm_id = Ident::new("__tvm_ffi_match_any_arm_id", span);
+ let selected = Ident::new("__tvm_ffi_match_any_selected", span);
+ let selected_value = Ident::new("__tvm_ffi_match_any_selected_value",
span);
+ let selected_enum = Ident::new("__TvmFfiMatchAnyArm", span);
+ let fallback_variant = Ident::new("Fallback", span);
+ let arm_count = arms.len();
+ let matchers = arms.iter().map(|arm| &arm.matcher).collect::<Vec<_>>();
+ let arm_types = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("__TvmFfiMatchAnyType{arm_id}"),
span))
+ .collect::<Vec<_>>();
+ let arm_variants = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("Arm{arm_id}"), span))
+ .collect::<Vec<_>>();
+ let arm_constants = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("__TVM_FFI_MATCH_ANY_ARM_{arm_id}"),
span))
+ .collect::<Vec<_>>();
+ let arm_constant_definitions =
+ arm_constants
+ .iter()
+ .enumerate()
+ .map(|(arm_id, arm_constant)| {
+ quote! {
+ const #arm_constant: #tvm_ffi::match_any::ArmId =
+ #arm_id as #tvm_ffi::match_any::ArmId;
+ }
+ });
+ let lookup_entries = arm_constants
+ .iter()
+ .enumerate()
+ .map(|(arm_id, arm_constant)| quote!((#type_indices[#arm_id],
#arm_constant)));
+ let pattern_list = matchers
+ .iter()
+ .rev()
+ .fold(quote!(()), |tail, matcher| quote!((#matcher, #tail)));
+
+ let lookup_arm_id = quote! {
+ {
+ use #tvm_ffi::match_any::LeafPatternMetadata as _;
+
+ let #probe =
+ #tvm_ffi::match_any::LeafPatternProbe::<#pattern_list>::new();
+ match (&#probe).leaf_pattern_list_id() {
+ ::core::option::Option::Some(#pattern_list_id) => {
+ static #static_table: ::std::sync::OnceLock<
Review Comment:
one problem is that when the arm is something like `T()`, where `T` is
related to the external function, then the table may never be used
##########
rust/tvm-ffi-macros/src/match_any.rs:
##########
@@ -105,41 +110,225 @@ pub fn expand(input: proc_macro::TokenStream) ->
proc_macro::TokenStream {
fn expand_match_any(input: MatchAnyInput) -> TokenStream {
let tvm_ffi = get_tvm_ffi_crate();
+ let scrutinee = input.scrutinee;
+ let fallback = input.fallback;
+ let arms = input.arms;
+ let can_attempt_leaf_lookup = arms.len() >= MIN_LOOKUP_TABLE_ARMS
+ && arms
+ .iter()
+ .all(|arm| arm.guard.is_none() && is_simple_binding(&arm.binding));
+
+ if can_attempt_leaf_lookup {
+ expand_leaf_lookup_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ } else {
+ expand_ordered_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ }
+}
+
+fn expand_ordered_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
let span = Span::mixed_site();
let source = Ident::new("__tvm_ffi_match_any_source", span);
let converted = Ident::new("__tvm_ffi_match_any_converted", span);
let view = Ident::new("__tvm_ffi_match_any_view", span);
let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
- let scrutinee = input.scrutinee;
- let fallback = input.fallback;
- let dispatch_fallback = fallback.clone();
- let arms = input.arms;
- let dispatch = arms
- .into_iter()
- .rev()
- .fold(quote!({ #dispatch_fallback }), |next, arm| {
- let matcher = arm.matcher;
- let binding = arm.binding;
- let body = arm.body;
- let matched = if let Some(guard) = arm.guard {
- quote!(::core::result::Result::Ok(#binding) if #guard)
- } else {
- quote!(::core::result::Result::Ok(#binding))
+ let dispatch = expand_ordered_dispatch(arms, fallback, &view, &rejected);
+
+ quote! {
+ {
+ let #source = &(#scrutinee);
+ let #converted: ::core::result::Result<
+ #tvm_ffi::AnyView<'_>,
+ ::core::convert::Infallible,
+ > =
::core::convert::TryInto::<#tvm_ffi::AnyView<'_>>::try_into(#source);
+ let #view = match #converted {
+ ::core::result::Result::Ok(view) => view,
+ ::core::result::Result::Err(error) => match error {},
};
+ if #view.type_index()
+ >= #tvm_ffi::TypeIndex::kTVMFFIStaticObjectBegin as i32
+ {
+ #dispatch
+ } else {
+ #fallback
+ }
+ }
+ }
+}
+
+fn expand_ordered_dispatch(
+ arms: &[TypedArm],
+ fallback: &Expr,
+ view: &Ident,
+ rejected: &Ident,
+) -> TokenStream {
+ arms.iter().rev().fold(quote!({ #fallback }), |next, arm| {
+ let matcher = &arm.matcher;
+ let binding = &arm.binding;
+ let body = &arm.body;
+ let matched = if let Some(guard) = &arm.guard {
+ quote!(::core::result::Result::Ok(#binding) if #guard)
+ } else {
+ quote!(::core::result::Result::Ok(#binding))
+ };
+
+ quote! {
+ match ::core::convert::TryInto::<#matcher>::try_into(#view) {
+ #matched => { #body },
+ #rejected => {
+ ::core::mem::drop(#rejected);
+ #next
+ }
+ }
+ }
+ })
+}
+
+fn expand_leaf_lookup_match(
Review Comment:
breaking it into several parts may be better for understanding.
##########
rust/tvm-ffi-macros/src/match_any.rs:
##########
@@ -105,41 +110,225 @@ pub fn expand(input: proc_macro::TokenStream) ->
proc_macro::TokenStream {
fn expand_match_any(input: MatchAnyInput) -> TokenStream {
let tvm_ffi = get_tvm_ffi_crate();
+ let scrutinee = input.scrutinee;
+ let fallback = input.fallback;
+ let arms = input.arms;
+ let can_attempt_leaf_lookup = arms.len() >= MIN_LOOKUP_TABLE_ARMS
+ && arms
+ .iter()
+ .all(|arm| arm.guard.is_none() && is_simple_binding(&arm.binding));
+
+ if can_attempt_leaf_lookup {
+ expand_leaf_lookup_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ } else {
+ expand_ordered_match(&tvm_ffi, &scrutinee, &arms, &fallback)
+ }
+}
+
+fn expand_ordered_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
let span = Span::mixed_site();
let source = Ident::new("__tvm_ffi_match_any_source", span);
let converted = Ident::new("__tvm_ffi_match_any_converted", span);
let view = Ident::new("__tvm_ffi_match_any_view", span);
let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
- let scrutinee = input.scrutinee;
- let fallback = input.fallback;
- let dispatch_fallback = fallback.clone();
- let arms = input.arms;
- let dispatch = arms
- .into_iter()
- .rev()
- .fold(quote!({ #dispatch_fallback }), |next, arm| {
- let matcher = arm.matcher;
- let binding = arm.binding;
- let body = arm.body;
- let matched = if let Some(guard) = arm.guard {
- quote!(::core::result::Result::Ok(#binding) if #guard)
- } else {
- quote!(::core::result::Result::Ok(#binding))
+ let dispatch = expand_ordered_dispatch(arms, fallback, &view, &rejected);
+
+ quote! {
+ {
+ let #source = &(#scrutinee);
+ let #converted: ::core::result::Result<
+ #tvm_ffi::AnyView<'_>,
+ ::core::convert::Infallible,
+ > =
::core::convert::TryInto::<#tvm_ffi::AnyView<'_>>::try_into(#source);
+ let #view = match #converted {
+ ::core::result::Result::Ok(view) => view,
+ ::core::result::Result::Err(error) => match error {},
};
+ if #view.type_index()
+ >= #tvm_ffi::TypeIndex::kTVMFFIStaticObjectBegin as i32
+ {
+ #dispatch
+ } else {
+ #fallback
+ }
+ }
+ }
+}
+
+fn expand_ordered_dispatch(
+ arms: &[TypedArm],
+ fallback: &Expr,
+ view: &Ident,
+ rejected: &Ident,
+) -> TokenStream {
+ arms.iter().rev().fold(quote!({ #fallback }), |next, arm| {
+ let matcher = &arm.matcher;
+ let binding = &arm.binding;
+ let body = &arm.body;
+ let matched = if let Some(guard) = &arm.guard {
+ quote!(::core::result::Result::Ok(#binding) if #guard)
+ } else {
+ quote!(::core::result::Result::Ok(#binding))
+ };
+
+ quote! {
+ match ::core::convert::TryInto::<#matcher>::try_into(#view) {
+ #matched => { #body },
+ #rejected => {
+ ::core::mem::drop(#rejected);
+ #next
+ }
+ }
+ }
+ })
+}
+
+fn expand_leaf_lookup_match(
+ tvm_ffi: &TokenStream,
+ scrutinee: &Expr,
+ arms: &[TypedArm],
+ fallback: &Expr,
+) -> TokenStream {
+ let span = Span::mixed_site();
+ let source = Ident::new("__tvm_ffi_match_any_source", span);
+ let converted = Ident::new("__tvm_ffi_match_any_converted", span);
+ let view = Ident::new("__tvm_ffi_match_any_view", span);
+ let rejected = Ident::new("__tvm_ffi_match_any_rejected", span);
+ let probe = Ident::new("__tvm_ffi_match_any_probe", span);
+ let pattern_list_id =
Ident::new("__tvm_ffi_match_any_leaf_pattern_list_id", span);
+ let type_indices = Ident::new("__tvm_ffi_match_any_type_indices", span);
+ let static_table = Ident::new("__TVM_FFI_MATCH_ANY_LEAF_TABLE", span);
+ let table = Ident::new("__tvm_ffi_match_any_leaf_table", span);
+ let arm_id = Ident::new("__tvm_ffi_match_any_arm_id", span);
+ let selected = Ident::new("__tvm_ffi_match_any_selected", span);
+ let selected_value = Ident::new("__tvm_ffi_match_any_selected_value",
span);
+ let selected_enum = Ident::new("__TvmFfiMatchAnyArm", span);
+ let fallback_variant = Ident::new("Fallback", span);
+ let arm_count = arms.len();
+ let matchers = arms.iter().map(|arm| &arm.matcher).collect::<Vec<_>>();
+ let arm_types = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("__TvmFfiMatchAnyType{arm_id}"),
span))
+ .collect::<Vec<_>>();
+ let arm_variants = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("Arm{arm_id}"), span))
+ .collect::<Vec<_>>();
+ let arm_constants = (0..arm_count)
+ .map(|arm_id| Ident::new(&format!("__TVM_FFI_MATCH_ANY_ARM_{arm_id}"),
span))
+ .collect::<Vec<_>>();
+ let arm_constant_definitions =
+ arm_constants
+ .iter()
+ .enumerate()
+ .map(|(arm_id, arm_constant)| {
+ quote! {
+ const #arm_constant: #tvm_ffi::match_any::ArmId =
+ #arm_id as #tvm_ffi::match_any::ArmId;
+ }
+ });
+ let lookup_entries = arm_constants
+ .iter()
+ .enumerate()
+ .map(|(arm_id, arm_constant)| quote!((#type_indices[#arm_id],
#arm_constant)));
+ let pattern_list = matchers
+ .iter()
+ .rev()
+ .fold(quote!(()), |tail, matcher| quote!((#matcher, #tail)));
+
+ let lookup_arm_id = quote! {
+ {
+ use #tvm_ffi::match_any::LeafPatternMetadata as _;
+
+ let #probe =
+ #tvm_ffi::match_any::LeafPatternProbe::<#pattern_list>::new();
+ match (&#probe).leaf_pattern_list_id() {
+ ::core::option::Option::Some(#pattern_list_id) => {
+ static #static_table: ::std::sync::OnceLock<
+ #tvm_ffi::match_any::LeafLookupTable,
+ > = ::std::sync::OnceLock::new();
+ let #table = #static_table.get_or_init(|| {
+ let mut #type_indices = [0_i32; #arm_count];
+ (&#probe).fill_leaf_type_indices(&mut #type_indices);
+ #tvm_ffi::match_any::LeafLookupTable::build(
+ #pattern_list_id,
+ &[#(#lookup_entries),*],
+ )
+ });
+ #table.lookup(#pattern_list_id, #view.type_index())
+ }
+ ::core::option::Option::None => {
+ ::core::result::Result::Err(())
+ }
+ }
+ }
+ };
+
+ let ordered_selection = arms.iter().enumerate().rev().fold(
Review Comment:
can we reuse the previous logic
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]