xref: /aosp_15_r20/external/tensorflow/tensorflow/dtensor/mlir/dce.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
17 #include "mlir/IR/Builders.h"  // from @llvm-project
18 #include "mlir/IR/Operation.h"  // from @llvm-project
19 #include "mlir/Interfaces/SideEffectInterfaces.h"  // from @llvm-project
20 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
21 #include "mlir/Transforms/Passes.h"  // from @llvm-project
22 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h"
23 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes_classes.h"
24 
25 namespace tensorflow {
26 namespace dtensor {
27 
28 namespace {
29 
30 // MLIR pass that removes trivially unused operations in graph.
31 struct DTensorDCE : public DTensorDCEBase<DTensorDCE> {
runOnOperationtensorflow::dtensor::__anon96b2e55c0111::DTensorDCE32   void runOnOperation() override {
33     mlir::MLIRContext& context = getContext();
34     mlir::OpBuilder builder(&context);
35     getOperation().walk([](mlir::Operation* op) {
36       if (mlir::isOpTriviallyDead(op)) op->erase();
37     });
38   }
39 };
40 
41 }  // namespace
42 
CreateDTensorDCE()43 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> CreateDTensorDCE() {
44   return std::make_unique<DTensorDCE>();
45 }
46 
47 }  // namespace dtensor
48 }  // namespace tensorflow
49