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

feat: initial draft of control flow lifting transform #399

Draft
wants to merge 18 commits into
base: next
Choose a base branch
from

Conversation

bitwalker
Copy link
Contributor

This PR implements a generic control flow lifting transformation framework, and an implementation of it for the HIR dialect.

The tranform converts unstructured control flow operations to structured control flow ops, e.g. hir.cond_br to hir.if. This includes complex control flow such as loops with loop-carried variables, reduce-style loops, etc.

Not all control flow is guaranteed to be lifted however. Currently, the primary limitation is that all return-like operations in a region must be equivalent (i.e. the same operation type), otherwise some control flow will be left unstructured.

For now, on the backend, this will result in a legalization error, as we do not have lowerings for unstructured control flow ops at this time. In the future we plan to lower directly to structured control flow, so this is more of a temporary solution for us anyway; but may also be useful for other compiler frontends.


@greenhat This is marked as a draft until I've finished testing and working out any kinks. I'll add the commit with the pipeline once that is ready. Fair warning, a lot of the code here is pretty rough, as it is pretty directly translated from C++ and I didn't have much time to spend re-architecting it to be as Rust-y as I'd like. Since long term we aim to lower directly to structured control flow anyway, I don't actually care all that much for now, but we may have to come back and clean this up at some point.

@bitwalker bitwalker added codegen blocker This issue is one of our top priorities labels Feb 14, 2025
@bitwalker bitwalker added this to the Beta 2 milestone Feb 14, 2025
@bitwalker bitwalker requested a review from greenhat February 14, 2025 04:27
@bitwalker bitwalker self-assigned this Feb 14, 2025
Copy link
Contributor

@greenhat greenhat left a comment

Choose a reason for hiding this comment

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

LGTM!

@bitwalker bitwalker force-pushed the bitwalker/cfg-to-scf branch from eacba9f to 739adc5 Compare February 17, 2025 19:52
This commit implements a generic control flow lifting transformation
framework, and an implementation of it for the HIR dialect.

The tranform converts unstructured control flow operations to structured
control flow ops, e.g. `hir.cond_br` to `hir.if`. This includes complex
control flow such as loops with loop-carried variables, reduce-style
loops, etc.

Not all control flow is guaranteed to be lifted however. Currently, the
primary limitation is that all return-like operations in a region must
be equivalent (i.e. the same operation type), otherwise some control
flow will be left unstructured.

For now, on the backend, this will result in a legalization error, as we
do not have lowerings for unstructured control flow ops at this time. In
the future we plan to lower directly to structured control flow, so
this is more of a temporary solution for us anyway; but may also be
useful for other compiler frontends.
This fixes an issue with the pass manager, which expects the
fully-qualified name of an op.
@bitwalker bitwalker force-pushed the bitwalker/cfg-to-scf branch from 739adc5 to 23a8f4c Compare February 17, 2025 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker This issue is one of our top priorities codegen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants