• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "mlir/Analysis/BufferViewFlowAnalysis.h"  // from @llvm-project
21 #include "mlir/Dialect/Affine/IR/AffineOps.h"  // from @llvm-project
22 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"  // from @llvm-project
23 #include "mlir/Dialect/Linalg/Transforms/CodegenStrategy.h"  // from @llvm-project
24 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"  // from @llvm-project
25 #include "mlir/Dialect/MemRef/IR/MemRef.h"  // from @llvm-project
26 #include "mlir/Dialect/SCF/SCF.h"  // from @llvm-project
27 #include "mlir/Dialect/Shape/IR/Shape.h"  // from @llvm-project
28 #include "mlir/Dialect/StandardOps/IR/Ops.h"  // from @llvm-project
29 #include "mlir/IR/AffineMap.h"  // from @llvm-project
30 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
31 #include "mlir/IR/ImplicitLocOpBuilder.h"  // from @llvm-project
32 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"  // from @llvm-project
33 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h"
34 #include "tensorflow/compiler/mlir/tools/kernel_gen/ir/tf_framework_ops.h"
35 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
36 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/rewriters.h"
37 
38 namespace mlir {
39 namespace kernel_gen {
40 namespace transforms {
41 namespace {
42 #define GEN_PASS_CLASSES
43 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
44 
45 // A pass to remove memref::AllocOps and memref::CopyOps ops if the only user
46 // of the copy is a linalg::GenericOp.
RemoveMemrefCopy(FuncOp func)47 void RemoveMemrefCopy(FuncOp func) {
48   llvm::SmallVector<memref::AllocOp, 8> allocs_to_remove;
49   llvm::SmallVector<memref::CopyOp, 8> copies_to_remove;
50 
51   // Gather all allocs and copies which are consumed by linalg::GenericOp ops.
52   func->walk([&](memref::AllocOp op) {
53     memref::CopyOp copy;
54     linalg::GenericOp generic;
55     bool at_most_one_copy = true;
56     bool at_most_one_generic = true;
57     for (auto user : op->getUsers()) {
58       if (auto copy_user = dyn_cast<memref::CopyOp>(user)) {
59         if (copy) {
60           at_most_one_copy = false;
61         } else {
62           copy = copy_user;
63         }
64       } else if (auto generic_user = dyn_cast<linalg::GenericOp>(user)) {
65         if (generic) {
66           at_most_one_generic = false;
67         } else {
68           generic = generic_user;
69         }
70       }
71     }
72     if (!copy || !at_most_one_copy) return;
73     if (!generic || !at_most_one_generic) return;
74     // The copy should have the alloc op as target.
75     if (copy.getTarget() != op.getResult()) return;
76 
77     // The copy should be before the linalg::GenericOp.
78     if (copy->getBlock() != generic->getBlock() ||
79         !copy->isBeforeInBlock(generic)) {
80       return;
81     }
82     allocs_to_remove.push_back(op);
83     copies_to_remove.push_back(copy);
84   });
85   for (auto it : llvm::zip(allocs_to_remove, copies_to_remove)) {
86     auto alloc_op = std::get<0>(it);
87     auto copy_op = std::get<1>(it);
88     auto source = copy_op.getSource().getDefiningOp();
89     copy_op->erase();
90     alloc_op->replaceAllUsesWith(source);
91     alloc_op->erase();
92   }
93 }
94 }  // namespace
95 
96 struct CopyCleanupPass : public CopyCleanupPassBase<CopyCleanupPass> {
getDependentDialectsmlir::kernel_gen::transforms::CopyCleanupPass97   void getDependentDialects(DialectRegistry &registry) const override {
98     registry.insert<memref::MemRefDialect, linalg::LinalgDialect>();
99   }
100 
runOnFunctionmlir::kernel_gen::transforms::CopyCleanupPass101   void runOnFunction() override { RemoveMemrefCopy(getFunction()); }
102 };
103 
CreateCopyCleanupPass()104 std::unique_ptr<FunctionPass> CreateCopyCleanupPass() {
105   return std::make_unique<CopyCleanupPass>();
106 }
107 
108 }  // namespace transforms
109 }  // namespace kernel_gen
110 }  // namespace mlir
111