Skip to content

Implement codegen for when blocks (#501)#501

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

Implement codegen for when blocks (#501)#501
daniellerozenblit wants to merge 6 commits intofacebook:devfrom
daniellerozenblit:export-D96342427

Conversation

@daniellerozenblit
Copy link
Contributor

@daniellerozenblit daniellerozenblit commented Mar 12, 2026

Summary:

Stack

The goal of this stack is to add support for conditional when blocks to sddl.

Diff

Implement code generation for when blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the when block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the jump_if VM opcode—this ties them all together.

Changes

  • generateWhen(): New method that generates code for when blocks using the jump_if opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

  • generateBlock(): Extracted block generation logic from generate() to enable reuse for when block bodies and record fields.

Differential Revision: D96342427

@meta-cla meta-cla bot added the cla signed label Mar 12, 2026
@meta-codesync
Copy link

meta-codesync bot commented Mar 12, 2026

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

daniellerozenblit added a commit to daniellerozenblit/openzl-1 that referenced this pull request Mar 12, 2026
Summary:
Pull Request resolved: facebook#501

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together.

### Changes

- **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

- **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields.

Differential Revision: D96342427
daniellerozenblit added a commit to daniellerozenblit/openzl-1 that referenced this pull request Mar 12, 2026
Summary:
Pull Request resolved: facebook#501

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together.

### Changes

- **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

- **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields.

Differential Revision: D96342427
daniellerozenblit added a commit to daniellerozenblit/openzl-1 that referenced this pull request Mar 12, 2026
Summary:
Pull Request resolved: facebook#501

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together.

### Changes

- **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

- **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields.

Differential Revision: D96342427
@meta-codesync meta-codesync bot changed the title Implement codegen for when blocks Implement codegen for when blocks (#501) Mar 12, 2026
daniellerozenblit and others added 6 commits March 12, 2026 14:27
Summary:
## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
This diff adds parsing and semantic analysis support for `when` blocks in sddl..

### Changes

- **ASTWhen node:** New AST node type representing a conditional `when` block. Contains a condition expression and a body of statements.

- **WhenRule grammar:** New grammar rule that parses `when <expr> { <statements> }` syntax.

- **SemanticAnalyzer:** Added `analyze(ASTWhen)` to validate the condition is numeric and recursively analyze the body statements.

Differential Revision: D96342217
Summary:
## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
This diff implements optimizer passes for `when` blocks in sddl., enabling constant folding and dead variable elimination for conditional constructs.

### Changes

- **ConstFoldPass:** Implemented `optimizeWhen()` that folds constant conditions—eliminates `when 0 { ... }` blocks entirely (dead code removal) and inlines the body for `when <non-zero> { ... }`.

- **DeadVarPass:** Implemented `optimizeWhen()` that tracks variable references in condition and body, applies dead variable elimination to when body.

Differential Revision: D96342234
Summary:
## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
This diff adds a new VM opcode `jump_if` that conditionally skips N instructions based on a condition. This opcode is required for implementing `when` blocks.

Differential Revision: D96342363
Summary:
Pull Request resolved: facebook#503

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Introduce a dedicated `ASTRecordField` type to explicitly represent record fields instead of using `ASSUME` operations.

Previously, record fields were represented as `ASSUME` ops (e.g., `field_name assume Type`), which made it difficult to distinguish field declarations from other operations. This change creates a proper AST node type for record fields, improving code clarity and enabling better support for conditional fields in subsequent diffs.

Differential Revision: D96342377
Summary:
Pull Request resolved: facebook#505

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Add semantic analysis check to prevent member access (e.g., `data.optional`) on fields declared inside `when` blocks.

Conditional fields may or may not exist at runtime depending on the `when` condition, so allowing direct member access would lead to undefined behavior.

Differential Revision: D96347373
Summary:
Pull Request resolved: facebook#501

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together.

### Changes

- **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

- **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields.

Differential Revision: D96342427
daniellerozenblit added a commit to daniellerozenblit/openzl-1 that referenced this pull request Mar 16, 2026
Summary:
Pull Request resolved: facebook#501

## Stack
The goal of this stack is to add support for conditional `when` blocks to sddl.

## Diff
Implement code generation for `when` blocks, enabling conditional field consumption based on runtime expressions.

This diff completes the `when` block feature by adding the code generation phase. Previous diffs in the stack added parsing, semantic analysis, optimizer support, and the `jump_if` VM opcode—this ties them all together.

### Changes

- **generateWhen():** New method that generates code for `when` blocks using the `jump_if` opcode. Emits the body instruction count, evaluates the condition, negates it, and conditionally skips the body.

- **generateBlock():** Extracted block generation logic from `generate()` to enable reuse for `when` block bodies and record fields.

Differential Revision: D96342427
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