Skip to content

Commit fcde4f6

Browse files
[mlir][Transforms][NFC] Dialect Conversion: Remove redundant lookupOrDefault (#110370)
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.
1 parent 3a5b9da commit fcde4f6

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ namespace {
7171
/// This class wraps a IRMapping to provide recursive lookup
7272
/// functionality, i.e. we will traverse if the mapped value also has a mapping.
7373
struct ConversionValueMapping {
74-
/// Lookup a mapped value within the map. If a mapping for the provided value
75-
/// does not exist then return the provided value. If `desiredType` is
76-
/// non-null, returns the most recently mapped value with that type. If an
77-
/// operand of that type does not exist, defaults to normal behavior.
74+
/// Lookup the most recently mapped value with the desired type in the
75+
/// mapping.
76+
///
77+
/// Special cases:
78+
/// - If the desired type is "null", simply return the most recently mapped
79+
/// value.
80+
/// - If there is no mapping to the desired type, also return the most
81+
/// recently mapped value.
82+
/// - If there is no mapping for the given value at all, return the given
83+
/// value.
7884
Value lookupOrDefault(Value from, Type desiredType = nullptr) const;
7985

8086
/// Lookup a mapped value within the map, or return null if a mapping does not
@@ -115,19 +121,11 @@ struct ConversionValueMapping {
115121

116122
Value ConversionValueMapping::lookupOrDefault(Value from,
117123
Type desiredType) const {
118-
// If there was no desired type, simply find the leaf value.
119-
if (!desiredType) {
120-
// If this value had a valid mapping, unmap that value as well in the case
121-
// that it was also replaced.
122-
while (auto mappedValue = mapping.lookupOrNull(from))
123-
from = mappedValue;
124-
return from;
125-
}
126-
127-
// Otherwise, try to find the deepest value that has the desired type.
124+
// Try to find the deepest value that has the desired type. If there is no
125+
// such value, simply return the deepest value.
128126
Value desiredValue;
129127
do {
130-
if (from.getType() == desiredType)
128+
if (!desiredType || from.getType() == desiredType)
131129
desiredValue = from;
132130

133131
Value mappedValue = mapping.lookupOrNull(from);
@@ -1136,7 +1134,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
11361134
MaterializationKind::Target, computeInsertPoint(newOperand),
11371135
operandLoc, /*inputs=*/newOperand, /*outputType=*/desiredType,
11381136
currentTypeConverter);
1139-
mapping.map(mapping.lookupOrDefault(newOperand), castValue);
1137+
mapping.map(newOperand, castValue);
11401138
newOperand = castValue;
11411139
}
11421140
remapped.push_back(newOperand);

0 commit comments

Comments
 (0)