Skip to content

Commit 5ec6ac8

Browse files
[mlir][Transforms][NFC] Dialect Conversion: Move lookup functions (#150743)
Add `lookupOrDefault` / `lookupOrNull` wrappers to `ConversionPatternRewriterImpl` and call those wrappers throughout the code base. This commit is in preparation of the One-Shot Dialect Conversion refactoring. In future, the implementation will bypass the `mapping` when rollback is disabled. The switch will be made in those wrapper functions.
1 parent cccde9b commit 5ec6ac8

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ struct ConversionValueMapping {
131131
/// value.
132132
ValueVector lookupOrDefault(Value from, TypeRange desiredTypes = {}) const;
133133

134-
/// Lookup the given value within the map, or return an empty vector if the
135-
/// value is not mapped. If it is mapped, this follows the same behavior
136-
/// as `lookupOrDefault`.
137-
ValueVector lookupOrNull(Value from, TypeRange desiredTypes = {}) const;
138-
139134
template <typename T>
140135
struct IsValueVector : std::is_same<std::decay_t<T>, ValueVector> {};
141136

@@ -238,15 +233,6 @@ ConversionValueMapping::lookupOrDefault(Value from,
238233
return !desiredValue.empty() ? std::move(desiredValue) : std::move(current);
239234
}
240235

241-
ValueVector ConversionValueMapping::lookupOrNull(Value from,
242-
TypeRange desiredTypes) const {
243-
ValueVector result = lookupOrDefault(from, desiredTypes);
244-
if (result == ValueVector{from} ||
245-
(!desiredTypes.empty() && TypeRange(ValueRange(result)) != desiredTypes))
246-
return {};
247-
return result;
248-
}
249-
250236
//===----------------------------------------------------------------------===//
251237
// Rewriter and Translation State
252238
//===----------------------------------------------------------------------===//
@@ -927,6 +913,23 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
927913
/// Return "true" if the given operation was replaced or erased.
928914
bool wasOpReplaced(Operation *op) const;
929915

916+
/// Lookup the most recently mapped values with the desired types in the
917+
/// mapping.
918+
///
919+
/// Special cases:
920+
/// - If the desired type range is empty, simply return the most recently
921+
/// mapped values.
922+
/// - If there is no mapping to the desired types, also return the most
923+
/// recently mapped values.
924+
/// - If there is no mapping for the given values at all, return the given
925+
/// value.
926+
ValueVector lookupOrDefault(Value from, TypeRange desiredTypes = {}) const;
927+
928+
/// Lookup the given value within the map, or return an empty vector if the
929+
/// value is not mapped. If it is mapped, this follows the same behavior
930+
/// as `lookupOrDefault`.
931+
ValueVector lookupOrNull(Value from, TypeRange desiredTypes = {}) const;
932+
930933
//===--------------------------------------------------------------------===//
931934
// IR Rewrites / Type Conversion
932935
//===--------------------------------------------------------------------===//
@@ -1249,6 +1252,22 @@ void ConversionPatternRewriterImpl::applyRewrites() {
12491252
// State Management
12501253
//===----------------------------------------------------------------------===//
12511254

1255+
ValueVector
1256+
ConversionPatternRewriterImpl::lookupOrDefault(Value from,
1257+
TypeRange desiredTypes) const {
1258+
return mapping.lookupOrDefault(from, desiredTypes);
1259+
}
1260+
1261+
ValueVector
1262+
ConversionPatternRewriterImpl::lookupOrNull(Value from,
1263+
TypeRange desiredTypes) const {
1264+
ValueVector result = lookupOrDefault(from, desiredTypes);
1265+
if (result == ValueVector{from} ||
1266+
(!desiredTypes.empty() && TypeRange(ValueRange(result)) != desiredTypes))
1267+
return {};
1268+
return result;
1269+
}
1270+
12521271
RewriterState ConversionPatternRewriterImpl::getCurrentState() {
12531272
return RewriterState(rewrites.size(), ignoredOps.size(), replacedOps.size());
12541273
}
@@ -1296,7 +1315,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
12961315
// The current pattern does not have a type converter. I.e., it does not
12971316
// distinguish between legal and illegal types. For each operand, simply
12981317
// pass through the most recently mapped values.
1299-
remapped.push_back(mapping.lookupOrDefault(operand));
1318+
remapped.push_back(lookupOrDefault(operand));
13001319
continue;
13011320
}
13021321

@@ -1315,7 +1334,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
13151334
continue;
13161335
}
13171336

1318-
ValueVector repl = mapping.lookupOrDefault(operand, legalTypes);
1337+
ValueVector repl = lookupOrDefault(operand, legalTypes);
13191338
if (!repl.empty() && TypeRange(ValueRange(repl)) == legalTypes) {
13201339
// Mapped values have the correct type or there is an existing
13211340
// materialization. Or the operand is not mapped at all and has the
@@ -1325,7 +1344,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
13251344
}
13261345

13271346
// Create a materialization for the most recently mapped values.
1328-
repl = mapping.lookupOrDefault(operand);
1347+
repl = lookupOrDefault(operand);
13291348
ValueRange castValues = buildUnresolvedMaterialization(
13301349
MaterializationKind::Target, computeInsertPoint(repl), operandLoc,
13311350
/*valuesToMap=*/repl, /*inputs=*/repl, /*outputTypes=*/legalTypes,
@@ -1520,7 +1539,7 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
15201539
// Try to find a replacement value with the same type in the conversion value
15211540
// mapping. This includes cached materializations. We try to reuse those
15221541
// instead of generating duplicate IR.
1523-
ValueVector repl = mapping.lookupOrNull(value, value.getType());
1542+
ValueVector repl = lookupOrNull(value, value.getType());
15241543
if (!repl.empty())
15251544
return repl.front();
15261545

@@ -1536,7 +1555,7 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
15361555
// No replacement value was found. Get the latest replacement value
15371556
// (regardless of the type) and build a source materialization to the
15381557
// original type.
1539-
repl = mapping.lookupOrNull(value);
1558+
repl = lookupOrNull(value);
15401559
if (repl.empty()) {
15411560
// No replacement value is registered in the mapping. This means that the
15421561
// value is dropped and no longer needed. (If the value were still needed,

0 commit comments

Comments
 (0)