Skip to content

Commit 3ef90f8

Browse files
authored
[emitc] Fix the translation switchop with argument of expressionop (#123701)
Now a `emitc.switch` with argument of `emitc.expression` wouldn't emit its argument to cpp. This patch fix it.
1 parent 0fe8e70 commit 3ef90f8

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
475475
emitc::SwitchOp switchOp) {
476476
raw_indented_ostream &os = emitter.ostream();
477477

478-
os << "\nswitch (" << emitter.getOrCreateName(switchOp.getArg()) << ") {";
478+
os << "\nswitch (";
479+
if (failed(emitter.emitOperand(switchOp.getArg())))
480+
return failure();
481+
os << ") {";
479482

480483
for (auto pair : llvm::zip(switchOp.getCases(), switchOp.getCaseRegions())) {
481484
os << "\ncase " << std::get<0>(pair) << ": {\n";

mlir/test/Target/Cpp/switch.mlir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,3 +882,39 @@ func.func @emitc_switch_ui64() {
882882
}
883883
return
884884
}
885+
886+
// CPP-DEFAULT-LABEL: void emitc_switch_expression() {
887+
// CPP-DEFAULT: int64_t v1 = 42;
888+
// CPP-DEFAULT: switch (-v1) {
889+
// CPP-DEFAULT: default: {
890+
// CPP-DEFAULT: break;
891+
// CPP-DEFAULT: }
892+
// CPP-DEFAULT: }
893+
// CPP-DEFAULT: return;
894+
// CPP-DEFAULT: }
895+
896+
// CPP-DECLTOP-LABEL: void emitc_switch_expression() {
897+
// CPP-DECLTOP: int64_t v1;
898+
// CPP-DECLTOP: v1 = 42;
899+
// CPP-DECLTOP: switch (-v1) {
900+
// CPP-DECLTOP: default: {
901+
// CPP-DECLTOP: break;
902+
// CPP-DECLTOP: }
903+
// CPP-DECLTOP: }
904+
// CPP-DECLTOP: return;
905+
// CPP-DECLTOP: }
906+
907+
func.func @emitc_switch_expression() {
908+
%x = "emitc.constant"(){value = 42 : i64} : () -> i64
909+
910+
%0 = emitc.expression : i64 {
911+
%a = emitc.unary_minus %x : (i64) -> i64
912+
emitc.yield %a : i64
913+
}
914+
915+
emitc.switch %0 : i64
916+
default {
917+
emitc.yield
918+
}
919+
return
920+
}

0 commit comments

Comments
 (0)