| 1 | //===- TypeID.cpp - MLIR TypeID -------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Support/TypeID.h" |
| 10 | #include "llvm/ADT/DenseMap.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "llvm/Support/RWMutex.h" |
| 14 | |
| 15 | #include "llvm/Support/Signals.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | using namespace mlir; |
| 19 | |
| 20 | #define DEBUG_TYPE "typeid" |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // TypeID Registry |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace { |
| 27 | struct ImplicitTypeIDRegistry { |
| 28 | /// Lookup or insert a TypeID for the given type name. |
| 29 | TypeID lookupOrInsert(StringRef typeName) { |
| 30 | // Perform a heuristic check to see if this type is in an anonymous |
| 31 | // namespace. String equality is not valid for anonymous types, so we try to |
| 32 | // abort whenever we see them. |
| 33 | // Check all known anonymous-namespace markers unconditionally: |
| 34 | // Clang : "(anonymous namespace)" |
| 35 | // GCC : "{anonymous}" |
| 36 | // MSVC : "anonymous-namespace" |
| 37 | #ifndef NDEBUG |
| 38 | if (typeName.contains("anonymous namespace" ) || |
| 39 | typeName.contains("{anonymous}" ) || |
| 40 | typeName.contains("anonymous-namespace" )) { |
| 41 | std::string errorStr; |
| 42 | { |
| 43 | llvm::raw_string_ostream errorOS(errorStr); |
| 44 | errorOS << "TypeID::get<" << typeName |
| 45 | << ">(): Using TypeID on a class with an anonymous " |
| 46 | "namespace requires an explicit TypeID definition. The " |
| 47 | "implicit fallback uses string name, which does not " |
| 48 | "guarantee uniqueness in anonymous contexts. Define an " |
| 49 | "explicit TypeID instantiation for this type using " |
| 50 | "`MLIR_DECLARE_EXPLICIT_TYPE_ID`/" |
| 51 | "`MLIR_DEFINE_EXPLICIT_TYPE_ID` or " |
| 52 | "`MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID`.\n" ; |
| 53 | } |
| 54 | llvm::report_fatal_error(llvm::StringRef(errorStr)); |
| 55 | } |
| 56 | #endif |
| 57 | |
| 58 | { // Try a read-only lookup first. |
| 59 | llvm::sys::SmartScopedReader<true> guard(mutex); |
| 60 | auto it = typeNameToID.find(Val: typeName); |
| 61 | if (it != typeNameToID.end()) |
| 62 | return it->second; |
| 63 | } |
| 64 | llvm::sys::SmartScopedWriter<true> guard(mutex); |
| 65 | auto it = typeNameToID.try_emplace(Key: typeName, Args: TypeID()); |
| 66 | if (it.second) |
| 67 | it.first->second = typeIDAllocator.allocate(); |
| 68 | return it.first->second; |
| 69 | } |
| 70 | |
| 71 | /// A mutex that guards access to the registry. |
| 72 | llvm::sys::SmartRWMutex<true> mutex; |
| 73 | |
| 74 | /// An allocator used for TypeID objects. |
| 75 | TypeIDAllocator typeIDAllocator; |
| 76 | |
| 77 | /// A map type name to TypeID. |
| 78 | DenseMap<StringRef, TypeID> typeNameToID; |
| 79 | }; |
| 80 | } // end namespace |
| 81 | |
| 82 | LLVM_ALWAYS_EXPORT TypeID |
| 83 | detail::FallbackTypeIDResolver::registerImplicitTypeID(StringRef name) { |
| 84 | static ImplicitTypeIDRegistry registry; |
| 85 | return registry.lookupOrInsert(typeName: name); |
| 86 | } |
| 87 | |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | // Builtin TypeIDs |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | |
| 92 | MLIR_DEFINE_EXPLICIT_SELF_OWNING_TYPE_ID(void) |
| 93 | |