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 "llvm/ADT/STLExtras.h" 17 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 18 #include "mlir/Pass/Pass.h" // from @llvm-project 19 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h" 20 21 namespace mlir { 22 namespace TF { 23 24 namespace { 25 26 class TFEnsureStaticShapes 27 : public TensorFlowEnsureStaticShapesPassBase<TFEnsureStaticShapes> { 28 public: runOnFunction()29 void runOnFunction() override { 30 auto result = getFunction()->walk([&](mlir::Operation* op) { 31 auto is_dynamic_shape_type = [](const mlir::Type type) { 32 if (ShapedType shaped_type = type.dyn_cast<ShapedType>()) { 33 return !shaped_type.hasStaticShape(); 34 } 35 return false; 36 }; 37 38 if (llvm::any_of(op->getResultTypes(), is_dynamic_shape_type) || 39 llvm::any_of(op->getOperandTypes(), is_dynamic_shape_type)) { 40 return WalkResult::interrupt(); 41 } 42 43 return WalkResult::advance(); 44 }); 45 46 if (result.wasInterrupted()) return signalPassFailure(); 47 } 48 }; 49 50 } // namespace 51 CreateTFEnsureStaticShapesPass()52std::unique_ptr<OperationPass<FuncOp>> CreateTFEnsureStaticShapesPass() { 53 return std::make_unique<TFEnsureStaticShapes>(); 54 } 55 56 } // namespace TF 57 } // namespace mlir 58