Skip to content

Commit 1ade6f3

Browse files
committed
[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
1 parent 258c9be commit 1ade6f3

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,26 @@
2424
using namespace mlir;
2525

2626
//===----------------------------------------------------------------------===//
27-
// Translation Parser
27+
// Diagnostic Filter
28+
//===----------------------------------------------------------------------===//
29+
30+
namespace {
31+
/// A scoped diagnostic handler that marks non-error diagnostics as handled. As
32+
/// a result, the main diagnostic handler does not print non-error diagnostics.
33+
class ErrorDiagnosticFilter : public ScopedDiagnosticHandler {
34+
public:
35+
ErrorDiagnosticFilter(MLIRContext *ctx) : ScopedDiagnosticHandler(ctx) {
36+
setHandler([](Diagnostic &diag) {
37+
if (diag.getSeverity() != DiagnosticSeverity::Error)
38+
return success();
39+
return failure();
40+
});
41+
}
42+
};
43+
} // namespace
44+
45+
//===----------------------------------------------------------------------===//
46+
// Translate Entry Point
2847
//===----------------------------------------------------------------------===//
2948

3049
LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
@@ -55,6 +74,12 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
5574
"expected-* lines on the corresponding line"),
5675
llvm::cl::init(false));
5776

77+
static llvm::cl::opt<bool> errorDiagnosticsOnly(
78+
"error-diagnostics-only",
79+
llvm::cl::desc("Filter all non-error diagnostics "
80+
"(discouraged: testing only!)"),
81+
llvm::cl::init(false));
82+
5883
llvm::InitLLVM y(argc, argv);
5984

6085
// Add flags for all the registered translations.
@@ -121,12 +146,17 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
121146

122147
if (verifyDiagnostics) {
123148
// In the diagnostic verification flow, we ignore whether the
124-
// translation failed (in most cases, it is expected to fail).
125-
// Instead, we check if the diagnostics were produced as expected.
149+
// translation failed (in most cases, it is expected to fail) and we do
150+
// not filter non-error diagnostics even if `errorDiagnosticsOnly` is
151+
// set. Instead, we check if the diagnostics were produced as expected.
126152
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(*sourceMgr,
127153
&context);
128154
(void)(*translationRequested)(sourceMgr, os, &context);
129155
result = sourceMgrHandler.verify();
156+
} else if (errorDiagnosticsOnly) {
157+
SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
158+
ErrorDiagnosticFilter diagnosticFilter(&context);
159+
result = (*translationRequested)(sourceMgr, *stream, &context);
130160
} else {
131161
SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
132162
result = (*translationRequested)(sourceMgr, *stream, &context);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: not mlir-translate %s -import-llvm -split-input-file -error-diagnostics-only 2>&1 | FileCheck %s --check-prefix=ERROR
2+
; RUN: not mlir-translate %s -import-llvm -split-input-file 2>&1 | FileCheck %s --check-prefix=ALL
3+
4+
; ERROR-NOT: warning:
5+
; ALL: warning:
6+
define void @warning(i64 %n, ptr %A) {
7+
entry:
8+
br label %end, !llvm.loop !0
9+
end:
10+
ret void
11+
}
12+
13+
!0 = distinct !{!0, !1, !2}
14+
!1 = !{!"llvm.loop.disable_nonforced"}
15+
!2 = !{!"llvm.loop.typo"}
16+
17+
; // -----
18+
19+
; ERROR: error:
20+
; ALL: error:
21+
define i32 @error(ptr %dst) {
22+
indirectbr ptr %dst, [label %bb1, label %bb2]
23+
bb1:
24+
ret i32 0
25+
bb2:
26+
ret i32 1
27+
}

0 commit comments

Comments
 (0)