From 1ade6f36e3b4652a17499ae261e60d5b0de52ce0 Mon Sep 17 00:00:00 2001 From: Tobias Gysi Date: Tue, 16 May 2023 07:21:22 +0000 Subject: [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 --- .../lib/Tools/mlir-translate/MlirTranslateMain.cpp | 36 ++++++++++++++++++++-- mlir/test/mlir-translate/import-error-only.ll | 27 ++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 mlir/test/mlir-translate/import-error-only.ll (limited to 'mlir') diff --git a/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp b/mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp index a79f6afae25b..92adb8d6ac97 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 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 000000000000..b5a4132f4777 --- /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 +} -- cgit v1.2.1