Skip to content

Commit

Permalink
[Wasm] Provide pre and post-order visitors for wasm::MatchBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagretscher committed Mar 18, 2024
1 parent af9e7a9 commit 118184c
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 35 deletions.
53 changes: 52 additions & 1 deletion src/backend/WasmOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5335,9 +5335,60 @@ void Match<m::wasm::HashBasedGroupJoin>::print(std::ostream &out, unsigned level
}


// explicit template instantiations
/*======================================================================================================================
* ThePreOrderMatchBaseVisitor, ThePostOrderMatchBaseVisitor
*====================================================================================================================*/

namespace {

template<bool C, bool PreOrder>
struct recursive_matchbase_visitor : TheRecursiveMatchBaseVisitorBase<C>
{
using super = TheRecursiveMatchBaseVisitorBase<C>;
template<typename T> using Const = typename super::template Const<T>;
using callback_t = std::conditional_t<C, ConstMatchBaseVisitor, MatchBaseVisitor>;

private:
callback_t &callback_;

public:
recursive_matchbase_visitor(callback_t &callback) : callback_(callback) { }

using super::operator();
#define DECLARE(CLASS) \
void operator()(Const<CLASS> &M) override { \
if constexpr (PreOrder) try { callback_(M); } catch (visit_skip_subtree) { return; } \
super::operator()(M); \
if constexpr (not PreOrder) callback_(M); \
}
M_WASM_MATCH_LIST(DECLARE)
#undef DECLARE
};

}

template<bool C>
void ThePreOrderMatchBaseVisitor<C>::operator()(Const<MatchBase> &M)
{
recursive_matchbase_visitor<C, /* PreOrder= */ true>{*this}(M);
}

template<bool C>
void ThePostOrderMatchBaseVisitor<C>::operator()(Const<MatchBase> &M)
{
recursive_matchbase_visitor<C, /* PreOrder= */ false>{*this}(M);
}


/*======================================================================================================================
* Explicit template instantiations
*====================================================================================================================*/

#define INSTANTIATE(CLASS) \
template struct m::wasm::CLASS; \
template struct m::Match<m::wasm::CLASS>;
M_WASM_OPERATOR_LIST_TEMPLATED(INSTANTIATE)
#undef INSTANTIATE

