9 #ifndef MLIR_IR_SYMBOLTABLE_H
10 #define MLIR_IR_SYMBOLTABLE_H
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/RWMutex.h"
34 return dyn_cast_or_null<T>(
lookup(name));
42 return dyn_cast_or_null<T>(
lookup(name));
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);
71 ArrayRef<SymbolTable *> others);
73 ArrayRef<SymbolTable *> others);
109 template <
unsigned N,
typename UniqueChecker>
111 UniqueChecker uniqueChecker,
112 unsigned &uniquingCounter) {
114 unsigned originalLength = nameBuffer.size();
116 nameBuffer.resize(originalLength);
118 nameBuffer += std::to_string(uniquingCounter++);
119 }
while (uniqueChecker(nameBuffer));
172 SymbolRefAttr symbol);
173 template <
typename T>
177 template <
typename T>
186 : owner(op), symbolRef(symbolRef) {}
199 SymbolRefAttr symbolRef;
205 UseRange(std::vector<SymbolUse> &&uses) : uses(std::move(uses)) {}
207 using iterator = std::vector<SymbolUse>::const_iterator;
210 bool empty()
const {
return uses.empty(); }
213 std::vector<SymbolUse> uses;
227 static std::optional<UseRange>
getSymbolUses(StringAttr symbol,
251 StringAttr newSymbol,
254 StringAttr newSymbolName,
257 StringAttr newSymbol,
Region *from);
259 StringAttr newSymbolName,
270 unsigned uniquingCounter = 0;
289 template <
typename T,
typename NameT>
291 return dyn_cast_or_null<T>(
307 template <
typename T>
311 template <
typename T>
340 : collection(collection) {}
354 template <
typename T,
typename NameT>
356 return dyn_cast_or_null<T>(
377 llvm::sys::SmartRWMutex<true> mutex;
397 auto it = symbolToUsers.find(symbol);
398 return it != symbolToUsers.end() ? it->second.getArrayRef() : std::nullopt;
403 return !symbolToUsers.count(symbol);
434 template <
typename ConcreteType>
447 template <
typename T>
454 template <
typename T>
462 template <
typename T>
478 NamedAttrList &attrs);
484 #include "mlir/IR/SymbolInterfaces.h.inc"
OpListType::iterator iterator
A symbol reference with a reference path containing a single element.
This class implements a lock-based shared wrapper around a symbol table collection that allows shared...
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)
Look up a symbol with the specified name within the specified symbol table operation,...
A trait used to provide symbol table functionalities to a region operation.
Operation * lookupSymbol(SymbolRefAttr symbol)
Operation * lookupSymbol(StringRef name)
T lookupSymbol(SymbolRefAttr symbol)
T lookupSymbol(StringAttr name)
T lookupSymbol(StringRef name)
Operation * lookupSymbol(StringAttr name)
Look up a symbol with the specified name, returning null if no such name exists.
static LogicalResult verifyRegionTrait(Operation *op)
Helper class for implementing traits.
Operation * getOperation()
Return the ultimate Operation being worked on.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
This class represents a collection of SymbolTables.
T lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
T lookupSymbolIn(Operation *symbolTableOp, NameT &&name)
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)
Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
SymbolTable & getSymbolTable(Operation *op)
Lookup, or create, a symbol table for an operation.
This class represents a specific symbol use.
SymbolUse(Operation *op, SymbolRefAttr symbolRef)
SymbolRefAttr getSymbolRef() const
Return the symbol reference that this use represents.
Operation * getUser() const
Return the operation user of this symbol reference.
This class implements a range of SymbolRef uses.
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...
static Visibility getSymbolVisibility(Operation *symbol)
Returns the visibility of the given symbol operation.
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
static void setSymbolVisibility(Operation *symbol, Visibility vis)
Sets the visibility of the given symbol operation.
Operation * getOp() const
Returns the associated operation.
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.
@ Nested
The symbol is visible to the current IR, which may include operations in symbol tables above the one ...
@ Public
The symbol is public and may be referenced anywhere internal or external to the visible references in...
@ Private
The symbol is private and may only be referenced by SymbolRefAttrs local to the operations within the...
static StringRef getVisibilityAttrName()
Return the name of the attribute used for symbol visibility.
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.
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
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 Operation * lookupSymbolIn(Operation *op, StringRef symbol)
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 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
static SmallString< N > generateSymbolName(StringRef name, UniqueChecker uniqueChecker, unsigned &uniquingCounter)
Generate a unique symbol name.
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.
This class represents a map of symbols to users, and provides efficient implementations of symbol que...
bool useEmpty(Operation *symbol) const
Return true if the given symbol has no uses.
void replaceAllUsesWith(Operation *symbol, StringAttr newSymbolName)
Replace all of the uses of the given symbol with newSymbolName.
ArrayRef< Operation * > getUsers(Operation *symbol) const
Return the users of the provided symbol operation.
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)