Skip to content

Conversation

@randomPoison
Copy link
Contributor

@randomPoison randomPoison commented Jan 21, 2026

This PR reworks the relooper algorithm to replace most usages of current_block with labeled blocks/breaks. Addresses #330.

The bulk of the changes are in relooper.rs and structures.rs:

  • In relooper.rs the core logic is in the relooper function, which takes the unstructured CFG and processes it into the "structured CFG".
  • In structures.rs we have the logic that takes the structured CFG and turns it into the "strucured AST", which is an intermediate AST that just represents the structured control flow we're translating.
    • I've replaced structured_cfg_help with a new process_cfg function that is responsible for generating the labeled blocks/breaks.
    • I've also added a secondary cleanup pass cleanup_labels that removes redundant labels and extra labeled blocks. This allows the logic in process_cfg to be simpler by conservatively labeling all blocks and breaks.

Don't try to review this commit-by-commit, the commit history is NOT clean and shows all of the experimentation and iteration I went through to get to this point. I only have all the commit history intact because I haven't had a need to squash yet, and we should squash this when merging to avoid polluting the commit history.

I'd also recommend not worrying too much about the diff here, and instead just review the new code as if it were all entirely fresh. Many of the parts here have been heavily reworked, and since I'm the only person deeply familiar with what the code was doing before it probably doesn't make sense to worry about how the new code differs from the old version.

NOTE: I still need to do a cleanup pass on the unit tests, since a lot of those were slapped together during experimentation. Snapshot tests are all clean, but I may want to add more as I cleanup the unit tests.

randomPoison and others added 30 commits January 21, 2026 11:20
- Try to create a loop if there's a single entry.
- Attempt to create a multiple if there are any handled entries/blocks.
- Fallback to a loop only if we can't create a multiple.

This brings us more in line with the original paper as well, and already fixes the disjoint loops case.

Also make a bunch of not-strictly-necessary changes to make things more organized and easier to work with. These should be split out into a separate PR before the functional refactoring.
Because apparently I have a different compiler on my laptop and it's mad about the missing statements, even though on my computer at home it works lolsob
Also differentiate between breaks and continues when relooping.
... by not generating a break if there's only one entry following a simple shape.
And also make the json output pretty.

This comment was marked as resolved.

@randomPoison
Copy link
Contributor Author

Oh joy, we have a subtle difference in behavior between platforms. Looks like the CFG labels that we generate on macos are slightly different than the labels we generate on Ubuntu, likely because the compiler we're using is different and is generating a slightly different CFG.

@randomPoison
Copy link
Contributor Author

Okay the tests should be clean and ready for review as well.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants