Skip to content

Commit ce4da0c

Browse files
authored
Enables textual IR roundtripping through --verifyRoundtrip (llvm#82946)
The patch enables roundtrip to textual file when running `--verifyRoundtrip`. The verification is successful if both textual and bytecode formats can roundtrip successfully.
1 parent c087beb commit ce4da0c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
276276
if (!irdlFile.empty() && failed(loadIRDLDialects(irdlFile, roundtripContext)))
277277
return failure();
278278

279+
std::string testType = (useBytecode) ? "bytecode" : "textual";
279280
// Print a first time with custom format (or bytecode) and parse it back to
280281
// the roundtripModule.
281282
{
@@ -289,16 +290,16 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
289290
}
290291
} else {
291292
op->print(ostream,
292-
OpPrintingFlags().printGenericOpForm(false).enableDebugInfo());
293+
OpPrintingFlags().printGenericOpForm().enableDebugInfo());
293294
}
294295
FallbackAsmResourceMap fallbackResourceMap;
295296
ParserConfig parseConfig(&roundtripContext, /*verifyAfterParse=*/true,
296297
&fallbackResourceMap);
297298
roundtripModule =
298299
parseSourceString<Operation *>(ostream.str(), parseConfig);
299300
if (!roundtripModule) {
300-
op->emitOpError()
301-
<< "failed to parse bytecode back, cannot verify round-trip.\n";
301+
op->emitOpError() << "failed to parse " << testType
302+
<< " content back, cannot verify round-trip.\n";
302303
return failure();
303304
}
304305
}
@@ -317,21 +318,22 @@ static LogicalResult doVerifyRoundTrip(Operation *op,
317318
}
318319
if (reference != roundtrip) {
319320
// TODO implement a diff.
320-
return op->emitOpError() << "roundTrip testing roundtripped module differs "
321-
"from reference:\n<<<<<<Reference\n"
322-
<< reference << "\n=====\n"
323-
<< roundtrip << "\n>>>>>roundtripped\n";
321+
return op->emitOpError()
322+
<< testType
323+
<< " roundTrip testing roundtripped module differs "
324+
"from reference:\n<<<<<<Reference\n"
325+
<< reference << "\n=====\n"
326+
<< roundtrip << "\n>>>>>roundtripped\n";
324327
}
325328

326329
return success();
327330
}
328331

329332
static LogicalResult doVerifyRoundTrip(Operation *op,
330333
const MlirOptMainConfig &config) {
331-
// Textual round-trip isn't fully robust at the moment (for example implicit
332-
// terminator are losing location informations).
333-
334-
return doVerifyRoundTrip(op, config, /*useBytecode=*/true);
334+
auto txtStatus = doVerifyRoundTrip(op, config, /*useBytecode=*/false);
335+
auto bcStatus = doVerifyRoundTrip(op, config, /*useBytecode=*/true);
336+
return success(succeeded(txtStatus) && succeeded(bcStatus));
335337
}
336338

337339
/// Perform the actions on the input file indicated by the command line flags

0 commit comments

Comments
 (0)