Skip to content

Commit

Permalink
[TVMScript] Use new variable frame in If/Then/Else (#14250)
Browse files Browse the repository at this point in the history
Previously, while TVMScript introduces a new scope for other
contexts (e.g. `for`, `while`, `with`, etc), the `if` and `else`
blocks did not introduce a new scope.  This caused erroneous parsing
errors if the `if` and `else` blocks each contained a variable with
the same name.  Added a `self.var_table.with_frame()` context resolves
this issue.
  • Loading branch information
Lunderberg authored Mar 23, 2023
1 parent 3b274aa commit 3f56a95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions python/tvm/script/parser/tir/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,12 @@ def visit_if(self: Parser, node: doc.If) -> None:
with self.var_table.with_frame():
with T.If(self.eval_expr(node.test)):
with T.Then():
self.visit_body(node.body)
with self.var_table.with_frame():
self.visit_body(node.body)
if node.orelse:
with T.Else():
self.visit_body(node.orelse)
with self.var_table.with_frame():
self.visit_body(node.orelse)


@dispatch.register(token="tir", type_name="Assert")
Expand Down
14 changes: 14 additions & 0 deletions tests/python/unittest/test_tvmscript_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,19 @@ def main(A: T.handle, B: T.handle):
return main


def if_then_else_var():
@T.prim_func
def main(n: T.int32):
if n == 0:
x = 5
T.evaluate(x)
else:
x = 10
T.evaluate(x)

return main


def tvm_shfl_builtins():
@T.prim_func
def func(
Expand Down Expand Up @@ -3740,6 +3753,7 @@ def func(
let_stmt_value,
string_stride,
merge_shape_var_def,
if_then_else_var,
tvm_shfl_builtins,
)

Expand Down

0 comments on commit 3f56a95

Please sign in to comment.