[Mlir-commits] [mlir] 1ade6f3 - [mlir] Add mlir translate flag to print errors only.

Tobias Gysi llvmlistbot at llvm.org
Tue May 16 00:33:05 PDT 2023


Author: Tobias Gysi
Date: 2023-05-16T07:32:13Z
New Revision: 1ade6f36e3b4652a17499ae261e60d5b0de52ce0

URL: https://github.com/llvm/llvm-project/commit/1ade6f36e3b4652a17499ae261e60d5b0de52ce0
DIFF: https://github.com/llvm/llvm-project/commit/1ade6f36e3b4652a17499ae261e60d5b0de52ce0.diff

LOG: [mlir] Add mlir translate flag to print errors only.

The revision adds a flag to mlir translate that suppresses
any non-error diagnostics. The flag is useful when importing
LLVM IR to LLVM dialect, which produces a lot of
warnings due to dropped metadata and debug intrinsics.

Reviewed By: Dinistro

Differential Revision: https://reviews.llvm.org/D150547

Added: 
    mlir/test/mlir-translate/import-error-only.ll

Modified: 
    mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp b/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp
index a79f6afae25b3..92adb8d6ac97c 100644
--- a/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp
+++ b/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp
@@ -24,7 +24,26 @@
 using namespace mlir;
 
 //===----------------------------------------------------------------------===//
-// Translation Parser
+// Diagnostic Filter
+//===----------------------------------------------------------------------===//
+
+namespace {
+/// A scoped diagnostic handler that marks non-error diagnostics as handled. As
+/// a result, the main diagnostic handler does not print non-error diagnostics.
+class ErrorDiagnosticFilter : public ScopedDiagnosticHandler {
+public:
+  ErrorDiagnosticFilter(MLIRContext *ctx) : ScopedDiagnosticHandler(ctx) {
+    setHandler([](Diagnostic &diag) {
+      if (diag.getSeverity() != DiagnosticSeverity::Error)
+        return success();
+      return failure();
+    });
+  }
+};
+} // namespace
+
+//===----------------------------------------------------------------------===//
+// Translate Entry Point
 //===----------------------------------------------------------------------===//
 
 LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
@@ -55,6 +74,12 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
                      "expected-* lines on the corresponding line"),
       llvm::cl::init(false));
 
+  static llvm::cl::opt<bool> errorDiagnosticsOnly(
+      "error-diagnostics-only",
+      llvm::cl::desc("Filter all non-error diagnostics "
+                     "(discouraged: testing only!)"),
+      llvm::cl::init(false));
+
   llvm::InitLLVM y(argc, argv);
 
   // Add flags for all the registered translations.
@@ -121,12 +146,17 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
 
       if (verifyDiagnostics) {
         // In the diagnostic verification flow, we ignore whether the
-        // translation failed (in most cases, it is expected to fail).
-        // Instead, we check if the diagnostics were produced as expected.
+        // translation failed (in most cases, it is expected to fail) and we do
+        // not filter non-error diagnostics even if `errorDiagnosticsOnly` is
+        // set. Instead, we check if the diagnostics were produced as expected.
         SourceMgrDiagnosticVerifierHandler sourceMgrHandler(*sourceMgr,
                                                             &context);
         (void)(*translationRequested)(sourceMgr, os, &context);
         result = sourceMgrHandler.verify();
+      } else if (errorDiagnosticsOnly) {
+        SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
+        ErrorDiagnosticFilter diagnosticFilter(&context);
+        result = (*translationRequested)(sourceMgr, *stream, &context);
       } else {
         SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
         result = (*translationRequested)(sourceMgr, *stream, &context);

diff  --git a/mlir/test/mlir-translate/import-error-only.ll b/mlir/test/mlir-translate/import-error-only.ll
new file mode 100644
index 0000000000000..b5a4132f4777c
--- /dev/null
+++ b/mlir/test/mlir-translate/import-error-only.ll
@@ -0,0 +1,27 @@
+; RUN: not mlir-translate %s -import-llvm -split-input-file -error-diagnostics-only 2>&1 | FileCheck %s --check-prefix=ERROR
+; RUN: not mlir-translate %s -import-llvm -split-input-file 2>&1 | FileCheck %s --check-prefix=ALL
+
+; ERROR-NOT: warning:
+; ALL:       warning:
+define void @warning(i64 %n, ptr %A) {
+entry:
+  br label %end, !llvm.loop !0
+end:
+  ret void
+}
+
+!0 = distinct !{!0, !1, !2}
+!1 = !{!"llvm.loop.disable_nonforced"}
+!2 = !{!"llvm.loop.typo"}
+
+; // -----
+
+; ERROR: error:
+; ALL:   error:
+define i32 @error(ptr %dst) {
+  indirectbr ptr %dst, [label %bb1, label %bb2]
+bb1:
+  ret i32 0
+bb2:
+  ret i32 1
+}


        


More information about the Mlir-commits mailing list