Home / File/ PruneUnusedLabelsHIR.ts — react Source File

PruneUnusedLabelsHIR.ts — react Source File

Architecture documentation for PruneUnusedLabelsHIR.ts, a typescript file in the react codebase. 5 imports, 0 dependents.

File typescript MIRInfrastructure HIR 5 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  957ab73b_3e28_1296_bcda_0a36395cb663["PruneUnusedLabelsHIR.ts"]
  18a78965_f593_105b_e5e8_07001321c2ec["HIR.ts"]
  957ab73b_3e28_1296_bcda_0a36395cb663 --> 18a78965_f593_105b_e5e8_07001321c2ec
  4a73a9b9_07eb_502f_14c1_2f045ba2666c["BlockId"]
  957ab73b_3e28_1296_bcda_0a36395cb663 --> 4a73a9b9_07eb_502f_14c1_2f045ba2666c
  b520037e_e135_2bd9_f501_1fe9a8324ce5["GotoVariant"]
  957ab73b_3e28_1296_bcda_0a36395cb663 --> b520037e_e135_2bd9_f501_1fe9a8324ce5
  9241c5c1_a9a7_17bc_e41c_e967225008dd["HIRFunction"]
  957ab73b_3e28_1296_bcda_0a36395cb663 --> 9241c5c1_a9a7_17bc_e41c_e967225008dd
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  957ab73b_3e28_1296_bcda_0a36395cb663 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  style 957ab73b_3e28_1296_bcda_0a36395cb663 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerError} from '..';
import {BlockId, GotoVariant, HIRFunction} from './HIR';

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.ts do?
PruneUnusedLabelsHIR.ts is a source file in the react codebase, written in typescript. It belongs to the MIRInfrastructure domain, HIR subdomain.
What functions are defined in PruneUnusedLabelsHIR.ts?
PruneUnusedLabelsHIR.ts defines 1 function(s): pruneUnusedLabelsHIR.
What does PruneUnusedLabelsHIR.ts depend on?
PruneUnusedLabelsHIR.ts imports 5 module(s): .., BlockId, GotoVariant, HIR.ts, HIRFunction.
Where is PruneUnusedLabelsHIR.ts in the architecture?
PruneUnusedLabelsHIR.ts is located at compiler/packages/babel-plugin-react-compiler/src/HIR/PruneUnusedLabelsHIR.ts (domain: MIRInfrastructure, subdomain: HIR, directory: compiler/packages/babel-plugin-react-compiler/src/HIR).

Analyze Your Own Codebase

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

Try Supermodel Free