Skip to content

Commit

Permalink
Add missing condition to if renesting rule (halide#7952)
Browse files Browse the repository at this point in the history
* Add missing condition to if renesting rule

* Add test

* clang-format
  • Loading branch information
abadams authored Nov 21, 2023
1 parent ad0f24e commit f5a4e49
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Simplify_And.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Expr Simplify::visit(const And *op, ExprInfo *bounds) {
rewrite(x && !x, false) ||
rewrite(!x && x, false) ||
rewrite(y <= x && x < y, false) ||
rewrite(y < x && x < y, false) ||
rewrite(x != c0 && x == c1, b, c0 != c1) ||
rewrite(x == c0 && x == c1, false, c0 != c1) ||
// Note: In the predicate below, if undefined overflow
Expand Down
1 change: 1 addition & 0 deletions src/Simplify_Stmts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ Stmt Simplify::visit(const Block *op) {
return mutate(result);
} else if (if_first &&
if_next &&
!if_next->else_case.defined() &&
is_pure(if_first->condition) &&
is_pure(if_next->condition) &&
is_const_one(mutate(!(if_first->condition && if_next->condition), nullptr))) {
Expand Down
18 changes: 18 additions & 0 deletions test/correctness/simplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,24 @@ void check_boolean() {
Block::make(not_no_op(x + 1), not_no_op(x + 2)),
not_no_op(x + 3)));

check(x < y && y < x, const_false());
check(Block::make(IfThenElse::make(x < y, not_no_op(x + 1), not_no_op(x + 2)),
IfThenElse::make(y < x, not_no_op(x + 3))),
IfThenElse::make(x < y, not_no_op(x + 1),
Block::make(not_no_op(x + 2),
IfThenElse::make(y < x, not_no_op(x + 3)))));

check(Block::make(IfThenElse::make(x < y, not_no_op(x + 1), not_no_op(x + 2)),
IfThenElse::make(y <= x, not_no_op(x + 3))),
IfThenElse::make(x < y, not_no_op(x + 1),
Block::make(not_no_op(x + 2),
not_no_op(x + 3))));

check(Block::make(IfThenElse::make(x < y, not_no_op(x + 1), not_no_op(x + 2)),
IfThenElse::make(y <= x, not_no_op(x + 3), not_no_op(x + 4))),
Block::make(IfThenElse::make(x < y, not_no_op(x + 1), not_no_op(x + 2)),
IfThenElse::make(y <= x, not_no_op(x + 3), not_no_op(x + 4))));

// The construct
// if (var == expr) then a else b;
// was being simplified incorrectly, but *only* if var was of type Bool.
Expand Down

0 comments on commit f5a4e49

Please sign in to comment.