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 <utility>
17
18 #include "mlir-hlo/Dialect/gml_st/transforms/transforms.h"
19 #include "mlir/Dialect/Func/IR/FuncOps.h"
20 #include "mlir/Dialect/Linalg/IR/Linalg.h"
21 #include "mlir/Dialect/Linalg/Passes.h"
22 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
23 #include "mlir/Support/LogicalResult.h"
24 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h"
27 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h"
28
29 namespace tensorflow {
30 namespace {
31
32 #define GEN_PASS_CLASSES
33 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h.inc"
34
35 constexpr llvm::StringRef kWasPeeledAttr = "PeelStLoopsPeeledAttr";
36
37 using mlir::gml_st::LoopOp;
38
39 struct PeelGmlStLoop : public mlir::OpRewritePattern<LoopOp> {
40 using mlir::OpRewritePattern<LoopOp>::OpRewritePattern;
41
matchAndRewritetensorflow::__anonf57143f70111::PeelGmlStLoop42 mlir::LogicalResult matchAndRewrite(
43 LoopOp loop, mlir::PatternRewriter &rewriter) const override {
44 if (loop->hasAttr(kWasPeeledAttr)) return mlir::failure();
45 auto true_attr = mlir::BoolAttr::get(rewriter.getContext(), true);
46 loop->setAttr(kWasPeeledAttr, true_attr);
47 for (int peeled_idx = loop.getNumLoops() - 1; peeled_idx >= 0;
48 peeled_idx--) {
49 LoopOp peel;
50 // Mark the new loop if one was created
51 if (mlir::gml_st::peelAndCanonicalizeGmlStLoop(rewriter, loop, peeled_idx,
52 peel)
53 .succeeded())
54 peel->setAttr(kWasPeeledAttr, true_attr);
55 }
56 return mlir::success();
57 }
58 };
59
60 struct PeelTiledLoopsPass : public PeelTiledLoopsBase<PeelTiledLoopsPass> {
runOnOperationtensorflow::__anonf57143f70111::PeelTiledLoopsPass61 void runOnOperation() override {
62 auto func_op = getOperation();
63
64 // Apply some canonicalizations before loop splitting confuses the
65 // situation.
66 // TODO(tpopp): See if this is still necessary in the integrated version.
67 mlir::RewritePatternSet canonicalizations(func_op.getContext());
68 LoopOp::getCanonicalizationPatterns(canonicalizations,
69 func_op.getContext());
70 mlir::linalg::populateLinalgTilingCanonicalizationPatterns(
71 canonicalizations);
72 (void)applyPatternsAndFoldGreedily(func_op, std::move(canonicalizations));
73
74 mlir::RewritePatternSet loop_peeling(func_op.getContext());
75 loop_peeling.add<PeelGmlStLoop>(func_op.getContext());
76 (void)applyPatternsAndFoldGreedily(func_op, std::move(loop_peeling));
77
78 func_op->walk([&](LoopOp op) {
79 if (op->hasAttr(kWasPeeledAttr)) op->removeAttr(kWasPeeledAttr);
80 });
81 }
82 };
83
84 } // namespace
85
86 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
CreatePeelTiledLoopsPass()87 CreatePeelTiledLoopsPass() {
88 return std::make_unique<PeelTiledLoopsPass>();
89 }
90 } // namespace tensorflow
91