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
16 changes: 16 additions & 0 deletions src/relax/ir/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,25 @@ TVM_REGISTER_GLOBAL("relax.If")
});

Tuple::Tuple(tvm::Array<relay::Expr> fields, Span span) {
Optional<StructInfo> tuple_sinfo = [&]() -> Optional<StructInfo> {
Array<StructInfo> field_sinfo;
for (const auto& field : fields) {
if (field->struct_info_.defined()) {
field_sinfo.push_back(GetStructInfo(field));
} else {
return NullOpt;
}
}
return TupleStructInfo(field_sinfo);
}();

ObjectPtr<TupleNode> n = make_object<TupleNode>();
n->fields = std::move(fields);
n->span = std::move(span);
if (tuple_sinfo) {
n->checked_type_ = GetStaticType(tuple_sinfo.value());
}
n->struct_info_ = tuple_sinfo;
data_ = std::move(n);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/python/relax/test_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ def test_tuple() -> None:
t[-3]


def test_tuple_sinfo_inferred_on_construction():
v0 = rx.Var("v0", rx.ObjectStructInfo())
v1 = rx.Var("v1", rx.ObjectStructInfo())
tup = rx.Tuple((v0, v1))

assert tup.struct_info_ is not None
tvm.ir.assert_structural_equal(
tup.struct_info, rx.TupleStructInfo([rx.ObjectStructInfo(), rx.ObjectStructInfo()])
)


def test_tuple_sinfo_requires_fields_with_known_sinfo():
v0 = rx.Var("v0", rx.ObjectStructInfo())
v1 = rx.Var("v1")
tup = rx.Tuple((v0, v1))

assert tup.struct_info_ is None


def test_match_cast() -> None:
# match_cast([16, 8], [m, n])
m = tir.Var("m", dtype="int64")
Expand Down