|
| 1 | +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "mlir/Dialect/SCF/SCF.h" // from @llvm-project |
| 17 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project |
| 18 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project |
| 19 | +#include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" |
| 20 | +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" |
| 21 | +#include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h" |
| 22 | + |
| 23 | +namespace mlir { |
| 24 | +namespace TF { |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +/// Convert the `tf.IfRegion` op to the `scf.if` op. |
| 29 | +class ConvertIfRegionOp : public OpRewritePattern<IfRegionOp> { |
| 30 | + public: |
| 31 | + using OpRewritePattern<IfRegionOp>::OpRewritePattern; |
| 32 | + |
| 33 | + LogicalResult matchAndRewrite(IfRegionOp op, |
| 34 | + PatternRewriter& rewriter) const override { |
| 35 | + // Creates the `then` or `else` region of the `scf.if` op. Note that |
| 36 | + // `tf_then_or_else_region` is the `then` or `else` region of the |
| 37 | + // `tf.IfRegion` op and `scf_then_or_else_region` is the `then` or `else` |
| 38 | + // region of the new `scf.if` op. Further, `tf_if_region_return_type` is the |
| 39 | + // list of return types of the `tf.IfRegion` op. |
| 40 | + auto createScfThenOrElse = [](Region& tf_then_or_else_region, |
| 41 | + Region& scf_then_or_else_region, |
| 42 | + TypeRange tf_if_region_return_type, |
| 43 | + PatternRewriter& rewriter) { |
| 44 | + // Clone all the ops of `tf_then_or_else_region` into |
| 45 | + // `scf_then_or_else_region`. |
| 46 | + rewriter.cloneRegionBefore(tf_then_or_else_region, |
| 47 | + &scf_then_or_else_region.front()); |
| 48 | + rewriter.eraseBlock(&scf_then_or_else_region.back()); |
| 49 | + |
| 50 | + Block* first_block_of_scf_then_or_else_region = |
| 51 | + &scf_then_or_else_region.front(); |
| 52 | + |
| 53 | + // Replace the current terminator (a `tf.Yield` op) with an `scf.yield` |
| 54 | + // op. The input of the `scf.yield` op is a list of results of `tf.Cast` |
| 55 | + // ops, each of which casts an operand of the current terminator to the |
| 56 | + // corresponding result type of the `tf.IfRegion` op. |
| 57 | + Operation* current_terminator = |
| 58 | + first_block_of_scf_then_or_else_region->getTerminator(); |
| 59 | + rewriter.setInsertionPoint(current_terminator); |
| 60 | + SmallVector<Value, 4> scf_yield_input; |
| 61 | + for (auto it : llvm::zip(tf_if_region_return_type, |
| 62 | + current_terminator->getOperands())) { |
| 63 | + scf_yield_input.push_back(rewriter.create<CastOp>( |
| 64 | + current_terminator->getLoc(), std::get<0>(it), std::get<1>(it))); |
| 65 | + } |
| 66 | + |
| 67 | + rewriter.replaceOpWithNewOp<scf::YieldOp>(current_terminator, |
| 68 | + scf_yield_input); |
| 69 | + }; |
| 70 | + |
| 71 | + Location loc = op.getLoc(); |
| 72 | + |
| 73 | + // The condition of an `scf.if` op is a 1-bit signless integer. Whereas, the |
| 74 | + // condition of the `tf.IfRegion` op is a 0-D tensor of 1-bit signless |
| 75 | + // integers. Thus, we use the `tensor.extract` op to compute the condition |
| 76 | + // of `scf.if` from that of `tf.IfRegion`. |
| 77 | + auto scf_if_condition = rewriter.create<tensor::ExtractOp>(loc, op.cond()); |
| 78 | + |
| 79 | + TypeRange tf_if_region_return_type = op.getResultTypes(); |
| 80 | + |
| 81 | + // Create the `scf.if` op. |
| 82 | + auto scf_if_op = |
| 83 | + rewriter.create<scf::IfOp>(loc, tf_if_region_return_type, |
| 84 | + scf_if_condition, /*withElseRegion=*/true); |
| 85 | + |
| 86 | + Region& then_region = op.then_branch(); |
| 87 | + Region& else_region = op.else_branch(); |
| 88 | + |
| 89 | + // Create the `then` and `else` regions of the `scf.if` op. |
| 90 | + createScfThenOrElse(then_region, scf_if_op.thenRegion(), |
| 91 | + tf_if_region_return_type, rewriter); |
| 92 | + createScfThenOrElse(else_region, scf_if_op.elseRegion(), |
| 93 | + tf_if_region_return_type, rewriter); |
| 94 | + |
| 95 | + // Replace the `tf.IfRegion` op with the results of the `scf.if` op. |
| 96 | + rewriter.replaceOp(op, scf_if_op.getResults()); |
| 97 | + return success(); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +} // end anonymous namespace |
| 102 | + |
| 103 | +void populateTfControlFlowToScfPatterns(MLIRContext* context, |
| 104 | + OwningRewritePatternList* patterns) { |
| 105 | + patterns->insert<ConvertIfRegionOp>(context); |
| 106 | +} |
| 107 | + |
| 108 | +struct ConvertTfControlFlowToScf |
| 109 | + : public ConvertTfControlFlowToScfPassBase<ConvertTfControlFlowToScf> { |
| 110 | + void runOnOperation() override { |
| 111 | + OwningRewritePatternList patterns(&getContext()); |
| 112 | + populateTfControlFlowToScfPatterns(&getContext(), &patterns); |
| 113 | + (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +std::unique_ptr<OperationPass<ModuleOp>> createConvertTfControlFlowToScfPass() { |
| 118 | + return std::make_unique<ConvertTfControlFlowToScf>(); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace TF |
| 122 | +} // end namespace mlir |
0 commit comments