1 /* Copyright 2017 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 // Contains helpers for use in shape inference.
17 
18 #include "tensorflow/compiler/jit/shape_inference_helpers.h"
19 
20 #include <vector>
21 
22 #include "tensorflow/core/graph/graph.h"
23 
24 namespace tensorflow {
25 
Remove(Graph * graph)26 Status BackEdgeHelper::Remove(Graph* graph) {
27   if (graph_ != nullptr) {
28     return errors::Internal("BackEdgeHelper duplicate call to Remove.");
29   }
30   graph_ = graph;
31   for (Node* n : graph_->nodes()) {
32     if (n->IsMerge()) {
33       for (const Edge* e : n->in_edges()) {
34         if (e->src()->IsNextIteration()) {
35           back_edges_.push_back(
36               BackEdge{e, e->src(), e->src_output(), e->dst(), e->dst_input()});
37         }
38       }
39     }
40   }
41   for (const BackEdge& be : back_edges_) {
42     graph_->RemoveEdge(be.edge);
43   }
44   return Status::OK();
45 }
46 
RemovedEdges() const47 const std::vector<BackEdgeHelper::BackEdge>& BackEdgeHelper::RemovedEdges()
48     const {
49   return back_edges_;
50 }
51 
Replace()52 Status BackEdgeHelper::Replace() {
53   if (graph_ == nullptr) {
54     return errors::Internal("BackEdgeHelper Replace called before Remove.");
55   }
56   if (replaced_) {
57     return errors::Internal("BackEdgeHelper Replace called more than once.");
58   }
59   replaced_ = true;
60   for (const BackEdge& be : back_edges_) {
61     graph_->AddEdge(be.src, be.src_output, be.dst, be.dst_input);
62   }
63   return Status::OK();
64 }
65 
66 }  // namespace tensorflow
67