yuanfz98 commented on issue #10211:
URL: https://github.com/apache/tvm/issues/10211#issuecomment-1057078163


   Hello,
   
   For semantic extension, I listed some items:
   - commutativity (identifying for instance (x+y) with (y+x))
   - associativity (identifying for instance (x+y)+z with x+(y+z))
   - distributivity (x*(y+z) and x\*y+x\*z)
   
   We may call tvm/arith library to do the job.
   ```cpp
   bool EquivalentTerms(const PrimExpr& a, const PrimExpr& b) {
     // For now, we just check the syntactic equality, but that could later 
become a semantic test,
     // for instance identifying computations modulo commutativity (like x+y 
and y+x), or modulo
     // associativity (like (x+y)+z and x+(y+z)), etc.
     arith::Analyzer analyser;
     PrimExpr a_simplified = analyser.Simplify(a);
     PrimExpr b_simplified = analyser.Simplify(b);
     return EqualTerms(a_simplified, b_simplified);
   }
   
   ```
   
   An example test :
   ```
   def test_semantic_equiv_distributivity():
       i1 = te.var("i1")
       i2 = te.var("i2")
       x = te.var("x")
       y = te.var("y")
       z = te.var("z")
       dtype = "int32"
       buffer = tvm.tir.decl_buffer((50,), dtype)
       body = tvm.tir.SeqStmt(
           [
               tvm.tir.Store(buffer.data, x*(y+z), i1),
               tvm.tir.Store(buffer.data, x*y+x*z, i2),
           ]
       )
   
       mod = tvm.IRModule.from_expr(tvm.tir.PrimFunc([i1, i2, x, y, z], body))
       body = tvm.tir.transform.CommonSubexprElimTIR()(mod)
   
       tvm.transform.PrintIR()(body)
   ```
   
   And I got:
   ```
   [15:10:03] /home/yuan/Coding/compiler/repos/tvm/src/ir/transform.cc:566: 
PrintIR():
   #[version = "0.0.5"]
   @main = primfn(i1: int32, i2: int32, x: int32, y: int32, z: int32) -> () {
     let cse_var_1: int32 = (x*(y + z))
      {
       buffer: Pointer(int32)[i1] = cse_var_1
       buffer[i2] = cse_var_1
     }
   }
   ```
   
   However I found that the arith system may not be able to do the 
commutativity (x+y != y+x and x\*y != y\*x). 
   


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