How to instantiate and evaluate tvm.tir.expr.Var?

Hi there, if I got a te experssion and get the values of all inputs nodes, how can I evaluate the value of leaf node?

For example,

a = te.var('a')
b = te.var('b')
c = a + b

and now it is known that a=1 and b=2. Is there any API that I can evaluate the c?

A very dirty work through is replace the str like

eval(str(c).replace(str(a), "1").replace(str(b), "2")) 
# the answer 3 

But this is not always correct (e.g., is the b var’s name is ba). If there is no direct evaluation API, can you advise how I can access to the AST of the expression?

There is a tvm.arith.Analyzer to bind variables and simplify https://github.com/apache/tvm/blob/main/python/tvm/arith/analyzer.py#L192

1 Like