comaniac commented on a change in pull request #9671:
URL: https://github.com/apache/tvm/pull/9671#discussion_r766051215



##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});
+  }
+
+  Expr Callback(const Expr& pre, const Expr& post,
+                const Map<DFPattern, Array<Expr>>& node_map) const override {
+    const CallNode* call = pre.as<CallNode>();
+    auto x = node_map[x_][0];
+    auto c1 = node_map[const1_][0];
+    auto c2 = node_map[const2_][0];
+
+    auto pre_call = call;
+    if (pre_call->args[1].as<ConstantNode>()) {
+      pre_call = pre_call->args[0].as<CallNode>();
+    } else {
+      pre_call = pre_call->args[1].as<CallNode>();
+    }
+    // check the first two input has one non-constant

Review comment:
       ```suggestion
       // Do nothing if both inputs are not constants as they will be constant 
folded already.
   ```

##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});
+  }
+
+  Expr Callback(const Expr& pre, const Expr& post,
+                const Map<DFPattern, Array<Expr>>& node_map) const override {
+    const CallNode* call = pre.as<CallNode>();
+    auto x = node_map[x_][0];
+    auto c1 = node_map[const1_][0];
+    auto c2 = node_map[const2_][0];
+
+    auto pre_call = call;
+    if (pre_call->args[1].as<ConstantNode>()) {

Review comment:
       ```suggestion
       // Find the next add call.
       if (pre_call->args[1].as<ConstantNode>()) {
   ```

##########
File path: src/relay/transforms/simplify_expr.cc
##########
@@ -585,6 +585,47 @@ class EliminateIdentityRewrite : public DFPatternRewrite {
   DFPattern const_;
 };
 
+/*! \brief Make two consecutive add able to be constant_folded. */
+class SimplifyConsecutiveAdd : public DFPatternRewrite {
+ public:
+  SimplifyConsecutiveAdd() {
+    x_ = IsWildcard();
+    const1_ = IsConstant();
+    const2_ = IsConstant();
+    DFPattern add_op1_ = IsOp("add");
+    DFPattern add_op2_ = IsOp("add");
+    pattern_ = add_op2_({add_op1_({x_, const1_}), const2_});

Review comment:
       1. Only private class members need the underline suffix.
   2. You actually don't need two `add_op`s because all ops are the same.
   3. Might be better to add a comment saying pattern matching supports 
commutative property for addition so this pattern already covers all possible 
cases.
   ```suggestion
       DFPattern add_op = IsOp("add");
       pattern_ = add_op({add_op({x_, const1_}), const2_});
   ```




-- 
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