Skip to content

[WIP] Trying to make ternaries do constant propagation - #239

Closed
everettbu wants to merge 7 commits into
mainfrom
constant-propagation-ternary
Closed

everettbu wants to merge 7 commits into
mainfrom
constant-propagation-ternary

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#32222
Original author: elicwhite


Summary

Some basic constant propagation for ternaries.

// input
const x = true ? b : c;

// output
const x = b;

This has a feature flag, currently disabled.

This change, even when the flag is turned off, does double the number of iterations that needs to be taken through the blocks in the file.

The previous logic was to detect all the local values within the current block (storing in constants), and if you get to an if statement, and that test is in your lookup, use it.

However, with ternaries, the test is in a different block that follows the if statement so it's not clear to me if you can do the single for loop with instructions and blocks together.

Now, it iterates through all the blocks and stores the constants, then iterates over the blocks again to try to inline.

This doesn't seem great, I'm curious if there is a better approach.

How did you test this change?

Jest with the flag disabled, only enabled for the single test. When the flag is enabled globally, there are some other cases it fails on that need to be addressed.

Playground: https://react-compiler-playground-git-fork-elicwhit-fcc939-fbopensource.vercel.app

@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Dec 13, 2025
@greptile-apps

greptile-apps Bot commented Dec 13, 2025

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR adds constant propagation for ternary expressions, allowing the compiler to optimize expressions like const x = true ? b : c into const x = b. The feature is gated behind the enableTernaryConstantPropagation flag (defaulting to true).

The implementation splits the constant propagation pass into two phases: first collecting constants across all blocks, then applying optimizations. This enables cross-block propagation needed for ternaries, where the test value is in a different block than the ternary terminal.

Critical Issues:

  • Infinite loop bug in ConstantPropagation.ts:176 where hasChanges = true is set before verifying the optimization actually succeeds
  • Double compilation pass in Pipeline.ts:195-196 runs even when feature is disabled, doubling cost for all functions
  • Typo in Environment.ts:627 comment ("ConstantPropgation")
  • Inconsistent equality operator (== vs ===) in ConstantPropagation.ts:197

Architecture Note:
The PR acknowledges uncertainty about the double-pass approach and questions whether a single-pass solution is possible (see PR description and code comments).

Confidence Score: 1/5

  • This PR has a critical infinite loop bug that will hang the compiler
  • The infinite loop bug at ConstantPropagation.ts:176 is a blocking issue that will cause the compiler to hang when a ternary test is constant but the target block isn't a goto. This must be fixed before merging.
  • Pay close attention to compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts - the infinite loop bug at line 176 must be resolved

Important Files Changed

File Analysis

Filename Score Overview
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts 4/5 adds feature flag enableTernaryConstantPropagation with typo in comment
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts 3/5 runs constant propagation twice to handle ternary optimization, but doubles compilation cost even when feature is disabled
compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts 2/5 adds ternary constant propagation with critical infinite loop bug and inconsistent equality check

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: commented-out code should be removed

Suggested change
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 144:144

Comment:
**style:** commented-out code should be removed

```suggestion
            instr.value.value = identifierValue;
```

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" should be "ConstantPropagation"

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: "ConstantPropgation" should be "ConstantPropagation"

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: this logic only handles the case where chosenBlock?.terminal.kind === 'goto', but doesn't update the terminal if this condition is false. This could leave the ternary terminal unresolved even when the test value is constant, potentially causing incomplete optimization.

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 200:200

Comment:
**logic:** this logic only handles the case where `chosenBlock?.terminal.kind === 'goto'`, but doesn't update the terminal if this condition is false. This could leave the ternary terminal unresolved even when the test value is constant, potentially causing incomplete optimization.

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from fdfa7a6 to cb1fef4 Compare December 13, 2025 01:38

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from cb1fef4 to fdfa7a6 Compare December 13, 2025 01:45

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@@ -121,6 +119,8 @@ function applyConstantPropagation(
}
}

