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 #include "tensorflow/compiler/xla/service/name_uniquer.h"
17 
18 #include "tensorflow/compiler/xla/types.h"
19 #include "tensorflow/core/lib/strings/strcat.h"
20 #include "tensorflow/core/platform/logging.h"
21 #include "tensorflow/core/platform/types.h"
22 
23 namespace xla {
24 
25 namespace {
26 
IsAllowed(char character)27 bool IsAllowed(char character) {
28   auto c = static_cast<unsigned char>(character);
29   return (isalnum(c) != 0) || c == '_' || c == '.' || c == '-';
30 }
31 
32 }  // namespace
33 
NameUniquer(const string & separator)34 NameUniquer::NameUniquer(const string& separator) {
35   CHECK(std::all_of(separator.begin(), separator.end(), IsAllowed))
36       << "separator should comprises allowed characters only";
37   separator_ = separator;
38 }
39 
GetSanitizedName(const string & name)40 /*static*/ string NameUniquer::GetSanitizedName(const string& name) {
41   string result = name;
42   CHECK(!result.empty()) << "name should not be empty";
43   char c = static_cast<unsigned char>(result[0]);
44   if (!isalpha(c) && c != '_') {
45     result[0] = '_';
46   }
47   for (int i = 1; i < result.length(); i++) {
48     if (!IsAllowed(result[i])) {
49       result[i] = '_';
50     }
51   }
52   return result;
53 }
54 
GetUniqueName(tensorflow::StringPiece prefix)55 string NameUniquer::GetUniqueName(tensorflow::StringPiece prefix) {
56   string root = prefix.empty() ? "name" : prefix.ToString();
57   root = GetSanitizedName(root);
58 
59   // Strip away numeric suffix (if any). Only recognize separator if it is in
60   // the middle of the name.
61   size_t separator_index = root.rfind(separator_);
62   if (separator_index != string::npos && (separator_index > 0) &&
63       (separator_index < root.size() - 1)) {
64     string after_suffix = root.substr(separator_index + 1);
65     int64 numeric_suffix;
66     if (tensorflow::strings::safe_strto64(after_suffix, &numeric_suffix)) {
67       // Remove numeric suffix from root.
68       root = root.substr(0, separator_index);
69       // Update count to at least the numeric suffix value to avoid future
70       // colisions with this name.
71       generated_names_[root] = std::max(generated_names_[root], numeric_suffix);
72     }
73   }
74 
75   int64* count = &(generated_names_[root]);
76   if (*count == 0) {
77     *count = 1;
78     return root;
79   } else {
80     tensorflow::strings::StrAppend(&root, separator_, *count);
81     // Increment lookup under old 'root' name.
82     (*count)++;
83     return root;
84   }
85 }
86 
87 }  // namespace xla
88