Skip to content
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
3 changes: 3 additions & 0 deletions mlir/include/mlir/Pass/PassRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ using PassAllocatorFunction = std::function<std::unique_ptr<Pass>()>;
// PassRegistry
//===----------------------------------------------------------------------===//

/// Prints the passes that were previously registered and stored in passRegistry
void printRegisteredPasses();

/// Structure to group information about a passes and pass pipelines (argument
/// to invoke via mlir-opt, description, pass pipeline builder).
class PassRegistryEntry {
Expand Down
10 changes: 10 additions & 0 deletions mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class MlirOptMainConfig {
return success();
}

/// List the registered passes and return.
MlirOptMainConfig &listPasses(bool list) {
listPassesFlag = list;
return *this;
}
bool shouldListPasses() const { return listPassesFlag; }

/// Enable running the reproducer information stored in resources (if
/// present).
MlirOptMainConfig &runReproducer(bool enableReproducer) {
Expand Down Expand Up @@ -209,6 +216,9 @@ class MlirOptMainConfig {
/// The callback to populate the pass manager.
std::function<LogicalResult(PassManager &)> passPipelineCallback;

/// List the registered passes and return.
bool listPassesFlag = false;

/// Enable running the reproducer.
bool runReproducerFlag = false;

Expand Down
26 changes: 26 additions & 0 deletions mlir/lib/Pass/PassRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ static void printOptionHelp(StringRef arg, StringRef desc, size_t indent,
// PassRegistry
//===----------------------------------------------------------------------===//

/// Prints the passes that were previously registered and stored in passRegistry
void mlir::printRegisteredPasses() {
size_t maxWidth = 0;
for (auto &entry : *passRegistry)
maxWidth = std::max(maxWidth, entry.second.getOptionWidth() + 4);

// Functor used to print the ordered entries of a registration map.
auto printOrderedEntries = [&](StringRef header, auto &map) {
llvm::SmallVector<PassRegistryEntry *, 32> orderedEntries;
for (auto &kv : map)
orderedEntries.push_back(&kv.second);
llvm::array_pod_sort(
orderedEntries.begin(), orderedEntries.end(),
[](PassRegistryEntry *const *lhs, PassRegistryEntry *const *rhs) {
return (*lhs)->getPassArgument().compare((*rhs)->getPassArgument());
});

llvm::outs().indent(0) << header << ":\n";
for (PassRegistryEntry *entry : orderedEntries)
entry->printHelpStr(/*indent=*/2, maxWidth);
};

// Print the available passes.
printOrderedEntries("Passes", *passRegistry);
}

/// Print the help information for this pass. This includes the argument,
/// description, and any pass options. `descIndent` is the indent that the
/// descriptions should be aligned.
Expand Down
16 changes: 16 additions & 0 deletions mlir/lib/Tools/mlir-opt/MlirOptMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "mlir/Parser/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/Timing.h"
#include "mlir/Support/ToolUtilities.h"
Expand Down Expand Up @@ -118,6 +119,10 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
"parsing"),
cl::location(useExplicitModuleFlag), cl::init(false));

static cl::opt<bool, /*ExternalStorage=*/true> listPasses(
"list-passes", cl::desc("Print the list of registered passes and exit"),
cl::location(listPassesFlag), cl::init(false));

static cl::opt<bool, /*ExternalStorage=*/true> runReproducer(
"run-reproducer", cl::desc("Run the pipeline stored in the reproducer"),
cl::location(runReproducerFlag), cl::init(false));
Expand Down Expand Up @@ -501,6 +506,11 @@ mlir::registerAndParseCLIOptions(int argc, char **argv,
return std::make_pair(inputFilename.getValue(), outputFilename.getValue());
}

static LogicalResult printRegisteredPassesAndReturn() {
mlir::printRegisteredPasses();
return success();
}

LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
std::unique_ptr<llvm::MemoryBuffer> buffer,
DialectRegistry &registry,
Expand All @@ -511,6 +521,9 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
llvm::outs() << "\n";
}

if (config.shouldListPasses())
return printRegisteredPassesAndReturn();

// The split-input-file mode is a very specific mode that slices the file
// up into small pieces and checks each independently.
// We use an explicit threadpool to avoid creating and joining/destroying
Expand Down Expand Up @@ -572,6 +585,9 @@ LogicalResult mlir::MlirOptMain(int argc, char **argv, llvm::StringRef toolName,
cl::ParseCommandLineOptions(argc, argv, helpHeader);
MlirOptMainConfig config = MlirOptMainConfig::createFromCLOptions();

if (config.shouldListPasses())
return printRegisteredPassesAndReturn();

// When reading from stdin and the input is a tty, it is often a user mistake
// and the process "appears to be stuck". Print a message to let the user know
// about it!
Expand Down