Skip to content

Commit 8e9b48a

Browse files
address comments
1 parent 2efb6d7 commit 8e9b48a

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ namespace {
137137
struct ValueVectorMapInfo {
138138
static ValueVector getEmptyKey() { return ValueVector{}; }
139139
static ValueVector getTombstoneKey() { return ValueVector{}; }
140-
static ::llvm::hash_code getHashValue(ValueVector val) {
140+
static ::llvm::hash_code getHashValue(const ValueVector &val) {
141141
return ::llvm::hash_combine_range(val.begin(), val.end());
142142
}
143-
static bool isEqual(ValueVector LHS, ValueVector RHS) { return LHS == RHS; }
143+
static bool isEqual(const ValueVector &LHS, const ValueVector &RHS) {
144+
return LHS == RHS;
145+
}
144146
};
145147

146148
/// This class wraps a IRMapping to provide recursive lookup
@@ -159,20 +161,18 @@ struct ConversionValueMapping {
159161
/// - If there is no mapping to the desired types, also return the most
160162
/// recently mapped values.
161163
/// - If there is no mapping for the given values at all, return the given
162-
/// values.
163-
ValueVector lookupOrDefault(ValueVector from,
164-
TypeRange desiredTypes = {}) const;
164+
/// value.
165+
ValueVector lookupOrDefault(Value from, TypeRange desiredTypes = {}) const;
165166

166-
/// Lookup the given values within the map, or return an empty vector if the
167-
/// values are not mapped. If they are mapped, this follows the same behavior
167+
/// Lookup the given value within the map, or return an empty vector if the
168+
/// value is not mapped. If it is mapped, this follows the same behavior
168169
/// as `lookupOrDefault`.
169-
ValueVector lookupOrNull(const ValueVector &from,
170-
TypeRange desiredTypes = {}) const;
170+
ValueVector lookupOrNull(Value from, TypeRange desiredTypes = {}) const;
171171

172172
template <typename T>
173173
struct IsValueVector : std::is_same<std::decay_t<T>, ValueVector> {};
174174

175-
/// Map a value to the one provided.
175+
/// Map a value vector to the one provided.
176176
template <typename OldVal, typename NewVal>
177177
std::enable_if_t<IsValueVector<OldVal>{} && IsValueVector<NewVal>{}>
178178
map(OldVal &&oldVal, NewVal &&newVal) {
@@ -192,6 +192,7 @@ struct ConversionValueMapping {
192192
mapping[std::forward<OldVal>(oldVal)] = std::forward<NewVal>(newVal);
193193
}
194194

195+
/// Map a value vector or single value to the one provided.
195196
template <typename OldVal, typename NewVal>
196197
std::enable_if_t<!IsValueVector<OldVal>{} || !IsValueVector<NewVal>{}>
197198
map(OldVal &&oldVal, NewVal &&newVal) {
@@ -217,53 +218,56 @@ struct ConversionValueMapping {
217218
} // namespace
218219

219220
ValueVector
220-
ConversionValueMapping::lookupOrDefault(ValueVector from,
221+
ConversionValueMapping::lookupOrDefault(Value from,
221222
TypeRange desiredTypes) const {
222223
// Try to find the deepest values that have the desired types. If there is no
223224
// such mapping, simply return the deepest values.
224225
ValueVector desiredValue;
226+
ValueVector current{from};
225227
do {
226228
// Store the current value if the types match.
227-
if (desiredTypes.empty() || TypeRange(from) == desiredTypes)
228-
desiredValue = from;
229+
if (TypeRange(current) == desiredTypes)
230+
desiredValue = current;
229231

230232
// If possible, Replace each value with (one or multiple) mapped values.
231233
ValueVector next;
232-
for (Value v : from) {
234+
for (Value v : current) {
233235
auto it = mapping.find({v});
234236
if (it != mapping.end()) {
235237
llvm::append_range(next, it->second);
236238
} else {
237239
next.push_back(v);
238240
}
239241
}
240-
if (next != from) {
242+
if (next != current) {
241243
// If at least one value was replaced, continue the lookup from there.
242-
from = std::move(next);
244+
current = std::move(next);
243245
continue;
244246
}
245247

246248
// Otherwise: Check if there is a mapping for the entire vector. Such
247249
// mappings are materializations. (N:M mapping are not supported for value
248250
// replacements.)
249-
auto it = mapping.find(from);
251+
auto it = mapping.find(current);
250252
if (it == mapping.end()) {
251253
// No mapping found: The lookup stops here.
252254
break;
253255
}
254-
from = it->second;
256+
current = it->second;
255257
} while (true);
256258

257259
// If the desired values were found use them, otherwise default to the leaf
258260
// values.
259-
return !desiredValue.empty() ? desiredValue : from;
261+
// Note: If `desiredTypes` is empty, this function always returns `current`.
262+
return !desiredValue.empty() ? desiredValue : current;
260263
}
261264

262-
ValueVector ConversionValueMapping::lookupOrNull(const ValueVector &from,
265+
ValueVector ConversionValueMapping::lookupOrNull(Value from,
263266
TypeRange desiredTypes) const {
264267
ValueVector result = lookupOrDefault(from, desiredTypes);
265268
TypeRange resultTypes(result);
266-
if (result == from || (!desiredTypes.empty() && resultTypes != desiredTypes))
269+
if (result == ValueVector{from} ||
270+
(!desiredTypes.empty() && resultTypes != desiredTypes))
267271
return {};
268272
return result;
269273
}
@@ -1261,7 +1265,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
12611265
// The current pattern does not have a type converter. I.e., it does not
12621266
// distinguish between legal and illegal types. For each operand, simply
12631267
// pass through the most recently mapped values.
1264-
remapped.push_back(mapping.lookupOrDefault({operand}));
1268+
remapped.push_back(mapping.lookupOrDefault(operand));
12651269
continue;
12661270
}
12671271

@@ -1280,7 +1284,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
12801284
continue;
12811285
}
12821286

1283-
ValueVector repl = mapping.lookupOrDefault({operand}, legalTypes);
1287+
ValueVector repl = mapping.lookupOrDefault(operand, legalTypes);
12841288
if (!repl.empty() && TypeRange(repl) == legalTypes) {
12851289
// Mapped values have the correct type or there is an existing
12861290
// materialization. Or the operand is not mapped at all and has the
@@ -1290,7 +1294,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
12901294
}
12911295

12921296
// Create a materialization for the most recently mapped values.
1293-
repl = mapping.lookupOrDefault({operand});
1297+
repl = mapping.lookupOrDefault(operand);
12941298
ValueRange castValues = buildUnresolvedMaterialization(
12951299
MaterializationKind::Target, computeInsertPoint(repl), operandLoc,
12961300
/*valuesToMap=*/repl, /*inputs=*/repl, /*outputTypes=*/legalTypes,
@@ -1428,10 +1432,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
14281432
continue;
14291433
}
14301434

1431-
// This is a 1->1+ mapping. 1->N mappings are not fully supported in the
1432-
// dialect conversion. Therefore, we need an argument materialization to
1433-
// turn the replacement block arguments into a single SSA value that can be
1434-
// used as a replacement.
1435+
// This is a 1->1+ mapping.
14351436
auto replArgs =
14361437
newBlock->getArguments().slice(inputMap->inputNo, inputMap->size);
14371438
ValueVector replArgVals = llvm::to_vector_of<Value, 1>(replArgs);
@@ -1487,7 +1488,7 @@ ValueRange ConversionPatternRewriterImpl::buildUnresolvedMaterialization(
14871488
Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
14881489
Value value, const TypeConverter *converter) {
14891490
// Find a replacement value with the same type.
1490-
ValueVector repl = mapping.lookupOrNull({value}, value.getType());
1491+
ValueVector repl = mapping.lookupOrNull(value, value.getType());
14911492
if (!repl.empty())
14921493
return repl.front();
14931494

@@ -1503,7 +1504,7 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
15031504
// No replacement value was found. Get the latest replacement value
15041505
// (regardless of the type) and build a source materialization to the
15051506
// original type.
1506-
repl = mapping.lookupOrNull({value});
1507+
repl = mapping.lookupOrNull(value);
15071508
if (repl.empty()) {
15081509
// No replacement value is registered in the mapping. This means that the
15091510
// value is dropped and no longer needed. (If the value were still needed,
@@ -1742,7 +1743,7 @@ void ConversionPatternRewriter::replaceUsesOfBlockArgument(BlockArgument from,
17421743
});
17431744
impl->appendRewrite<ReplaceBlockArgRewrite>(from.getOwner(), from,
17441745
impl->currentTypeConverter);
1745-
impl->mapping.map(impl->mapping.lookupOrDefault({from}), to);
1746+
impl->mapping.map(impl->mapping.lookupOrDefault(from), to);
17461747
}
17471748

17481749
Value ConversionPatternRewriter::getRemappedValue(Value key) {

0 commit comments

Comments
 (0)