Skip to content

[mlir][Transforms][NFC] Dialect Conversion: Keep unresolvedMaterializations up to date #144254

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
Jun 18, 2025
Merged
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
33 changes: 20 additions & 13 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ namespace detail {
struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
explicit ConversionPatternRewriterImpl(MLIRContext *ctx,
const ConversionConfig &config)
: context(ctx), eraseRewriter(ctx), config(config) {}
: context(ctx), config(config) {}

//===--------------------------------------------------------------------===//
// State Management
Expand Down Expand Up @@ -981,8 +981,11 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
/// no new IR is created between calls to `eraseOp`/`eraseBlock`.
struct SingleEraseRewriter : public RewriterBase, RewriterBase::Listener {
public:
SingleEraseRewriter(MLIRContext *context)
: RewriterBase(context, /*listener=*/this) {}
SingleEraseRewriter(
MLIRContext *context,
std::function<void(Operation *)> opErasedCallback = nullptr)
: RewriterBase(context, /*listener=*/this),
opErasedCallback(opErasedCallback) {}

/// Erase the given op (unless it was already erased).
void eraseOp(Operation *op) override {
Expand All @@ -1003,13 +1006,20 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {

bool wasErased(void *ptr) const { return erased.contains(ptr); }

void notifyOperationErased(Operation *op) override { erased.insert(op); }
void notifyOperationErased(Operation *op) override {
erased.insert(op);
if (opErasedCallback)
opErasedCallback(op);
}

void notifyBlockErased(Block *block) override { erased.insert(block); }

private:
/// Pointers to all erased operations and blocks.
DenseSet<void *> erased;

/// A callback that is invoked when an operation is erased.
std::function<void(Operation *)> opErasedCallback;
};

//===--------------------------------------------------------------------===//
Expand All @@ -1019,11 +1029,6 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
/// MLIR context.
MLIRContext *context;

/// A rewriter that keeps track of ops/block that were already erased and
/// skips duplicate op/block erasures. This rewriter is used during the
/// "cleanup" phase.
SingleEraseRewriter eraseRewriter;

// Mapping between replaced values that differ in type. This happens when
// replacing a value with one of a different type.
ConversionValueMapping mapping;
Expand Down Expand Up @@ -1195,6 +1200,11 @@ void ConversionPatternRewriterImpl::applyRewrites() {
rewrites[i]->commit(rewriter);

// Clean up all rewrites.
SingleEraseRewriter eraseRewriter(
context, /*opErasedCallback=*/[&](Operation *op) {
if (auto castOp = dyn_cast<UnrealizedConversionCastOp>(op))
unresolvedMaterializations.erase(castOp);
});
for (auto &rewrite : rewrites)
rewrite->cleanup(eraseRewriter);
}
Expand Down Expand Up @@ -2714,11 +2724,8 @@ LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
SmallVector<UnrealizedConversionCastOp> allCastOps;
const DenseMap<UnrealizedConversionCastOp, UnresolvedMaterializationRewrite *>
&materializations = rewriterImpl.unresolvedMaterializations;
for (auto it : materializations) {
if (rewriterImpl.eraseRewriter.wasErased(it.first))
continue;
for (auto it : materializations)
allCastOps.push_back(it.first);
}

// Reconcile all UnrealizedConversionCastOps that were inserted by the
// dialect conversion frameworks. (Not the one that were inserted by
Expand Down
Loading