Skip to content

Commit

Permalink
The simplifier sometimes reconstructs expressions without changing them
Browse files Browse the repository at this point in the history
causing this code to recurse infinitely.

Making the simplifier not do this would be awkward. One egregious
instance was fixed.
  • Loading branch information
abadams committed Mar 1, 2016
1 parent bec4b0e commit b6748c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ class Bounds : public IRVisitor {
} else if (op->call_type == Call::Intrinsic &&
(op->name == Call::shift_left || op->name == Call::shift_right || op->name == Call::bitwise_and)) {
Expr simplified = simplify(op);
if (!simplified.same_as(op)) {
if (!equal(simplified, op)) {
simplified.accept(this);
} else {
// Just use the bounds of the type
Expand Down
17 changes: 8 additions & 9 deletions src/Simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,11 @@ class Simplify : public IRMutator {
Expr a = mutate(op->a);
Expr b = mutate(op->b);

// rearrange const + varying to varying + const, to cut down
// on cases to check
if (is_simple_const(a) && !is_simple_const(b)) std::swap(a, b);

// Rearrange a + min or a + max to min + a or max + a to cut down on cases to check
if (b.as<Min>() || b.as<Max>()) {
// Rearrange a few patterns to cut down on the number of cases
// to check later.
if ((is_simple_const(a) && !is_simple_const(b)) ||
(b.as<Min>() && !a.as<Min>()) ||
(b.as<Max>() && !a.as<Max>())) {
std::swap(a, b);
}

Expand Down Expand Up @@ -664,7 +663,7 @@ class Simplify : public IRMutator {
ia % ib == 0) {
// x*4 + y*2 -> (x*2 + y)*2
Expr ratio = make_const(a.type(), div_imp(ia, ib));
expr = mutate((mul_a->a * ratio + mul_b->a) * mul_b->b);
expr = mutate((mul_a->a * ratio + mul_b->a) * mul_b->b);
} else if (a.same_as(op->a) && b.same_as(op->b)) {
// If we've made no changes, and can't find a rule to apply, return the operator unchanged.
expr = op;
Expand Down Expand Up @@ -1029,7 +1028,7 @@ class Simplify : public IRMutator {
const Add *add_a = a.as<Add>();
const Sub *sub_a = a.as<Sub>();
const Mul *mul_a = a.as<Mul>();
const Mul *mul_b = b.as<Mul>();
const Mul *mul_b = b.as<Mul>();

if (is_zero(a)) {
expr = a;
Expand Down Expand Up @@ -3320,7 +3319,7 @@ class Simplify : public IRMutator {
stmt = ProducerConsumer::make(op->name, produce, update, consume);
}
}

void visit(const Block *op) {
Stmt first = mutate(op->first);

Expand Down

0 comments on commit b6748c8

Please sign in to comment.