Skip to content

Commit

Permalink
[TVMScript] Fix parse minimal i32 literal for tir script (apache#12772)
Browse files Browse the repository at this point in the history
This change tries to fix an issue due to apache#12515.

Previously the logic for `-2147483648` is  `parse(-literal)` = `-parse(literal)`, and all integer literals are converted to i32 (either the literal value actually overflow or not).

Since after apache#12515, parse `2147483648` results in an i64 typed integer rather than i32, `-2147483648` then becomes an i64 integer too, which is not reasonable.
  • Loading branch information
wrongtest-intellif authored Sep 15, 2022
1 parent 6a05184 commit 9a3b3dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/tvm/script/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,13 @@ def transform_Call(self, node):
)
if node.func_name.name in self._unaryop_maker:
rhs = self.transform(node.params[0])
if node.func_name.name == ast.BuiltinOp.USub and isinstance(
node.params[0], ast.Constant
):
# '-literal' should be parsed together for proper literal type inference
if not isinstance(rhs, (tvm.tir.IntImm, tvm.tir.FloatImm)):
self.report_error("The literal is illegal after -", node.params[0].span)
return tvm.tir.const(-rhs.value)
return self._unaryop_maker[node.func_name.name](
rhs, span=tvm_span_from_synr(node.span)
)
Expand Down
10 changes: 10 additions & 0 deletions tests/python/unittest/test_tvmscript_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3381,6 +3381,15 @@ def func(
return func


def minimal_i32_literal():
@T.prim_func
def func() -> None:
T.evaluate(T.int32(-2147483648))
T.evaluate(-T.int64(2147483648))

return func


ir_generator = tvm.testing.parameter(
opt_gemm_normalize,
opt_gemm_lower,
Expand Down Expand Up @@ -3423,6 +3432,7 @@ def func(
decl_buffer,
allocate_and_decl_buffer,
float_infinity,
minimal_i32_literal,
)


Expand Down

0 comments on commit 9a3b3dd

Please sign in to comment.