Skip to content

Commit ac9b5ca

Browse files
committed
[mlir][emitc][cf] add 'cf.switch' support in CppEmitter
1 parent 2d36550 commit ac9b5ca

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,41 @@ static LogicalResult printOperation(CppEmitter &emitter,
774774
return printBinaryOperation(emitter, operation, "||");
775775
}
776776

777+
static LogicalResult printOperation(CppEmitter &emitter,
778+
cf::SwitchOp switchOp) {
779+
raw_indented_ostream &os = emitter.ostream();
780+
auto iteratorCaseValues = (*switchOp.getCaseValues()).begin();
781+
auto iteratorCaseValuesEnd = (*switchOp.getCaseValues()).end();
782+
783+
os << "\nswitch(" << emitter.getOrCreateName(switchOp.getFlag()) << ") {";
784+
785+
for (const auto caseBlock : switchOp.getCaseDestinations()) {
786+
if (iteratorCaseValues == iteratorCaseValuesEnd)
787+
return switchOp.emitOpError("case's value is absent for case block");
788+
789+
os << "\ncase " << *(iteratorCaseValues++) << ": {\n";
790+
os.indent() << "goto ";
791+
792+
if (!(emitter.hasBlockLabel(*caseBlock)))
793+
return switchOp.emitOpError("unable to find label for case block");
794+
os << emitter.getOrCreateName(*caseBlock) << ";\n";
795+
796+
os.unindent() << "}";
797+
}
798+
799+
os << "\ndefault: {\n";
800+
os.indent() << "goto ";
801+
802+
if (!(emitter.hasBlockLabel(*switchOp.getDefaultDestination())))
803+
return switchOp.emitOpError("unable to find label for default block");
804+
os << emitter.getOrCreateName(*switchOp.getDefaultDestination()) << ";\n";
805+
806+
os.unindent() << "}\n";
807+
os << "}\n";
808+
809+
return success();
810+
}
811+
777812
static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {
778813

779814
raw_indented_ostream &os = emitter.ostream();
@@ -998,7 +1033,7 @@ static LogicalResult printFunctionBody(CppEmitter &emitter,
9981033
// trailing semicolon is handled within the printOperation function.
9991034
bool trailingSemicolon =
10001035
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp,
1001-
emitc::IfOp, emitc::VerbatimOp>(op);
1036+
cf::SwitchOp, emitc::IfOp, emitc::VerbatimOp>(op);
10021037

10031038
if (failed(emitter.emitOperation(
10041039
op, /*trailingSemicolon=*/trailingSemicolon)))
@@ -1496,7 +1531,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
14961531
// Builtin ops.
14971532
.Case<ModuleOp>([&](auto op) { return printOperation(*this, op); })
14981533
// CF ops.
1499-
.Case<cf::BranchOp, cf::CondBranchOp>(
1534+
.Case<cf::BranchOp, cf::CondBranchOp, cf::SwitchOp>(
15001535
[&](auto op) { return printOperation(*this, op); })
15011536
// EmitC ops.
15021537
.Case<emitc::AddOp, emitc::ApplyOp, emitc::AssignOp,

mlir/test/Target/Cpp/switch.mlir

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s
2+
3+
func.func @switch_func(%a: i32, %b: i32, %c: i32) -> () {
4+
cf.switch %b : i32, [
5+
default: ^bb1(%a : i32),
6+
42: ^bb1(%b : i32),
7+
43: ^bb2(%c : i32),
8+
44: ^bb3(%c : i32)
9+
]
10+
11+
^bb1(%x1 : i32) :
12+
%y1 = "emitc.add" (%x1, %x1) : (i32, i32) -> i32
13+
return
14+
15+
^bb2(%x2 : i32) :
16+
%y2 = "emitc.sub" (%x2, %x2) : (i32, i32) -> i32
17+
return
18+
19+
^bb3(%x3 : i32) :
20+
%y3 = "emitc.mul" (%x3, %x3) : (i32, i32) -> i32
21+
return
22+
}
23+
// CHECK: void switch_func(int32_t [[V0:[^ ]*]], int32_t [[V1:[^ ]*]], int32_t [[V2:[^ ]*]]) {
24+
// CHECK: switch([[V1:[^ ]*]]) {
25+
// CHECK-NEXT: case 42: {
26+
// CHECK-NEXT: goto label2;
27+
// CHECK-NEXT: }
28+
// CHECK-NEXT: case 43: {
29+
// CHECK-NEXT: goto label3;
30+
// CHECK-NEXT: }
31+
// CHECK-NEXT: case 44: {
32+
// CHECK-NEXT: goto label4;
33+
// CHECK-NEXT: }
34+
// CHECK-NEXT: default: {
35+
// CHECK-NEXT: goto label2;
36+
// CHECK-NEXT: }
37+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)