• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "absl/strings/ascii.h"
19 #include "absl/strings/match.h"
20 #include "absl/strings/numbers.h"
21 #include "absl/strings/str_cat.h"
22 #include "tensorflow/compiler/xla/primitive_util.h"
23 #include "tensorflow/compiler/xla/types.h"
24 #include "tensorflow/core/platform/logging.h"
25 #include "tensorflow/core/platform/types.h"
26 
27 namespace xla {
28 
29 namespace {
30 
IsAllowed(char character)31 bool IsAllowed(char character) {
32   auto c = static_cast<unsigned char>(character);
33   return (absl::ascii_isalnum(c) != 0) || c == '_' || c == '.' || c == '-';
34 }
35 
36 }  // namespace
37 
NameUniquer(const string & separator)38 NameUniquer::NameUniquer(const string& separator) {
39   CHECK(absl::c_all_of(separator, IsAllowed))
40       << "separator should comprises allowed characters only";
41   separator_ = separator;
42 }
43 
GetSanitizedName(const string & name)44 /*static*/ string NameUniquer::GetSanitizedName(const string& name) {
45   if (name.empty()) {
46     return "";
47   }
48 
49   string result = name;
50   char c = static_cast<unsigned char>(result[0]);
51   if (!absl::ascii_isalpha(c) && c != '_') {
52     result[0] = '_';
53   }
54   for (int i = 1; i < result.length(); i++) {
55     if (!IsAllowed(result[i])) {
56       result[i] = '_';
57     }
58   }
59 
60   // HLO primitive type names (with the exception of 'tuple') are keywords in
61   // the HLO text representation and cannot be names, so append an underscore if
62   // the name is a primitive type.
63   if (primitive_util::IsPrimitiveTypeName(result) && result != "tuple") {
64     result += "_";
65   }
66 
67   if (absl::StartsWith(result, "__") && !absl::StartsWith(result, "__xla_")) {
68     // Morph name prefix __ that is not __xla_, to avoid using name prefixes
69     // reserved by the backends, such as __llvm_retpoline_ reserved by the LLVM
70     // x86 backend.
71     result[0] = 'a';
72   }
73 
74   return result;
75 }
76 
GetUniqueName(absl::string_view prefix)77 string NameUniquer::GetUniqueName(absl::string_view prefix) {
78   string root = GetSanitizedName(prefix.empty() ? "name" : string(prefix));
79 
80   // Strip away numeric suffix (if any). Only recognize separator if it is in
81   // the middle of the name.
82   bool has_numeric_suffix = false;
83   int64 numeric_suffix = 0;
84   size_t separator_index = root.rfind(separator_);
85   if (separator_index != string::npos && (separator_index > 0) &&
86       (separator_index < root.size() - 1)) {
87     string after_suffix = root.substr(separator_index + 1);
88     if (absl::SimpleAtoi(after_suffix, &numeric_suffix)) {
89       has_numeric_suffix = true;
90       // Remove numeric suffix from root.
91       root = root.substr(0, separator_index);
92     } else {
93       // absl::SimpleAtoi may modify numeric_suffix even if it returns false.
94       numeric_suffix = 0;
95     }
96   }
97 
98   SequentialIdGenerator& id_generator = generated_names_[root];
99   numeric_suffix = id_generator.RegisterId(numeric_suffix);
100   if (numeric_suffix == 0) {
101     return has_numeric_suffix ? absl::StrCat(root, separator_, 0) : root;
102   }
103   absl::StrAppend(&root, separator_, numeric_suffix);
104   return root;
105 }
106 
107 }  // namespace xla
108