Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 1197fa1

Browse files
committed
flattenSequenceOrSet: harden types
Make flattenSequenceOrSet a function template with a static assertion on allowed types (ScheduleTreeElemSet or ScheduleTreeElemSequence). The same behavior can also be achieve with std::enable_if, but the error message is not as clear. This commit illustrates the effects of having ScheduleTreeElem* inherit from ScheduleTree. Arguably, this function belongs to schedule_transforms.cc, but this would invert the inclusion order (schedule_transforms.h includes schedule_tree.h and not the inverse). Even more arguably, the flattening should not be automatic in TC schedule trees since they don't maintain any other isl tree properties. Leaving this discussion to future work.
1 parent fd7b88d commit 1197fa1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

tc/core/polyhedral/schedule_tree.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ namespace detail {
126126

127127
std::ostream& operator<<(std::ostream& os, const ScheduleTree& tree);
128128

129+
template <typename T>
129130
void flattenSequenceOrSet(ScheduleTree* tree);
130131

131132
struct ScheduleTree {
@@ -401,11 +402,11 @@ struct ScheduleTree {
401402
"Arguments must be rvalue references to ScheduleTreeUPtr");
402403

403404
auto ctx = arg->ctx_;
404-
auto res = ScheduleTreeUPtr(new T(ctx));
405+
auto res = new T(ctx);
406+
flattenSequenceOrSet(res);
405407
res->appendChildren(
406408
vectorFromArgs(std::forward<Arg>(arg), std::forward<Args>(args)...));
407-
flattenSequenceOrSet(res.get());
408-
return res;
409+
return ScheduleTreeUPtr(res);
409410
}
410411

411412
// Make a (deep) copy of "tree".
@@ -485,11 +486,12 @@ struct ScheduleTree {
485486
};
486487

487488
// Flatten nested nodes of the same type.
488-
inline void flattenSequenceOrSet(ScheduleTree* tree) {
489-
// This should be enforced by the type system...
490-
TC_CHECK(
491-
tree->type_ == ScheduleTreeType::Sequence ||
492-
tree->type_ == ScheduleTreeType::Set);
489+
template <typename T>
490+
inline void flattenSequenceOrSet(T* tree) {
491+
static_assert(
492+
std::is_same<ScheduleTreeElemSet, T>::value ||
493+
std::is_same<ScheduleTreeElemSequence, T>::value,
494+
"Only Sequence or Set node can be flattened");
493495

494496
// Iterate over the changing list of children. If a child has the same list
495497
// type as a parent, replace it with grandchildren and traverse them too.

0 commit comments

Comments
 (0)