Skip to content

Commit a3e2893

Browse files
committed
[MLIR] Add remark flags to mlir-opt
1 parent 3821885 commit a3e2893

File tree

4 files changed

+103
-10
lines changed

4 files changed

+103
-10
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ enum class VerbosityLevel {
3838
ErrorsWarningsAndRemarks
3939
};
4040

41+
using RemarkFormat = enum {
42+
REMARK_FORMAT_STDOUT,
43+
REMARK_FORMAT_YAML,
44+
REMARK_FORMAT_BITSTREAM,
45+
};
46+
4147
/// Configuration options for the mlir-opt tool.
4248
/// This is intended to help building tools like mlir-opt by collecting the
4349
/// supported options.
@@ -221,9 +227,25 @@ class MlirOptMainConfig {
221227
}
222228
bool shouldVerifyRoundtrip() const { return verifyRoundtripFlag; }
223229

230+
/// Checks if any remark filters are set.
231+
bool shouldEmitRemarks() const {
232+
// Emit all remarks only when no filters are specified.
233+
const bool hasFilters =
234+
!remarksPassedFlag.empty() || !remarksFailedFlag.empty() ||
235+
!remarksMissedFlag.empty() || !remarksAnalyseFlag.empty();
236+
return hasFilters;
237+
}
238+
224239
/// Reproducer file generation (no crash required).
225240
StringRef getReproducerFilename() const { return generateReproducerFileFlag; }
226241

242+
/// Remark options
243+
RemarkFormat remarkFormatFlag;
244+
std::string remarksPassedFlag = "";
245+
std::string remarksFailedFlag = "";
246+
std::string remarksMissedFlag = "";
247+
std::string remarksAnalyseFlag = "";
248+
227249
protected:
228250
/// Allow operation with no registered dialects.
229251
/// This option is for convenience during testing only and discouraged in

mlir/lib/Tools/mlir-opt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ add_mlir_library(MLIROptLib
1313
MLIRPluginsLib
1414
MLIRSupport
1515
MLIRIRDL
16+
MLIRRemarkStreamer
1617
)

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

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@
2323
#include "mlir/IR/Diagnostics.h"
2424
#include "mlir/IR/Location.h"
2525
#include "mlir/IR/MLIRContext.h"
26+
#include "mlir/IR/Remarks.h"
2627
#include "mlir/Parser/Parser.h"
2728
#include "mlir/Pass/PassManager.h"
2829
#include "mlir/Pass/PassRegistry.h"
30+
#include "mlir/Remark/RemarkStreamer.h"
2931
#include "mlir/Support/FileUtilities.h"
3032
#include "mlir/Support/Timing.h"
3133
#include "mlir/Support/ToolUtilities.h"
3234
#include "mlir/Tools/ParseUtilities.h"
3335
#include "mlir/Tools/Plugins/DialectPlugin.h"
3436
#include "mlir/Tools/Plugins/PassPlugin.h"
3537
#include "llvm/ADT/StringRef.h"
38+
#include "llvm/Remarks/RemarkFormat.h"
3639
#include "llvm/Support/CommandLine.h"
3740
#include "llvm/Support/InitLLVM.h"
3841
#include "llvm/Support/LogicalResult.h"
@@ -204,6 +207,41 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
204207
cl::location(generateReproducerFileFlag), cl::init(""),
205208
cl::value_desc("filename"));
206209

