@@ -381,21 +381,33 @@ Stmt IRMutator::visit(const HoistedStorage *op) {
381
381
}
382
382
383
383
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 ;
389
398
}
390
- return p.first ->second ;
391
399
}
392
400
393
401
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 ;
397
410
}
398
- return p.first ->second ;
399
411
}
400
412
401
413
std::pair<std::vector<Expr>, bool > IRMutator::mutate_with_changes (const std::vector<Expr> &old_exprs) {
0 commit comments