|
| 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 | + |
| 17 | +#include "llvm/ADT/SmallVector.h" |
| 18 | +#include "llvm/Support/Casting.h" |
| 19 | +#include "mlir-hlo/Dialect/mhlo/IR/chlo_ops.h" |
| 20 | +#include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h" |
| 21 | +#include "mlir-hlo/Dialect/mhlo/transforms/map_chlo_to_hlo_op.h" |
| 22 | +#include "mlir-hlo/Dialect/mhlo/transforms/passes.h" |
| 23 | +#include "mlir-hlo/Dialect/mhlo/transforms/rewriters.h" |
| 24 | +#include "mlir/Dialect/SCF/SCF.h" |
| 25 | +#include "mlir/Dialect/Shape/IR/Shape.h" |
| 26 | +#include "mlir/Dialect/StandardOps/IR/Ops.h" |
| 27 | +#include "mlir/Dialect/Tensor/IR/Tensor.h" |
| 28 | +#include "mlir/IR/BuiltinOps.h" |
| 29 | +#include "mlir/IR/BuiltinTypes.h" |
| 30 | +#include "mlir/IR/MLIRContext.h" |
| 31 | +#include "mlir/IR/Operation.h" |
| 32 | +#include "mlir/IR/OperationSupport.h" |
| 33 | +#include "mlir/IR/PatternMatch.h" |
| 34 | +#include "mlir/Interfaces/InferTypeOpInterface.h" |
| 35 | +#include "mlir/Pass/Pass.h" |
| 36 | +#include "mlir/Transforms/DialectConversion.h" |
| 37 | + |
| 38 | +namespace mlir { |
| 39 | +namespace mhlo { |
| 40 | +namespace { |
| 41 | + |
| 42 | +struct ShapeOfOpConversion : public OpConversionPattern<shape::ShapeOfOp> { |
| 43 | + explicit ShapeOfOpConversion(MLIRContext *context) |
| 44 | + : OpConversionPattern<shape::ShapeOfOp>(context) {} |
| 45 | + |
| 46 | + LogicalResult matchAndRewrite( |
| 47 | + shape::ShapeOfOp op, ArrayRef<Value> operands, |
| 48 | + ConversionPatternRewriter &rewriter) const override { |
| 49 | + shape::ShapeOfOp::Adaptor transformed(operands); |
| 50 | + |
| 51 | + auto shape_origin = llvm::dyn_cast_or_null<InferShapedTypeOpInterface>( |
| 52 | + transformed.arg().getDefiningOp()); |
| 53 | + if (!shape_origin) return failure(); |
| 54 | + |
| 55 | + llvm::SmallVector<Value, 1> reified_shapes; |
| 56 | + if (failed(shape_origin.reifyReturnTypeShapes(rewriter, reified_shapes))) |
| 57 | + return failure(); |
| 58 | + |
| 59 | + assert(reified_shapes.size() == 1); |
| 60 | + Value reified_shape = reified_shapes.front(); |
| 61 | + if (reified_shape.getType() != op.getType()) { |
| 62 | + reified_shape = rewriter.create<tensor::CastOp>(op.getLoc(), op.getType(), |
| 63 | + reified_shape); |
| 64 | + } |
| 65 | + |
| 66 | + rewriter.replaceOp(op, reified_shapes.front()); |
| 67 | + return success(); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +struct MoveUpDynamicBroadcastsForFusionPass |
| 72 | + : public PassWrapper<MoveUpDynamicBroadcastsForFusionPass, FunctionPass> { |
| 73 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 74 | + registry.insert<shape::ShapeDialect, mhlo::MhloDialect>(); |
| 75 | + } |
| 76 | + |
| 77 | + void runOnFunction() override { |
| 78 | + // Setup target legality. |
| 79 | + MLIRContext &ctx = getContext(); |
| 80 | + ConversionTarget target(ctx); |
| 81 | + PopulateMoveUpDynamicBroadcastsForFusionLegality(&target); |
| 82 | + |
| 83 | + // Populate rewrite patterns. |
| 84 | + OwningRewritePatternList patterns; |
| 85 | + mhlo::PopulateMoveUpDynamicBroadcastsForFusionPatterns(&ctx, &patterns); |
| 86 | + |
| 87 | + // Apply transformation. |
| 88 | + if (failed(applyPartialConversion(getFunction(), target, |
| 89 | + std::move(patterns)))) { |
| 90 | + return signalPassFailure(); |
| 91 | + } |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace |
| 96 | + |
| 97 | +void PopulateMoveUpDynamicBroadcastsForFusionLegality( |
| 98 | + ConversionTarget *target) { |
| 99 | + target->addLegalDialect<MhloDialect, StandardOpsDialect, |
| 100 | + tensor::TensorDialect>(); |
| 101 | +} |
| 102 | + |
| 103 | +void PopulateMoveUpDynamicBroadcastsForFusionPatterns( |
| 104 | + MLIRContext *context, OwningRewritePatternList *patterns) { |
| 105 | + // clang-format off |
| 106 | + patterns->insert<ShapeOfOpConversion>(context); |
| 107 | + // clang-format on |
| 108 | +} |
| 109 | + |
| 110 | +std::unique_ptr<FunctionPass> createMoveUpDynamicBroadcastsForFusionPass() { |
| 111 | + return std::make_unique<MoveUpDynamicBroadcastsForFusionPass>(); |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace mhlo |
| 115 | +} // namespace mlir |
0 commit comments