1 /* Copyright 2022 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 "tensorflow/core/transforms/functional_to_region/pass.h"
17
18 #include <memory>
19 #include <utility>
20
21 #include "mlir/IR/PatternMatch.h" // from @llvm-project
22 #include "mlir/IR/SymbolTable.h" // from @llvm-project
23 #include "mlir/Pass/PassManager.h" // from @llvm-project
24 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project
25 #include "tensorflow/core/transforms/functional_to_region/impl.h"
26 #include "tensorflow/core/transforms/pass_detail.h"
27
28 namespace mlir {
29 namespace tfg {
30
31 namespace {
32 struct FunctionalToRegionPass
33 : public FunctionalToRegionBase<FunctionalToRegionPass> {
runOnOperationmlir::tfg::__anon160826730111::FunctionalToRegionPass34 void runOnOperation() override {
35 SymbolTable table(getOperation());
36 RewritePatternSet patterns(&getContext());
37 PopulateFunctionalToRegionPatterns(patterns, table);
38
39 GreedyRewriteConfig config;
40 // Use top-down traversal for more efficient conversion. Disable region
41 // simplification as all regions are single block.
42 config.useTopDownTraversal = true;
43 config.enableRegionSimplification = false;
44 // If there are deeply nested conditionals, instantiating them too deep will
45 // cause the verifiers, which are implemented recursively, to stack
46 // overflow. Set a relatively low iteration limit.
47 config.maxIterations = 16;
48 if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns),
49 config)))
50 signalPassFailure();
51 }
52 };
53 } // namespace
54
CreateFunctionalToRegionPass()55 std::unique_ptr<Pass> CreateFunctionalToRegionPass() {
56 return std::make_unique<FunctionalToRegionPass>();
57 }
58
59 } // namespace tfg
60 } // namespace mlir
61