Skip to main content

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
The name is inspired by Rust Compiler’s HIR, but React Compiler’s HIR is even more high-level, distinguishing between similar constructs like:
  • if vs ternary (? :) vs logical (&&, ||)
  • for vs while vs for..of loops
  • 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 Kinds:
  • block: Regular sequential block
  • loop: Loop header/test block
  • value: Block producing a value (ternary/logical branches)
  • sequence: Sequence expression block
  • catch: 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:
HIR:

Conditional Function

Input:
HIR:

Loop Example

Input:
HIR (simplified):

SSA Form

After EnterSSA pass, HIR is converted to SSA: Before SSA:
After SSA:

Effects System

After effect inference, instructions are annotated:
Effect Annotations:
  • Alias a -> b: b aliases a
  • Capture a -> b: b captures a
  • Mutate a: a is mutated
  • Freeze a: a is frozen (immutable)
  • Render a: a is used in render context
  • Impure a: a contains impure value

Reactive Scopes in HIR

After scope inference, instructions are labeled:
Scope [1, 4) depends on props.count
Scope [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 valid
  • assertTerminalSuccessorsExist: Terminal successors are in the CFG
  • assertTerminalPredsExist: Predecessor sets are correct
  • assertValidBlockNesting: Block nesting is well-formed
  • assertValidMutableRanges: 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