1c484c7ddSChia-hung Duan //===- OptReductionPass.cpp - Optimization Reduction Pass Wrapper ---------===//
2c484c7ddSChia-hung Duan //
3c484c7ddSChia-hung Duan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c484c7ddSChia-hung Duan // See https://llvm.org/LICENSE.txt for license information.
5c484c7ddSChia-hung Duan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c484c7ddSChia-hung Duan //
7c484c7ddSChia-hung Duan //===----------------------------------------------------------------------===//
8c484c7ddSChia-hung Duan //
9c484c7ddSChia-hung Duan // This file defines the Opt Reduction Pass Wrapper. It creates a MLIR pass to
10c484c7ddSChia-hung Duan // run any optimization pass within it and only replaces the output module with
11c484c7ddSChia-hung Duan // the transformed version if it is smaller and interesting.
12c484c7ddSChia-hung Duan //
13c484c7ddSChia-hung Duan //===----------------------------------------------------------------------===//
14c484c7ddSChia-hung Duan 
15c484c7ddSChia-hung Duan #include "mlir/Pass/PassManager.h"
16c484c7ddSChia-hung Duan #include "mlir/Pass/PassRegistry.h"
17c484c7ddSChia-hung Duan #include "mlir/Reducer/PassDetail.h"
18c484c7ddSChia-hung Duan #include "mlir/Reducer/Passes.h"
19c484c7ddSChia-hung Duan #include "mlir/Reducer/Tester.h"
20c484c7ddSChia-hung Duan #include "llvm/Support/Debug.h"
21c484c7ddSChia-hung Duan 
22c484c7ddSChia-hung Duan #define DEBUG_TYPE "mlir-reduce"
23c484c7ddSChia-hung Duan 
24c484c7ddSChia-hung Duan using namespace mlir;
25c484c7ddSChia-hung Duan 
26c484c7ddSChia-hung Duan namespace {
27c484c7ddSChia-hung Duan 
28c484c7ddSChia-hung Duan class OptReductionPass : public OptReductionBase<OptReductionPass> {
29c484c7ddSChia-hung Duan public:
30c484c7ddSChia-hung Duan   /// Runs the pass instance in the pass pipeline.
31c484c7ddSChia-hung Duan   void runOnOperation() override;
32c484c7ddSChia-hung Duan };
33c484c7ddSChia-hung Duan 
34be0a7e9fSMehdi Amini } // namespace
35c484c7ddSChia-hung Duan 
36c484c7ddSChia-hung Duan /// Runs the pass instance in the pass pipeline.
runOnOperation()37c484c7ddSChia-hung Duan void OptReductionPass::runOnOperation() {
38c484c7ddSChia-hung Duan   LLVM_DEBUG(llvm::dbgs() << "\nOptimization Reduction pass: ");
39c484c7ddSChia-hung Duan 
40c484c7ddSChia-hung Duan   Tester test(testerName, testerArgs);
41c484c7ddSChia-hung Duan 
42c484c7ddSChia-hung Duan   ModuleOp module = this->getOperation();
43c484c7ddSChia-hung Duan   ModuleOp moduleVariant = module.clone();
44c484c7ddSChia-hung Duan 
45*0f304ef0SRiver Riddle   OpPassManager passManager("builtin.module");
46c484c7ddSChia-hung Duan   if (failed(parsePassPipeline(optPass, passManager))) {
471a001dedSChia-hung Duan     module.emitError() << "\nfailed to parse pass pipeline";
481a001dedSChia-hung Duan     return signalPassFailure();
49c484c7ddSChia-hung Duan   }
50c484c7ddSChia-hung Duan 
51c484c7ddSChia-hung Duan   std::pair<Tester::Interestingness, int> original = test.isInteresting(module);
52c484c7ddSChia-hung Duan   if (original.first != Tester::Interestingness::True) {
531a001dedSChia-hung Duan     module.emitError() << "\nthe original input is not interested";
541a001dedSChia-hung Duan     return signalPassFailure();
55c484c7ddSChia-hung Duan   }
56c484c7ddSChia-hung Duan 
57*0f304ef0SRiver Riddle   // Temporarily push the variant under the main module and execute the pipeline
58*0f304ef0SRiver Riddle   // on it.
59*0f304ef0SRiver Riddle   module.getBody()->push_back(moduleVariant);
60*0f304ef0SRiver Riddle   LogicalResult pipelineResult = runPipeline(passManager, moduleVariant);
61*0f304ef0SRiver Riddle   moduleVariant->remove();
62*0f304ef0SRiver Riddle 
63*0f304ef0SRiver Riddle   if (failed(pipelineResult)) {
641a001dedSChia-hung Duan     module.emitError() << "\nfailed to run pass pipeline";
651a001dedSChia-hung Duan     return signalPassFailure();
66c484c7ddSChia-hung Duan   }
67c484c7ddSChia-hung Duan 
68c484c7ddSChia-hung Duan   std::pair<Tester::Interestingness, int> reduced =
69c484c7ddSChia-hung Duan       test.isInteresting(moduleVariant);
70c484c7ddSChia-hung Duan 
71c484c7ddSChia-hung Duan   if (reduced.first == Tester::Interestingness::True &&
72c484c7ddSChia-hung Duan       reduced.second < original.second) {
73c484c7ddSChia-hung Duan     module.getBody()->clear();
74c484c7ddSChia-hung Duan     module.getBody()->getOperations().splice(
75c484c7ddSChia-hung Duan         module.getBody()->begin(), moduleVariant.getBody()->getOperations());
76c484c7ddSChia-hung Duan     LLVM_DEBUG(llvm::dbgs() << "\nSuccessful Transformed version\n\n");
77c484c7ddSChia-hung Duan   } else {
78c484c7ddSChia-hung Duan     LLVM_DEBUG(llvm::dbgs() << "\nUnsuccessful Transformed version\n\n");
79c484c7ddSChia-hung Duan   }
80c484c7ddSChia-hung Duan 
81c484c7ddSChia-hung Duan   moduleVariant->destroy();
82c484c7ddSChia-hung Duan 
83c484c7ddSChia-hung Duan   LLVM_DEBUG(llvm::dbgs() << "Pass Complete\n\n");
84c484c7ddSChia-hung Duan }
85c484c7ddSChia-hung Duan 
createOptReductionPass()86c484c7ddSChia-hung Duan std::unique_ptr<Pass> mlir::createOptReductionPass() {
87c484c7ddSChia-hung Duan   return std::make_unique<OptReductionPass>();
88c484c7ddSChia-hung Duan }
89