Skip to content

compiler: Infer dependencies for pruned scopes #29783

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

Closed
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
isUseRefType,
makeInstructionId,
Place,
PrunedReactiveScopeBlock,
ReactiveFunction,
ReactiveInstruction,
ReactiveScope,
Expand Down Expand Up @@ -687,6 +688,16 @@ class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
scope.scope.dependencies = scopeDependencies;
}

override visitPrunedScope(
scopeBlock: PrunedReactiveScopeBlock,
context: Context
): void {
const scopeDependencies = context.enter(scopeBlock.scope, () => {
this.visitBlock(scopeBlock.instructions, context);
});
scopeBlock.scope.dependencies = scopeDependencies;
}

override visitInstruction(
instruction: ReactiveInstruction,
context: Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import {
IdentifierId,
PrunedReactiveScopeBlock,
ReactiveFunction,
ReactiveInstruction,
ReactiveScope,
ReactiveScopeBlock,
isSetStateType,
} from "../HIR";
Expand Down Expand Up @@ -95,23 +97,34 @@ class Visitor extends ReactiveFunctionVisitor<ReactiveIdentifiers> {
state: ReactiveIdentifiers
): void {
this.traverseScope(scopeBlock, state);
for (const dep of scopeBlock.scope.dependencies) {
this.visitScopeImpl(scopeBlock.scope, state);
}

override visitPrunedScope(
scopeBlock: PrunedReactiveScopeBlock,
state: ReactiveIdentifiers
): void {
this.traversePrunedScope(scopeBlock, state);
}

visitScopeImpl(scope: ReactiveScope, state: ReactiveIdentifiers): void {
for (const dep of scope.dependencies) {
const isReactive = state.has(dep.identifier.id);
if (!isReactive) {
scopeBlock.scope.dependencies.delete(dep);
scope.dependencies.delete(dep);
}
}
if (scopeBlock.scope.dependencies.size !== 0) {
if (scope.dependencies.size !== 0) {
/**
* If any of a scope's dependencies are reactive, then all of its
* outputs will re-evaluate whenever those dependencies change.
* Mark all of the outputs as reactive to reflect the fact that
* they may change in practice based on a reactive input.
*/
for (const [, declaration] of scopeBlock.scope.declarations) {
for (const [, declaration] of scope.declarations) {
state.add(declaration.identifier.id);
}
for (const reassignment of scopeBlock.scope.reassignments) {
for (const reassignment of scope.reassignments) {
state.add(reassignment.id);
}
}
Expand Down