[Mlir-commits] [mlir] [mlir][Transforms][NFC] Dialect conversion: Eagerly build reverse mapping (PR #101476)
Matthias Springer
llvmlistbot at llvm.org
Sat Aug 3 00:00:44 PDT 2024
https://github.com/matthias-springer updated https://github.com/llvm/llvm-project/pull/101476
>From 82afd9d07f6c693eeeed39a30b3d1e305a347d86 Mon Sep 17 00:00:00 2001
From: Matthias Springer <mspringer at nvidia.com>
Date: Thu, 1 Aug 2024 13:37:50 +0200
Subject: [PATCH] [mlir][Transforms][NFC] Dialect conversion: Eagerly build
reverse mapping
The "inverse mapping" is an inverse IRMapping that points from replaced values to their original values. This inverse mapping is needed when legalizing unresolved materializations, to figure out if a value has any uses. (It is not sufficient to examine the IR, because some IR changes have not been materialized yet.)
There was a check in `OperationConverter::finalize` that computed the inverse mapping only when needed. This check is not needed. `legalizeUnresolvedMaterializations` always computes the inverse mapping, so we can just do that in `OperationConverter::finalize` before calling `legalizeUnresolvedMaterializations`.
---
.../Transforms/Utils/DialectConversion.cpp | 26 +++++++++----------
1 file changed, 12 insertions(+), 14 deletions(-)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index e86da57fb9157..8f9b21b7ee1e5 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2352,7 +2352,7 @@ struct OperationConverter {
LogicalResult legalizeUnresolvedMaterializations(
ConversionPatternRewriter &rewriter,
ConversionPatternRewriterImpl &rewriterImpl,
- std::optional<DenseMap<Value, SmallVector<Value>>> &inverseMapping);
+ DenseMap<Value, SmallVector<Value>> &inverseMapping);
/// Legalize an operation result that was marked as "erased".
LogicalResult
@@ -2454,10 +2454,12 @@ LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
LogicalResult
OperationConverter::finalize(ConversionPatternRewriter &rewriter) {
- std::optional<DenseMap<Value, SmallVector<Value>>> inverseMapping;
ConversionPatternRewriterImpl &rewriterImpl = rewriter.getImpl();
- if (failed(legalizeConvertedArgumentTypes(rewriter, rewriterImpl)) ||
- failed(legalizeUnresolvedMaterializations(rewriter, rewriterImpl,
+ if (failed(legalizeConvertedArgumentTypes(rewriter, rewriterImpl)))
+ return failure();
+ DenseMap<Value, SmallVector<Value>> inverseMapping =
+ rewriterImpl.mapping.getInverse();
+ if (failed(legalizeUnresolvedMaterializations(rewriter, rewriterImpl,
inverseMapping)))
return failure();
@@ -2483,15 +2485,11 @@ OperationConverter::finalize(ConversionPatternRewriter &rewriter) {
if (result.getType() == newValue.getType())
continue;
- // Compute the inverse mapping only if it is really needed.
- if (!inverseMapping)
- inverseMapping = rewriterImpl.mapping.getInverse();
-
// Legalize this result.
rewriter.setInsertionPoint(op);
if (failed(legalizeChangedResultType(
op, result, newValue, opReplacement->getConverter(), rewriter,
- rewriterImpl, *inverseMapping)))
+ rewriterImpl, inverseMapping)))
return failure();
}
}
@@ -2503,6 +2501,8 @@ LogicalResult OperationConverter::legalizeConvertedArgumentTypes(
ConversionPatternRewriterImpl &rewriterImpl) {
// Functor used to check if all users of a value will be dead after
// conversion.
+ // TODO: This should probably query the inverse mapping, same as in
+ // `legalizeChangedResultType`.
auto findLiveUser = [&](Value val) {
auto liveUserIt = llvm::find_if_not(val.getUsers(), [&](Operation *user) {
return rewriterImpl.isOpIgnored(user);
@@ -2796,20 +2796,18 @@ static LogicalResult legalizeUnresolvedMaterialization(
LogicalResult OperationConverter::legalizeUnresolvedMaterializations(
ConversionPatternRewriter &rewriter,
ConversionPatternRewriterImpl &rewriterImpl,
- std::optional<DenseMap<Value, SmallVector<Value>>> &inverseMapping) {
- inverseMapping = rewriterImpl.mapping.getInverse();
-
+ DenseMap<Value, SmallVector<Value>> &inverseMapping) {
// As an initial step, compute all of the inserted materializations that we
// expect to persist beyond the conversion process.
DenseMap<Operation *, UnresolvedMaterializationRewrite *> materializationOps;
SetVector<UnresolvedMaterializationRewrite *> necessaryMaterializations;
computeNecessaryMaterializations(materializationOps, rewriter, rewriterImpl,
- *inverseMapping, necessaryMaterializations);
+ inverseMapping, necessaryMaterializations);
// Once computed, legalize any necessary materializations.
for (auto *mat : necessaryMaterializations) {
if (failed(legalizeUnresolvedMaterialization(
- *mat, materializationOps, rewriter, rewriterImpl, *inverseMapping)))
+ *mat, materializationOps, rewriter, rewriterImpl, inverseMapping)))
return failure();
}
return success();
More information about the Mlir-commits
mailing list