1/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
24namespace tensorflow {
25namespace dtensor {
26
27namespace {
28#define GEN_PASS_DEF_DTENSORDCE
29#include "tensorflow/dtensor/mlir/dtensor_passes.h.inc"
30
31// MLIR pass that removes trivially unused operations in graph.
32struct DTensorDCE : public impl::DTensorDCEBase<DTensorDCE> {
33 void runOnOperation() override {
34 mlir::MLIRContext& context = getContext();
35 mlir::OpBuilder builder(&context);
36 getOperation().walk([](mlir::Operation* op) {
37 if (mlir::isOpTriviallyDead(op)) op->erase();
38 });
39 }
40};
41
42} // namespace
43
44std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> CreateDTensorDCE() {
45 return std::make_unique<DTensorDCE>();
46}
47
48} // namespace dtensor
49} // namespace tensorflow
50