vinx13 commented on code in PR #11373:
URL: https://github.com/apache/tvm/pull/11373#discussion_r879870628


##########
src/arith/rewrite_simplify.cc:
##########
@@ -256,13 +256,16 @@ PrimExpr RewriteSimplifier::Impl::VisitExpr_(const 
SubNode* op) {
     TVM_TRY_REWRITE(broadcast(x, lanes) - broadcast(y, lanes), broadcast(x - 
y, lanes));
   }
 
+  // cancelation rules
+  TVM_TRY_REWRITE_IF(x - x, ZeroWithTypeLike(x),
+                     SideEffect(x.Eval()) <= CallEffectKind::kReadState);
+  TVM_TRY_REWRITE_IF((x + y) - y, x, SideEffect(y.Eval()) <= 
CallEffectKind::kReadState);
+  TVM_TRY_REWRITE_IF((x + y) - x, y, SideEffect(x.Eval()) <= 
CallEffectKind::kReadState);
+  TVM_TRY_REWRITE_IF(x - (y + x), 0 - y, SideEffect(x.Eval()) <= 
CallEffectKind::kReadState);
+  TVM_TRY_REWRITE_IF(x - (x + y), 0 - y, SideEffect(x.Eval()) <= 
CallEffectKind::kReadState);
+
   if (IsIndexType(op->dtype)) {
     // Index rules
-    // cancelation rules

Review Comment:
   The integer simplifcation should be fast path (and clean, hopefully without 
Nans), it would be better to put new rules in else branch even if it means some 
duplications, since (recursively) checking side effects is expensive.
   Doing floating point simplification is probably fine, but given the 
possibility of introducing additional types, it might be safer to say something 
like
   ```
   if (IsIndexType(op->dtype)) {
     old rules
   } else if (op->dtype.is_float())
     new rules
   }
   ```
   



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