Skip to content

Commit a0af416

Browse files
committed
fix: variable optimization preserving unused nextContext calls due to listeners
With the recent changes for local refs, we now also generate all variables in the create block. This works overall, but we end up with incorrect variable optimization because calls of `nextContext` in listener functions end up tricking the optimization logic into thinking that the outer `nextContext` calls in the create block are _relevant_ for the listener function body. e.g. ``` if (create) { const _bla = nextContext(); i0.listener(() => { restoreView(); const _ctx1 = nextContext(); /* do somth with _ctx1 */ }); } ``` `_bla` can safely be removed here, even though a `ContextRead/Write` has occurred inside the listener operation. We process listeners separately.
1 parent 6dc6117 commit a0af416

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

packages/compiler/src/template/pipeline/src/phases/variable_optimization.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,15 @@ function fencesForIrExpression(expr: ir.Expression): Fence {
276276
function collectOpInfo(op: ir.CreateOp|ir.UpdateOp): OpInfo {
277277
let fences = Fence.None;
278278
const variablesUsed = new Set<ir.XrefId>();
279-
ir.visitExpressionsInOp(op, expr => {
279+
ir.visitExpressionsInOp(op, (expr, flags) => {
280280
if (!ir.isIrExpression(expr)) {
281281
return;
282282
}
283+
// Fences from child operations are not of interest because they allocate
284+
// their own variables.
285+
if (flags & ir.VisitorContextFlag.InChildOperation) {
286+
return;
287+
}
283288

284289
switch (expr.kind) {
285290
case ir.ExpressionKind.ReadVariable:

0 commit comments

Comments
 (0)