Skip to content

Commit c11d977

Browse files
authored
Don't cache mutations of Exprs that have only one reference to them (#8518)
Don't cache mutations of Exprs that have only one reference to them. This speeds up lowering of local laplacian by about 5%
1 parent 526364f commit c11d977

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/IRMutator.cpp

+22-10
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,33 @@ Stmt IRMutator::visit(const HoistedStorage *op) {
381381
}
382382

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

393401
Expr IRGraphMutator::mutate(const Expr &e) {
394-
auto p = expr_replacements.emplace(e, Expr());
395-
if (p.second) {
396-
p.first->second = IRMutator::mutate(e);
402+
if (e.is_sole_reference()) {
403+
return IRMutator::mutate(e);
404+
} else {
405+
auto p = expr_replacements.emplace(e, Expr());
406+
if (p.second) {
407+
p.first->second = IRMutator::mutate(e);
408+
}
409+
return p.first->second;
397410
}
398-
return p.first->second;
399411
}
400412

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

0 commit comments

Comments
 (0)