• 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_NAME_UNIQUER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_NAME_UNIQUER_H_
18 
19 #include <string>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/container/flat_hash_set.h"
23 #include "absl/strings/string_view.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/core/platform/macros.h"
26 
27 namespace xla {
28 
29 // Simple stateful class that helps generate "unique" names. To use it, simply
30 // call GetUniqueName as many times as needed. The names returned by
31 // GetUniqueName are guaranteed to be distinct for this instance of the class.
32 // Note that the names will be sanitized to match regexp
33 // "[a-zA-Z_][a-zA-Z0-9_.-]*".
34 class NameUniquer {
35  public:
36   // The separator must contain allowed characters only: "[a-zA-Z0-9_.-]".
37   explicit NameUniquer(const string& separator = "__");
38 
39   // Get a sanitized unique name in a string, with an optional prefix for
40   // convenience.
41   string GetUniqueName(absl::string_view prefix = "");
42 
43   // Sanitizes and returns the name. Unallowed characters will be replaced with
44   // '_'. The result will match the regexp "[a-zA-Z_][a-zA-Z0-9_.-]*".
45   static string GetSanitizedName(const string& name);
46 
47  private:
48   // Used to track and generate new identifiers for the same instruction name
49   // root.
50   class SequentialIdGenerator {
51    public:
52     SequentialIdGenerator() = default;
53 
54     // Tries to register id as used identifier. If id is not already used, the
55     // id itself will be returned. Otherwise a new one will be generated, and
56     // returned.
RegisterId(int64 id)57     int64 RegisterId(int64 id) {
58       if (used_.insert(id).second) {
59         return id;
60       }
61       while (!used_.insert(next_).second) {
62         ++next_;
63       }
64       return next_++;
65     }
66 
67    private:
68     // The next identifier to be tried.
69     int64 next_ = 0;
70 
71     // Set of all the identifiers which has been used.
72     absl::flat_hash_set<int64> used_;
73   };
74 
75   // The string to use to separate the prefix of the name from the uniquing
76   // integer value.
77   string separator_;
78 
79   // Map from name prefix to the generator data structure which tracks used
80   // identifiers and generates new ones.
81   absl::flat_hash_map<string, SequentialIdGenerator> generated_names_;
82 
83   TF_DISALLOW_COPY_AND_ASSIGN(NameUniquer);
84 };
85 
86 }  // namespace xla
87 
88 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_NAME_UNIQUER_H_
89