1 /* Copyright 2022 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 <memory>
17 #include <utility>
18
19 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
20 #include "mlir/Dialect/Func/IR/FuncOps.h"
21 #include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h"
22 #include "mlir/Pass/Pass.h"
23 #include "mlir/Transforms/DialectConversion.h"
24 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
25 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h"
26
27 namespace tensorflow {
28 namespace {
29
30 #define GEN_PASS_CLASSES
31 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h.inc"
32
33 using mlir::MLIRContext;
34 using mlir::Operation;
35 using mlir::vector::MultiDimReductionOp;
36 using mlir::vector::VectorMultiReductionLowering;
37
38 struct RewriteVectorMultiReductionPass
39 : public RewriteVectorMultiReductionPassBase<
40 RewriteVectorMultiReductionPass> {
runOnOperationtensorflow::__anon6bc958fe0111::RewriteVectorMultiReductionPass41 void runOnOperation() override {
42 MLIRContext* ctx = &getContext();
43 Operation* op = getOperation();
44 if (failed(RewriteTwoAndMoreDimReductions(ctx, op))) signalPassFailure();
45 if (failed(RewriteOneDimReductions(ctx, op))) signalPassFailure();
46 }
47
48 // Rewrite N-D reductions as the sequence of vector operations without
49 // horizontal reduction, i.e. `vector.reduction`.
RewriteTwoAndMoreDimReductionstensorflow::__anon6bc958fe0111::RewriteVectorMultiReductionPass50 mlir::LogicalResult RewriteTwoAndMoreDimReductions(MLIRContext* ctx,
51 Operation* op) const {
52 mlir::ConversionTarget target(*ctx);
53 target.addLegalDialect<mlir::arith::ArithmeticDialect,
54 mlir::vector::VectorDialect>();
55 target.addDynamicallyLegalOp<MultiDimReductionOp>(
56 [&](MultiDimReductionOp op) {
57 return op.getSourceVectorType().getRank() == 1;
58 });
59
60 mlir::RewritePatternSet patterns(ctx);
61 mlir::vector::populateVectorMultiReductionLoweringPatterns(
62 patterns, VectorMultiReductionLowering::InnerParallel);
63 return applyPartialConversion(op, target, std::move(patterns));
64 }
65
66 // Rewrite 1D reductions as a `vector.reduction`.
RewriteOneDimReductionstensorflow::__anon6bc958fe0111::RewriteVectorMultiReductionPass67 mlir::LogicalResult RewriteOneDimReductions(MLIRContext* ctx,
68 Operation* op) const {
69 mlir::RewritePatternSet patterns(ctx);
70 mlir::vector::populateVectorMultiReductionLoweringPatterns(
71 patterns, VectorMultiReductionLowering::InnerReduction);
72 return applyPatternsAndFoldGreedily(op, std::move(patterns));
73 }
74 };
75
76 } // namespace
77
78 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
createRewriteVectorMultiReductionPass()79 createRewriteVectorMultiReductionPass() {
80 return std::make_unique<RewriteVectorMultiReductionPass>();
81 }
82
83 } // namespace tensorflow
84