template struct m::wasm::ThePreOrderMatchBaseVisitor<true>;
template struct m::wasm::ThePostOrderMatchBaseVisitor<true>;
97 changes: 63 additions & 34 deletions src/backend/WasmOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,41 +229,41 @@ namespace m {
M_WASM_OPERATOR_LIST_TEMPLATED(X)


#define MAKE_MATCH_(OP) Match<wasm::OP>
#define MAKE_MATCH_(OP) m::Match<m::wasm::OP>
#define M_WASM_MATCH_LIST_NON_TEMPLATED(X) M_TRANSFORM_X_MACRO(X, M_WASM_OPERATOR_LIST_NON_TEMPLATED, MAKE_MATCH_)
#define M_WASM_MATCH_LIST_TEMPLATED(X) \
X(Match<wasm::Callback<false>>) \
X(Match<wasm::Callback<true>>) \
X(Match<wasm::Print<false>>) \
X(Match<wasm::Print<true>>) \
X(Match<wasm::Scan<false>>) \
X(Match<wasm::Scan<true>>) \
X(Match<wasm::Filter<false>>) \
X(Match<wasm::Filter<true>>) \
X(Match<wasm::Quicksort<false>>) \
X(Match<wasm::Quicksort<true>>) \
X(Match<wasm::NestedLoopsJoin<false>>) \
X(Match<wasm::NestedLoopsJoin<true>>) \
X(Match<wasm::SimpleHashJoin<M_COMMA(false) false>>) \
X(Match<wasm::SimpleHashJoin<M_COMMA(false) true>>) \
X(Match<wasm::SimpleHashJoin<M_COMMA(true) false>>) \
X(Match<wasm::SimpleHashJoin<M_COMMA(true) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(false) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(false) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(true) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(true) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(false) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(false) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(true) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(true) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(false) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(false) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(true) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(true) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(false) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(false) true>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(true) false>>) \
X(Match<wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(true) true>>)
X(m::Match<m::wasm::Callback<false>>) \
X(m::Match<m::wasm::Callback<true>>) \
X(m::Match<m::wasm::Print<false>>) \
X(m::Match<m::wasm::Print<true>>) \
X(m::Match<m::wasm::Scan<false>>) \
X(m::Match<m::wasm::Scan<true>>) \
X(m::Match<m::wasm::Filter<false>>) \
X(m::Match<m::wasm::Filter<true>>) \
X(m::Match<m::wasm::Quicksort<false>>) \
X(m::Match<m::wasm::Quicksort<true>>) \
X(m::Match<m::wasm::NestedLoopsJoin<false>>) \
X(m::Match<m::wasm::NestedLoopsJoin<true>>) \
X(m::Match<m::wasm::SimpleHashJoin<M_COMMA(false) false>>) \
X(m::Match<m::wasm::SimpleHashJoin<M_COMMA(false) true>>) \
X(m::Match<m::wasm::SimpleHashJoin<M_COMMA(true) false>>) \
X(m::Match<m::wasm::SimpleHashJoin<M_COMMA(true) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(false) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(false) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(true) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(false) M_COMMA(true) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(false) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(false) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(true) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(false) M_COMMA(true) M_COMMA(true) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(false) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(false) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(true) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(false) M_COMMA(true) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(false) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(false) true>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(true) false>>) \
X(m::Match<m::wasm::SortMergeJoin<M_COMMA(true) M_COMMA(true) M_COMMA(true) true>>)
#define M_WASM_MATCH_LIST(X) \
M_WASM_MATCH_LIST_NON_TEMPLATED(X) \
M_WASM_MATCH_LIST_TEMPLATED(X)
Expand Down Expand Up @@ -1133,7 +1133,8 @@ struct TheRecursiveMatchBaseVisitorBase : std::conditional_t<C, ConstMatchBaseVi

virtual ~TheRecursiveMatchBaseVisitorBase() { }

using super::operator();
/* omit using super::operator() to convert to intermediate match classes and call recursive implementation */
void operator()(Const<MatchBase> &M) override { super::operator()(M); }
void operator()(Const<MatchLeaf>&) override { /* nothing to be done */ }
void operator()(Const<MatchSingleChild> &M) override { (*this)(*M.child); }
void operator()(Const<Match<Projection>> &M) override { if (M.child) (*this)(**M.child); }
Expand All @@ -1142,6 +1143,34 @@ struct TheRecursiveMatchBaseVisitorBase : std::conditional_t<C, ConstMatchBaseVi

using RecursiveConstMatchBaseVisitorBase = TheRecursiveMatchBaseVisitorBase<true>;

template<bool C>
struct M_EXPORT ThePreOrderMatchBaseVisitor : std::conditional_t<C, ConstMatchBaseVisitor, MatchBaseVisitor>
{
using super = std::conditional_t<C, ConstMatchBaseVisitor, MatchBaseVisitor>;
template<typename T> using Const = typename super::template Const<T>;

virtual ~ThePreOrderMatchBaseVisitor() { }

void operator()(Const<MatchBase>&);
};

template<bool C>
struct M_EXPORT ThePostOrderMatchBaseVisitor : std::conditional_t<C, ConstMatchBaseVisitor, MatchBaseVisitor>
{
using super = std::conditional_t<C, ConstMatchBaseVisitor, MatchBaseVisitor>;
template<typename T> using Const = typename super::template Const<T>;

virtual ~ThePostOrderMatchBaseVisitor() { }

void operator()(Const<MatchBase>&);
};

using ConstPreOrderMatchBaseVisitor = ThePreOrderMatchBaseVisitor<true>;
using ConstPostOrderMatchBaseVisitor = ThePostOrderMatchBaseVisitor<true>;

M_MAKE_STL_VISITABLE(ConstPreOrderMatchBaseVisitor, const MatchBase, M_WASM_VISITABLE_MATCH_LIST)
M_MAKE_STL_VISITABLE(ConstPostOrderMatchBaseVisitor, const MatchBase, M_WASM_VISITABLE_MATCH_LIST)

#undef M_WASM_VISITABLE_MATCH_LIST

}
Expand Down

0 comments on commit 118184c

Please sign in to comment.