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