Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | for (auto result : op->getResults()) | ||||
addToWorklist(user); | addToWorklist(user); | ||||
} | } | ||||
private: | private: | ||||
// Look over the provided operands for any defining operations that should | // Look over the provided operands for any defining operations that should | ||||
// be re-added to the worklist. This function should be called when an | // be re-added to the worklist. This function should be called when an | ||||
// operation is modified or removed, as it may trigger further | // operation is modified or removed, as it may trigger further | ||||
// simplifications. | // simplifications. | ||||
template <typename Operands> void addToWorklist(Operands &&operands) { | template <typename Operands> | ||||
void addToWorklist(Operands &&operands) { | |||||
for (Value operand : operands) { | for (Value operand : operands) { | ||||
// If the use count of this operand is now < 2, we re-add the defining | // If the use count of this operand is now < 2, we re-add the defining | ||||
// operation to the worklist. | // operation to the worklist. | ||||
// TODO(riverriddle) This is based on the fact that zero use operations | // TODO(riverriddle) This is based on the fact that zero use operations | ||||
// may be deleted, and that single use values often have more | // may be deleted, and that single use values often have more | ||||
// canonicalization opportunities. | // canonicalization opportunities. | ||||
if (!operand.use_empty() && !operand.hasOneUse()) | if (!operand.use_empty() && !operand.hasOneUse()) | ||||
continue; | continue; | ||||
Show All 12 Lines | private: | ||||
std::vector<Operation *> worklist; | std::vector<Operation *> worklist; | ||||
DenseMap<Operation *, unsigned> worklistMap; | DenseMap<Operation *, unsigned> worklistMap; | ||||
/// Non-pattern based folder for operations. | /// Non-pattern based folder for operations. | ||||
OperationFolder folder; | OperationFolder folder; | ||||
}; | }; | ||||
} // end anonymous namespace | } // end anonymous namespace | ||||
/// Perform the rewrites while folding and erasing any dead ops. | /// Performs the rewrites while folding and erasing any dead ops. Returns true | ||||
/// if the rewrite converges in `maxIterations`. | |||||
bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, | bool GreedyPatternRewriteDriver::simplify(MutableArrayRef<Region> regions, | ||||
int maxIterations) { | int maxIterations) { | ||||
// Add the given operation to the worklist. | // Add the given operation to the worklist. | ||||
auto collectOps = [this](Operation *op) { addToWorklist(op); }; | auto collectOps = [this](Operation *op) { addToWorklist(op); }; | ||||
bool changed = false; | bool changed = false; | ||||
int i = 0; | int i = 0; | ||||
do { | do { | ||||
Show All 33 Lines | while (!worklist.empty()) { | ||||
for (auto result : op->getResults()) | for (auto result : op->getResults()) | ||||
for (auto *userOp : result.getUsers()) | for (auto *userOp : result.getUsers()) | ||||
addToWorklist(userOp); | addToWorklist(userOp); | ||||
notifyOperationRemoved(op); | notifyOperationRemoved(op); | ||||
}; | }; | ||||
// Try to fold this op. | // Try to fold this op. | ||||
if (succeeded(folder.tryToFold(op, collectOps, preReplaceAction))) { | bool inPlaceUpdate; | ||||
if ((succeeded(folder.tryToFold(op, collectOps, preReplaceAction, | |||||
&inPlaceUpdate)))) { | |||||
rriddle: We could just set this inside of preReplaceAction instead of changing the folder API for now? | |||||
Two quick questions
bondhugula: Two quick questions
1) preReplaceAction is also not called when the folding fails. Did you… | |||||
changed = true; | changed = true; | ||||
if (!inPlaceUpdate) | |||||
continue; | continue; | ||||
} | } | ||||
// Make sure that any new operations are inserted at this point. | // Make sure that any new operations are inserted at this point. | ||||
setInsertionPoint(op); | setInsertionPoint(op); | ||||
// Try to match one of the patterns. The rewriter is automatically | // Try to match one of the patterns. The rewriter is automatically | ||||
// notified of any necessary changes, so there is nothing else to do here. | // notified of any necessary changes, so there is nothing else to do here. | ||||
changed |= matcher.matchAndRewrite(op, *this); | changed |= matcher.matchAndRewrite(op, *this); | ||||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
We could just set this inside of preReplaceAction instead of changing the folder API for now?