1 /* Copyright 2022 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 XLA_RUNTIME_TYPE_ID_H_ 17 #define XLA_RUNTIME_TYPE_ID_H_ 18 19 #include <cstdint> 20 #include <functional> 21 #include <memory> 22 23 #include "llvm/ADT/DenseMap.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "mlir/Support/TypeID.h" // from @llvm-project 26 27 namespace xla { 28 namespace runtime { 29 30 using ::mlir::TypeID; // NOLINT 31 32 class TypeIDNameRegistry { 33 public: 34 using RegistrationFn = std::function<void(TypeIDNameRegistry&)>; 35 36 TypeIDNameRegistry() = default; 37 ~TypeIDNameRegistry() = default; 38 39 template <typename T> Register(llvm::StringRef type_name)40 void Register(llvm::StringRef type_name) { 41 auto type_id = TypeID::get<T>(); 42 auto inserted = type_id_name_map_.try_emplace(type_id, type_name); 43 assert(inserted.second && "duplicate typeid name registration"); 44 (void)inserted; 45 } 46 47 llvm::StringRef FindTypeIDSymbolName(TypeID type_id); 48 ForEach(std::function<void (llvm::StringRef,TypeID)> f)49 void ForEach(std::function<void(llvm::StringRef, TypeID)> f) const { 50 for (auto& kv : type_id_name_map_) f(kv.second, kv.first); 51 } 52 53 private: 54 llvm::DenseMap<TypeID, llvm::StringRef> type_id_name_map_; 55 }; 56 57 } // namespace runtime 58 } // namespace xla 59 60 #endif // XLA_RUNTIME_TYPE_ID_H_ 61