-
Notifications
You must be signed in to change notification settings - Fork 399
[SwitchToIf] support multi-result scf.index_switch + add regression test #9245
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
Merged
cowardsa
merged 2 commits into
llvm:main
from
Bynaryman:improve-switch-to-if-multi-result
Nov 27, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,57 @@ module { | |
| } | ||
| } | ||
|
|
||
| // Switches that yield multiple values should thread every result through the | ||
| // nested if-else chain. | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func.func @multi_result( | ||
| // CHECK-SAME: %[[ARG:.*]]: index) -> (i32, f32) { | ||
| // CHECK: %[[ZERO:.*]] = arith.constant 0 : index | ||
| // CHECK: %[[CMP0:.*]] = arith.cmpi eq, %[[ARG]], %[[ZERO]] : index | ||
| // CHECK: %[[IF0:[0-9]+]]:2 = scf.if %[[CMP0]] -> (i32, f32) { | ||
| // CHECK: %[[C0:.*]] = arith.constant 10 : i32 | ||
| // CHECK: %[[F0:.*]] = arith.constant 1.000000e+00 : f32 | ||
| // CHECK: scf.yield %[[C0]], %[[F0]] : i32, f32 | ||
| // CHECK: } else { | ||
| // CHECK: %[[ONE:.*]] = arith.constant 1 : index | ||
| // CHECK: %[[CMP1:.*]] = arith.cmpi eq, %[[ARG]], %[[ONE]] : index | ||
| // CHECK: %[[IF1:[0-9]+]]:2 = scf.if %[[CMP1]] -> (i32, f32) { | ||
| // CHECK: %[[C1:.*]] = arith.constant 20 : i32 | ||
| // CHECK: %[[F1:.*]] = arith.constant 2.000000e+00 : f32 | ||
| // CHECK: scf.yield %[[C1]], %[[F1]] : i32, f32 | ||
| // CHECK: } else { | ||
| // CHECK: %[[CDEF:.*]] = arith.constant 30 : i32 | ||
| // CHECK: %[[FDEF:.*]] = arith.constant 3.000000e+00 : f32 | ||
| // CHECK: scf.yield %[[CDEF]], %[[FDEF]] : i32, f32 | ||
|
Comment on lines
+80
to
+82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amusing observation - C default -> CDEF (just thought you'd written a string of letters)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realizing that, Constant of the default case in my mind.. |
||
| // CHECK: } | ||
| // CHECK: scf.yield %[[IF1]]#0, %[[IF1]]#1 : i32, f32 | ||
| // CHECK: } | ||
| // CHECK: return %[[IF0]]#0, %[[IF0]]#1 : i32, f32 | ||
| // CHECK: } | ||
| module { | ||
| func.func @multi_result(%arg0 : index) -> (i32, f32) { | ||
| %0, %1 = scf.index_switch %arg0 -> i32, f32 | ||
| case 0 { | ||
| %c0 = arith.constant 10 : i32 | ||
| %f0 = arith.constant 1.0 : f32 | ||
| scf.yield %c0, %f0 : i32, f32 | ||
| } | ||
| case 1 { | ||
| %c1 = arith.constant 20 : i32 | ||
| %f1 = arith.constant 2.0 : f32 | ||
| scf.yield %c1, %f1 : i32, f32 | ||
| } | ||
| default { | ||
| %cd = arith.constant 30 : i32 | ||
| %fd = arith.constant 3.0 : f32 | ||
| scf.yield %cd, %fd : i32, f32 | ||
| } | ||
| return %0, %1 : i32, f32 | ||
| } | ||
| } | ||
|
|
||
| // Switch to nested if-else when the yielded result is empty | ||
|
|
||
| // ----- | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If getArg fails I would have thought this should be a failure? I.e. return failure(); as then the operator is not satisfying its definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I addressed that the simplest possible way @cowardsa