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 // This file contains the patterns to convert std.index_cast on tensors to
17 // tensor ops and index_cast on scalars.
18
19 #include <utility>
20
21 #include "mlir-hlo/Transforms/PassDetail.h"
22 #include "mlir-hlo/Transforms/passes.h"
23 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
24 #include "mlir/Dialect/Func/IR/FuncOps.h"
25 #include "mlir/Dialect/Tensor/IR/Tensor.h"
26 #include "mlir/IR/PatternMatch.h"
27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
28
29 namespace mlir {
30 namespace {
31
32 // index_cast is not defined on tensors, so lower it to a tensor.generate.
33 struct IndexCastConverter : public OpRewritePattern<arith::IndexCastOp> {
34 public:
35 using OpRewritePattern::OpRewritePattern;
matchAndRewritemlir::__anon3aafeac60111::IndexCastConverter36 LogicalResult matchAndRewrite(arith::IndexCastOp op,
37 PatternRewriter &rewriter) const final {
38 // Only rank 1 is supported for now.
39 auto resultTy = op.getType().dyn_cast<ShapedType>();
40 if (!resultTy || resultTy.getRank() != 1) return failure();
41
42 rewriter.replaceOpWithNewOp<tensor::GenerateOp>(
43 op, op.getType(),
44 resultTy.hasStaticShape() ? ValueRange{}
45 : ValueRange{rewriter.create<tensor::DimOp>(
46 op.getLoc(), op.getIn(), 0)},
47 [&](OpBuilder &b, Location loc, ValueRange args) {
48 Value dim = args.front();
49 Value extent = b.create<tensor::ExtractOp>(loc, op.getIn(), dim);
50 Value casted = b.create<arith::IndexCastOp>(
51 loc, resultTy.getElementType(), extent);
52 b.create<tensor::YieldOp>(loc, casted);
53 });
54 return success();
55 }
56 };
57
58 struct LowerIndexCastPass : public LowerIndexCastPassBase<LowerIndexCastPass> {
getDependentDialectsmlir::__anon3aafeac60111::LowerIndexCastPass59 void getDependentDialects(DialectRegistry ®istry) const override {
60 registry.insert<tensor::TensorDialect>();
61 }
62
runOnOperationmlir::__anon3aafeac60111::LowerIndexCastPass63 void runOnOperation() override {
64 RewritePatternSet patterns(&getContext());
65 patterns.add<IndexCastConverter>(patterns.getContext());
66 if (failed(
67 applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
68 return signalPassFailure();
69 }
70 };
71
72 } // namespace
73
createLowerIndexCastPass()74 std::unique_ptr<OperationPass<func::FuncOp>> createLowerIndexCastPass() {
75 return std::make_unique<LowerIndexCastPass>();
76 }
77
78 } // namespace mlir
79