-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][emitc] Fix the error with closing bracket in CppEmitter in switchOp #110269
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
Conversation
@llvm/pr-subscribers-mlir-emitc Author: Andrey Timonin (EtoAndruwa) ChangesWhile working with Full diff: https://github.com/llvm/llvm-project/pull/110269.diff 3 Files Affected:
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 2c1ac27cfb8ff8..7c84ab4dd39eb7 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -1405,6 +1405,7 @@ def EmitC_SwitchOp : EmitC_Op<"switch", [RecursiveMemoryEffects,
func2(v4);
break;
}
+ }
```
}];
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 30657d8fccb154..d26adec500a113 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -489,7 +489,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
if (failed(emitSwitchCase(emitter, os, switchOp.getDefaultRegion())))
return failure();
- os.unindent() << "}";
+ os.unindent() << "}\n}";
return success();
}
diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir
index f9b8600606ec2b..3339c026179492 100644
--- a/mlir/test/Target/Cpp/switch.mlir
+++ b/mlir/test/Target/Cpp/switch.mlir
@@ -17,6 +17,7 @@
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -40,6 +41,7 @@
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ptrdiff_t() {
@@ -77,6 +79,7 @@ func.func @emitc_switch_ptrdiff_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -100,6 +103,7 @@ func.func @emitc_switch_ptrdiff_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ssize_t() {
@@ -138,6 +142,7 @@ func.func @emitc_switch_ssize_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -161,6 +166,7 @@ func.func @emitc_switch_ssize_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_size_t() {
@@ -199,6 +205,7 @@ func.func @emitc_switch_size_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -222,6 +229,7 @@ func.func @emitc_switch_size_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_index() {
@@ -260,6 +268,7 @@ func.func @emitc_switch_index() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -283,6 +292,7 @@ func.func @emitc_switch_index() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_opaque() {
@@ -322,6 +332,7 @@ func.func @emitc_switch_opaque() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -345,6 +356,7 @@ func.func @emitc_switch_opaque() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i1() {
@@ -383,6 +395,7 @@ func.func @emitc_switch_i1() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -406,6 +419,7 @@ func.func @emitc_switch_i1() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i8() {
@@ -444,6 +458,7 @@ func.func @emitc_switch_i8() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -467,6 +482,7 @@ func.func @emitc_switch_i8() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui8() {
@@ -505,6 +521,7 @@ func.func @emitc_switch_ui8() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -528,6 +545,7 @@ func.func @emitc_switch_ui8() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i16() {
@@ -566,6 +584,7 @@ func.func @emitc_switch_i16() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -589,6 +608,7 @@ func.func @emitc_switch_i16() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui16() {
@@ -627,6 +647,7 @@ func.func @emitc_switch_ui16() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -650,6 +671,7 @@ func.func @emitc_switch_ui16() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i32() {
@@ -688,6 +710,7 @@ func.func @emitc_switch_i32() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -711,6 +734,7 @@ func.func @emitc_switch_i32() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui32() {
@@ -749,6 +773,7 @@ func.func @emitc_switch_ui32() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -772,6 +797,7 @@ func.func @emitc_switch_ui32() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i64() {
@@ -810,6 +836,7 @@ func.func @emitc_switch_i64() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -833,6 +860,7 @@ func.func @emitc_switch_i64() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui64() {
|
@llvm/pr-subscribers-mlir Author: Andrey Timonin (EtoAndruwa) ChangesWhile working with Full diff: https://github.com/llvm/llvm-project/pull/110269.diff 3 Files Affected:
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 2c1ac27cfb8ff8..7c84ab4dd39eb7 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -1405,6 +1405,7 @@ def EmitC_SwitchOp : EmitC_Op<"switch", [RecursiveMemoryEffects,
func2(v4);
break;
}
+ }
```
}];
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 30657d8fccb154..d26adec500a113 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -489,7 +489,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
if (failed(emitSwitchCase(emitter, os, switchOp.getDefaultRegion())))
return failure();
- os.unindent() << "}";
+ os.unindent() << "}\n}";
return success();
}
diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir
index f9b8600606ec2b..3339c026179492 100644
--- a/mlir/test/Target/Cpp/switch.mlir
+++ b/mlir/test/Target/Cpp/switch.mlir
@@ -17,6 +17,7 @@
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -40,6 +41,7 @@
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ptrdiff_t() {
@@ -77,6 +79,7 @@ func.func @emitc_switch_ptrdiff_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -100,6 +103,7 @@ func.func @emitc_switch_ptrdiff_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ssize_t() {
@@ -138,6 +142,7 @@ func.func @emitc_switch_ssize_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -161,6 +166,7 @@ func.func @emitc_switch_ssize_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_size_t() {
@@ -199,6 +205,7 @@ func.func @emitc_switch_size_t() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -222,6 +229,7 @@ func.func @emitc_switch_size_t() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_index() {
@@ -260,6 +268,7 @@ func.func @emitc_switch_index() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -283,6 +292,7 @@ func.func @emitc_switch_index() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_opaque() {
@@ -322,6 +332,7 @@ func.func @emitc_switch_opaque() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -345,6 +356,7 @@ func.func @emitc_switch_opaque() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i1() {
@@ -383,6 +395,7 @@ func.func @emitc_switch_i1() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -406,6 +419,7 @@ func.func @emitc_switch_i1() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i8() {
@@ -444,6 +458,7 @@ func.func @emitc_switch_i8() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -467,6 +482,7 @@ func.func @emitc_switch_i8() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui8() {
@@ -505,6 +521,7 @@ func.func @emitc_switch_ui8() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -528,6 +545,7 @@ func.func @emitc_switch_ui8() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i16() {
@@ -566,6 +584,7 @@ func.func @emitc_switch_i16() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -589,6 +608,7 @@ func.func @emitc_switch_i16() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui16() {
@@ -627,6 +647,7 @@ func.func @emitc_switch_ui16() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -650,6 +671,7 @@ func.func @emitc_switch_ui16() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i32() {
@@ -688,6 +710,7 @@ func.func @emitc_switch_i32() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -711,6 +734,7 @@ func.func @emitc_switch_i32() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui32() {
@@ -749,6 +773,7 @@ func.func @emitc_switch_ui32() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -772,6 +797,7 @@ func.func @emitc_switch_ui32() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_i64() {
@@ -810,6 +836,7 @@ func.func @emitc_switch_i64() {
// CPP-DEFAULT: func2(v4);
// CPP-DEFAULT: break;
// CPP-DEFAULT: }
+// CPP-DEFAULT: }
// CPP-DEFAULT: return;
// CPP-DEFAULT: }
@@ -833,6 +860,7 @@ func.func @emitc_switch_i64() {
// CPP-DECLTOP: func2(v2);
// CPP-DECLTOP: break;
// CPP-DECLTOP: }
+// CPP-DECLTOP: }
// CPP-DECLTOP: return;
// CPP-DECLTOP: }
func.func @emitc_switch_ui64() {
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this.
Maybe we should use the raw_indented_ostream::DelimitedScope
RAII helper to prevent such errors.
Hi, @simon-camp! Thanks for the fast response. Should I try refactoring it using |
I'd merge this fix as is. Switching to DelimitedScope can be done later as an NFC if anyone wants to do the refactoring. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix.
I will make some changes inside my git config for the merge. Sorry for that. |
2ea1db6
to
8bfb9a7
Compare
…tchOp This commit fixed error with closing bracket in CppEmitter within printOperation for emitc::SwitchOp.
8bfb9a7
to
0b02485
Compare
Hello, @marbre! Can you please merge this? |
Excuse the delay. Thanks for fixing this. |
…tchOp (llvm#110269) While working with `emitc::SwitchOp`, it was identified that `mlir-translate` emits **invalid C code** for switch. This commit fixes the issue with the closing bracket in `CppEmitter` within `printOperation` for `emitc::SwitchOp`.
While working with
emitc::SwitchOp
, it was identified thatmlir-translate
emits invalid C code for switch.This commit fixes the issue with the closing bracket in
CppEmitter
withinprintOperation
foremitc::SwitchOp
.