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

[compiler][donotcommit] show differences between identifier + scope ranges #30409

Closed
wants to merge 1 commit into from
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 @@ -98,6 +98,7 @@ import {
} from "../Validation";
import { validateLocalsNotReassignedAfterRender } from "../Validation/ValidateLocalsNotReassignedAfterRender";
import { outlineFunctions } from "../Optimization/OutlineFunctions";
import { assertMutableRangesAreAlwaysScopes } from "../HIR/BuildReactiveScopeTerminalsHIR";

export type CompilerPipelineValue =
| { kind: "ast"; name: string; value: CodegenFunction }
Expand Down Expand Up @@ -299,6 +300,8 @@ function* runWithEnvironment(
value: hir,
});

assertMutableRangesAreAlwaysScopes(hir);

assertValidBlockNesting(hir);

flattenReactiveLoopsHIR(hir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,74 @@ import {
GotoVariant,
HIRFunction,
InstructionId,
MutableRange,
Place,
ReactiveScope,
ReactiveScopeTerminal,
ScopeId,
} from "./HIR";
import { printPlace } from "./PrintHIR";
import {
eachInstructionLValue,
eachInstructionOperand,
eachTerminalOperand,
} from "./visitors";

export function assertMutableRangesAreAlwaysScopes(fn: HIRFunction): void {
const scopeRanges: Set<MutableRange> = new Set();

for (const [_, block] of fn.body.blocks) {
if (
block.terminal.kind === "scope" ||
block.terminal.kind === "pruned-scope"
) {
scopeRanges.add(block.terminal.scope.range);
}
}

const validate = (place: Place): void => {
if (
place.identifier.mutableRange.end >
place.identifier.mutableRange.start + 1
) {
if (scopeRanges.size !== 0) {
CompilerError.invariant(
scopeRanges.has(place.identifier.mutableRange),
{
reason: "Mutable range not found in scope ranges!",
description: `${printPlace(place)}`,
loc: place.loc,
}
);
}
if (place.identifier.scope != null) {
// Useful for debugging before we build reactive scope terminals
CompilerError.invariant(
place.identifier.scope.range === place.identifier.mutableRange,
{
reason: "Mutable range inconsistent with range of attached scope",
description: `${printPlace(place)}`,
loc: place.loc,
}
);
}
}
};

for (const [_, block] of fn.body.blocks) {
for (const instr of block.instructions) {
for (const place of eachInstructionLValue(instr)) {
validate(place);
}
for (const place of eachInstructionOperand(instr)) {
validate(place);
}
}
for (const place of eachTerminalOperand(block.terminal)) {
validate(place);
}
}
}

/**
* This pass assumes that all program blocks are properly nested with respect to fallthroughs
Expand Down
4 changes: 4 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,10 @@ export type AbstractValue = {
context: ReadonlySet<Place>;
};

export function isRangeMutable(range: MutableRange): boolean {
return range.end > range.start + 1;
}

/**
* The reason for the kind of a value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
InstructionId,
Place,
ReactiveScope,
isRangeMutable,
makeInstructionId,
} from ".";
import { getPlaceScope } from "../ReactiveScopes/BuildReactiveBlocks";
Expand Down Expand Up @@ -124,6 +125,9 @@ export function mergeOverlappingReactiveScopesHIR(fn: HIRFunction): void {
const nextScope = joinedScopes.find(originalScope);
if (nextScope !== null && nextScope !== originalScope) {
place.identifier.scope = nextScope;
if (isRangeMutable(place.identifier.mutableRange)) {
place.identifier.mutableRange = nextScope.range;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
InstructionValue,
LValue,
ManualMemoDependency,
MutableRange,

Check failure on line 24 in compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

'MutableRange' is defined but never used. Allowed unused vars must match /^_/u
ObjectMethod,
ObjectPropertyKey,
Pattern,
Expand All @@ -35,7 +35,7 @@
Terminal,
Type,
} from "./HIR";
import { GotoVariant, InstructionKind } from "./HIR";
import { GotoVariant, InstructionKind, isRangeMutable } from "./HIR";

export type Options = {
indent: number;
Expand Down Expand Up @@ -716,10 +716,6 @@
return value;
}

function isMutable(range: MutableRange): boolean {
return range.end > range.start + 1;
}

const DEBUG_MUTABLE_RANGES = false;
function printMutableRange(identifier: Identifier): string {
if (DEBUG_MUTABLE_RANGES) {
Expand All @@ -732,11 +728,11 @@
) {
return `[${range.start}:${range.end}] scope=[${scopeRange.start}:${scopeRange.end}]`;
}
return isMutable(range) ? `[${range.start}:${range.end}]` : "";
return isRangeMutable(range) ? `[${range.start}:${range.end}]` : "";
}
// in non-debug mode, prefer the scope range if it exists
const range = identifier.scope?.range ?? identifier.mutableRange;
return isMutable(range) ? `[${range.start}:${range.end}]` : "";
return isRangeMutable(range) ? `[${range.start}:${range.end}]` : "";
}

export function printLValue(lval: LValue): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HIRFunction,
IdentifierId,
ReactiveScope,
isRangeMutable,
makeInstructionId,
} from "../HIR";
import DisjointSet from "../Utils/DisjointSet";
Expand Down Expand Up @@ -69,10 +70,19 @@ export function alignMethodCallScopes(fn: HIRFunction): void {
const mappedScope = scopeMapping.get(instr.lvalue.identifier.id);
if (mappedScope !== undefined) {
instr.lvalue.identifier.scope = mappedScope;
if (
mappedScope != null &&
isRangeMutable(instr.lvalue.identifier.mutableRange)
) {
instr.lvalue.identifier.mutableRange = mappedScope.range;
}
} else if (instr.lvalue.identifier.scope !== null) {
const mergedScope = mergedScopes.find(instr.lvalue.identifier.scope);
if (mergedScope != null) {
instr.lvalue.identifier.scope = mergedScope;
if (isRangeMutable(instr.lvalue.identifier.mutableRange)) {
instr.lvalue.identifier.mutableRange = mergedScope.range;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
HIRFunction,
Identifier,
ReactiveScope,
isRangeMutable,
makeInstructionId,
} from "../HIR";
import { eachInstructionValueOperand } from "../HIR/visitors";
Expand Down Expand Up @@ -93,6 +94,9 @@ export function alignObjectMethodScopes(fn: HIRFunction): void {
const root = scopeGroupsMap.get(identifier.scope);
if (root != null) {
identifier.scope = root;
if (isRangeMutable(identifier.mutableRange)) {
identifier.mutableRange = root.range;
}
}
// otherwise, this identifier's scope was not affected by this pass
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import {
HIRFunction,
Identifier,

Check failure on line 10 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MemoizeFbtAndMacroOperandsInSameScope.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

'Identifier' is defined but never used. Allowed unused vars must match /^_/u
IdentifierId,
makeInstructionId,
Place,
ReactiveValue,
isRangeMutable,
} from "../HIR";
import { eachReactiveValueOperand } from "./visitors";

Expand Down Expand Up @@ -134,6 +136,9 @@
*/
for (const operand of eachReactiveValueOperand(value)) {
operand.identifier.scope = fbtScope;
if (isRangeMutable(operand.identifier.mutableRange)) {
operand.identifier.mutableRange = fbtScope.range;
}

// Expand the jsx element's range to account for its operands
fbtScope.range.start = makeInstructionId(
Expand Down Expand Up @@ -167,6 +172,9 @@
continue;
}
operand.identifier.scope = fbtScope;
if (isRangeMutable(operand.identifier.mutableRange)) {
operand.identifier.mutableRange = fbtScope.range;
}

// Expand the jsx element's range to account for its operands
fbtScope.range.start = makeInstructionId(
Expand Down
Loading