llvm-project/mlir/test/lib/Pass/TestPassManager.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
5.0 KiB
C++
Raw Normal View History

//===- TestPassManager.cpp - Test pass manager functionality --------------===//
//
// Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/Function.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
using namespace mlir;
namespace {
struct TestModulePass : public ModulePass<TestModulePass> {
void runOnModule() final {}
};
struct TestFunctionPass : public FunctionPass<TestFunctionPass> {
void runOnFunction() final {}
};
class TestOptionsPass : public FunctionPass<TestOptionsPass> {
public:
struct Options : public PassPipelineOptions<Options> {
ListOption<int> listOption{*this, "list",
llvm::cl::MiscFlags::CommaSeparated,
llvm::cl::desc("Example list option")};
ListOption<std::string> stringListOption{
*this, "string-list", llvm::cl::MiscFlags::CommaSeparated,
llvm::cl::desc("Example string list option")};
Option<std::string> stringOption{*this, "string",
llvm::cl::desc("Example string option")};
};
TestOptionsPass() = default;
TestOptionsPass(const TestOptionsPass &) {}
TestOptionsPass(const Options &options) {
listOption->assign(options.listOption.begin(), options.listOption.end());
stringOption.setValue(options.stringOption);
stringListOption->assign(options.stringListOption.begin(),
options.stringListOption.end());
}
void runOnFunction() final {}
ListOption<int> listOption{*this, "list", llvm::cl::MiscFlags::CommaSeparated,
llvm::cl::desc("Example list option")};
ListOption<std::string> stringListOption{
*this, "string-list", llvm::cl::MiscFlags::CommaSeparated,
llvm::cl::desc("Example string list option")};
Option<std::string> stringOption{*this, "string",
llvm::cl::desc("Example string option")};
};
/// A test pass that always aborts to enable testing the crash recovery
/// mechanism of the pass manager.
class TestCrashRecoveryPass : public OperationPass<TestCrashRecoveryPass> {
void runOnOperation() final { abort(); }
};
Add support for instance specific pass statistics. Statistics are a way to keep track of what the compiler is doing and how effective various optimizations are. It is useful to see what optimizations are contributing to making a particular program run faster. Pass-instance specific statistics take this even further as you can see the effect of placing a particular pass at specific places within the pass pipeline, e.g. they could help answer questions like "what happens if I run CSE again here". Statistics can be added to a pass by simply adding members of type 'Pass::Statistics'. This class takes as a constructor arguments: the parent pass pointer, a name, and a description. Statistics can be dumped by the pass manager in a similar manner to how pass timing information is dumped, i.e. via PassManager::enableStatistics programmatically; or -pass-statistics and -pass-statistics-display via the command line pass manager options. Below is an example: struct MyPass : public OperationPass<MyPass> { Statistic testStat{this, "testStat", "A test statistic"}; void runOnOperation() { ... ++testStat; ... } }; $ mlir-opt -pass-pipeline='func(my-pass,my-pass)' foo.mlir -pass-statistics Pipeline Display: ===-------------------------------------------------------------------------=== ... Pass statistics report ... ===-------------------------------------------------------------------------=== 'func' Pipeline MyPass (S) 15 testStat - A test statistic MyPass (S) 6 testStat - A test statistic List Display: ===-------------------------------------------------------------------------=== ... Pass statistics report ... ===-------------------------------------------------------------------------=== MyPass (S) 21 testStat - A test statistic PiperOrigin-RevId: 284022014
2019-12-06 03:52:58 +08:00
/// A test pass that contains a statistic.
struct TestStatisticPass : public OperationPass<TestStatisticPass> {
TestStatisticPass() = default;
TestStatisticPass(const TestStatisticPass &) {}
Statistic opCount{this, "num-ops", "Number of operations counted"};
void runOnOperation() final {
getOperation()->walk([&](Operation *) { ++opCount; });
}
};
} // end anonymous namespace
static void testNestedPipeline(OpPassManager &pm) {
// Nest a module pipeline that contains:
/// A module pass.
auto &modulePM = pm.nest<ModuleOp>();
modulePM.addPass(std::make_unique<TestModulePass>());
/// A nested function pass.
auto &nestedFunctionPM = modulePM.nest<FuncOp>();
nestedFunctionPM.addPass(std::make_unique<TestFunctionPass>());
// Nest a function pipeline that contains a single pass.
auto &functionPM = pm.nest<FuncOp>();
functionPM.addPass(std::make_unique<TestFunctionPass>());
}
static void testNestedPipelineTextual(OpPassManager &pm) {
(void)parsePassPipeline("test-pm-nested-pipeline", pm);
}
static PassRegistration<TestOptionsPass>
reg("test-options-pass", "Test options parsing capabilities");
static PassRegistration<TestModulePass>
unusedMP("test-module-pass", "Test a module pass in the pass manager");
static PassRegistration<TestFunctionPass>
unusedFP("test-function-pass", "Test a function pass in the pass manager");
static PassRegistration<TestCrashRecoveryPass>
unusedCrashP("test-pass-crash",
"Test a pass in the pass manager that always crashes");
Add support for instance specific pass statistics. Statistics are a way to keep track of what the compiler is doing and how effective various optimizations are. It is useful to see what optimizations are contributing to making a particular program run faster. Pass-instance specific statistics take this even further as you can see the effect of placing a particular pass at specific places within the pass pipeline, e.g. they could help answer questions like "what happens if I run CSE again here". Statistics can be added to a pass by simply adding members of type 'Pass::Statistics'. This class takes as a constructor arguments: the parent pass pointer, a name, and a description. Statistics can be dumped by the pass manager in a similar manner to how pass timing information is dumped, i.e. via PassManager::enableStatistics programmatically; or -pass-statistics and -pass-statistics-display via the command line pass manager options. Below is an example: struct MyPass : public OperationPass<MyPass> { Statistic testStat{this, "testStat", "A test statistic"}; void runOnOperation() { ... ++testStat; ... } }; $ mlir-opt -pass-pipeline='func(my-pass,my-pass)' foo.mlir -pass-statistics Pipeline Display: ===-------------------------------------------------------------------------=== ... Pass statistics report ... ===-------------------------------------------------------------------------=== 'func' Pipeline MyPass (S) 15 testStat - A test statistic MyPass (S) 6 testStat - A test statistic List Display: ===-------------------------------------------------------------------------=== ... Pass statistics report ... ===-------------------------------------------------------------------------=== MyPass (S) 21 testStat - A test statistic PiperOrigin-RevId: 284022014
2019-12-06 03:52:58 +08:00
static PassRegistration<TestStatisticPass> unusedStatP("test-stats-pass",
"Test pass statistics");
static PassPipelineRegistration<>
unused("test-pm-nested-pipeline",
"Test a nested pipeline in the pass manager", testNestedPipeline);
static PassPipelineRegistration<>
unusedTextual("test-textual-pm-nested-pipeline",
"Test a nested pipeline in the pass manager",
testNestedPipelineTextual);
static PassPipelineRegistration<>
unusedDump("test-dump-pipeline",
"Dumps the pipeline build so far for debugging purposes",
[](OpPassManager &pm) {
pm.printAsTextualPipeline(llvm::errs());
llvm::errs() << "\n";
});
static PassPipelineRegistration<TestOptionsPass::Options>
registerOptionsPassPipeline(
"test-options-pass-pipeline",
"Parses options using pass pipeline registration",
[](OpPassManager &pm, const TestOptionsPass::Options &options) {
pm.addPass(std::make_unique<TestOptionsPass>(options));
});