Skip to content

Commit 1f422dc

Browse files
authored
[MLIR][mlir-opt] add support for disabling diagnostics (#117669)
This PR adds a command line argument `--mlir-disable-diagnostic` for disabling diagnostic information for mlir-opt. When debugging with mlir-opt, some developers would like to disable the diagnostic information and focus specifically on the dumped IR. For example, triton-lang/triton#5250
1 parent 700d9ac commit 1f422dc

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class DialectRegistry;
3131
class PassPipelineCLParser;
3232
class PassManager;
3333

34+
/// enum class to indicate the verbosity level of the diagnostic filter.
35+
enum class VerbosityLevel {
36+
ErrorsOnly = 0,
37+
ErrorsAndWarnings,
38+
ErrorsWarningsAndRemarks
39+
};
40+
3441
/// Configuration options for the mlir-opt tool.
3542
/// This is intended to help building tools like mlir-opt by collecting the
3643
/// supported options.
@@ -74,6 +81,11 @@ class MlirOptMainConfig {
7481
dumpPassPipelineFlag = dump;
7582
return *this;
7683
}
84+
85+
VerbosityLevel getDiagnosticVerbosityLevel() const {
86+
return diagnosticVerbosityLevelFlag;
87+
}
88+
7789
bool shouldDumpPassPipeline() const { return dumpPassPipelineFlag; }
7890

7991
/// Set the output format to bytecode instead of textual IR.
@@ -82,10 +94,13 @@ class MlirOptMainConfig {
8294
return *this;
8395
}
8496
bool shouldEmitBytecode() const { return emitBytecodeFlag; }
97+
8598
bool shouldElideResourceDataFromBytecode() const {
8699
return elideResourceDataFromBytecodeFlag;
87100
}
88101

102+
bool shouldShowNotes() const { return !disableDiagnosticNotesFlag; }
103+
89104
/// Set the IRDL file to load before processing the input.
90105
MlirOptMainConfig &setIrdlFile(StringRef file) {
91106
irdlFileFlag = file;
@@ -209,6 +224,11 @@ class MlirOptMainConfig {
209224
/// Configuration for the debugging hooks.
210225
tracing::DebugConfig debugConfig;
211226

227+
/// Verbosity level of diagnostic information. 0: Errors only,
228+
/// 1: Errors and warnings, 2: Errors, warnings and remarks.
229+
VerbosityLevel diagnosticVerbosityLevelFlag =
230+
VerbosityLevel::ErrorsWarningsAndRemarks;
231+
212232
/// Print the pipeline that will be run.
213233
bool dumpPassPipelineFlag = false;
214234

@@ -242,6 +262,11 @@ class MlirOptMainConfig {
242262
/// Show the registered dialects before trying to load the input file.
243263
bool showDialectsFlag = false;
244264

265+
/// Show the notes in diagnostic information. Notes can be included in
266+
/// any diagnostic information, so it is not specified in the verbosity
267+
/// level.
268+
bool disableDiagnosticNotesFlag = true;
269+
245270
/// Split the input file based on the given marker into chunks and process
246271
/// each chunk independently. Input is not split if empty.
247272
std::string splitInputFileFlag = "";

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/Support/CommandLine.h"
4242
#include "llvm/Support/FileUtilities.h"
4343
#include "llvm/Support/InitLLVM.h"
44+
#include "llvm/Support/LogicalResult.h"
4445
#include "llvm/Support/ManagedStatic.h"
4546
#include "llvm/Support/Process.h"
4647
#include "llvm/Support/Regex.h"
@@ -108,6 +109,23 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
108109
cl::desc("IRDL file to register before processing the input"),
109110
cl::location(irdlFileFlag), cl::init(""), cl::value_desc("filename"));
110111

112+
static cl::opt<VerbosityLevel, /*ExternalStorage=*/true>
113+
diagnosticVerbosityLevel(
114+
"mlir-diagnostic-verbosity-level",
115+
cl::desc("Choose level of diagnostic information"),
116+
cl::location(diagnosticVerbosityLevelFlag),
117+
cl::init(VerbosityLevel::ErrorsWarningsAndRemarks),
118+
cl::values(
119+
clEnumValN(VerbosityLevel::ErrorsOnly, "errors", "Errors only"),
120+
clEnumValN(VerbosityLevel::ErrorsAndWarnings, "warnings",
121+
"Errors and warnings"),
122+
clEnumValN(VerbosityLevel::ErrorsWarningsAndRemarks, "remarks",
123+
"Errors, warnings and remarks")));
124+
125+
static cl::opt<bool, /*ExternalStorage=*/true> disableDiagnosticNotes(
126+
"mlir-disable-diagnostic-notes", cl::desc("Disable diagnostic notes."),
127+
cl::location(disableDiagnosticNotesFlag), cl::init(false));
128+
111129
static cl::opt<bool, /*ExternalStorage=*/true> enableDebuggerHook(
112130
"mlir-enable-debugger-hook",
113131
cl::desc("Enable Debugger hook for debugging MLIR Actions"),
@@ -133,15 +151,17 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
133151
cl::location(showDialectsFlag), cl::init(false));
134152

135153
static cl::opt<std::string, /*ExternalStorage=*/true> splitInputFile{
136-
"split-input-file", llvm::cl::ValueOptional,
154+
"split-input-file",
155+
llvm::cl::ValueOptional,
137156
cl::callback([&](const std::string &str) {
138157
// Implicit value: use default marker if flag was used without value.
139158
if (str.empty())
140159
splitInputFile.setValue(kDefaultSplitMarker);
141160
}),
142161
cl::desc("Split the input file into chunks using the given or "
143162
"default marker and process each chunk independently"),
144-
cl::location(splitInputFileFlag), cl::init("")};
163+
cl::location(splitInputFileFlag),
164+
cl::init("")};
145165

146166
static cl::opt<std::string, /*ExternalStorage=*/true> outputSplitMarker(
147167
"output-split-marker",
@@ -207,6 +227,44 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
207227
/// setDialectPluginsCallback(DialectRegistry&).
208228
cl::list<std::string> *dialectPlugins = nullptr;
209229
};
230+
231+
/// A scoped diagnostic handler that suppresses certain diagnostics based on
232+
/// the verbosity level and whether the diagnostic is a note.
233+
class DiagnosticFilter : public ScopedDiagnosticHandler {
234+
public:
235+
DiagnosticFilter(MLIRContext *ctx, VerbosityLevel verbosityLevel,
236+
bool showNotes = true)
237+
: ScopedDiagnosticHandler(ctx) {
238+
setHandler([verbosityLevel, showNotes](Diagnostic &diag) {
239+
auto severity = diag.getSeverity();
240+
switch (severity) {
241+
case DiagnosticSeverity::Error:
242+
// failure indicates that the error is not handled by the filter and
243+
// goes through to the default handler. Therefore, the error can be
244+
// successfully printed.
245+
return failure();
246+
case DiagnosticSeverity::Warning:
247+
if (verbosityLevel == VerbosityLevel::ErrorsOnly)
248+
return success();
249+
else
250+
return failure();
251+
case DiagnosticSeverity::Remark:
252+
if (verbosityLevel == VerbosityLevel::ErrorsOnly ||
253+
verbosityLevel == VerbosityLevel::ErrorsAndWarnings)
254+
return success();
255+
else
256+
return failure();
257+
case DiagnosticSeverity::Note:
258+
if (showNotes)
259+
return failure();
260+
else
261+
return success();
262+
default:
263+
llvm_unreachable("Unknown diagnostic severity");
264+
}
265+
});
266+
}
267+
};
210268
} // namespace
211269

212270
ManagedStatic<MlirOptMainConfigCLOptions> clOptionsConfig;
@@ -479,6 +537,9 @@ static LogicalResult processBuffer(raw_ostream &os,
479537
// otherwise just perform the actions without worrying about it.
480538
if (!config.shouldVerifyDiagnostics()) {
481539
SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
540+
DiagnosticFilter diagnosticFilter(&context,
541+
config.getDiagnosticVerbosityLevel(),
542+
config.shouldShowNotes());
482543
return performActions(os, sourceMgr, &context, config);
483544
}
484545

0 commit comments

Comments
 (0)