Home / Function/ visitInstruction() — react Function Reference

visitInstruction() — react Function Reference

Architecture documentation for the visitInstruction() function in PromoteUsedTemporaries.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  3b1a75b6_5928_8dea_5f17_cf94a0c97374["visitInstruction()"]
  5e243776_c8a5_6f17_8176_c71b7263aa40["PromoteInterposedTemporaries"]
  3b1a75b6_5928_8dea_5f17_cf94a0c97374 -->|defined in| 5e243776_c8a5_6f17_8176_c71b7263aa40
  af4d3127_0abf_3e44_50a8_d7a9dd0b9b58["visitInstruction()"]
  3b1a75b6_5928_8dea_5f17_cf94a0c97374 -->|calls| af4d3127_0abf_3e44_50a8_d7a9dd0b9b58
  21b1eb1e_eaf5_5238_3a24_f56eb8ef7278["eachInstructionValueLValue()"]
  3b1a75b6_5928_8dea_5f17_cf94a0c97374 -->|calls| 21b1eb1e_eaf5_5238_3a24_f56eb8ef7278
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  3b1a75b6_5928_8dea_5f17_cf94a0c97374 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  f5637d03_fd91_50b8_9da7_b2a24c91bab7["eachPatternOperand()"]
  3b1a75b6_5928_8dea_5f17_cf94a0c97374 -->|calls| f5637d03_fd91_50b8_9da7_b2a24c91bab7
  style 3b1a75b6_5928_8dea_5f17_cf94a0c97374 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PromoteUsedTemporaries.ts lines 285–422

  override visitInstruction(
    instruction: ReactiveInstruction,
    state: InterState,
  ): void {
    for (const lval of eachInstructionValueLValue(instruction.value)) {
      CompilerError.invariant(lval.identifier.name != null, {
        reason:
          'PromoteInterposedTemporaries: Assignment targets not expected to be temporaries',
        loc: instruction.loc,
      });
    }

    switch (instruction.value.kind) {
      case 'CallExpression':
      case 'MethodCall':
      case 'Await':
      case 'PropertyStore':
      case 'PropertyDelete':
      case 'ComputedStore':
      case 'ComputedDelete':
      case 'PostfixUpdate':
      case 'PrefixUpdate':
      case 'StoreLocal':
      case 'StoreContext':
      case 'StoreGlobal':
      case 'Destructure': {
        let constStore = false;

        if (
          (instruction.value.kind === 'StoreContext' ||
            instruction.value.kind === 'StoreLocal') &&
          (instruction.value.lvalue.kind === 'Const' ||
            instruction.value.lvalue.kind === 'HoistedConst')
        ) {
          /*
           * If an identifier is const, we don't need to worry about it
           * being mutated between being loaded and being used
           */
          this.#consts.add(instruction.value.lvalue.place.identifier.id);
          constStore = true;
        }
        if (
          instruction.value.kind === 'Destructure' &&
          (instruction.value.lvalue.kind === 'Const' ||
            instruction.value.lvalue.kind === 'HoistedConst')
        ) {
          [...eachPatternOperand(instruction.value.lvalue.pattern)].forEach(
            ident => this.#consts.add(ident.identifier.id),
          );
          constStore = true;
        }
        if (instruction.value.kind === 'MethodCall') {
          // Treat property of method call as constlike so we don't promote it.
          this.#consts.add(instruction.value.property.identifier.id);
        }

        super.visitInstruction(instruction, state);
        if (
          !constStore &&
          (instruction.lvalue == null ||
            instruction.lvalue.identifier.name != null)
        ) {
          /*
           * If we've stripped the lvalue or promoted the lvalue, then we will emit this instruction
           * as a statement in codegen.
           *
           * If this instruction will be emitted directly as a statement rather than as a temporary
           * during codegen, then it can interpose between the defs and the uses of other temporaries.
           * Since this instruction could potentially mutate those defs, it's not safe to relocate
           * the definition of those temporaries to after this instruction. Mark all those temporaries
           * as needing promotion, but don't promote them until we actually see them being used.
           */
          for (const [key, [ident, _]] of state.entries()) {
            state.set(key, [ident, true]);
          }
        }
        if (instruction.lvalue && instruction.lvalue.identifier.name === null) {
          // Add this instruction's lvalue to the state, initially not marked as needing promotion
          state.set(instruction.lvalue.identifier.id, [
            instruction.lvalue.identifier,
            false,

Domain

Subdomains

Frequently Asked Questions

What does visitInstruction() do?
visitInstruction() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PromoteUsedTemporaries.ts.
Where is visitInstruction() defined?
visitInstruction() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PromoteUsedTemporaries.ts at line 285.
What does visitInstruction() call?
visitInstruction() calls 4 function(s): eachInstructionValueLValue, eachPatternOperand, invariant, visitInstruction.

Analyze Your Own Codebase

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

Try Supermodel Free