Skip to content

[MLIR][mlir-opt] add support for disabling diagnostics #117669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class DialectRegistry;
class PassPipelineCLParser;
class PassManager;

/// enum class to indicate the verbosity level of the diagnostic filter.
enum class VerbosityLevel {
ErrorsOnly = 0,
ErrorsAndWarnings,
ErrorsWarningsAndRemarks
};

/// Configuration options for the mlir-opt tool.
/// This is intended to help building tools like mlir-opt by collecting the
/// supported options.
Expand Down Expand Up @@ -74,6 +81,11 @@ class MlirOptMainConfig {
dumpPassPipelineFlag = dump;
return *this;
}

VerbosityLevel getDiagnosticVerbosityLevel() const {
return diagnosticVerbosityLevelFlag;
}

bool shouldDumpPassPipeline() const { return dumpPassPipelineFlag; }

/// Set the output format to bytecode instead of textual IR.
Expand All @@ -82,10 +94,13 @@ class MlirOptMainConfig {
return *this;
}
bool shouldEmitBytecode() const { return emitBytecodeFlag; }

bool shouldElideResourceDataFromBytecode() const {
return elideResourceDataFromBytecodeFlag;
}

bool shouldShowNotes() const { return !disableDiagnosticNotesFlag; }

/// Set the IRDL file to load before processing the input.
MlirOptMainConfig &setIrdlFile(StringRef file) {
irdlFileFlag = file;
Expand Down Expand Up @@ -202,6 +217,11 @@ class MlirOptMainConfig {
/// Configuration for the debugging hooks.
tracing::DebugConfig debugConfig;

/// Verbosity level of diagnostic information. 0: Errors only,
/// 1: Errors and warnings, 2: Errors, warnings and remarks.
VerbosityLevel diagnosticVerbosityLevelFlag =
VerbosityLevel::ErrorsWarningsAndRemarks;

/// Print the pipeline that will be run.
bool dumpPassPipelineFlag = false;

Expand Down Expand Up @@ -235,6 +255,11 @@ class MlirOptMainConfig {
/// Show the registered dialects before trying to load the input file.
bool showDialectsFlag = false;

/// Show the notes in diagnostic information. Notes can be included in
/// any diagnostic information, so it is not specified in the verbosity
/// level.
bool disableDiagnosticNotesFlag = true;

/// Split the input file based on the given marker into chunks and process
/// each chunk independently. Input is not split if empty.
std::string splitInputFileFlag = "";
Expand Down
65 changes: 63 additions & 2 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/LogicalResult.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Regex.h"
Expand Down Expand Up @@ -108,6 +109,23 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
cl::desc("IRDL file to register before processing the input"),
cl::location(irdlFileFlag), cl::init(""), cl::value_desc("filename"));

static cl::opt<VerbosityLevel, /*ExternalStorage=*/true>
diagnosticVerbosityLevel(
"mlir-diagnostic-verbosity-level",
cl::desc("Choose level of diagnostic information"),
cl::location(diagnosticVerbosityLevelFlag),
cl::init(VerbosityLevel::ErrorsWarningsAndRemarks),
cl::values(
clEnumValN(VerbosityLevel::ErrorsOnly, "errors", "Errors only"),
clEnumValN(VerbosityLevel::ErrorsAndWarnings, "warnings",
"Errors and warnings"),
clEnumValN(VerbosityLevel::ErrorsWarningsAndRemarks, "remarks",
"Errors, warnings and remarks")));

static cl::opt<bool, /*ExternalStorage=*/true> disableDiagnosticNotes(
"mlir-disable-diagnostic-notes", cl::desc("Disable diagnostic notes."),
cl::location(disableDiagnosticNotesFlag), cl::init(false));

static cl::opt<bool, /*ExternalStorage=*/true> enableDebuggerHook(
"mlir-enable-debugger-hook",
cl::desc("Enable Debugger hook for debugging MLIR Actions"),
Expand All @@ -133,15 +151,17 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
cl::location(showDialectsFlag), cl::init(false));

static cl::opt<std::string, /*ExternalStorage=*/true> splitInputFile{
"split-input-file", llvm::cl::ValueOptional,
"split-input-file",
llvm::cl::ValueOptional,
cl::callback([&](const std::string &str) {
// Implicit value: use default marker if flag was used without value.
if (str.empty())
splitInputFile.setValue(kDefaultSplitMarker);
}),
cl::desc("Split the input file into chunks using the given or "
"default marker and process each chunk independently"),
cl::location(splitInputFileFlag), cl::init("")};
cl::location(splitInputFileFlag),
cl::init("")};

static cl::opt<std::string, /*ExternalStorage=*/true> outputSplitMarker(
"output-split-marker",
Expand Down Expand Up @@ -202,6 +222,44 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
/// setDialectPluginsCallback(DialectRegistry&).
cl::list<std::string> *dialectPlugins = nullptr;
};

/// A scoped diagnostic handler that suppresses certain diagnostics based on
/// the verbosity level and whether the diagnostic is a note.
class DiagnosticFilter : public ScopedDiagnosticHandler {
public:
DiagnosticFilter(MLIRContext *ctx, VerbosityLevel verbosityLevel,
bool showNotes = true)
: ScopedDiagnosticHandler(ctx) {
setHandler([verbosityLevel, showNotes](Diagnostic &diag) {
auto severity = diag.getSeverity();
switch (severity) {
case DiagnosticSeverity::Error:
// failure indicates that the error is not handled by the filter and
// goes through to the default handler. Therefore, the error can be
// successfully printed.
return failure();
case DiagnosticSeverity::Warning:
if (verbosityLevel == VerbosityLevel::ErrorsOnly)
return success();
else
return failure();
case DiagnosticSeverity::Remark:
if (verbosityLevel == VerbosityLevel::ErrorsOnly ||
verbosityLevel == VerbosityLevel::ErrorsAndWarnings)
return success();
else
return failure();
case DiagnosticSeverity::Note:
if (showNotes)
return failure();
else
return success();
default:
llvm_unreachable("Unknown diagnostic severity");
}
});
}
};
} // namespace

ManagedStatic<MlirOptMainConfigCLOptions> clOptionsConfig;
Expand Down Expand Up @@ -474,6 +532,9 @@ static LogicalResult processBuffer(raw_ostream &os,
// otherwise just perform the actions without worrying about it.
if (!config.shouldVerifyDiagnostics()) {
SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
DiagnosticFilter diagnosticFilter(&context,
config.getDiagnosticVerbosityLevel(),
config.shouldShowNotes());
return performActions(os, sourceMgr, &context, config);
}

Expand Down