diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index a2911386f12af..17a562a0d119a 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -122,6 +122,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 a reference to type 'type' or returns failure. @@ -1762,7 +1766,7 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) { return success(); if (isa(op)) { + emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op)) { trailingSemicolon = false; } diff --git a/mlir/test/Target/Cpp/declare_func.mlir b/mlir/test/Target/Cpp/declare_func.mlir index 40f73d659b368..040a46734a66d 100644 --- a/mlir/test/Target/Cpp/declare_func.mlir +++ b/mlir/test/Target/Cpp/declare_func.mlir @@ -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 } diff --git a/mlir/test/Target/Cpp/for.mlir b/mlir/test/Target/Cpp/for.mlir index 19b590f7d14b9..f44901654e7bb 100644 --- a/mlir/test/Target/Cpp/for.mlir +++ b/mlir/test/Target/Cpp/for.mlir @@ -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 : !emitc.size_t, %arg1 : !emitc.size_t, %arg2 : !emitc.size_t) { %lb = emitc.expression : !emitc.size_t { @@ -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) { diff --git a/mlir/test/Target/Cpp/if.mlir b/mlir/test/Target/Cpp/if.mlir index d3b792192c8b1..9a7e12b891bc9 100644 --- a/mlir/test/Target/Cpp/if.mlir +++ b/mlir/test/Target/Cpp/if.mlir @@ -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 { 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 0000000000000..4b1b55944434e --- /dev/null +++ b/mlir/test/Target/Cpp/no_extra_semicolon.mlir @@ -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; diff --git a/mlir/test/Target/Cpp/switch.mlir b/mlir/test/Target/Cpp/switch.mlir index 3339c02617949..1a8f5e2dfd2b6 100644 --- a/mlir/test/Target/Cpp/switch.mlir +++ b/mlir/test/Target/Cpp/switch.mlir @@ -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;