const localReassignments = new Map<IdentifierId, Place>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: localReassignments only tracks reassignments within a single block, but StoreLocal values may depend on values from previous blocks. Since constants are already global across blocks, tracking local reassignments separately may miss cross-block propagation opportunities or create inconsistencies.

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 122:122

Comment:
**style:** `localReassignments` only tracks reassignments within a single block, but `StoreLocal` values may depend on values from previous blocks. Since constants are already global across blocks, tracking local reassignments separately may miss cross-block propagation opportunities or create inconsistencies.

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from fdfa7a6 to cb1fef4 Compare December 13, 2025 02:39

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: using loose equality == instead of strict equality ===

Suggested change
if (fallthrough?.terminal.kind == 'goto') {
if (fallthrough?.terminal.kind === 'goto') {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** using loose equality `==` instead of strict equality `===`

```suggestion
          if (fallthrough?.terminal.kind === 'goto') {
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +181 to +183
// I think I can only set this if the block isn't
// used in a value position by its predecessor
block.kind = 'block';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: setting block.kind = 'block' unconditionally may be incorrect if the block is legitimately a sequence/value block for other reasons

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 181:183

Comment:
**style:** setting `block.kind = 'block'` unconditionally may be incorrect if the block is legitimately a sequence/value block for other reasons

How can I resolve this? If you propose a fix, please make it concise.

* const x = b;
*/
validateNoVoidUseMemo: z.boolean().default(true),
enableTernaryConstantPropagation: z.boolean().default(true),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: flag defaults to true but PR description states "currently disabled"—verify the intended default value

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 635:635

Comment:
**style:** flag defaults to `true` but PR description states "currently disabled"—verify the intended default value

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from cb1fef4 to fdfa7a6 Compare December 13, 2025 02:44

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +193 to +209
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: hasChanges set to true before checking if optimization can be applied, causing infinite loop if chosenBlock?.terminal.kind !== 'goto'

Suggested change
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 193:209

Comment:
**logic:** `hasChanges` set to `true` before checking if optimization can be applied, causing infinite loop if `chosenBlock?.terminal.kind !== 'goto'`

```suggestion
        if (testValue !== null && testValue.kind === 'Primitive') {
          const targetBlockId = testValue.value
            ? branchBlock.terminal.consequent
            : branchBlock.terminal.alternate;

          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
              kind: 'goto',
              variant: GotoVariant.Break,
              block: targetBlockId,
              id: terminal.id,
              loc: terminal.loc,
            };
          }
        }
```

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from fdfa7a6 to cb1fef4 Compare December 13, 2025 03:30

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: inconsistent equality operator - use === instead of == to match lines 206 and 210

Suggested change
if (fallthrough?.terminal.kind == 'goto') {
if (fallthrough?.terminal.kind === 'goto') {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** inconsistent equality operator - use `===` instead of `==` to match lines 206 and 210

```suggestion
        if (fallthrough?.terminal.kind === 'goto') {
```

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from cb1fef4 to fdfa7a6 Compare December 13, 2025 03:38

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +199 to +208
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: when chosenBlock?.terminal.kind !== 'goto', the block terminal remains a ternary, but hasChanges = true on line 194 causes an infinite loop since the fixpoint iteration never terminates

Suggested change
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 199:208

Comment:
**logic:** when `chosenBlock?.terminal.kind !== 'goto'`, the block terminal remains a ternary, but `hasChanges = true` on line 194 causes an infinite loop since the fixpoint iteration never terminates

```suggestion
          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
              kind: 'goto',
              variant: GotoVariant.Break,
              block: targetBlockId,
              id: terminal.id,
              loc: terminal.loc,
            };
          }
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +152
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: localReassignments only tracks within a single block, but the ternary optimization needs constants from previous blocks. Since you're iterating all blocks to collect constants (lines 109-155), consider tracking StoreLocal propagation in the global constants map instead of a local map that gets discarded after each block

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:152

