Skip to content

Commit be96bd7

Browse files
authored
[mlir][emitc] Don't emit extra semicolon after bracket (#122464)
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.
1 parent d0b641b commit be96bd7

File tree

6 files changed

+42
-19
lines changed

6 files changed

+42
-19
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ struct CppEmitter {
120120
LogicalResult emitAttribute(Location loc, Attribute attr);
121121

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

125129
/// Emits type 'type' or returns failure.
@@ -1036,16 +1040,7 @@ static LogicalResult printFunctionBody(CppEmitter &emitter,
10361040
return failure();
10371041
}
10381042
for (Operation &op : block.getOperations()) {
1039-
// When generating code for an emitc.if or cf.cond_br op no semicolon
1040-
// needs to be printed after the closing brace.
1041-
// When generating code for an emitc.for and emitc.verbatim op, printing a
1042-
// trailing semicolon is handled within the printOperation function.
1043-
bool trailingSemicolon =
1044-
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp,
1045-
emitc::IfOp, emitc::SwitchOp, emitc::VerbatimOp>(op);
1046-
1047-
if (failed(emitter.emitOperation(
1048-
op, /*trailingSemicolon=*/trailingSemicolon)))
1043+
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/true)))
10491044
return failure();
10501045
}
10511046
}
@@ -1607,6 +1602,12 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
16071602
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
16081603
return success();
16091604

1605+
// Never emit a semicolon for some operations, especially if endening with
1606+
// `}`.
1607+
trailingSemicolon &=
1608+
!isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
1609+
emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op);
1610+
16101611
os << (trailingSemicolon ? ";\n" : "\n");
16111612

16121613
return success();

mlir/test/Target/Cpp/declare_func.mlir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s
22

33
// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]);
44
emitc.declare_func @bar
5-
// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]) {
5+
// CHECK: int32_t bar(int32_t [[V1:[^ ]*]]) {
6+
// CHECK-NEXT: return [[V1]];
7+
// CHECK-NEXT: }
68
emitc.func @bar(%arg0: i32) -> i32 {
79
emitc.return %arg0 : i32
810
}

mlir/test/Target/Cpp/for.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
2-
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
2+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP
33

44
func.func @test_for(%arg0 : index, %arg1 : index, %arg2 : index) {
55
%lb = emitc.expression : index {
@@ -160,5 +160,5 @@ func.func @test_for_yield_2() {
160160
return
161161
}
162162
// CPP-DEFAULT: void test_for_yield_2() {
163-
// CPP-DEFAULT: {{.*}}= M_PI
163+
// CPP-DEFAULT: {{.*}}= M_PI;
164164
// CPP-DEFAULT: for (size_t [[IN:.*]] = 0; [[IN]] < 10; [[IN]] += 1) {

mlir/test/Target/Cpp/if.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
2-
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
2+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP
33

44
func.func @test_if(%arg0: i1, %arg1: f32) {
55
emitc.if %arg0 {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s
2+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s
3+
4+
func.func @no_extra_semicolon(%arg0: i1) {
5+
emitc.if %arg0 {
6+
emitc.include "myheader.h"
7+
emitc.if %arg0 {
8+
}
9+
emitc.verbatim "return;"
10+
}
11+
return
12+
}
13+
// CHECK: void no_extra_semicolon(bool [[V0:[^ ]*]]) {
14+
// CHECK-NEXT: if ([[V0]]) {
15+
// CHECK-NEXT: #include "myheader.h"
16+
// CHECK-NEXT: if ([[V0]]) {
17+
// CHECK-NEXT: }
18+
// CHECK-NEXT: return;
19+
// CHECK-NEXT: }
20+
// CHECK-NEXT: return;

mlir/test/Target/Cpp/switch.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
2-
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck --match-full-lines %s -check-prefix=CPP-DEFAULT
2+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck --match-full-lines %s -check-prefix=CPP-DECLTOP
33

44
// CPP-DEFAULT-LABEL: void emitc_switch_ptrdiff_t() {
55
// CPP-DEFAULT: ptrdiff_t v1 = 1;

0 commit comments

Comments
 (0)