Skip to content

Commit

Permalink
[TVMScript,Fix] Fix findsource when classes are indented
Browse files Browse the repository at this point in the history
Leaving class definitions was not correctly handled when recreating
scoping information. The fix correctly pops scope whenever the
indentation level becomes less than the current scope.
  • Loading branch information
Tristan Konolige committed Feb 6, 2023
1 parent cec5f0b commit 4225528
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/tvm/script/parser/core/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ def findsource(obj):
name = tokens[1].split(":")[0].split("(")[0] + "<locals>"
elif tokens[0] == "class":
name = tokens[1].split(":")[0].split("(")[0]
# pop scope if we are less indented
while scope_stack and indent_info[scope_stack[-1]] >= indent:
scope_stack.pop()
if name:
while scope_stack and indent_info[scope_stack[-1]] >= indent:
scope_stack.pop()
scope_stack.append(name)
indent_info[name] = indent
if scope_stack == qual_names:
Expand Down
15 changes: 15 additions & 0 deletions tests/python/unittest/test_tvmscript_parser_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,20 @@ def test_source_ast():
assert isinstance(for_block, doc.With) and len(for_block.body) == 2


def test_nesting_parsing():
class dummy:
pass

for i in range(1):

@tvm.script.ir_module
class Module:
@T.prim_func
def impl(
A: T.Buffer[(12, 196, 64), "float32"],
) -> None:
T.evaluate(0)


if __name__ == "__main__":
tvm.testing.main()

0 comments on commit 4225528

Please sign in to comment.