Comment:
**style:** `localReassignments` only tracks within a single block, but the ternary optimization needs constants from previous blocks. Since you're iterating all blocks to collect constants (lines 109-155), consider tracking `StoreLocal` propagation in the global `constants` map instead of a local map that gets discarded after each block

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +193 to +202
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles the cost. Consider a single-pass approach where ternary optimization is integrated into the main constant propagation loop, or use a flag to skip the first pass when ternary propagation is disabled

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 193:202

Comment:
**style:** running constant propagation twice doubles the cost. Consider a single-pass approach where ternary optimization is integrated into the main constant propagation loop, or use a flag to skip the first pass when ternary propagation is disabled

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from fdfa7a6 to cb1fef4 Compare December 13, 2025 03:53

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from cb1fef4 to fdfa7a6 Compare December 13, 2025 04:00

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +194 to +208
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: hasChanges = true on line 194 occurs before verifying optimization is possible on line 200. When chosenBlock?.terminal.kind !== 'goto', the ternary terminal remains unchanged but hasChanges = true triggers infinite loop in the fixpoint iteration.

Suggested change
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:208

Comment:
**logic:** `hasChanges = true` on line 194 occurs before verifying optimization is possible on line 200. When `chosenBlock?.terminal.kind !== 'goto'`, the ternary terminal remains unchanged but `hasChanges = true` triggers infinite loop in the fixpoint iteration.

```suggestion
          const testValue = read(constants, branchBlock.terminal.test);
          if (testValue !== null && testValue.kind === 'Primitive') {
            const targetBlockId = testValue.value
              ? branchBlock.terminal.consequent
              : branchBlock.terminal.alternate;

            const chosenBlock = fn.body.blocks.get(targetBlockId);
            if (chosenBlock?.terminal.kind === 'goto') {
              hasChanges = true;
              block.terminal = {
                kind: 'goto',
                variant: GotoVariant.Break,
                block: targetBlockId,
                id: terminal.id,
                loc: terminal.loc,
              };
            }
          }
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +199 to +208
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: When chosenBlock?.terminal.kind !== 'goto', the ternary isn't resolved but remains in the HIR. Since the ternary test value is constant but the target block isn't a simple goto, the ternary block stays unoptimized. Consider whether you should either transform the block differently or continue with the unoptimized ternary

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 199:208

Comment:
**style:** When `chosenBlock?.terminal.kind !== 'goto'`, the ternary isn't resolved but remains in the HIR. Since the ternary test value is constant but the target block isn't a simple goto, the ternary block stays unoptimized. Consider whether you should either transform the block differently or continue with the unoptimized ternary

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +152
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: localReassignments only tracks within a single block but is discarded after each block iteration. Since ternary optimization needs constants from previous blocks (which are in the global constants map), verify this local tracking integrates correctly with the cross-block ternary logic

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:152

Comment:
**style:** `localReassignments` only tracks within a single block but is discarded after each block iteration. Since ternary optimization needs constants from previous blocks (which are in the global `constants` map), verify this local tracking integrates correctly with the cross-block ternary logic

How can I resolve this? If you propose a fix, please make it concise.

@everettbu
everettbu force-pushed the constant-propagation-ternary branch from fdfa7a6 to cb1fef4 Compare December 13, 2025 04:12

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 6 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +194 to +208
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true set on line 194 before verifying the terminal actually gets updated on line 201. when chosenBlock?.terminal.kind !== 'goto' (line 200 check fails), the ternary terminal stays unchanged but hasChanges = true causes the fixpoint loop at line 63 to never terminate

move line 194 inside the if block after line 207:

Suggested change
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:208

Comment:
**logic:** infinite loop: `hasChanges = true` set on line 194 before verifying the terminal actually gets updated on line 201. when `chosenBlock?.terminal.kind !== 'goto'` (line 200 check fails), the ternary terminal stays unchanged but `hasChanges = true` causes the fixpoint loop at line 63 to never terminate

move line 194 inside the if block after line 207:

```suggestion
        const testValue = read(constants, branchBlock.terminal.test);
        if (testValue !== null && testValue.kind === 'Primitive') {
          const targetBlockId = testValue.value
            ? branchBlock.terminal.consequent
            : branchBlock.terminal.alternate;

          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
              kind: 'goto',
              variant: GotoVariant.Break,
              block: targetBlockId,
              id: terminal.id,
              loc: terminal.loc,
            };
          }
        }
