/home/arjun/llvm-project/mlir/lib/IR/Dialect.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Dialect.cpp - Dialect implementation -------------------------------===// |
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/IR/Dialect.h" |
10 | | #include "mlir/IR/Diagnostics.h" |
11 | | #include "mlir/IR/DialectHooks.h" |
12 | | #include "mlir/IR/DialectImplementation.h" |
13 | | #include "mlir/IR/DialectInterface.h" |
14 | | #include "mlir/IR/MLIRContext.h" |
15 | | #include "mlir/IR/Operation.h" |
16 | | #include "llvm/ADT/MapVector.h" |
17 | | #include "llvm/ADT/Twine.h" |
18 | | #include "llvm/Support/ManagedStatic.h" |
19 | | #include "llvm/Support/Regex.h" |
20 | | |
21 | | using namespace mlir; |
22 | | using namespace detail; |
23 | | |
24 | 0 | DialectAsmParser::~DialectAsmParser() {} |
25 | | |
26 | | //===----------------------------------------------------------------------===// |
27 | | // Dialect Registration |
28 | | //===----------------------------------------------------------------------===// |
29 | | |
30 | | /// Registry for all dialect allocation functions. |
31 | | static llvm::ManagedStatic<llvm::MapVector<TypeID, DialectAllocatorFunction>> |
32 | | dialectRegistry; |
33 | | |
34 | | /// Registry for functions that set dialect hooks. |
35 | | static llvm::ManagedStatic<llvm::MapVector<TypeID, DialectHooksSetter>> |
36 | | dialectHooksRegistry; |
37 | | |
38 | | void Dialect::registerDialectAllocator( |
39 | 0 | TypeID typeID, const DialectAllocatorFunction &function) { |
40 | 0 | assert(function && |
41 | 0 | "Attempting to register an empty dialect initialize function"); |
42 | 0 | dialectRegistry->insert({typeID, function}); |
43 | 0 | } |
44 | | |
45 | | /// Registers a function to set specific hooks for a specific dialect, typically |
46 | | /// used through the DialectHooksRegistration template. |
47 | | void DialectHooks::registerDialectHooksSetter( |
48 | 0 | TypeID typeID, const DialectHooksSetter &function) { |
49 | 0 | assert( |
50 | 0 | function && |
51 | 0 | "Attempting to register an empty dialect hooks initialization function"); |
52 | 0 |
|
53 | 0 | dialectHooksRegistry->insert({typeID, function}); |
54 | 0 | } |
55 | | |
56 | | /// Registers all dialects and hooks from the global registries with the |
57 | | /// specified MLIRContext. |
58 | 0 | void mlir::registerAllDialects(MLIRContext *context) { |
59 | 0 | for (const auto &it : *dialectRegistry) |
60 | 0 | it.second(context); |
61 | 0 | for (const auto &it : *dialectHooksRegistry) |
62 | 0 | it.second(context); |
63 | 0 | } |
64 | | |
65 | | //===----------------------------------------------------------------------===// |
66 | | // Dialect |
67 | | //===----------------------------------------------------------------------===// |
68 | | |
69 | | Dialect::Dialect(StringRef name, MLIRContext *context) |
70 | 0 | : name(name), context(context) { |
71 | 0 | assert(isValidNamespace(name) && "invalid dialect namespace"); |
72 | 0 | registerDialect(context); |
73 | 0 | } |
74 | | |
75 | 0 | Dialect::~Dialect() {} |
76 | | |
77 | | /// Verify an attribute from this dialect on the argument at 'argIndex' for |
78 | | /// the region at 'regionIndex' on the given operation. Returns failure if |
79 | | /// the verification failed, success otherwise. This hook may optionally be |
80 | | /// invoked from any operation containing a region. |
81 | | LogicalResult Dialect::verifyRegionArgAttribute(Operation *, unsigned, unsigned, |
82 | 0 | NamedAttribute) { |
83 | 0 | return success(); |
84 | 0 | } |
85 | | |
86 | | /// Verify an attribute from this dialect on the result at 'resultIndex' for |
87 | | /// the region at 'regionIndex' on the given operation. Returns failure if |
88 | | /// the verification failed, success otherwise. This hook may optionally be |
89 | | /// invoked from any operation containing a region. |
90 | | LogicalResult Dialect::verifyRegionResultAttribute(Operation *, unsigned, |
91 | 0 | unsigned, NamedAttribute) { |
92 | 0 | return success(); |
93 | 0 | } |
94 | | |
95 | | /// Parse an attribute registered to this dialect. |
96 | 0 | Attribute Dialect::parseAttribute(DialectAsmParser &parser, Type type) const { |
97 | 0 | parser.emitError(parser.getNameLoc()) |
98 | 0 | << "dialect '" << getNamespace() |
99 | 0 | << "' provides no attribute parsing hook"; |
100 | 0 | return Attribute(); |
101 | 0 | } |
102 | | |
103 | | /// Parse a type registered to this dialect. |
104 | 0 | Type Dialect::parseType(DialectAsmParser &parser) const { |
105 | 0 | // If this dialect allows unknown types, then represent this with OpaqueType. |
106 | 0 | if (allowsUnknownTypes()) { |
107 | 0 | auto ns = Identifier::get(getNamespace(), getContext()); |
108 | 0 | return OpaqueType::get(ns, parser.getFullSymbolSpec(), getContext()); |
109 | 0 | } |
110 | 0 | |
111 | 0 | parser.emitError(parser.getNameLoc()) |
112 | 0 | << "dialect '" << getNamespace() << "' provides no type parsing hook"; |
113 | 0 | return Type(); |
114 | 0 | } |
115 | | |
116 | | /// Utility function that returns if the given string is a valid dialect |
117 | | /// namespace. |
118 | 0 | bool Dialect::isValidNamespace(StringRef str) { |
119 | 0 | if (str.empty()) |
120 | 0 | return true; |
121 | 0 | llvm::Regex dialectNameRegex("^[a-zA-Z_][a-zA-Z_0-9\\$]*$"); |
122 | 0 | return dialectNameRegex.match(str); |
123 | 0 | } |
124 | | |
125 | | /// Register a set of dialect interfaces with this dialect instance. |
126 | 0 | void Dialect::addInterface(std::unique_ptr<DialectInterface> interface) { |
127 | 0 | auto it = registeredInterfaces.try_emplace(interface->getID(), |
128 | 0 | std::move(interface)); |
129 | 0 | (void)it; |
130 | 0 | assert(it.second && "interface kind has already been registered"); |
131 | 0 | } |
132 | | |
133 | | //===----------------------------------------------------------------------===// |
134 | | // Dialect Interface |
135 | | //===----------------------------------------------------------------------===// |
136 | | |
137 | 0 | DialectInterface::~DialectInterface() {} |
138 | | |
139 | | DialectInterfaceCollectionBase::DialectInterfaceCollectionBase( |
140 | 0 | MLIRContext *ctx, TypeID interfaceKind) { |
141 | 0 | for (auto *dialect : ctx->getRegisteredDialects()) { |
142 | 0 | if (auto *interface = dialect->getRegisteredInterface(interfaceKind)) { |
143 | 0 | interfaces.insert(interface); |
144 | 0 | orderedInterfaces.push_back(interface); |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 0 | DialectInterfaceCollectionBase::~DialectInterfaceCollectionBase() {} |
150 | | |
151 | | /// Get the interface for the dialect of given operation, or null if one |
152 | | /// is not registered. |
153 | | const DialectInterface * |
154 | 0 | DialectInterfaceCollectionBase::getInterfaceFor(Operation *op) const { |
155 | 0 | return getInterfaceFor(op->getDialect()); |
156 | 0 | } |