• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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/Pass/Pass.h"  // from @llvm-project
17 #include "mlir/Pass/PassRegistry.h"  // from @llvm-project
18 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
20 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
21 
22 namespace mlir {
23 namespace TF {
24 
25 namespace {
26 
27 constexpr char kShapeInvariantAttr[] = "shape_invariant";
28 
29 class DropWhileShapeInvariantPass
30     : public DropWhileShapeInvariantPassBase<DropWhileShapeInvariantPass> {
31   void runOnFunction() override;
32 };
33 
34 class DropWhileShapeInvariantInDeviceClusterPass
35     : public DropWhileShapeInvariantInDeviceClusterPassBase<
36           DropWhileShapeInvariantInDeviceClusterPass> {
37   void runOnFunction() override;
38 };
39 
DropWhileShapeInvariantAttr(Operation * op)40 void DropWhileShapeInvariantAttr(Operation* op) {
41   if (llvm::isa<WhileOp, WhileRegionOp>(op))
42     op->removeAttr(kShapeInvariantAttr);
43 }
runOnFunction()44 void DropWhileShapeInvariantPass::runOnFunction() {
45   getFunction().walk([](Operation* op) { DropWhileShapeInvariantAttr(op); });
46 }
47 
runOnFunction()48 void DropWhileShapeInvariantInDeviceClusterPass::runOnFunction() {
49   getFunction().walk([](tf_device::ClusterOp cluster) {
50     cluster.walk([](Operation* op) { DropWhileShapeInvariantAttr(op); });
51   });
52 }
53 }  // namespace
54 
CreateDropWhileShapeInvariantPass()55 std::unique_ptr<OperationPass<FuncOp>> CreateDropWhileShapeInvariantPass() {
56   return std::make_unique<DropWhileShapeInvariantPass>();
57 }
58 
59 std::unique_ptr<OperationPass<FuncOp>>
CreateDropWhileShapeInvariantInDeviceClusterPass()60 CreateDropWhileShapeInvariantInDeviceClusterPass() {
61   return std::make_unique<DropWhileShapeInvariantInDeviceClusterPass>();
62 }
63 
64 }  // namespace TF
65 }  // namespace mlir
66