1 /* Copyright 2019 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 <stack>
17 
18 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
19 #include "mlir/Pass/Pass.h"  // from @llvm-project
20 #include "mlir/Support/LLVM.h"  // from @llvm-project
21 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
23 #include "tensorflow/compiler/mlir/tensorflow/transforms/shape_inference.h"
24 #include "tensorflow/core/framework/shape_inference.h"
25 
26 namespace mlir {
27 namespace TF {
28 
29 namespace {
30 
31 // This transformation pass propagate shapes on the TensorFlow graph.
32 // It is a ModulePass in order to be able to change function types.
33 class ShapeInference : public TensorFlowShapeInferencePassBase<ShapeInference> {
34  public:
runOnOperation()35   void runOnOperation() override {
36     auto failure_or_converged =
37         InferModuleShape(getOperation(), max_iterations_);
38     if (failed(failure_or_converged)) return signalPassFailure();
39     if (!failure_or_converged.getValue()) {
40       getOperation().emitError()
41           << "shape inference pass did not reach convergence after "
42           << max_iterations_;
43       return signalPassFailure();
44     }
45   }
46 };
47 }  // namespace
48 
CreateTFShapeInferencePass()49 std::unique_ptr<OperationPass<ModuleOp>> CreateTFShapeInferencePass() {
50   return std::make_unique<ShapeInference>();
51 }
52 
53 }  // namespace TF
54 }  // namespace mlir
55