41
41
#include " llvm/Support/CommandLine.h"
42
42
#include " llvm/Support/FileUtilities.h"
43
43
#include " llvm/Support/InitLLVM.h"
44
+ #include " llvm/Support/LogicalResult.h"
44
45
#include " llvm/Support/ManagedStatic.h"
45
46
#include " llvm/Support/Process.h"
46
47
#include " llvm/Support/Regex.h"
@@ -108,6 +109,23 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
108
109
cl::desc (" IRDL file to register before processing the input" ),
109
110
cl::location (irdlFileFlag), cl::init (" " ), cl::value_desc (" filename" ));
110
111
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
+
111
129
static cl::opt<bool , /* ExternalStorage=*/ true > enableDebuggerHook (
112
130
" mlir-enable-debugger-hook" ,
113
131
cl::desc (" Enable Debugger hook for debugging MLIR Actions" ),
@@ -133,15 +151,17 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
133
151
cl::location (showDialectsFlag), cl::init (false ));
134
152
135
153
static cl::opt<std::string, /* ExternalStorage=*/ true > splitInputFile{
136
- " split-input-file" , llvm::cl::ValueOptional,
154
+ " split-input-file" ,
155
+ llvm::cl::ValueOptional,
137
156
cl::callback ([&](const std::string &str) {
138
157
// Implicit value: use default marker if flag was used without value.
139
158
if (str.empty ())
140
159
splitInputFile.setValue (kDefaultSplitMarker );
141
160
}),
142
161
cl::desc (" Split the input file into chunks using the given or "
143
162
" default marker and process each chunk independently" ),
144
- cl::location (splitInputFileFlag), cl::init (" " )};
163
+ cl::location (splitInputFileFlag),
164
+ cl::init (" " )};
145
165
146
166
static cl::opt<std::string, /* ExternalStorage=*/ true > outputSplitMarker (
147
167
" output-split-marker" ,
@@ -207,6 +227,44 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
207
227
// / setDialectPluginsCallback(DialectRegistry&).
208
228
cl::list<std::string> *dialectPlugins = nullptr ;
209
229
};
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
+ };
210
268
} // namespace
211
269
212
270
ManagedStatic<MlirOptMainConfigCLOptions> clOptionsConfig;
@@ -479,6 +537,9 @@ static LogicalResult processBuffer(raw_ostream &os,
479
537
// otherwise just perform the actions without worrying about it.
480
538
if (!config.shouldVerifyDiagnostics ()) {
481
539
SourceMgrDiagnosticHandler sourceMgrHandler (*sourceMgr, &context);
540
+ DiagnosticFilter diagnosticFilter (&context,
541
+ config.getDiagnosticVerbosityLevel (),
542
+ config.shouldShowNotes ());
482
543
return performActions (os, sourceMgr, &context, config);
483
544
}
484
545
0 commit comments