Skip to content

Add TypeAliasPass optimization pass#473

Open
daniellerozenblit wants to merge 6 commits intofacebook:devfrom
daniellerozenblit:export-D95455849
Open

Add TypeAliasPass optimization pass#473
daniellerozenblit wants to merge 6 commits intofacebook:devfrom
daniellerozenblit:export-D95455849

Conversation

@daniellerozenblit
Copy link
Contributor

Summary:
Add a new optimization pass that resolves type aliases in the AST before
codegen runs. When the pass encounters ASSIGN(MyType, Int32LE) where
the RHS is a field type (builtin, bytes, array, or record), it records
the alias. Later, when it encounters a variable reference to that alias,
it substitutes the resolved type inline.

This runs between ConstFoldPass (which may simplify expressions like
array lengths) and DeadVarPass (which removes the now-dead ASSIGN
definitions whose variables are no longer referenced).

Key behaviors:

  • Simple aliases: MyType = Int32LE: MyType resolves to Int32LE
  • Chained aliases: A = Int32LE, B = A: B resolves transitively
    (because A was already resolved when B's RHS was optimized)
  • Record field scoping: type aliases inside record fields are scoped
    to the record body (save/restore pattern matching ConstFoldPass)

Differential Revision: D95455849

Summary:

This diff splits `CompilerTest.cpp` into separate test files per compiler stage. This makes it easier to test the functionality of each component in isolation; e.g making sure the parser produces the expected AST before optimizations.

Differential Revision: D95084293
Summary:

# Stack
The goal of this stack is to add support for variable assignments and assume operations to SDDL2.

# Diff
This diff implements variable assignment and reference support in the code generator. This enables SDDL2 programs to use global variables for parameterizing field sizes and reusing type definitions.

- Implemented `Op::ASSIGN` codegen: evaluates the RHS expression, allocates a VM register via `getOrAssignRegister()`, and emits `var.store`.
- Implemented `ASTVar` reference codegen: looks up the variable's register and emits `var.load`.  If this is the last reference to a particular variable, returns the register to the free pool.
- Added a register allocator (`var_registry_`, `free_registers_`, `next_register_`) that reuses freed registers and enforces the `SDDL2_VAR_REGISTER_COUNT` (256) limit at compile time.

Differential Revision: D94947414
Summary:

# Stack
The goal of this stack is to add support for variable assignments and assume operations to SDDL2.

# Diff
This diff adds a `MEMBER` op to the SDDL grammar, which can be used to access fields of consumed recrds. This operator (`.`) was already supported in the syntax.

Differential Revision: D95396139
Summary:

# Stack
The goal of this stack is to add support for variable assignments and assume operations to SDDL2.

# Diff
This diff handles the `MEMBER` op in the dead var optimizer.

Differential Revision: D95396507
Summary:

# Stack
The goal of this stack is to add support for variable assignments and assume operations to SDDL2.

# Diff
The diff updates the semantic analyzer to support the `MEMBER` operation. This mainly validates that operands are semantically correct (e.g., LHS is a consumed record, RHS is a field).

Differential Revision: D95430690
Summary:
Add a new optimization pass that resolves type aliases in the AST before
codegen runs. When the pass encounters `ASSIGN(MyType, Int32LE)` where
the RHS is a field type (builtin, bytes, array, or record), it records
the alias. Later, when it encounters a variable reference to that alias,
it substitutes the resolved type inline.

This runs between ConstFoldPass (which may simplify expressions like
array lengths) and DeadVarPass (which removes the now-dead ASSIGN
definitions whose variables are no longer referenced).

Key behaviors:
- Simple aliases: `MyType = Int32LE` → `: MyType` resolves to Int32LE
- Chained aliases: `A = Int32LE`, `B = A` → `: B` resolves transitively
  (because A was already resolved when B's RHS was optimized)
- Record field scoping: type aliases inside record fields are scoped
  to the record body (save/restore pattern matching ConstFoldPass)

Differential Revision: D95455849
@meta-cla meta-cla bot added the cla signed label Mar 6, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 6, 2026

@daniellerozenblit has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95455849.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant