Skip to content

[emitc] Fix the translation switchop with argument of expressionop #123701

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 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
36 changes: 36 additions & 0 deletions mlir/test/Target/Cpp/switch.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading