Skip to content

[mlir][Transforms] Dialect conversion: Simplify replaceOp implementation #145155

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 7 additions & 19 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,37 +1580,25 @@ void ConversionPatternRewriterImpl::replaceOp(

// Check if replaced op is an unresolved materialization, i.e., an
// unrealized_conversion_cast op that was created by the conversion driver.
bool isUnresolvedMaterialization = false;
if (auto castOp = dyn_cast<UnrealizedConversionCastOp>(op))
if (unresolvedMaterializations.contains(castOp))
isUnresolvedMaterialization = true;
if (auto castOp = dyn_cast<UnrealizedConversionCastOp>(op)) {
// Make sure that the user does not mess with unresolved materializations
// that were inserted by the conversion driver. We keep track of these
// ops in internal data structures.
assert(!unresolvedMaterializations.contains(castOp) &&
"attempting to replace/erase an unresolved materialization");
}

// Create mappings for each of the new result values.
for (auto [repl, result] : llvm::zip_equal(newValues, op->getResults())) {
if (repl.empty()) {
// This result was dropped and no replacement value was provided.
if (isUnresolvedMaterialization) {
// Do not create another materializations if we are erasing a
// materialization.
continue;
}

// Materialize a replacement value "out of thin air".
buildUnresolvedMaterialization(
MaterializationKind::Source, computeInsertPoint(result),
result.getLoc(), /*valuesToMap=*/{result}, /*inputs=*/ValueRange(),
/*outputTypes=*/result.getType(), /*originalType=*/Type(),
currentTypeConverter);
continue;
} else {
// Make sure that the user does not mess with unresolved materializations
// that were inserted by the conversion driver. We keep track of these
// ops in internal data structures. Erasing them must be allowed because
// this can happen when the user is erasing an entire block (including
// its body). But replacing them with another value should be forbidden
// to avoid problems with the `mapping`.
assert(!isUnresolvedMaterialization &&
"attempting to replace an unresolved materialization");
}

// Remap result to replacement value.
Expand Down
Loading