-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[CIR] Upstream initial support for switch statements #137106
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
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1f56e1
Add initial CIR support for switch operation
Andres-Salamanca b5d56d5
Add codegen for switch and EqualCase kind
Andres-Salamanca 08ad256
Add early return after NYI to prevent crashes
Andres-Salamanca 6e1e7c3
Fixed format, addressed reviews, and added more tests for better cove…
Andres-Salamanca a8741c5
Remove empty verifier
Andres-Salamanca ad4fd87
Rename tests to match with new mangled names
Andres-Salamanca 7d5a53f
Add more switch tests and mark unsupported constructs as TODO
Andres-Salamanca d7dbe0c
Apply new code review suggestions
Andres-Salamanca 25aabcd
Grammar corrections
Andres-Salamanca 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 | ||||
---|---|---|---|---|---|---|
|
@@ -470,7 +470,8 @@ def StoreOp : CIR_Op<"store", [ | |||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
def ReturnOp : CIR_Op<"return", [ParentOneOf<["FuncOp", "ScopeOp", "IfOp", | ||||||
"DoWhileOp", "WhileOp", "ForOp"]>, | ||||||
"SwitchOp", "DoWhileOp","WhileOp", | ||||||
"ForOp", "CaseOp"]>, | ||||||
Terminator]> { | ||||||
let summary = "Return from function"; | ||||||
let description = [{ | ||||||
|
@@ -609,8 +610,9 @@ def ConditionOp : CIR_Op<"condition", [ | |||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
def YieldOp : CIR_Op<"yield", [ReturnLike, Terminator, | ||||||
ParentOneOf<["IfOp", "ScopeOp", "WhileOp", | ||||||
"ForOp", "DoWhileOp"]>]> { | ||||||
ParentOneOf<["IfOp", "ScopeOp", "SwitchOp", | ||||||
"WhileOp", "ForOp", "CaseOp", | ||||||
"DoWhileOp"]>]> { | ||||||
let summary = "Represents the default branching behaviour of a region"; | ||||||
let description = [{ | ||||||
The `cir.yield` operation terminates regions on different CIR operations, | ||||||
|
@@ -753,6 +755,220 @@ def ScopeOp : CIR_Op<"scope", [ | |||||
]; | ||||||
} | ||||||
|
||||||
//===----------------------------------------------------------------------===// | ||||||
// SwitchOp | ||||||
//===----------------------------------------------------------------------===// | ||||||
|
||||||
def CaseOpKind_DT : I32EnumAttrCase<"Default", 1, "default">; | ||||||
def CaseOpKind_EQ : I32EnumAttrCase<"Equal", 2, "equal">; | ||||||
def CaseOpKind_AO : I32EnumAttrCase<"Anyof", 3, "anyof">; | ||||||
def CaseOpKind_RG : I32EnumAttrCase<"Range", 4, "range">; | ||||||
|
||||||
def CaseOpKind : I32EnumAttr< | ||||||
"CaseOpKind", | ||||||
"case kind", | ||||||
[CaseOpKind_DT, CaseOpKind_EQ, CaseOpKind_AO, CaseOpKind_RG]> { | ||||||
let cppNamespace = "::cir"; | ||||||
} | ||||||
|
||||||
def CaseOp : CIR_Op<"case", [ | ||||||
DeclareOpInterfaceMethods<RegionBranchOpInterface>, | ||||||
RecursivelySpeculatable, AutomaticAllocationScope]> { | ||||||
let summary = "Case operation"; | ||||||
let description = [{ | ||||||
The `cir.case` operation represents a case within a C/C++ switch. | ||||||
The `cir.case` operation must be in a `cir.switch` operation directly | ||||||
or indirectly. | ||||||
|
||||||
The `cir.case` have 4 kinds: | ||||||
- `equal, <constant>`: equality of the second case operand against the | ||||||
condition. | ||||||
- `anyof, [constant-list]`: equals to any of the values in a subsequent | ||||||
following list. | ||||||
- `range, [lower-bound, upper-bound]`: the condition is within the closed | ||||||
interval. | ||||||
- `default`: any other value. | ||||||
|
||||||
Each case region must be explicitly terminated. | ||||||
}]; | ||||||
|
||||||
let arguments = (ins ArrayAttr:$value, CaseOpKind:$kind); | ||||||
let regions = (region AnyRegion:$caseRegion); | ||||||
|
||||||
let assemblyFormat = "`(` $kind `,` $value `)` $caseRegion attr-dict"; | ||||||
|
||||||
let skipDefaultBuilders = 1; | ||||||
let builders = [ | ||||||
OpBuilder<(ins "mlir::ArrayAttr":$value, | ||||||
"CaseOpKind":$kind, | ||||||
"mlir::OpBuilder::InsertPoint &":$insertPoint)> | ||||||
]; | ||||||
} | ||||||
|
||||||
def SwitchOp : CIR_Op<"switch", | ||||||
[SameVariadicOperandSize, | ||||||
DeclareOpInterfaceMethods<RegionBranchOpInterface>, | ||||||
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments]> { | ||||||
let summary = "Switch operation"; | ||||||
let description = [{ | ||||||
The `cir.switch` operation represents C/C++ switch functionality for | ||||||
conditionally executing multiple regions of code. The operand to an switch | ||||||
is an integral condition value. | ||||||
|
||||||
The set of `cir.case` operations and their enclosing `cir.switch` | ||||||
represents the semantics of a C/C++ switch statement. Users can use | ||||||
`collectCases(llvm::SmallVector<CaseOp> &cases)` to collect the `cir.case` | ||||||
operation in the `cir.switch` operation easily. | ||||||
|
||||||
The `cir.case` operations doesn't have to be in the region of `cir.switch` | ||||||
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.
Suggested change
This one too. |
||||||
directly. However, when all the `cir.case` operations live in the region | ||||||
of `cir.switch` directly and there are no other operations except the ending | ||||||
`cir.yield` operation in the region of `cir.switch` directly, we say the | ||||||
`cir.switch` operation is in a simple form. Users can use | ||||||
`bool isSimpleForm(llvm::SmallVector<CaseOp> &cases)` member function to | ||||||
detect if the `cir.switch` operation is in a simple form. The simple form | ||||||
makes it easier for analyses to handle the `cir.switch` operation | ||||||
and makes the boundary to give up clear. | ||||||
|
||||||
To make the simple form as common as possible, CIR code generation attaches | ||||||
operations corresponding to the statements that lives between top level | ||||||
cases into the closest `cir.case` operation. | ||||||
|
||||||
For example, | ||||||
|
||||||
``` | ||||||
switch(int cond) { | ||||||
case 4: | ||||||
a++; | ||||||
b++; | ||||||
case 5: | ||||||
c++; | ||||||
|
||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
The statement `b++` is not a sub-statement of the case statement `case 4`. | ||||||
But to make the generated `cir.switch` a simple form, we will attach the | ||||||
statement `b++` into the closest `cir.case` operation. So that the generated | ||||||
code will be like: | ||||||
|
||||||
``` | ||||||
cir.switch(int cond) { | ||||||
cir.case(equal, 4) { | ||||||
a++; | ||||||
b++; | ||||||
cir.yield | ||||||
} | ||||||
cir.case(equal, 5) { | ||||||
c++; | ||||||
cir.yield | ||||||
} | ||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
For the same reason, we will hoist the case statement as the substatement | ||||||
of another case statement so that they will be in the same level. For | ||||||
example, | ||||||
|
||||||
``` | ||||||
switch(int cond) { | ||||||
case 4: | ||||||
default; | ||||||
case 5: | ||||||
a++; | ||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
will be generated as | ||||||
|
||||||
``` | ||||||
cir.switch(int cond) { | ||||||
cir.case(equal, 4) { | ||||||
cir.yield | ||||||
} | ||||||
cir.case(default) { | ||||||
cir.yield | ||||||
} | ||||||
cir.case(equal, 5) { | ||||||
a++; | ||||||
cir.yield | ||||||
} | ||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
The cir.switch is not be considered "simple" if any of the following is | ||||||
true: | ||||||
- There are case statements of the switch statement that are scope | ||||||
other than the top level compound statement scope. Note that a case | ||||||
statement itself doesn't form a scope. | ||||||
- The sub-statement of the switch statement is not a compound statement. | ||||||
- There is any code before the first case statement. For example, | ||||||
|
||||||
``` | ||||||
switch(int cond) { | ||||||
l: | ||||||
b++; | ||||||
|
||||||
case 4: | ||||||
a++; | ||||||
break; | ||||||
|
||||||
case 5: | ||||||
goto l; | ||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
the generated CIR for this non-simple switch would be: | ||||||
|
||||||
``` | ||||||
cir.switch(int cond) { | ||||||
cir.label "l" | ||||||
b++; | ||||||
cir.case(4) { | ||||||
a++; | ||||||
cir.break | ||||||
} | ||||||
cir.case(5) { | ||||||
goto "l" | ||||||
} | ||||||
cir.yield | ||||||
} | ||||||
``` | ||||||
}]; | ||||||
|
||||||
let arguments = (ins CIR_IntType:$condition); | ||||||
|
||||||
let regions = (region AnyRegion:$body); | ||||||
|
||||||
let skipDefaultBuilders = 1; | ||||||
let builders = [ | ||||||
OpBuilder<(ins "mlir::Value":$condition, | ||||||
"BuilderOpStateCallbackRef":$switchBuilder)> | ||||||
]; | ||||||
|
||||||
let assemblyFormat = [{ | ||||||
custom<SwitchOp>( | ||||||
$body, $condition, type($condition) | ||||||
) | ||||||
attr-dict | ||||||
}]; | ||||||
|
||||||
let extraClassDeclaration = [{ | ||||||
// Collect cases in the switch. | ||||||
void collectCases(llvm::SmallVectorImpl<CaseOp> &cases); | ||||||
|
||||||
// Check if the switch is in a simple form. | ||||||
// If yes, collect the cases to \param cases. | ||||||
// This is an expensive and need to be used with caution. | ||||||
bool isSimpleForm(llvm::SmallVectorImpl<CaseOp> &cases); | ||||||
}]; | ||||||
} | ||||||
|
||||||
//===----------------------------------------------------------------------===// | ||||||
// BrOp | ||||||
//===----------------------------------------------------------------------===// | ||||||
|
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
Oops, something went wrong.
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.
Sorry I missed this one in my earlier grammar corrections.