From 50abbc2b5a43aaac64a689f69352af1c77fc3076 Mon Sep 17 00:00:00 2001 From: Oleksandr Zinenko Date: Wed, 4 Jul 2018 14:23:16 +0200 Subject: [PATCH 1/4] varargToVector: use std::common_type instead of template recursion Idea by Lorenzo Chelini . --- islutils/matchers-inl.h | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/islutils/matchers-inl.h b/islutils/matchers-inl.h index d4d4312..742967a 100644 --- a/islutils/matchers-inl.h +++ b/islutils/matchers-inl.h @@ -1,23 +1,15 @@ #include namespace { -template -inline typename std::enable_if::type -appendVarargToVector(std::vector &vec, Args... args) { - vec.push_back(std::get<0>(std::tuple(args...))); - appendVarargToVector(vec, args...); -} - -template -inline typename std::enable_if::type -appendVarargToVector(std::vector &vec, Args...) { - (void)vec; -} - -template -std::vector varargToVector(Args... args) { - std::vector result; - appendVarargToVector(result, args...); +template +std::vector::type> +varargToVector(Args... args) { + std::vector::type> result; + result.reserve(sizeof...(Args)); + for (const auto &a : + {static_cast::type>(args)...}) { + result.emplace_back(a); + } return result; } } // namespace From a8d336f73d1825635b0a9fd4b6a32aa9e56f5a58 Mon Sep 17 00:00:00 2001 From: Oleksandr Zinenko Date: Wed, 4 Jul 2018 14:41:23 +0200 Subject: [PATCH 2/4] ScheduleNodeMatcher: make isMatching a static member It was always intended to be a static member becase it takes the matcher as argument. --- islutils/matchers.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/islutils/matchers.h b/islutils/matchers.h index 6f9676b..204a933 100644 --- a/islutils/matchers.h +++ b/islutils/matchers.h @@ -151,7 +151,8 @@ class ScheduleNodeMatcher { #undef DECL_FRIEND_TYPE_MATCH public: - bool isMatching(const ScheduleNodeMatcher &matcher, isl::schedule_node node); + static bool isMatching(const ScheduleNodeMatcher &matcher, + isl::schedule_node node); private: isl_schedule_node_type current_; From fe8ca355d422bb8793910a7973d92142b58ed811 Mon Sep 17 00:00:00 2001 From: Oleksandr Zinenko Date: Wed, 4 Jul 2018 16:55:15 +0200 Subject: [PATCH 3/4] add sibling matchers --- islutils/matchers.cc | 41 +++++++++++++++++++++++++++++++++++++++++ islutils/matchers.h | 9 +++++++++ 2 files changed, 50 insertions(+) diff --git a/islutils/matchers.cc b/islutils/matchers.cc index 49c1af0..e5118d0 100644 --- a/islutils/matchers.cc +++ b/islutils/matchers.cc @@ -34,4 +34,45 @@ bool ScheduleNodeMatcher::isMatching(const ScheduleNodeMatcher &matcher, return true; } +static bool hasPreviousSiblingImpl(isl::schedule_node node, + const ScheduleNodeMatcher &siblingMatcher) { + while (isl_schedule_node_has_previous_sibling(node.get()) == isl_bool_true) { + node = isl::manage(isl_schedule_node_previous_sibling(node.release())); + if (ScheduleNodeMatcher::isMatching(siblingMatcher, node)) { + return true; + } + } + return false; +} + +static bool hasNextSiblingImpl(isl::schedule_node node, + const ScheduleNodeMatcher &siblingMatcher) { + while (isl_schedule_node_has_next_sibling(node.get()) == isl_bool_true) { + node = isl::manage(isl_schedule_node_next_sibling(node.release())); + if (ScheduleNodeMatcher::isMatching(siblingMatcher, node)) { + return true; + } + } + return false; +} + +std::function +hasPreviousSibling(const ScheduleNodeMatcher &siblingMatcher) { + return std::bind(hasPreviousSiblingImpl, std::placeholders::_1, + siblingMatcher); +} + +std::function +hasNextSibling(const ScheduleNodeMatcher &siblingMatcher) { + return std::bind(hasNextSiblingImpl, std::placeholders::_1, siblingMatcher); +} + +std::function +hasSibling(const ScheduleNodeMatcher &siblingMatcher) { + return [siblingMatcher](isl::schedule_node node) { + return hasPreviousSiblingImpl(node, siblingMatcher) || + hasNextSiblingImpl(node, siblingMatcher); + }; +} + } // namespace matchers diff --git a/islutils/matchers.h b/islutils/matchers.h index 204a933..4a52706 100644 --- a/islutils/matchers.h +++ b/islutils/matchers.h @@ -160,6 +160,15 @@ class ScheduleNodeMatcher { std::function nodeCallback_; }; +std::function +hasPreviousSibling(const ScheduleNodeMatcher &siblingMatcher); + +std::function +hasNextSibling(const ScheduleNodeMatcher &siblingMatcher); + +std::function +hasSibling(const ScheduleNodeMatcher &siblingMatcher); + #include "matchers-inl.h" } // namespace matchers From 039b6fedc286cc4e236c663b260b5b02336e9f0b Mon Sep 17 00:00:00 2001 From: Oleksandr Zinenko Date: Wed, 4 Jul 2018 16:55:31 +0200 Subject: [PATCH 4/4] add descendant matcher --- islutils/matchers.cc | 29 +++++++++++++++++++++++++++++ islutils/matchers.h | 3 +++ 2 files changed, 32 insertions(+) diff --git a/islutils/matchers.cc b/islutils/matchers.cc index e5118d0..4788b75 100644 --- a/islutils/matchers.cc +++ b/islutils/matchers.cc @@ -75,4 +75,33 @@ hasSibling(const ScheduleNodeMatcher &siblingMatcher) { }; } +std::function +hasDescendant(const ScheduleNodeMatcher &descendantMatcher) { + isl::schedule_node n; + return [descendantMatcher](isl::schedule_node node) { + // Cannot use capturing lambdas as C function pointers. + struct Data { + bool found; + const ScheduleNodeMatcher &descendantMatcher; + }; + Data data{false, descendantMatcher}; + + auto r = isl_schedule_node_foreach_descendant_top_down( + node.get(), + [](__isl_keep isl_schedule_node *cn, void *user) -> isl_bool { + auto data = static_cast(user); + if (data->found) { + return isl_bool_false; + } + + auto n = isl::manage_copy(cn); + data->found = + ScheduleNodeMatcher::isMatching(data->descendantMatcher, n); + return data->found ? isl_bool_false : isl_bool_true; + }, + &data); + return r == isl_stat_ok && data.found; + }; +} + } // namespace matchers diff --git a/islutils/matchers.h b/islutils/matchers.h index 4a52706..96d76f5 100644 --- a/islutils/matchers.h +++ b/islutils/matchers.h @@ -169,6 +169,9 @@ hasNextSibling(const ScheduleNodeMatcher &siblingMatcher); std::function hasSibling(const ScheduleNodeMatcher &siblingMatcher); +std::function +hasDescendant(const ScheduleNodeMatcher &descendantMatcher); + #include "matchers-inl.h" } // namespace matchers