Home / Function/ pruneUnusedLabelsHIR() — react Function Reference

pruneUnusedLabelsHIR() — react Function Reference

Architecture documentation for the pruneUnusedLabelsHIR() function in PruneUnusedLabelsHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  aef694e8_06b7_3bfd_6242_d3718fc65a5b["pruneUnusedLabelsHIR()"]
  957ab73b_3e28_1296_bcda_0a36395cb663["PruneUnusedLabelsHIR.ts"]
  aef694e8_06b7_3bfd_6242_d3718fc65a5b -->|defined in| 957ab73b_3e28_1296_bcda_0a36395cb663
  style aef694e8_06b7_3bfd_6242_d3718fc65a5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/PruneUnusedLabelsHIR.ts lines 11–87

export function pruneUnusedLabelsHIR(fn: HIRFunction): void {
  const merged: Array<{
    label: BlockId;
    next: BlockId;
    fallthrough: BlockId;
  }> = [];
  const rewrites: Map<BlockId, BlockId> = new Map();
  for (const [blockId, block] of fn.body.blocks) {
    const terminal = block.terminal;
    if (terminal.kind === 'label') {
      const {block: nextId, fallthrough: fallthroughId} = terminal;
      const next = fn.body.blocks.get(nextId)!;
      const fallthrough = fn.body.blocks.get(fallthroughId)!;
      if (
        next.terminal.kind === 'goto' &&
        next.terminal.variant === GotoVariant.Break &&
        next.terminal.block === fallthroughId
      ) {
        if (next.kind === 'block' && fallthrough.kind === 'block') {
          // Only merge normal block types
          merged.push({
            label: blockId,
            next: nextId,
            fallthrough: fallthroughId,
          });
        }
      }
    }
  }

  for (const {
    label: originalLabelId,
    next: nextId,
    fallthrough: fallthroughId,
  } of merged) {
    const labelId = rewrites.get(originalLabelId) ?? originalLabelId;
    const label = fn.body.blocks.get(labelId)!;
    const next = fn.body.blocks.get(nextId)!;
    const fallthrough = fn.body.blocks.get(fallthroughId)!;

    // Merge block and fallthrough
    CompilerError.invariant(
      next.phis.size === 0 && fallthrough.phis.size === 0,
      {
        reason: 'Unexpected phis when merging label blocks',
        loc: label.terminal.loc,
      },
    );

    CompilerError.invariant(
      next.preds.size === 1 &&
        fallthrough.preds.size === 1 &&
        next.preds.has(originalLabelId) &&
        fallthrough.preds.has(nextId),
      {
        reason: 'Unexpected block predecessors when merging label blocks',
        loc: label.terminal.loc,
      },
    );

    label.instructions.push(...next.instructions, ...fallthrough.instructions);
    label.terminal = fallthrough.terminal;
    fn.body.blocks.delete(nextId);
    fn.body.blocks.delete(fallthroughId);
    rewrites.set(fallthroughId, labelId);
  }

  for (const [_, block] of fn.body.blocks) {
    for (const pred of block.preds) {
      const rewritten = rewrites.get(pred);
      if (rewritten != null) {
        block.preds.delete(pred);
        block.preds.add(rewritten);
      }
    }
  }
}

Subdomains

Frequently Asked Questions

What does pruneUnusedLabelsHIR() do?
pruneUnusedLabelsHIR() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/PruneUnusedLabelsHIR.ts.
Where is pruneUnusedLabelsHIR() defined?
pruneUnusedLabelsHIR() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/PruneUnusedLabelsHIR.ts at line 11.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free