Skip to content

[mlir][Transforms][NFC] Dialect Conversion: Remove redundant lookupOrDefault #110370

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
Sep 28, 2024

Conversation

matthias-springer
Copy link
Member

Remove a redundant lookupOrDefault that has no effect.

When no type is passed to lookupOrDefault, that function returns the furthest mapped value (by following the mapping iteratively). If there is no mapped value with the desired type, then the function also returns the furthest mapped value.

The value that was passed to the redundant lookupOrDefault was produced by this code:

Value newOperand = mapping.lookupOrDefault(operand, desiredType);

There are 2 possible cases:

  • Case 1: There is no mapping to desiredType. Then newOperand is the furthest mapped value.
  • Case 2: There is a mapping to desiredType. Then the type of newOperand is desiredType and the "if" branch that encloses the redundant lookupOrDefault is not executed at all.

Also improve the documentation of ConversionValueMapping::lookupOrDefault and simplify the implementation a bit.

…ult`

Remove a redundant `lookupOrDefault` that has no effect.

When no type is passed to `lookupOrDefault`, that function returns the furthest mapped value (by following the mapping iteratively).

The value that was passed to the redundant `lookupOrDefault` was produced by this code:
```
Value newOperand = mapping.lookupOrDefault(operand, desiredType);
```

There are 2 possible cases:
- Case 1: There is no mapping to `desiredType`. Then `newOperand` is the furthest mapped value.
- Case 2: There is a mapping to `desiredType`. Then the type of `newOperand` is `desiredType` and the "if" branch that encloses the redundant `lookupOrDefault` is not executed at all.
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Sep 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2024

@llvm/pr-subscribers-mlir-core

Author: Matthias Springer (matthias-springer)

Changes

Remove a redundant lookupOrDefault that has no effect.

When no type is passed to lookupOrDefault, that function returns the furthest mapped value (by following the mapping iteratively). If there is no mapped value with the desired type, then the function also returns the furthest mapped value.

The value that was passed to the redundant lookupOrDefault was produced by this code:

Value newOperand = mapping.lookupOrDefault(operand, desiredType);

There are 2 possible cases:

  • Case 1: There is no mapping to desiredType. Then newOperand is the furthest mapped value.
  • Case 2: There is a mapping to desiredType. Then the type of newOperand is desiredType and the "if" branch that encloses the redundant lookupOrDefault is not executed at all.

Also improve the documentation of ConversionValueMapping::lookupOrDefault and simplify the implementation a bit.


Full diff: https://github.com/llvm/llvm-project/pull/110370.diff

1 Files Affected:

  • (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+14-16)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 69036e947ebdb0..4693edadfb5eec 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -71,10 +71,16 @@ namespace {
 /// This class wraps a IRMapping to provide recursive lookup
 /// functionality, i.e. we will traverse if the mapped value also has a mapping.
 struct ConversionValueMapping {
-  /// Lookup a mapped value within the map. If a mapping for the provided value
-  /// does not exist then return the provided value. If `desiredType` is
-  /// non-null, returns the most recently mapped value with that type. If an
-  /// operand of that type does not exist, defaults to normal behavior.
+  /// Lookup the most recently mapped value with the desired type in the
+  /// mapping.
+  ///
+  /// Special cases:
+  /// - If the desired type is "null", simply return the most recently mapped
+  ///   value.
+  /// - If there is no mapping to the desired type, also return the most
+  ///   recently mapped value.
+  /// - If there is no mapping for the given value at all, return the given
+  ///   value.
   Value lookupOrDefault(Value from, Type desiredType = nullptr) const;
 
   /// Lookup a mapped value within the map, or return null if a mapping does not
@@ -115,19 +121,11 @@ struct ConversionValueMapping {
 
 Value ConversionValueMapping::lookupOrDefault(Value from,
                                               Type desiredType) const {
-  // If there was no desired type, simply find the leaf value.
-  if (!desiredType) {
-    // If this value had a valid mapping, unmap that value as well in the case
-    // that it was also replaced.
-    while (auto mappedValue = mapping.lookupOrNull(from))
-      from = mappedValue;
-    return from;
-  }
-
-  // Otherwise, try to find the deepest value that has the desired type.
+  // Try to find the deepest value that has the desired type. If there is no
+  // such value, simply return the deepest value.
   Value desiredValue;
   do {
-    if (from.getType() == desiredType)
+    if (!desiredType || from.getType() == desiredType)
       desiredValue = from;
 
     Value mappedValue = mapping.lookupOrNull(from);
@@ -1136,7 +1134,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
           MaterializationKind::Target, computeInsertPoint(newOperand),
           operandLoc, /*inputs=*/newOperand, /*outputType=*/desiredType,
           currentTypeConverter);
-      mapping.map(mapping.lookupOrDefault(newOperand), castValue);
+      mapping.map(newOperand, castValue);
       newOperand = castValue;
     }
     remapped.push_back(newOperand);

@llvmbot
Copy link
Member

llvmbot commented Sep 28, 2024

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

Changes

Remove a redundant lookupOrDefault that has no effect.

When no type is passed to lookupOrDefault, that function returns the furthest mapped value (by following the mapping iteratively). If there is no mapped value with the desired type, then the function also returns the furthest mapped value.

The value that was passed to the redundant lookupOrDefault was produced by this code:

Value newOperand = mapping.lookupOrDefault(operand, desiredType);

There are 2 possible cases:

  • Case 1: There is no mapping to desiredType. Then newOperand is the furthest mapped value.
  • Case 2: There is a mapping to desiredType. Then the type of newOperand is desiredType and the "if" branch that encloses the redundant lookupOrDefault is not executed at all.

Also improve the documentation of ConversionValueMapping::lookupOrDefault and simplify the implementation a bit.


Full diff: https://github.com/llvm/llvm-project/pull/110370.diff

1 Files Affected:

  • (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+14-16)
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 69036e947ebdb0..4693edadfb5eec 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -71,10 +71,16 @@ namespace {
 /// This class wraps a IRMapping to provide recursive lookup
 /// functionality, i.e. we will traverse if the mapped value also has a mapping.
 struct ConversionValueMapping {
-  /// Lookup a mapped value within the map. If a mapping for the provided value
-  /// does not exist then return the provided value. If `desiredType` is
-  /// non-null, returns the most recently mapped value with that type. If an
-  /// operand of that type does not exist, defaults to normal behavior.
+  /// Lookup the most recently mapped value with the desired type in the
+  /// mapping.
+  ///
+  /// Special cases:
+  /// - If the desired type is "null", simply return the most recently mapped
+  ///   value.
+  /// - If there is no mapping to the desired type, also return the most
+  ///   recently mapped value.
+  /// - If there is no mapping for the given value at all, return the given
+  ///   value.
   Value lookupOrDefault(Value from, Type desiredType = nullptr) const;
 
   /// Lookup a mapped value within the map, or return null if a mapping does not
@@ -115,19 +121,11 @@ struct ConversionValueMapping {
 
 Value ConversionValueMapping::lookupOrDefault(Value from,
                                               Type desiredType) const {
-  // If there was no desired type, simply find the leaf value.
-  if (!desiredType) {
-    // If this value had a valid mapping, unmap that value as well in the case
-    // that it was also replaced.
-    while (auto mappedValue = mapping.lookupOrNull(from))
-      from = mappedValue;
-    return from;
-  }
-
-  // Otherwise, try to find the deepest value that has the desired type.
+  // Try to find the deepest value that has the desired type. If there is no
+  // such value, simply return the deepest value.
   Value desiredValue;
   do {
-    if (from.getType() == desiredType)
+    if (!desiredType || from.getType() == desiredType)
       desiredValue = from;
 
     Value mappedValue = mapping.lookupOrNull(from);
@@ -1136,7 +1134,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
           MaterializationKind::Target, computeInsertPoint(newOperand),
           operandLoc, /*inputs=*/newOperand, /*outputType=*/desiredType,
           currentTypeConverter);
-      mapping.map(mapping.lookupOrDefault(newOperand), castValue);
+      mapping.map(newOperand, castValue);
       newOperand = castValue;
     }
     remapped.push_back(newOperand);

@matthias-springer matthias-springer merged commit fcde4f6 into main Sep 28, 2024
11 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/redundant_lookup branch September 28, 2024 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants