Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Trying to make ternaries do constant propagation #32222

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Jan 28, 2025
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
Contributor

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

@elicwhite elicwhite marked this pull request as ready for review January 28, 2025 01:03
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