Skip to content

[MLIR] Add replace-operands option to mlir-reduce #153100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mlir/include/mlir/Reducer/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def ReductionTreePass : Pass<"reduction-tree"> {
Option<"traversalModeId", "traversal-mode", "unsigned",
/* default */"0",
"The graph traversal mode, the default is single-path mode">,
Option<"replaceOperands", "replace-operands", "bool",
/* default */"false",
"Whether the pass should replace operands with previously defined values with the same type">,

] # CommonReductionPassOptions.options;
}

Expand Down
40 changes: 32 additions & 8 deletions mlir/lib/Reducer/ReductionTreePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
//===----------------------------------------------------------------------===//

#include "mlir/IR/DialectInterface.h"
#include "mlir/IR/Dominance.h"
#include "mlir/Reducer/Passes.h"
#include "mlir/Reducer/ReductionNode.h"
#include "mlir/Reducer/ReductionPatternInterface.h"
#include "mlir/Reducer/Tester.h"
#include "mlir/Rewrite/FrozenRewritePatternSet.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

#include "llvm/ADT/ArrayRef.h"
Expand All @@ -38,7 +40,7 @@ using namespace mlir;
static void applyPatterns(Region &region,
const FrozenRewritePatternSet &patterns,
ArrayRef<ReductionNode::Range> rangeToKeep,
bool eraseOpNotInRange) {
bool eraseOpNotInRange, bool replaceOperands) {
std::vector<Operation *> opsNotInRange;
std::vector<Operation *> opsInRange;
size_t keepIndex = 0;
Expand All @@ -53,17 +55,35 @@ static void applyPatterns(Region &region,
opsInRange.push_back(&op.value());
}

DominanceInfo domInfo(region.getParentOp());
mlir::DenseMap<mlir::Type, mlir::SmallVector<mlir::Value, 5>> valueMap;

// `applyOpPatternsGreedily` with folding may erase the ops so we can't do the
// pattern matching in above iteration. Besides, erase op not-in-range may end
// up in invalid module, so `applyOpPatternsGreedily` with folding should come
// before that transform.
for (Operation *op : opsInRange) {
if (replaceOperands)
for (auto operandTie : llvm::enumerate(op->getOperands())) {
size_t index = operandTie.index();
auto operand = operandTie.value();
for (auto candidate : valueMap[operand.getType()])
if (domInfo.properlyDominates(candidate, op)) {
op->setOperand(index, candidate);
break;
}
}

// `applyOpPatternsGreedily` with folding returns whether the op is
// converted. Omit it because we don't have expectation this reduction will
// be success or not.
(void)applyOpPatternsGreedily(op, patterns,
GreedyRewriteConfig().setStrictness(
GreedyRewriteStrictness::ExistingOps));

if (op && replaceOperands)
for (auto result : op->getResults())
valueMap[result.getType()].push_back(result);
}

if (eraseOpNotInRange)
Expand All @@ -83,7 +103,8 @@ static void applyPatterns(Region &region,
template <typename IteratorType>
static LogicalResult findOptimal(ModuleOp module, Region &region,
const FrozenRewritePatternSet &patterns,
const Tester &test, bool eraseOpNotInRange) {
const Tester &test, bool eraseOpNotInRange,
bool replaceOperands) {
std::pair<Tester::Interestingness, size_t> initStatus =
test.isInteresting(module);
// While exploring the reduction tree, we always branch from an interesting
Expand Down Expand Up @@ -111,7 +132,7 @@ static LogicalResult findOptimal(ModuleOp module, Region &region,
Region &curRegion = currentNode.getRegion();

applyPatterns(curRegion, patterns, currentNode.getRanges(),
eraseOpNotInRange);
eraseOpNotInRange, replaceOperands);
currentNode.update(test.isInteresting(currentNode.getModule()));

if (currentNode.isInteresting() == Tester::Interestingness::True &&
Expand All @@ -134,7 +155,8 @@ static LogicalResult findOptimal(ModuleOp module, Region &region,
// Reduce the region through the optimal path.
while (!trace.empty()) {
ReductionNode *top = trace.pop_back_val();
applyPatterns(region, patterns, top->getStartRanges(), eraseOpNotInRange);
applyPatterns(region, patterns, top->getStartRanges(), eraseOpNotInRange,
replaceOperands);
}

if (test.isInteresting(module).first != Tester::Interestingness::True)
Expand All @@ -148,19 +170,21 @@ static LogicalResult findOptimal(ModuleOp module, Region &region,
template <typename IteratorType>
static LogicalResult findOptimal(ModuleOp module, Region &region,
const FrozenRewritePatternSet &patterns,
const Tester &test) {
const Tester &test, bool replaceOperands) {
// We separate the reduction process into 2 steps, the first one is to erase
// redundant operations and the second one is to apply the reducer patterns.

// In the first phase, we don't apply any patterns so that we only select the
// range of operations to keep to the module stay interesting.
if (failed(findOptimal<IteratorType>(module, region, /*patterns=*/{}, test,
/*eraseOpNotInRange=*/true)))
/*eraseOpNotInRange=*/true,
replaceOperands)))
return failure();
// In the second phase, we suppose that no operation is redundant, so we try
// to rewrite the operation into simpler form.
return findOptimal<IteratorType>(module, region, patterns, test,
/*eraseOpNotInRange=*/false);
/*eraseOpNotInRange=*/false,
/*replaceOperands=*/false);
}

namespace {
Expand Down Expand Up @@ -248,7 +272,7 @@ LogicalResult ReductionTreePass::reduceOp(ModuleOp module, Region &region) {
switch (traversalModeId) {
case TraversalMode::SinglePath:
return findOptimal<ReductionNode::iterator<TraversalMode::SinglePath>>(
module, region, reducerPatterns, test);
module, region, reducerPatterns, test, replaceOperands);
default:
return module.emitError() << "unsupported traversal mode detected";
}
Expand Down
14 changes: 14 additions & 0 deletions mlir/test/mlir-reduce/replace-operands.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// UNSUPPORTED: system-windows
// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/failure-test.sh replace-operands=true' | FileCheck %s

// CHECK-LABEL: func.func @main
func.func @main() {
// CHECK-NEXT: %[[RESULT:.*]] = arith.constant 3 : i32
// CHECK-NEXT: {{.*}} = "test.op_crash"(%[[RESULT]], %[[RESULT]]) : (i32, i32) -> i32
// CHECK-NEXT return

%c1 = arith.constant 3 : i32
%c2 = arith.constant 2 : i32
%2 = "test.op_crash" (%c1, %c2) : (i32, i32) -> i32
return
}