From a1c9dd7c8093e30266f643e6832fbe61a6e506bc Mon Sep 17 00:00:00 2001 From: Andrey Timonin Date: Thu, 10 Oct 2024 14:31:18 +0300 Subject: [PATCH] [mlir][emitc] Fix the error with closing bracket in CppEmitter in switchOp (#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`. --- mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 1 + mlir/lib/Target/Cpp/TranslateToCpp.cpp | 2 +- mlir/test/Target/Cpp/switch.mlir | 28 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) 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() {