wrongtest-intellif commented on code in PR #13933:
URL: https://github.com/apache/tvm/pull/13933#discussion_r1102204087


##########
src/arith/pattern_match.h:
##########
@@ -813,6 +835,77 @@ inline PCallExpr<PIfThenElseOp, TCond, TA, TB> 
if_then_else(const Pattern<TCond>
                                                  false_value.derived());
 }
 
+template <typename... TPattern>
+class PMatchesOneOf {
+ public:
+  explicit PMatchesOneOf(const TPattern&... patterns) : patterns_{patterns...} 
{}
+
+  /*! \brief Check if value matches one of the patterns.
+   *
+   * This call also populates the PVars with matched value based on
+   * the first successful match.  The values in PVars are valid until
+   * the next call to Match.
+   *
+   * \param value The value to be matched against.
+   *
+   * \return Whether value matches the pattern.
+   */
+  template <typename NodeType>
+  inline bool Match(const NodeType& value) const {
+    return Match(value, []() { return true; });
+  }
+
+  /*! \brief Check if value matches one of the patterns.
+   *
+   * This call also populates the PVars with matched value based on
+   * the first successful match.  The values in PVars are valid until
+   * the next call to Match.
+   *
+   * \param value The value to be matched against.
+   *
+   * \param cond A callable that performs additional validation,
+   * returning true if the match passes.  This will typically be a
+   * lambda function written in terms of the filled PVars.  This will
+   * be called once for each successful pattern match.  If `cond()`
+   * returns false, the next match will be attempted.
+   *
+   * \return Whether value matches the pattern.
+   */
+  template <typename NodeType, typename Condition>
+  inline bool Match(const NodeType& value, Condition cond) const {
+    return MatchImpl(value, cond, 
std::make_index_sequence<sizeof...(TPattern)>());
+  }
+
+ private:
+  template <typename NodeType, typename Condition>
+  inline bool MatchImpl(const NodeType& value, Condition cond, 
std::index_sequence<>) const {
+    return false;
+  }
+
+  template <typename NodeType, typename Condition, size_t FirstIndex, 
size_t... RemainingIndices>
+  inline bool MatchImpl(const NodeType& value, Condition cond,
+                        std::index_sequence<FirstIndex, RemainingIndices...>) 
const {
+    return std::get<FirstIndex>(patterns_).Match(value, cond) ||
+           MatchImpl(value, cond, std::index_sequence<RemainingIndices...>());
+  }
+
+  std::tuple<const TPattern&...> patterns_;

Review Comment:
   Could you kindly explain a bit about why we use reference here :) 
@Lunderberg 



##########
src/arith/rewrite_simplify.cc:
##########
@@ -228,10 +228,13 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const 
AddNode* op) {
     TVM_TRY_REWRITE_IF(max(y + z * c1, x) + z * c2, max(x + z * c2, y),
                        c1.Eval()->value == -c2.Eval()->value);
 
-    TVM_TRY_REWRITE(max(x, y) + min(x, y), x + y);
-    TVM_TRY_REWRITE(min(x, y) + max(x, y), x + y);
-    TVM_TRY_REWRITE(max(x, y) + min(y, x), x + y);
-    TVM_TRY_REWRITE(min(x, y) + max(y, x), x + y);
+    TVM_TRY_REWRITE((PMatchesOneOf{

Review Comment:
   I think there may exist a pitfall that one may write something like 
`PMatchesOneOf(x + c,  x + y)`,and directly `y.eval()`? It would be great if we 
could left some comment on such mis-usages :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to