@@ -693,34 +693,38 @@ template <typename TargetType>
693693TVM_FFI_INLINE bool IsObjectInstance (int32_t object_type_index) {
694694 static_assert (std::is_base_of_v<Object, TargetType>);
695695 // Everything is a subclass of object.
696- if constexpr (std::is_same<TargetType, Object>::value) return true ;
697-
698- if constexpr (TargetType::_type_final) {
696+ if constexpr (std::is_same<TargetType, Object>::value) {
697+ return true ;
698+ } else if constexpr (TargetType::_type_final) {
699699 // if the target type is a final type
700700 // then we only need to check the equivalence.
701701 return object_type_index == TargetType::RuntimeTypeIndex ();
702- }
703-
704- // if target type is a non-leaf type
705- // Check if type index falls into the range of reserved slots.
706- int32_t target_type_index = TargetType::RuntimeTypeIndex ();
707- int32_t begin = target_type_index;
708- // The condition will be optimized by constant-folding.
709- if constexpr (TargetType::_type_child_slots != 0 ) {
710- // total_slots = child_slots + 1 (including self)
711- int32_t end = begin + TargetType::_type_child_slots + 1 ;
712- if (object_type_index >= begin && object_type_index < end) return true ;
713702 } else {
714- if (object_type_index == begin) return true ;
715- }
716- if (!TargetType::_type_child_slots_can_overflow) return false ;
717- // Invariance: parent index is always smaller than the child.
718- if (object_type_index < target_type_index) return false ;
719- // Do a runtime lookup of type information
720- // the function checks that the info exists
721- const TypeInfo* type_info = TVMFFIGetTypeInfo (object_type_index);
722- return (type_info->type_depth > TargetType::_type_depth &&
723- type_info->type_acenstors [TargetType::_type_depth] == target_type_index);
703+ // Explicitly enclose in else to eliminate this branch early in compilation.
704+ // if target type is a non-leaf type
705+ // Check if type index falls into the range of reserved slots.
706+ int32_t target_type_index = TargetType::RuntimeTypeIndex ();
707+ int32_t begin = target_type_index;
708+ // The condition will be optimized by constant-folding.
709+ if constexpr (TargetType::_type_child_slots != 0 ) {
710+ // total_slots = child_slots + 1 (including self)
711+ int32_t end = begin + TargetType::_type_child_slots + 1 ;
712+ if (object_type_index >= begin && object_type_index < end) return true ;
713+ } else {
714+ if (object_type_index == begin) return true ;
715+ }
716+ if constexpr (TargetType::_type_child_slots_can_overflow) {
717+ // Invariance: parent index is always smaller than the child.
718+ if (object_type_index < target_type_index) return false ;
719+ // Do a runtime lookup of type information
720+ // the function checks that the info exists
721+ const TypeInfo* type_info = TVMFFIGetTypeInfo (object_type_index);
722+ return (type_info->type_depth > TargetType::_type_depth &&
723+ type_info->type_acenstors [TargetType::_type_depth]->type_index == target_type_index);
724+ } else {
725+ return false ;
726+ }
727+ }
724728}
725729
726730/* !
0 commit comments