| 1 | //===- TestPassManager.cpp - Test pass manager functionality --------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestOpsCore.h" |
| 10 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 11 | #include "mlir/IR/BuiltinOps.h" |
| 12 | #include "mlir/Pass/Pass.h" |
| 13 | #include "mlir/Pass/PassManager.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | namespace { |
| 18 | struct TestModulePass |
| 19 | : public PassWrapper<TestModulePass, OperationPass<ModuleOp>> { |
| 20 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestModulePass) |
| 21 | |
| 22 | void runOnOperation() final {} |
| 23 | StringRef getArgument() const final { return "test-module-pass" ; } |
| 24 | StringRef getDescription() const final { |
| 25 | return "Test a module pass in the pass manager" ; |
| 26 | } |
| 27 | }; |
| 28 | struct TestFunctionPass |
| 29 | : public PassWrapper<TestFunctionPass, OperationPass<func::FuncOp>> { |
| 30 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFunctionPass) |
| 31 | |
| 32 | void runOnOperation() final {} |
| 33 | StringRef getArgument() const final { return "test-function-pass" ; } |
| 34 | StringRef getDescription() const final { |
| 35 | return "Test a function pass in the pass manager" ; |
| 36 | } |
| 37 | }; |
| 38 | struct TestInterfacePass |
| 39 | : public PassWrapper<TestInterfacePass, |
| 40 | InterfacePass<FunctionOpInterface>> { |
| 41 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestInterfacePass) |
| 42 | |
| 43 | void runOnOperation() final { |
| 44 | getOperation()->emitRemark() << "Executing interface pass on operation" ; |
| 45 | } |
| 46 | StringRef getArgument() const final { return "test-interface-pass" ; } |
| 47 | StringRef getDescription() const final { |
| 48 | return "Test an interface pass (running on FunctionOpInterface) in the " |
| 49 | "pass manager" ; |
| 50 | } |
| 51 | }; |
| 52 | struct TestOptionsPass |
| 53 | : public PassWrapper<TestOptionsPass, OperationPass<func::FuncOp>> { |
| 54 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOptionsPass) |
| 55 | |
| 56 | enum Enum { Zero, One, Two }; |
| 57 | |
| 58 | struct Options : public PassPipelineOptions<Options> { |
| 59 | ListOption<int> listOption{*this, "list" , |
| 60 | llvm::cl::desc("Example list option" )}; |
| 61 | ListOption<std::string> stringListOption{ |
| 62 | *this, "string-list" , llvm::cl::desc("Example string list option" )}; |
| 63 | Option<std::string> stringOption{*this, "string" , |
| 64 | llvm::cl::desc("Example string option" )}; |
| 65 | Option<Enum> enumOption{ |
| 66 | *this, "enum" , llvm::cl::desc("Example enum option" ), |
| 67 | llvm::cl::values(clEnumValN(0, "zero" , "Example zero value" ), |
| 68 | clEnumValN(1, "one" , "Example one value" ), |
| 69 | clEnumValN(2, "two" , "Example two value" ))}; |
| 70 | |
| 71 | Options() = default; |
| 72 | Options(const Options &rhs) { *this = rhs; } |
| 73 | Options &operator=(const Options &rhs) { |
| 74 | copyOptionValuesFrom(other: rhs); |
| 75 | return *this; |
| 76 | } |
| 77 | }; |
| 78 | TestOptionsPass() = default; |
| 79 | TestOptionsPass(const TestOptionsPass &) : PassWrapper() {} |
| 80 | TestOptionsPass(const Options &options) { |
| 81 | listOption = options.listOption; |
| 82 | stringOption = options.stringOption; |
| 83 | stringListOption = options.stringListOption; |
| 84 | enumOption = options.enumOption; |
| 85 | } |
| 86 | |
| 87 | void runOnOperation() final {} |
| 88 | StringRef getArgument() const final { return "test-options-pass" ; } |
| 89 | StringRef getDescription() const final { |
| 90 | return "Test options parsing capabilities" ; |
| 91 | } |
| 92 | |
| 93 | ListOption<int> listOption{*this, "list" , |
| 94 | llvm::cl::desc("Example list option" )}; |
| 95 | ListOption<std::string> stringListOption{ |
| 96 | *this, "string-list" , llvm::cl::desc("Example string list option" )}; |
| 97 | Option<std::string> stringOption{*this, "string" , |
| 98 | llvm::cl::desc("Example string option" )}; |
| 99 | Option<Enum> enumOption{ |
| 100 | *this, "enum" , llvm::cl::desc("Example enum option" ), |
| 101 | llvm::cl::values(clEnumValN(0, "zero" , "Example zero value" ), |
| 102 | clEnumValN(1, "one" , "Example one value" ), |
| 103 | clEnumValN(2, "two" , "Example two value" ))}; |
| 104 | }; |
| 105 | |
| 106 | struct TestOptionsSuperPass |
| 107 | : public PassWrapper<TestOptionsSuperPass, OperationPass<func::FuncOp>> { |
| 108 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOptionsSuperPass) |
| 109 | |
| 110 | struct Options : public PassPipelineOptions<Options> { |
| 111 | ListOption<TestOptionsPass::Options> listOption{ |
| 112 | *this, "super-list" , |
| 113 | llvm::cl::desc("Example list of PassPipelineOptions option" )}; |
| 114 | |
| 115 | Options() = default; |
| 116 | }; |
| 117 | |
| 118 | TestOptionsSuperPass() = default; |
| 119 | TestOptionsSuperPass(const TestOptionsSuperPass &) : PassWrapper() {} |
| 120 | TestOptionsSuperPass(const Options &options) { |
| 121 | listOption = options.listOption; |
| 122 | } |
| 123 | |
| 124 | void runOnOperation() final {} |
| 125 | StringRef getArgument() const final { return "test-options-super-pass" ; } |
| 126 | StringRef getDescription() const final { |
| 127 | return "Test options of options parsing capabilities" ; |
| 128 | } |
| 129 | |
| 130 | ListOption<TestOptionsPass::Options> listOption{ |
| 131 | *this, "list" , |
| 132 | llvm::cl::desc("Example list of PassPipelineOptions option" )}; |
| 133 | }; |
| 134 | |
| 135 | struct TestOptionsPassA |
| 136 | : public PassWrapper<TestOptionsPassA, OperationPass<func::FuncOp>> { |
| 137 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOptionsPassA) |
| 138 | |
| 139 | struct Options : public PassPipelineOptions<Options> { |
| 140 | Option<bool> foo{*this, "foo" , llvm::cl::desc("Example boolean option" )}; |
| 141 | }; |
| 142 | |
| 143 | TestOptionsPassA() = default; |
| 144 | TestOptionsPassA(const TestOptionsPassA &) : PassWrapper() {} |
| 145 | TestOptionsPassA(const Options &options) { this->options.foo = options.foo; } |
| 146 | |
| 147 | void runOnOperation() final {} |
| 148 | StringRef getArgument() const final { return "test-options-pass-a" ; } |
| 149 | StringRef getDescription() const final { |
| 150 | return "Test superset options parsing capabilities - subset A" ; |
| 151 | } |
| 152 | |
| 153 | Options options; |
| 154 | }; |
| 155 | |
| 156 | struct TestOptionsPassB |
| 157 | : public PassWrapper<TestOptionsPassB, OperationPass<func::FuncOp>> { |
| 158 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOptionsPassB) |
| 159 | |
| 160 | struct Options : public PassPipelineOptions<Options> { |
| 161 | Option<bool> bar{*this, "bar" , llvm::cl::desc("Example boolean option" )}; |
| 162 | }; |
| 163 | |
| 164 | TestOptionsPassB() = default; |
| 165 | TestOptionsPassB(const TestOptionsPassB &) : PassWrapper() {} |
| 166 | TestOptionsPassB(const Options &options) { this->options.bar = options.bar; } |
| 167 | |
| 168 | void runOnOperation() final {} |
| 169 | StringRef getArgument() const final { return "test-options-pass-b" ; } |
| 170 | StringRef getDescription() const final { |
| 171 | return "Test superset options parsing capabilities - subset B" ; |
| 172 | } |
| 173 | |
| 174 | Options options; |
| 175 | }; |
| 176 | |
| 177 | struct TestPipelineOptionsSuperSetAB : TestOptionsPassA::Options, |
| 178 | TestOptionsPassB::Options {}; |
| 179 | |
| 180 | /// A test pass that always aborts to enable testing the crash recovery |
| 181 | /// mechanism of the pass manager. |
| 182 | struct TestCrashRecoveryPass |
| 183 | : public PassWrapper<TestCrashRecoveryPass, OperationPass<>> { |
| 184 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestCrashRecoveryPass) |
| 185 | |
| 186 | void runOnOperation() final { abort(); } |
| 187 | StringRef getArgument() const final { return "test-pass-crash" ; } |
| 188 | StringRef getDescription() const final { |
| 189 | return "Test a pass in the pass manager that always crashes" ; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | /// A test pass that always fails to enable testing the failure recovery |
| 194 | /// mechanisms of the pass manager. |
| 195 | struct TestFailurePass : public PassWrapper<TestFailurePass, OperationPass<>> { |
| 196 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFailurePass) |
| 197 | |
| 198 | TestFailurePass() = default; |
| 199 | TestFailurePass(const TestFailurePass &other) : PassWrapper(other) {} |
| 200 | |
| 201 | void runOnOperation() final { |
| 202 | signalPassFailure(); |
| 203 | if (genDiagnostics) |
| 204 | mlir::emitError(loc: getOperation()->getLoc(), message: "illegal operation" ); |
| 205 | } |
| 206 | StringRef getArgument() const final { return "test-pass-failure" ; } |
| 207 | StringRef getDescription() const final { |
| 208 | return "Test a pass in the pass manager that always fails" ; |
| 209 | } |
| 210 | |
| 211 | Option<bool> genDiagnostics{*this, "gen-diagnostics" , |
| 212 | llvm::cl::desc("Generate a diagnostic message" )}; |
| 213 | }; |
| 214 | |
| 215 | /// A test pass that creates an invalid operation in a function body. |
| 216 | struct TestInvalidIRPass |
| 217 | : public PassWrapper<TestInvalidIRPass, |
| 218 | InterfacePass<FunctionOpInterface>> { |
| 219 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestInvalidIRPass) |
| 220 | |
| 221 | TestInvalidIRPass() = default; |
| 222 | TestInvalidIRPass(const TestInvalidIRPass &other) : PassWrapper(other) {} |
| 223 | |
| 224 | StringRef getArgument() const final { return "test-pass-create-invalid-ir" ; } |
| 225 | StringRef getDescription() const final { |
| 226 | return "Test pass that adds an invalid operation in a function body" ; |
| 227 | } |
| 228 | void getDependentDialects(DialectRegistry ®istry) const final { |
| 229 | registry.insert<test::TestDialect>(); |
| 230 | } |
| 231 | void runOnOperation() final { |
| 232 | if (signalFailure) |
| 233 | signalPassFailure(); |
| 234 | if (!emitInvalidIR) |
| 235 | return; |
| 236 | OpBuilder b(getOperation().getFunctionBody()); |
| 237 | OperationState state(b.getUnknownLoc(), "test.any_attr_of_i32_str" ); |
| 238 | b.create(state); |
| 239 | } |
| 240 | Option<bool> signalFailure{*this, "signal-pass-failure" , |
| 241 | llvm::cl::desc("Trigger a pass failure" )}; |
| 242 | Option<bool> emitInvalidIR{*this, "emit-invalid-ir" , llvm::cl::init(Val: true), |
| 243 | llvm::cl::desc("Emit invalid IR" )}; |
| 244 | }; |
| 245 | |
| 246 | /// A test pass that always fails to enable testing the failure recovery |
| 247 | /// mechanisms of the pass manager. |
| 248 | struct TestInvalidParentPass |
| 249 | : public PassWrapper<TestInvalidParentPass, |
| 250 | InterfacePass<FunctionOpInterface>> { |
| 251 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestInvalidParentPass) |
| 252 | |
| 253 | StringRef getArgument() const final { return "test-pass-invalid-parent" ; } |
| 254 | StringRef getDescription() const final { |
| 255 | return "Test a pass in the pass manager that makes the parent operation " |
| 256 | "invalid" ; |
| 257 | } |
| 258 | void getDependentDialects(DialectRegistry ®istry) const final { |
| 259 | registry.insert<test::TestDialect>(); |
| 260 | } |
| 261 | void runOnOperation() final { |
| 262 | FunctionOpInterface op = getOperation(); |
| 263 | OpBuilder b(op.getFunctionBody()); |
| 264 | test::TestCallOp::create(b, op.getLoc(), TypeRange(), "some_unknown_func" , |
| 265 | ValueRange()); |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | /// A test pass that contains a statistic. |
| 270 | struct TestStatisticPass |
| 271 | : public PassWrapper<TestStatisticPass, OperationPass<>> { |
| 272 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestStatisticPass) |
| 273 | |
| 274 | TestStatisticPass() = default; |
| 275 | TestStatisticPass(const TestStatisticPass &) : PassWrapper() {} |
| 276 | StringRef getArgument() const final { return "test-stats-pass" ; } |
| 277 | StringRef getDescription() const final { return "Test pass statistics" ; } |
| 278 | |
| 279 | // Use a couple of statistics to verify their ordering |
| 280 | // in the print out. The statistics are registered in the order |
| 281 | // of construction, so put "num-ops2" before "num-ops" and |
| 282 | // make sure that the order is reversed. |
| 283 | Statistic opCountDuplicate{this, "num-ops2" , |
| 284 | "Number of operations counted one more time" }; |
| 285 | Statistic opCount{this, "num-ops" , "Number of operations counted" }; |
| 286 | |
| 287 | void runOnOperation() final { |
| 288 | getOperation()->walk(callback: [&](Operation *) { ++opCount; }); |
| 289 | getOperation()->walk(callback: [&](Operation *) { ++opCountDuplicate; }); |
| 290 | } |
| 291 | }; |
| 292 | } // namespace |
| 293 | |
| 294 | static void testNestedPipeline(OpPassManager &pm) { |
| 295 | // Nest a module pipeline that contains: |
| 296 | /// A module pass. |
| 297 | auto &modulePM = pm.nest<ModuleOp>(); |
| 298 | modulePM.addPass(pass: std::make_unique<TestModulePass>()); |
| 299 | /// A nested function pass. |
| 300 | auto &nestedFunctionPM = modulePM.nest<func::FuncOp>(); |
| 301 | nestedFunctionPM.addPass(pass: std::make_unique<TestFunctionPass>()); |
| 302 | |
| 303 | // Nest a function pipeline that contains a single pass. |
| 304 | auto &functionPM = pm.nest<func::FuncOp>(); |
| 305 | functionPM.addPass(pass: std::make_unique<TestFunctionPass>()); |
| 306 | } |
| 307 | |
| 308 | static void testNestedPipelineTextual(OpPassManager &pm) { |
| 309 | (void)parsePassPipeline(pipeline: "test-pm-nested-pipeline" , pm); |
| 310 | } |
| 311 | |
| 312 | namespace mlir { |
| 313 | void registerPassManagerTestPass() { |
| 314 | PassRegistration<TestOptionsPass>(); |
| 315 | PassRegistration<TestOptionsSuperPass>(); |
| 316 | |
| 317 | PassRegistration<TestOptionsPassA>(); |
| 318 | PassRegistration<TestOptionsPassB>(); |
| 319 | |
| 320 | PassRegistration<TestModulePass>(); |
| 321 | |
| 322 | PassRegistration<TestFunctionPass>(); |
| 323 | |
| 324 | PassRegistration<TestInterfacePass>(); |
| 325 | |
| 326 | PassRegistration<TestCrashRecoveryPass>(); |
| 327 | PassRegistration<TestFailurePass>(); |
| 328 | PassRegistration<TestInvalidIRPass>(); |
| 329 | PassRegistration<TestInvalidParentPass>(); |
| 330 | |
| 331 | PassRegistration<TestStatisticPass>(); |
| 332 | |
| 333 | PassPipelineRegistration<>("test-pm-nested-pipeline" , |
| 334 | "Test a nested pipeline in the pass manager" , |
| 335 | testNestedPipeline); |
| 336 | PassPipelineRegistration<>("test-textual-pm-nested-pipeline" , |
| 337 | "Test a nested pipeline in the pass manager" , |
| 338 | testNestedPipelineTextual); |
| 339 | |
| 340 | PassPipelineRegistration<TestOptionsPass::Options> |
| 341 | registerOptionsPassPipeline( |
| 342 | "test-options-pass-pipeline" , |
| 343 | "Parses options using pass pipeline registration" , |
| 344 | [](OpPassManager &pm, const TestOptionsPass::Options &options) { |
| 345 | pm.addPass(pass: std::make_unique<TestOptionsPass>(args: options)); |
| 346 | }); |
| 347 | |
| 348 | PassPipelineRegistration<TestOptionsSuperPass::Options> |
| 349 | registerOptionsSuperPassPipeline( |
| 350 | "test-options-super-pass-pipeline" , |
| 351 | "Parses options of PassPipelineOptions using pass pipeline " |
| 352 | "registration" , |
| 353 | [](OpPassManager &pm, const TestOptionsSuperPass::Options &options) { |
| 354 | pm.addPass(pass: std::make_unique<TestOptionsSuperPass>(args: options)); |
| 355 | }); |
| 356 | |
| 357 | PassPipelineRegistration<TestPipelineOptionsSuperSetAB> |
| 358 | registerPipelineOptionsSuperSetABPipeline( |
| 359 | "test-options-super-set-ab-pipeline" , |
| 360 | "Parses options of PassPipelineOptions using pass pipeline " |
| 361 | "registration" , |
| 362 | [](OpPassManager &pm, const TestPipelineOptionsSuperSetAB &options) { |
| 363 | // Pass superset AB options to subset options A and B |
| 364 | pm.addPass(pass: std::make_unique<TestOptionsPassA>(args: options)); |
| 365 | pm.addPass(pass: std::make_unique<TestOptionsPassB>(args: options)); |
| 366 | }); |
| 367 | } |
| 368 | } // namespace mlir |
| 369 | |