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_ops.h" 19 20 namespace mlir { 21 namespace TF { 22 23 namespace { 24 25 constexpr char kShapeInvariantAttr[] = "shape_invariant"; 26 27 // Drop `shape_invariant` attribute from tf.While and tf.WhileRegion op. This 28 // would allow shape inference pass to further refine operand/result shapes of 29 // these ops. This is only safe to do when compiling to XLA. 30 class DropWhileShapeInvariantPass 31 : public PassWrapper<DropWhileShapeInvariantPass, FunctionPass> { 32 void runOnFunction() override; 33 }; 34 runOnFunction()35void DropWhileShapeInvariantPass::runOnFunction() { 36 getFunction().walk([](Operation* op) { 37 if (llvm::isa<WhileOp, WhileRegionOp>(op)) 38 op->removeAttr(kShapeInvariantAttr); 39 }); 40 } 41 42 static PassRegistration<DropWhileShapeInvariantPass> pass( 43 "tf-drop-while-shape-invariant", 44 "Drop `shape_invariant` attrbute from While/WhileRegion ops."); 45 46 } // namespace 47 CreateDropWhileShapeInvariantPass()48std::unique_ptr<OperationPass<FuncOp>> CreateDropWhileShapeInvariantPass() { 49 return std::make_unique<DropWhileShapeInvariantPass>(); 50 } 51 52 } // namespace TF 53 } // namespace mlir 54