2019-09-09 10:57:25 +08:00
|
|
|
//===- TestPassManager.cpp - Test pass manager functionality --------------===//
|
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
// 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
|
2019-09-09 10:57:25 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-09-09 10:57:25 +08:00
|
|
|
|
|
|
|
#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 {}
|
|
|
|
};
|
2019-10-09 09:23:13 +08:00
|
|
|
class TestOptionsPass : public FunctionPass<TestOptionsPass> {
|
|
|
|
public:
|
2019-12-24 07:54:55 +08:00
|
|
|
struct Options : public PassPipelineOptions<Options> {
|
|
|
|
ListOption<int> listOption{*this, "list",
|
|
|
|
llvm::cl::MiscFlags::CommaSeparated,
|
|
|
|
llvm::cl::desc("Example list option")};
|
|
|
|
ListOption<std::string> stringListOption{
|
2019-10-09 12:15:49 +08:00
|
|
|
*this, "string-list", llvm::cl::MiscFlags::CommaSeparated,
|
|
|
|
llvm::cl::desc("Example string list option")};
|
2019-10-09 09:23:13 +08:00
|
|
|
Option<std::string> stringOption{*this, "string",
|
|
|
|
llvm::cl::desc("Example string option")};
|
|
|
|
};
|
2019-12-24 07:54:55 +08:00
|
|
|
TestOptionsPass() = default;
|
|
|
|
TestOptionsPass(const TestOptionsPass &) {}
|
2019-10-09 09:23:13 +08:00
|
|
|
TestOptionsPass(const Options &options) {
|
2019-12-24 07:54:55 +08:00
|
|
|
listOption->assign(options.listOption.begin(), options.listOption.end());
|
|
|
|
stringOption.setValue(options.stringOption);
|
|
|
|
stringListOption->assign(options.stringListOption.begin(),
|
|
|
|
options.stringListOption.end());
|
2019-10-09 09:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runOnFunction() final {}
|
|
|
|
|
2019-12-24 07:54:55 +08:00
|
|
|
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")};
|
2019-10-09 09:23:13 +08:00
|
|
|
};
|
2019-10-11 10:19:11 +08:00
|
|
|
|
|
|
|
/// 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(); }
|
|
|
|
};
|
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; });
|
|
|
|
}
|
|
|
|
};
|
2019-10-11 10:19:11 +08:00
|
|
|
} // end anonymous namespace
|
2019-09-09 10:57:25 +08:00
|
|
|
|
2019-09-14 03:09:50 +08:00
|
|
|
static void testNestedPipeline(OpPassManager &pm) {
|
2019-09-09 10:57:25 +08:00
|
|
|
// 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>());
|
|
|
|
}
|
|
|
|
|
2019-09-14 08:53:28 +08:00
|
|
|
static void testNestedPipelineTextual(OpPassManager &pm) {
|
|
|
|
(void)parsePassPipeline("test-pm-nested-pipeline", pm);
|
|
|
|
}
|
|
|
|
|
2019-12-24 07:54:55 +08:00
|
|
|
static PassRegistration<TestOptionsPass>
|
2019-10-09 09:23:13 +08:00
|
|
|
reg("test-options-pass", "Test options parsing capabilities");
|
|
|
|
|
2019-09-14 03:09:50 +08:00
|
|
|
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");
|
|
|
|
|
2019-10-11 10:19:11 +08:00
|
|
|
static PassRegistration<TestCrashRecoveryPass>
|
|
|
|
unusedCrashP("test-pass-crash",
|
|
|
|
"Test a pass in the pass manager that always crashes");
|
|
|
|
|
2019-12-06 03:52:58 +08:00
|
|
|
static PassRegistration<TestStatisticPass> unusedStatP("test-stats-pass",
|
|
|
|
"Test pass statistics");
|
|
|
|
|
2019-10-09 09:23:13 +08:00
|
|
|
static PassPipelineRegistration<>
|
2019-09-09 10:57:25 +08:00
|
|
|
unused("test-pm-nested-pipeline",
|
|
|
|
"Test a nested pipeline in the pass manager", testNestedPipeline);
|
2019-10-09 09:23:13 +08:00
|
|
|
static PassPipelineRegistration<>
|
2019-09-14 08:53:28 +08:00
|
|
|
unusedTextual("test-textual-pm-nested-pipeline",
|
|
|
|
"Test a nested pipeline in the pass manager",
|
|
|
|
testNestedPipelineTextual);
|
2019-10-10 04:48:38 +08:00
|
|
|
static PassPipelineRegistration<>
|
|
|
|
unusedDump("test-dump-pipeline",
|
|
|
|
"Dumps the pipeline build so far for debugging purposes",
|
|
|
|
[](OpPassManager &pm) {
|
|
|
|
pm.printAsTextualPipeline(llvm::errs());
|
|
|
|
llvm::errs() << "\n";
|
|
|
|
});
|
2019-10-09 09:23:13 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
});
|