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 #ifndef TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
17 #define TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
18 
19 #include "absl/strings/str_join.h"
20 #include "tensorflow/compiler/xla/status_macros.h"
21 #include "tensorflow/core/graph/graph.h"
22 
23 // Utility functions shared between functionalize cond and while.
24 
25 namespace tensorflow {
26 
27 // Check that the graph has no cycle containing the given node.
28 Status CheckNodeNotInCycle(const Node* node, const int num_nodes);
29 
30 // Comparison function used for sorting nodes consistently.
31 // a) resource variables are last, and
32 // b) sort lexicographically by name (for deterministic output).
33 struct NodeCmpByNameResourcesLast {
34   bool operator()(const Node* lhs, const Node* rhs) const;
35 };
36 
37 // Returns the Node* created from the NodeDef in the Graph.
38 xla::StatusOr<Node*> AddNodeDefToGraph(const NodeDef& node_def, Graph* graph);
39 
40 // Build a retval node of given type and index.
41 xla::StatusOr<Node*> BuildRetvalNode(Graph* graph, DataType type, int index);
42 
43 // Returns a textual representation of the names of the nodes in the input.
44 template <typename T>
NodesToString(const T & nodes)45 string NodesToString(const T& nodes) {
46   return absl::StrCat("{",
47                       absl::StrJoin(nodes, ",",
48                                     [](string* output, const Node* node) {
49                                       absl::StrAppend(output, node->name());
50                                     }),
51                       "}");
52 }
53 
54 }  // namespace tensorflow
55 
56 #endif  // TENSORFLOW_COMPILER_TF2XLA_FUNCTIONALIZE_CONTROL_FLOW_UTIL_H_
57