Skip to content

[mlir] int-range-optmizations: Fix referencing of deleted ops #91807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mlir/include/mlir/Analysis/DataFlowFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ class DataFlowSolver {
return static_cast<const StateT *>(it->second.get());
}

/// Erase any analysis state associated with the given program point.
template <typename PointT>
void eraseState(PointT point) {
ProgramPoint pp(point);

for (auto it = analysisStates.begin(); it != analysisStates.end(); ++it) {
if (it->first.first == pp)
analysisStates.erase(it);
}
}

/// Get a uniqued program point instance. If one is not present, it is
/// created with the provided arguments.
template <typename PointT, typename... Args>
Expand Down
25 changes: 24 additions & 1 deletion mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ static FailureOr<bool> handleUge(ConstantIntRanges lhs, ConstantIntRanges rhs) {
}

namespace {
/// This class listens on IR transformations performed during a pass relying on
/// information from a `DataflowSolver`. It erases state associated with the
/// erased operation and its results from the `DataFlowSolver` so that Patterns
/// do not accidentally query old state information for newly created Ops.
class DataFlowListener : public RewriterBase::Listener {
public:
DataFlowListener(DataFlowSolver &s) : s(s) {}

protected:
void notifyOperationErased(Operation *op) override {
s.eraseState(op);
for (Value res : op->getResults())
s.eraseState(res);
}

DataFlowSolver &s;
};

struct ConvertCmpOp : public OpRewritePattern<arith::CmpIOp> {

ConvertCmpOp(MLIRContext *context, DataFlowSolver &s)
Expand Down Expand Up @@ -167,10 +185,15 @@ struct IntRangeOptimizationsPass
if (failed(solver.initializeAndRun(op)))
return signalPassFailure();

DataFlowListener listener(solver);

RewritePatternSet patterns(ctx);
populateIntRangeOptimizationsPatterns(patterns, solver);

if (failed(applyPatternsAndFoldGreedily(op, std::move(patterns))))
GreedyRewriteConfig config;
config.listener = &listener;

if (failed(applyPatternsAndFoldGreedily(op, std::move(patterns), config)))
signalPassFailure();
}
};
Expand Down