Skip to content
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
38 changes: 17 additions & 21 deletions include/tvm/ir/base_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,20 @@ struct TypeTraits<TypedExpr<ExpectedType>>
const auto* expr = details::ObjectUnsafe::RawObjectPtrFromUnowned<ExprNode>(src->v_obj);
return details::AnyUnsafe::CheckAnyViewStrict<ExpectedType>(expr->ty);
}

TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
if (src->type_index >= TypeIndex::kTVMFFIStaticObjectBegin && src->v_obj != nullptr &&
details::IsObjectInstance<ExprNode>(src->type_index)) {
const auto* expr = details::ObjectUnsafe::RawObjectPtrFromUnowned<ExprNode>(src->v_obj);
if (expr->ty.defined()) {
const auto* prim_ty = expr->ty.as<PrimTypeNode>();
std::string ty =
prim_ty ? std::string(ffi::DLDataTypeToString(prim_ty->dtype)) : expr->ty.GetTypeKey();
return TypeIndexToTypeKey(src->type_index) + "[ty=" + ty + "]";
}
}
return TypeTraitsBase::GetMismatchTypeInfo(src);
}
};

template <>
Expand All @@ -499,25 +513,15 @@ inline constexpr bool use_default_type_traits_v<PrimExpr> = false;
template <>
struct TypeTraits<PrimExpr> : public ObjectRefWithFallbackTraitsBase<PrimExpr, StrictBool, int64_t,
double, PrimExprConvertible> {
using Base =
ObjectRefWithFallbackTraitsBase<PrimExpr, StrictBool, int64_t, double, PrimExprConvertible>;

TVM_FFI_INLINE static bool CheckAnyStrict(const TVMFFIAny* src) {
if (src->type_index == TypeIndex::kTVMFFINone) return PrimExpr::_type_is_nullable;
return TypeTraits<TypedExpr<PrimType>>::CheckAnyStrict(src);
}

TVM_FFI_INLINE static std::string TypeStr() { return "ir.PrimExpr"; }

TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* source) {
if (source->type_index >= TypeIndex::kTVMFFIStaticObjectBegin && source->v_obj != nullptr &&
details::IsObjectInstance<ExprNode>(source->type_index)) {
const auto* expr = details::ObjectUnsafe::RawObjectPtrFromUnowned<ExprNode>(source->v_obj);
if (expr->ty.defined() && !details::AnyUnsafe::CheckAnyViewStrict<PrimType>(expr->ty)) {
return TypeIndexToTypeKey(source->type_index) + "[ty=" + expr->ty.GetTypeKey() + "]";
}
}
return Base::GetMismatchTypeInfo(source);
TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
return TypeTraits<TypedExpr<PrimType>>::GetMismatchTypeInfo(src);
}

TVM_DLL static PrimExpr ConvertFallbackValue(StrictBool value);
Expand Down Expand Up @@ -551,15 +555,7 @@ struct TypeTraits<IntExpr> : public ObjectRefWithFallbackTraitsBase<IntExpr, int
TVM_FFI_INLINE static std::string TypeStr() { return "ir.IntExpr"; }

TVM_FFI_INLINE static std::string GetMismatchTypeInfo(const TVMFFIAny* src) {
if (src->type_index >= TypeIndex::kTVMFFIStaticObjectBegin &&
details::IsObjectInstance<ExprNode>(src->type_index)) {
const auto* expr = details::ObjectUnsafe::RawObjectPtrFromUnowned<ExprNode>(src->v_obj);
if (const auto* ty = expr->ty.as<PrimTypeNode>()) {
return TypeIndexToTypeKey(src->type_index) +
"[dtype=" + ffi::DLDataTypeToString(ty->dtype) + "]";
}
}
return TypeTraits<PrimExpr>::GetMismatchTypeInfo(src);
return TypeTraits<TypedExpr<PrimType>>::GetMismatchTypeInfo(src);
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/python/tirx-base/test_tir_op_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_scalar_integer_signature():
assert tirx.tvm_struct_get(ptr, index, 2, dtype="int32").args[1].same_as(index)
assert tirx.tvm_struct_get(ptr, 1 << 40, 2, dtype="int32").args[1].ty.dtype == "int64"
for dtype in ("float32", "bool", "int32x4", "int32xvscalex4"):
with pytest.raises(TypeError, match=r"index.*expected `ir.IntExpr`.*dtype="):
with pytest.raises(TypeError, match=rf"index.*expected `ir.IntExpr`.*\[ty={dtype}\]"):
tirx.tvm_struct_get(ptr, tirx.Var("index", dtype), 2, dtype="int32")


Expand Down
Loading