Skip to content

Commit

Permalink
[wip][compiler] Stop rewriting IdentifierId in LeaveSSA
Browse files Browse the repository at this point in the history
ghstack-source-id: b3c84364193cc58d405edda235252336d0aefab5
Pull Request resolved: #30573
  • Loading branch information
josephsavona committed Aug 1, 2024
1 parent 3682b79 commit 4be1224
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {CompilerError} from '../CompilerError';
import {
DeclarationId,
Identifier,
IdentifierId,
IdentifierName,
Expand Down Expand Up @@ -121,8 +122,8 @@ class Visitor extends ReactiveFunctionVisitor<Scopes> {
}

class Scopes {
#seen: Map<IdentifierId, IdentifierName> = new Map();
#stack: Array<Map<string, IdentifierId>> = [new Map()];
#seen: Map<DeclarationId, IdentifierName> = new Map();
#stack: Array<Map<string, DeclarationId>> = [new Map()];
#globals: Set<string>;
names: Set<ValidIdentifierName> = new Set();

Expand All @@ -135,7 +136,7 @@ class Scopes {
if (originalName === null) {
return;
}
const mappedName = this.#seen.get(identifier.id);
const mappedName = this.#seen.get(identifier.declarationId);
if (mappedName !== undefined) {
identifier.name = mappedName;
return;
Expand All @@ -158,12 +159,12 @@ class Scopes {
}
const identifierName = makeIdentifierName(name);
identifier.name = identifierName;
this.#seen.set(identifier.id, identifierName);
this.#stack.at(-1)!.set(identifierName.value, identifier.id);
this.#seen.set(identifier.declarationId, identifierName);
this.#stack.at(-1)!.set(identifierName.value, identifier.declarationId);
this.names.add(identifierName.value);
}

#lookup(name: string): IdentifierId | null {
#lookup(name: string): DeclarationId | null {
for (let i = this.#stack.length - 1; i >= 0; i--) {
const scope = this.#stack[i]!;
const entry = scope.get(name);
Expand Down
115 changes: 114 additions & 1 deletion compiler/packages/babel-plugin-react-compiler/src/SSA/LeaveSSA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {CompilerError} from '../CompilerError';
import {
BasicBlock,
BlockId,
DeclarationId,
HIRFunction,
Identifier,
InstructionKind,
Expand All @@ -27,6 +28,118 @@ import {
terminalFallthrough,
} from '../HIR/visitors';

export function leaveSSA(fn: HIRFunction): void {
const declarations = new Map<DeclarationId, LValue | LValuePattern>();
for (const param of fn.params) {
let place: Place = param.kind === 'Identifier' ? param : param.place;
if (place.identifier.name !== null) {
declarations.set(place.identifier.declarationId, {
kind: InstructionKind.Let,
place,
});
}
}
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
const {value} = instr;
switch (value.kind) {
case 'DeclareContext':
case 'DeclareLocal': {
const lvalue = value.lvalue;
CompilerError.invariant(
!declarations.has(lvalue.place.identifier.declarationId),
{
reason: `Expected variable not to be defined prior to declaration`,
description: `${printPlace(lvalue.place)} was already defined`,
loc: lvalue.place.loc,
},
);
declarations.set(lvalue.place.identifier.declarationId, lvalue);
if (lvalue.kind === InstructionKind.Let) {
lvalue.kind = InstructionKind.Const;
}
break;
}
case 'StoreContext':
case 'StoreLocal': {
const lvalue = value.lvalue;
if (lvalue.place.identifier.name !== null) {
if (lvalue.kind === InstructionKind.Reassign) {
const declaration = declarations.get(
lvalue.place.identifier.declarationId,
);
CompilerError.invariant(declaration !== undefined, {
reason: `Expected variable to have been defined`,
description: `No declaration for ${printPlace(lvalue.place)}`,
loc: lvalue.place.loc,
});
declaration.kind = InstructionKind.Let;
} else {
CompilerError.invariant(
!declarations.has(lvalue.place.identifier.declarationId),
{
reason: `Expected variable not to be defined prior to declaration`,
description: `${printPlace(lvalue.place)} was already defined`,
loc: lvalue.place.loc,
},
);
declarations.set(lvalue.place.identifier.declarationId, lvalue);
if (lvalue.kind === InstructionKind.Let) {
lvalue.kind = InstructionKind.Const;
}
}
}
break;
}
case 'Destructure': {
const lvalue = value.lvalue;
if (lvalue.kind === InstructionKind.Reassign) {
for (const place of eachPatternOperand(lvalue.pattern)) {
const declaration = declarations.get(
place.identifier.declarationId,
);
CompilerError.invariant(declaration !== undefined, {
reason: `Expected variable to have been defined`,
description: `No declaration for ${printPlace(place)}`,
loc: place.loc,
});
declaration.kind = InstructionKind.Let;
}
} else {
for (const place of eachPatternOperand(lvalue.pattern)) {
CompilerError.invariant(
!declarations.has(place.identifier.declarationId),
{
reason: `Expected variable not to be defined prior to declaration`,
description: `${printPlace(place)} was already defined`,
loc: place.loc,
},
);
declarations.set(place.identifier.declarationId, lvalue);
}
if (lvalue.kind === InstructionKind.Let) {
lvalue.kind = InstructionKind.Const;
}
}
break;
}
case 'PostfixUpdate':
case 'PrefixUpdate': {
const lvalue = value.lvalue;
const declaration = declarations.get(lvalue.identifier.declarationId);
CompilerError.invariant(declaration !== undefined, {
reason: `Expected variable to have been defined`,
description: `No declaration for ${printPlace(lvalue)}`,
loc: lvalue.loc,
});
declaration.kind = InstructionKind.Let;
break;
}
}
}
}
}

/*
* Removes SSA form by converting all phis into explicit bindings and assignments. There are two main categories
* of phis:
Expand Down Expand Up @@ -89,7 +202,7 @@ import {
* )
* ```
*/
export function leaveSSA(fn: HIRFunction): void {
export function _leaveSSA(fn: HIRFunction): void {
// Maps identifier names to their original declaration.
const declarations: Map<
string,
Expand Down

0 comments on commit 4be1224

Please sign in to comment.