Changeset View
Changeset View
Standalone View
Standalone View
mlir/test/lib/IR/TestSymbolUses.cpp
//===- TestSymbolUses.cpp - Pass to test symbol uselists ------------------===// | //===- TestSymbolUses.cpp - Pass to test symbol uselists ------------------===// | ||||
Lint: Lint: clang-format-diff not found in user's PATH; not linting file. | |||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "TestDialect.h" | #include "TestDialect.h" | ||||
#include "mlir/IR/Function.h" | #include "mlir/IR/Function.h" | ||||
#include "mlir/Pass/Pass.h" | #include "mlir/Pass/Pass.h" | ||||
using namespace mlir; | using namespace mlir; | ||||
namespace { | namespace { | ||||
/// This is a symbol test pass that tests the symbol uselist functionality | /// This is a symbol test pass that tests the symbol uselist functionality | ||||
/// provided by the symbol table along with erasing from the symbol table. | /// provided by the symbol table along with erasing from the symbol table. | ||||
struct SymbolUsesPass : public ModulePass<SymbolUsesPass> { | struct SymbolUsesPass : public OperationPass<SymbolUsesPass, ModuleOp> { | ||||
WalkResult operateOnSymbol(Operation *symbol, ModuleOp module, | WalkResult operateOnSymbol(Operation *symbol, ModuleOp module, | ||||
SmallVectorImpl<FuncOp> &deadFunctions) { | SmallVectorImpl<FuncOp> &deadFunctions) { | ||||
// Test computing uses on a non symboltable op. | // Test computing uses on a non symboltable op. | ||||
Optional<SymbolTable::UseRange> symbolUses = | Optional<SymbolTable::UseRange> symbolUses = | ||||
SymbolTable::getSymbolUses(symbol); | SymbolTable::getSymbolUses(symbol); | ||||
// Test the conservative failure case. | // Test the conservative failure case. | ||||
if (!symbolUses) { | if (!symbolUses) { | ||||
Show All 27 Lines | for (SymbolTable::SymbolUse symbolUse : *symbolUses) { | ||||
<< "found use of symbol : " << symbolUse.getSymbolRef() << " : " | << "found use of symbol : " << symbolUse.getSymbolRef() << " : " | ||||
<< symbol->getAttr(SymbolTable::getSymbolAttrName()); | << symbol->getAttr(SymbolTable::getSymbolAttrName()); | ||||
} | } | ||||
} | } | ||||
symbol->emitRemark() << "symbol has " << llvm::size(*symbolUses) << " uses"; | symbol->emitRemark() << "symbol has " << llvm::size(*symbolUses) << " uses"; | ||||
return WalkResult::advance(); | return WalkResult::advance(); | ||||
} | } | ||||
void runOnModule() override { | void runOnOperation() override { | ||||
auto module = getModule(); | auto module = getOperation(); | ||||
// Walk nested symbols. | // Walk nested symbols. | ||||
SmallVector<FuncOp, 4> deadFunctions; | SmallVector<FuncOp, 4> deadFunctions; | ||||
module.getBodyRegion().walk([&](Operation *nestedOp) { | module.getBodyRegion().walk([&](Operation *nestedOp) { | ||||
if (SymbolTable::isSymbol(nestedOp)) | if (SymbolTable::isSymbol(nestedOp)) | ||||
return operateOnSymbol(nestedOp, module, deadFunctions); | return operateOnSymbol(nestedOp, module, deadFunctions); | ||||
return WalkResult::advance(); | return WalkResult::advance(); | ||||
}); | }); | ||||
Show All 9 Lines | for (Operation *op : deadFunctions) { | ||||
"expected erased operation to be unknown now"); | "expected erased operation to be unknown now"); | ||||
module.emitRemark() << name << " function successfully erased"; | module.emitRemark() << name << " function successfully erased"; | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
/// This is a symbol test pass that tests the symbol use replacement | /// This is a symbol test pass that tests the symbol use replacement | ||||
/// functionality provided by the symbol table. | /// functionality provided by the symbol table. | ||||
struct SymbolReplacementPass : public ModulePass<SymbolReplacementPass> { | struct SymbolReplacementPass | ||||
void runOnModule() override { | : public OperationPass<SymbolReplacementPass, ModuleOp> { | ||||
auto module = getModule(); | void runOnOperation() override { | ||||
auto module = getOperation(); | |||||
// Walk nested functions and modules. | // Walk nested functions and modules. | ||||
module.getBodyRegion().walk([&](Operation *nestedOp) { | module.getBodyRegion().walk([&](Operation *nestedOp) { | ||||
StringAttr newName = nestedOp->getAttrOfType<StringAttr>("sym.new_name"); | StringAttr newName = nestedOp->getAttrOfType<StringAttr>("sym.new_name"); | ||||
if (!newName) | if (!newName) | ||||
return; | return; | ||||
if (succeeded(SymbolTable::replaceAllSymbolUses( | if (succeeded(SymbolTable::replaceAllSymbolUses( | ||||
nestedOp, newName.getValue(), &module.getBodyRegion()))) | nestedOp, newName.getValue(), &module.getBodyRegion()))) | ||||
Show All 15 Lines |
clang-format-diff not found in user's PATH; not linting file.