|
| 1 | +//===- TosaToSCF.cpp - Lowering Tosa to SCF Dialect -----------------------===// |
| 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 | +// These rewriters lower from the Tosa to the SCF dialect. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Conversion/TosaToSCF/TosaToSCF.h" |
| 14 | +#include "mlir/Dialect/SCF/SCF.h" |
| 15 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 16 | +#include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 17 | +#include "mlir/IR/BlockAndValueMapping.h" |
| 18 | +#include "mlir/IR/PatternMatch.h" |
| 19 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 20 | + |
| 21 | +using namespace mlir; |
| 22 | +using namespace tosa; |
| 23 | + |
| 24 | +static void inlineIfCase(Region &srcRegion, Region &dstRegion, |
| 25 | + OperandRange operands, PatternRewriter &rewriter) { |
| 26 | + BlockAndValueMapping mapper; |
| 27 | + dstRegion.takeBody(srcRegion); |
| 28 | + Block *headBlock = &dstRegion.front(); |
| 29 | + for (auto it : llvm::zip(headBlock->getArguments(), operands)) |
| 30 | + std::get<0>(it).replaceAllUsesWith(std::get<1>(it)); |
| 31 | + |
| 32 | + for (auto &block : dstRegion) { |
| 33 | + llvm::SmallVector<Operation *> toDelete; |
| 34 | + block.walk([&](tosa::YieldOp yield) { |
| 35 | + rewriter.setInsertionPoint(yield); |
| 36 | + rewriter.create<scf::YieldOp>(yield.getLoc(), yield.inputs()); |
| 37 | + toDelete.push_back(yield); |
| 38 | + }); |
| 39 | + for (Operation *val : toDelete) |
| 40 | + rewriter.eraseOp(val); |
| 41 | + } |
| 42 | + |
| 43 | + headBlock->eraseArguments( |
| 44 | + llvm::to_vector<4>(llvm::seq<unsigned>(0, headBlock->getNumArguments()))); |
| 45 | +} |
| 46 | + |
| 47 | +static void inlineWhileCase(Region &srcRegion, Region &dstRegion, |
| 48 | + OperandRange operands, PatternRewriter &rewriter, |
| 49 | + bool isCond) { |
| 50 | + BlockAndValueMapping mapper; |
| 51 | + dstRegion.takeBody(srcRegion); |
| 52 | + |
| 53 | + for (auto &block : dstRegion) { |
| 54 | + llvm::SmallVector<Operation *> toDelete; |
| 55 | + block.walk([&](tosa::YieldOp yield) { |
| 56 | + rewriter.setInsertionPoint(yield); |
| 57 | + if (isCond) { |
| 58 | + auto condition = rewriter.create<tensor::ExtractOp>( |
| 59 | + yield.getLoc(), yield.getOperand(0)); |
| 60 | + rewriter.create<scf::ConditionOp>(yield.getLoc(), condition, |
| 61 | + block.getArguments()); |
| 62 | + } else { |
| 63 | + rewriter.setInsertionPoint(yield); |
| 64 | + rewriter.create<scf::YieldOp>(yield.getLoc(), yield.inputs()); |
| 65 | + } |
| 66 | + toDelete.push_back(yield); |
| 67 | + }); |
| 68 | + for (Operation *val : toDelete) |
| 69 | + rewriter.eraseOp(val); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +namespace { |
| 74 | + |
| 75 | +class IfOpConverter : public OpRewritePattern<tosa::IfOp> { |
| 76 | +public: |
| 77 | + using OpRewritePattern<tosa::IfOp>::OpRewritePattern; |
| 78 | + |
| 79 | + LogicalResult matchAndRewrite(tosa::IfOp op, |
| 80 | + PatternRewriter &rewriter) const final { |
| 81 | + auto condition = rewriter.create<tensor::ExtractOp>(op.getLoc(), op.cond()); |
| 82 | + auto newIf = rewriter.replaceOpWithNewOp<scf::IfOp>(op, op.getResultTypes(), |
| 83 | + condition, true); |
| 84 | + |
| 85 | + inlineIfCase(op.then_branch(), newIf.thenRegion(), op.inputs(), rewriter); |
| 86 | + inlineIfCase(op.else_branch(), newIf.elseRegion(), op.inputs(), rewriter); |
| 87 | + return success(); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +class WhileOpConverter : public OpRewritePattern<tosa::WhileOp> { |
| 92 | +public: |
| 93 | + using OpRewritePattern<tosa::WhileOp>::OpRewritePattern; |
| 94 | + |
| 95 | + LogicalResult matchAndRewrite(tosa::WhileOp op, |
| 96 | + PatternRewriter &rewriter) const final { |
| 97 | + auto newWhile = rewriter.replaceOpWithNewOp<scf::WhileOp>( |
| 98 | + op, op.getResultTypes(), op.inputs()); |
| 99 | + |
| 100 | + inlineWhileCase(op.cond(), newWhile.before(), op.inputs(), rewriter, true); |
| 101 | + inlineWhileCase(op.body(), newWhile.after(), op.inputs(), rewriter, false); |
| 102 | + |
| 103 | + return success(); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace |
| 108 | + |
| 109 | +void mlir::tosa::populateTosaToSCFConversionPatterns( |
| 110 | + MLIRContext *context, OwningRewritePatternList *patterns) { |
| 111 | + patterns->insert<IfOpConverter>(context); |
| 112 | + patterns->insert<WhileOpConverter>(context); |
| 113 | +} |
0 commit comments