MLIR 22.0.0git
TypeID.cpp
Go to the documentation of this file.
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
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
18using namespace mlir;
19
20#define DEBUG_TYPE "typeid"
21
22//===----------------------------------------------------------------------===//
23// TypeID Registry
24//===----------------------------------------------------------------------===//
25
26namespace {
27struct 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#ifndef NDEBUG
34#if defined(_MSC_VER)
35 if (typeName.contains("anonymous-namespace")) {
36#else
37 if (typeName.contains("anonymous namespace")) {
38#endif
39 std::string errorStr;
40 {
41 llvm::raw_string_ostream errorOS(errorStr);
42 errorOS << "TypeID::get<" << typeName
43 << ">(): Using TypeID on a class with an anonymous "
44 "namespace requires an explicit TypeID definition. The "
45 "implicit fallback uses string name, which does not "
46 "guarantee uniqueness in anonymous contexts. Define an "
47 "explicit TypeID instantiation for this type using "
48 "`MLIR_DECLARE_EXPLICIT_TYPE_ID`/"
49 "`MLIR_DEFINE_EXPLICIT_TYPE_ID` or "
50 "`MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID`.\n";
51 }
52 llvm::report_fatal_error(errorStr);
53 }
54#endif
55
56 { // Try a read-only lookup first.
57 llvm::sys::SmartScopedReader<true> guard(mutex);
58 auto it = typeNameToID.find(typeName);
59 if (it != typeNameToID.end())
60 return it->second;
61 }
62 llvm::sys::SmartScopedWriter<true> guard(mutex);
63 auto it = typeNameToID.try_emplace(typeName, TypeID());
64 if (it.second)
65 it.first->second = typeIDAllocator.allocate();
66 return it.first->second;
67 }
68
69 /// A mutex that guards access to the registry.
70 llvm::sys::SmartRWMutex<true> mutex;
71
72 /// An allocator used for TypeID objects.
73 TypeIDAllocator typeIDAllocator;
74
75 /// A map type name to TypeID.
76 DenseMap<StringRef, TypeID> typeNameToID;
77};
78} // end namespace
79
80LLVM_ALWAYS_EXPORT TypeID
82 static ImplicitTypeIDRegistry registry;
83 return registry.lookupOrInsert(name);
84}
85
86//===----------------------------------------------------------------------===//
87// Builtin TypeIDs
88//===----------------------------------------------------------------------===//
89
#define MLIR_DEFINE_EXPLICIT_SELF_OWNING_TYPE_ID(CLASS_NAME)
Definition TypeID.h:276
This class provides a way to define new TypeIDs at runtime.
Definition TypeID.h:349
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
static LLVM_ALWAYS_EXPORT TypeID registerImplicitTypeID(StringRef name)
Register an implicit type ID for the given type name.
Definition TypeID.cpp:81
Include the generated interface declarations.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:126