HIR: High-level Intermediate Representation
HIR (High-level Intermediate Representation) is React Compiler’s primary internal data structure for representing and analyzing JavaScript code.What is HIR?
HIR is a control-flow graph (CFG) representation that:- Preserves high-level JavaScript constructs
- Makes control flow explicit
- Enables precise dataflow analysis
- Supports SSA (Static Single Assignment) form
ifvs ternary (? :) vs logical (&&,||)forvswhilevsfor..ofloops- Method calls vs function calls
HIR Structure
HIRFunction
The top-level structure representing a compiled function:BasicBlock
A sequence of instructions with a single terminator:block: Regular sequential blockloop: Loop header/test blockvalue: Block producing a value (ternary/logical branches)sequence: Sequence expression blockcatch: Exception handler block
Instruction
A single operation:InstructionValue
The operation performed by an instruction:Terminal
Control flow at the end of a block:Place
A reference to a value:Phi Nodes
SSA merge points:Example HIR
Simple Function
Input:Conditional Function
Input:Loop Example
Input:SSA Form
AfterEnterSSA pass, HIR is converted to SSA:
Before SSA:
Effects System
After effect inference, instructions are annotated:Alias a -> b: b aliases aCapture a -> b: b captures aMutate a: a is mutatedFreeze a: a is frozen (immutable)Render a: a is used in render contextImpure a: a contains impure value
Reactive Scopes in HIR
After scope inference, instructions are labeled:[1, 4) depends on props.countScope
[4, 6) depends on $4
Type Annotations
After type inference:Working with HIR
Traversing the CFG
Finding Dependencies
HIR Validation
The compiler includes several HIR validation passes:assertConsistentIdentifiers: All identifier references are validassertTerminalSuccessorsExist: Terminal successors are in the CFGassertTerminalPredsExist: Predecessor sets are correctassertValidBlockNesting: Block nesting is well-formedassertValidMutableRanges: Mutable ranges don’t overlap incorrectly
Next Steps
Architecture
Understand the compiler pipeline
Optimization Passes
Learn about HIR transformations
Contributing
Contribute to the compiler
How It Works
High-level compilation overview