1 //===- OptReductionPass.cpp - Optimization Reduction Pass Wrapper ---------===//
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 // This file defines the Opt Reduction Pass Wrapper. It creates a MLIR pass to
10 // run any optimization pass within it and only replaces the output module with
11 // the transformed version if it is smaller and interesting.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "mlir/Reducer/OptReductionPass.h"
16
17 #define DEBUG_TYPE "mlir-reduce"
18
19 using namespace mlir;
20
OptReductionPass(const Tester & test,MLIRContext * context,std::unique_ptr<Pass> optPass)21 OptReductionPass::OptReductionPass(const Tester &test, MLIRContext *context,
22 std::unique_ptr<Pass> optPass)
23 : context(context), test(test), optPass(std::move(optPass)) {}
24
OptReductionPass(const OptReductionPass & srcPass)25 OptReductionPass::OptReductionPass(const OptReductionPass &srcPass)
26 : OptReductionBase<OptReductionPass>(srcPass), test(srcPass.test),
27 optPass(srcPass.optPass.get()) {}
28
29 /// Runs the pass instance in the pass pipeline.
runOnOperation()30 void OptReductionPass::runOnOperation() {
31 LLVM_DEBUG(llvm::dbgs() << "\nOptimization Reduction pass: ");
32 LLVM_DEBUG(llvm::dbgs() << optPass.get()->getName() << "\nTesting:\n");
33
34 ModuleOp module = this->getOperation();
35 ModuleOp moduleVariant = module.clone();
36 PassManager pmTransform(context);
37 pmTransform.addPass(std::move(optPass));
38
39 if (failed(pmTransform.run(moduleVariant)))
40 return;
41
42 ReductionNode original(module, nullptr);
43 original.measureAndTest(test);
44
45 ReductionNode reduced(moduleVariant, nullptr);
46 reduced.measureAndTest(test);
47
48 if (reduced.isInteresting() && reduced.getSize() < original.getSize()) {
49 ReductionTreeUtils::updateGoldenModule(module, reduced.getModule().clone());
50 LLVM_DEBUG(llvm::dbgs() << "\nSuccessful Transformed version\n\n");
51 } else {
52 LLVM_DEBUG(llvm::dbgs() << "\nUnsuccessful Transformed version\n\n");
53 }
54
55 LLVM_DEBUG(llvm::dbgs() << "Pass Complete\n\n");
56 }
57