MLIR 24.0.0git
SymbolTable.h
Go to the documentation of this file.
1//===- SymbolTable.h - MLIR Symbol Table Class ------------------*- C++ -*-===//
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#ifndef MLIR_IR_SYMBOLTABLE_H
10#define MLIR_IR_SYMBOLTABLE_H
11
12#include "mlir/IR/Attributes.h"
14#include "llvm/ADT/SetVector.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/Support/RWMutex.h"
17
18namespace mlir {
19
20/// This class allows for representing and managing the symbol table used by
21/// operations with the 'SymbolTable' trait. Inserting into and erasing from
22/// this SymbolTable will also insert and erase from the Operation given to it
23/// at construction.
25public:
26 /// Build a symbol table with the symbols within the given operation.
27 SymbolTable(Operation *symbolTableOp);
28
29 /// Look up a symbol with the specified name, returning null if no such
30 /// name exists. Names never include the @ on them.
31 Operation *lookup(StringRef name) const;
32 template <typename T>
33 T lookup(StringRef name) const {
34 return dyn_cast_or_null<T>(lookup(name));
35 }
36
37 /// Look up a symbol with the specified name, returning null if no such
38 /// name exists. Names never include the @ on them.
39 Operation *lookup(StringAttr name) const;
40 template <typename T>
41 T lookup(StringAttr name) const {
42 return dyn_cast_or_null<T>(lookup(name));
43 }
44
45 /// Remove the given symbol from the table, without deleting it.
46 void remove(Operation *op);
47
48 /// Erase the given symbol from the table and delete the operation.
49 void erase(Operation *symbol);
50
51 /// Insert a new symbol into the table, and rename it as necessary to avoid
52 /// collisions. Also insert at the specified location in the body of the
53 /// associated operation if it is not already there. It is asserted that the
54 /// symbol is not inside another operation. Return the name of the symbol
55 /// after insertion as attribute.
56 StringAttr insert(Operation *symbol, Block::iterator insertPt = {});
57
58 /// Renames the given op or the op refered to by the given name to the given
59 /// new name and updates the symbol table and all usages of the symbol
60 /// accordingly. Fails if the updating of the usages fails.
61 LogicalResult rename(StringAttr from, StringAttr to);
62 LogicalResult rename(Operation *op, StringAttr to);
63 LogicalResult rename(StringAttr from, StringRef to);
64 LogicalResult rename(Operation *op, StringRef to);
65
66 /// Renames the given op or the op refered to by the given name to the a name
67 /// that is unique within this and the provided other symbol tables and
68 /// updates the symbol table and all usages of the symbol accordingly. Returns
69 /// the new name or failure if the renaming fails.
70 FailureOr<StringAttr> renameToUnique(StringAttr from,
71 ArrayRef<SymbolTable *> others);
72 FailureOr<StringAttr> renameToUnique(Operation *op,
73 ArrayRef<SymbolTable *> others);
74
75 /// Returns the associated operation.
76 Operation *getOp() const { return symbolTableOp; }
77
78 //===--------------------------------------------------------------------===//
79 // Symbol Utilities
80 //===--------------------------------------------------------------------===//
81
82 /// An enumeration detailing the different visibility types that a symbol may
83 /// have.
84 enum class Visibility {
85 /// The symbol is public and may be referenced anywhere internal or external
86 /// to the visible references in the IR.
88
89 /// The symbol is private and may only be referenced by SymbolRefAttrs local
90 /// to the operations within the current symbol table.
92
93 /// The symbol is visible to the current IR, which may include operations in
94 /// symbol tables above the one that owns the current symbol. `Nested`
95 /// visibility allows for referencing a symbol outside of its current symbol
96 /// table, while retaining the ability to observe all uses.
98 };
99
100 /// Generate a unique symbol name. Iteratively increase uniquingCounter
101 /// and use it as a suffix for symbol names until uniqueChecker does not
102 /// detect any conflict.
103 template <unsigned N, typename UniqueChecker>
104 static SmallString<N> generateSymbolName(StringRef name,
105 UniqueChecker uniqueChecker,
106 unsigned &uniquingCounter) {
107 SmallString<N> nameBuffer(name);
108 unsigned originalLength = nameBuffer.size();
109 do {
110 nameBuffer.resize(originalLength);
111 nameBuffer += '_';
112 nameBuffer += std::to_string(uniquingCounter++);
113 } while (uniqueChecker(nameBuffer));
114
115 return nameBuffer;
116 }
117
118 /// Returns the name of the given symbol operation, aborting if no symbol is
119 /// present.
120 static StringAttr getSymbolName(Operation *symbol);
121 /// Compatibility alias for generated interface code that still refers to the
122 /// symbol name attribute by convention.
123 static StringRef getSymbolAttrName() { return "sym_name"; }
124
125 /// Sets the name of the given symbol operation.
126 static void setSymbolName(Operation *symbol, StringAttr name);
127 static void setSymbolName(Operation *symbol, StringRef name) {
128 setSymbolName(symbol, StringAttr::get(symbol->getContext(), name));
129 }
130
131 /// Returns the visibility of the given symbol operation, which is required to
132 /// implement `SymbolOpInterface`.
133 static Visibility getSymbolVisibility(Operation *symbol);
134 /// Sets the visibility of the given symbol operation, which is required to
135 /// implement `SymbolOpInterface`.
136 static void setSymbolVisibility(Operation *symbol, Visibility vis);
137
138 /// Returns the nearest symbol table from a given operation `from`. Returns
139 /// nullptr if no valid parent symbol table could be found.
141
142 /// Walks all symbol table operations nested within, and including, `op`. For
143 /// each symbol table operation, the provided callback is invoked with the op
144 /// and a boolean signifying if the symbols within that symbol table can be
145 /// treated as if all uses within the IR are visible to the caller.
146 /// `allSymUsesVisible` identifies whether all of the symbol uses of symbols
147 /// within `op` are visible.
148 static void walkSymbolTables(Operation *op, bool allSymUsesVisible,
149 function_ref<void(Operation *, bool)> callback);
150
151 /// Returns the operation registered with the given symbol name with the
152 /// regions of 'symbolTableOp'. 'symbolTableOp' is required to be an operation
153 /// with the 'OpTrait::SymbolTable' trait.
154 static Operation *lookupSymbolIn(Operation *op, StringAttr symbol);
155 static Operation *lookupSymbolIn(Operation *op, StringRef symbol) {
156 return lookupSymbolIn(op, StringAttr::get(op->getContext(), symbol));
157 }
158 static Operation *lookupSymbolIn(Operation *op, SymbolRefAttr symbol);
159 /// A variant of 'lookupSymbolIn' that returns all of the symbols referenced
160 /// by a given SymbolRefAttr. Returns failure if any of the nested references
161 /// could not be resolved.
162 static LogicalResult lookupSymbolIn(Operation *op, SymbolRefAttr symbol,
164
165 /// Returns the operation registered with the given symbol name within the
166 /// closest parent operation of, or including, 'from' with the
167 /// 'OpTrait::SymbolTable' trait. Returns nullptr if no valid symbol was
168 /// found.
169 static Operation *lookupNearestSymbolFrom(Operation *from, StringAttr symbol);
171 SymbolRefAttr symbol);
172 template <typename T>
173 static T lookupNearestSymbolFrom(Operation *from, StringAttr symbol) {
174 return dyn_cast_or_null<T>(lookupNearestSymbolFrom(from, symbol));
175 }
176 template <typename T>
177 static T lookupNearestSymbolFrom(Operation *from, SymbolRefAttr symbol) {
178 return dyn_cast_or_null<T>(lookupNearestSymbolFrom(from, symbol));
179 }
180
181 /// This class represents a specific symbol use.
182 class SymbolUse {
183 public:
184 SymbolUse(Operation *op, SymbolRefAttr symbolRef)
185 : owner(op), symbolRef(symbolRef) {}
186
187 /// Return the operation user of this symbol reference.
188 Operation *getUser() const { return owner; }
189
190 /// Return the symbol reference that this use represents.
191 SymbolRefAttr getSymbolRef() const { return symbolRef; }
192
193 private:
194 /// The operation that this access is held by.
195 Operation *owner;
196
197 /// The symbol reference that this use represents.
198 SymbolRefAttr symbolRef;
199 };
200
201 /// This class implements a range of SymbolRef uses.
202 class UseRange {
203 public:
204 UseRange(std::vector<SymbolUse> &&uses) : uses(std::move(uses)) {}
205
206 using iterator = std::vector<SymbolUse>::const_iterator;
207 iterator begin() const { return uses.begin(); }
208 iterator end() const { return uses.end(); }
209 bool empty() const { return uses.empty(); }
210
211 private:
212 std::vector<SymbolUse> uses;
213 };
214
215 /// Get an iterator range for all of the uses, for any symbol, that are nested
216 /// within the given operation 'from'. This does not traverse into any nested
217 /// symbol tables. This function returns std::nullopt if there are any unknown
218 /// operations that may potentially be symbol tables.
219 static std::optional<UseRange> getSymbolUses(Operation *from);
220 static std::optional<UseRange> getSymbolUses(Region *from);
221
222 /// Get all of the uses of the given symbol that are nested within the given
223 /// operation 'from'. This does not traverse into any nested symbol tables.
224 /// This function returns std::nullopt if there are any unknown operations
225 /// that may potentially be symbol tables.
226 static std::optional<UseRange> getSymbolUses(StringAttr symbol,
227 Operation *from);
228 static std::optional<UseRange> getSymbolUses(Operation *symbol,
229 Operation *from);
230 static std::optional<UseRange> getSymbolUses(StringAttr symbol, Region *from);
231 static std::optional<UseRange> getSymbolUses(Operation *symbol, Region *from);
232
233 /// Return if the given symbol is known to have no uses that are nested
234 /// within the given operation 'from'. This does not traverse into any nested
235 /// symbol tables. This function will also return false if there are any
236 /// unknown operations that may potentially be symbol tables. This doesn't
237 /// necessarily mean that there are no uses, we just can't conservatively
238 /// prove it.
239 static bool symbolKnownUseEmpty(StringAttr symbol, Operation *from);
240 static bool symbolKnownUseEmpty(Operation *symbol, Operation *from);
241 static bool symbolKnownUseEmpty(StringAttr symbol, Region *from);
242 static bool symbolKnownUseEmpty(Operation *symbol, Region *from);
243
244 /// Attempt to replace all uses of the given symbol 'oldSymbol' with the
245 /// provided symbol 'newSymbol' that are nested within the given operation
246 /// 'from'. This does not traverse into any nested symbol tables. If there are
247 /// any unknown operations that may potentially be symbol tables, no uses are
248 /// replaced and failure is returned.
249 static LogicalResult replaceAllSymbolUses(StringAttr oldSymbol,
250 StringAttr newSymbol,
251 Operation *from);
252 static LogicalResult replaceAllSymbolUses(Operation *oldSymbol,
253 StringAttr newSymbolName,
254 Operation *from);
255 static LogicalResult replaceAllSymbolUses(StringAttr oldSymbol,
256 StringAttr newSymbol, Region *from);
257 static LogicalResult replaceAllSymbolUses(Operation *oldSymbol,
258 StringAttr newSymbolName,
259 Region *from);
260
261private:
262 Operation *symbolTableOp;
263
264 /// This is a mapping from a name to the symbol with that name. They key is
265 /// always known to be a StringAttr.
267
268 /// This is used when name conflicts are detected.
269 unsigned uniquingCounter = 0;
270};
271
273
274//===----------------------------------------------------------------------===//
275// SymbolTableCollection
276//===----------------------------------------------------------------------===//
277
278/// This class represents a collection of `SymbolTable`s. This simplifies
279/// certain algorithms that run recursively on nested symbol tables. Symbol
280/// tables are constructed lazily to reduce the upfront cost of constructing
281/// unnecessary tables.
283public:
284 virtual ~SymbolTableCollection() = default;
285
286 /// Look up a symbol with the specified name within the specified symbol table
287 /// operation, returning null if no such name exists.
288 virtual Operation *lookupSymbolIn(Operation *symbolTableOp,
289 StringAttr symbol);
290 virtual Operation *lookupSymbolIn(Operation *symbolTableOp,
291 SymbolRefAttr name);
292 template <typename T, typename NameT>
293 T lookupSymbolIn(Operation *symbolTableOp, NameT &&name) {
294 return dyn_cast_or_null<T>(
295 lookupSymbolIn(symbolTableOp, std::forward<NameT>(name)));
296 }
297 /// A variant of 'lookupSymbolIn' that returns all of the symbols referenced
298 /// by a given SymbolRefAttr when resolved within the provided symbol table
299 /// operation. Returns failure if any of the nested references could not be
300 /// resolved.
301 virtual LogicalResult lookupSymbolIn(Operation *symbolTableOp,
302 SymbolRefAttr name,
304
305 /// Returns the operation registered with the given symbol name within the
306 /// closest parent operation of, or including, 'from' with the
307 /// 'OpTrait::SymbolTable' trait. Returns nullptr if no valid symbol was
308 /// found.
310 StringAttr symbol);
312 SymbolRefAttr symbol);
313 template <typename T>
314 T lookupNearestSymbolFrom(Operation *from, StringAttr symbol) {
315 return dyn_cast_or_null<T>(lookupNearestSymbolFrom(from, symbol));
316 }
317 template <typename T>
318 T lookupNearestSymbolFrom(Operation *from, SymbolRefAttr symbol) {
319 return dyn_cast_or_null<T>(lookupNearestSymbolFrom(from, symbol));
320 }
321
322 /// Lookup, or create, a symbol table for an operation.
324
325 /// Invalidate the cached symbol table for an operation.
326 /// This is important when doing IR modifications that erase and also create
327 /// operations having the 'OpTrait::SymbolTable' trait. If a symbol table of
328 /// an erased operation is not invalidated, a new operation sharing the same
329 /// address would be associated with outdated, and wrong, information.
330 virtual void invalidateSymbolTable(Operation *op);
331
332private:
334
335 /// The constructed symbol tables nested within this table.
337};
338
339//===----------------------------------------------------------------------===//
340// LockedSymbolTableCollection
341//===----------------------------------------------------------------------===//
342
343/// This class implements a lock-based shared wrapper around a symbol table
344/// collection that allows shared access to the collection of symbol tables.
345/// This class does not protect shared access to individual symbol tables.
346/// `SymbolTableCollection` lazily instantiates `SymbolTable` instances for
347/// symbol table operations, making read operations not thread-safe. This class
348/// provides a thread-safe `lookupSymbolIn` implementation by synchronizing the
349/// lazy `SymbolTable` lookup.
351public:
353 : collection(collection) {}
354
355 /// Look up a symbol with the specified name within the specified symbol table
356 /// operation, returning null if no such name exists.
357 Operation *lookupSymbolIn(Operation *symbolTableOp,
358 StringAttr symbol) override;
359 /// Look up a symbol with the specified name within the specified symbol table
360 /// operation, returning null if no such name exists.
361 Operation *lookupSymbolIn(Operation *symbolTableOp, FlatSymbolRefAttr symbol);
362 /// Look up a potentially nested symbol within the specified symbol table
363 /// operation, returning null if no such symbol exists.
364 Operation *lookupSymbolIn(Operation *symbolTableOp,
365 SymbolRefAttr name) override;
366
367 /// Lookup a symbol of a particular kind within the specified symbol table,
368 /// returning null if the symbol was not found.
369 template <typename T, typename NameT>
370 T lookupSymbolIn(Operation *symbolTableOp, NameT &&name) {
371 return dyn_cast_or_null<T>(
372 lookupSymbolIn(symbolTableOp, std::forward<NameT>(name)));
373 }
374
375 /// A variant of 'lookupSymbolIn' that returns all of the symbols referenced
376 /// by a given SymbolRefAttr when resolved within the provided symbol table
377 /// operation. Returns failure if any of the nested references could not be
378 /// resolved.
379 LogicalResult lookupSymbolIn(Operation *symbolTableOp, SymbolRefAttr name,
380 SmallVectorImpl<Operation *> &symbols) override;
381
382private:
383 /// Get the symbol table for the symbol table operation, constructing if it
384 /// does not exist. This function provides thread safety over `collection`
385 /// by locking when performing the lookup and when inserting
386 /// lazily-constructed symbol tables.
387 SymbolTable &getSymbolTable(Operation *symbolTableOp) override;
388
389 /// The symbol tables to manage.
390 SymbolTableCollection &collection;
391 /// The mutex protecting access to the symbol table collection.
392 llvm::sys::SmartRWMutex<true> mutex;
393};
394
395//===----------------------------------------------------------------------===//
396// SymbolUserMap
397//===----------------------------------------------------------------------===//
398
399/// This class represents a map of symbols to users, and provides efficient
400/// implementations of symbol queries related to users; such as collecting the
401/// users of a symbol, replacing all uses, etc.
403public:
404 /// Build a user map for all of the symbols defined in regions nested under
405 /// 'symbolTableOp'. A reference to the provided symbol table collection is
406 /// kept by the user map to ensure efficient lookups, thus the lifetime should
407 /// extend beyond that of this map.
408 SymbolUserMap(SymbolTableCollection &symbolTable, Operation *symbolTableOp);
409
410 /// Return the users of the provided symbol operation.
412 auto it = symbolToUsers.find(symbol);
413 return it != symbolToUsers.end() ? it->second.getArrayRef()
415 }
416
417 /// Return true if the given symbol has no uses.
418 bool useEmpty(Operation *symbol) const {
419 return !symbolToUsers.count(symbol);
420 }
421
422 /// Replace all of the uses of the given symbol with `newSymbolName`.
423 void replaceAllUsesWith(Operation *symbol, StringAttr newSymbolName);
424
425private:
426 /// A reference to the symbol table used to construct this map.
427 SymbolTableCollection &symbolTable;
428
429 /// A map of symbol operations to symbol users.
431};
432
433//===----------------------------------------------------------------------===//
434// SymbolTable Trait Types
435//===----------------------------------------------------------------------===//
436
437namespace detail {
438LogicalResult verifySymbolTable(Operation *op);
439LogicalResult verifySymbol(Operation *op);
440
441} // namespace detail
442
443namespace OpTrait {
444/// A trait that provides the name accessors for symbol operations that store
445/// their name in the conventional `sym_name` inherent attribute.
446template <typename ConcreteType>
447class SymbolName : public TraitBase<ConcreteType, SymbolName> {
448public:
449 StringAttr getNameAttr() {
450 return cast<ConcreteType>(this->getOperation()).getSymNameAttr();
451 }
452
453 StringRef getName() { return getNameAttr().getValue(); }
454
455 void setName(StringAttr name) {
456 cast<ConcreteType>(this->getOperation()).setSymNameAttr(name);
457 }
458
459 void setName(StringRef name) {
460 setName(StringAttr::get(this->getOperation()->getContext(), name));
461 }
462};
463
464/// A trait that provides visibility accessors for symbol operations that store
465/// their visibility in the conventional `sym_visibility` inherent attribute.
466template <typename ConcreteType>
467class SymbolVisibility : public TraitBase<ConcreteType, SymbolVisibility> {
468public:
470 auto concrete = cast<ConcreteType>(this->getOperation());
471 StringAttr visibility = concrete.getSymVisibilityAttr();
472 if (!visibility || visibility.getValue() == "public")
473 return ::mlir::SymbolTable::Visibility::Public;
474 if (visibility.getValue() == "private")
475 return ::mlir::SymbolTable::Visibility::Private;
476 assert(visibility.getValue() == "nested" && "invalid symbol visibility");
477 return ::mlir::SymbolTable::Visibility::Nested;
478 }
479
481 auto concrete = cast<ConcreteType>(this->getOperation());
482 if (visibility == ::mlir::SymbolTable::Visibility::Public) {
483 concrete.setSymVisibilityAttr({});
484 return;
485 }
486 assert((visibility == ::mlir::SymbolTable::Visibility::Private ||
488 "invalid symbol visibility");
489 StringRef value = visibility == ::mlir::SymbolTable::Visibility::Private
490 ? "private"
491 : "nested";
492 concrete.setSymVisibilityAttr(
493 StringAttr::get(this->getOperation()->getContext(), value));
494 }
495};
496
497/// A trait used to provide symbol table functionalities to a region operation.
498/// This operation must hold exactly 1 region. Once attached, all operations
499/// that are directly within the region, i.e not including those within child
500/// regions, and implement `SymbolOpInterface` will be verified to ensure that
501/// their names are uniqued.
502template <typename ConcreteType>
503class SymbolTable : public TraitBase<ConcreteType, SymbolTable> {
504public:
505 static LogicalResult verifyRegionTrait(Operation *op) {
506 return ::mlir::detail::verifySymbolTable(op);
507 }
508
509 /// Look up a symbol with the specified name, returning null if no such
510 /// name exists. Symbol names never include the @ on them. Note: This
511 /// performs a linear scan of held symbols.
512 Operation *lookupSymbol(StringAttr name) {
514 }
515 template <typename T>
516 T lookupSymbol(StringAttr name) {
517 return dyn_cast_or_null<T>(lookupSymbol(name));
518 }
519 Operation *lookupSymbol(SymbolRefAttr symbol) {
520 return mlir::SymbolTable::lookupSymbolIn(this->getOperation(), symbol);
521 }
522 template <typename T>
523 T lookupSymbol(SymbolRefAttr symbol) {
524 return dyn_cast_or_null<T>(lookupSymbol(symbol));
525 }
526
527 Operation *lookupSymbol(StringRef name) {
529 }
530 template <typename T>
531 T lookupSymbol(StringRef name) {
532 return dyn_cast_or_null<T>(lookupSymbol(name));
533 }
534};
535
536} // namespace OpTrait
537
538//===----------------------------------------------------------------------===//
539// Visibility parsing implementation.
540//===----------------------------------------------------------------------===//
541
542namespace impl {
543/// Parse an optional visibility attribute keyword (i.e., public, private, or
544/// nested) without quotes in a string attribute named 'attrName'.
545ParseResult parseOptionalVisibilityKeyword(OpAsmParser &parser,
546 NamedAttrList &attrs);
547} // namespace impl
548
549} // namespace mlir
550
551/// Include the generated symbol interfaces.
552#include "mlir/IR/SymbolInterfaces.h.inc"
553#include "mlir/IR/SymbolInterfacesAttrInterface.h.inc"
554
555#endif // MLIR_IR_SYMBOLTABLE_H
b getContext())
OpListType::iterator iterator
Definition Block.h:164
A symbol reference with a reference path containing a single element.
T lookupSymbolIn(Operation *symbolTableOp, NameT &&name)
Lookup a symbol of a particular kind within the specified symbol table, returning null if the symbol ...
LockedSymbolTableCollection(SymbolTableCollection &collection)
Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol) override
Look up a symbol with the specified name within the specified symbol table operation,...
A trait that provides the name accessors for symbol operations that store their name in the conventio...
void setName(StringAttr name)
void setName(StringRef name)
A trait used to provide symbol table functionalities to a region operation.
Operation * lookupSymbol(SymbolRefAttr symbol)
Operation * lookupSymbol(StringRef name)
T lookupSymbol(SymbolRefAttr symbol)
Operation * lookupSymbol(StringAttr name)
Look up a symbol with the specified name, returning null if no such name exists.
T lookupSymbol(StringAttr name)
T lookupSymbol(StringRef name)
static LogicalResult verifyRegionTrait(Operation *op)
A trait that provides visibility accessors for symbol operations that store their visibility in the c...
void setVisibility(::mlir::SymbolTable::Visibility visibility)
::mlir::SymbolTable::Visibility getVisibility()
Helper class for implementing traits.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class represents a collection of SymbolTables.
T lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
virtual ~SymbolTableCollection()=default
T lookupSymbolIn(Operation *symbolTableOp, NameT &&name)
virtual Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
T lookupNearestSymbolFrom(Operation *from, SymbolRefAttr symbol)
virtual Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
friend class LockedSymbolTableCollection
virtual void invalidateSymbolTable(Operation *op)
Invalidate the cached symbol table for an operation.
virtual SymbolTable & getSymbolTable(Operation *op)
Lookup, or create, a symbol table for an operation.
SymbolUse(Operation *op, SymbolRefAttr symbolRef)
Operation * getUser() const
Return the operation user of this symbol reference.
SymbolRefAttr getSymbolRef() const
Return the symbol reference that this use represents.
UseRange(std::vector< SymbolUse > &&uses)
std::vector< SymbolUse >::const_iterator iterator
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:24
static SmallString< N > generateSymbolName(StringRef name, UniqueChecker uniqueChecker, unsigned &uniquingCounter)
Generate a unique symbol name.
static Visibility getSymbolVisibility(Operation *symbol)
Returns the visibility of the given symbol operation, which is required to implement SymbolOpInterfac...
static StringRef getSymbolAttrName()
Compatibility alias for generated interface code that still refers to the symbol name attribute by co...
static void setSymbolVisibility(Operation *symbol, Visibility vis)
Sets the visibility of the given symbol operation, which is required to implement SymbolOpInterface.
static T lookupNearestSymbolFrom(Operation *from, SymbolRefAttr symbol)
static LogicalResult replaceAllSymbolUses(StringAttr oldSymbol, StringAttr newSymbol, Operation *from)
Attempt to replace all uses of the given symbol 'oldSymbol' with the provided symbol 'newSymbol' that...
Visibility
An enumeration detailing the different visibility types that a symbol may have.
Definition SymbolTable.h:84
@ Nested
The symbol is visible to the current IR, which may include operations in symbol tables above the one ...
Definition SymbolTable.h:97
@ Public
The symbol is public and may be referenced anywhere internal or external to the visible references in...
Definition SymbolTable.h:87
@ Private
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
Definition SymbolTable.h:91
static T lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
LogicalResult rename(StringAttr from, StringAttr to)
Renames the given op or the op refered to by the given name to the given new name and updates the sym...
void erase(Operation *symbol)
Erase the given symbol from the table and delete the operation.
Operation * getOp() const
Returns the associated operation.
Definition SymbolTable.h:76
static void setSymbolName(Operation *symbol, StringRef name)
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
T lookup(StringRef name) const
Definition SymbolTable.h:33
SymbolTable(Operation *symbolTableOp)
Build a symbol table with the symbols within the given operation.
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
static void setSymbolName(Operation *symbol, StringAttr name)
Sets the name of the given symbol operation.
static bool symbolKnownUseEmpty(StringAttr symbol, Operation *from)
Return if the given symbol is known to have no uses that are nested within the given operation 'from'...
FailureOr< StringAttr > renameToUnique(StringAttr from, ArrayRef< SymbolTable * > others)
Renames the given op or the op refered to by the given name to the a name that is unique within this ...
static Operation * lookupSymbolIn(Operation *op, StringRef symbol)
static void walkSymbolTables(Operation *op, bool allSymUsesVisible, function_ref< void(Operation *, bool)> callback)
Walks all symbol table operations nested within, and including, op.
static StringAttr getSymbolName(Operation *symbol)
Returns the name of the given symbol operation, aborting if no symbol is present.
static std::optional< UseRange > getSymbolUses(Operation *from)
Get an iterator range for all of the uses, for any symbol, that are nested within the given operation...
T lookup(StringAttr name) const
Definition SymbolTable.h:41
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
void remove(Operation *op)
Remove the given symbol from the table, without deleting it.
static Operation * getNearestSymbolTable(Operation *from)
Returns the nearest symbol table from a given operation from.
bool useEmpty(Operation *symbol) const
Return true if the given symbol has no uses.
ArrayRef< Operation * > getUsers(Operation *symbol) const
Return the users of the provided symbol operation.
void replaceAllUsesWith(Operation *symbol, StringAttr newSymbolName)
Replace all of the uses of the given symbol with newSymbolName.
SymbolUserMap(SymbolTableCollection &symbolTable, Operation *symbolTableOp)
Build a user map for all of the symbols defined in regions nested under 'symbolTableOp'.
LogicalResult verifySymbol(Operation *op)
LogicalResult verifySymbolTable(Operation *op)
ParseResult parseOptionalVisibilityKeyword(OpAsmParser &parser, NamedAttrList &attrs)
Parse an optional visibility attribute keyword (i.e., public, private, or nested) without quotes in a...
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:120
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147