From 30b5f72411738accffa6ae58b179392ea0f5e512 Mon Sep 17 00:00:00 2001 From: Matthias Gehre Date: Tue, 8 Oct 2024 13:33:22 +0200 Subject: [PATCH] Don't print semicolon after emitc.verbatim within emitc.for There was code to suppress printing semicolons after emitc.verbatim in the function that emits a function body, but then it would still print `#pragma;` when the `emitc.verbatim "#pragma"` was within a loop body. I moved that code into the general printOperation() function, so it applies to emitc.verbatim independent of what the parent op is. --- mlir/lib/Target/Cpp/TranslateToCpp.cpp | 16 ++++++---------- mlir/test/Target/Cpp/verbatim.mlir | 12 ++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp index c8a5cb5fbd3f61..73256451ef1487 100644 --- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp +++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp @@ -1063,16 +1063,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(op); - - if (failed(emitter.emitOperation( - op, /*trailingSemicolon=*/trailingSemicolon))) + if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/true))) return failure(); } } @@ -1630,6 +1621,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) { shouldBeInlined(cast(op)))) return success(); + if (isa(op)) { + trailingSemicolon = false; + } + os << (trailingSemicolon ? ";\n" : "\n"); return success(); diff --git a/mlir/test/Target/Cpp/verbatim.mlir b/mlir/test/Target/Cpp/verbatim.mlir index 41e39dcb6ca900..1522cc32e79a52 100644 --- a/mlir/test/Target/Cpp/verbatim.mlir +++ b/mlir/test/Target/Cpp/verbatim.mlir @@ -40,6 +40,18 @@ emitc.func @func(%arg: f32) { emitc.verbatim "#pragma my2 var={} property" %a : !emitc.array<3x7xi32> // CHECK-NEXT: #pragma my2 var=[[A]] property + emitc.return +} +// Check that no semicolon is printed after verbatim within emitc.for +emitc.func @in_loop(%arg: f32) { + %start = emitc.literal "0" : !emitc.size_t + %stop = emitc.literal "10" : !emitc.size_t + %step = emitc.literal "1" : !emitc.size_t + emitc.for %iter = %start to %stop step %step { + emitc.verbatim "#pragma" + // CHECK: #pragma + emitc.yield + } emitc.return }