Skip to content

[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

Merged
merged 1 commit into from
Jan 14, 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
21 changes: 11 additions & 10 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ struct CppEmitter {
LogicalResult emitAttribute(Location loc, Attribute attr);

/// Emits operation 'op' with/without training semicolon or returns failure.
///
/// For operations that should never be followed by a semicolon, like ForOp,
/// the `trailingSemicolon` argument is ignored and a semicolon is not
/// emitted.
LogicalResult emitOperation(Operation &op, bool trailingSemicolon);

/// Emits type 'type' or returns failure.
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -1607,6 +1602,12 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
return success();

// Never emit a semicolon for some operations, especially if endening with
// `}`.
trailingSemicolon &=
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op);

os << (trailingSemicolon ? ";\n" : "\n");

return success();
Expand Down
6 changes: 4 additions & 2 deletions mlir/test/Target/Cpp/declare_func.mlir
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s

// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]);
emitc.declare_func @bar
// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]) {
// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]) {
// CHECK-NEXT: return [[V1]];
// CHECK-NEXT: }
emitc.func @bar(%arg0: i32) -> i32 {
emitc.return %arg0 : i32
}
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/Target/Cpp/for.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP

func.func @test_for(%arg0 : index, %arg1 : index, %arg2 : index) {
%lb = emitc.expression : index {
Expand Down Expand Up @@ -160,5 +160,5 @@ func.func @test_for_yield_2() {
return
}
// CPP-DEFAULT: void test_for_yield_2() {
// CPP-DEFAULT: {{.*}}= M_PI
// CPP-DEFAULT: {{.*}}= M_PI;
// CPP-DEFAULT: for (size_t [[IN:.*]] = 0; [[IN]] < 10; [[IN]] += 1) {
4 changes: 2 additions & 2 deletions mlir/test/Target/Cpp/if.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP

func.func @test_if(%arg0: i1, %arg1: f32) {
emitc.if %arg0 {
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Target/Cpp/no_extra_semicolon.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s

func.func @no_extra_semicolon(%arg0: i1) {
emitc.if %arg0 {
emitc.include "myheader.h"
emitc.if %arg0 {
}
emitc.verbatim "return;"
}
return
}
// CHECK: void no_extra_semicolon(bool [[V0:[^ ]*]]) {
// CHECK-NEXT: if ([[V0]]) {
// CHECK-NEXT: #include "myheader.h"
// CHECK-NEXT: if ([[V0]]) {
// CHECK-NEXT: }
// CHECK-NEXT: return;
// CHECK-NEXT: }
// CHECK-NEXT: return;
4 changes: 2 additions & 2 deletions mlir/test/Target/Cpp/switch.mlir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP

// CPP-DEFAULT-LABEL: void emitc_switch_ptrdiff_t() {
// CPP-DEFAULT: ptrdiff_t v1 = 1;
Expand Down
Loading