20#include "llvm/Support/DebugLog.h"
21#include "llvm/Support/MemoryBuffer.h"
24#define GEN_PASS_DEF_OPTREDUCTIONPASS
25#include "mlir/Reducer/Passes.h.inc"
28#define DEBUG_TYPE "mlir-reduce"
34class OptReductionPass :
public impl::OptReductionPassBase<OptReductionPass> {
39 void runOnOperation()
override;
45void OptReductionPass::runOnOperation() {
46 LDBG() <<
"\nOptimization Reduction pass: ";
48 Tester test(testerName, testerArgs);
49 Operation *topOp = this->getOperation();
51 std::string pipelineStr = optPass;
52 if (pipelineStr.empty()) {
53 if (!optPassFile.empty()) {
54 auto fileOrErr = llvm::MemoryBuffer::getFile(optPassFile);
55 if (std::error_code ec = fileOrErr.getError()) {
56 topOp->
emitError() <<
"Could not open pass pipeline file: "
57 << optPassFile <<
" (" << ec.message() <<
")";
58 return signalPassFailure();
60 pipelineStr = fileOrErr.get()->getBuffer().trim().str();
64 PassManager passManager(topOp->
getName());
66 topOp->
emitError() <<
"\nfailed to parse pass pipeline";
67 return signalPassFailure();
70 std::pair<Tester::Interestingness, int> original = test.isInteresting(topOp);
71 if (original.first != Tester::Interestingness::True) {
72 topOp->
emitError() <<
"\nthe original input is not interested";
73 return signalPassFailure();
75 Operation *topOpVariant = topOp->
clone();
77 LogicalResult pipelineResult = passManager.run(topOpVariant);
78 if (
failed(pipelineResult)) {
79 topOp->
emitError() <<
"\nfailed to run pass pipeline";
80 return signalPassFailure();
83 std::pair<Tester::Interestingness, int> reduced =
84 test.isInteresting(topOpVariant);
86 if (reduced.first == Tester::Interestingness::True &&
87 reduced.second < original.second) {
93 LDBG() <<
"\nSuccessful Transformed version\n";
95 LDBG() <<
"\nUnsuccessful Transformed version\n";
100 LDBG() <<
"Pass Complete\n";
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
void destroy()
Destroys this operation and its subclass data.
OperationName getName()
The name of an operation is the key identifier for it.
Operation * clone(IRMapping &mapper, const CloneOptions &options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
BlockListType & getBlocks()
Include the generated interface declarations.
LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm, raw_ostream &errorStream=llvm::errs())
Parse the textual representation of a pass pipeline, adding the result to 'pm' on success.