1//===- AttrTypeSubElements.cpp - Attr and Type SubElement Interfaces ------===//
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/Operation.h"
10#include <optional>
11
12using namespace mlir;
13
14//===----------------------------------------------------------------------===//
15// AttrTypeWalker
16//===----------------------------------------------------------------------===//
17
18WalkResult AttrTypeWalker::walkImpl(Attribute attr, WalkOrder order) {
19 return walkImpl(element: attr, walkFns&: attrWalkFns, order);
20}
21WalkResult AttrTypeWalker::walkImpl(Type type, WalkOrder order) {
22 return walkImpl(element: type, walkFns&: typeWalkFns, order);
23}
24
25template <typename T, typename WalkFns>
26WalkResult AttrTypeWalker::walkImpl(T element, WalkFns &walkFns,
27 WalkOrder order) {
28 // Check if we've already walk this element before.
29 auto key = std::make_pair(element.getAsOpaquePointer(), (int)order);
30 auto [it, inserted] =
31 visitedAttrTypes.try_emplace(key, WalkResult::advance());
32 if (!inserted)
33 return it->second;
34
35 // If we are walking in post order, walk the sub elements first.
36 if (order == WalkOrder::PostOrder) {
37 if (walkSubElements(element, order).wasInterrupted())
38 return visitedAttrTypes[key] = WalkResult::interrupt();
39 }
40
41 // Walk this element, bailing if skipped or interrupted.
42 for (auto &walkFn : llvm::reverse(walkFns)) {
43 WalkResult walkResult = walkFn(element);
44 if (walkResult.wasInterrupted())
45 return visitedAttrTypes[key] = WalkResult::interrupt();
46 if (walkResult.wasSkipped())
47 return WalkResult::advance();
48 }
49
50 // If we are walking in pre-order, walk the sub elements last.
51 if (order == WalkOrder::PreOrder) {
52 if (walkSubElements(element, order).wasInterrupted())
53 return WalkResult::interrupt();
54 }
55 return WalkResult::advance();
56}
57
58template <typename T>
59WalkResult AttrTypeWalker::walkSubElements(T interface, WalkOrder order) {
60 WalkResult result = WalkResult::advance();
61 auto walkFn = [&](auto element) {
62 if (element && !result.wasInterrupted())
63 result = walkImpl(element, order);
64 };
65 interface.walkImmediateSubElements(walkFn, walkFn);
66 return result.wasInterrupted() ? result : WalkResult::advance();
67}
68
69//===----------------------------------------------------------------------===//
70/// AttrTypeReplacerBase
71//===----------------------------------------------------------------------===//
72
73template <typename Concrete>
74void detail::AttrTypeReplacerBase<Concrete>::addReplacement(
75 ReplaceFn<Attribute> fn) {
76 attrReplacementFns.emplace_back(args: std::move(fn));
77}
78
79template <typename Concrete>
80void detail::AttrTypeReplacerBase<Concrete>::addReplacement(
81 ReplaceFn<Type> fn) {
82 typeReplacementFns.push_back(x: std::move(fn));
83}
84
85template <typename Concrete>
86void detail::AttrTypeReplacerBase<Concrete>::replaceElementsIn(
87 Operation *op, bool replaceAttrs, bool replaceLocs, bool replaceTypes) {
88 // Functor that replaces the given element if the new value is different,
89 // otherwise returns nullptr.
90 auto replaceIfDifferent = [&](auto element) {
91 auto replacement = static_cast<Concrete *>(this)->replace(element);
92 return (replacement && replacement != element) ? replacement : nullptr;
93 };
94
95 // Update the attribute dictionary.
96 if (replaceAttrs) {
97 if (auto newAttrs = replaceIfDifferent(op->getRawDictionaryAttrs()))
98 op->setDiscardableAttrs(cast<DictionaryAttr>(newAttrs));
99
100 if (op->getPropertiesStorageSize()) {
101 op->getName().walkInherentAttrs(op, visitor: [&](StringRef, Attribute &attr) {
102 if (Attribute replacement = replaceIfDifferent(attr))
103 attr = replacement;
104 });
105 }
106 }
107
108 // If we aren't updating locations or types, we're done.
109 if (!replaceTypes && !replaceLocs)
110 return;
111
112 // Update the location.
113 if (replaceLocs) {
114 if (Attribute newLoc = replaceIfDifferent(op->getLoc()))
115 op->setLoc(cast<LocationAttr>(Val&: newLoc));
116 }
117
118 // Update the result types.
119 if (replaceTypes) {
120 for (OpResult result : op->getResults())
121 if (Type newType = replaceIfDifferent(result.getType()))
122 result.setType(newType);
123 }
124
125 // Update any nested block arguments.
126 for (Region &region : op->getRegions()) {
127 for (Block &block : region) {
128 for (BlockArgument &arg : block.getArguments()) {
129 if (replaceLocs) {
130 if (Attribute newLoc = replaceIfDifferent(arg.getLoc()))
131 arg.setLoc(cast<LocationAttr>(Val&: newLoc));
132 }
133
134 if (replaceTypes) {
135 if (Type newType = replaceIfDifferent(arg.getType()))
136 arg.setType(newType);
137 }
138 }
139 }
140 }
141}
142
143template <typename Concrete>
144void detail::AttrTypeReplacerBase<Concrete>::recursivelyReplaceElementsIn(
145 Operation *op, bool replaceAttrs, bool replaceLocs, bool replaceTypes) {
146 op->walk([&](Operation *nestedOp) {
147 replaceElementsIn(op: nestedOp, replaceAttrs, replaceLocs, replaceTypes);
148 });
149}
150
151template <typename T, typename Replacer>
152static void updateSubElementImpl(T element, Replacer &replacer,
153 SmallVectorImpl<T> &newElements,
154 FailureOr<bool> &changed) {
155 // Bail early if we failed at any point.
156 if (failed(Result: changed))
157 return;
158
159 // Guard against potentially null inputs. We always map null to null.
160 if (!element) {
161 newElements.push_back(nullptr);
162 return;
163 }
164
165 // Replace the element.
166 if (T result = replacer.replace(element)) {
167 newElements.push_back(result);
168 if (result != element)
169 changed = true;
170 } else {
171 changed = failure();
172 }
173}
174
175template <typename T, typename Replacer>
176static T replaceSubElements(T interface, Replacer &replacer) {
177 // Walk the current sub-elements, replacing them as necessary.
178 SmallVector<Attribute, 16> newAttrs;
179 SmallVector<Type, 16> newTypes;
180 FailureOr<bool> changed = false;
181 interface.walkImmediateSubElements(
182 [&](Attribute element) {
183 updateSubElementImpl(element, replacer, newAttrs, changed);
184 },
185 [&](Type element) {
186 updateSubElementImpl(element, replacer, newTypes, changed);
187 });
188 if (failed(Result: changed))
189 return nullptr;
190
191 // If any sub-elements changed, use the new elements during the replacement.
192 T result = interface;
193 if (*changed)
194 result = interface.replaceImmediateSubElements(newAttrs, newTypes);
195 return result;
196}
197
198/// Shared implementation of replacing a given attribute or type element.
199template <typename T, typename ReplaceFns, typename Replacer>
200static T replaceElementImpl(T element, ReplaceFns &replaceFns,
201 Replacer &replacer) {
202 T result = element;
203 WalkResult walkResult = WalkResult::advance();
204 for (auto &replaceFn : llvm::reverse(replaceFns)) {
205 if (std::optional<std::pair<T, WalkResult>> newRes = replaceFn(element)) {
206 std::tie(result, walkResult) = *newRes;
207 break;
208 }
209 }
210
211 // If an error occurred, return nullptr to indicate failure.
212 if (walkResult.wasInterrupted() || !result) {
213 return nullptr;
214 }
215
216 // Handle replacing sub-elements if this element is also a container.
217 if (!walkResult.wasSkipped()) {
218 // Replace the sub elements of this element, bailing if we fail.
219 if (!(result = replaceSubElements(result, replacer))) {
220 return nullptr;
221 }
222 }
223
224 return result;
225}
226
227template <typename Concrete>
228Attribute detail::AttrTypeReplacerBase<Concrete>::replaceBase(Attribute attr) {
229 return replaceElementImpl(attr, attrReplacementFns,
230 *static_cast<Concrete *>(this));
231}
232
233template <typename Concrete>
234Type detail::AttrTypeReplacerBase<Concrete>::replaceBase(Type type) {
235 return replaceElementImpl(type, typeReplacementFns,
236 *static_cast<Concrete *>(this));
237}
238
239//===----------------------------------------------------------------------===//
240/// AttrTypeReplacer
241//===----------------------------------------------------------------------===//
242
243template class detail::AttrTypeReplacerBase<AttrTypeReplacer>;
244
245template <typename T>
246T AttrTypeReplacer::cachedReplaceImpl(T element) {
247 const void *opaqueElement = element.getAsOpaquePointer();
248 auto [it, inserted] = cache.try_emplace(Key: opaqueElement, Args&: opaqueElement);
249 if (!inserted)
250 return T::getFromOpaquePointer(it->second);
251
252 T result = replaceBase(element);
253
254 cache[opaqueElement] = result.getAsOpaquePointer();
255 return result;
256}
257
258Attribute AttrTypeReplacer::replace(Attribute attr) {
259 return cachedReplaceImpl(element: attr);
260}
261
262Type AttrTypeReplacer::replace(Type type) { return cachedReplaceImpl(element: type); }
263
264//===----------------------------------------------------------------------===//
265/// CyclicAttrTypeReplacer
266//===----------------------------------------------------------------------===//
267
268template class detail::AttrTypeReplacerBase<CyclicAttrTypeReplacer>;
269
270CyclicAttrTypeReplacer::CyclicAttrTypeReplacer()
271 : cache([&](void *attr) { return breakCycleImpl(element: attr); }) {}
272
273void CyclicAttrTypeReplacer::addCycleBreaker(CycleBreakerFn<Attribute> fn) {
274 attrCycleBreakerFns.emplace_back(args: std::move(fn));
275}
276
277void CyclicAttrTypeReplacer::addCycleBreaker(CycleBreakerFn<Type> fn) {
278 typeCycleBreakerFns.emplace_back(args: std::move(fn));
279}
280
281template <typename T>
282T CyclicAttrTypeReplacer::cachedReplaceImpl(T element) {
283 void *opaqueTaggedElement = AttrOrType(element).getOpaqueValue();
284 CyclicReplacerCache<void *, const void *>::CacheEntry cacheEntry =
285 cache.lookupOrInit(element: opaqueTaggedElement);
286 if (auto resultOpt = cacheEntry.get())
287 return T::getFromOpaquePointer(*resultOpt);
288
289 T result = replaceBase(element);
290
291 cacheEntry.resolve(result: result.getAsOpaquePointer());
292 return result;
293}
294
295Attribute CyclicAttrTypeReplacer::replace(Attribute attr) {
296 return cachedReplaceImpl(element: attr);
297}
298
299Type CyclicAttrTypeReplacer::replace(Type type) {
300 return cachedReplaceImpl(element: type);
301}
302
303std::optional<const void *>
304CyclicAttrTypeReplacer::breakCycleImpl(void *element) {
305 AttrOrType attrType = AttrOrType::getFromOpaqueValue(VP: element);
306 if (auto attr = dyn_cast<Attribute>(Val&: attrType)) {
307 for (auto &cyclicReplaceFn : llvm::reverse(C&: attrCycleBreakerFns)) {
308 if (std::optional<Attribute> newRes = cyclicReplaceFn(attr)) {
309 return newRes->getAsOpaquePointer();
310 }
311 }
312 } else {
313 auto type = dyn_cast<Type>(Val&: attrType);
314 for (auto &cyclicReplaceFn : llvm::reverse(C&: typeCycleBreakerFns)) {
315 if (std::optional<Type> newRes = cyclicReplaceFn(type)) {
316 return newRes->getAsOpaquePointer();
317 }
318 }
319 }
320 return std::nullopt;
321}
322
323//===----------------------------------------------------------------------===//
324// AttrTypeImmediateSubElementWalker
325//===----------------------------------------------------------------------===//
326
327void AttrTypeImmediateSubElementWalker::walk(Attribute element) {
328 if (element)
329 walkAttrsFn(element);
330}
331
332void AttrTypeImmediateSubElementWalker::walk(Type element) {
333 if (element)
334 walkTypesFn(element);
335}
336