diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index a91f5ab931140..01de0e41f2035 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -475,7 +475,10 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::SwitchOp switchOp) { raw_indented_ostream &os = emitter.ostream(); - os << "\nswitch (" << emitter.getOrCreateName(switchOp.getArg()) << ") {"; + os << "\nswitch ("; + if (failed(emitter.emitOperand(switchOp.getArg()))) + return failure(); + os << ") {"; for (auto pair : llvm::zip(switchOp.getCases(), switchOp.getCaseRegions())) { os << "\ncase " << std::get<0>(pair) << ": {\n"; diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir index 1a8f5e2dfd2b6..4e20c1fc6536a 100644 --- a/mlir/test/Target/Cpp/switch.mlir +++ b/mlir/test/Target/Cpp/switch.mlir @@ -882,3 +882,39 @@ func.func @emitc_switch_ui64() { } return } + +// CPP-DEFAULT-LABEL: void emitc_switch_expression() { +// CPP-DEFAULT: int64_t v1 = 42; +// CPP-DEFAULT: switch (-v1) { +// CPP-DEFAULT: default: { +// CPP-DEFAULT: break; +// CPP-DEFAULT: } +// CPP-DEFAULT: } +// CPP-DEFAULT: return; +// CPP-DEFAULT: } + +// CPP-DECLTOP-LABEL: void emitc_switch_expression() { +// CPP-DECLTOP: int64_t v1; +// CPP-DECLTOP: v1 = 42; +// CPP-DECLTOP: switch (-v1) { +// CPP-DECLTOP: default: { +// CPP-DECLTOP: break; +// CPP-DECLTOP: } +// CPP-DECLTOP: } +// CPP-DECLTOP: return; +// CPP-DECLTOP: } + +func.func @emitc_switch_expression() { + %x = "emitc.constant"(){value = 42 : i64} : () -> i64 + + %0 = emitc.expression : i64 { + %a = emitc.unary_minus %x : (i64) -> i64 + emitc.yield %a : i64 + } + + emitc.switch %0 : i64 + default { + emitc.yield + } + return +}