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

Don't cache mutations of Exprs that have only one reference to them #8518

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
32 changes: 22 additions & 10 deletions src/IRMutator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,21 +381,33 @@ Stmt IRMutator::visit(const HoistedStorage *op) {
}

Stmt IRGraphMutator::mutate(const Stmt &s) {
auto p = stmt_replacements.emplace(s, Stmt());
if (p.second) {
// N.B: Inserting into a map (as the recursive mutate call
// does), does not invalidate existing iterators.
p.first->second = IRMutator::mutate(s);
if (s.is_sole_reference()) {
// There's no point in caching mutations of this Stmt. We can never
// possibly see it again, and it can't be in the cache already if this
// is the sole reference. Doing this here and in the Expr mutate method
// below speeds up lowering by about 5%
return IRMutator::mutate(s);
} else {
auto p = stmt_replacements.emplace(s, Stmt());
if (p.second) {
// N.B: Inserting into a map (as the recursive mutate call
// does), does not invalidate existing iterators.
p.first->second = IRMutator::mutate(s);
}
return p.first->second;
}
return p.first->second;
}

Expr IRGraphMutator::mutate(const Expr &e) {
auto p = expr_replacements.emplace(e, Expr());
if (p.second) {
p.first->second = IRMutator::mutate(e);
if (e.is_sole_reference()) {
return IRMutator::mutate(e);
} else {
auto p = expr_replacements.emplace(e, Expr());
if (p.second) {
p.first->second = IRMutator::mutate(e);
}
return p.first->second;
}
return p.first->second;
}

std::pair<std::vector<Expr>, bool> IRMutator::mutate_with_changes(const std::vector<Expr> &old_exprs) {
Expand Down