Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TVMScript] Preserve LetStmt of constants #14531

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/tvm/script/parser/tir/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ def bind_assign_value(self: Parser, node: doc.expr, var_name: str, value: Any) -
):
IRBuilder.name(var_name, value)
return value
elif isinstance(value, PrimExpr):
else:
value = tvm.runtime.convert(value)
frame = T.LetStmt(value)
var = frame.var
IRBuilder.name(var_name, var)
frame.add_callback(partial(frame.__exit__, None, None, None))
frame.__enter__()
return var
return value


@dispatch.register(token="tir", type_name="For")
Expand Down
2 changes: 1 addition & 1 deletion tests/python/unittest/test_arith_domain_touched.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@T.prim_func
def scalar_func(a: T.handle, b: T.handle):
m = T.int32()
n = 100
n = T.meta_var(100)
A = T.match_buffer(a, (n, m))
B = T.match_buffer(b, (n, m))

Expand Down
19 changes: 17 additions & 2 deletions tests/python/unittest/test_tvmscript_syntax_sugar.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ def func_without_type_annotation(A: T.Buffer((1,), "int32")):
def test_letstmt_bind_with_constant():
@T.prim_func
def constant_binds():
x = 1
y = 42.0
x = T.meta_var(1)
y = T.meta_var(42.0)
T.evaluate(T.cast(x, "float32") + y)

@T.prim_func
Expand Down Expand Up @@ -414,6 +414,21 @@ def implicit(i: T.int32):
assert_structural_equal(implicit, explicit)


def test_preserve_trivial_let_binding_of_value():
@T.prim_func
def explicit(i: T.int32):
j = T.int32()
T.LetStmt(42, var=j)
T.evaluate(j)

@T.prim_func
def implicit(i: T.int32):
j = 42
T.evaluate(j)

assert_structural_equal(implicit, explicit)


def test_preserve_parameter_name():
@T.prim_func
def func(i: T.int32):
Expand Down