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

Fix performance regression in is_monotonic #6105

Merged
merged 1 commit into from Jun 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/IROperator.cpp
Expand Up @@ -1030,6 +1030,21 @@ struct RemoveLikelies : public IRMutator {
}
};

// TODO: There could just be one IRMutator that can remove
// calls from a list. If we need more of these, it might be worth
// doing that refactor.
struct RemovePromises : public IRMutator {
using IRMutator::visit;
Expr visit(const Call *op) override {
if (op->is_intrinsic(Call::promise_clamped) ||
op->is_intrinsic(Call::unsafe_promise_clamped)) {
return mutate(op->args[0]);
} else {
return IRMutator::visit(op);
}
}
};

} // namespace

Expr remove_likelies(const Expr &e) {
Expand All @@ -1040,6 +1055,14 @@ Stmt remove_likelies(const Stmt &s) {
return RemoveLikelies().mutate(s);
}

Expr remove_promises(const Expr &e) {
return RemovePromises().mutate(e);
}

Stmt remove_promises(const Stmt &s) {
return RemovePromises().mutate(s);
}

Expr unwrap_tags(const Expr &e) {
if (const Call *tag = Call::as_tag(e)) {
return unwrap_tags(tag->args[0]);
Expand Down
8 changes: 8 additions & 0 deletions src/IROperator.h
Expand Up @@ -310,6 +310,14 @@ Expr remove_likelies(const Expr &e);
* all calls to likely() and likely_if_innermost() removed. */
Stmt remove_likelies(const Stmt &s);

/** Return an Expr that is identical to the input Expr, but with
* all calls to promise_clamped() and unsafe_promise_clamped() removed. */
Expr remove_promises(const Expr &e);

/** Return a Stmt that is identical to the input Stmt, but with
* all calls to promise_clamped() and unsafe_promise_clamped() removed. */
Stmt remove_promises(const Stmt &s);

/** If the expression is a tag helper call, remove it and return
* the tagged expression. If not, returns the expression. */
Expr unwrap_tags(const Expr &e);
Expand Down
4 changes: 3 additions & 1 deletion src/Monotonic.cpp
Expand Up @@ -631,7 +631,7 @@ ConstantInterval derivative_bounds(const Expr &e, const std::string &var, const
return ConstantInterval::everything();
}
DerivativeBounds m(var, scope);
e.accept(&m);
remove_likelies(remove_promises(e)).accept(&m);
return m.result;
}

Expand Down Expand Up @@ -726,6 +726,8 @@ void is_monotonic_test() {

check_unknown(select(x > 0, y, z));

check_increasing(select(0 < x, promise_clamped(x - 1, x - 1, z) + 1, promise_clamped(x, x, z)));

check_constant(y);

check_increasing(select(x < 17, y, y + 1));
Expand Down