1 //===- TestPassManager.cpp - Test pass manager functionality --------------===//
2 //
3 // Copyright 2019 The MLIR Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // =============================================================================
17 
18 #include "mlir/IR/Function.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Pass/PassManager.h"
21 
22 using namespace mlir;
23 
24 namespace {
25 struct TestModulePass : public ModulePass<TestModulePass> {
26   void runOnModule() final {}
27 };
28 struct TestFunctionPass : public FunctionPass<TestFunctionPass> {
29   void runOnFunction() final {}
30 };
31 } // namespace
32 
33 static void testNestedPipeline(OpPassManager &pm) {
34   // Nest a module pipeline that contains:
35   /// A module pass.
36   auto &modulePM = pm.nest<ModuleOp>();
37   modulePM.addPass(std::make_unique<TestModulePass>());
38   /// A nested function pass.
39   auto &nestedFunctionPM = modulePM.nest<FuncOp>();
40   nestedFunctionPM.addPass(std::make_unique<TestFunctionPass>());
41 
42   // Nest a function pipeline that contains a single pass.
43   auto &functionPM = pm.nest<FuncOp>();
44   functionPM.addPass(std::make_unique<TestFunctionPass>());
45 }
46 
47 static void testNestedPipelineTextual(OpPassManager &pm) {
48   (void)parsePassPipeline("test-pm-nested-pipeline", pm);
49 }
50 
51 static PassRegistration<TestModulePass>
52     unusedMP("test-module-pass", "Test a module pass in the pass manager");
53 static PassRegistration<TestFunctionPass>
54     unusedFP("test-function-pass", "Test a function pass in the pass manager");
55 
56 static PassPipelineRegistration
57     unused("test-pm-nested-pipeline",
58            "Test a nested pipeline in the pass manager", testNestedPipeline);
59 static PassPipelineRegistration
60     unusedTextual("test-textual-pm-nested-pipeline",
61                   "Test a nested pipeline in the pass manager",
62                   testNestedPipelineTextual);
63