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

[ARITH] Simplify casts of constants 0 and 1 #3758

Merged
merged 4 commits into from
Aug 13, 2019
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
7 changes: 7 additions & 0 deletions src/arithmetic/rewrite_simplify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,13 @@ Mutate_(const Variable* op, const Expr& self) {
return self;
}

Expr RewriteSimplifier::Impl::
Mutate_(const Cast* op, const Expr& self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us just add cast between (uint/float/int here), ignore vector types for now as they are not as common.

Expr ret = IRMutator::Mutate_(op, self);
op = ret.as<Cast>();
return cast(op->type, op->value);
}

Expr RewriteSimplifier::operator()(const Expr& expr) {
// Run simplification in post order
Expr res = expr;
Expand Down
1 change: 1 addition & 0 deletions src/arithmetic/rewrite_simplify.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class RewriteSimplifier::Impl : public IRMutator {
Expr Mutate_(const Call* op, const Expr& self) override;
Expr Mutate_(const Let* op, const Expr& self) override;
Expr Mutate_(const Variable* op, const Expr& self) override;
Expr Mutate_(const Cast* op, const Expr& self) override;

protected:
/*! \brief internal structure for comparison. */
Expand Down
5 changes: 5 additions & 0 deletions src/lang/expr_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ bool is_const_power_of_two_integer(const Expr& x, int* shift) {

Expr cast(const Type& t, Expr value) {
using ir::IntImm;
using ir::UIntImm;
using ir::FloatImm;
if (value.type() == t) return value;
// const fold IntImm as they are used in index computations
if (t.lanes() == 1) {
if (const IntImm* op = value.as<IntImm>()) {
return make_const(t, op->value);
} else if (const UIntImm* op = value.as<UIntImm>()) {
return make_const(t, op->value);
} else if (const FloatImm* op = value.as<FloatImm>()) {
return make_const(t, op->value);
}
Expand All @@ -122,6 +125,8 @@ Expr cast(const Type& t, Expr value) {
if (value.type() != vtype) {
if (const IntImm* op = value.as<IntImm>()) {
value = make_const(vtype, op->value);
} else if (const UIntImm* op = value.as<UIntImm>()) {
return make_const(t, op->value);
} else if (const FloatImm* op = value.as<FloatImm>()) {
value = make_const(vtype, op->value);
} else {
Expand Down
13 changes: 13 additions & 0 deletions tests/python/unittest/test_arith_rewrite_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,18 @@ def test_let_simplify():
z = tvm.expr.Let(x, 1, x + 1)
ck.verify(z + z, 4)

def test_cast_simplify():
ck = RewriteChecker()
x = tvm.var("x")

dtypes = ["float32", "float16", "int32", "int8", "bool"]
for dtype1 in dtypes:
ck.verify(tvm.expr.Cast(dtype1, x - x), tvm.const(0, dtype1))
ck.verify(tvm.expr.Cast(dtype1, x == x), tvm.const(1, dtype1))
for dtype2 in dtypes:
for i in [0, 1, 2, 3]:
ck.verify(tvm.expr.Cast(dtype1, tvm.const(i, dtype2)), tvm.const(i, dtype1))

if __name__ == "__main__":
test_floordiv_index_simplify()
test_floormod_index_simplify()
Expand All @@ -819,3 +831,4 @@ def test_let_simplify():
test_select_simplify()
test_logical_simplify()
test_let_simplify()
test_cast_simplify()