-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][emitc] Don't emit extra semicolon after bracket #122464
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 @llvm/pr-subscribers-mlir Author: Kirill Chibisov (kchibisov) ChangesExtra semicolons were emitted for operations that should never have them, since not every place was checking whether semicolon would be actually needed. Thus change the emitOperation to ignore trailingSemicolon field for such operations. Full diff: https://github.com/llvm/llvm-project/pull/122464.diff 2 Files Affected:
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index d26adec500a113..dba9a625382def 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -120,6 +120,10 @@ struct CppEmitter {
LogicalResult emitAttribute(Location loc, Attribute attr);
/// Emits operation 'op' with/without training semicolon or returns failure.
+ ///
+ /// If the operation should not be followed by semicolon, like VerbatimOp,
+ /// the `trailingSemicolon` argument is ignore and semicolon is not
+ /// emitted.
LogicalResult emitOperation(Operation &op, bool trailingSemicolon);
/// Emits type 'type' or returns failure.
@@ -1036,16 +1040,7 @@ static LogicalResult printFunctionBody(CppEmitter &emitter,
return failure();
}
for (Operation &op : block.getOperations()) {
- // When generating code for an emitc.if or cf.cond_br op no semicolon
- // needs to be printed after the closing brace.
- // When generating code for an emitc.for and emitc.verbatim op, printing a
- // trailing semicolon is handled within the printOperation function.
- bool trailingSemicolon =
- !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp,
- emitc::IfOp, emitc::SwitchOp, emitc::VerbatimOp>(op);
-
- if (failed(emitter.emitOperation(
- op, /*trailingSemicolon=*/trailingSemicolon)))
+ if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/true)))
return failure();
}
}
@@ -1607,6 +1602,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
return success();
+ // Never emit semicolon for operations that end with } or opaque.
+ trailingSemicolon &=
+ !isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
+ emitc::SwitchOp, emitc::VerbatimOp, emitc::IncludeOp>(op);
+
os << (trailingSemicolon ? ";\n" : "\n");
return success();
diff --git a/mlir/test/Target/Cpp/no_extra_semicolon.mlir b/mlir/test/Target/Cpp/no_extra_semicolon.mlir
new file mode 100644
index 00000000000000..e6e0c58daf4e42
--- /dev/null
+++ b/mlir/test/Target/Cpp/no_extra_semicolon.mlir
@@ -0,0 +1,18 @@
+// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
+// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s
+
+func.func @no_extra_semicolon(%arg0: i1) {
+ emitc.if %arg0 {
+ emitc.if %arg0 {
+ }
+ emitc.verbatim "return;"
+ }
+ return
+}
+// CHECK: void test_if(bool [[V0:[^ ]*]]) {
+// CHECK-NEXT: if ([[V0]]) {
+// CHECK-NEXT: if ([[V0]]) {
+// CHECK-NEXT: }
+// CHECK-NEXT: return;
+// CHECK-NEXT: }
+// CHECK-NEXT: return;
|
6e51e97
to
5520426
Compare
Thanks, this moves the code in the right direction! I think we should eventually get rid of the |
5520426
to
0fa37f5
Compare
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.
Looks good!
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.
Looks good, just some minor nits.
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.
Another nit (keep ops in lexicographic order)
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 patch and the other reviewers for the first rounds! Looks good to me, just a minor NIT.
Extra semicolons were emitted for operations that should never have them, since not every place was checking whether semicolon would be actually needed. Thus change the emitOperation to ignore trailingSemicolon field for such operations.
a6a22c0
to
2da43ed
Compare
Thanks! In case you don't have write access, let me know and I can merged on your behalf. |
I don't have write access. |
At the moment this would be squashed with just your email. Do you want to add your name before I merge? |
@marbre I'm not sure what you're talking about, the commit has all the correct formatting + name/etc? There's also nothing to squash, since it's a single commit. Do you want me to do something extra, since nothing changed between how I was submitting things in the past and now. |
You can see it like that https://patch-diff.githubusercontent.com/raw/llvm/llvm-project/pull/122464.patch , you can see that the name is clearly there. |
I might have got confused by the GitHub UI which only shows the email address for the squash commit. Merged it, thanks again for the fix! |
Extra semicolons were emitted for operations that should never have them, since not every place was checking whether semicolon would be actually needed.
Thus change the emitOperation to ignore trailingSemicolon field for such operations.