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

[ClangIR][CIRGen] Introduce CaseOp and refactor SwitchOp #1006

Merged
merged 2 commits into from
Oct 29, 2024

Conversation

ChuanqiXu9
Copy link
Member

@ChuanqiXu9 ChuanqiXu9 commented Oct 25, 2024

Close #522

This solves the issue we can't handle case in nested scopes and we can't handle if the switch body is not a compound statement.

The core idea of the patch is to introduce the cir.case operation to the language. Then we can get the cases by traversing the body of the cir.switch operation easily instead of counting the regions and the attributes.

Every cir.case operation has a region and now the cir.switch has only one region too. But to make the analysis and optimizations easier, I add a new concept simple form here. That a simple cir.switch operation is: all the cir.case operation owned by the cir.switch lives in the top level blocks of the cir.switch region and there is no other operations except the ending cir.yield. This solves the previous simplified for common-case vs general solution discussion in #522. After implemented this, I feel the correct answer to it is, we want a general solution for constructing and lowering the operations but we like simple and common case for analysis and optimizations. We just mixed the different phases.

For other semantics, see CIROps.td.

For lowering, we can make it generally by lower the cases one by one and finally lower the switch itself.

Although this patch has 1000+ lines of changes, I feel it is relatively neat especially it erases some odd behaviors before.

Tested with Spec2017's C benchmarks except 500.perlbench_r.

Copy link

github-actions bot commented Oct 25, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@bcardosolopes
Copy link
Member

Great to see improvements in this area, thanks for working on this. I'm gonna take a closer look on Monday but I like the overall direction from your description! By the way, seems like there's a formatting issue.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

I'm super happy with the result of this PR, thanks for putting the time to improve this. Modeling cases as op's make way more sense, way to go. I have comments all over but they are mostly cosmetic.

@bcardosolopes bcardosolopes changed the title [clangir] Introduce CaseOp and refactoring SwitchOp [ClangIR][CIRGen] Introduce CaseOp and refactor SwitchOp Oct 28, 2024
@ChuanqiXu9 ChuanqiXu9 force-pushed the refacroting_switch_op branch from 36e4d28 to 95614bc Compare October 29, 2024 02:36
@ChuanqiXu9
Copy link
Member Author

The format check failed still. What is the version of clang-format in the CI? I am using the same clang-format just built in the same true of CIR.

@smeenai
Copy link
Collaborator

smeenai commented Oct 29, 2024

@ChuanqiXu9
Copy link
Member Author

Per https://github.com/llvm/clangir/actions/runs/11566254518/workflow?pr=1006#L63, seems like clang-format 18.0.7.

Thanks. I don't have clang18 so I tried to format it manually this time.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

Awesome, thanks again for working on this!

@bcardosolopes bcardosolopes merged commit 8f54bcd into llvm:main Oct 29, 2024
6 checks passed
@smeenai
Copy link
Collaborator

smeenai commented Oct 29, 2024

I updated the docs: https://llvm.github.io/clangir/Dialect/ops.html#cirswitch-cirswitchop. We can also hopefully close out all our switch-related issues now after verifying them :)

lanza pushed a commit that referenced this pull request Nov 5, 2024
Close #522

This solves the issue we can't handle `case` in nested scopes and we
can't handle if the switch body is not a compound statement.

The core idea of the patch is to introduce the `cir.case` operation to
the language. Then we can get the cases by traversing the body of the
`cir.switch` operation easily instead of counting the regions and the
attributes.

Every `cir.case` operation has a region and now the `cir.switch` has
only one region too. But to make the analysis and optimizations easier,
I add a new concept `simple form` here. That a simple `cir.switch`
operation is: all the `cir.case` operation owned by the `cir.switch`
lives in the top level blocks of the `cir.switch` region and there is no
other operations except the ending `cir.yield`. This solves the previous
`simplified for common-case` vs `general solution` discussion in
#522. After implemented this, I
feel the correct answer to it is, we want a general solution for
constructing and lowering the operations but we like simple and common
case for analysis and optimizations. We just mixed the different phases.

For other semantics, see `CIROps.td`.

For lowering, we can make it generally by lower the cases one by one and
finally lower the switch itself.

Although this patch has 1000+ lines of changes, I feel it is relatively
neat especially it erases some odd behaviors before.

Tested with Spec2017's C benchmarks except 500.perlbench_r.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assertion failure on switch statement with case label in nested block
3 participants