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

[Opt] Algebraic simplification for binary operations with two operands having the same value #2111

Merged
merged 2 commits into from
Dec 22, 2020
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
77 changes: 56 additions & 21 deletions taichi/transforms/alg_simp.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "taichi/ir/analysis.h"
#include "taichi/ir/ir.h"
#include "taichi/ir/statements.h"
#include "taichi/ir/transforms.h"
Expand All @@ -19,6 +20,23 @@ class AlgSimp : public BasicStmtVisitor {
}
}

void replace_with_zero(Stmt *stmt) {
auto zero =
Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(stmt->ret_type));
stmt->replace_with(zero.get());
modifier.insert_before(stmt, std::move(zero));
modifier.erase(stmt);
}

void replace_with_one(Stmt *stmt) {
auto one = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(1));
auto one_raw = one.get();
modifier.insert_before(stmt, std::move(one));
cast_to_result_type(one_raw, stmt);
stmt->replace_with(one_raw);
modifier.erase(stmt);
}

public:
static constexpr int max_weaken_exponent = 32;
using BasicStmtVisitor::visit;
Expand All @@ -39,8 +57,6 @@ class AlgSimp : public BasicStmtVisitor {
void visit(BinaryOpStmt *stmt) override {
auto lhs = stmt->lhs->cast<ConstStmt>();
auto rhs = stmt->rhs->cast<ConstStmt>();
if (!lhs && !rhs)
return;
if (stmt->width() != 1) {
return;
}
Expand All @@ -56,6 +72,17 @@ class AlgSimp : public BasicStmtVisitor {
// 0 +|^ a -> a
stmt->replace_with(stmt->rhs);
modifier.erase(stmt);
} else if (stmt->op_type == BinaryOpType::bit_or &&
irpass::analysis::same_value(stmt->lhs, stmt->rhs)) {
// a | a -> a
stmt->replace_with(stmt->lhs);
modifier.erase(stmt);
} else if ((stmt->op_type == BinaryOpType::sub ||
stmt->op_type == BinaryOpType::bit_xor) &&
(fast_math || is_integral(stmt->ret_type)) &&
irpass::analysis::same_value(stmt->lhs, stmt->rhs)) {
// fast_math or integral operands: a -^ a -> 0
replace_with_zero(stmt);
}
} else if (stmt->op_type == BinaryOpType::mul ||
stmt->op_type == BinaryOpType::div) {
Expand All @@ -71,19 +98,12 @@ class AlgSimp : public BasicStmtVisitor {
stmt->op_type == BinaryOpType::mul &&
(alg_is_zero(lhs) || alg_is_zero(rhs))) {
// fast_math or integral operands: 0 * a -> 0, a * 0 -> 0
if (alg_is_zero(lhs) && lhs->ret_type == stmt->ret_type) {
stmt->replace_with(stmt->lhs);
modifier.erase(stmt);
} else if (alg_is_zero(rhs) && rhs->ret_type == stmt->ret_type) {
stmt->replace_with(stmt->rhs);
modifier.erase(stmt);
} else {
auto zero = Stmt::make<ConstStmt>(
LaneAttribute<TypedConstant>(stmt->ret_type));
stmt->replace_with(zero.get());
modifier.insert_before(stmt, std::move(zero));
modifier.erase(stmt);
}
replace_with_zero(stmt);
} else if ((fast_math || is_integral(stmt->ret_type)) &&
stmt->op_type == BinaryOpType::div &&
irpass::analysis::same_value(stmt->lhs, stmt->rhs)) {
// fast_math or integral operands: a / a -> 1
replace_with_one(stmt);
} else if (stmt->op_type == BinaryOpType::mul &&
(alg_is_two(lhs) || alg_is_two(rhs))) {
// 2 * a -> a + a, a * 2 -> a + a
Expand Down Expand Up @@ -130,12 +150,7 @@ class AlgSimp : public BasicStmtVisitor {
modifier.erase(stmt);
} else if (exponent == 0) {
// a ** 0 -> 1
auto one = Stmt::make<ConstStmt>(LaneAttribute<TypedConstant>(1));
auto one_raw = one.get();
modifier.insert_before(stmt, std::move(one));
cast_to_result_type(one_raw, stmt);
stmt->replace_with(one_raw);
modifier.erase(stmt);
replace_with_one(stmt);
} else if (exponent == 0.5) {
// a ** 0.5 -> sqrt(a)
auto a = stmt->lhs;
Expand Down Expand Up @@ -205,6 +220,10 @@ class AlgSimp : public BasicStmtVisitor {
// -1 & a -> a
stmt->replace_with(stmt->rhs);
modifier.erase(stmt);
} else if (irpass::analysis::same_value(stmt->lhs, stmt->rhs)) {
// a & a -> a
stmt->replace_with(stmt->lhs);
modifier.erase(stmt);
}
} else if (stmt->op_type == BinaryOpType::bit_sar ||
stmt->op_type == BinaryOpType::bit_shl ||
Expand All @@ -218,6 +237,22 @@ class AlgSimp : public BasicStmtVisitor {
stmt->replace_with(stmt->lhs);
modifier.erase(stmt);
}
} else if (is_comparison(stmt->op_type)) {
if ((fast_math || is_integral(stmt->lhs->ret_type)) &&
irpass::analysis::same_value(stmt->lhs, stmt->rhs)) {
// fast_math or integral operands: a == a -> 1, a != a -> 0
if (stmt->op_type == BinaryOpType::cmp_eq ||
stmt->op_type == BinaryOpType::cmp_ge ||
stmt->op_type == BinaryOpType::cmp_le) {
replace_with_one(stmt);
} else if (stmt->op_type == BinaryOpType::cmp_ne ||
stmt->op_type == BinaryOpType::cmp_gt ||
stmt->op_type == BinaryOpType::cmp_lt) {
replace_with_zero(stmt);
} else {
TI_NOT_IMPLEMENTED
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions taichi/transforms/check_out_of_bound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class CheckOutOfBound : public BasicStmtVisitor {
break;
}
}
if (modified)
irpass::type_check(node);
return modified;
}
};
Expand Down