Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,11 @@ def CIR_SwitchOp : CIR_Op<"switch", [
conditionally executing multiple regions of code. The operand to an switch
is an integral condition value.

Besides accepting an int type of condition and regions of cir code, it also accepts
a boolean allEnumCasesCovered indicating if all cases of an enum is covered. Note that
having a default CaseOp inside the switch doesn't imply allEnumCasesCovered, the OG AST switch
needs to have each case spelled out.

The set of `cir.case` operations and their enclosing `cir.switch`
represent the semantics of a C/C++ switch statement. Users can use
`collectCases(llvm::SmallVector<CaseOp> &cases)` to collect the `cir.case`
Expand Down Expand Up @@ -1173,7 +1178,8 @@ def CIR_SwitchOp : CIR_Op<"switch", [
```
}];

let arguments = (ins CIR_IntType:$condition);
let arguments = (ins CIR_IntType:$condition,
DefaultValuedAttr<BoolAttr, "false">:$allEnumCasesCovered);

let regions = (region AnyRegion:$body);

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,8 @@ mlir::LogicalResult CIRGenFunction::emitSwitchStmt(const clang::SwitchStmt &s) {
terminateBody(builder, caseOp.getCaseRegion(), caseOp.getLoc());
terminateBody(builder, swop.getBody(), swop.getLoc());

swop.setAllEnumCasesCovered(s.isAllEnumCasesCovered());

return res;
}

Expand Down
30 changes: 30 additions & 0 deletions clang/test/CIR/IR/switch.cir
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,33 @@ cir.func @s0() {
// CHECK-NEXT: }
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }


// Pretends that this is lowered from a C file and was tagged with allEnumCasesCovered = true
cir.func @s1(%1 : !s32i) {
cir.switch (%1 : !s32i) {
cir.case (default, []) {
cir.return
}
cir.case (equal, [#cir.int<1> : !s32i]) {
cir.yield
}
cir.case (equal, [#cir.int<2> : !s32i]) {
cir.yield
}
cir.yield
} { allEnumCasesCovered = true}
cir.return
}
// CHECK: cir.switch (%arg0 : !s32i) {
// CHECK-NEXT: cir.case(default, []) {
// CHECK-NEXT: cir.return
// CHECK-NEXT: }
// CHECK-NEXT: cir.case(equal, [#cir.int<1> : !s32i]) {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }
// CHECK-NEXT: cir.case(equal, [#cir.int<2> : !s32i]) {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }
// CHECK-NEXT: cir.yield
// CHECK-NEXT: } {allEnumCasesCovered = true}