Skip to content

Conversation

@elicwhite
Copy link
Member

@elicwhite elicwhite commented Jan 25, 2025

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

Comment on lines 193 to 194
fn.body.blocks.get(branchBlock.terminal.consequent)!.kind = 'block';
fn.body.blocks.get(branchBlock.terminal.alternate)!.kind = 'block';
Copy link
Member

Choose a reason for hiding this comment

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

This assumes that the consequent block doesn't have further nested value blocks "within" it, which would also need to be converted to value blocks. Simplest thing to do is to add a check that the target block ends with a goto to the fallthrough of the outer ternary

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.

3 participants