14 #define GEN_PASS_DEF_DUPLICATEFUNCTIONELIMINATIONPASS
15 #include "mlir/Dialect/Func/Transforms/Passes.h.inc"
22 struct DuplicateFuncOpEquivalenceInfo
25 static unsigned getHashValue(
const func::FuncOp cFunc) {
27 return DenseMapInfo<func::FuncOp>::getHashValue(cFunc);
31 llvm::hash_code hash = {};
32 func::FuncOp func =
const_cast<func::FuncOp &
>(cFunc);
33 StringAttr symNameAttrName = func.getSymNameAttrName();
34 for (NamedAttribute namedAttr : cFunc->getAttrs()) {
35 StringAttr attrName = namedAttr.getName();
36 if (attrName == symNameAttrName)
38 hash = llvm::hash_combine(hash, namedAttr);
42 func.getBody().walk([&](Operation *op) {
43 hash = llvm::hash_combine(
53 static bool isEqual(func::FuncOp lhs, func::FuncOp rhs) {
56 if (lhs == getTombstoneKey() || lhs == getEmptyKey() ||
57 rhs == getTombstoneKey() || rhs == getEmptyKey())
60 if (lhs.isDeclaration() || rhs.isDeclaration())
64 if (lhs->getDiscardableAttrDictionary() !=
65 rhs->getDiscardableAttrDictionary())
71 auto pLhs = lhs.getProperties();
72 auto pRhs = rhs.getProperties();
73 pLhs.sym_name =
nullptr;
74 pRhs.sym_name =
nullptr;
84 struct DuplicateFunctionEliminationPass
85 :
public func::impl::DuplicateFunctionEliminationPassBase<
86 DuplicateFunctionEliminationPass> {
88 using DuplicateFunctionEliminationPassBase<
89 DuplicateFunctionEliminationPass>::DuplicateFunctionEliminationPassBase;
91 void runOnOperation()
override {
92 auto module = getOperation();
95 DenseSet<func::FuncOp, DuplicateFuncOpEquivalenceInfo> uniqueFuncOps;
96 DenseMap<StringAttr, func::FuncOp> getRepresentant;
97 DenseSet<func::FuncOp> toBeErased;
98 module.walk([&](func::FuncOp f) {
99 auto [repr, inserted] = uniqueFuncOps.insert(f);
100 getRepresentant[f.getSymNameAttr()] = *repr;
102 toBeErased.insert(f);
108 SymbolTableCollection symbolTable;
109 SymbolUserMap userMap(symbolTable, module);
110 for (
auto it : toBeErased) {
111 StringAttr oldSymbol = it.getSymNameAttr();
112 StringAttr newSymbol = getRepresentant[oldSymbol].getSymNameAttr();
113 userMap.replaceAllUsesWith(it, newSymbol);
Include the generated interface declarations.
static llvm::hash_code ignoreHashValue(Value)
Helper that can be used with computeHash above to ignore operation operands/result mapping.
static bool isRegionEquivalentTo(Region *lhs, Region *rhs, function_ref< LogicalResult(Value, Value)> checkEquivalent, function_ref< void(Value, Value)> markEquivalent, OperationEquivalence::Flags flags, function_ref< LogicalResult(ValueRange, ValueRange)> checkCommutativeEquivalent=nullptr)
Compare two regions (including their subregions) and return if they are equivalent.
static llvm::hash_code computeHash(Operation *op, function_ref< llvm::hash_code(Value)> hashOperands=[](Value v) { return hash_value(v);}, function_ref< llvm::hash_code(Value)> hashResults=[](Value v) { return hash_value(v);}, Flags flags=Flags::None)
Compute a hash for the given operation.