2019-06-19 11:55:27 -07:00
|
|
|
//===- SymbolTable.cpp - MLIR Symbol Table Class --------------------------===//
|
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09:35:36 -08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-06-19 11:55:27 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-19 11:55:27 -07:00
|
|
|
|
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2020-11-09 08:23:55 -08:00
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2020-01-13 15:23:01 -08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2019-07-08 22:31:25 -07:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2020-01-13 15:54:08 -08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2023-01-13 21:05:06 -08:00
|
|
|
#include <optional>
|
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) {
|
2020-10-28 22:00:55 -07:00
|
|
|
return op->getNumRegions() == 1 && !op->getDialect();
|
2019-10-08 18:38:05 -07:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
/// Returns the string name of the given symbol, or null if this is not a
|
2020-01-13 15:23:01 -08:00
|
|
|
/// symbol.
|
2021-08-29 14:22:24 -07:00
|
|
|
static StringAttr getNameIfSymbol(Operation *op) {
|
|
|
|
return op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName());
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2021-11-16 17:21:15 +00:00
|
|
|
static StringAttr getNameIfSymbol(Operation *op, StringAttr symbolAttrNameId) {
|
2021-08-29 14:22:24 -07:00
|
|
|
return op->getAttrOfType<StringAttr>(symbolAttrNameId);
|
2020-10-26 19:31:10 -07:00
|
|
|
}
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
/// Computes the nested symbol reference attribute for the symbol 'symbolName'
|
|
|
|
/// that are usable within the symbol table operations from 'symbol' as far up
|
|
|
|
/// to the given operation 'within', where 'within' is an ancestor of 'symbol'.
|
|
|
|
/// Returns success if all references up to 'within' could be computed.
|
|
|
|
static LogicalResult
|
2021-08-29 14:22:24 -07:00
|
|
|
collectValidReferencesFor(Operation *symbol, StringAttr symbolName,
|
2020-01-13 15:23:01 -08:00
|
|
|
Operation *within,
|
|
|
|
SmallVectorImpl<SymbolRefAttr> &results) {
|
|
|
|
assert(within->isAncestor(symbol) && "expected 'within' to be an ancestor");
|
|
|
|
MLIRContext *ctx = symbol->getContext();
|
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
auto leafRef = FlatSymbolRefAttr::get(symbolName);
|
2020-01-13 15:23:01 -08:00
|
|
|
results.push_back(leafRef);
|
|
|
|
|
|
|
|
// Early exit for when 'within' is the parent of 'symbol'.
|
|
|
|
Operation *symbolTableOp = symbol->getParentOp();
|
|
|
|
if (within == symbolTableOp)
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// Collect references until 'symbolTableOp' reaches 'within'.
|
|
|
|
SmallVector<FlatSymbolRefAttr, 1> nestedRefs(1, leafRef);
|
2021-11-16 17:21:15 +00:00
|
|
|
StringAttr symbolNameId =
|
|
|
|
StringAttr::get(ctx, SymbolTable::getSymbolAttrName());
|
2020-01-13 15:23:01 -08:00
|
|
|
do {
|
|
|
|
// Each parent of 'symbol' should define a symbol table.
|
|
|
|
if (!symbolTableOp->hasTrait<OpTrait::SymbolTable>())
|
|
|
|
return failure();
|
|
|
|
// Each parent of 'symbol' should also be a symbol.
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symbolTableName = getNameIfSymbol(symbolTableOp, symbolNameId);
|
2020-01-13 15:23:01 -08:00
|
|
|
if (!symbolTableName)
|
|
|
|
return failure();
|
2021-08-29 14:22:24 -07:00
|
|
|
results.push_back(SymbolRefAttr::get(symbolTableName, nestedRefs));
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
symbolTableOp = symbolTableOp->getParentOp();
|
|
|
|
if (symbolTableOp == within)
|
|
|
|
break;
|
|
|
|
nestedRefs.insert(nestedRefs.begin(),
|
2021-08-29 14:22:24 -07:00
|
|
|
FlatSymbolRefAttr::get(symbolTableName));
|
2020-01-13 15:23:01 -08:00
|
|
|
} while (true);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:57:00 -07:00
|
|
|
/// Walk all of the operations within the given set of regions, without
|
|
|
|
/// traversing into any nested symbol tables. Stops walking if the result of the
|
|
|
|
/// callback is anything other than `WalkResult::advance`.
|
2023-01-14 01:25:58 -08:00
|
|
|
static std::optional<WalkResult>
|
2020-10-16 11:57:00 -07:00
|
|
|
walkSymbolTable(MutableArrayRef<Region> regions,
|
2023-01-14 01:25:58 -08:00
|
|
|
function_ref<std::optional<WalkResult>(Operation *)> callback) {
|
2020-10-16 11:57:00 -07:00
|
|
|
SmallVector<Region *, 1> worklist(llvm::make_pointer_range(regions));
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
for (Operation &op : worklist.pop_back_val()->getOps()) {
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<WalkResult> result = callback(&op);
|
2020-10-16 11:57:00 -07:00
|
|
|
if (result != WalkResult::advance())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:22:19 -07:00
|
|
|
/// Walk all of the operations nested under, and including, the given operation,
|
|
|
|
/// without traversing into any nested symbol tables. Stops walking if the
|
|
|
|
/// result of the callback is anything other than `WalkResult::advance`.
|
2023-01-14 01:25:58 -08:00
|
|
|
static std::optional<WalkResult>
|
2022-07-26 13:22:19 -07:00
|
|
|
walkSymbolTable(Operation *op,
|
2023-01-14 01:25:58 -08:00
|
|
|
function_ref<std::optional<WalkResult>(Operation *)> callback) {
|
|
|
|
std::optional<WalkResult> result = callback(op);
|
2022-07-26 13:22:19 -07:00
|
|
|
if (result != WalkResult::advance() || op->hasTrait<OpTrait::SymbolTable>())
|
|
|
|
return result;
|
|
|
|
return walkSymbolTable(op->getRegions(), callback);
|
|
|
|
}
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
/// Build a symbol table with the symbols within the given operation.
|
2019-12-05 03:56:18 -08:00
|
|
|
SymbolTable::SymbolTable(Operation *symbolTableOp)
|
|
|
|
: symbolTableOp(symbolTableOp) {
|
|
|
|
assert(symbolTableOp->hasTrait<OpTrait::SymbolTable>() &&
|
2019-07-08 22:31:25 -07:00
|
|
|
"expected operation to have SymbolTable trait");
|
2019-12-05 03:56:18 -08:00
|
|
|
assert(symbolTableOp->getNumRegions() == 1 &&
|
2019-07-08 22:31:25 -07:00
|
|
|
"expected operation to have a single region");
|
2020-04-14 14:53:07 -07:00
|
|
|
assert(llvm::hasSingleElement(symbolTableOp->getRegion(0)) &&
|
2019-12-05 03:56:18 -08:00
|
|
|
"expected operation to have a single block");
|
|
|
|
|
2021-11-16 17:21:15 +00:00
|
|
|
StringAttr symbolNameId = StringAttr::get(symbolTableOp->getContext(),
|
|
|
|
SymbolTable::getSymbolAttrName());
|
2019-12-05 03:56:18 -08:00
|
|
|
for (auto &op : symbolTableOp->getRegion(0).front()) {
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr name = getNameIfSymbol(&op, symbolNameId);
|
2020-01-13 15:23:01 -08:00
|
|
|
if (!name)
|
2019-12-05 03:56:18 -08:00
|
|
|
continue;
|
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
auto inserted = symbolTable.insert({name, &op});
|
2019-12-05 03:56:18 -08:00
|
|
|
(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 {
|
2021-08-29 14:22:24 -07:00
|
|
|
return lookup(StringAttr::get(symbolTableOp->getContext(), name));
|
|
|
|
}
|
|
|
|
Operation *SymbolTable::lookup(StringAttr name) const {
|
2019-07-03 13:21:24 -07:00
|
|
|
return symbolTable.lookup(name);
|
2019-06-19 11:55:27 -07:00
|
|
|
}
|
|
|
|
|
2022-09-09 02:03:47 -07:00
|
|
|
void SymbolTable::remove(Operation *op) {
|
|
|
|
StringAttr name = getNameIfSymbol(op);
|
2020-01-13 15:23:01 -08:00
|
|
|
assert(name && "expected valid 'name' attribute");
|
2022-09-09 02:03:47 -07:00
|
|
|
assert(op->getParentOp() == symbolTableOp &&
|
2019-12-05 03:56:18 -08:00
|
|
|
"expected this operation to be inside of the operation with this "
|
|
|
|
"SymbolTable");
|
2019-07-08 22:31:25 -07:00
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
auto it = symbolTable.find(name);
|
2022-09-09 02:03:47 -07:00
|
|
|
if (it != symbolTable.end() && it->second == op)
|
2019-06-19 11:55:27 -07:00
|
|
|
symbolTable.erase(it);
|
2022-09-09 02:03:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::erase(Operation *symbol) {
|
|
|
|
remove(symbol);
|
|
|
|
symbol->erase();
|
2019-06-19 11:55:27 -07:00
|
|
|
}
|
|
|
|
|
2020-12-16 00:13:05 +01:00
|
|
|
// TODO: Consider if this should be renamed to something like insertOrUpdate
|
|
|
|
/// Insert a new symbol into the table and associated operation if not already
|
2021-11-02 12:38:01 +01:00
|
|
|
/// there and rename it as necessary to avoid collisions. Return the name of
|
|
|
|
/// the symbol after insertion as attribute.
|
|
|
|
StringAttr SymbolTable::insert(Operation *symbol, Block::iterator insertPt) {
|
2020-12-16 00:13:05 +01:00
|
|
|
// The symbol cannot be the child of another op and must be the child of the
|
|
|
|
// symbolTableOp after this.
|
|
|
|
//
|
|
|
|
// TODO: consider if SymbolTable's constructor should behave the same.
|
|
|
|
if (!symbol->getParentOp()) {
|
|
|
|
auto &body = symbolTableOp->getRegion(0).front();
|
2021-03-11 23:58:02 +00:00
|
|
|
if (insertPt == Block::iterator()) {
|
|
|
|
insertPt = Block::iterator(body.end());
|
|
|
|
} else {
|
|
|
|
assert((insertPt == body.end() ||
|
|
|
|
insertPt->getParentOp() == symbolTableOp) &&
|
|
|
|
"expected insertPt to be in the associated module operation");
|
|
|
|
}
|
|
|
|
// Insert before the terminator, if any.
|
|
|
|
if (insertPt == Block::iterator(body.end()) && !body.empty() &&
|
|
|
|
std::prev(body.end())->hasTrait<OpTrait::IsTerminator>())
|
|
|
|
insertPt = std::prev(body.end());
|
2020-12-16 00:13:05 +01:00
|
|
|
|
|
|
|
body.getOperations().insert(insertPt, symbol);
|
|
|
|
}
|
|
|
|
assert(symbol->getParentOp() == symbolTableOp &&
|
|
|
|
"symbol is already inserted in another op");
|
2019-12-05 03:56:18 -08:00
|
|
|
|
2019-06-19 11:55:27 -07:00
|
|
|
// Add this symbol to the symbol table, uniquing the name if a conflict is
|
|
|
|
// detected.
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr name = getSymbolName(symbol);
|
2020-01-13 15:23:01 -08:00
|
|
|
if (symbolTable.insert({name, symbol}).second)
|
2021-11-02 12:38:01 +01:00
|
|
|
return name;
|
2020-12-16 00:13:05 +01:00
|
|
|
// If the symbol was already in the table, also return.
|
|
|
|
if (symbolTable.lookup(name) == symbol)
|
2021-11-02 12:38:01 +01:00
|
|
|
return name;
|
2019-06-19 11:55:27 -07:00
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
MLIRContext *context = symbol->getContext();
|
[mlir][gpu] Introduce `gpu.dynamic_shared_memory` Op (#71546)
While the `gpu.launch` Op allows setting the size via the
`dynamic_shared_memory_size` argument, accessing the dynamic shared
memory is very convoluted. This PR implements the proposed Op,
`gpu.dynamic_shared_memory` that aims to simplify the utilization of
dynamic shared memory.
RFC:
https://discourse.llvm.org/t/rfc-simplifying-dynamic-shared-memory-access-in-gpu/
**Proposal from RFC**
This PR `gpu.dynamic.shared.memory` Op to use dynamic shared memory
feature efficiently. It is is a powerful feature that enables the
allocation of shared memory at runtime with the kernel launch on the
host. Afterwards, the memory can be accessed directly from the device. I
believe similar story exists for AMDGPU.
**Current way Using Dynamic Shared Memory with MLIR**
Let me illustrate the challenges of using dynamic shared memory in MLIR
with an example below. The process involves several steps:
- memref.global 0-sized array LLVM's NVPTX backend expects
- dynamic_shared_memory_size Set the size of dynamic shared memory
- memref.get_global Access the global symbol
- reinterpret_cast and subview Many OPs for pointer arithmetic
```
// Step 1. Create 0-sized global symbol. Manually set the alignment
memref.global "private" @dynamicShmem : memref<0xf16, 3> { alignment = 16 }
func.func @main() {
// Step 2. Allocate shared memory
gpu.launch blocks(...) threads(...)
dynamic_shared_memory_size %c10000 {
// Step 3. Access the global object
%shmem = memref.get_global @dynamicShmem : memref<0xf16, 3>
// Step 4. A sequence of `memref.reinterpret_cast` and `memref.subview` operations.
%4 = memref.reinterpret_cast %shmem to offset: [0], sizes: [14, 64, 128], strides: [8192,128,1] : memref<0xf16, 3> to memref<14x64x128xf16,3>
%5 = memref.subview %4[7, 0, 0][7, 64, 128][1,1,1] : memref<14x64x128xf16,3> to memref<7x64x128xf16, strided<[8192, 128, 1], offset: 57344>, 3>
%6 = memref.subview %5[2, 0, 0][1, 64, 128][1,1,1] : memref<7x64x128xf16, strided<[8192, 128, 1], offset: 57344>, 3> to memref<64x128xf16, strided<[128, 1], offset: 73728>, 3>
%7 = memref.subview %6[0, 0][64, 64][1,1] : memref<64x128xf16, strided<[128, 1], offset: 73728>, 3> to memref<64x64xf16, strided<[128, 1], offset: 73728>, 3>
%8 = memref.subview %6[32, 0][64, 64][1,1] : memref<64x128xf16, strided<[128, 1], offset: 73728>, 3> to memref<64x64xf16, strided<[128, 1], offset: 77824>, 3>
// Step.5 Use
"test.use.shared.memory"(%7) : (memref<64x64xf16, strided<[128, 1], offset: 73728>, 3>) -> (index)
"test.use.shared.memory"(%8) : (memref<64x64xf16, strided<[128, 1], offset: 77824>, 3>) -> (index)
gpu.terminator
}
```
Let’s write the program above with that:
```
func.func @main() {
gpu.launch blocks(...) threads(...) dynamic_shared_memory_size %c10000 {
%i = arith.constant 18 : index
// Step 1: Obtain shared memory directly
%shmem = gpu.dynamic_shared_memory : memref<?xi8, 3>
%c147456 = arith.constant 147456 : index
%c155648 = arith.constant 155648 : index
%7 = memref.view %shmem[%c147456][] : memref<?xi8, 3> to memref<64x64xf16, 3>
%8 = memref.view %shmem[%c155648][] : memref<?xi8, 3> to memref<64x64xf16, 3>
// Step 2: Utilize the shared memory
"test.use.shared.memory"(%7) : (memref<64x64xf16, 3>) -> (index)
"test.use.shared.memory"(%8) : (memref<64x64xf16, 3>) -> (index)
}
}
```
This PR resolves #72513
2023-11-16 14:42:17 +01:00
|
|
|
SmallString<128> nameBuffer = generateSymbolName<128>(
|
|
|
|
name.getValue(),
|
|
|
|
[&](StringRef candidate) {
|
|
|
|
return !symbolTable
|
|
|
|
.insert({StringAttr::get(context, candidate), symbol})
|
|
|
|
.second;
|
|
|
|
},
|
|
|
|
uniquingCounter);
|
2020-01-13 15:23:01 -08:00
|
|
|
setSymbolName(symbol, nameBuffer);
|
2021-11-02 12:38:01 +01:00
|
|
|
return getSymbolName(symbol);
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
|
|
|
|
[mlir][transform] Fix handling of transitive include in interpreter. (#67560)
Until now, the interpreter would only load those symbols from the
provided library files that were declared in the main transform module.
However, sequences in the library may include other sequences on their
own. Until now, if such sequences were not *also* declared in the main
transform module, the interpreter would fail to resolve them. Forward
declaring all of them is undesirable as it defeats the purpose of
encapsulation into library modules.
This PR implements a kind of linker for transform scripts to solve this
problem. The linker merges all symbols of the library module into the
main module before interpreting the latter. Symbols whose names collide
are handled as follows: (1) if they are both functions (in the sense of
`FunctionOpInterface`) with compatible signatures, one is external, and
the other one is public, then they are merged; (2) of one of them is
private, that one is renamed; and (3) an error is raised otherwise.
One consequence of this change is that the loading of the library files
in the interpreter pass is not idempotent anymore, i.e., subsequent
interpreter passes cannot (and need not) load the same library files again
since would lead to doubly defined symbols.
2023-10-06 10:56:57 +02:00
|
|
|
LogicalResult SymbolTable::rename(StringAttr from, StringAttr to) {
|
|
|
|
Operation *op = lookup(from);
|
|
|
|
return rename(op, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult SymbolTable::rename(Operation *op, StringAttr to) {
|
|
|
|
StringAttr from = getNameIfSymbol(op);
|
2023-10-06 11:16:42 +02:00
|
|
|
(void)from;
|
[mlir][transform] Fix handling of transitive include in interpreter. (#67560)
Until now, the interpreter would only load those symbols from the
provided library files that were declared in the main transform module.
However, sequences in the library may include other sequences on their
own. Until now, if such sequences were not *also* declared in the main
transform module, the interpreter would fail to resolve them. Forward
declaring all of them is undesirable as it defeats the purpose of
encapsulation into library modules.
This PR implements a kind of linker for transform scripts to solve this
problem. The linker merges all symbols of the library module into the
main module before interpreting the latter. Symbols whose names collide
are handled as follows: (1) if they are both functions (in the sense of
`FunctionOpInterface`) with compatible signatures, one is external, and
the other one is public, then they are merged; (2) of one of them is
private, that one is renamed; and (3) an error is raised otherwise.
One consequence of this change is that the loading of the library files
in the interpreter pass is not idempotent anymore, i.e., subsequent
interpreter passes cannot (and need not) load the same library files again
since would lead to doubly defined symbols.
2023-10-06 10:56:57 +02:00
|
|
|
|
|
|
|
assert(from && "expected valid 'name' attribute");
|
|
|
|
assert(op->getParentOp() == symbolTableOp &&
|
|
|
|
"expected this operation to be inside of the operation with this "
|
|
|
|
"SymbolTable");
|
|
|
|
assert(lookup(from) == op && "current name does not resolve to op");
|
|
|
|
assert(lookup(to) == nullptr && "new name already exists");
|
|
|
|
|
|
|
|
if (failed(SymbolTable::replaceAllSymbolUses(op, to, getOp())))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Remove op with old name, change name, add with new name. The order is
|
|
|
|
// important here due to how `remove` and `insert` rely on the op name.
|
|
|
|
remove(op);
|
|
|
|
setSymbolName(op, to);
|
|
|
|
insert(op);
|
|
|
|
|
|
|
|
assert(lookup(to) == op && "new name does not resolve to renamed op");
|
|
|
|
assert(lookup(from) == nullptr && "old name still exists");
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult SymbolTable::rename(StringAttr from, StringRef to) {
|
|
|
|
auto toAttr = StringAttr::get(getOp()->getContext(), to);
|
|
|
|
return rename(from, toAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult SymbolTable::rename(Operation *op, StringRef to) {
|
|
|
|
auto toAttr = StringAttr::get(getOp()->getContext(), to);
|
|
|
|
return rename(op, toAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
FailureOr<StringAttr>
|
|
|
|
SymbolTable::renameToUnique(StringAttr oldName,
|
|
|
|
ArrayRef<SymbolTable *> others) {
|
|
|
|
|
|
|
|
// Determine new name that is unique in all symbol tables.
|
|
|
|
StringAttr newName;
|
|
|
|
{
|
|
|
|
MLIRContext *context = oldName.getContext();
|
|
|
|
SmallString<64> prefix = oldName.getValue();
|
|
|
|
int uniqueId = 0;
|
|
|
|
prefix.push_back('_');
|
|
|
|
while (true) {
|
|
|
|
newName = StringAttr::get(context, prefix + Twine(uniqueId++));
|
|
|
|
auto lookupNewName = [&](SymbolTable *st) { return st->lookup(newName); };
|
|
|
|
if (!lookupNewName(this) && llvm::none_of(others, lookupNewName)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply renaming.
|
|
|
|
if (failed(rename(oldName, newName)))
|
|
|
|
return failure();
|
|
|
|
return newName;
|
|
|
|
}
|
|
|
|
|
|
|
|
FailureOr<StringAttr>
|
|
|
|
SymbolTable::renameToUnique(Operation *op, ArrayRef<SymbolTable *> others) {
|
|
|
|
StringAttr from = getNameIfSymbol(op);
|
|
|
|
assert(from && "expected valid 'name' attribute");
|
|
|
|
return renameToUnique(from, others);
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
/// Returns the name of the given symbol operation.
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr SymbolTable::getSymbolName(Operation *symbol) {
|
|
|
|
StringAttr name = getNameIfSymbol(symbol);
|
2020-01-13 15:23:01 -08:00
|
|
|
assert(name && "expected valid symbol name");
|
2021-08-29 14:22:24 -07:00
|
|
|
return name;
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2021-08-29 14:22:24 -07:00
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
/// Sets the name of the given symbol operation.
|
2021-08-29 14:22:24 -07:00
|
|
|
void SymbolTable::setSymbolName(Operation *symbol, StringAttr name) {
|
|
|
|
symbol->setAttr(getSymbolAttrName(), name);
|
2019-07-08 22:31:25 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 15:54:08 -08:00
|
|
|
/// Returns the visibility of the given symbol operation.
|
|
|
|
SymbolTable::Visibility SymbolTable::getSymbolVisibility(Operation *symbol) {
|
|
|
|
// If the attribute doesn't exist, assume public.
|
|
|
|
StringAttr vis = symbol->getAttrOfType<StringAttr>(getVisibilityAttrName());
|
|
|
|
if (!vis)
|
|
|
|
return Visibility::Public;
|
|
|
|
|
|
|
|
// Otherwise, switch on the string value.
|
2020-10-07 16:17:35 +02:00
|
|
|
return StringSwitch<Visibility>(vis.getValue())
|
2020-01-13 15:54:08 -08:00
|
|
|
.Case("private", Visibility::Private)
|
|
|
|
.Case("nested", Visibility::Nested)
|
|
|
|
.Case("public", Visibility::Public);
|
|
|
|
}
|
|
|
|
/// Sets the visibility of the given symbol operation.
|
|
|
|
void SymbolTable::setSymbolVisibility(Operation *symbol, Visibility vis) {
|
|
|
|
MLIRContext *ctx = symbol->getContext();
|
|
|
|
|
|
|
|
// If the visibility is public, just drop the attribute as this is the
|
|
|
|
// default.
|
|
|
|
if (vis == Visibility::Public) {
|
2021-11-16 17:21:15 +00:00
|
|
|
symbol->removeAttr(StringAttr::get(ctx, getVisibilityAttrName()));
|
2020-01-13 15:54:08 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, update the attribute.
|
|
|
|
assert((vis == Visibility::Private || vis == Visibility::Nested) &&
|
|
|
|
"unknown symbol visibility kind");
|
|
|
|
|
|
|
|
StringRef visName = vis == Visibility::Private ? "private" : "nested";
|
2021-02-08 09:44:03 +01:00
|
|
|
symbol->setAttr(getVisibilityAttrName(), StringAttr::get(ctx, visName));
|
2020-01-13 15:54:08 -08:00
|
|
|
}
|
|
|
|
|
2020-01-26 10:55:17 -05:00
|
|
|
/// Returns the nearest symbol table from a given operation `from`. Returns
|
|
|
|
/// nullptr if no valid parent symbol table could be found.
|
|
|
|
Operation *SymbolTable::getNearestSymbolTable(Operation *from) {
|
|
|
|
assert(from && "expected valid operation");
|
|
|
|
if (isPotentiallyUnknownSymbolTable(from))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
while (!from->hasTrait<OpTrait::SymbolTable>()) {
|
|
|
|
from = from->getParentOp();
|
|
|
|
|
|
|
|
// Check that this is a valid op and isn't an unknown symbol table.
|
|
|
|
if (!from || isPotentiallyUnknownSymbolTable(from))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:58:23 -07:00
|
|
|
/// Walks all symbol table operations nested within, and including, `op`. For
|
|
|
|
/// each symbol table operation, the provided callback is invoked with the op
|
|
|
|
/// and a boolean signifying if the symbols within that symbol table can be
|
|
|
|
/// treated as if all uses are visible. `allSymUsesVisible` identifies whether
|
|
|
|
/// all of the symbol uses of symbols within `op` are visible.
|
|
|
|
void SymbolTable::walkSymbolTables(
|
|
|
|
Operation *op, bool allSymUsesVisible,
|
|
|
|
function_ref<void(Operation *, bool)> callback) {
|
|
|
|
bool isSymbolTable = op->hasTrait<OpTrait::SymbolTable>();
|
|
|
|
if (isSymbolTable) {
|
|
|
|
SymbolOpInterface symbol = dyn_cast<SymbolOpInterface>(op);
|
|
|
|
allSymUsesVisible |= !symbol || symbol.isPrivate();
|
|
|
|
} else {
|
|
|
|
// Otherwise if 'op' is not a symbol table, any nested symbols are
|
|
|
|
// guaranteed to be hidden.
|
|
|
|
allSymUsesVisible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Region ®ion : op->getRegions())
|
|
|
|
for (Block &block : region)
|
|
|
|
for (Operation &nestedOp : block)
|
|
|
|
walkSymbolTables(&nestedOp, allSymUsesVisible, callback);
|
|
|
|
|
|
|
|
// If 'op' had the symbol table trait, visit it after any nested symbol
|
|
|
|
// tables.
|
|
|
|
if (isSymbolTable)
|
|
|
|
callback(op, allSymUsesVisible);
|
|
|
|
}
|
|
|
|
|
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,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symbol) {
|
2019-09-23 11:43:43 -07:00
|
|
|
assert(symbolTableOp->hasTrait<OpTrait::SymbolTable>());
|
2021-03-11 23:58:02 +00:00
|
|
|
Region ®ion = symbolTableOp->getRegion(0);
|
|
|
|
if (region.empty())
|
|
|
|
return nullptr;
|
2019-09-23 11:43:43 -07:00
|
|
|
|
|
|
|
// Look for a symbol with the given name.
|
2021-11-16 17:21:15 +00:00
|
|
|
StringAttr symbolNameId = StringAttr::get(symbolTableOp->getContext(),
|
|
|
|
SymbolTable::getSymbolAttrName());
|
2021-03-11 23:58:02 +00:00
|
|
|
for (auto &op : region.front())
|
2020-10-26 19:31:10 -07:00
|
|
|
if (getNameIfSymbol(&op, symbolNameId) == symbol)
|
2020-05-04 17:46:06 -07:00
|
|
|
return &op;
|
2019-09-23 11:43:43 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-01-13 15:23:01 -08:00
|
|
|
Operation *SymbolTable::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
SymbolRefAttr symbol) {
|
2020-01-27 23:24:48 -08:00
|
|
|
SmallVector<Operation *, 4> resolvedSymbols;
|
|
|
|
if (failed(lookupSymbolIn(symbolTableOp, symbol, resolvedSymbols)))
|
|
|
|
return nullptr;
|
|
|
|
return resolvedSymbols.back();
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:56:52 -07:00
|
|
|
/// Internal implementation of `lookupSymbolIn` that allows for specialized
|
|
|
|
/// implementations of the lookup function.
|
|
|
|
static LogicalResult lookupSymbolInImpl(
|
|
|
|
Operation *symbolTableOp, SymbolRefAttr symbol,
|
|
|
|
SmallVectorImpl<Operation *> &symbols,
|
2021-08-29 14:22:24 -07:00
|
|
|
function_ref<Operation *(Operation *, StringAttr)> lookupSymbolFn) {
|
2020-01-13 15:23:01 -08:00
|
|
|
assert(symbolTableOp->hasTrait<OpTrait::SymbolTable>());
|
|
|
|
|
|
|
|
// Lookup the root reference for this symbol.
|
2020-10-16 11:56:52 -07:00
|
|
|
symbolTableOp = lookupSymbolFn(symbolTableOp, symbol.getRootReference());
|
2020-01-13 15:23:01 -08:00
|
|
|
if (!symbolTableOp)
|
2020-01-27 23:24:48 -08:00
|
|
|
return failure();
|
|
|
|
symbols.push_back(symbolTableOp);
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
// If there are no nested references, just return the root symbol directly.
|
|
|
|
ArrayRef<FlatSymbolRefAttr> nestedRefs = symbol.getNestedReferences();
|
|
|
|
if (nestedRefs.empty())
|
2020-01-27 23:24:48 -08:00
|
|
|
return success();
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
// Verify that the root is also a symbol table.
|
|
|
|
if (!symbolTableOp->hasTrait<OpTrait::SymbolTable>())
|
2020-01-27 23:24:48 -08:00
|
|
|
return failure();
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
// Otherwise, lookup each of the nested non-leaf references and ensure that
|
|
|
|
// each corresponds to a valid symbol table.
|
|
|
|
for (FlatSymbolRefAttr ref : nestedRefs.drop_back()) {
|
2021-08-29 14:22:24 -07:00
|
|
|
symbolTableOp = lookupSymbolFn(symbolTableOp, ref.getAttr());
|
2020-01-13 15:23:01 -08:00
|
|
|
if (!symbolTableOp || !symbolTableOp->hasTrait<OpTrait::SymbolTable>())
|
2020-01-27 23:24:48 -08:00
|
|
|
return failure();
|
|
|
|
symbols.push_back(symbolTableOp);
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2020-10-16 11:56:52 -07:00
|
|
|
symbols.push_back(lookupSymbolFn(symbolTableOp, symbol.getLeafReference()));
|
2020-01-27 23:24:48 -08:00
|
|
|
return success(symbols.back());
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2019-09-23 11:43:43 -07:00
|
|
|
|
2020-10-16 11:56:52 -07:00
|
|
|
LogicalResult
|
|
|
|
SymbolTable::lookupSymbolIn(Operation *symbolTableOp, SymbolRefAttr symbol,
|
|
|
|
SmallVectorImpl<Operation *> &symbols) {
|
2021-08-29 14:22:24 -07:00
|
|
|
auto lookupFn = [](Operation *symbolTableOp, StringAttr symbol) {
|
2020-10-16 11:56:52 -07:00
|
|
|
return lookupSymbolIn(symbolTableOp, symbol);
|
|
|
|
};
|
|
|
|
return lookupSymbolInImpl(symbolTableOp, symbol, symbols, lookupFn);
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:43:43 -07:00
|
|
|
/// 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,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symbol) {
|
2020-01-13 15:23:01 -08:00
|
|
|
Operation *symbolTableOp = getNearestSymbolTable(from);
|
|
|
|
return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr;
|
|
|
|
}
|
|
|
|
Operation *SymbolTable::lookupNearestSymbolFrom(Operation *from,
|
|
|
|
SymbolRefAttr symbol) {
|
|
|
|
Operation *symbolTableOp = getNearestSymbolTable(from);
|
|
|
|
return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr;
|
2019-09-23 11:43:43 -07:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:59:39 -07:00
|
|
|
raw_ostream &mlir::operator<<(raw_ostream &os,
|
|
|
|
SymbolTable::Visibility visibility) {
|
|
|
|
switch (visibility) {
|
|
|
|
case SymbolTable::Visibility::Public:
|
|
|
|
return os << "public";
|
|
|
|
case SymbolTable::Visibility::Private:
|
|
|
|
return os << "private";
|
|
|
|
case SymbolTable::Visibility::Nested:
|
|
|
|
return os << "nested";
|
|
|
|
}
|
2021-06-09 00:42:21 +00:00
|
|
|
llvm_unreachable("Unexpected visibility");
|
2021-06-07 13:59:39 -07:00
|
|
|
}
|
|
|
|
|
2019-07-08 22:31:25 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable Trait Types
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-27 12:57:32 -07:00
|
|
|
LogicalResult detail::verifySymbolTable(Operation *op) {
|
2019-07-08 22:31:25 -07:00
|
|
|
if (op->getNumRegions() != 1)
|
|
|
|
return op->emitOpError()
|
|
|
|
<< "Operations with a 'SymbolTable' must have exactly one region";
|
2020-04-14 14:53:07 -07:00
|
|
|
if (!llvm::hasSingleElement(op->getRegion(0)))
|
2019-12-05 03:56:18 -08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "Operations with a 'SymbolTable' must have exactly one block";
|
2019-07-08 22:31:25 -07:00
|
|
|
|
2019-10-20 00:11:03 -07:00
|
|
|
// Check that all symbols are uniquely named within child regions.
|
2020-01-13 15:23:01 -08:00
|
|
|
DenseMap<Attribute, Location> nameToOrigLoc;
|
2019-07-08 22:31:25 -07:00
|
|
|
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.
|
2020-01-13 15:23:01 -08:00
|
|
|
auto it = nameToOrigLoc.try_emplace(nameAttr, op.getLoc());
|
2019-07-08 22:31:25 -07:00
|
|
|
if (!it.second)
|
|
|
|
return op.emitError()
|
|
|
|
.append("redefinition of symbol named '", nameAttr.getValue(), "'")
|
|
|
|
.attachNote(it.first->second)
|
|
|
|
.append("see existing symbol definition here");
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 11:57:00 -07:00
|
|
|
|
|
|
|
// Verify any nested symbol user operations.
|
|
|
|
SymbolTableCollection symbolTable;
|
2023-01-14 01:25:58 -08:00
|
|
|
auto verifySymbolUserFn = [&](Operation *op) -> std::optional<WalkResult> {
|
2020-10-16 11:57:00 -07:00
|
|
|
if (SymbolUserOpInterface user = dyn_cast<SymbolUserOpInterface>(op))
|
|
|
|
return WalkResult(user.verifySymbolUses(symbolTable));
|
|
|
|
return WalkResult::advance();
|
|
|
|
};
|
|
|
|
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<WalkResult> result =
|
2020-10-16 11:57:00 -07:00
|
|
|
walkSymbolTable(op->getRegions(), verifySymbolUserFn);
|
|
|
|
return success(result && !result->wasInterrupted());
|
2019-06-19 11:55:27 -07:00
|
|
|
}
|
2019-10-08 10:21:26 -07:00
|
|
|
|
2020-04-27 12:57:32 -07:00
|
|
|
LogicalResult detail::verifySymbol(Operation *op) {
|
2020-01-13 15:54:08 -08:00
|
|
|
// Verify the name attribute.
|
2019-10-21 09:58:22 -07:00
|
|
|
if (!op->getAttrOfType<StringAttr>(mlir::SymbolTable::getSymbolAttrName()))
|
|
|
|
return op->emitOpError() << "requires string attribute '"
|
|
|
|
<< mlir::SymbolTable::getSymbolAttrName() << "'";
|
2020-01-13 15:54:08 -08:00
|
|
|
|
|
|
|
// Verify the visibility attribute.
|
|
|
|
if (Attribute vis = op->getAttr(mlir::SymbolTable::getVisibilityAttrName())) {
|
2023-05-11 11:10:46 +02:00
|
|
|
StringAttr visStrAttr = llvm::dyn_cast<StringAttr>(vis);
|
2020-01-13 15:54:08 -08:00
|
|
|
if (!visStrAttr)
|
|
|
|
return op->emitOpError() << "requires visibility attribute '"
|
|
|
|
<< mlir::SymbolTable::getVisibilityAttrName()
|
|
|
|
<< "' to be a string attribute, but got " << vis;
|
|
|
|
|
|
|
|
if (!llvm::is_contained(ArrayRef<StringRef>{"public", "private", "nested"},
|
|
|
|
visStrAttr.getValue()))
|
|
|
|
return op->emitOpError()
|
|
|
|
<< "visibility expected to be one of [\"public\", \"private\", "
|
|
|
|
"\"nested\"], but got "
|
|
|
|
<< visStrAttr;
|
|
|
|
}
|
2019-10-21 09:58:22 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-10-08 10:21:26 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-10-24 10:46:47 -07:00
|
|
|
// Symbol Use Lists
|
2019-10-08 10:21:26 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Walk all of the symbol references within the given operation, invoking the
|
2022-07-26 13:22:19 -07:00
|
|
|
/// provided callback for each found use. The callbacks takes the use of the
|
|
|
|
/// symbol.
|
|
|
|
static WalkResult
|
|
|
|
walkSymbolRefs(Operation *op,
|
|
|
|
function_ref<WalkResult(SymbolTable::SymbolUse)> callback) {
|
2023-01-20 21:39:13 -08:00
|
|
|
return op->getAttrDictionary().walk<WalkOrder::PreOrder>(
|
|
|
|
[&](SymbolRefAttr symbolRef) {
|
2022-07-26 13:22:19 -07:00
|
|
|
if (callback({op, symbolRef}).wasInterrupted())
|
2019-10-18 21:28:47 -07:00
|
|
|
return WalkResult::interrupt();
|
2019-10-24 10:46:47 -07:00
|
|
|
|
2023-01-20 21:39:13 -08:00
|
|
|
// Don't walk nested references.
|
|
|
|
return WalkResult::skip();
|
|
|
|
});
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Walk all of the uses, for any symbol, that are nested within the given
|
2020-01-27 22:57:06 -08:00
|
|
|
/// regions, invoking the provided callback for each. This does not traverse
|
|
|
|
/// into any nested symbol tables.
|
2023-01-14 01:25:58 -08:00
|
|
|
static std::optional<WalkResult>
|
2022-07-26 13:22:19 -07:00
|
|
|
walkSymbolUses(MutableArrayRef<Region> regions,
|
|
|
|
function_ref<WalkResult(SymbolTable::SymbolUse)> callback) {
|
2023-01-14 01:25:58 -08:00
|
|
|
return walkSymbolTable(regions,
|
|
|
|
[&](Operation *op) -> std::optional<WalkResult> {
|
|
|
|
// Check that this isn't a potentially unknown symbol
|
|
|
|
// table.
|
|
|
|
if (isPotentiallyUnknownSymbolTable(op))
|
|
|
|
return std::nullopt;
|
2020-05-04 17:46:06 -07:00
|
|
|
|
2023-01-14 01:25:58 -08:00
|
|
|
return walkSymbolRefs(op, callback);
|
|
|
|
});
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
2020-01-27 22:57:06 -08:00
|
|
|
/// Walk all of the uses, for any symbol, that are nested within the given
|
2020-04-05 11:30:01 +09:00
|
|
|
/// operation 'from', invoking the provided callback for each. This does not
|
2020-01-27 22:57:06 -08:00
|
|
|
/// traverse into any nested symbol tables.
|
2023-01-14 01:25:58 -08:00
|
|
|
static std::optional<WalkResult>
|
2022-07-26 13:22:19 -07:00
|
|
|
walkSymbolUses(Operation *from,
|
|
|
|
function_ref<WalkResult(SymbolTable::SymbolUse)> callback) {
|
2020-01-27 22:57:06 -08:00
|
|
|
// If this operation has regions, and it, as well as its dialect, isn'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(from))
|
2022-12-03 18:50:27 -08:00
|
|
|
return std::nullopt;
|
2020-01-27 22:57:06 -08:00
|
|
|
|
|
|
|
// Walk the uses on this operation.
|
|
|
|
if (walkSymbolRefs(from, callback).wasInterrupted())
|
|
|
|
return WalkResult::interrupt();
|
|
|
|
|
|
|
|
// Only recurse if this operation is not a symbol table. A symbol table
|
|
|
|
// defines a new scope, so we can't walk the attributes from within the symbol
|
|
|
|
// table op.
|
|
|
|
if (!from->hasTrait<OpTrait::SymbolTable>())
|
|
|
|
return walkSymbolUses(from->getRegions(), callback);
|
|
|
|
return WalkResult::advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// This class represents a single symbol scope. A symbol scope represents the
|
|
|
|
/// set of operations nested within a symbol table that may reference symbols
|
|
|
|
/// within that table. A symbol scope does not contain the symbol table
|
|
|
|
/// operation itself, just its contained operations. A scope ends at leaf
|
|
|
|
/// operations or another symbol table operation.
|
|
|
|
struct SymbolScope {
|
|
|
|
/// Walk the symbol uses within this scope, invoking the given callback.
|
|
|
|
/// This variant is used when the callback type matches that expected by
|
|
|
|
/// 'walkSymbolUses'.
|
|
|
|
template <typename CallbackT,
|
2022-10-15 21:07:03 -07:00
|
|
|
std::enable_if_t<!std::is_same<
|
2020-04-14 14:52:52 -07:00
|
|
|
typename llvm::function_traits<CallbackT>::result_t,
|
|
|
|
void>::value> * = nullptr>
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<WalkResult> walk(CallbackT cback) {
|
2023-05-26 10:17:47 +02:00
|
|
|
if (Region *region = llvm::dyn_cast_if_present<Region *>(limit))
|
2020-01-27 22:57:06 -08:00
|
|
|
return walkSymbolUses(*region, cback);
|
|
|
|
return walkSymbolUses(limit.get<Operation *>(), cback);
|
|
|
|
}
|
|
|
|
/// This variant is used when the callback type matches a stripped down type:
|
|
|
|
/// void(SymbolTable::SymbolUse use)
|
|
|
|
template <typename CallbackT,
|
2022-10-15 21:07:03 -07:00
|
|
|
std::enable_if_t<std::is_same<
|
2020-04-14 14:52:52 -07:00
|
|
|
typename llvm::function_traits<CallbackT>::result_t,
|
|
|
|
void>::value> * = nullptr>
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<WalkResult> walk(CallbackT cback) {
|
2022-07-26 13:22:19 -07:00
|
|
|
return walk([=](SymbolTable::SymbolUse use) {
|
2020-01-27 22:57:06 -08:00
|
|
|
return cback(use), WalkResult::advance();
|
|
|
|
});
|
|
|
|
}
|
2019-10-08 10:21:26 -07:00
|
|
|
|
2022-07-26 13:22:19 -07:00
|
|
|
/// Walk all of the operations nested under the current scope without
|
|
|
|
/// traversing into any nested symbol tables.
|
|
|
|
template <typename CallbackT>
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<WalkResult> walkSymbolTable(CallbackT &&cback) {
|
2023-05-26 10:17:47 +02:00
|
|
|
if (Region *region = llvm::dyn_cast_if_present<Region *>(limit))
|
2022-07-26 13:22:19 -07:00
|
|
|
return ::walkSymbolTable(*region, cback);
|
|
|
|
return ::walkSymbolTable(limit.get<Operation *>(), cback);
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:57:06 -08:00
|
|
|
/// The representation of the symbol within this scope.
|
|
|
|
SymbolRefAttr symbol;
|
|
|
|
|
|
|
|
/// The IR unit representing this scope.
|
|
|
|
llvm::PointerUnion<Operation *, Region *> limit;
|
|
|
|
};
|
2021-12-07 18:27:58 +00:00
|
|
|
} // namespace
|
2020-01-27 22:57:06 -08:00
|
|
|
|
|
|
|
/// Collect all of the symbol scopes from 'symbol' to (inclusive) 'limit'.
|
|
|
|
static SmallVector<SymbolScope, 2> collectSymbolScopes(Operation *symbol,
|
|
|
|
Operation *limit) {
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symName = SymbolTable::getSymbolName(symbol);
|
2020-01-13 15:23:01 -08:00
|
|
|
assert(!symbol->hasTrait<OpTrait::SymbolTable>() || symbol != limit);
|
|
|
|
|
|
|
|
// Compute the ancestors of 'limit'.
|
2021-04-15 16:08:54 -07:00
|
|
|
SetVector<Operation *, SmallVector<Operation *, 4>,
|
|
|
|
SmallPtrSet<Operation *, 4>>
|
2020-01-13 15:23:01 -08:00
|
|
|
limitAncestors;
|
|
|
|
Operation *limitAncestor = limit;
|
|
|
|
do {
|
|
|
|
// Check to see if 'symbol' is an ancestor of 'limit'.
|
|
|
|
if (limitAncestor == symbol) {
|
|
|
|
// Check that the nearest symbol table is 'symbol's parent. SymbolRefAttr
|
|
|
|
// doesn't support parent references.
|
2020-01-27 22:57:06 -08:00
|
|
|
if (SymbolTable::getNearestSymbolTable(limit->getParentOp()) ==
|
|
|
|
symbol->getParentOp())
|
2021-08-29 14:22:24 -07:00
|
|
|
return {{SymbolRefAttr::get(symName), limit}};
|
2020-01-27 22:57:06 -08:00
|
|
|
return {};
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
limitAncestors.insert(limitAncestor);
|
|
|
|
} while ((limitAncestor = limitAncestor->getParentOp()));
|
|
|
|
|
|
|
|
// Try to find the first ancestor of 'symbol' that is an ancestor of 'limit'.
|
|
|
|
Operation *commonAncestor = symbol->getParentOp();
|
|
|
|
do {
|
|
|
|
if (limitAncestors.count(commonAncestor))
|
|
|
|
break;
|
|
|
|
} while ((commonAncestor = commonAncestor->getParentOp()));
|
|
|
|
assert(commonAncestor && "'limit' and 'symbol' have no common ancestor");
|
|
|
|
|
|
|
|
// Compute the set of valid nested references for 'symbol' as far up to the
|
|
|
|
// common ancestor as possible.
|
|
|
|
SmallVector<SymbolRefAttr, 2> references;
|
2020-01-27 22:57:06 -08:00
|
|
|
bool collectedAllReferences = succeeded(
|
|
|
|
collectValidReferencesFor(symbol, symName, commonAncestor, references));
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
// Handle the case where the common ancestor is 'limit'.
|
|
|
|
if (commonAncestor == limit) {
|
2020-01-27 22:57:06 -08:00
|
|
|
SmallVector<SymbolScope, 2> scopes;
|
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
// Walk each of the ancestors of 'symbol', calling the compute function for
|
|
|
|
// each one.
|
|
|
|
Operation *limitIt = symbol->getParentOp();
|
|
|
|
for (size_t i = 0, e = references.size(); i != e;
|
|
|
|
++i, limitIt = limitIt->getParentOp()) {
|
2020-01-27 22:57:06 -08:00
|
|
|
assert(limitIt->hasTrait<OpTrait::SymbolTable>());
|
|
|
|
scopes.push_back({references[i], &limitIt->getRegion(0)});
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2020-01-27 22:57:06 -08:00
|
|
|
return scopes;
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we just need the symbol reference for 'symbol' that will be
|
|
|
|
// used within 'limit'. This is the last reference in the list we computed
|
|
|
|
// above if we were able to collect all references.
|
|
|
|
if (!collectedAllReferences)
|
2020-01-27 22:57:06 -08:00
|
|
|
return {};
|
|
|
|
return {{references.back(), limit}};
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2020-01-27 22:57:06 -08:00
|
|
|
static SmallVector<SymbolScope, 2> collectSymbolScopes(Operation *symbol,
|
|
|
|
Region *limit) {
|
|
|
|
auto scopes = collectSymbolScopes(symbol, limit->getParentOp());
|
2020-01-13 15:23:01 -08:00
|
|
|
|
2020-01-27 22:57:06 -08:00
|
|
|
// If we collected some scopes to walk, make sure to constrain the one for
|
|
|
|
// limit to the specific region requested.
|
|
|
|
if (!scopes.empty())
|
|
|
|
scopes.back().limit = limit;
|
|
|
|
return scopes;
|
|
|
|
}
|
2021-08-29 14:22:24 -07:00
|
|
|
static SmallVector<SymbolScope, 1> collectSymbolScopes(StringAttr symbol,
|
[mlir] Make overloads of SymbolTable::replaceAllSymbolUses consistent. (#68320)
This function has several overloads that allow to specify the symbol
that should be renamed and the scope for that renaming in different
ways. The overloads were inconsistent in the following way (quoted
strings are `StringAttr`s, other variables are `Operation *`):
* `replaceAllSymbolUses(symbolOp, "new_symbol", scopeOp)` would traverse
into the nested regions of `scopeOp` and hence rename the symbol inside
of `scopeOp`.
* `replaceAllSymbolUses("symbol", "new_symbol", scopeOp)` would *not*
traverse into the nested regions of `scopeOp` and hence *not* rename the
symbol.
The underlying behavior was spread over different places and is somewhat
hard to understand. The two overloads above mainly differed by what
`collectSymbolScopes` computed, which is itself overloaded. If `scopeOp`
is a top-level module, then the overload on `(Operation *, Operation
*)`, which is used in the first of the above cases, computes a scope
where the body region of the module is the `limit`; however, the
overload on `(StringAttr, Operation *)` computed the module op itself as
the `limit`. Later, `walkSymbolTable` would walk the body of the module
if it was given as a region but it would *not* enter the regions of the
module op because that op has a symbol table (which was assumed to be a
*different* scope).
The fix in this commit is change the behavior of `collectSymbolScopes`
such that the `(StringAttr, Operation *)` overload returns a scope for
each region in the `limit` argument.
2023-10-10 07:47:08 +02:00
|
|
|
Region *limit) {
|
2021-08-29 14:22:24 -07:00
|
|
|
return {{SymbolRefAttr::get(symbol), limit}};
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
|
|
|
|
[mlir] Make overloads of SymbolTable::replaceAllSymbolUses consistent. (#68320)
This function has several overloads that allow to specify the symbol
that should be renamed and the scope for that renaming in different
ways. The overloads were inconsistent in the following way (quoted
strings are `StringAttr`s, other variables are `Operation *`):
* `replaceAllSymbolUses(symbolOp, "new_symbol", scopeOp)` would traverse
into the nested regions of `scopeOp` and hence rename the symbol inside
of `scopeOp`.
* `replaceAllSymbolUses("symbol", "new_symbol", scopeOp)` would *not*
traverse into the nested regions of `scopeOp` and hence *not* rename the
symbol.
The underlying behavior was spread over different places and is somewhat
hard to understand. The two overloads above mainly differed by what
`collectSymbolScopes` computed, which is itself overloaded. If `scopeOp`
is a top-level module, then the overload on `(Operation *, Operation
*)`, which is used in the first of the above cases, computes a scope
where the body region of the module is the `limit`; however, the
overload on `(StringAttr, Operation *)` computed the module op itself as
the `limit`. Later, `walkSymbolTable` would walk the body of the module
if it was given as a region but it would *not* enter the regions of the
module op because that op has a symbol table (which was assumed to be a
*different* scope).
The fix in this commit is change the behavior of `collectSymbolScopes`
such that the `(StringAttr, Operation *)` overload returns a scope for
each region in the `limit` argument.
2023-10-10 07:47:08 +02:00
|
|
|
static SmallVector<SymbolScope, 1> collectSymbolScopes(StringAttr symbol,
|
|
|
|
Operation *limit) {
|
|
|
|
SmallVector<SymbolScope, 1> scopes;
|
|
|
|
auto symbolRef = SymbolRefAttr::get(symbol);
|
|
|
|
for (auto ®ion : limit->getRegions())
|
|
|
|
scopes.push_back({symbolRef, ®ion});
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
/// Returns true if the given reference 'SubRef' is a sub reference of the
|
|
|
|
/// reference 'ref', i.e. 'ref' is a further qualified reference.
|
|
|
|
static bool isReferencePrefixOf(SymbolRefAttr subRef, SymbolRefAttr ref) {
|
|
|
|
if (ref == subRef)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the references are not pointer equal, check to see if `subRef` is a
|
|
|
|
// prefix of `ref`.
|
2023-05-11 11:10:46 +02:00
|
|
|
if (llvm::isa<FlatSymbolRefAttr>(ref) ||
|
2020-01-13 15:23:01 -08:00
|
|
|
ref.getRootReference() != subRef.getRootReference())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto refLeafs = ref.getNestedReferences();
|
|
|
|
auto subRefLeafs = subRef.getNestedReferences();
|
|
|
|
return subRefLeafs.size() < refLeafs.size() &&
|
|
|
|
subRefLeafs == refLeafs.take_front(subRefLeafs.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable::getSymbolUses
|
|
|
|
|
2020-01-27 22:57:06 -08:00
|
|
|
/// The implementation of SymbolTable::getSymbolUses below.
|
|
|
|
template <typename FromT>
|
2022-12-19 19:50:54 +01:00
|
|
|
static std::optional<SymbolTable::UseRange> getSymbolUsesImpl(FromT from) {
|
2020-01-27 22:57:06 -08:00
|
|
|
std::vector<SymbolTable::SymbolUse> uses;
|
2022-07-26 13:22:19 -07:00
|
|
|
auto walkFn = [&](SymbolTable::SymbolUse symbolUse) {
|
2020-01-27 22:57:06 -08:00
|
|
|
uses.push_back(symbolUse);
|
|
|
|
return WalkResult::advance();
|
|
|
|
};
|
|
|
|
auto result = walkSymbolUses(from, walkFn);
|
2022-12-19 19:50:54 +01:00
|
|
|
return result ? std::optional<SymbolTable::UseRange>(std::move(uses))
|
2022-12-03 18:50:27 -08:00
|
|
|
: std::nullopt;
|
2020-01-27 22:57:06 -08:00
|
|
|
}
|
|
|
|
|
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
|
2019-10-24 10:46:47 -07:00
|
|
|
/// also define a symbol table. This is because we treat the region as the
|
|
|
|
/// boundary of the symbol table, and not the op itself. This function returns
|
2022-12-10 17:11:23 -08:00
|
|
|
/// std::nullopt if there are any unknown operations that may potentially be
|
|
|
|
/// symbol tables.
|
2022-12-19 19:50:54 +01:00
|
|
|
auto SymbolTable::getSymbolUses(Operation *from) -> std::optional<UseRange> {
|
2020-01-27 22:57:06 -08:00
|
|
|
return getSymbolUsesImpl(from);
|
|
|
|
}
|
2022-12-19 19:50:54 +01:00
|
|
|
auto SymbolTable::getSymbolUses(Region *from) -> std::optional<UseRange> {
|
2020-01-27 22:57:06 -08:00
|
|
|
return getSymbolUsesImpl(MutableArrayRef<Region>(*from));
|
2019-10-08 18:38:05 -07:00
|
|
|
}
|
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable::getSymbolUses
|
|
|
|
|
|
|
|
/// The implementation of SymbolTable::getSymbolUses below.
|
2020-01-27 22:57:06 -08:00
|
|
|
template <typename SymbolT, typename IRUnitT>
|
2022-12-19 19:50:54 +01:00
|
|
|
static std::optional<SymbolTable::UseRange> getSymbolUsesImpl(SymbolT symbol,
|
|
|
|
IRUnitT *limit) {
|
2020-01-13 15:23:01 -08:00
|
|
|
std::vector<SymbolTable::SymbolUse> uses;
|
2020-01-27 22:57:06 -08:00
|
|
|
for (SymbolScope &scope : collectSymbolScopes(symbol, limit)) {
|
|
|
|
if (!scope.walk([&](SymbolTable::SymbolUse symbolUse) {
|
|
|
|
if (isReferencePrefixOf(scope.symbol, symbolUse.getSymbolRef()))
|
2020-01-13 15:23:01 -08:00
|
|
|
uses.push_back(symbolUse);
|
2020-01-27 22:57:06 -08:00
|
|
|
}))
|
2022-12-03 18:50:27 -08:00
|
|
|
return std::nullopt;
|
2020-01-27 22:57:06 -08:00
|
|
|
}
|
|
|
|
return SymbolTable::UseRange(std::move(uses));
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
|
|
|
|
2019-10-08 18:38:05 -07:00
|
|
|
/// 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
|
2022-12-04 17:23:50 -08:00
|
|
|
/// traverse into any nested symbol tables. This function returns std::nullopt
|
|
|
|
/// if there are any unknown operations that may potentially be symbol tables.
|
2021-08-29 14:22:24 -07:00
|
|
|
auto SymbolTable::getSymbolUses(StringAttr symbol, Operation *from)
|
2022-12-19 19:50:54 +01:00
|
|
|
-> std::optional<UseRange> {
|
2020-01-13 15:23:01 -08:00
|
|
|
return getSymbolUsesImpl(symbol, from);
|
|
|
|
}
|
|
|
|
auto SymbolTable::getSymbolUses(Operation *symbol, Operation *from)
|
2022-12-19 19:50:54 +01:00
|
|
|
-> std::optional<UseRange> {
|
2020-01-13 15:23:01 -08:00
|
|
|
return getSymbolUsesImpl(symbol, from);
|
|
|
|
}
|
2021-08-29 14:22:24 -07:00
|
|
|
auto SymbolTable::getSymbolUses(StringAttr symbol, Region *from)
|
2022-12-19 19:50:54 +01:00
|
|
|
-> std::optional<UseRange> {
|
2020-01-27 22:57:06 -08:00
|
|
|
return getSymbolUsesImpl(symbol, from);
|
|
|
|
}
|
|
|
|
auto SymbolTable::getSymbolUses(Operation *symbol, Region *from)
|
2022-12-19 19:50:54 +01:00
|
|
|
-> std::optional<UseRange> {
|
2020-01-27 22:57:06 -08:00
|
|
|
return getSymbolUsesImpl(symbol, from);
|
|
|
|
}
|
2019-10-08 18:38:05 -07:00
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable::symbolKnownUseEmpty
|
|
|
|
|
|
|
|
/// The implementation of SymbolTable::symbolKnownUseEmpty below.
|
2020-01-27 22:57:06 -08:00
|
|
|
template <typename SymbolT, typename IRUnitT>
|
|
|
|
static bool symbolKnownUseEmptyImpl(SymbolT symbol, IRUnitT *limit) {
|
|
|
|
for (SymbolScope &scope : collectSymbolScopes(symbol, limit)) {
|
|
|
|
// Walk all of the symbol uses looking for a reference to 'symbol'.
|
2022-07-26 13:22:19 -07:00
|
|
|
if (scope.walk([&](SymbolTable::SymbolUse symbolUse) {
|
2020-01-27 22:57:06 -08:00
|
|
|
return isReferencePrefixOf(scope.symbol, symbolUse.getSymbolRef())
|
2020-01-13 15:23:01 -08:00
|
|
|
? WalkResult::interrupt()
|
|
|
|
: WalkResult::advance();
|
2020-01-27 22:57:06 -08:00
|
|
|
}) != WalkResult::advance())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
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
|
2020-01-27 22:57:06 -08:00
|
|
|
/// tables. This function will also return false if there are any unknown
|
|
|
|
/// operations that may potentially be symbol tables.
|
2021-08-29 14:22:24 -07:00
|
|
|
bool SymbolTable::symbolKnownUseEmpty(StringAttr symbol, Operation *from) {
|
2020-01-13 15:23:01 -08:00
|
|
|
return symbolKnownUseEmptyImpl(symbol, from);
|
|
|
|
}
|
|
|
|
bool SymbolTable::symbolKnownUseEmpty(Operation *symbol, Operation *from) {
|
|
|
|
return symbolKnownUseEmptyImpl(symbol, from);
|
2019-10-08 10:21:26 -07:00
|
|
|
}
|
2021-08-29 14:22:24 -07:00
|
|
|
bool SymbolTable::symbolKnownUseEmpty(StringAttr symbol, Region *from) {
|
2020-01-27 22:57:06 -08:00
|
|
|
return symbolKnownUseEmptyImpl(symbol, from);
|
|
|
|
}
|
|
|
|
bool SymbolTable::symbolKnownUseEmpty(Operation *symbol, Region *from) {
|
|
|
|
return symbolKnownUseEmptyImpl(symbol, from);
|
|
|
|
}
|
2019-10-24 10:46:47 -07:00
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTable::replaceAllSymbolUses
|
|
|
|
|
|
|
|
/// Generates a new symbol reference attribute with a new leaf reference.
|
2020-01-14 14:06:12 +01:00
|
|
|
static SymbolRefAttr generateNewRefAttr(SymbolRefAttr oldAttr,
|
|
|
|
FlatSymbolRefAttr newLeafAttr) {
|
2023-05-11 11:10:46 +02:00
|
|
|
if (llvm::isa<FlatSymbolRefAttr>(oldAttr))
|
2020-01-13 15:23:01 -08:00
|
|
|
return newLeafAttr;
|
|
|
|
auto nestedRefs = llvm::to_vector<2>(oldAttr.getNestedReferences());
|
|
|
|
nestedRefs.back() = newLeafAttr;
|
2021-08-29 14:22:24 -07:00
|
|
|
return SymbolRefAttr::get(oldAttr.getRootReference(), nestedRefs);
|
2020-01-13 15:23:01 -08:00
|
|
|
}
|
2019-10-24 10:46:47 -07:00
|
|
|
|
2020-01-13 15:23:01 -08:00
|
|
|
/// The implementation of SymbolTable::replaceAllSymbolUses below.
|
2020-01-27 22:57:06 -08:00
|
|
|
template <typename SymbolT, typename IRUnitT>
|
|
|
|
static LogicalResult
|
2021-08-29 14:22:24 -07:00
|
|
|
replaceAllSymbolUsesImpl(SymbolT symbol, StringAttr newSymbol, IRUnitT *limit) {
|
2020-01-13 15:23:01 -08:00
|
|
|
// Generate a new attribute to replace the given attribute.
|
2021-08-29 14:22:24 -07:00
|
|
|
FlatSymbolRefAttr newLeafAttr = FlatSymbolRefAttr::get(newSymbol);
|
2020-01-27 22:57:06 -08:00
|
|
|
for (SymbolScope &scope : collectSymbolScopes(symbol, limit)) {
|
2022-07-26 13:22:19 -07:00
|
|
|
SymbolRefAttr oldAttr = scope.symbol;
|
2020-01-27 22:57:06 -08:00
|
|
|
SymbolRefAttr newAttr = generateNewRefAttr(scope.symbol, newLeafAttr);
|
2022-11-09 20:59:40 -08:00
|
|
|
AttrTypeReplacer replacer;
|
|
|
|
replacer.addReplacement(
|
|
|
|
[&](SymbolRefAttr attr) -> std::pair<Attribute, WalkResult> {
|
|
|
|
// Regardless of the match, don't walk nested SymbolRefAttrs, we don't
|
|
|
|
// want to accidentally replace an inner reference.
|
|
|
|
if (attr == oldAttr)
|
|
|
|
return {newAttr, WalkResult::skip()};
|
|
|
|
// Handle prefix matches.
|
|
|
|
if (isReferencePrefixOf(oldAttr, attr)) {
|
2022-07-26 13:22:19 -07:00
|
|
|
auto oldNestedRefs = oldAttr.getNestedReferences();
|
2022-11-09 20:59:40 -08:00
|
|
|
auto nestedRefs = attr.getNestedReferences();
|
2022-07-26 13:22:19 -07:00
|
|
|
if (oldNestedRefs.empty())
|
2022-07-28 02:40:08 -07:00
|
|
|
return {SymbolRefAttr::get(newSymbol, nestedRefs),
|
|
|
|
WalkResult::skip()};
|
2022-07-26 13:22:19 -07:00
|
|
|
|
|
|
|
auto newNestedRefs = llvm::to_vector<4>(nestedRefs);
|
|
|
|
newNestedRefs[oldNestedRefs.size() - 1] = newLeafAttr;
|
2022-11-09 20:59:40 -08:00
|
|
|
return {SymbolRefAttr::get(attr.getRootReference(), newNestedRefs),
|
|
|
|
WalkResult::skip()};
|
2022-07-26 13:22:19 -07:00
|
|
|
}
|
2022-07-28 02:40:08 -07:00
|
|
|
return {attr, WalkResult::skip()};
|
2022-11-09 20:59:40 -08:00
|
|
|
});
|
|
|
|
|
2023-01-14 01:25:58 -08:00
|
|
|
auto walkFn = [&](Operation *op) -> std::optional<WalkResult> {
|
2022-11-09 20:59:40 -08:00
|
|
|
replacer.replaceElementsIn(op);
|
2019-10-24 10:46:47 -07:00
|
|
|
return WalkResult::advance();
|
2020-01-13 15:23:01 -08:00
|
|
|
};
|
2022-07-26 13:22:19 -07:00
|
|
|
if (!scope.walkSymbolTable(walkFn))
|
2020-01-27 22:57:06 -08:00
|
|
|
return failure();
|
|
|
|
}
|
2019-10-24 10:46:47 -07:00
|
|
|
return success();
|
|
|
|
}
|
2020-01-13 15:23:01 -08:00
|
|
|
|
|
|
|
/// Attempt to replace all uses of the given symbol 'oldSymbol' with the
|
|
|
|
/// provided symbol 'newSymbol' that are nested within the given operation
|
2020-01-27 22:57:06 -08:00
|
|
|
/// 'from'. This does not traverse into any nested symbol tables. If there are
|
|
|
|
/// any unknown operations that may potentially be symbol tables, no uses are
|
|
|
|
/// replaced and failure is returned.
|
2021-08-29 14:22:24 -07:00
|
|
|
LogicalResult SymbolTable::replaceAllSymbolUses(StringAttr oldSymbol,
|
|
|
|
StringAttr newSymbol,
|
2020-01-13 15:23:01 -08:00
|
|
|
Operation *from) {
|
|
|
|
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from);
|
|
|
|
}
|
|
|
|
LogicalResult SymbolTable::replaceAllSymbolUses(Operation *oldSymbol,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr newSymbol,
|
2020-01-13 15:23:01 -08:00
|
|
|
Operation *from) {
|
|
|
|
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from);
|
|
|
|
}
|
2021-08-29 14:22:24 -07:00
|
|
|
LogicalResult SymbolTable::replaceAllSymbolUses(StringAttr oldSymbol,
|
|
|
|
StringAttr newSymbol,
|
2020-01-27 22:57:06 -08:00
|
|
|
Region *from) {
|
|
|
|
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from);
|
|
|
|
}
|
|
|
|
LogicalResult SymbolTable::replaceAllSymbolUses(Operation *oldSymbol,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr newSymbol,
|
2020-01-27 22:57:06 -08:00
|
|
|
Region *from) {
|
|
|
|
return replaceAllSymbolUsesImpl(oldSymbol, newSymbol, from);
|
|
|
|
}
|
2020-04-27 12:57:32 -07:00
|
|
|
|
2020-10-16 11:56:52 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolTableCollection
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Operation *SymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symbol) {
|
2020-10-16 11:56:52 -07:00
|
|
|
return getSymbolTable(symbolTableOp).lookup(symbol);
|
|
|
|
}
|
|
|
|
Operation *SymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
SymbolRefAttr name) {
|
|
|
|
SmallVector<Operation *, 4> symbols;
|
|
|
|
if (failed(lookupSymbolIn(symbolTableOp, name, symbols)))
|
|
|
|
return nullptr;
|
|
|
|
return symbols.back();
|
|
|
|
}
|
|
|
|
/// A variant of 'lookupSymbolIn' that returns all of the symbols referenced by
|
|
|
|
/// a given SymbolRefAttr. Returns failure if any of the nested references could
|
|
|
|
/// not be resolved.
|
|
|
|
LogicalResult
|
|
|
|
SymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
SymbolRefAttr name,
|
|
|
|
SmallVectorImpl<Operation *> &symbols) {
|
2021-08-29 14:22:24 -07:00
|
|
|
auto lookupFn = [this](Operation *symbolTableOp, StringAttr symbol) {
|
2020-10-16 11:56:52 -07:00
|
|
|
return lookupSymbolIn(symbolTableOp, symbol);
|
|
|
|
};
|
|
|
|
return lookupSymbolInImpl(symbolTableOp, name, symbols, lookupFn);
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:57:00 -07:00
|
|
|
/// Returns the operation registered with the given symbol name within the
|
|
|
|
/// closest parent operation of, or including, 'from' with the
|
|
|
|
/// 'OpTrait::SymbolTable' trait. Returns nullptr if no valid symbol was
|
|
|
|
/// found.
|
|
|
|
Operation *SymbolTableCollection::lookupNearestSymbolFrom(Operation *from,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr symbol) {
|
2020-10-16 11:57:00 -07:00
|
|
|
Operation *symbolTableOp = SymbolTable::getNearestSymbolTable(from);
|
|
|
|
return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr;
|
|
|
|
}
|
|
|
|
Operation *
|
|
|
|
SymbolTableCollection::lookupNearestSymbolFrom(Operation *from,
|
|
|
|
SymbolRefAttr symbol) {
|
|
|
|
Operation *symbolTableOp = SymbolTable::getNearestSymbolTable(from);
|
|
|
|
return symbolTableOp ? lookupSymbolIn(symbolTableOp, symbol) : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:56:52 -07:00
|
|
|
/// Lookup, or create, a symbol table for an operation.
|
|
|
|
SymbolTable &SymbolTableCollection::getSymbolTable(Operation *op) {
|
|
|
|
auto it = symbolTables.try_emplace(op, nullptr);
|
|
|
|
if (it.second)
|
|
|
|
it.first->second = std::make_unique<SymbolTable>(op);
|
|
|
|
return *it.first->second;
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:55:52 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LockedSymbolTableCollection
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Operation *LockedSymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
StringAttr symbol) {
|
|
|
|
return getSymbolTable(symbolTableOp).lookup(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *
|
|
|
|
LockedSymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
FlatSymbolRefAttr symbol) {
|
|
|
|
return lookupSymbolIn(symbolTableOp, symbol.getAttr());
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation *LockedSymbolTableCollection::lookupSymbolIn(Operation *symbolTableOp,
|
|
|
|
SymbolRefAttr name) {
|
|
|
|
SmallVector<Operation *> symbols;
|
|
|
|
if (failed(lookupSymbolIn(symbolTableOp, name, symbols)))
|
|
|
|
return nullptr;
|
|
|
|
return symbols.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
LogicalResult LockedSymbolTableCollection::lookupSymbolIn(
|
|
|
|
Operation *symbolTableOp, SymbolRefAttr name,
|
|
|
|
SmallVectorImpl<Operation *> &symbols) {
|
|
|
|
auto lookupFn = [this](Operation *symbolTableOp, StringAttr symbol) {
|
|
|
|
return lookupSymbolIn(symbolTableOp, symbol);
|
|
|
|
};
|
|
|
|
return lookupSymbolInImpl(symbolTableOp, name, symbols, lookupFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolTable &
|
2023-02-22 14:05:50 -08:00
|
|
|
LockedSymbolTableCollection::getSymbolTable(Operation *symbolTableOp) {
|
2023-02-21 09:55:52 -08:00
|
|
|
assert(symbolTableOp->hasTrait<OpTrait::SymbolTable>());
|
|
|
|
// Try to find an existing symbol table.
|
|
|
|
{
|
|
|
|
llvm::sys::SmartScopedReader<true> lock(mutex);
|
|
|
|
auto it = collection.symbolTables.find(symbolTableOp);
|
|
|
|
if (it != collection.symbolTables.end())
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
// Create a symbol table for the operation. Perform construction outside of
|
|
|
|
// the critical section.
|
|
|
|
auto symbolTable = std::make_unique<SymbolTable>(symbolTableOp);
|
|
|
|
// Insert the constructed symbol table.
|
|
|
|
llvm::sys::SmartScopedWriter<true> lock(mutex);
|
|
|
|
return *collection.symbolTables
|
|
|
|
.insert({symbolTableOp, std::move(symbolTable)})
|
|
|
|
.first->second;
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:01:54 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolUserMap
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
SymbolUserMap::SymbolUserMap(SymbolTableCollection &symbolTable,
|
|
|
|
Operation *symbolTableOp)
|
|
|
|
: symbolTable(symbolTable) {
|
|
|
|
// Walk each of the symbol tables looking for discardable callgraph nodes.
|
|
|
|
SmallVector<Operation *> symbols;
|
|
|
|
auto walkFn = [&](Operation *symbolTableOp, bool allUsesVisible) {
|
|
|
|
for (Operation &nestedOp : symbolTableOp->getRegion(0).getOps()) {
|
|
|
|
auto symbolUses = SymbolTable::getSymbolUses(&nestedOp);
|
|
|
|
assert(symbolUses && "expected uses to be valid");
|
|
|
|
|
|
|
|
for (const SymbolTable::SymbolUse &use : *symbolUses) {
|
|
|
|
symbols.clear();
|
|
|
|
(void)symbolTable.lookupSymbolIn(symbolTableOp, use.getSymbolRef(),
|
|
|
|
symbols);
|
|
|
|
for (Operation *symbolOp : symbols)
|
|
|
|
symbolToUsers[symbolOp].insert(use.getUser());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// We just set `allSymUsesVisible` to false here because it isn't necessary
|
|
|
|
// for building the user map.
|
|
|
|
SymbolTable::walkSymbolTables(symbolTableOp, /*allSymUsesVisible=*/false,
|
|
|
|
walkFn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolUserMap::replaceAllUsesWith(Operation *symbol,
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr newSymbolName) {
|
2021-03-09 15:01:54 -08:00
|
|
|
auto it = symbolToUsers.find(symbol);
|
|
|
|
if (it == symbolToUsers.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Replace the uses within the users of `symbol`.
|
2022-07-08 08:57:37 +03:00
|
|
|
for (Operation *user : it->second)
|
2021-03-09 15:01:54 -08:00
|
|
|
(void)SymbolTable::replaceAllSymbolUses(symbol, newSymbolName, user);
|
|
|
|
|
|
|
|
// Move the current users of `symbol` to the new symbol if it is in the
|
|
|
|
// symbol table.
|
|
|
|
Operation *newSymbol =
|
|
|
|
symbolTable.lookupSymbolIn(symbol->getParentOp(), newSymbolName);
|
|
|
|
if (newSymbol != symbol) {
|
2022-07-08 08:57:37 +03:00
|
|
|
// Transfer over the users to the new symbol. The reference to the old one
|
|
|
|
// is fetched again as the iterator is invalidated during the insertion.
|
|
|
|
auto newIt = symbolToUsers.try_emplace(newSymbol, SetVector<Operation *>{});
|
|
|
|
auto oldIt = symbolToUsers.find(symbol);
|
|
|
|
assert(oldIt != symbolToUsers.end() && "missing old users list");
|
|
|
|
if (newIt.second)
|
|
|
|
newIt.first->second = std::move(oldIt->second);
|
2021-03-09 15:01:54 -08:00
|
|
|
else
|
2022-07-08 08:57:37 +03:00
|
|
|
newIt.first->second.set_union(oldIt->second);
|
|
|
|
symbolToUsers.erase(oldIt);
|
2021-03-09 15:01:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:23:55 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visibility parsing implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ParseResult impl::parseOptionalVisibilityKeyword(OpAsmParser &parser,
|
|
|
|
NamedAttrList &attrs) {
|
|
|
|
StringRef visibility;
|
|
|
|
if (parser.parseOptionalKeyword(&visibility, {"public", "private", "nested"}))
|
|
|
|
return failure();
|
|
|
|
|
|
|
|
StringAttr visibilityAttr = parser.getBuilder().getStringAttr(visibility);
|
|
|
|
attrs.push_back(parser.getBuilder().getNamedAttr(
|
|
|
|
SymbolTable::getVisibilityAttrName(), visibilityAttr));
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:57:32 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol Interfaces
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Include the generated symbol interfaces.
|
|
|
|
#include "mlir/IR/SymbolInterfaces.cpp.inc"
|