Skip to content

Commit

Permalink
[compiler] Add Identifier.declarationId
Browse files Browse the repository at this point in the history
ghstack-source-id: 54973a034bf9ca2e778326a5b0bf367113a63736
Pull Request resolved: #30569
  • Loading branch information
josephsavona committed Aug 1, 2024
1 parent 56dbd58 commit 3682b79
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
31 changes: 28 additions & 3 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,19 @@ export type NonLocalBinding =

// Represents a user-defined variable (has a name) or a temporary variable (no name).
export type Identifier = {
/*
* unique value to distinguish a variable, since name is not guaranteed to
* exist or be unique
/**
* After EnterSSA, `id` uniquely identifies an SSA instance of a variable.
* Before EnterSSA, `id` matches `declarationId`.
*/
id: IdentifierId;

/**
* Uniquely identifies a given variable in the original program. If a value is
* reassigned in the original program each reassigned value will have a distinct
* `id` (after EnterSSA), but they will still have the same `declarationId`.
*/
declarationId: DeclarationId;

// null for temporaries. name is primarily used for debugging.
name: IdentifierName | null;
// The range for which this variable is mutable
Expand Down Expand Up @@ -1494,6 +1502,23 @@ export function makeIdentifierId(id: number): IdentifierId {
return id as IdentifierId;
}

/*
* Simulated opaque type for IdentifierId to prevent using normal numbers as ids
* accidentally.
*/
const opageDeclarationId = Symbol();
export type DeclarationId = number & {[opageDeclarationId]: 'DeclarationId'};

export function makeDeclarationId(id: number): DeclarationId {
CompilerError.invariant(id >= 0 && Number.isInteger(id), {
reason: 'Expected declaration id to be a non-negative integer',
description: null,
loc: null,
suggestions: null,
});
return id as DeclarationId;
}

/*
* Simulated opaque type for InstructionId to prevent using normal numbers as ids
* accidentally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
Terminal,
VariableBinding,
makeBlockId,
makeDeclarationId,
makeIdentifierName,
makeInstructionId,
makeType,
Expand Down Expand Up @@ -184,6 +185,7 @@ export default class HIRBuilder {
const id = this.nextIdentifierId;
return {
id,
declarationId: makeDeclarationId(id),
name: null,
mutableRange: {start: makeInstructionId(0), end: makeInstructionId(0)},
scope: null,
Expand Down Expand Up @@ -326,6 +328,7 @@ export default class HIRBuilder {
const id = this.nextIdentifierId;
const identifier: Identifier = {
id,
declarationId: makeDeclarationId(id),
name: makeIdentifierName(name),
mutableRange: {
start: makeInstructionId(0),
Expand Down Expand Up @@ -895,10 +898,12 @@ export function createTemporaryPlace(
env: Environment,
loc: SourceLocation,
): Place {
const id = env.nextIdentifierId;
return {
kind: 'Identifier',
identifier: {
id: env.nextIdentifierId,
id,
declarationId: makeDeclarationId(id),
mutableRange: {start: makeInstructionId(0), end: makeInstructionId(0)},
name: null,
scope: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
promoteTemporary,
reversePostorderBlocks,
} from '../HIR';
import {markInstructionIds, markPredecessors} from '../HIR/HIRBuilder';
import {
createTemporaryPlace,
markInstructionIds,
markPredecessors,
} from '../HIR/HIRBuilder';
import {eachInstructionValueOperand} from '../HIR/visitors';
import {retainWhere} from '../Utils/utils';

Expand Down Expand Up @@ -225,23 +229,7 @@ function rewriteBlock(
block.instructions.push({
id: makeInstructionId(0),
loc: terminal.loc,
lvalue: {
effect: Effect.Unknown,
identifier: {
id: env.nextIdentifierId,
mutableRange: {
start: makeInstructionId(0),
end: makeInstructionId(0),
},
name: null,
scope: null,
type: makeType(),
loc: terminal.loc,
},
kind: 'Identifier',
reactive: false,
loc: terminal.loc,
},
lvalue: createTemporaryPlace(env, terminal.loc),
value: {
kind: 'StoreLocal',
lvalue: {kind: InstructionKind.Reassign, place: {...returnValue}},
Expand All @@ -267,23 +255,7 @@ function declareTemporary(
block.instructions.push({
id: makeInstructionId(0),
loc: GeneratedSource,
lvalue: {
effect: Effect.Unknown,
identifier: {
id: env.nextIdentifierId,
mutableRange: {
start: makeInstructionId(0),
end: makeInstructionId(0),
},
name: null,
scope: null,
type: makeType(),
loc: result.loc,
},
kind: 'Identifier',
reactive: false,
loc: GeneratedSource,
},
lvalue: createTemporaryPlace(env, result.loc),
value: {
kind: 'DeclareLocal',
lvalue: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class SSABuilder {
makeId(oldId: Identifier): Identifier {
return {
id: this.nextSsaId,
declarationId: oldId.declarationId,
name: oldId.name,
mutableRange: {
start: makeInstructionId(0),
Expand Down

0 comments on commit 3682b79

Please sign in to comment.