py::arg("enable"), "Enable / disable verify-each.")
.def_static(
"parse",
- [](const std::string pipeline, DefaultingPyMlirContext context) {
+ [](const std::string &pipeline, DefaultingPyMlirContext context) {
MlirPassManager passManager = mlirPassManagerCreate(context->get());
- MlirLogicalResult status = mlirParsePassPipeline(
+ PyPrintAccumulator errorMsg;
+ MlirLogicalResult status = mlirOpPassManagerAddPipeline(
mlirPassManagerGetAsOpPassManager(passManager),
- mlirStringRefCreate(pipeline.data(), pipeline.size()));
+ mlirStringRefCreate(pipeline.data(), pipeline.size()),
+ errorMsg.getCallback(), errorMsg.getUserData());
if (mlirLogicalResultIsFailure(status))
- throw SetPyError(PyExc_ValueError,
- llvm::Twine("invalid pass pipeline '") +
- pipeline + "'.");
+ throw SetPyError(PyExc_ValueError, std::string(errorMsg.join()));
return new PyPassManager(passManager);
},
py::arg("pipeline"), py::arg("context") = py::none(),
fwrite(str.data, 1, str.length, stderr);
}
+static void dontPrint(MlirStringRef str, void *userData) {
+ (void)str;
+ (void)userData;
+}
+
void testPrintPassPipeline() {
MlirContext ctx = mlirContextCreate();
MlirPassManager pm = mlirPassManagerCreate(ctx);
MlirLogicalResult status = mlirParsePassPipeline(
mlirPassManagerGetAsOpPassManager(pm),
mlirStringRefCreateFromCString(
- "builtin.module(func.func(print-op-stats{json=false}),"
- " func.func(print-op-stats{json=false}))"));
+ "builtin.module(func.func(print-op-stats{json=false}))"));
// Expect a failure, we haven't registered the print-op-stats pass yet.
if (mlirLogicalResultIsSuccess(status)) {
fprintf(
status = mlirParsePassPipeline(
mlirPassManagerGetAsOpPassManager(pm),
mlirStringRefCreateFromCString(
- "builtin.module(func.func(print-op-stats{json=false}),"
- " func.func(print-op-stats{json=false}))"));
+ "builtin.module(func.func(print-op-stats{json=false}))"));
// Expect a failure, we haven't registered the print-op-stats pass yet.
if (mlirLogicalResultIsFailure(status)) {
fprintf(stderr,
exit(EXIT_FAILURE);
}
- // CHECK: Round-trip: builtin.module(builtin.module(
- // CHECK-SAME: func.func(print-op-stats{json=false}),
- // CHECK-SAME: func.func(print-op-stats{json=false})
- // CHECK-SAME: ))
+ // CHECK: Round-trip: builtin.module(
+ // CHECK-SAME: builtin.module(func.func(print-op-stats{json=false}))
+ // CHECK-SAME: )
fprintf(stderr, "Round-trip: ");
mlirPrintPassPipeline(mlirPassManagerGetAsOpPassManager(pm), printToStderr,
NULL);
fprintf(stderr, "\n");
+
+ // Try appending a pass:
+ status = mlirOpPassManagerAddPipeline(
+ mlirPassManagerGetAsOpPassManager(pm),
+ mlirStringRefCreateFromCString("func.func(print-op-stats{json=false})"),
+ printToStderr, NULL);
+ if (mlirLogicalResultIsFailure(status)) {
+ fprintf(stderr, "Unexpected failure appending pipeline\n");
+ exit(EXIT_FAILURE);
+ }
+ // CHECK: Appended: builtin.module(
+ // CHECK-SAME: builtin.module(func.func(print-op-stats{json=false})),
+ // CHECK-SAME: func.func(print-op-stats{json=false})
+ // CHECK-SAME: )
+ fprintf(stderr, "Appended: ");
+ mlirPrintPassPipeline(mlirPassManagerGetAsOpPassManager(pm), printToStderr,
+ NULL);
+ fprintf(stderr, "\n");
+
+ mlirPassManagerDestroy(pm);
+ mlirContextDestroy(ctx);
+}
+
+void testParseErrorCapture() {
+ // CHECK-LABEL: testParseErrorCapture:
+ fprintf(stderr, "\nTEST: testParseErrorCapture:\n");
+
+ MlirContext ctx = mlirContextCreate();
+ MlirPassManager pm = mlirPassManagerCreate(ctx);
+ MlirOpPassManager opm = mlirPassManagerGetAsOpPassManager(pm);
+ MlirStringRef invalidPipeline = mlirStringRefCreateFromCString("invalid");
+
+ // CHECK: mlirOpPassManagerAddPipeline:
+ // CHECK: 'invalid' does not refer to a registered pass or pass pipeline
+ fprintf(stderr, "mlirOpPassManagerAddPipeline:\n");
+ if (mlirLogicalResultIsSuccess(mlirOpPassManagerAddPipeline(
+ opm, invalidPipeline, printToStderr, NULL)))
+ exit(EXIT_FAILURE);
+ fprintf(stderr, "\n");
+
+ // Make sure all output is going through the callback.
+ // CHECK: dontPrint: <>
+ fprintf(stderr, "dontPrint: <");
+ if (mlirLogicalResultIsSuccess(
+ mlirOpPassManagerAddPipeline(opm, invalidPipeline, dontPrint, NULL)))
+ exit(EXIT_FAILURE);
+ fprintf(stderr, ">\n");
+
mlirPassManagerDestroy(pm);
mlirContextDestroy(ctx);
}
testRunPassOnNestedModule();
testPrintPassPipeline();
testParsePassPipeline();
+ testParseErrorCapture();
testExternalPass();
return 0;
}
# An unregistered pass should not parse.
try:
pm = PassManager.parse("builtin.module(func.func(not-existing-pass{json=false}))")
- # TODO: this error should be propagate to Python but the C API does not help right now.
- # CHECK: error: 'not-existing-pass' does not refer to a registered pass or pass pipeline
except ValueError as e:
- # CHECK: ValueError exception: invalid pass pipeline 'builtin.module(func.func(not-existing-pass{json=false}))'.
+ # CHECK: ValueError exception: {{.+}} 'not-existing-pass' does not refer to a registered pass
log("ValueError exception:", e)
else:
log("Exception not produced")
try:
pm = PassManager.parse("unknown-pass")
except ValueError as e:
- # CHECK: ValueError exception: invalid pass pipeline 'unknown-pass'.
+ # CHECK: ValueError exception: MLIR Textual PassPipeline Parser:1:1: error:
+ # CHECK-SAME: 'unknown-pass' does not refer to a registered pass or pass pipeline
+ # CHECK: unknown-pass
+ # CHECK: ^
log("ValueError exception:", e)
else:
log("Exception not produced")
try:
pm = PassManager.parse("func.func(normalize-memrefs)")
except ValueError as e:
- # CHECK: Can't add pass 'NormalizeMemRefs' restricted to 'builtin.module' on a PassManager intended to run on 'func.func', did you intend to nest?
- # CHECK: ValueError exception: invalid pass pipeline 'func.func(normalize-memrefs)'.
+ # CHECK: ValueError exception: Can't add pass 'NormalizeMemRefs' restricted to 'builtin.module' on a PassManager intended to run on 'func.func', did you intend to nest?
log("ValueError exception:", e)
else:
log("Exception not produced")