Skip to content

Commit 4be1224

Browse files
committed
[wip][compiler] Stop rewriting IdentifierId in LeaveSSA
ghstack-source-id: b3c8436 Pull Request resolved: #30573
1 parent 3682b79 commit 4be1224

File tree

2 files changed

+121
-7
lines changed

2 files changed

+121
-7
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/RenameVariables.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import {CompilerError} from '../CompilerError';
99
import {
10+
DeclarationId,
1011
Identifier,
1112
IdentifierId,
1213
IdentifierName,
@@ -121,8 +122,8 @@ class Visitor extends ReactiveFunctionVisitor<Scopes> {
121122
}
122123

123124
class Scopes {
124-
#seen: Map<IdentifierId, IdentifierName> = new Map();
125-
#stack: Array<Map<string, IdentifierId>> = [new Map()];
125+
#seen: Map<DeclarationId, IdentifierName> = new Map();
126+
#stack: Array<Map<string, DeclarationId>> = [new Map()];
126127
#globals: Set<string>;
127128
names: Set<ValidIdentifierName> = new Set();
128129

@@ -135,7 +136,7 @@ class Scopes {
135136
if (originalName === null) {
136137
return;
137138
}
138-
const mappedName = this.#seen.get(identifier.id);
139+
const mappedName = this.#seen.get(identifier.declarationId);
139140
if (mappedName !== undefined) {
140141
identifier.name = mappedName;
141142
return;
@@ -158,12 +159,12 @@ class Scopes {
158159
}
159160
const identifierName = makeIdentifierName(name);
160161
identifier.name = identifierName;
161-
this.#seen.set(identifier.id, identifierName);
162-
this.#stack.at(-1)!.set(identifierName.value, identifier.id);
162+
this.#seen.set(identifier.declarationId, identifierName);
163+
this.#stack.at(-1)!.set(identifierName.value, identifier.declarationId);
163164
this.names.add(identifierName.value);
164165
}
165166

166-
#lookup(name: string): IdentifierId | null {
167+
#lookup(name: string): DeclarationId | null {
167168
for (let i = this.#stack.length - 1; i >= 0; i--) {
168169
const scope = this.#stack[i]!;
169170
const entry = scope.get(name);

compiler/packages/babel-plugin-react-compiler/src/SSA/LeaveSSA.ts

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {CompilerError} from '../CompilerError';
99
import {
1010
BasicBlock,
1111
BlockId,
12+
DeclarationId,
1213
HIRFunction,
1314
Identifier,
1415
InstructionKind,
@@ -27,6 +28,118 @@ import {
2728
terminalFallthrough,
2829
} from '../HIR/visitors';
2930

31+
export function leaveSSA(fn: HIRFunction): void {
32+
const declarations = new Map<DeclarationId, LValue | LValuePattern>();
33+
for (const param of fn.params) {
34+
let place: Place = param.kind === 'Identifier' ? param : param.place;
35+
if (place.identifier.name !== null) {
36+
declarations.set(place.identifier.declarationId, {
37+
kind: InstructionKind.Let,
38+
place,
39+
});
40+
}
41+
}
42+
for (const [, block] of fn.body.blocks) {
43+
for (const instr of block.instructions) {
44+
const {value} = instr;
45+
switch (value.kind) {
46+
case 'DeclareContext':
47+
case 'DeclareLocal': {
48+
const lvalue = value.lvalue;
49+
CompilerError.invariant(
50+
!declarations.has(lvalue.place.identifier.declarationId),
51+
{
52+
reason: `Expected variable not to be defined prior to declaration`,
53+
description: `${printPlace(lvalue.place)} was already defined`,
54+
loc: lvalue.place.loc,
55+
},
56+
);
57+
declarations.set(lvalue.place.identifier.declarationId, lvalue);
58+
if (lvalue.kind === InstructionKind.Let) {
59+
lvalue.kind = InstructionKind.Const;
60+
}
61+
break;
62+
}
63+
case 'StoreContext':
64+
case 'StoreLocal': {
65+
const lvalue = value.lvalue;
66+
if (lvalue.place.identifier.name !== null) {
67+
if (lvalue.kind === InstructionKind.Reassign) {
68+
const declaration = declarations.get(
69+
lvalue.place.identifier.declarationId,
70+
);
71+
CompilerError.invariant(declaration !== undefined, {
72+
reason: `Expected variable to have been defined`,
73+
description: `No declaration for ${printPlace(lvalue.place)}`,
74+
loc: lvalue.place.loc,
75+
});
76+
declaration.kind = InstructionKind.Let;
77+
} else {
78+
CompilerError.invariant(
79+
!declarations.has(lvalue.place.identifier.declarationId),
80+
{
81+
reason: `Expected variable not to be defined prior to declaration`,
82+
description: `${printPlace(lvalue.place)} was already defined`,
83+
loc: lvalue.place.loc,
84+
},
85+
);
86+
declarations.set(lvalue.place.identifier.declarationId, lvalue);
87+
if (lvalue.kind === InstructionKind.Let) {
88+
lvalue.kind = InstructionKind.Const;
89+
}
90+
}
91+
}
92+
break;
93+
}
94+
case 'Destructure': {
95+
const lvalue = value.lvalue;
96+
if (lvalue.kind === InstructionKind.Reassign) {
97+
for (const place of eachPatternOperand(lvalue.pattern)) {
98+
const declaration = declarations.get(
99+
place.identifier.declarationId,
100+
);
101+
CompilerError.invariant(declaration !== undefined, {
102+
reason: `Expected variable to have been defined`,
103+
description: `No declaration for ${printPlace(place)}`,
104+
loc: place.loc,
105+
});
106+
declaration.kind = InstructionKind.Let;
107+
}
108+
} else {
109+
for (const place of eachPatternOperand(lvalue.pattern)) {
110+
CompilerError.invariant(
111+
!declarations.has(place.identifier.declarationId),
112+
{
113+
reason: `Expected variable not to be defined prior to declaration`,
114+
description: `${printPlace(place)} was already defined`,
115+
loc: place.loc,
116+
},
117+
);
118+
declarations.set(place.identifier.declarationId, lvalue);
119+
}
120+
if (lvalue.kind === InstructionKind.Let) {
121+
lvalue.kind = InstructionKind.Const;
122+
}
123+
}
124+
break;
125+
}
126+
case 'PostfixUpdate':
127+
case 'PrefixUpdate': {
128+
const lvalue = value.lvalue;
129+
const declaration = declarations.get(lvalue.identifier.declarationId);
130+
CompilerError.invariant(declaration !== undefined, {
131+
reason: `Expected variable to have been defined`,
132+
description: `No declaration for ${printPlace(lvalue)}`,
133+
loc: lvalue.loc,
134+
});
135+
declaration.kind = InstructionKind.Let;
136+
break;
137+
}
138+
}
139+
}
140+
}
141+
}
142+
30143
/*
31144
* Removes SSA form by converting all phis into explicit bindings and assignments. There are two main categories
32145
* of phis:
@@ -89,7 +202,7 @@ import {
89202
* )
90203
* ```
91204
*/
92-
export function leaveSSA(fn: HIRFunction): void {
205+
export function _leaveSSA(fn: HIRFunction): void {
93206
// Maps identifier names to their original declaration.
94207
const declarations: Map<
95208
string,

0 commit comments

Comments
 (0)