2019-06-19 11:55:27 -07:00
|
|
|
//===- SymbolTable.cpp - MLIR Symbol Table Class --------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2019-07-08 22:31:25 -07:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2019-06-19 11:55:27 -07:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
/// Return true if the given operation is unknown and may potentially define a
|
|
|
|
/// symbol table.
|
|
|
|
static bool isPotentiallyUnknownSymbolTable(Operation *op) {
|
|
|
|
return !op->getDialect() && op->getNumRegions() == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
/// Build a symbol table with the symbols within the given operation.
|
|
|
|
SymbolTable::SymbolTable(Operation *op) : context(op->getContext()) {
|
|
|
|
assert(op->hasTrait<OpTrait::SymbolTable>() &&
|
|
|
|
"expected operation to have SymbolTable trait");
|
|
|
|
assert(op->getNumRegions() == 1 &&
|
|
|
|
"expected operation to have a single region");
|
|
|
|
|
|
|
|
for (auto &block : op->getRegion(0)) {
|
|
|
|
for (auto &op : block) {
|
|
|
|
auto nameAttr = op.getAttrOfType<StringAttr>(getSymbolAttrName());
|
|
|
|
if (!nameAttr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto inserted = symbolTable.insert({nameAttr.getValue(), &op});
|
|
|
|
(void)inserted;
|
|
|
|
assert(inserted.second &&
|
|
|
|
"expected region to contain uniquely named symbol operations");
|
|
|
|
}
|
2019-06-30 10:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 11:55:27 -07:00
|
|
|
/// Look up a symbol with the specified name, returning null if no such name
|
|
|
|
/// exists. Names never include the @ on them.
|
2019-07-08 22:31:25 -07:00
|
|
|
Operation *SymbolTable::lookup(StringRef name) const {
|
2019-07-03 13:21:24 -07:00
|
|
|
return symbolTable.lookup(name);
|
2019-06-19 11:55:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Erase the given symbol from the table.
|
2019-07-08 22:31:25 -07:00
|
|
|
void SymbolTable::erase(Operation *symbol) {
|
|
|
|
auto nameAttr = symbol->getAttrOfType<StringAttr>(getSymbolAttrName());
|
|
|
|
assert(nameAttr && "expected valid 'name' attribute");
|
|
|
|
|
|
|
|
auto it = symbolTable.find(nameAttr.getValue());
|
2019-06-19 11:55:27 -07:00
|
|
|
if (it != symbolTable.end() && it->second == symbol)
|
|
|
|
symbolTable.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a new symbol into the table, and rename it as necessary to avoid
|
|
|
|
/// collisions.
|
2019-07-08 22:31:25 -07:00
|
|
|
void SymbolTable::insert(Operation *symbol) {
|
|
|
|
auto nameAttr = symbol->getAttrOfType<StringAttr>(getSymbolAttrName());
|
|
|
|
assert(nameAttr && "expected valid 'name' attribute");
|
|
|
|
|
2019-06-19 11:55:27 -07:00
|
|
|
// Add this symbol to the symbol table, uniquing the name if a conflict is
|
|
|
|
// detected.
|
2019-07-08 22:31:25 -07:00
|
|
|
if (symbolTable.insert({nameAttr.getValue(), symbol}).second)
|
2019-06-19 11:55:27 -07:00
|
|
|
return;
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
// If a conflict was detected, then the symbol will not have been added to
|
|
|
|
// the symbol table. Try suffixes until we get to a unique name that works.
|
|
|
|
SmallString<128> nameBuffer(nameAttr.getValue());
|
2019-06-19 11:55:27 -07:00
|
|
|
unsigned originalLength = nameBuffer.size();
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
// Iteratively try suffixes until we find one that isn't used.
|
2019-06-19 11:55:27 -07:00
|
|
|
do {
|
|
|
|
nameBuffer.resize(originalLength);
|
|
|
|
nameBuffer += '_';
|
|
|
|
nameBuffer += std::to_string(uniquingCounter++);
|
2019-07-03 13:21:24 -07:00
|
|
|
} while (!symbolTable.insert({nameBuffer, symbol}).second);
|
2019-07-08 22:31:25 -07:00
|
|
|
symbol->setAttr(getSymbolAttrName(), StringAttr::get(nameBuffer, context));
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:43:43 -07:00
|
|
|
/// Returns the operation registered with the given symbol name with the
|
|
|
|
/// regions of 'symbolTableOp'. 'symbolTableOp' is required to be an operation
|
|
|
|
/// with the 'OpTrait::SymbolTable' trait. Returns nullptr if no valid symbol
|
|
|
|
/// was found.
|
|
|
|
Operation *SymbolTable::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
StringRef symbol) {
|
|
|
|
assert(symbolTableOp->hasTrait<OpTrait::SymbolTable>());
|
|
|
|
|
|
|
|
// Look for a symbol with the given name.
|
|
|
|
for (auto &block : symbolTableOp->getRegion(0)) {
|
|
|
|
for (auto &op : block) {
|
|
|
|
auto nameAttr = op.template getAttrOfType<StringAttr>(
|
|
|
|
mlir::SymbolTable::getSymbolAttrName());
|
|
|
|
if (nameAttr && nameAttr.getValue() == symbol)
|
|
|
|
return &op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the operation registered with the given symbol name within the
|
|
|
|
/// closes parent operation with the 'OpTrait::SymbolTable' trait. Returns
|
|
|
|
/// nullptr if no valid symbol was found.
|
|
|
|
Operation *SymbolTable::lookupNearestSymbolFrom(Operation *from,
|
|
|
|
StringRef symbol) {
|
2019-10-08 18:38:05 -07:00
|
|
|
assert(from && "expected valid operation");
|
|
|
|
while (!from->hasTrait<OpTrait::SymbolTable>()) {
|
2019-09-23 11:43:43 -07:00
|
|
|
from = from->getParentOp();
|
2019-10-08 18:38:05 -07:00
|
|
|
|
|
|
|
// Check that this is a valid op and isn't an unknown symbol table.
|
|
|
|
if (!from || isPotentiallyUnknownSymbolTable(from))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return lookupSymbolIn(from, symbol);
|
2019-09-23 11:43:43 -07:00
|
|
|
}
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable Trait Types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LogicalResult OpTrait::impl::verifySymbolTable(Operation *op) {
|
|
|
|
if (op->getNumRegions() != 1)
|
|
|
|
return op->emitOpError()
|
|
|
|
<< "Operations with a 'SymbolTable' must have exactly one region";
|
|
|
|
|
2019-10-20 00:11:03 -07:00
|
|
|
// Check that all symbols are uniquely named within child regions.
|
2019-07-08 22:31:25 -07:00
|
|
|
llvm::StringMap<Location> nameToOrigLoc;
|
|
|
|
for (auto &block : op->getRegion(0)) {
|
|
|
|
for (auto &op : block) {
|
|
|
|
// Check for a symbol name attribute.
|
|
|
|
auto nameAttr =
|
|
|
|
op.getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName());
|
|
|
|
if (!nameAttr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Try to insert this symbol into the table.
|
|
|
|
auto it = nameToOrigLoc.try_emplace(nameAttr.getValue(), op.getLoc());
|
|
|
|
if (!it.second)
|
|
|
|
return op.emitError()
|
|
|
|
.append("redefinition of symbol named '", nameAttr.getValue(), "'")
|
|
|
|
.attachNote(it.first->second)
|
|
|
|
.append("see existing symbol definition here");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success();
|
2019-06-19 11:55:27 -07:00
|
|
|
}
|
2019-10-08 10:21:26 -07:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable Trait Types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Walk all of the symbol references within the given operation, invoking the
|
|
|
|
/// provided callback for each found use.
|
|
|
|
static WalkResult
|
|
|
|
walkSymbolRefs(Operation *op,
|
|
|
|
function_ref<WalkResult(SymbolTable::SymbolUse)> callback) {
|
|
|
|
// Check to see if the operation has any attributes.
|
|
|
|
DictionaryAttr attrDict = op->getAttrList().getDictionary();
|
|
|
|
if (!attrDict)
|
|
|
|
return WalkResult::advance();
|
|
|
|
|
|
|
|
// A worklist of a container attribute and the current index into the held
|
|
|
|
// attribute list.
|
|
|
|
SmallVector<std::pair<Attribute, unsigned>, 1> worklist;
|
|
|
|
worklist.push_back({attrDict, /*index*/ 0});
|
|
|
|
|
2019-10-18 21:28:47 -07:00
|
|
|
// Process the symbol references within the given nested attribute range.
|
|
|
|
auto processAttrs = [&](unsigned &index, auto attrRange) -> WalkResult {
|
|
|
|
for (Attribute attr : llvm::drop_begin(attrRange, index)) {
|
|
|
|
// Make sure to keep the index counter in sync.
|
|
|
|
++index;
|
|
|
|
|
|
|
|
/// Check for a nested container attribute, these will also need to be
|
|
|
|
/// walked.
|
|
|
|
if (attr.isa<ArrayAttr>() || attr.isa<DictionaryAttr>()) {
|
|
|
|
worklist.push_back({attr, /*index*/ 0});
|
|
|
|
return WalkResult::advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the provided callback if we find a symbol use and check for a
|
|
|
|
// requested interrupt.
|
|
|
|
if (auto symbolRef = attr.dyn_cast<SymbolRefAttr>())
|
|
|
|
if (callback(SymbolTable::SymbolUse(op, symbolRef)).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-18 21:28:47 -07:00
|
|
|
// Pop this container attribute from the worklist.
|
|
|
|
worklist.pop_back();
|
|
|
|
return WalkResult::advance();
|
|
|
|
};
|
|
|
|
|
|
|
|
WalkResult result = WalkResult::advance();
|
|
|
|
do {
|
|
|
|
Attribute attr = worklist.back().first;
|
|
|
|
unsigned &index = worklist.back().second;
|
|
|
|
|
|
|
|
// Process the given attribute, which is guaranteed to be a container.
|
|
|
|
if (auto dict = attr.dyn_cast<DictionaryAttr>())
|
|
|
|
result = processAttrs(index, make_second_range(dict.getValue()));
|
|
|
|
else
|
|
|
|
result = processAttrs(index, attr.cast<ArrayAttr>().getValue());
|
|
|
|
} while (!worklist.empty() && !result.wasInterrupted());
|
|
|
|
return result;
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Walk all of the uses, for any symbol, that are nested within the given
|
|
|
|
/// operation 'from', invoking the provided callback for each. This does not
|
|
|
|
/// traverse into any nested symbol tables, and will also only return uses on
|
|
|
|
/// 'from' if it does not also define a symbol table.
|
2019-10-08 18:38:05 -07:00
|
|
|
static Optional<WalkResult>
|
|
|
|
walkSymbolUses(Operation *from,
|
|
|
|
function_ref<WalkResult(SymbolTable::SymbolUse)> callback) {
|
2019-10-08 10:21:26 -07:00
|
|
|
// If from is not a symbol table, check for uses. A symbol table defines a new
|
|
|
|
// scope, so we can't walk the attributes from the symbol table op.
|
|
|
|
if (!from->hasTrait<OpTrait::SymbolTable>()) {
|
|
|
|
if (walkSymbolRefs(from, callback).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<Region *, 1> worklist;
|
|
|
|
worklist.reserve(from->getNumRegions());
|
|
|
|
for (Region ®ion : from->getRegions())
|
|
|
|
worklist.push_back(®ion);
|
|
|
|
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
Region *region = worklist.pop_back_val();
|
|
|
|
for (Block &block : *region) {
|
|
|
|
for (Operation &op : block) {
|
|
|
|
if (walkSymbolRefs(&op, callback).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
// If this operation has regions, and it as well as its dialect arent't
|
|
|
|
// registered then conservatively fail. The operation may define a
|
|
|
|
// symbol table, so we can't opaquely know if we should traverse to find
|
|
|
|
// nested uses.
|
|
|
|
if (isPotentiallyUnknownSymbolTable(&op))
|
|
|
|
return llvm::None;
|
|
|
|
|
2019-10-08 10:21:26 -07:00
|
|
|
// If this op defines a new symbol table scope, we can't traverse. Any
|
|
|
|
// symbol references nested within 'op' are different semantically.
|
|
|
|
if (!op.hasTrait<OpTrait::SymbolTable>()) {
|
|
|
|
for (Region ®ion : op.getRegions())
|
|
|
|
worklist.push_back(®ion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WalkResult::advance();
|
|
|
|
}
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
/// Get an iterator range for all of the uses, for any symbol, that are nested
|
|
|
|
/// within the given operation 'from'. This does not traverse into any nested
|
|
|
|
/// symbol tables, and will also only return uses on 'from' if it does not
|
|
|
|
/// also define a symbol table. This function returns None if there are any
|
|
|
|
/// unknown operations that may potentially be symbol tables.
|
|
|
|
auto SymbolTable::getSymbolUses(Operation *from) -> Optional<UseRange> {
|
|
|
|
std::vector<SymbolUse> uses;
|
|
|
|
Optional<WalkResult> result = walkSymbolUses(from, [&](SymbolUse symbolUse) {
|
|
|
|
uses.push_back(symbolUse);
|
|
|
|
return WalkResult::advance();
|
|
|
|
});
|
|
|
|
return result ? Optional<UseRange>(std::move(uses)) : Optional<UseRange>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all of the uses of the given symbol that are nested within the given
|
2019-10-08 10:21:26 -07:00
|
|
|
/// operation 'from', invoking the provided callback for each. This does not
|
|
|
|
/// traverse into any nested symbol tables, and will also only return uses on
|
2019-10-08 18:38:05 -07:00
|
|
|
/// 'from' if it does not also define a symbol table. This function returns
|
|
|
|
/// None if there are any unknown operations that may potentially be symbol
|
|
|
|
/// tables.
|
|
|
|
auto SymbolTable::getSymbolUses(StringRef symbol, Operation *from)
|
|
|
|
-> Optional<UseRange> {
|
2019-10-08 10:21:26 -07:00
|
|
|
SymbolRefAttr symbolRefAttr = SymbolRefAttr::get(symbol, from->getContext());
|
2019-10-08 18:38:05 -07:00
|
|
|
|
|
|
|
std::vector<SymbolUse> uses;
|
|
|
|
Optional<WalkResult> result = walkSymbolUses(from, [&](SymbolUse symbolUse) {
|
|
|
|
if (symbolRefAttr == symbolUse.getSymbolRef())
|
|
|
|
uses.push_back(symbolUse);
|
|
|
|
return WalkResult::advance();
|
2019-10-08 10:21:26 -07:00
|
|
|
});
|
2019-10-08 18:38:05 -07:00
|
|
|
return result ? Optional<UseRange>(std::move(uses)) : Optional<UseRange>();
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
/// Return if the given symbol is known to have no uses that are nested within
|
|
|
|
/// the given operation 'from'. This does not traverse into any nested symbol
|
|
|
|
/// tables, and will also only count uses on 'from' if it does not also define
|
|
|
|
/// a symbol table. This function will also return false if there are any
|
|
|
|
/// unknown operations that may potentially be symbol tables.
|
|
|
|
bool SymbolTable::symbolKnownUseEmpty(StringRef symbol, Operation *from) {
|
2019-10-08 10:21:26 -07:00
|
|
|
SymbolRefAttr symbolRefAttr = SymbolRefAttr::get(symbol, from->getContext());
|
|
|
|
|
|
|
|
// Walk all of the symbol uses looking for a reference to 'symbol'.
|
2019-10-08 18:38:05 -07:00
|
|
|
Optional<WalkResult> walkResult =
|
|
|
|
walkSymbolUses(from, [&](SymbolUse symbolUse) {
|
|
|
|
return symbolUse.getSymbolRef() == symbolRefAttr
|
|
|
|
? WalkResult::interrupt()
|
|
|
|
: WalkResult::advance();
|
|
|
|
});
|
|
|
|
return !walkResult || !walkResult->wasInterrupted();
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|