Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/IR/SymbolTable.cpp
Show First 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | void SymbolTable::insert(Operation *symbol, Block::iterator insertPt) { | ||||
do { | do { | ||||
nameBuffer.resize(originalLength); | nameBuffer.resize(originalLength); | ||||
nameBuffer += '_'; | nameBuffer += '_'; | ||||
nameBuffer += std::to_string(uniquingCounter++); | nameBuffer += std::to_string(uniquingCounter++); | ||||
} while (!symbolTable.insert({nameBuffer, symbol}).second); | } while (!symbolTable.insert({nameBuffer, symbol}).second); | ||||
setSymbolName(symbol, nameBuffer); | setSymbolName(symbol, nameBuffer); | ||||
} | } | ||||
/// Returns true if the given operation defines a symbol. | |||||
bool SymbolTable::isSymbol(Operation *op) { | |||||
return op->hasTrait<OpTrait::Symbol>() || getNameIfSymbol(op).hasValue(); | |||||
} | |||||
/// Returns the name of the given symbol operation. | /// Returns the name of the given symbol operation. | ||||
StringRef SymbolTable::getSymbolName(Operation *symbol) { | StringRef SymbolTable::getSymbolName(Operation *symbol) { | ||||
Optional<StringRef> name = getNameIfSymbol(symbol); | Optional<StringRef> name = getNameIfSymbol(symbol); | ||||
assert(name && "expected valid symbol name"); | assert(name && "expected valid symbol name"); | ||||
return *name; | return *name; | ||||
} | } | ||||
/// Sets the name of the given symbol operation. | /// Sets the name of the given symbol operation. | ||||
void SymbolTable::setSymbolName(Operation *symbol, StringRef name) { | void SymbolTable::setSymbolName(Operation *symbol, StringRef name) { | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | Operation *SymbolTable::lookupNearestSymbolFrom(Operation *from, | ||||
Operation *symbolTableOp = getNearestSymbolTable(from); | Operation *symbolTableOp = getNearestSymbolTable(from); | ||||
return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr; | return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr; | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// SymbolTable Trait Types | // SymbolTable Trait Types | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
LogicalResult OpTrait::impl::verifySymbolTable(Operation *op) { | LogicalResult detail::verifySymbolTable(Operation *op) { | ||||
if (op->getNumRegions() != 1) | if (op->getNumRegions() != 1) | ||||
return op->emitOpError() | return op->emitOpError() | ||||
<< "Operations with a 'SymbolTable' must have exactly one region"; | << "Operations with a 'SymbolTable' must have exactly one region"; | ||||
if (!llvm::hasSingleElement(op->getRegion(0))) | if (!llvm::hasSingleElement(op->getRegion(0))) | ||||
return op->emitOpError() | return op->emitOpError() | ||||
<< "Operations with a 'SymbolTable' must have exactly one block"; | << "Operations with a 'SymbolTable' must have exactly one block"; | ||||
// Check that all symbols are uniquely named within child regions. | // Check that all symbols are uniquely named within child regions. | ||||
Show All 13 Lines | for (auto &op : block) { | ||||
.append("redefinition of symbol named '", nameAttr.getValue(), "'") | .append("redefinition of symbol named '", nameAttr.getValue(), "'") | ||||
.attachNote(it.first->second) | .attachNote(it.first->second) | ||||
.append("see existing symbol definition here"); | .append("see existing symbol definition here"); | ||||
} | } | ||||
} | } | ||||
return success(); | return success(); | ||||
} | } | ||||
LogicalResult OpTrait::impl::verifySymbol(Operation *op) { | LogicalResult detail::verifySymbol(Operation *op) { | ||||
// Verify the name attribute. | // Verify the name attribute. | ||||
if (!op->getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName())) | if (!op->getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName())) | ||||
return op->emitOpError() << "requires string attribute '" | return op->emitOpError() << "requires string attribute '" | ||||
<< mlir::SymbolTable::getSymbolAttrName() << "'"; | << mlir::SymbolTable::getSymbolAttrName() << "'"; | ||||
// Verify the visibility attribute. | // Verify the visibility attribute. | ||||
if (Attribute vis = op->getAttr(mlir::SymbolTable::getVisibilityAttrName())) { | if (Attribute vis = op->getAttr(mlir::SymbolTable::getVisibilityAttrName())) { | ||||
StringAttr visStrAttr = vis.dyn_cast<StringAttr>(); | StringAttr visStrAttr = vis.dyn_cast<StringAttr>(); | ||||
▲ Show 20 Lines • Show All 533 Lines • ▼ Show 20 Lines | LogicalResult SymbolTable::replaceAllSymbolUses(StringRef oldSymbol, | ||||
Region *from) { | Region *from) { | ||||
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from); | return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from); | ||||
} | } | ||||
LogicalResult SymbolTable::replaceAllSymbolUses(Operation *oldSymbol, | LogicalResult SymbolTable::replaceAllSymbolUses(Operation *oldSymbol, | ||||
StringRef newSymbol, | StringRef newSymbol, | ||||
Region *from) { | Region *from) { | ||||
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from); | return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from); | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | |||||
// Symbol Interfaces | |||||
//===----------------------------------------------------------------------===// | |||||
/// Include the generated symbol interfaces. | |||||
#include "mlir/IR/SymbolInterfaces.cpp.inc" |