Skip to content

Commit

Permalink
[Fix][Arith] Analyzer simplification starts with canonical
Browse files Browse the repository at this point in the history
This PR updates the order of arithmetic analyzer simplification, by
adding a stage of canonical simplification at the very beginning so
that every simplification always starts with a canonical round. This
is because the rewrite simplification may destroy some PrimExpr property
that the canonical simplification can make use of. Therefore, adding
the canonical one in the front can maximize the use of canonical
simplification.
  • Loading branch information
MasterJH5574 committed Feb 2, 2023
1 parent 92b688c commit 361feea
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/arith/analyzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ bool Analyzer::CanProve(const PrimExpr& expr) {
PrimExpr Analyzer::Simplify(const PrimExpr& expr, int steps) {
PrimExpr res = expr;

// Always starts with a canonical simplification, as some structural property
// of an expression might be destroyed by rewrite simplification.
res = this->canonical_simplify(res);

for (int i = 0; i < steps; ++i) {
if (tir::is_const_int(res)) {
return res;
Expand Down
2 changes: 1 addition & 1 deletion src/tir/op/op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ PrimExpr any(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span sp
PrimExpr max(PrimExpr source, Array<IterVar> rdom, Array<PrimExpr> init, Span span) {
Var x("x", source.dtype(), span), y("y", source.dtype(), span);
PrimExpr result = tir::Max(x, y, span);
PrimExpr identity_element = min_value(source.dtype(), span);
PrimExpr identity_element = min_value_symmetric(source.dtype(), span);
tir::CommReducer combiner = tir::CommReducer({x}, {y}, {result}, {identity_element}, span);
return tir::Reduce(combiner, {source}, rdom, make_const(DataType::Bool(1), true), 0, init, span);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/python/unittest/test_arith_canonical_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,13 @@ def test_simplify_cast():
ck.verify(res, 2)


def test_simplify_normalize_min_value_not_error():
ck = CanonicalChecker()
x = te.var("x", "int32")
expr = te.min_value_symmetric("int32") - x == 0
# The simplify should not error in this case.
ck.verify(expr, 0 - x == te.max_value("int32"))


if __name__ == "__main__":
tvm.testing.main()
11 changes: 2 additions & 9 deletions tests/python/unittest/test_arith_intset.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def check_region_bound(expect_region, var_dom, mode, predicate=None):
expect_begin, expect_end = expect_desc[binding]
result_begin = analyzer.simplify(intset.min_value, 3)
result_end = analyzer.simplify(intset.max_value + 1, 3)
print(result_end)
assert analyzer.can_prove_equal(
result_begin - expect_begin, 0
), f"{result_begin} vs {expect_begin}"
Expand Down Expand Up @@ -306,10 +305,7 @@ def test_region_lower_bound_for_non_perfect_tile():
+ h2: {
(): (
tvm.tir.max(h3 * 8, 1),
tvm.tir.max(h3 * 8, 1)
- tvm.tir.max(h3 * 8, 214)
- tvm.tir.max(1 - h3 * 8, 0)
+ 224,
tvm.tir.min(0, h3 * 8 - 214) + 224,
),
((h3, 0),): (1, 10), # h3 == 0: region is [1, 10)
((h3, 10),): (h3 * 8, h3 * 8 + 10), # 0 < h3 <= 26: region is [h3 * 8, h3 * 8 + 10)
Expand All @@ -333,10 +329,7 @@ def test_region_lower_bound_for_non_perfect_tile():
+ h1: {
(): (
tvm.tir.max(h3 * 8, 1),
tvm.tir.max(h3 * 8, 1)
- tvm.tir.max(h3 * 8, 214)
- tvm.tir.max(1 - h3 * 8, 0)
+ 224,
tvm.tir.min(0, h3 * 8 - 214) + 224,
),
((h3, 0),): (1, 10),
((h3, 10),): (h3 * 8, h3 * 8 + 10),
Expand Down
38 changes: 38 additions & 0 deletions tests/python/unittest/test_arith_simplify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import tvm
import tvm.testing
from tvm import tir


def test_simplify_reshape_flattened_index():
ana = tvm.arith.Analyzer()

i0 = tir.Var("i0", "int64")
i1 = tir.Var("i1", "int64")
ana.bind(i0, tvm.ir.Range(0, 8))
ana.bind(i1, tvm.ir.Range(0, 3))

i_flattened = i0 * 3 + i1
assert tvm.ir.structural_equal(
ana.simplify((i_flattened) // 12 * 12 + (i_flattened) % 12 // 4 * 4 + (i_flattened) % 4),
i_flattened,
)


if __name__ == "__main__":
tvm.testing.main()
2 changes: 1 addition & 1 deletion tests/python/unittest/test_tir_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def assert_simplified_equal(index_simplified, index_direct):
index_simplified = A.offset_of(
(idxd(idxm(k0, idxd(k1, s)), n), idxm(idxm(k0, idxd(k1, s)), n) + idxm(k0, k1))
)
index_direct = A.offset_of((0, idxm(k0, k1) + idxm(k0, idxd(k1, s))))
index_direct = A.offset_of((0, idxm(k0, idxd(k1, s)) + idxm(k0, k1)))
assert_simplified_equal(index_simplified, index_direct)
# Test Case3
index_simplified = A.offset_of(
Expand Down
6 changes: 3 additions & 3 deletions tests/python/unittest/test_tir_schedule_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_suggest_index_map_winograd():
floordiv(i0, 2),
floordiv(i1, 2),
floormod(i0, 2),
floormod(((i1 * 4) + floordiv(i2, 32)), 8),
floormod(i1, 2) * 4 + floordiv(i2, 32),
floormod(i2, 32),
floordiv(i3, 32),
floormod(i3, 32),
Expand All @@ -137,8 +137,8 @@ def test_suggest_index_map_winograd():
expected_inverse_index_map = IndexMap.from_func(
lambda i0, i1, i2, i3, i4, i5, i6: (
((i0 * 2) + i2),
((i1 * 2) + floordiv(((i3 * 32) + i4), 128)),
floormod(((i3 * 32) + i4), 128),
i1 * 2 + floordiv(i3, 4),
floormod(i3, 4) * 32 + i4,
((i5 * 32) + i6),
)
)
Expand Down

0 comments on commit 361feea

Please sign in to comment.