[Mlir-commits] [mlir] f09a28e - [mlir][Transforms][NFC] Dialect conversion: Eagerly build reverse mapping (#101476)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Aug 8 22:51:33 PDT 2024
Author: Matthias Springer
Date: 2024-08-09T07:51:30+02:00
New Revision: f09a28e66cfc5815dbac3e42944b5d0432f970bc
URL: https://github.com/llvm/llvm-project/commit/f09a28e66cfc5815dbac3e42944b5d0432f970bc
DIFF: https://github.com/llvm/llvm-project/commit/f09a28e66cfc5815dbac3e42944b5d0432f970bc.diff
LOG: [mlir][Transforms][NFC] Dialect conversion: Eagerly build reverse mapping (#101476)
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`.
Depends on #98805
Added:
Modified:
mlir/lib/Transforms/Utils/DialectConversion.cpp
Removed:
################################################################################
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