[TVMScript] Use tir::Evaluate if expression is in statement context#13396
[TVMScript] Use tir::Evaluate if expression is in statement context#13396Hzfengsy merged 2 commits intoapache:mainfrom
Conversation
For the previous version of the parser, this was special-cased for some intrinsic operators. After the new TVMScript was enabled in apache#12496, any `PrimExpr` that appears in the body of a statement is silently ignored. This commit updates the parser to instead wrap the bare `PrimExpr` in a `tir::Evaluate` node. This change effectively allows [expression statements](https://docs.python.org/3/reference/simple_stmts.html#expression-statements) in TVMScript, which are converted to `tir::Evaluate` nodes during parsing.
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
|
Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.
Generated by tvm-bot |
|
This PR is in response to CI failures found in #13130, when writing TVMScript that used the |
junrushao
left a comment
There was a problem hiding this comment.
LGTM! I was thinking about the same thing but holding back a bit because I dont want to change TIR too often, but it is definitely a positive change and I really love it
| elif isinstance(res, (int, bool)): | ||
| T.evaluate(tvm.tir.const(res)) |
There was a problem hiding this comment.
hey i was wondering if we want to explicitly print T.evaluate(0) vs 0? The former one might look a bit clearer from my perspective
There was a problem hiding this comment.
I was going back and forth on it, and I like the idea of printing T.evaluate() except in the case of CallNode. The more aggressive sugaring would be to render tir::Evaluate(0) as pass, which would be even clearer to read from a casual Python reader.
| @T.prim_func | ||
| def func(A: T.Buffer[1, "int32"]): | ||
| T.evaluate(T.assume(A[0] == 5)) | ||
| A[0] = 10 |
There was a problem hiding this comment.
just curious what's this line for?
There was a problem hiding this comment.
Mostly because T.assume doesn't have any effect, and I was pulling this in from a failing case of T.assume in a separate PR. This was from a unit test that validated a pass that removed T.assume calls from a primfunc.
There was a problem hiding this comment.
Ah I see. Thanks for the clarification!
| // printing we only need to print the value. | ||
| Doc doc; | ||
| doc << tir_prefix_ << ".evaluate(" << Print(op->value) << ")"; | ||
| doc << Print(op->value); |
There was a problem hiding this comment.
How about we only sugar CallNode?
There was a problem hiding this comment.
I like that idea for the printing. Maybe best of both worlds, to have the sugar apply all the time when parsing, but only CallNode drops the printing of T.evaluate(...) when generating TVMScript.
Thank you! It's definitely a lot easier to add parsing/printing sugar to TVMScript than to change the TIR in C++. |
|
Thanks for the great update! This helps a lot with the usability of TVMScript :-) Let's get it in ASAP! |
…pache#13396) * [TVMScript] Use tir::Evaluate if expression is in statement context For the previous version of the parser, this was special-cased for some intrinsic operators. After the new TVMScript was enabled in apache#12496, any `PrimExpr` that appears in the body of a statement is silently ignored. This commit updates the parser to instead wrap the bare `PrimExpr` in a `tir::Evaluate` node. This change effectively allows [expression statements](https://docs.python.org/3/reference/simple_stmts.html#expression-statements) in TVMScript, which are converted to `tir::Evaluate` nodes during parsing. * Update to print T.evaluate() for readability, except for CallNode
For the previous version of the parser, this was special-cased for some intrinsic operators. After the new TVMScript was enabled in #12496, any
PrimExprthat appears in the body of a statement is silently ignored. This commit updates the parser to instead wrap the barePrimExprin atir::Evaluatenode.This change effectively allows expression statements in TVMScript, which are converted to
tir::Evaluatenodes during parsing.