210+
static cl::OptionCategory remarkCategory(
211+
"Remark Options",
212+
"Filter remarks by regular expression (llvm::Regex syntax).");
213+
214+
static llvm::cl::opt<RemarkFormat, /*ExternalStorage=*/true> remarkFormat{
215+
"remark-format",
216+
llvm::cl::desc("Specify the format for remark output."),
217+
cl::location(remarkFormatFlag),
218+
llvm::cl::value_desc("format"),
219+
llvm::cl::init(REMARK_FORMAT_STDOUT),
220+
llvm::cl::values(
221+
clEnumValN(REMARK_FORMAT_STDOUT, "emitRemark",
222+
"Print as emitRemark to command-line"),
223+
clEnumValN(REMARK_FORMAT_YAML, "yaml", "Print yaml file"),
224+
clEnumValN(REMARK_FORMAT_BITSTREAM, "bitstream",
225+
"Print bitstream file")),
226+
llvm::cl::cat(remarkCategory)};
227+
228+
static cl::opt<std::string, /*ExternalStorage=*/true> remarksPassed(
229+
"remarks-passed", cl::desc("Show passed remarks"),
230+
cl::location(remarksPassedFlag), cl::init(""), cl::cat(remarkCategory));
231+
232+
static cl::opt<std::string, /*ExternalStorage=*/true> remarksFailed(
233+
"remarks-failed", cl::desc("Show failed remarks"),
234+
cl::location(remarksFailedFlag), cl::init(""), cl::cat(remarkCategory));
235+
236+
static cl::opt<std::string, /*ExternalStorage=*/true> remarksMissed(
237+
"remarks-missed", cl::desc("Show missed remarks"),
238+
cl::location(remarksMissedFlag), cl::init(""), cl::cat(remarkCategory));
239+
240+
static cl::opt<std::string, /*ExternalStorage=*/true> remarksAnalyse(
241+
"remarks-analyse", cl::desc("Show analysis remarks"),
242+
cl::location(remarksAnalyseFlag), cl::init(""),
243+
cl::cat(remarkCategory));
244+
207245
/// Set the callback to load a pass plugin.
208246
passPlugins.setCallback([&](const std::string &pluginPath) {
209247
auto plugin = PassPlugin::load(pluginPath);
@@ -241,23 +279,23 @@ class DiagnosticFilter : public ScopedDiagnosticHandler {
241279
setHandler([verbosityLevel, showNotes](Diagnostic &diag) {
242280
auto severity = diag.getSeverity();
243281
switch (severity) {
244-
case DiagnosticSeverity::Error:
282+
case mlir::DiagnosticSeverity::Error:
245283
// failure indicates that the error is not handled by the filter and
246284
// goes through to the default handler. Therefore, the error can be
247285
// successfully printed.
248286
return failure();
249-
case DiagnosticSeverity::Warning:
287+
case mlir::DiagnosticSeverity::Warning:
250288
if (verbosityLevel == VerbosityLevel::ErrorsOnly)
251289
return success();
252290
else
253291
return failure();
254-
case DiagnosticSeverity::Remark:
292+
case mlir::DiagnosticSeverity::Remark:
255293
if (verbosityLevel == VerbosityLevel::ErrorsOnly ||
256294
verbosityLevel == VerbosityLevel::ErrorsAndWarnings)
257295
return success();
258296
else
259297
return failure();
260-
case DiagnosticSeverity::Note:
298+
case mlir::DiagnosticSeverity::Note:
261299
if (showNotes)
262300
return failure();
263301
else
@@ -462,6 +500,38 @@ performActions(raw_ostream &os,
462500

463501
context->enableMultithreading(wasThreadingEnabled);
464502

503+
// Set up optimization remarks.
504+
if (config.shouldEmitRemarks()) {
505+
remark::RemarkCategories cats{
506+
config.remarksPassedFlag, config.remarksFailedFlag,
507+
config.remarksMissedFlag, config.remarksAnalyseFlag};
508+
509+
mlir::MLIRContext &ctx = *context;
510+
511+
switch (config.remarkFormatFlag) {
512+
case REMARK_FORMAT_STDOUT:
513+
if (failed(mlir::remark::enableOptimizationRemarks(ctx, nullptr, cats)))
514+
return failure();
515+
break;
516+
517+
case REMARK_FORMAT_YAML: {
518+
constexpr llvm::StringLiteral file{"mlir-remarks.yaml"};
519+
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
520+
ctx, file, llvm::remarks::Format::YAML, cats)))
521+
return failure();
522+
break;
523+
}
524+
525+
case REMARK_FORMAT_BITSTREAM: {
526+
constexpr llvm::StringLiteral File{"mlir-remarks.bitstream"};
527+
if (failed(mlir::remark::enableOptimizationRemarksWithLLVMStreamer(
528+
ctx, File, llvm::remarks::Format::Bitstream, cats)))
529+
return failure();
530+
break;
531+
}
532+
}
533+
}
534+
465535
// Prepare the pass manager, applying command-line and reproducer options.
466536
PassManager pm(op.get()->getName(), PassManager::Nesting::Implicit);
467537
pm.enableVerifier(config.shouldVerifyPasses());
@@ -523,8 +593,8 @@ processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
523593
SMLoc());
524594
sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
525595

526-
// Create a context just for the current buffer. Disable threading on creation
527-
// since we'll inject the thread-pool separately.
596+
// Create a context just for the current buffer. Disable threading on
597+
// creation since we'll inject the thread-pool separately.
528598
MLIRContext context(registry, MLIRContext::Threading::DISABLED);
529599
if (threadPool)
530600
context.setThreadPool(*threadPool);
@@ -669,9 +739,9 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv,
669739
if (config.shouldListPasses())
670740
return printRegisteredPassesAndReturn();
671741

672-
// When reading from stdin and the input is a tty, it is often a user mistake
673-
// and the process "appears to be stuck". Print a message to let the user know
674-
// about it!
742+
// When reading from stdin and the input is a tty, it is often a user
743+
// mistake and the process "appears to be stuck". Print a message to let the
744+
// user know about it!
675745
if (inputFilename == "-" &&
676746
sys::Process::FileDescriptorIsDisplayed(fileno(stdin)))
677747
llvm::errs() << "(processing input from stdin now, hit ctrl-c/ctrl-d to "

mlir/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ add_mlir_unittest(MLIRIRTests
1414
MemrefLayoutTest.cpp
1515
OperationSupportTest.cpp
1616
PatternMatchTest.cpp
17-
RemarkTest.cpp
17+
RemarkTest.cpp
1818
ShapedTypeTest.cpp
1919
SymbolTableTest.cpp
2020
TypeTest.cpp

0 commit comments

Comments
 (0)