```

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +153
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break statement after StoreLocal case - if additional cases added later, this will fall through

Suggested change
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}
localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}
localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
break;
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:153

Comment:
**syntax:** missing `break` statement after `StoreLocal` case - if additional cases added later, this will fall through

```suggestion
      switch (instr.value.kind) {
        case 'StoreLocal': {
          const identifierValue = localReassignments.get(
            instr.value.value.identifier.id,
          );
          if (identifierValue != null) {
            // constants.set(value.lvalue.place.identifier.id, placeValue);
            instr.value.value = identifierValue;
          }

          localReassignments.set(
            instr.value.lvalue.place.identifier.id,
            instr.value.value,
          );
          break;
        }
      }
```

How can I resolve this? If you propose a fix, please make it concise.

const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use !== instead of != for consistency with strict equality checks elsewhere in the codebase

Suggested change
if (identifierValue != null) {
if (identifierValue !== null) {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 143:143

Comment:
**style:** use `!==` instead of `!=` for consistency with strict equality checks elsewhere in the codebase

```suggestion
          if (identifierValue !== null) {
```

How can I resolve this? If you propose a fix, please make it concise.

instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: remove commented-out code

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 144:144

Comment:
**style:** remove commented-out code

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +193 to +202
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 193:202

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 627:627

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use === instead of == for consistency with lines 206 and 210

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** use `===` instead of `==` for consistency with lines 206 and 210

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +181 to +183
// I think I can only set this if the block isn't
// used in a value position by its predecessor
block.kind = 'block';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: comment expresses uncertainty but code sets block.kind = 'block' unconditionally - verify this is correct for all ternary blocks or make it conditional

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 181:183

Comment:
**style:** comment expresses uncertainty but code sets `block.kind = 'block'` unconditionally - verify this is correct for all ternary blocks or make it conditional

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +192 to +201
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 192:201

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true set before verifying terminal actually gets modified. when chosenBlock?.terminal.kind !== 'goto' check fails on line 200, the ternary terminal stays unchanged but the fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set hasChanges when optimization succeeds:

Suggested change
hasChanges = true;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` set before verifying terminal actually gets modified. when `chosenBlock?.terminal.kind !== 'goto'` check fails on line 200, the ternary terminal stays unchanged but the fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set `hasChanges` when optimization succeeds:

```suggestion
          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +153
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break statement after StoreLocal case - if additional cases are added later, this will fall through

Suggested change
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}
localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}
localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
break;
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:153

Comment:
**syntax:** missing `break` statement after `StoreLocal` case - if additional cases are added later, this will fall through

```suggestion
      switch (instr.value.kind) {
        case 'StoreLocal': {
          const identifierValue = localReassignments.get(
            instr.value.value.identifier.id,
          );
          if (identifierValue != null) {
            // constants.set(value.lvalue.place.identifier.id, placeValue);
            instr.value.value = identifierValue;
          }

          localReassignments.set(
            instr.value.lvalue.place.identifier.id,
            instr.value.value,
          );
          break;
        }
      }
```

How can I resolve this? If you propose a fix, please make it concise.

@@ -671,56 +626,208 @@ export const EnvironmentConfigSchema = z.object({
lowerContextAccess: ExternalFunctionSchema.nullable().default(null),

/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Suggested change
/**
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 628:628

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +193 to +202
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when enableTernaryConstantPropagation is false

consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 193:202

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`

consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use !== instead of != for consistency with strict equality checks elsewhere in the codebase

Suggested change
if (identifierValue != null) {
if (identifierValue !== null) {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 143:143

Comment:
**style:** use `!==` instead of `!=` for consistency with strict equality checks elsewhere in the codebase

```suggestion
          if (identifierValue !== null) {
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 627:627

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: inconsistent equality operator - use === instead of == to match lines 206 and 210

Suggested change
if (fallthrough?.terminal.kind == 'goto') {
if (fallthrough?.terminal.kind === 'goto') {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** inconsistent equality operator - use `===` instead of `==` to match lines 206 and 210

```suggestion
          if (fallthrough?.terminal.kind === 'goto') {
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +192 to +201
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 192:201

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +181 to +183
// I think I can only set this if the block isn't
// used in a value position by its predecessor
block.kind = 'block';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: comment expresses uncertainty but code sets block.kind = 'block' unconditionally - verify this is correct for all ternary blocks or make it conditional based on whether the block is used in a value position by its predecessor

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 181:183

Comment:
**style:** comment expresses uncertainty but code sets `block.kind = 'block'` unconditionally - verify this is correct for all ternary blocks or make it conditional based on whether the block is used in a value position by its predecessor

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true set before verifying terminal actually gets modified. when chosenBlock?.terminal.kind !== 'goto' check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` set before verifying terminal actually gets modified. when `chosenBlock?.terminal.kind !== 'goto'` check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +152
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break - if additional cases added later, will fall through

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:152

Comment:
**syntax:** missing `break` - if additional cases added later, will fall through

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true before verifying terminal gets updated. when chosenBlock?.terminal.kind !== 'goto' check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` before verifying terminal gets updated. when `chosenBlock?.terminal.kind !== 'goto'` check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +194 to +208
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: setting hasChanges = true on line 194 before verifying the terminal transformation succeeds causes infinite loop. when the check chosenBlock?.terminal.kind !== 'goto' fails on line 200, the ternary terminal remains unchanged but the fixpoint loop at line 63 never terminates.

Suggested change
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:208

Comment:
**logic:** setting `hasChanges = true` on line 194 before verifying the terminal transformation succeeds causes infinite loop. when the check `chosenBlock?.terminal.kind !== 'goto'` fails on line 200, the ternary terminal remains unchanged but the fixpoint loop at line 63 never terminates.

```suggestion
          const testValue = read(constants, branchBlock.terminal.test);
          if (testValue !== null && testValue.kind === 'Primitive') {
            const targetBlockId = testValue.value
              ? branchBlock.terminal.consequent
              : branchBlock.terminal.alternate;

            const chosenBlock = fn.body.blocks.get(targetBlockId);
            if (chosenBlock?.terminal.kind === 'goto') {
              hasChanges = true;
              block.terminal = {
                kind: 'goto',
                variant: GotoVariant.Break,
                block: targetBlockId,
                id: terminal.id,
                loc: terminal.loc,
              };
            }
          }
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true set before verifying terminal actually gets modified. when chosenBlock?.terminal.kind !== 'goto' check fails on line 200, the ternary terminal stays unchanged but the fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` set before verifying terminal actually gets modified. when `chosenBlock?.terminal.kind !== 'goto'` check fails on line 200, the ternary terminal stays unchanged but the fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;
const targetBlockId = testValue.value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use === instead of == for consistency with lines 206 and 210

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 195:195

Comment:
**style:** use `===` instead of `==` for consistency with lines 206 and 210

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines 196 to +201
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass still executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 196:201

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass still executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +153
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break statement after StoreLocal case - if additional cases are added later, this will fall through

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:153

Comment:
**syntax:** missing `break` statement after `StoreLocal` case - if additional cases are added later, this will fall through

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: setting hasChanges = true before verifying the terminal actually gets modified causes the fixpoint loop at line 63 to run forever when the optimization can't be applied. when chosenBlock?.terminal.kind !== 'goto' (line 200 check fails), the ternary terminal remains unchanged but the loop never terminates.

move this line inside the if block after line 207 to only set hasChanges when the optimization actually succeeds:

Suggested change
hasChanges = true;
const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: setting `hasChanges = true` before verifying the terminal actually gets modified causes the fixpoint loop at line 63 to run forever when the optimization can't be applied. when `chosenBlock?.terminal.kind !== 'goto'` (line 200 check fails), the ternary terminal remains unchanged but the loop never terminates.

move this line inside the if block after line 207 to only set `hasChanges` when the optimization actually succeeds:

```suggestion
          const testValue = read(constants, branchBlock.terminal.test);
        if (testValue !== null && testValue.kind === 'Primitive') {
          const targetBlockId = testValue.value
            ? branchBlock.terminal.consequent
            : branchBlock.terminal.alternate;

          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: inconsistent equality operator - use === instead of == to match lines 206 and 210

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** inconsistent equality operator - use `===` instead of `==` to match lines 206 and 210

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 627:627

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true before verifying terminal gets updated. when chosenBlock?.terminal.kind !== 'goto' check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` before verifying terminal gets updated. when `chosenBlock?.terminal.kind !== 'goto'` check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

How can I resolve this? If you propose a fix, please make it concise.

instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break - if additional cases added later, will fall through

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 152:152

Comment:
**syntax:** missing `break` - if additional cases added later, will fall through

How can I resolve this? If you propose a fix, please make it concise.

instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: remove commented-out code

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 144:144

Comment:
**style:** remove commented-out code

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +194 to +208
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true on line 194 before verifying terminal actually gets updated on line 201. when chosenBlock?.terminal.kind !== 'goto' (line 200 check fails), the ternary terminal stays unchanged but hasChanges = true causes the fixpoint loop at line 63 to never terminate.

Suggested change
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:208

Comment:
**logic:** infinite loop: `hasChanges = true` on line 194 before verifying terminal actually gets updated on line 201. when `chosenBlock?.terminal.kind !== 'goto'` (line 200 check fails), the ternary terminal stays unchanged but `hasChanges = true` causes the fixpoint loop at line 63 to never terminate.

```suggestion
          const testValue = read(constants, branchBlock.terminal.test);
          if (testValue !== null && testValue.kind === 'Primitive') {
            const targetBlockId = testValue.value
              ? branchBlock.terminal.consequent
              : branchBlock.terminal.alternate;

            const chosenBlock = fn.body.blocks.get(targetBlockId);
            if (chosenBlock?.terminal.kind === 'goto') {
              hasChanges = true;
              block.terminal = {
                kind: 'goto',
                variant: GotoVariant.Break,
                block: targetBlockId,
                id: terminal.id,
                loc: terminal.loc,
              };
            }
          }
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +194 to +208
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;

const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: setting hasChanges = true on line 194 before verifying the terminal actually gets updated causes the fixpoint loop to never terminate when the optimization can't be applied. when the check chosenBlock?.terminal.kind !== 'goto' fails on line 200, the ternary terminal remains unchanged but hasChanges = true causes the loop at line 63 to run forever.

move hasChanges = true to after line 207, inside the if block:

Suggested change
hasChanges = true;
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
const targetBlockId = testValue.value
? branchBlock.terminal.consequent
: branchBlock.terminal.alternate;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
kind: 'goto',
variant: GotoVariant.Break,
block: targetBlockId,
id: terminal.id,
loc: terminal.loc,
};
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:208

Comment:
**logic:** infinite loop: setting `hasChanges = true` on line 194 before verifying the terminal actually gets updated causes the fixpoint loop to never terminate when the optimization can't be applied. when the check `chosenBlock?.terminal.kind !== 'goto'` fails on line 200, the ternary terminal remains unchanged but `hasChanges = true` causes the loop at line 63 to run forever.

move `hasChanges = true` to after line 207, inside the if block:

```suggestion
          const targetBlockId = testValue.value
            ? branchBlock.terminal.consequent
            : branchBlock.terminal.alternate;

          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
              kind: 'goto',
              variant: GotoVariant.Break,
              block: targetBlockId,
              id: terminal.id,
              loc: terminal.loc,
            };
          }
```

How can I resolve this? If you propose a fix, please make it concise.

@@ -671,56 +626,208 @@ export const EnvironmentConfigSchema = z.object({
lowerContextAccess: ExternalFunctionSchema.nullable().default(null),

/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
/**
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 628:628

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +153
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break statement after StoreLocal case - will fall through if additional cases are added later

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:153

Comment:
**syntax:** missing `break` statement after `StoreLocal` case - will fall through if additional cases are added later

How can I resolve this? If you propose a fix, please make it concise.

const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;
const targetBlockId = testValue.value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use === instead of == for consistency with strict equality checks on lines 206 and 210

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 195:195

Comment:
**style:** use `===` instead of `==` for consistency with strict equality checks on lines 206 and 210

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

How can I resolve this? If you propose a fix, please make it concise.

instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break - if additional cases added later, will fall through

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 152:152

Comment:
**syntax:** missing `break` - if additional cases added later, will fall through

How can I resolve this? If you propose a fix, please make it concise.


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true set before verifying terminal gets modified. when chosenBlock?.terminal.kind !== 'goto' check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` set before verifying terminal gets modified. when `chosenBlock?.terminal.kind !== 'goto'` check fails (line 200), ternary stays unchanged but fixpoint loop at line 63 never terminates

move this line inside the if block after line 207 to only set when optimization succeeds

How can I resolve this? If you propose a fix, please make it concise.

instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: commented-out code

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 144:144

Comment:
**style:** commented-out code

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +193 to +202
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 193:202

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 196-197) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile


const testValue = read(constants, branchBlock.terminal.test);
if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: hasChanges = true before verifying terminal gets modified. when chosenBlock?.terminal.kind !== 'goto' check fails on line 200, ternary stays unchanged but fixpoint loop at line 63 never terminates

move this inside the if block after line 207:

Suggested change
hasChanges = true;
const chosenBlock = fn.body.blocks.get(targetBlockId);
if (chosenBlock?.terminal.kind === 'goto') {
hasChanges = true;
block.terminal = {
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 194:194

Comment:
**logic:** infinite loop: `hasChanges = true` before verifying terminal gets modified. when `chosenBlock?.terminal.kind !== 'goto'` check fails on line 200, ternary stays unchanged but fixpoint loop at line 63 never terminates

move this inside the if block after line 207:

```suggestion
          const chosenBlock = fn.body.blocks.get(targetBlockId);
          if (chosenBlock?.terminal.kind === 'goto') {
            hasChanges = true;
            block.terminal = {
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +138 to +152
switch (instr.value.kind) {
case 'StoreLocal': {
const identifierValue = localReassignments.get(
instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);
instr.value.value = identifierValue;
}

localReassignments.set(
instr.value.lvalue.place.identifier.id,
instr.value.value,
);
}

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: missing break - if additional cases added later, will fall through

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 138:152

Comment:
**syntax:** missing `break` - if additional cases added later, will fall through

How can I resolve this? If you propose a fix, please make it concise.

instr.value.value.identifier.id,
);
if (identifierValue != null) {
// constants.set(value.lvalue.place.identifier.id, placeValue);

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: commented-out code

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 144:144

Comment:
**style:** commented-out code

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: missing 'a' in "ConstantPropgation"

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 629:629

Comment:
**syntax:** typo: missing 'a' in "ConstantPropgation"

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +193 to +202
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass still executes the full fixpoint loop even when enableTernaryConstantPropagation is false. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 193:202

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass still executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`. consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.

ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 5 comments

Edit Code Review Agent Settings | Greptile

const testValue = read(constants, branchBlock.terminal.test);

if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: infinite loop: setting hasChanges = true before verifying the terminal actually gets modified causes the fixpoint loop at line 63 to run forever when the optimization can't be applied

when the ternary test value is constant but the subsequent checks fail (e.g., branch block not found, terminal not a branch, etc.), the ternary terminal remains unchanged but hasChanges = true causes infinite iteration

move this line after all checks pass and the terminal is successfully updated (after line 191)

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 176:176

Comment:
**logic:** infinite loop: setting `hasChanges = true` before verifying the terminal actually gets modified causes the fixpoint loop at line 63 to run forever when the optimization can't be applied

when the ternary test value is constant but the subsequent checks fail (e.g., branch block not found, terminal not a branch, etc.), the ternary terminal remains unchanged but `hasChanges = true` causes infinite iteration

move this line after all checks pass and the terminal is successfully updated (after line 191)

How can I resolve this? If you propose a fix, please make it concise.

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: use === instead of == for consistency with lines 206 and 210

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** use `===` instead of `==` for consistency with lines 206 and 210

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +192 to +201
const wasTernaryConstantPropagationEnabled =
env.config.enableTernaryConstantPropagation;
env.config.enableTernaryConstantPropagation = false;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

env.config.enableTernaryConstantPropagation =
wasTernaryConstantPropagationEnabled;
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagationTernary', value: hir});

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when enableTernaryConstantPropagation is false

consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 192:201

Comment:
**style:** running constant propagation twice doubles compilation cost for every function. the first pass (lines 195-196) executes the full fixpoint loop even when `enableTernaryConstantPropagation` is `false`

consider integrating ternary optimization into a single pass or refactoring to avoid redundant work

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 627:627

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +181 to +183
// I think I can only set this if the block isn't
// used in a value position by its predecessor
block.kind = 'block';

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: comment expresses uncertainty but code sets block.kind = 'block' unconditionally

verify this is correct for all ternary blocks or make it conditional based on whether the block is used in a value position by its predecessor

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 181:183

Comment:
**style:** comment expresses uncertainty but code sets `block.kind = 'block'` unconditionally

verify this is correct for all ternary blocks or make it conditional based on whether the block is used in a value position by its predecessor

How can I resolve this? If you propose a fix, please make it concise.

ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9 files reviewed, 4 comments

Edit Code Review Agent Settings | Greptile

const testValue = read(constants, branchBlock.terminal.test);

if (testValue !== null && testValue.kind === 'Primitive') {
hasChanges = true;

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: hasChanges = true set before verifying optimization succeeds. when the chosen block isn't a goto (line 206-207 checks), terminal stays unchanged but fixpoint loop at line 63 runs infinitely.

move after line 212 inside the transformation logic

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 176:176

Comment:
**logic:** `hasChanges = true` set before verifying optimization succeeds. when the chosen block isn't a goto (line 206-207 checks), terminal stays unchanged but fixpoint loop at line 63 runs infinitely.

move after line 212 inside the transformation logic

How can I resolve this? If you propose a fix, please make it concise.

branchBlock.terminal.fallthrough,
);

if (fallthrough?.terminal.kind == 'goto') {

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: loose equality == instead of strict === (inconsistent with lines 206 and 210)

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Optimization/ConstantPropagation.ts
Line: 197:197

Comment:
**style:** loose equality `==` instead of strict `===` (inconsistent with lines 206 and 210)

How can I resolve this? If you propose a fix, please make it concise.


/**
* If enabled, will validate useMemos that don't return any values:
* If enabled, ConstantPropgation will try to resolve ternaries.

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: "ConstantPropgation" missing 'a'

Suggested change
* If enabled, ConstantPropgation will try to resolve ternaries.
* If enabled, ConstantPropagation will try to resolve ternaries.
Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
Line: 627:627

Comment:
**syntax:** typo: "ConstantPropgation" missing 'a'

```suggestion
   * If enabled, ConstantPropagation will try to resolve ternaries.
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines 195 to 196
constantPropagation(hir);
log({kind: 'hir', name: 'ConstantPropagation', value: hir});

ghost Dec 15, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: first pass runs full fixpoint loop even when enableTernaryConstantPropagation is false, doubling compilation cost for every function

Prompt To Fix With AI
This is a comment left during a code review.
Path: compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts
Line: 195:196

Comment:
**style:** first pass runs full fixpoint loop even when `enableTernaryConstantPropagation` is `false`, doubling compilation cost for every function

How can I resolve this? If you propose a fix, please make it concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants