Skip to content

Commit

Permalink
[BugFix][Arith] IterMapRewriter abort rewriting once failure
Browse files Browse the repository at this point in the history
This PR fixes an issue of the IterMapRewriter. Prior to this PR, the
mutation function of the rewriter class returns the mutation results
even when an invalid PrimExpr pattern was detected.

Returning the mutation results when failure is not expected, and in
such cases we should "abort" the mutation, and return the input
PrimExpr which is not mutated, since insisting on returning the
mutation results sometimes it incurs further error on other arith
components like simplification.

One unit test is added to ensure the rewriter behaves as expectation.
  • Loading branch information
MasterJH5574 committed Sep 6, 2023
1 parent 4a0fd58 commit 84ca3d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/arith/iter_affine_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class IterMapRewriter : public ExprMutator {
ErrorLogger(this) << "IterMapExpr or subclasses should only result from calls in "
<< "IterMapRewriter using DirectMutate. "
<< "Indirect return occurred in " << input_expr;
return input_expr;
}
return expr;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/python/unittest/test_arith_iter_affine_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,5 +1285,25 @@ def test_normalize_to_iter_sum():
)


def test_detect_iter_map_with_bufferload_recursion():
n = tvm.tir.Var("n", "int32")
m = tvm.tir.Var("m", "int32")
divisor = tvm.tir.Var("divisor", "int32")

i = tvm.tir.Var("i", "int32")
j = tvm.tir.Var("j", "int32")

buffer = tvm.tir.decl_buffer((n,), "int32", name="seqlen")

indices = [(buffer[i] + j) // divisor]
iter_vars = {
i: tvm.ir.Range(tvm.tir.const(0, "int32"), n),
j: tvm.ir.Range(tvm.tir.const(0, "int32"), m),
}

result = tvm.arith.detect_iter_map(indices, iter_vars)
assert len(result.indices) == 0


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

0 comments on commit 84ca3d2

Please sign in to comment.